blob: 77deed6047f4766c990f779286ccc83579a7d9a4 [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)) {
185 S.Diag(AL.getLoc(), Diag) << AL.getName() << 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/// A helper function to provide Attribute Name for the Attr types
Erich Keanee891aa92018-07-13 15:07:47 +0000229/// AND the ParsedAttr.
Erich Keane623efd82017-03-30 21:48:55 +0000230template <typename AttrInfo>
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000231static typename std::enable_if<std::is_base_of<Attr, AttrInfo>::value,
Erich Keane623efd82017-03-30 21:48:55 +0000232 const AttrInfo *>::type
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000233getAttrName(const AttrInfo &AL) {
234 return &AL;
Erich Keane623efd82017-03-30 21:48:55 +0000235}
Erich Keanee891aa92018-07-13 15:07:47 +0000236static const IdentifierInfo *getAttrName(const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000237 return AL.getName();
Erich Keane623efd82017-03-30 21:48:55 +0000238}
239
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000240/// If Expr is a valid integer constant, get the value of the integer
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000241/// expression and return success or failure. May output an error.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000242template <typename AttrInfo>
243static bool checkUInt32Argument(Sema &S, const AttrInfo &AI, const Expr *Expr,
Erich Keane623efd82017-03-30 21:48:55 +0000244 uint32_t &Val, unsigned Idx = UINT_MAX) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000245 llvm::APSInt I(32);
246 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
247 !Expr->isIntegerConstantExpr(I, S.Context)) {
248 if (Idx != UINT_MAX)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000249 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
250 << getAttrName(AI) << Idx << AANT_ArgumentIntegerConstant
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000251 << Expr->getSourceRange();
252 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000253 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_type)
254 << getAttrName(AI) << AANT_ArgumentIntegerConstant
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000255 << Expr->getSourceRange();
256 return false;
257 }
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000258
259 if (!I.isIntN(32)) {
Aaron Ballman31f42312014-07-24 14:51:23 +0000260 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
261 << I.toString(10, false) << 32 << /* Unsigned */ 1;
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000262 return false;
263 }
264
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000265 Val = (uint32_t)I.getZExtValue();
266 return true;
267}
268
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000269/// Wrapper around checkUInt32Argument, with an extra check to be sure
George Burgess IVe3763372016-12-22 02:50:20 +0000270/// that the result will fit into a regular (signed) int. All args have the same
271/// purpose as they do in checkUInt32Argument.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000272template <typename AttrInfo>
273static bool checkPositiveIntArgument(Sema &S, const AttrInfo &AI, const Expr *Expr,
Erich Keane623efd82017-03-30 21:48:55 +0000274 int &Val, unsigned Idx = UINT_MAX) {
George Burgess IVe3763372016-12-22 02:50:20 +0000275 uint32_t UVal;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000276 if (!checkUInt32Argument(S, AI, Expr, UVal, Idx))
George Burgess IVe3763372016-12-22 02:50:20 +0000277 return false;
278
George Burgess IVa8049572016-12-22 19:00:31 +0000279 if (UVal > (uint32_t)std::numeric_limits<int>::max()) {
George Burgess IVe3763372016-12-22 02:50:20 +0000280 llvm::APSInt I(32); // for toString
281 I = UVal;
282 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
283 << I.toString(10, false) << 32 << /* Unsigned */ 0;
284 return false;
285 }
286
287 Val = UVal;
288 return true;
289}
290
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000291/// Diagnose mutually exclusive attributes when present on a given
Aaron Ballmanfb763042013-12-02 18:05:46 +0000292/// declaration. Returns true if diagnosed.
293template <typename AttrTy>
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +0000294static bool checkAttrMutualExclusion(Sema &S, Decl *D, SourceRange Range,
295 IdentifierInfo *Ident) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000296 if (const auto *A = D->getAttr<AttrTy>()) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +0000297 S.Diag(Range.getBegin(), diag::err_attributes_are_not_compatible) << Ident
298 << A;
299 S.Diag(A->getLocation(), diag::note_conflicting_attribute);
Aaron Ballmanfb763042013-12-02 18:05:46 +0000300 return true;
301 }
302 return false;
303}
304
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000305/// Check if IdxExpr is a valid parameter index for a function or
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000306/// instance method D. May output an error.
307///
308/// \returns true if IdxExpr is a valid index.
Erich Keane623efd82017-03-30 21:48:55 +0000309template <typename AttrInfo>
310static bool checkFunctionOrMethodParameterIndex(
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000311 Sema &S, const Decl *D, const AttrInfo &AI, unsigned AttrArgNum,
Joel E. Denny81508102018-03-13 14:51:22 +0000312 const Expr *IdxExpr, ParamIdx &Idx, bool CanIndexImplicitThis = false) {
David Majnemer06864812015-04-07 06:01:53 +0000313 assert(isFunctionOrMethodOrBlock(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000314
315 // In C++ the implicit 'this' function parameter also counts.
316 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000317 bool HP = hasFunctionProto(D);
318 bool HasImplicitThisParam = isInstanceMethod(D);
319 bool IV = HP && isFunctionOrMethodVariadic(D);
Alp Toker601b22c2014-01-21 23:35:24 +0000320 unsigned NumParams =
321 (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000322
323 llvm::APSInt IdxInt;
324 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
325 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000326 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
327 << getAttrName(AI) << AttrArgNum << AANT_ArgumentIntegerConstant
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000328 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000329 return false;
330 }
331
Joel E. Denny81508102018-03-13 14:51:22 +0000332 unsigned IdxSource = IdxInt.getLimitedValue(UINT_MAX);
333 if (IdxSource < 1 || (!IV && IdxSource > NumParams)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000334 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_out_of_bounds)
Joel E. Denny81508102018-03-13 14:51:22 +0000335 << getAttrName(AI) << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000336 return false;
337 }
Joel E. Denny81508102018-03-13 14:51:22 +0000338 if (HasImplicitThisParam && !CanIndexImplicitThis) {
339 if (IdxSource == 1) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000340 S.Diag(getAttrLoc(AI),
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000341 diag::err_attribute_invalid_implicit_this_argument)
Joel E. Denny81508102018-03-13 14:51:22 +0000342 << getAttrName(AI) << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000343 return false;
344 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000345 }
346
Joel E. Denny81508102018-03-13 14:51:22 +0000347 Idx = ParamIdx(IdxSource, D);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000348 return true;
349}
350
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000351/// Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000352/// If not emit an error and return false. If the argument is an identifier it
353/// will emit an error with a fixit hint and treat it as if it was a string
354/// literal.
Erich Keanee891aa92018-07-13 15:07:47 +0000355bool Sema::checkStringLiteralArgumentAttr(const ParsedAttr &AL, unsigned ArgNum,
356 StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000357 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000358 // Look for identifiers. If we have one emit a hint to fix it to a literal.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000359 if (AL.isArgIdent(ArgNum)) {
360 IdentifierLoc *Loc = AL.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000361 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000362 << AL.getName() << AANT_ArgumentString
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000363 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Craig Topper07fa1762015-11-15 02:31:46 +0000364 << FixItHint::CreateInsertion(getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000365 Str = Loc->Ident->getName();
366 if (ArgLocation)
367 *ArgLocation = Loc->Loc;
368 return true;
369 }
370
371 // Now check for an actual string literal.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000372 Expr *ArgExpr = AL.getArgAsExpr(ArgNum);
373 const auto *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000374 if (ArgLocation)
375 *ArgLocation = ArgExpr->getLocStart();
376
377 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000378 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000379 << AL.getName() << AANT_ArgumentString;
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000380 return false;
381 }
382
383 Str = Literal->getString();
384 return true;
385}
386
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000387/// Applies the given attribute to the Decl without performing any
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000388/// additional semantic checking.
389template <typename AttrType>
Erich Keanee891aa92018-07-13 15:07:47 +0000390static void handleSimpleAttribute(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000391 D->addAttr(::new (S.Context) AttrType(AL.getRange(), S.Context,
392 AL.getAttributeSpellingListIndex()));
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000393}
394
Justin Lebar3eaaf862016-01-13 01:07:35 +0000395template <typename AttrType>
396static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000397 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000398 handleSimpleAttribute<AttrType>(S, D, AL);
Justin Lebar3eaaf862016-01-13 01:07:35 +0000399}
400
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000401/// Applies the given attribute to the Decl so long as the Decl doesn't
Justin Lebar3eaaf862016-01-13 01:07:35 +0000402/// already have one of the given incompatible attributes.
403template <typename AttrType, typename IncompatibleAttrType,
404 typename... IncompatibleAttrTypes>
405static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000406 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000407 if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL.getRange(),
408 AL.getName()))
Justin Lebar3eaaf862016-01-13 01:07:35 +0000409 return;
410 handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000411 AL);
Justin Lebar3eaaf862016-01-13 01:07:35 +0000412}
413
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000414/// Check if the passed-in expression is of type int or bool.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000415static bool isIntOrBool(Expr *Exp) {
416 QualType QT = Exp->getType();
417 return QT->isBooleanType() || QT->isIntegerType();
418}
419
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000420
421// Check to see if the type is a smart pointer of some kind. We assume
422// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000423static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
Richard Smithcf4bdde2015-02-21 02:45:19 +0000424 DeclContextLookupResult Res1 = RT->getDecl()->lookup(
425 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000426 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000427 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000428
Richard Smithcf4bdde2015-02-21 02:45:19 +0000429 DeclContextLookupResult Res2 = RT->getDecl()->lookup(
430 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000431 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000432 return false;
433
434 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000435}
436
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000437/// Check if passed in Decl is a pointer type.
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000438/// Note that this function may produce an error message.
439/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000440static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000441 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000442 const auto *VD = cast<ValueDecl>(D);
443 QualType QT = VD->getType();
Aaron Ballman553e6812013-12-26 14:54:11 +0000444 if (QT->isAnyPointerType())
445 return true;
446
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000447 if (const auto *RT = QT->getAs<RecordType>()) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000448 // If it's an incomplete type, it could be a smart pointer; skip it.
449 // (We don't want to force template instantiation if we can avoid it,
450 // since that would alter the order in which templates are instantiated.)
451 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000452 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000453
Aaron Ballman553e6812013-12-26 14:54:11 +0000454 if (threadSafetyCheckIsSmartPointer(S, RT))
455 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000456 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000457
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000458 S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
459 << AL.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000460 return false;
461}
462
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000463/// Checks that the passed in QualType either is of RecordType or points
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000464/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000465static const RecordType *getRecordType(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000466 if (const auto *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000467 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000468
469 // Now check if we point to record type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000470 if (const auto *PT = QT->getAs<PointerType>())
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000471 return PT->getPointeeType()->getAs<RecordType>();
472
Craig Topperc3ec1492014-05-26 06:22:03 +0000473 return nullptr;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000474}
475
Josh Gao55afa752017-08-11 07:54:35 +0000476static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000477 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000478
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000479 if (!RT)
480 return false;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000481
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000482 // Don't check for the capability if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000483 if (RT->isIncompleteType())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000484 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000485
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000486 // Allow smart pointers to be used as capability objects.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000487 // FIXME -- Check the type that the smart pointer points to.
488 if (threadSafetyCheckIsSmartPointer(S, RT))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000489 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000490
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000491 // Check if the record itself has a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000492 RecordDecl *RD = RT->getDecl();
Josh Gao55afa752017-08-11 07:54:35 +0000493 if (RD->hasAttr<CapabilityAttr>())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000494 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000495
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000496 // Else check if any base classes have a capability.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000497 if (const auto *CRD = dyn_cast<CXXRecordDecl>(RD)) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000498 CXXBasePaths BPaths(false, false);
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000499 if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &) {
500 const auto *Type = BS->getType()->getAs<RecordType>();
Josh Gao55afa752017-08-11 07:54:35 +0000501 return Type->getDecl()->hasAttr<CapabilityAttr>();
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000502 }, BPaths))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000503 return true;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000504 }
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000505 return false;
506}
507
Josh Gao55afa752017-08-11 07:54:35 +0000508static bool checkTypedefTypeForCapability(QualType Ty) {
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000509 const auto *TD = Ty->getAs<TypedefType>();
510 if (!TD)
511 return false;
512
513 TypedefNameDecl *TN = TD->getDecl();
514 if (!TN)
515 return false;
516
Josh Gao55afa752017-08-11 07:54:35 +0000517 return TN->hasAttr<CapabilityAttr>();
Aaron Ballman76050722014-04-04 15:13:57 +0000518}
519
Josh Gaob40c1772017-08-08 19:44:35 +0000520static bool typeHasCapability(Sema &S, QualType Ty) {
Josh Gao55afa752017-08-11 07:54:35 +0000521 if (checkTypedefTypeForCapability(Ty))
522 return true;
Josh Gaob40c1772017-08-08 19:44:35 +0000523
Josh Gao55afa752017-08-11 07:54:35 +0000524 if (checkRecordTypeForCapability(S, Ty))
525 return true;
526
527 return false;
Josh Gaob40c1772017-08-08 19:44:35 +0000528}
529
Aaron Ballman76050722014-04-04 15:13:57 +0000530static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
531 // Capability expressions are simple expressions involving the boolean logic
532 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
533 // a DeclRefExpr is found, its type should be checked to determine whether it
534 // is a capability or not.
535
Yi Kong2d58d192017-12-14 22:24:45 +0000536 if (const auto *E = dyn_cast<CastExpr>(Ex))
Aaron Ballman76050722014-04-04 15:13:57 +0000537 return isCapabilityExpr(S, E->getSubExpr());
538 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
539 return isCapabilityExpr(S, E->getSubExpr());
540 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
Yi Kong2d58d192017-12-14 22:24:45 +0000541 if (E->getOpcode() == UO_LNot || E->getOpcode() == UO_AddrOf ||
542 E->getOpcode() == UO_Deref)
Aaron Ballman76050722014-04-04 15:13:57 +0000543 return isCapabilityExpr(S, E->getSubExpr());
544 return false;
545 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
546 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
547 return isCapabilityExpr(S, E->getLHS()) &&
548 isCapabilityExpr(S, E->getRHS());
549 return false;
550 }
551
Yi Kong2d58d192017-12-14 22:24:45 +0000552 return typeHasCapability(S, Ex->getType());
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000553}
554
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000555/// Checks that all attribute arguments, starting from Sidx, resolve to
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000556/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000557/// \param Sidx The attribute argument index to start checking with.
558/// \param ParamIdxOk Whether an argument can be indexing into a function
559/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000560static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000561 const ParsedAttr &AL,
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000562 SmallVectorImpl<Expr *> &Args,
563 int Sidx = 0,
564 bool ParamIdxOk = false) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000565 for (unsigned Idx = Sidx; Idx < AL.getNumArgs(); ++Idx) {
566 Expr *ArgExp = AL.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000567
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000568 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000569 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000570 Args.push_back(ArgExp);
571 continue;
572 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000573
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000574 if (const auto *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000575 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000576 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000577 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000578 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000579 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000580 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000581 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000582
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000583 // We allow constant strings to be used as a placeholder for expressions
584 // that are not valid C++ syntax, but warn that they are ignored.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000585 S.Diag(AL.getLoc(), diag::warn_thread_attribute_ignored) << AL.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000586 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000587 continue;
588 }
589
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000590 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000591
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000592 // A pointer to member expression of the form &MyClass::mu is treated
593 // specially -- we need to look at the type of the member.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000594 if (const auto *UOp = dyn_cast<UnaryOperator>(ArgExp))
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000595 if (UOp->getOpcode() == UO_AddrOf)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000596 if (const auto *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000597 if (DRE->getDecl()->isCXXInstanceMember())
598 ArgTy = DRE->getDecl()->getType();
599
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000600 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000601 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000602
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000603 // Now check if we index into a record type function param.
Josh Gao55afa752017-08-11 07:54:35 +0000604 if(!RT && ParamIdxOk) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000605 const auto *FD = dyn_cast<FunctionDecl>(D);
606 const auto *IL = dyn_cast<IntegerLiteral>(ArgExp);
Josh Gao55afa752017-08-11 07:54:35 +0000607 if(FD && IL) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000608 unsigned int NumParams = FD->getNumParams();
609 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000610 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
611 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000612 if (!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
613 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_range)
614 << AL.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000615 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000616 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000617 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000618 }
619 }
620
Aaron Ballman76050722014-04-04 15:13:57 +0000621 // If the type does not have a capability, see if the components of the
622 // expression have capabilities. This allows for writing C code where the
623 // capability may be on the type, and the expression is a capability
624 // boolean logic expression. Eg) requires_capability(A || B && !C)
625 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000626 S.Diag(AL.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
627 << AL.getName() << ArgTy;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000628
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000629 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000630 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000631}
632
Chris Lattner58418ff2008-06-29 00:16:31 +0000633//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000634// Attribute Implementations
635//===----------------------------------------------------------------------===//
636
Erich Keanee891aa92018-07-13 15:07:47 +0000637static void handlePtGuardedVarAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000638 if (!threadSafetyCheckIsPointer(S, D, AL))
Michael Han3be3b442012-07-23 18:48:41 +0000639 return;
640
Michael Han99315932013-01-24 16:46:58 +0000641 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000642 PtGuardedVarAttr(AL.getRange(), S.Context,
643 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000644}
645
Erich Keanee891aa92018-07-13 15:07:47 +0000646static bool checkGuardedByAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000647 Expr *&Arg) {
648 SmallVector<Expr *, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000649 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000650 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000651 unsigned Size = Args.size();
652 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000653 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000654
Michael Han3be3b442012-07-23 18:48:41 +0000655 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000656
Michael Han3be3b442012-07-23 18:48:41 +0000657 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000658}
659
Erich Keanee891aa92018-07-13 15:07:47 +0000660static void handleGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000661 Expr *Arg = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000662 if (!checkGuardedByAttrCommon(S, D, AL, Arg))
Michael Han3be3b442012-07-23 18:48:41 +0000663 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000664
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000665 D->addAttr(::new (S.Context) GuardedByAttr(
666 AL.getRange(), S.Context, Arg, AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000667}
668
Erich Keanee891aa92018-07-13 15:07:47 +0000669static void handlePtGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000670 Expr *Arg = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000671 if (!checkGuardedByAttrCommon(S, D, AL, Arg))
Michael Han3be3b442012-07-23 18:48:41 +0000672 return;
673
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000674 if (!threadSafetyCheckIsPointer(S, D, AL))
Michael Han3be3b442012-07-23 18:48:41 +0000675 return;
676
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000677 D->addAttr(::new (S.Context) PtGuardedByAttr(
678 AL.getRange(), S.Context, Arg, AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000679}
680
Erich Keanee891aa92018-07-13 15:07:47 +0000681static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000682 SmallVectorImpl<Expr *> &Args) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000683 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000684 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000685
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000686 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000687 QualType QT = cast<ValueDecl>(D)->getType();
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000688 if (!QT->isDependentType() && !typeHasCapability(S, QT)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000689 S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
690 << AL.getName();
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000691 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000692 }
693
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000694 // Check that all arguments are lockable objects.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000695 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000696 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000697 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000698
Michael Han3be3b442012-07-23 18:48:41 +0000699 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000700}
701
Erich Keanee891aa92018-07-13 15:07:47 +0000702static void handleAcquiredAfterAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000703 SmallVector<Expr *, 1> Args;
704 if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000705 return;
706
707 Expr **StartArg = &Args[0];
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000708 D->addAttr(::new (S.Context) AcquiredAfterAttr(
709 AL.getRange(), S.Context, StartArg, Args.size(),
710 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000711}
712
Erich Keanee891aa92018-07-13 15:07:47 +0000713static void handleAcquiredBeforeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000714 SmallVector<Expr *, 1> Args;
715 if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000716 return;
717
718 Expr **StartArg = &Args[0];
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000719 D->addAttr(::new (S.Context) AcquiredBeforeAttr(
720 AL.getRange(), S.Context, StartArg, Args.size(),
721 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000722}
723
Erich Keanee891aa92018-07-13 15:07:47 +0000724static bool checkLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000725 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000726 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000727 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000728 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000729
Michael Han3be3b442012-07-23 18:48:41 +0000730 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000731}
732
Erich Keanee891aa92018-07-13 15:07:47 +0000733static void handleAssertSharedLockAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000734 SmallVector<Expr *, 1> Args;
735 if (!checkLockFunAttrCommon(S, D, AL, Args))
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000736 return;
737
738 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000739 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000740 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000741 AssertSharedLockAttr(AL.getRange(), S.Context, StartArg, Size,
742 AL.getAttributeSpellingListIndex()));
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000743}
744
745static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +0000746 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000747 SmallVector<Expr *, 1> Args;
748 if (!checkLockFunAttrCommon(S, D, AL, Args))
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000749 return;
750
751 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000752 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000753 D->addAttr(::new (S.Context) AssertExclusiveLockAttr(
754 AL.getRange(), S.Context, StartArg, Size,
755 AL.getAttributeSpellingListIndex()));
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000756}
757
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000758/// Checks to be sure that the given parameter number is in bounds, and
Aaron Ballman836684a2018-02-25 20:40:06 +0000759/// is an integral type. Will emit appropriate diagnostics if this returns
George Burgess IVe3763372016-12-22 02:50:20 +0000760/// false.
761///
Aaron Ballman836684a2018-02-25 20:40:06 +0000762/// AttrArgNo is used to actually retrieve the argument, so it's base-0.
Erich Keane623efd82017-03-30 21:48:55 +0000763template <typename AttrInfo>
764static bool checkParamIsIntegerType(Sema &S, const FunctionDecl *FD,
Joel E. Denny81508102018-03-13 14:51:22 +0000765 const AttrInfo &AI, unsigned AttrArgNo) {
Aaron Ballman836684a2018-02-25 20:40:06 +0000766 assert(AI.isArgExpr(AttrArgNo) && "Expected expression argument");
767 Expr *AttrArg = AI.getArgAsExpr(AttrArgNo);
Joel E. Denny81508102018-03-13 14:51:22 +0000768 ParamIdx Idx;
Aaron Ballman836684a2018-02-25 20:40:06 +0000769 if (!checkFunctionOrMethodParameterIndex(S, FD, AI, AttrArgNo + 1, AttrArg,
Erich Keane623efd82017-03-30 21:48:55 +0000770 Idx))
771 return false;
772
Joel E. Denny81508102018-03-13 14:51:22 +0000773 const ParmVarDecl *Param = FD->getParamDecl(Idx.getASTIndex());
Erich Keane623efd82017-03-30 21:48:55 +0000774 if (!Param->getType()->isIntegerType() && !Param->getType()->isCharType()) {
775 SourceLocation SrcLoc = AttrArg->getLocStart();
776 S.Diag(SrcLoc, diag::err_attribute_integers_only)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000777 << getAttrName(AI) << Param->getSourceRange();
Erich Keane623efd82017-03-30 21:48:55 +0000778 return false;
779 }
780 return true;
781}
782
Erich Keanee891aa92018-07-13 15:07:47 +0000783static void handleAllocSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000784 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
785 !checkAttributeAtMostNumArgs(S, AL, 2))
George Burgess IVe3763372016-12-22 02:50:20 +0000786 return;
787
788 const auto *FD = cast<FunctionDecl>(D);
789 if (!FD->getReturnType()->isPointerType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000790 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
791 << AL.getName();
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)
828 << AL.getName() << 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)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000910 S.Diag(AL.getLoc(), diag::err_attr_cond_never_constant_expr)
911 << AL.getName();
George Burgess IV0d546532016-11-10 21:47:12 +0000912 for (const PartialDiagnosticAt &PDiag : Diags)
913 S.Diag(PDiag.first, PDiag.second);
George Burgess IV177399e2017-01-09 04:12:14 +0000914 return false;
915 }
916 return true;
917}
918
Erich Keanee891aa92018-07-13 15:07:47 +0000919static void handleEnableIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000920 S.Diag(AL.getLoc(), diag::ext_clang_enable_if);
George Burgess IV177399e2017-01-09 04:12:14 +0000921
922 Expr *Cond;
923 StringRef Msg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000924 if (checkFunctionConditionAttr(S, D, AL, Cond, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +0000925 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000926 EnableIfAttr(AL.getRange(), S.Context, Cond, Msg,
927 AL.getAttributeSpellingListIndex()));
George Burgess IV177399e2017-01-09 04:12:14 +0000928}
929
930namespace {
931/// Determines if a given Expr references any of the given function's
932/// ParmVarDecls, or the function's implicit `this` parameter (if applicable).
933class ArgumentDependenceChecker
934 : public RecursiveASTVisitor<ArgumentDependenceChecker> {
935#ifndef NDEBUG
936 const CXXRecordDecl *ClassType;
937#endif
938 llvm::SmallPtrSet<const ParmVarDecl *, 16> Parms;
939 bool Result;
940
941public:
942 ArgumentDependenceChecker(const FunctionDecl *FD) {
943#ifndef NDEBUG
944 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
945 ClassType = MD->getParent();
946 else
947 ClassType = nullptr;
948#endif
949 Parms.insert(FD->param_begin(), FD->param_end());
950 }
951
952 bool referencesArgs(Expr *E) {
953 Result = false;
954 TraverseStmt(E);
955 return Result;
956 }
957
958 bool VisitCXXThisExpr(CXXThisExpr *E) {
959 assert(E->getType()->getPointeeCXXRecordDecl() == ClassType &&
960 "`this` doesn't refer to the enclosing class?");
961 Result = true;
962 return false;
963 }
964
965 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
966 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
967 if (Parms.count(PVD)) {
968 Result = true;
969 return false;
970 }
971 return true;
972 }
973};
974}
975
Erich Keanee891aa92018-07-13 15:07:47 +0000976static void handleDiagnoseIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000977 S.Diag(AL.getLoc(), diag::ext_clang_diagnose_if);
George Burgess IV177399e2017-01-09 04:12:14 +0000978
979 Expr *Cond;
980 StringRef Msg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000981 if (!checkFunctionConditionAttr(S, D, AL, Cond, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +0000982 return;
983
984 StringRef DiagTypeStr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000985 if (!S.checkStringLiteralArgumentAttr(AL, 2, DiagTypeStr))
George Burgess IV177399e2017-01-09 04:12:14 +0000986 return;
987
988 DiagnoseIfAttr::DiagnosticType DiagType;
989 if (!DiagnoseIfAttr::ConvertStrToDiagnosticType(DiagTypeStr, DiagType)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000990 S.Diag(AL.getArgAsExpr(2)->getLocStart(),
George Burgess IV177399e2017-01-09 04:12:14 +0000991 diag::err_diagnose_if_invalid_diagnostic_type);
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000992 return;
993 }
994
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +0000995 bool ArgDependent = false;
Argyrios Kyrtzidis5f0c0aa2017-05-24 18:35:01 +0000996 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +0000997 ArgDependent = ArgumentDependenceChecker(FD).referencesArgs(Cond);
George Burgess IV177399e2017-01-09 04:12:14 +0000998 D->addAttr(::new (S.Context) DiagnoseIfAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000999 AL.getRange(), S.Context, Cond, Msg, DiagType, ArgDependent,
1000 cast<NamedDecl>(D), AL.getAttributeSpellingListIndex()));
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001001}
1002
Erich Keanee891aa92018-07-13 15:07:47 +00001003static void handlePassObjectSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001004 if (D->hasAttr<PassObjectSizeAttr>()) {
1005 S.Diag(D->getLocStart(), diag::err_attribute_only_once_per_parameter)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001006 << AL.getName();
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001007 return;
1008 }
1009
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001010 Expr *E = AL.getArgAsExpr(0);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001011 uint32_t Type;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001012 if (!checkUInt32Argument(S, AL, E, Type, /*Idx=*/1))
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001013 return;
1014
1015 // pass_object_size's argument is passed in as the second argument of
1016 // __builtin_object_size. So, it has the same constraints as that second
1017 // argument; namely, it must be in the range [0, 3].
1018 if (Type > 3) {
1019 S.Diag(E->getLocStart(), diag::err_attribute_argument_outof_range)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001020 << AL.getName() << 0 << 3 << E->getSourceRange();
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001021 return;
1022 }
1023
1024 // pass_object_size is only supported on constant pointer parameters; as a
1025 // kindness to users, we allow the parameter to be non-const for declarations.
1026 // At this point, we have no clue if `D` belongs to a function declaration or
1027 // definition, so we defer the constness check until later.
1028 if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) {
1029 S.Diag(D->getLocStart(), diag::err_attribute_pointers_only)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001030 << AL.getName() << 1;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001031 return;
1032 }
1033
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001034 D->addAttr(::new (S.Context) PassObjectSizeAttr(
1035 AL.getRange(), S.Context, (int)Type, AL.getAttributeSpellingListIndex()));
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001036}
1037
Erich Keanee891aa92018-07-13 15:07:47 +00001038static void handleConsumableAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Blaikie16f76d22013-09-06 01:28:43 +00001039 ConsumableAttr::ConsumedState DefaultState;
1040
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001041 if (AL.isArgIdent(0)) {
1042 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00001043 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1044 DefaultState)) {
1045 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001046 << AL.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +00001047 return;
1048 }
David Blaikie16f76d22013-09-06 01:28:43 +00001049 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001050 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1051 << AL.getName() << AANT_ArgumentIdentifier;
David Blaikie16f76d22013-09-06 01:28:43 +00001052 return;
1053 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001054
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001055 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001056 ConsumableAttr(AL.getRange(), S.Context, DefaultState,
1057 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001058}
1059
1060static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
Erich Keanee891aa92018-07-13 15:07:47 +00001061 const ParsedAttr &AL) {
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001062 ASTContext &CurrContext = S.getASTContext();
1063 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001064
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001065 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1066 if (!RD->hasAttr<ConsumableAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001067 S.Diag(AL.getLoc(), diag::warn_attr_on_unconsumable_class) <<
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001068 RD->getNameAsString();
Fangrui Song6907ce22018-07-30 19:24:48 +00001069
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001070 return false;
1071 }
1072 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001073
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001074 return true;
1075}
1076
Erich Keanee891aa92018-07-13 15:07:47 +00001077static void handleCallableWhenAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001078 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001079 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001080
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001081 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001082 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001083
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001084 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001085 for (unsigned ArgIndex = 0; ArgIndex < AL.getNumArgs(); ++ArgIndex) {
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001086 CallableWhenAttr::ConsumedState CallableState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001087
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001088 StringRef StateString;
1089 SourceLocation Loc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001090 if (AL.isArgIdent(ArgIndex)) {
1091 IdentifierLoc *Ident = AL.getArgAsIdent(ArgIndex);
Aaron Ballman55ef1512014-12-19 16:42:04 +00001092 StateString = Ident->Ident->getName();
1093 Loc = Ident->Loc;
1094 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001095 if (!S.checkStringLiteralArgumentAttr(AL, ArgIndex, StateString, &Loc))
Aaron Ballman55ef1512014-12-19 16:42:04 +00001096 return;
1097 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001098
1099 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +00001100 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001101 S.Diag(Loc, diag::warn_attribute_type_not_supported)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001102 << AL.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001103 return;
1104 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001105
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001106 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001107 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001108
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001109 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001110 CallableWhenAttr(AL.getRange(), S.Context, States.data(),
1111 States.size(), AL.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001112}
1113
Erich Keanee891aa92018-07-13 15:07:47 +00001114static void handleParamTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
DeLesley Hutchins69391772013-10-17 23:23:53 +00001115 ParamTypestateAttr::ConsumedState ParamState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001116
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001117 if (AL.isArgIdent(0)) {
1118 IdentifierLoc *Ident = AL.getArgAsIdent(0);
DeLesley Hutchins69391772013-10-17 23:23:53 +00001119 StringRef StateString = Ident->Ident->getName();
1120
1121 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
1122 ParamState)) {
1123 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001124 << AL.getName() << StateString;
DeLesley Hutchins69391772013-10-17 23:23:53 +00001125 return;
1126 }
1127 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001128 S.Diag(AL.getLoc(), diag::err_attribute_argument_type) <<
1129 AL.getName() << AANT_ArgumentIdentifier;
DeLesley Hutchins69391772013-10-17 23:23:53 +00001130 return;
1131 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001132
DeLesley Hutchins69391772013-10-17 23:23:53 +00001133 // FIXME: This check is currently being done in the analysis. It can be
1134 // enabled here only after the parser propagates attributes at
1135 // template specialization definition, not declaration.
1136 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
1137 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1138 //
1139 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001140 // S.Diag(AL.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
DeLesley Hutchins69391772013-10-17 23:23:53 +00001141 // ReturnType.getAsString();
1142 // return;
1143 //}
Fangrui Song6907ce22018-07-30 19:24:48 +00001144
DeLesley Hutchins69391772013-10-17 23:23:53 +00001145 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001146 ParamTypestateAttr(AL.getRange(), S.Context, ParamState,
1147 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins69391772013-10-17 23:23:53 +00001148}
1149
Erich Keanee891aa92018-07-13 15:07:47 +00001150static void handleReturnTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001151 ReturnTypestateAttr::ConsumedState ReturnState;
Fangrui Song6907ce22018-07-30 19:24:48 +00001152
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001153 if (AL.isArgIdent(0)) {
1154 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00001155 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1156 ReturnState)) {
1157 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001158 << AL.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001159 return;
1160 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001161 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001162 S.Diag(AL.getLoc(), diag::err_attribute_argument_type) <<
1163 AL.getName() << AANT_ArgumentIdentifier;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001164 return;
1165 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001166
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001167 // FIXME: This check is currently being done in the analysis. It can be
1168 // enabled here only after the parser propagates attributes at
1169 // template specialization definition, not declaration.
1170 //QualType ReturnType;
1171 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001172 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1173 // ReturnType = Param->getType();
1174 //
1175 //} else if (const CXXConstructorDecl *Constructor =
1176 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001177 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001178 //
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001179 //} else {
Fangrui Song6907ce22018-07-30 19:24:48 +00001180 //
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001181 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1182 //}
1183 //
1184 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1185 //
1186 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1187 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1188 // ReturnType.getAsString();
1189 // return;
1190 //}
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001191
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001192 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001193 ReturnTypestateAttr(AL.getRange(), S.Context, ReturnState,
1194 AL.getAttributeSpellingListIndex()));
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001195}
1196
Erich Keanee891aa92018-07-13 15:07:47 +00001197static void handleSetTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001198 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001199 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001200
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001201 SetTypestateAttr::ConsumedState NewState;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001202 if (AL.isArgIdent(0)) {
1203 IdentifierLoc *Ident = AL.getArgAsIdent(0);
Aaron Ballman91c98e12013-10-14 23:22:37 +00001204 StringRef Param = Ident->Ident->getName();
1205 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1206 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001207 << AL.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001208 return;
1209 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001210 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001211 S.Diag(AL.getLoc(), diag::err_attribute_argument_type) <<
1212 AL.getName() << AANT_ArgumentIdentifier;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001213 return;
1214 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001215
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001216 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001217 SetTypestateAttr(AL.getRange(), S.Context, NewState,
1218 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001219}
1220
Erich Keanee891aa92018-07-13 15:07:47 +00001221static void handleTestTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001222 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001223 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001224
1225 TestTypestateAttr::ConsumedState TestState;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001226 if (AL.isArgIdent(0)) {
1227 IdentifierLoc *Ident = AL.getArgAsIdent(0);
Aaron Ballman91c98e12013-10-14 23:22:37 +00001228 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001229 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001230 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001231 << AL.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001232 return;
1233 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001234 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001235 S.Diag(AL.getLoc(), diag::err_attribute_argument_type) <<
1236 AL.getName() << AANT_ArgumentIdentifier;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001237 return;
1238 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001239
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001240 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001241 TestTypestateAttr(AL.getRange(), S.Context, TestState,
1242 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001243}
1244
Erich Keanee891aa92018-07-13 15:07:47 +00001245static void handleExtVectorTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001246 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001247 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001248}
1249
Erich Keanee891aa92018-07-13 15:07:47 +00001250static void handlePackedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001251 if (auto *TD = dyn_cast<TagDecl>(D))
1252 TD->addAttr(::new (S.Context) PackedAttr(AL.getRange(), S.Context,
1253 AL.getAttributeSpellingListIndex()));
1254 else if (auto *FD = dyn_cast<FieldDecl>(D)) {
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001255 bool BitfieldByteAligned = (!FD->getType()->isDependentType() &&
1256 !FD->getType()->isIncompleteType() &&
1257 FD->isBitField() &&
1258 S.Context.getTypeAlign(FD->getType()) <= 8);
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001259
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001260 if (S.getASTContext().getTargetInfo().getTriple().isPS4()) {
1261 if (BitfieldByteAligned)
1262 // The PS4 target needs to maintain ABI backwards compatibility.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001263 S.Diag(AL.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
1264 << AL.getName() << FD->getType();
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001265 else
1266 FD->addAttr(::new (S.Context) PackedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001267 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001268 } else {
1269 // Report warning about changed offset in the newer compiler versions.
1270 if (BitfieldByteAligned)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001271 S.Diag(AL.getLoc(), diag::warn_attribute_packed_for_bitfield);
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001272
1273 FD->addAttr(::new (S.Context) PackedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001274 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001275 }
1276
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001277 } else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001278 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001279}
1280
Erich Keanee891aa92018-07-13 15:07:47 +00001281static bool checkIBOutletCommon(Sema &S, Decl *D, const ParsedAttr &AL) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001282 // The IBOutlet/IBOutletCollection attributes only apply to instance
1283 // variables or properties of Objective-C classes. The outlet must also
1284 // have an object reference type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001285 if (const auto *VD = dyn_cast<ObjCIvarDecl>(D)) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001286 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001287 S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
1288 << AL.getName() << VD->getType() << 0;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001289 return false;
1290 }
1291 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001292 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001293 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001294 S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
1295 << AL.getName() << PD->getType() << 1;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001296 return false;
1297 }
1298 }
1299 else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001300 S.Diag(AL.getLoc(), diag::warn_attribute_iboutlet) << AL.getName();
Ted Kremenek7fd17232011-09-29 07:02:25 +00001301 return false;
1302 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001303
Ted Kremenek7fd17232011-09-29 07:02:25 +00001304 return true;
1305}
1306
Erich Keanee891aa92018-07-13 15:07:47 +00001307static void handleIBOutlet(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001308 if (!checkIBOutletCommon(S, D, AL))
Ted Kremenek1f672822010-02-18 03:08:58 +00001309 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001310
Michael Han99315932013-01-24 16:46:58 +00001311 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001312 IBOutletAttr(AL.getRange(), S.Context,
1313 AL.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001314}
1315
Erich Keanee891aa92018-07-13 15:07:47 +00001316static void handleIBOutletCollection(Sema &S, Decl *D, const ParsedAttr &AL) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001317
1318 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001319 if (AL.getNumArgs() > 1) {
1320 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1321 << AL.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001322 return;
1323 }
1324
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001325 if (!checkIBOutletCommon(S, D, AL))
Ted Kremenek26bde772010-05-19 17:38:06 +00001326 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001327
Richard Smithb1f9a282013-10-31 01:56:18 +00001328 ParsedType PT;
1329
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001330 if (AL.hasParsedType())
1331 PT = AL.getTypeArg();
Richard Smithb1f9a282013-10-31 01:56:18 +00001332 else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001333 PT = S.getTypeName(S.Context.Idents.get("NSObject"), AL.getLoc(),
Richard Smithb1f9a282013-10-31 01:56:18 +00001334 S.getScopeForContext(D->getDeclContext()->getParent()));
1335 if (!PT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001336 S.Diag(AL.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
Richard Smithb1f9a282013-10-31 01:56:18 +00001337 return;
1338 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001339 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001340
Craig Topperc3ec1492014-05-26 06:22:03 +00001341 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001342 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1343 if (!QTLoc)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001344 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, AL.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001345
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001346 // Diagnose use of non-object type in iboutletcollection attribute.
1347 // FIXME. Gnu attribute extension ignores use of builtin types in
1348 // attributes. So, __attribute__((iboutletcollection(char))) will be
1349 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001350 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001351 S.Diag(AL.getLoc(),
Richard Smithb1f9a282013-10-31 01:56:18 +00001352 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1353 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001354 return;
1355 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001356
Michael Han99315932013-01-24 16:46:58 +00001357 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001358 IBOutletCollectionAttr(AL.getRange(), S.Context, QTLoc,
1359 AL.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001360}
1361
Hal Finkelee90a222014-09-26 05:04:30 +00001362bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1363 if (RefOkay) {
1364 if (T->isReferenceType())
1365 return true;
1366 } else {
1367 T = T.getNonReferenceType();
1368 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001369
Hal Finkelee90a222014-09-26 05:04:30 +00001370 // The nonnull attribute, and other similar attributes, can be applied to a
1371 // transparent union that contains a pointer type.
Richard Smith588bd9b2014-08-27 04:59:42 +00001372 if (const RecordType *UT = T->getAsUnionType()) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001373 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1374 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001375 for (const auto *I : UD->fields()) {
1376 QualType QT = I->getType();
Richard Smith588bd9b2014-08-27 04:59:42 +00001377 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1378 return true;
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001379 }
1380 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001381 }
1382
1383 return T->isAnyPointerType() || T->isBlockPointerType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001384}
1385
Erich Keanee891aa92018-07-13 15:07:47 +00001386static bool attrNonNullArgCheck(Sema &S, QualType T, const ParsedAttr &AL,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001387 SourceRange AttrParmRange,
Hal Finkelee90a222014-09-26 05:04:30 +00001388 SourceRange TypeRange,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001389 bool isReturnValue = false) {
Hal Finkelee90a222014-09-26 05:04:30 +00001390 if (!S.isValidPointerAttrType(T)) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001391 if (isReturnValue)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001392 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
1393 << AL.getName() << AttrParmRange << TypeRange;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001394 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001395 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
1396 << AL.getName() << AttrParmRange << TypeRange << 0;
Ted Kremenek9aedc152014-01-17 06:24:56 +00001397 return false;
1398 }
1399 return true;
1400}
1401
Erich Keanee891aa92018-07-13 15:07:47 +00001402static void handleNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Joel E. Denny81508102018-03-13 14:51:22 +00001403 SmallVector<ParamIdx, 8> NonNullArgs;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001404 for (unsigned I = 0; I < AL.getNumArgs(); ++I) {
1405 Expr *Ex = AL.getArgAsExpr(I);
Joel E. Denny81508102018-03-13 14:51:22 +00001406 ParamIdx Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001407 if (!checkFunctionOrMethodParameterIndex(S, D, AL, I + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001408 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001409
1410 // Is the function argument a pointer type?
Joel E. Denny81508102018-03-13 14:51:22 +00001411 if (Idx.getASTIndex() < getFunctionOrMethodNumParams(D) &&
1412 !attrNonNullArgCheck(
1413 S, getFunctionOrMethodParamType(D, Idx.getASTIndex()), AL,
1414 Ex->getSourceRange(),
1415 getFunctionOrMethodParamRange(D, Idx.getASTIndex())))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001416 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001417
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001418 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001419 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001420
1421 // If no arguments were specified to __attribute__((nonnull)) then all pointer
Richard Smith588bd9b2014-08-27 04:59:42 +00001422 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1423 // check if the attribute came from a macro expansion or a template
1424 // instantiation.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001425 if (NonNullArgs.empty() && AL.getLoc().isFileID() &&
Richard Smith51ec0cf2017-02-21 01:17:38 +00001426 !S.inTemplateInstantiation()) {
Richard Smith588bd9b2014-08-27 04:59:42 +00001427 bool AnyPointers = isFunctionOrMethodVariadic(D);
1428 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1429 I != E && !AnyPointers; ++I) {
1430 QualType T = getFunctionOrMethodParamType(D, I);
Hal Finkelee90a222014-09-26 05:04:30 +00001431 if (T->isDependentType() || S.isValidPointerAttrType(T))
Richard Smith588bd9b2014-08-27 04:59:42 +00001432 AnyPointers = true;
Ted Kremenek5fa50522008-11-18 06:52:58 +00001433 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001434
Richard Smith588bd9b2014-08-27 04:59:42 +00001435 if (!AnyPointers)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001436 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001437 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001438
Joel E. Denny81508102018-03-13 14:51:22 +00001439 ParamIdx *Start = NonNullArgs.data();
Richard Smith588bd9b2014-08-27 04:59:42 +00001440 unsigned Size = NonNullArgs.size();
1441 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001442 D->addAttr(::new (S.Context)
Joel E. Denny81508102018-03-13 14:51:22 +00001443 NonNullAttr(AL.getRange(), S.Context, Start, Size,
1444 AL.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001445}
1446
Jordan Rosec9399072014-02-11 17:27:59 +00001447static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00001448 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001449 if (AL.getNumArgs() > 0) {
Jordan Rosec9399072014-02-11 17:27:59 +00001450 if (D->getFunctionType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001451 handleNonNullAttr(S, D, AL);
Jordan Rosec9399072014-02-11 17:27:59 +00001452 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001453 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
Jordan Rosec9399072014-02-11 17:27:59 +00001454 << D->getSourceRange();
1455 }
1456 return;
1457 }
1458
1459 // Is the argument a pointer type?
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001460 if (!attrNonNullArgCheck(S, D->getType(), AL, SourceRange(),
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001461 D->getSourceRange()))
Jordan Rosec9399072014-02-11 17:27:59 +00001462 return;
1463
1464 D->addAttr(::new (S.Context)
Joel E. Denny81508102018-03-13 14:51:22 +00001465 NonNullAttr(AL.getRange(), S.Context, nullptr, 0,
1466 AL.getAttributeSpellingListIndex()));
Jordan Rosec9399072014-02-11 17:27:59 +00001467}
1468
Erich Keanee891aa92018-07-13 15:07:47 +00001469static void handleReturnsNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001470 QualType ResultType = getFunctionOrMethodResultType(D);
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001471 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001472 if (!attrNonNullArgCheck(S, ResultType, AL, SourceRange(), SR,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001473 /* isReturnValue */ true))
1474 return;
1475
1476 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001477 ReturnsNonNullAttr(AL.getRange(), S.Context,
1478 AL.getAttributeSpellingListIndex()));
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001479}
1480
Erich Keanee891aa92018-07-13 15:07:47 +00001481static void handleNoEscapeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Akira Hatanaka98a49332017-09-22 00:41:05 +00001482 if (D->isInvalidDecl())
1483 return;
1484
1485 // noescape only applies to pointer types.
1486 QualType T = cast<ParmVarDecl>(D)->getType();
1487 if (!S.isValidPointerAttrType(T, /* RefOkay */ true)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001488 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
1489 << AL.getName() << AL.getRange() << 0;
Akira Hatanaka98a49332017-09-22 00:41:05 +00001490 return;
1491 }
1492
1493 D->addAttr(::new (S.Context) NoEscapeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001494 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Akira Hatanaka98a49332017-09-22 00:41:05 +00001495}
1496
Erich Keanee891aa92018-07-13 15:07:47 +00001497static void handleAssumeAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001498 Expr *E = AL.getArgAsExpr(0),
1499 *OE = AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr;
1500 S.AddAssumeAlignedAttr(AL.getRange(), D, E, OE,
1501 AL.getAttributeSpellingListIndex());
Hal Finkelee90a222014-09-26 05:04:30 +00001502}
1503
Erich Keanee891aa92018-07-13 15:07:47 +00001504static void handleAllocAlignAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001505 S.AddAllocAlignAttr(AL.getRange(), D, AL.getArgAsExpr(0),
1506 AL.getAttributeSpellingListIndex());
Erich Keane623efd82017-03-30 21:48:55 +00001507}
1508
Hal Finkelee90a222014-09-26 05:04:30 +00001509void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
1510 Expr *OE, unsigned SpellingListIndex) {
1511 QualType ResultType = getFunctionOrMethodResultType(D);
1512 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1513
1514 AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex);
1515 SourceLocation AttrLoc = AttrRange.getBegin();
1516
1517 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1518 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1519 << &TmpAttr << AttrRange << SR;
1520 return;
1521 }
1522
1523 if (!E->isValueDependent()) {
1524 llvm::APSInt I(64);
1525 if (!E->isIntegerConstantExpr(I, Context)) {
1526 if (OE)
1527 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1528 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1529 << E->getSourceRange();
1530 else
1531 Diag(AttrLoc, diag::err_attribute_argument_type)
1532 << &TmpAttr << AANT_ArgumentIntegerConstant
1533 << E->getSourceRange();
1534 return;
1535 }
1536
1537 if (!I.isPowerOf2()) {
1538 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1539 << E->getSourceRange();
1540 return;
1541 }
1542 }
1543
1544 if (OE) {
1545 if (!OE->isValueDependent()) {
1546 llvm::APSInt I(64);
1547 if (!OE->isIntegerConstantExpr(I, Context)) {
1548 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1549 << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1550 << OE->getSourceRange();
1551 return;
1552 }
1553 }
1554 }
1555
1556 D->addAttr(::new (Context)
1557 AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex));
1558}
1559
Erich Keane623efd82017-03-30 21:48:55 +00001560void Sema::AddAllocAlignAttr(SourceRange AttrRange, Decl *D, Expr *ParamExpr,
1561 unsigned SpellingListIndex) {
1562 QualType ResultType = getFunctionOrMethodResultType(D);
1563
Joel E. Denny81508102018-03-13 14:51:22 +00001564 AllocAlignAttr TmpAttr(AttrRange, Context, ParamIdx(), SpellingListIndex);
Erich Keane623efd82017-03-30 21:48:55 +00001565 SourceLocation AttrLoc = AttrRange.getBegin();
1566
1567 if (!ResultType->isDependentType() &&
1568 !isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1569 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1570 << &TmpAttr << AttrRange << getFunctionOrMethodResultSourceRange(D);
1571 return;
1572 }
1573
Joel E. Denny81508102018-03-13 14:51:22 +00001574 ParamIdx Idx;
Erich Keane623efd82017-03-30 21:48:55 +00001575 const auto *FuncDecl = cast<FunctionDecl>(D);
1576 if (!checkFunctionOrMethodParameterIndex(*this, FuncDecl, TmpAttr,
Joel E. Denny81508102018-03-13 14:51:22 +00001577 /*AttrArgNo=*/1, ParamExpr, Idx))
Erich Keane623efd82017-03-30 21:48:55 +00001578 return;
1579
Joel E. Denny81508102018-03-13 14:51:22 +00001580 QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Erich Keane623efd82017-03-30 21:48:55 +00001581 if (!Ty->isDependentType() && !Ty->isIntegralType(Context)) {
1582 Diag(ParamExpr->getLocStart(), diag::err_attribute_integers_only)
Joel E. Denny81508102018-03-13 14:51:22 +00001583 << &TmpAttr
1584 << FuncDecl->getParamDecl(Idx.getASTIndex())->getSourceRange();
Erich Keane623efd82017-03-30 21:48:55 +00001585 return;
1586 }
1587
Joel E. Denny81508102018-03-13 14:51:22 +00001588 D->addAttr(::new (Context)
1589 AllocAlignAttr(AttrRange, Context, Idx, SpellingListIndex));
Erich Keane623efd82017-03-30 21:48:55 +00001590}
1591
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001592/// Normalize the attribute, __foo__ becomes foo.
1593/// Returns true if normalization was applied.
1594static bool normalizeName(StringRef &AttrName) {
Aaron Ballman62692362015-10-09 13:53:24 +00001595 if (AttrName.size() > 4 && AttrName.startswith("__") &&
1596 AttrName.endswith("__")) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001597 AttrName = AttrName.drop_front(2).drop_back(2);
1598 return true;
1599 }
1600 return false;
1601}
1602
Erich Keanee891aa92018-07-13 15:07:47 +00001603static void handleOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001604 // This attribute must be applied to a function declaration. The first
1605 // argument to the attribute must be an identifier, the name of the resource,
1606 // for example: malloc. The following arguments must be argument indexes, the
1607 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001608 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001609 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001610 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001611
Aaron Ballman00e99962013-08-31 01:11:41 +00001612 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001613 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001614 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001615 return;
1616 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001617
Richard Smith852e9ce2013-11-27 01:46:48 +00001618 // Figure out our Kind.
1619 OwnershipAttr::OwnershipKind K =
Craig Topperc3ec1492014-05-26 06:22:03 +00001620 OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0,
Richard Smith852e9ce2013-11-27 01:46:48 +00001621 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001622
Richard Smith852e9ce2013-11-27 01:46:48 +00001623 // Check arguments.
1624 switch (K) {
1625 case OwnershipAttr::Takes:
1626 case OwnershipAttr::Holds:
1627 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001628 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1629 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001630 return;
1631 }
1632 break;
1633 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001634 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001635 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1636 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001637 return;
1638 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001639 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001640 }
1641
Richard Smith852e9ce2013-11-27 01:46:48 +00001642 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001643
Richard Smith852e9ce2013-11-27 01:46:48 +00001644 StringRef ModuleName = Module->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001645 if (normalizeName(ModuleName)) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001646 Module = &S.PP.getIdentifierTable().get(ModuleName);
1647 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001648
Joel E. Denny81508102018-03-13 14:51:22 +00001649 SmallVector<ParamIdx, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001650 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1651 Expr *Ex = AL.getArgAsExpr(i);
Joel E. Denny81508102018-03-13 14:51:22 +00001652 ParamIdx Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001653 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001654 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001655
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001656 // Is the function argument a pointer type?
Joel E. Denny81508102018-03-13 14:51:22 +00001657 QualType T = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001658 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001659 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001660 case OwnershipAttr::Takes:
1661 case OwnershipAttr::Holds:
1662 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1663 Err = 0;
1664 break;
1665 case OwnershipAttr::Returns:
1666 if (!T->isIntegerType())
1667 Err = 1;
1668 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001669 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001670 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001671 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001672 << Ex->getSourceRange();
1673 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001674 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001675
1676 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001677 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001678 // Cannot have two ownership attributes of different kinds for the same
1679 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001680 if (I->getOwnKind() != K && I->args_end() !=
1681 std::find(I->args_begin(), I->args_end(), Idx)) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001682 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001683 << AL.getName() << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001684 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001685 } else if (K == OwnershipAttr::Returns &&
1686 I->getOwnKind() == OwnershipAttr::Returns) {
1687 // A returns attribute conflicts with any other returns attribute using
Joel E. Denny81508102018-03-13 14:51:22 +00001688 // a different index.
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001689 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1690 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
Joel E. Denny81508102018-03-13 14:51:22 +00001691 << I->args_begin()->getSourceIndex();
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001692 if (I->args_size())
1693 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
Joel E. Denny81508102018-03-13 14:51:22 +00001694 << Idx.getSourceIndex() << Ex->getSourceRange();
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001695 return;
1696 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001697 }
1698 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001699 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001700 }
1701
Joel E. Denny81508102018-03-13 14:51:22 +00001702 ParamIdx *Start = OwnershipArgs.data();
1703 unsigned Size = OwnershipArgs.size();
1704 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001705 D->addAttr(::new (S.Context)
Joel E. Denny81508102018-03-13 14:51:22 +00001706 OwnershipAttr(AL.getLoc(), S.Context, Module, Start, Size,
1707 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001708}
1709
Erich Keanee891aa92018-07-13 15:07:47 +00001710static void handleWeakRefAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001711 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001712 if (AL.getNumArgs() > 1) {
1713 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1714 << AL.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001715 return;
1716 }
1717
1718 // gcc rejects
1719 // class c {
1720 // static int a __attribute__((weakref ("v2")));
1721 // static int b() __attribute__((weakref ("f3")));
1722 // };
1723 // and ignores the attributes of
1724 // void f(void) {
1725 // static int a __attribute__((weakref ("v2")));
1726 // }
1727 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001728 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001729 if (!Ctx->isFileContext()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001730 S.Diag(AL.getLoc(), diag::err_attribute_weakref_not_global_context)
1731 << cast<NamedDecl>(D);
Sebastian Redl50c68252010-08-31 00:36:30 +00001732 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001733 }
1734
1735 // The GCC manual says
1736 //
1737 // At present, a declaration to which `weakref' is attached can only
1738 // be `static'.
1739 //
1740 // It also says
1741 //
1742 // Without a TARGET,
1743 // given as an argument to `weakref' or to `alias', `weakref' is
1744 // equivalent to `weak'.
1745 //
1746 // gcc 4.4.1 will accept
1747 // int a7 __attribute__((weakref));
1748 // as
1749 // int a7 __attribute__((weak));
1750 // This looks like a bug in gcc. We reject that for now. We should revisit
1751 // it if this behaviour is actually used.
1752
Rafael Espindolac18086a2010-02-23 22:00:30 +00001753 // GCC rejects
1754 // static ((alias ("y"), weakref)).
1755 // Should we? How to check that weakref is before or after alias?
1756
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001757 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1758 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1759 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001760 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001761 if (AL.getNumArgs() && S.checkStringLiteralArgumentAttr(AL, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001762 // GCC will accept anything as the argument of weakref. Should we
1763 // check for an existing decl?
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001764 D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str,
1765 AL.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001766
Michael Han99315932013-01-24 16:46:58 +00001767 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001768 WeakRefAttr(AL.getRange(), S.Context,
1769 AL.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001770}
1771
Erich Keanee891aa92018-07-13 15:07:47 +00001772static void handleIFuncAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001773 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001774 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001775 return;
1776
1777 // Aliases should be on declarations, not definitions.
1778 const auto *FD = cast<FunctionDecl>(D);
1779 if (FD->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001780 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 1;
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001781 return;
1782 }
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001783
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001784 D->addAttr(::new (S.Context) IFuncAttr(AL.getRange(), S.Context, Str,
1785 AL.getAttributeSpellingListIndex()));
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001786}
1787
Erich Keanee891aa92018-07-13 15:07:47 +00001788static void handleAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001789 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001790 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001791 return;
1792
Douglas Gregore8bbc122011-09-02 00:18:52 +00001793 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001794 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_darwin);
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001795 return;
1796 }
Justin Lebara8f0254b2016-01-23 21:28:10 +00001797 if (S.Context.getTargetInfo().getTriple().isNVPTX()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001798 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_nvptx);
Justin Lebara8f0254b2016-01-23 21:28:10 +00001799 }
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001800
David Majnemer2dc81462015-01-19 09:00:28 +00001801 // Aliases should be on declarations, not definitions.
1802 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1803 if (FD->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001804 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 0;
David Majnemer2dc81462015-01-19 09:00:28 +00001805 return;
1806 }
1807 } else {
1808 const auto *VD = cast<VarDecl>(D);
1809 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001810 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << VD << 0;
David Majnemer2dc81462015-01-19 09:00:28 +00001811 return;
1812 }
1813 }
1814
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001815 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001816
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001817 D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str,
1818 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001819}
1820
Erich Keanee891aa92018-07-13 15:07:47 +00001821static void handleTLSModelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001822 StringRef Model;
1823 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001824 // Check that it is a string.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001825 if (!S.checkStringLiteralArgumentAttr(AL, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001826 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001827
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001828 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001829 if (Model != "global-dynamic" && Model != "local-dynamic"
1830 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001831 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001832 return;
1833 }
1834
Michael Han99315932013-01-24 16:46:58 +00001835 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001836 TLSModelAttr(AL.getRange(), S.Context, Model,
1837 AL.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001838}
1839
Erich Keanee891aa92018-07-13 15:07:47 +00001840static void handleRestrictAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemer631a90b2015-02-04 07:23:21 +00001841 QualType ResultType = getFunctionOrMethodResultType(D);
1842 if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
1843 D->addAttr(::new (S.Context) RestrictAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001844 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
David Majnemer631a90b2015-02-04 07:23:21 +00001845 return;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001846 }
1847
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001848 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
1849 << AL.getName() << getFunctionOrMethodResultSourceRange(D);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001850}
1851
Erich Keane3efe0022018-07-20 14:13:28 +00001852static void handleCPUSpecificAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1853 FunctionDecl *FD = cast<FunctionDecl>(D);
1854 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
1855 return;
1856
1857 SmallVector<IdentifierInfo *, 8> CPUs;
1858 for (unsigned ArgNo = 0; ArgNo < getNumAttributeArgs(AL); ++ArgNo) {
1859 if (!AL.isArgIdent(ArgNo)) {
1860 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1861 << AL.getName() << AANT_ArgumentIdentifier;
1862 return;
1863 }
1864
1865 IdentifierLoc *CPUArg = AL.getArgAsIdent(ArgNo);
1866 StringRef CPUName = CPUArg->Ident->getName().trim();
1867
1868 if (!S.Context.getTargetInfo().validateCPUSpecificCPUDispatch(CPUName)) {
1869 S.Diag(CPUArg->Loc, diag::err_invalid_cpu_specific_dispatch_value)
1870 << CPUName << (AL.getKind() == ParsedAttr::AT_CPUDispatch);
1871 return;
1872 }
1873
1874 const TargetInfo &Target = S.Context.getTargetInfo();
1875 if (llvm::any_of(CPUs, [CPUName, &Target](const IdentifierInfo *Cur) {
1876 return Target.CPUSpecificManglingCharacter(CPUName) ==
1877 Target.CPUSpecificManglingCharacter(Cur->getName());
1878 })) {
1879 S.Diag(AL.getLoc(), diag::warn_multiversion_duplicate_entries);
1880 return;
1881 }
1882 CPUs.push_back(CPUArg->Ident);
1883 }
1884
1885 FD->setIsMultiVersion(true);
1886 if (AL.getKind() == ParsedAttr::AT_CPUSpecific)
1887 D->addAttr(::new (S.Context) CPUSpecificAttr(
1888 AL.getRange(), S.Context, CPUs.data(), CPUs.size(),
1889 AL.getAttributeSpellingListIndex()));
1890 else
1891 D->addAttr(::new (S.Context) CPUDispatchAttr(
1892 AL.getRange(), S.Context, CPUs.data(), CPUs.size(),
1893 AL.getAttributeSpellingListIndex()));
1894}
1895
Erich Keanee891aa92018-07-13 15:07:47 +00001896static void handleCommonAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001897 if (S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001898 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
1899 << AL.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001900 return;
1901 }
1902
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001903 if (CommonAttr *CA = S.mergeCommonAttr(D, AL.getRange(), AL.getName(),
1904 AL.getAttributeSpellingListIndex()))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001905 D->addAttr(CA);
Eric Christopher8a2ee392010-12-02 02:45:55 +00001906}
1907
Erich Keanee891aa92018-07-13 15:07:47 +00001908static void handleNakedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001909 if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, AL.getRange(),
1910 AL.getName()))
Charles Davis0e379112016-08-08 21:19:08 +00001911 return;
1912
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001913 if (AL.isDeclspecAttribute()) {
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00001914 const auto &Triple = S.getASTContext().getTargetInfo().getTriple();
1915 const auto &Arch = Triple.getArch();
1916 if (Arch != llvm::Triple::x86 &&
1917 (Arch != llvm::Triple::arm && Arch != llvm::Triple::thumb)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001918 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_on_arch)
1919 << AL.getName() << Triple.getArchName();
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00001920 return;
1921 }
1922 }
1923
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001924 D->addAttr(::new (S.Context) NakedAttr(AL.getRange(), S.Context,
1925 AL.getAttributeSpellingListIndex()));
Charles Davis0e379112016-08-08 21:19:08 +00001926}
1927
Erich Keanee891aa92018-07-13 15:07:47 +00001928static void handleNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001929 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001930
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001931 if (!isa<ObjCMethodDecl>(D)) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00001932 S.Diag(Attrs.getLoc(), diag::warn_attribute_wrong_decl_type)
1933 << Attrs.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001934 return;
1935 }
1936
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001937 D->addAttr(::new (S.Context) NoReturnAttr(
Erich Keaneb11ebc52017-09-27 03:20:13 +00001938 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001939}
1940
Erich Keanee891aa92018-07-13 15:07:47 +00001941static void handleNoCfCheckAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
Oren Ben Simhon220671a2018-03-17 13:31:35 +00001942 if (!S.getLangOpts().CFProtectionBranch)
1943 S.Diag(Attrs.getLoc(), diag::warn_nocf_check_attribute_ignored);
1944 else
1945 handleSimpleAttribute<AnyX86NoCfCheckAttr>(S, D, Attrs);
John McCall3882ace2011-01-05 12:14:39 +00001946}
1947
Erich Keanee891aa92018-07-13 15:07:47 +00001948bool Sema::CheckAttrNoArgs(const ParsedAttr &Attrs) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00001949 if (!checkAttributeNumArgs(*this, Attrs, 0)) {
1950 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00001951 return true;
1952 }
1953
1954 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001955}
1956
Erich Keanee891aa92018-07-13 15:07:47 +00001957bool Sema::CheckAttrTarget(const ParsedAttr &AL) {
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001958 // Check whether the attribute is valid on the current target.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001959 if (!AL.existsInTarget(Context.getTargetInfo())) {
1960 Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) << AL.getName();
1961 AL.setInvalid();
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001962 return true;
1963 }
1964
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001965 return false;
1966}
1967
Erich Keanee891aa92018-07-13 15:07:47 +00001968static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1969
Ted Kremenek5295ce82010-08-19 00:51:58 +00001970 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1971 // because 'analyzer_noreturn' does not impact the type.
David Majnemer06864812015-04-07 06:01:53 +00001972 if (!isFunctionOrMethodOrBlock(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001973 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00001974 if (!VD || (!VD->getType()->isBlockPointerType() &&
1975 !VD->getType()->isFunctionPointerType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001976 S.Diag(AL.getLoc(),
1977 AL.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Craig Topper8f7f3ea2015-11-17 05:40:05 +00001978 : diag::warn_attribute_wrong_decl_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001979 << AL.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001980 return;
1981 }
1982 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001983
Michael Han99315932013-01-24 16:46:58 +00001984 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001985 AnalyzerNoReturnAttr(AL.getRange(), S.Context,
1986 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001987}
1988
John Thompsoncdb847ba2010-08-09 21:53:52 +00001989// PS3 PPU-specific.
Erich Keanee891aa92018-07-13 15:07:47 +00001990static void handleVecReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1991 /*
1992 Returning a Vector Class in Registers
1993
1994 According to the PPU ABI specifications, a class with a single member of
1995 vector type is returned in memory when used as the return value of a
1996 function.
1997 This results in inefficient code when implementing vector classes. To return
1998 the value in a single vector register, add the vecreturn attribute to the
1999 class definition. This attribute is also applicable to struct types.
2000
2001 Example:
2002
2003 struct Vector
2004 {
2005 __vector float xyzw;
2006 } __attribute__((vecreturn));
2007
2008 Vector Add(Vector lhs, Vector rhs)
2009 {
2010 Vector result;
2011 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
2012 return result; // This will be returned in a register
2013 }
2014 */
Aaron Ballman3e424b52013-12-26 18:30:57 +00002015 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002016 S.Diag(AL.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00002017 return;
2018 }
2019
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002020 const auto *R = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00002021 int count = 0;
2022
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002023 if (!isa<CXXRecordDecl>(R)) {
2024 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00002025 return;
2026 }
2027
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002028 if (!cast<CXXRecordDecl>(R)->isPOD()) {
2029 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
John Thompson9a587aaa2010-09-18 01:12:07 +00002030 return;
2031 }
2032
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002033 for (const auto *I : R->fields()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002034 if ((count == 1) || !I->getType()->isVectorType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002035 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00002036 return;
2037 }
2038 count++;
2039 }
2040
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002041 D->addAttr(::new (S.Context) VecReturnAttr(
2042 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00002043}
2044
Richard Smithe233fbf2013-01-28 22:42:45 +00002045static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002046 const ParsedAttr &AL) {
Richard Smithe233fbf2013-01-28 22:42:45 +00002047 if (isa<ParmVarDecl>(D)) {
2048 // [[carries_dependency]] can only be applied to a parameter if it is a
2049 // parameter of a function declaration or lambda.
2050 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002051 S.Diag(AL.getLoc(),
Richard Smithe233fbf2013-01-28 22:42:45 +00002052 diag::err_carries_dependency_param_not_function_decl);
2053 return;
2054 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002055 }
Richard Smithe233fbf2013-01-28 22:42:45 +00002056
2057 D->addAttr(::new (S.Context) CarriesDependencyAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002058 AL.getRange(), S.Context,
2059 AL.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002060}
2061
Erich Keanee891aa92018-07-13 15:07:47 +00002062static void handleUnusedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002063 bool IsCXX17Attr = AL.isCXX11Attribute() && !AL.getScopeName();
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002064
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002065 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002066 // about using it as an extension.
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002067 if (!S.getLangOpts().CPlusPlus17 && IsCXX17Attr)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002068 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL.getName();
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002069
2070 D->addAttr(::new (S.Context) UnusedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002071 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002072}
2073
Erich Keanee891aa92018-07-13 15:07:47 +00002074static void handleConstructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002075 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002076 if (AL.getNumArgs() &&
2077 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002078 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002079
Michael Han99315932013-01-24 16:46:58 +00002080 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002081 ConstructorAttr(AL.getRange(), S.Context, priority,
2082 AL.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002083}
2084
Erich Keanee891aa92018-07-13 15:07:47 +00002085static void handleDestructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00002086 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002087 if (AL.getNumArgs() &&
2088 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002089 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002090
Michael Han99315932013-01-24 16:46:58 +00002091 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002092 DestructorAttr(AL.getRange(), S.Context, priority,
2093 AL.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002094}
2095
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002096template <typename AttrTy>
Erich Keanee891aa92018-07-13 15:07:47 +00002097static void handleAttrWithMessage(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002098 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002099 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002100 if (AL.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002101 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002102
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002103 D->addAttr(::new (S.Context) AttrTy(AL.getRange(), S.Context, Str,
2104 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00002105}
2106
Ted Kremenek438f8db2014-02-22 01:06:05 +00002107static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002108 const ParsedAttr &AL) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00002109 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002110 S.Diag(AL.getLoc(), diag::err_objc_attr_protocol_requires_definition)
2111 << AL.getName() << AL.getRange();
Ted Kremenek27cfe102014-02-21 22:49:04 +00002112 return;
2113 }
2114
Ted Kremenek28eace62013-11-23 01:01:34 +00002115 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002116 ObjCExplicitProtocolImplAttr(AL.getRange(), S.Context,
2117 AL.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00002118}
2119
Jordy Rose740b0c22012-05-08 03:27:22 +00002120static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2121 IdentifierInfo *Platform,
2122 VersionTuple Introduced,
2123 VersionTuple Deprecated,
2124 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002125 StringRef PlatformName
2126 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2127 if (PlatformName.empty())
2128 PlatformName = Platform->getName();
2129
2130 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2131 // of these steps are needed).
2132 if (!Introduced.empty() && !Deprecated.empty() &&
2133 !(Introduced <= Deprecated)) {
2134 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2135 << 1 << PlatformName << Deprecated.getAsString()
2136 << 0 << Introduced.getAsString();
2137 return true;
2138 }
2139
2140 if (!Introduced.empty() && !Obsoleted.empty() &&
2141 !(Introduced <= Obsoleted)) {
2142 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2143 << 2 << PlatformName << Obsoleted.getAsString()
2144 << 0 << Introduced.getAsString();
2145 return true;
2146 }
2147
2148 if (!Deprecated.empty() && !Obsoleted.empty() &&
2149 !(Deprecated <= Obsoleted)) {
2150 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2151 << 2 << PlatformName << Obsoleted.getAsString()
2152 << 1 << Deprecated.getAsString();
2153 return true;
2154 }
2155
2156 return false;
2157}
2158
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002159/// Check whether the two versions match.
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002160///
2161/// If either version tuple is empty, then they are assumed to match. If
2162/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2163static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2164 bool BeforeIsOkay) {
2165 if (X.empty() || Y.empty())
2166 return true;
2167
2168 if (X == Y)
2169 return true;
2170
2171 if (BeforeIsOkay && X < Y)
2172 return true;
2173
2174 return false;
2175}
2176
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002177AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002178 IdentifierInfo *Platform,
Manman Ren719a8642016-05-06 21:04:01 +00002179 bool Implicit,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002180 VersionTuple Introduced,
2181 VersionTuple Deprecated,
2182 VersionTuple Obsoleted,
2183 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002184 StringRef Message,
Manman Rend8039df2016-02-22 04:47:24 +00002185 bool IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002186 StringRef Replacement,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002187 AvailabilityMergeKind AMK,
Michael Han99315932013-01-24 16:46:58 +00002188 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002189 VersionTuple MergedIntroduced = Introduced;
2190 VersionTuple MergedDeprecated = Deprecated;
2191 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002192 bool FoundAny = false;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002193 bool OverrideOrImpl = false;
2194 switch (AMK) {
2195 case AMK_None:
2196 case AMK_Redeclaration:
2197 OverrideOrImpl = false;
2198 break;
2199
2200 case AMK_Override:
2201 case AMK_ProtocolImplementation:
2202 OverrideOrImpl = true;
2203 break;
2204 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002205
Rafael Espindolac67f2232012-05-10 02:50:16 +00002206 if (D->hasAttrs()) {
2207 AttrVec &Attrs = D->getAttrs();
2208 for (unsigned i = 0, e = Attrs.size(); i != e;) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002209 const auto *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002210 if (!OldAA) {
2211 ++i;
2212 continue;
2213 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002214
Rafael Espindolac67f2232012-05-10 02:50:16 +00002215 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2216 if (OldPlatform != Platform) {
2217 ++i;
2218 continue;
2219 }
2220
Tim Northover7a73cc72015-10-30 16:30:49 +00002221 // If there is an existing availability attribute for this platform that
2222 // is explicit and the new one is implicit use the explicit one and
2223 // discard the new implicit attribute.
Manman Ren719a8642016-05-06 21:04:01 +00002224 if (!OldAA->isImplicit() && Implicit) {
Tim Northover7a73cc72015-10-30 16:30:49 +00002225 return nullptr;
2226 }
2227
2228 // If there is an existing attribute for this platform that is implicit
2229 // and the new attribute is explicit then erase the old one and
2230 // continue processing the attributes.
Manman Ren719a8642016-05-06 21:04:01 +00002231 if (!Implicit && OldAA->isImplicit()) {
Tim Northover7a73cc72015-10-30 16:30:49 +00002232 Attrs.erase(Attrs.begin() + i);
2233 --e;
2234 continue;
2235 }
2236
Rafael Espindolac67f2232012-05-10 02:50:16 +00002237 FoundAny = true;
2238 VersionTuple OldIntroduced = OldAA->getIntroduced();
2239 VersionTuple OldDeprecated = OldAA->getDeprecated();
2240 VersionTuple OldObsoleted = OldAA->getObsoleted();
2241 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002242
Douglas Gregord2a713e2015-09-30 21:27:42 +00002243 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
2244 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
2245 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002246 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregord2a713e2015-09-30 21:27:42 +00002247 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
2248 if (OverrideOrImpl) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002249 int Which = -1;
2250 VersionTuple FirstVersion;
2251 VersionTuple SecondVersion;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002252 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002253 Which = 0;
2254 FirstVersion = OldIntroduced;
2255 SecondVersion = Introduced;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002256 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002257 Which = 1;
2258 FirstVersion = Deprecated;
2259 SecondVersion = OldDeprecated;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002260 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002261 Which = 2;
2262 FirstVersion = Obsoleted;
2263 SecondVersion = OldObsoleted;
2264 }
2265
2266 if (Which == -1) {
2267 Diag(OldAA->getLocation(),
2268 diag::warn_mismatched_availability_override_unavail)
Douglas Gregord2a713e2015-09-30 21:27:42 +00002269 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2270 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002271 } else {
2272 Diag(OldAA->getLocation(),
2273 diag::warn_mismatched_availability_override)
2274 << Which
2275 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
Douglas Gregord2a713e2015-09-30 21:27:42 +00002276 << FirstVersion.getAsString() << SecondVersion.getAsString()
2277 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002278 }
Douglas Gregord2a713e2015-09-30 21:27:42 +00002279 if (AMK == AMK_Override)
2280 Diag(Range.getBegin(), diag::note_overridden_method);
2281 else
2282 Diag(Range.getBegin(), diag::note_protocol_method);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002283 } else {
2284 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2285 Diag(Range.getBegin(), diag::note_previous_attribute);
2286 }
2287
Rafael Espindolac67f2232012-05-10 02:50:16 +00002288 Attrs.erase(Attrs.begin() + i);
2289 --e;
2290 continue;
2291 }
2292
2293 VersionTuple MergedIntroduced2 = MergedIntroduced;
2294 VersionTuple MergedDeprecated2 = MergedDeprecated;
2295 VersionTuple MergedObsoleted2 = MergedObsoleted;
2296
2297 if (MergedIntroduced2.empty())
2298 MergedIntroduced2 = OldIntroduced;
2299 if (MergedDeprecated2.empty())
2300 MergedDeprecated2 = OldDeprecated;
2301 if (MergedObsoleted2.empty())
2302 MergedObsoleted2 = OldObsoleted;
2303
2304 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2305 MergedIntroduced2, MergedDeprecated2,
2306 MergedObsoleted2)) {
2307 Attrs.erase(Attrs.begin() + i);
2308 --e;
2309 continue;
2310 }
2311
2312 MergedIntroduced = MergedIntroduced2;
2313 MergedDeprecated = MergedDeprecated2;
2314 MergedObsoleted = MergedObsoleted2;
2315 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002316 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002317 }
2318
2319 if (FoundAny &&
2320 MergedIntroduced == Introduced &&
2321 MergedDeprecated == Deprecated &&
2322 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00002323 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002324
Douglas Gregord2a713e2015-09-30 21:27:42 +00002325 // Only create a new attribute if !OverrideOrImpl, but we want to do
Ted Kremenekb5445722013-04-06 00:34:27 +00002326 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002327 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002328 MergedDeprecated, MergedObsoleted) &&
Douglas Gregord2a713e2015-09-30 21:27:42 +00002329 !OverrideOrImpl) {
Manman Ren719a8642016-05-06 21:04:01 +00002330 auto *Avail = ::new (Context) AvailabilityAttr(Range, Context, Platform,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002331 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002332 Obsoleted, IsUnavailable, Message,
Manman Ren75bc6762016-03-21 17:30:55 +00002333 IsStrict, Replacement,
2334 AttrSpellingListIndex);
Manman Ren719a8642016-05-06 21:04:01 +00002335 Avail->setImplicit(Implicit);
2336 return Avail;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002337 }
Craig Topperc3ec1492014-05-26 06:22:03 +00002338 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002339}
2340
Erich Keanee891aa92018-07-13 15:07:47 +00002341static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002342 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00002343 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002344 IdentifierLoc *Platform = AL.getArgAsIdent(0);
2345 unsigned Index = AL.getAttributeSpellingListIndex();
Fangrui Song6907ce22018-07-30 19:24:48 +00002346
Aaron Ballman00e99962013-08-31 01:11:41 +00002347 IdentifierInfo *II = Platform->Ident;
2348 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2349 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2350 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002351
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002352 auto *ND = dyn_cast<NamedDecl>(D);
Alex Lorenz472cc792017-04-20 09:35:02 +00002353 if (!ND) // We warned about this already, so just return.
Rafael Espindolac231fab2013-01-08 21:30:32 +00002354 return;
Rafael Espindolac231fab2013-01-08 21:30:32 +00002355
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002356 AvailabilityChange Introduced = AL.getAvailabilityIntroduced();
2357 AvailabilityChange Deprecated = AL.getAvailabilityDeprecated();
2358 AvailabilityChange Obsoleted = AL.getAvailabilityObsoleted();
2359 bool IsUnavailable = AL.getUnavailableLoc().isValid();
2360 bool IsStrict = AL.getStrictLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002361 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002362 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002363 Str = SE->getString();
Manman Ren75bc6762016-03-21 17:30:55 +00002364 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002365 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getReplacementExpr()))
Manman Ren75bc6762016-03-21 17:30:55 +00002366 Replacement = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002367
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002368 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, AL.getRange(), II,
Manman Ren719a8642016-05-06 21:04:01 +00002369 false/*Implicit*/,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002370 Introduced.Version,
2371 Deprecated.Version,
2372 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002373 IsUnavailable, Str,
Manman Ren75bc6762016-03-21 17:30:55 +00002374 IsStrict, Replacement,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002375 Sema::AMK_None,
Michael Han99315932013-01-24 16:46:58 +00002376 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002377 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002378 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002379
2380 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2381 // matches before the start of the watchOS platform.
2382 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2383 IdentifierInfo *NewII = nullptr;
2384 if (II->getName() == "ios")
2385 NewII = &S.Context.Idents.get("watchos");
2386 else if (II->getName() == "ios_app_extension")
2387 NewII = &S.Context.Idents.get("watchos_app_extension");
2388
2389 if (NewII) {
2390 auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple {
2391 if (Version.empty())
2392 return Version;
2393 auto Major = Version.getMajor();
2394 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2395 if (NewMajor >= 2) {
2396 if (Version.getMinor().hasValue()) {
2397 if (Version.getSubminor().hasValue())
2398 return VersionTuple(NewMajor, Version.getMinor().getValue(),
2399 Version.getSubminor().getValue());
2400 else
2401 return VersionTuple(NewMajor, Version.getMinor().getValue());
2402 }
2403 }
2404
2405 return VersionTuple(2, 0);
2406 };
2407
2408 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2409 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2410 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2411
2412 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002413 AL.getRange(),
Tim Northover7a73cc72015-10-30 16:30:49 +00002414 NewII,
Manman Ren719a8642016-05-06 21:04:01 +00002415 true/*Implicit*/,
Tim Northover7a73cc72015-10-30 16:30:49 +00002416 NewIntroduced,
2417 NewDeprecated,
2418 NewObsoleted,
2419 IsUnavailable, Str,
Manman Rend8039df2016-02-22 04:47:24 +00002420 IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002421 Replacement,
Tim Northover7a73cc72015-10-30 16:30:49 +00002422 Sema::AMK_None,
2423 Index);
2424 if (NewAttr)
2425 D->addAttr(NewAttr);
2426 }
2427 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2428 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2429 // matches before the start of the tvOS platform.
2430 IdentifierInfo *NewII = nullptr;
2431 if (II->getName() == "ios")
2432 NewII = &S.Context.Idents.get("tvos");
2433 else if (II->getName() == "ios_app_extension")
2434 NewII = &S.Context.Idents.get("tvos_app_extension");
2435
2436 if (NewII) {
2437 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002438 AL.getRange(),
Tim Northover7a73cc72015-10-30 16:30:49 +00002439 NewII,
Manman Ren719a8642016-05-06 21:04:01 +00002440 true/*Implicit*/,
Tim Northover7a73cc72015-10-30 16:30:49 +00002441 Introduced.Version,
2442 Deprecated.Version,
2443 Obsoleted.Version,
2444 IsUnavailable, Str,
Manman Rend8039df2016-02-22 04:47:24 +00002445 IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002446 Replacement,
Tim Northover7a73cc72015-10-30 16:30:49 +00002447 Sema::AMK_None,
2448 Index);
2449 if (NewAttr)
2450 D->addAttr(NewAttr);
2451 }
2452 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002453}
2454
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002455static void handleExternalSourceSymbolAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002456 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002457 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002458 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002459 assert(checkAttributeAtMostNumArgs(S, AL, 3) &&
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002460 "Invalid number of arguments in an external_source_symbol attribute");
2461
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002462 StringRef Language;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002463 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(0)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002464 Language = SE->getString();
2465 StringRef DefinedIn;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002466 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(1)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002467 DefinedIn = SE->getString();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002468 bool IsGeneratedDeclaration = AL.getArgAsIdent(2) != nullptr;
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002469
2470 D->addAttr(::new (S.Context) ExternalSourceSymbolAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002471 AL.getRange(), S.Context, Language, DefinedIn, IsGeneratedDeclaration,
2472 AL.getAttributeSpellingListIndex()));
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002473}
2474
John McCalld041a9b2013-02-20 01:54:26 +00002475template <class T>
2476static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2477 typename T::VisibilityType value,
2478 unsigned attrSpellingListIndex) {
2479 T *existingAttr = D->getAttr<T>();
2480 if (existingAttr) {
2481 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2482 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00002483 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00002484 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2485 S.Diag(range.getBegin(), diag::note_previous_attribute);
2486 D->dropAttr<T>();
2487 }
2488 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2489}
2490
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002491VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002492 VisibilityAttr::VisibilityType Vis,
2493 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002494 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2495 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002496}
2497
John McCalld041a9b2013-02-20 01:54:26 +00002498TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2499 TypeVisibilityAttr::VisibilityType Vis,
2500 unsigned AttrSpellingListIndex) {
2501 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2502 AttrSpellingListIndex);
2503}
2504
Erich Keanee891aa92018-07-13 15:07:47 +00002505static void handleVisibilityAttr(Sema &S, Decl *D, const ParsedAttr &AL,
John McCalld041a9b2013-02-20 01:54:26 +00002506 bool isTypeVisibility) {
2507 // Visibility attributes don't mean anything on a typedef.
2508 if (isa<TypedefNameDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002509 S.Diag(AL.getRange().getBegin(), diag::warn_attribute_ignored)
2510 << AL.getName();
John McCalld041a9b2013-02-20 01:54:26 +00002511 return;
2512 }
2513
2514 // 'type_visibility' can only go on a type or namespace.
2515 if (isTypeVisibility &&
2516 !(isa<TagDecl>(D) ||
2517 isa<ObjCInterfaceDecl>(D) ||
2518 isa<NamespaceDecl>(D))) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002519 S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2520 << AL.getName() << ExpectedTypeOrNamespace;
John McCalld041a9b2013-02-20 01:54:26 +00002521 return;
2522 }
2523
Benjamin Kramer70370212013-09-09 15:08:57 +00002524 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002525 StringRef TypeStr;
2526 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002527 if (!S.checkStringLiteralArgumentAttr(AL, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002528 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002529
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002530 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002531 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002532 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002533 << AL.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002534 return;
2535 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002536
Aaron Ballman682ee422013-09-11 19:47:58 +00002537 // Complain about attempts to use protected visibility on targets
2538 // (like Darwin) that don't support it.
2539 if (type == VisibilityAttr::Protected &&
2540 !S.Context.getTargetInfo().hasProtectedVisibility()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002541 S.Diag(AL.getLoc(), diag::warn_attribute_protected_visibility);
Aaron Ballman682ee422013-09-11 19:47:58 +00002542 type = VisibilityAttr::Default;
2543 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002544
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002545 unsigned Index = AL.getAttributeSpellingListIndex();
2546 Attr *newAttr;
John McCalld041a9b2013-02-20 01:54:26 +00002547 if (isTypeVisibility) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002548 newAttr = S.mergeTypeVisibilityAttr(D, AL.getRange(),
John McCalld041a9b2013-02-20 01:54:26 +00002549 (TypeVisibilityAttr::VisibilityType) type,
2550 Index);
2551 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002552 newAttr = S.mergeVisibilityAttr(D, AL.getRange(), type, Index);
John McCalld041a9b2013-02-20 01:54:26 +00002553 }
2554 if (newAttr)
2555 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002556}
2557
Erich Keanee891aa92018-07-13 15:07:47 +00002558static void handleObjCMethodFamilyAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002559 const auto *M = cast<ObjCMethodDecl>(D);
2560 if (!AL.isArgIdent(0)) {
2561 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
2562 << AL.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002563 return;
2564 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002565
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002566 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00002567 ObjCMethodFamilyAttr::FamilyKind F;
2568 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002569 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
2570 << AL.getName() << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002571 return;
2572 }
2573
Alp Toker314cc812014-01-25 16:55:45 +00002574 if (F == ObjCMethodFamilyAttr::OMF_init &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002575 !M->getReturnType()->isObjCObjectPointerType()) {
2576 S.Diag(M->getLocation(), diag::err_init_method_bad_return_type)
2577 << M->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002578 // Ignore the attribute.
2579 return;
2580 }
2581
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002582 D->addAttr(new (S.Context) ObjCMethodFamilyAttr(
2583 AL.getRange(), S.Context, F, AL.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002584}
2585
Erich Keanee891aa92018-07-13 15:07:47 +00002586static void handleObjCNSObject(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002587 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002588 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002589 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002590 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2591 return;
2592 }
2593 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002594 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002595 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002596 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002597 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2598 return;
2599 }
2600 }
2601 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002602 // It is okay to include this attribute on properties, e.g.:
2603 //
2604 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2605 //
2606 // In this case it follows tradition and suppresses an error in the above
Fangrui Song6907ce22018-07-30 19:24:48 +00002607 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002608 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002609 }
Michael Han99315932013-01-24 16:46:58 +00002610 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002611 ObjCNSObjectAttr(AL.getRange(), S.Context,
2612 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002613}
2614
Erich Keanee891aa92018-07-13 15:07:47 +00002615static void handleObjCIndependentClass(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002616 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002617 QualType T = TD->getUnderlyingType();
2618 if (!T->isObjCObjectPointerType()) {
2619 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2620 return;
2621 }
2622 } else {
2623 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2624 return;
2625 }
2626 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002627 ObjCIndependentClassAttr(AL.getRange(), S.Context,
2628 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002629}
2630
Erich Keanee891aa92018-07-13 15:07:47 +00002631static void handleBlocksAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002632 if (!AL.isArgIdent(0)) {
2633 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
2634 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002635 return;
2636 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002637
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002638 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002639 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002640 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002641 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
2642 << AL.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002643 return;
2644 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002645
Michael Han99315932013-01-24 16:46:58 +00002646 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002647 BlocksAttr(AL.getRange(), S.Context, type,
2648 AL.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002649}
2650
Erich Keanee891aa92018-07-13 15:07:47 +00002651static void handleSentinelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002652 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002653 if (AL.getNumArgs() > 0) {
2654 Expr *E = AL.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002655 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002656 if (E->isTypeDependent() || E->isValueDependent() ||
2657 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002658 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
2659 << AL.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002660 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002661 return;
2662 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002663
John McCallb46f2872011-09-09 07:56:05 +00002664 if (Idx.isSigned() && Idx.isNegative()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002665 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_less_than_zero)
Chris Lattner3b054132008-11-19 05:08:23 +00002666 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002667 return;
2668 }
John McCallb46f2872011-09-09 07:56:05 +00002669
2670 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002671 }
2672
Aaron Ballman18a78382013-11-21 00:28:23 +00002673 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002674 if (AL.getNumArgs() > 1) {
2675 Expr *E = AL.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002676 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002677 if (E->isTypeDependent() || E->isValueDependent() ||
2678 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002679 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
2680 << AL.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002681 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002682 return;
2683 }
2684 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002685
John McCallb46f2872011-09-09 07:56:05 +00002686 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002687 // FIXME: This error message could be improved, it would be nice
2688 // to say what the bounds actually are.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002689 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
Chris Lattner3b054132008-11-19 05:08:23 +00002690 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002691 return;
2692 }
2693 }
2694
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002695 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002696 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002697 if (isa<FunctionNoProtoType>(FT)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002698 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_named_arguments);
Chris Lattner9363e312009-03-17 23:03:47 +00002699 return;
2700 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002701
Chris Lattner9363e312009-03-17 23:03:47 +00002702 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002703 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002704 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002705 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002706 } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002707 if (!MD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002708 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002709 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002710 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002711 } else if (const auto *BD = dyn_cast<BlockDecl>(D)) {
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002712 if (!BD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002713 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002714 return;
2715 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002716 } else if (const auto *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002717 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002718 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002719 const FunctionType *FT = Ty->isFunctionPointerType()
2720 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002721 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002722 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002723 int m = Ty->isFunctionPointerType() ? 0 : 1;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002724 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002725 return;
2726 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002727 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002728 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
2729 << AL.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002730 return;
2731 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002732 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002733 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
2734 << AL.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002735 return;
2736 }
Michael Han99315932013-01-24 16:46:58 +00002737 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002738 SentinelAttr(AL.getRange(), S.Context, sentinel, nullPos,
2739 AL.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002740}
2741
Erich Keanee891aa92018-07-13 15:07:47 +00002742static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL) {
Alp Toker314cc812014-01-25 16:55:45 +00002743 if (D->getFunctionType() &&
2744 D->getFunctionType()->getReturnType()->isVoidType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002745 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method)
2746 << AL.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002747 return;
2748 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002749 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002750 if (MD->getReturnType()->isVoidType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002751 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method)
2752 << AL.getName() << 1;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002753 return;
2754 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002755
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002756 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
Aaron Ballmane7964782016-03-07 22:44:55 +00002757 // about using it as an extension.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002758 if (!S.getLangOpts().CPlusPlus17 && AL.isCXX11Attribute() &&
2759 !AL.getScopeName())
2760 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL.getName();
Aaron Ballmane7964782016-03-07 22:44:55 +00002761
Fangrui Song6907ce22018-07-30 19:24:48 +00002762 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002763 WarnUnusedResultAttr(AL.getRange(), S.Context,
2764 AL.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002765}
2766
Erich Keanee891aa92018-07-13 15:07:47 +00002767static void handleWeakImportAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002768 // weak_import only applies to variable & function declarations.
2769 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002770 if (!D->canBeWeakImported(isDef)) {
2771 if (isDef)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002772 S.Diag(AL.getLoc(), diag::warn_attribute_invalid_on_definition)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002773 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002774 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002775 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002776 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002777 // Nothing to warn about here.
2778 } else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002779 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
2780 << AL.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002781
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002782 return;
2783 }
2784
Michael Han99315932013-01-24 16:46:58 +00002785 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002786 WeakImportAttr(AL.getRange(), S.Context,
2787 AL.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002788}
2789
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002790// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002791template <typename WorkGroupAttr>
Erich Keanee891aa92018-07-13 15:07:47 +00002792static void handleWorkGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002793 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002794 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002795 const Expr *E = AL.getArgAsExpr(i);
2796 if (!checkUInt32Argument(S, AL, E, WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002797 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002798 if (WGSize[i] == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002799 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
2800 << AL.getName() << E->getSourceRange();
Joey Goulyb1d23a82014-05-19 14:41:38 +00002801 return;
2802 }
2803 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002804
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002805 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2806 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2807 Existing->getYDim() == WGSize[1] &&
2808 Existing->getZDim() == WGSize[2]))
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002809 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002810
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002811 D->addAttr(::new (S.Context) WorkGroupAttr(AL.getRange(), S.Context,
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002812 WGSize[0], WGSize[1], WGSize[2],
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002813 AL.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002814}
2815
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002816// Handles intel_reqd_sub_group_size.
Erich Keanee891aa92018-07-13 15:07:47 +00002817static void handleSubGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002818 uint32_t SGSize;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002819 const Expr *E = AL.getArgAsExpr(0);
2820 if (!checkUInt32Argument(S, AL, E, SGSize))
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002821 return;
2822 if (SGSize == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002823 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
2824 << AL.getName() << E->getSourceRange();
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002825 return;
2826 }
2827
2828 OpenCLIntelReqdSubGroupSizeAttr *Existing =
2829 D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>();
2830 if (Existing && Existing->getSubGroupSize() != SGSize)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002831 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL.getName();
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002832
2833 D->addAttr(::new (S.Context) OpenCLIntelReqdSubGroupSizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002834 AL.getRange(), S.Context, SGSize,
2835 AL.getAttributeSpellingListIndex()));
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002836}
2837
Erich Keanee891aa92018-07-13 15:07:47 +00002838static void handleVecTypeHint(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002839 if (!AL.hasParsedType()) {
2840 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
2841 << AL.getName() << 1;
Aaron Ballman00e99962013-08-31 01:11:41 +00002842 return;
2843 }
2844
Craig Topperc3ec1492014-05-26 06:22:03 +00002845 TypeSourceInfo *ParmTSI = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002846 QualType ParmType = S.GetTypeFromParser(AL.getTypeArg(), &ParmTSI);
Richard Smithb87c4652013-10-31 21:23:20 +00002847 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002848
2849 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2850 (ParmType->isBooleanType() ||
2851 !ParmType->isIntegralType(S.getASTContext()))) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002852 S.Diag(AL.getLoc(), diag::err_attribute_argument_vec_type_hint)
Joey Goulyaba589c2013-03-08 09:42:32 +00002853 << ParmType;
2854 return;
2855 }
2856
Aaron Ballmana9e05402013-12-02 22:16:55 +00002857 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002858 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002859 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL.getName();
Joey Goulyaba589c2013-03-08 09:42:32 +00002860 return;
2861 }
2862 }
2863
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002864 D->addAttr(::new (S.Context) VecTypeHintAttr(AL.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002865 ParmTSI,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002866 AL.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002867}
2868
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002869SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002870 StringRef Name,
2871 unsigned AttrSpellingListIndex) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002872 // Explicit or partial specializations do not inherit
2873 // the section attribute from the primary template.
2874 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2875 if (AttrSpellingListIndex == SectionAttr::Declspec_allocate &&
2876 FD->isFunctionTemplateSpecialization())
2877 return nullptr;
2878 }
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002879 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2880 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002881 return nullptr;
Erich Keane7963e8b2018-07-18 20:04:48 +00002882 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
2883 << 1 /*section*/;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002884 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002885 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002886 }
Michael Han99315932013-01-24 16:46:58 +00002887 return ::new (Context) SectionAttr(Range, Context, Name,
2888 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002889}
2890
Reid Kleckner2a133222015-03-04 23:39:17 +00002891bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2892 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2893 if (!Error.empty()) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002894 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error
Fangrui Song6907ce22018-07-30 19:24:48 +00002895 << 1 /*'section'*/;
Reid Kleckner2a133222015-03-04 23:39:17 +00002896 return false;
2897 }
2898 return true;
2899}
2900
Erich Keanee891aa92018-07-13 15:07:47 +00002901static void handleSectionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002902 // Make sure that there is a string literal as the sections's single
2903 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002904 StringRef Str;
2905 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002906 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002907 return;
Mike Stump11289f42009-09-09 15:08:12 +00002908
Reid Kleckner2a133222015-03-04 23:39:17 +00002909 if (!S.checkSectionName(LiteralLoc, Str))
2910 return;
2911
Chris Lattner30ba6742009-08-10 19:03:04 +00002912 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002913 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002914 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002915 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002916 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002917 return;
2918 }
Mike Stump11289f42009-09-09 15:08:12 +00002919
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002920 unsigned Index = AL.getAttributeSpellingListIndex();
2921 SectionAttr *NewAttr = S.mergeSectionAttr(D, AL.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002922 if (NewAttr)
2923 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002924}
2925
Erich Keane7963e8b2018-07-18 20:04:48 +00002926static bool checkCodeSegName(Sema&S, SourceLocation LiteralLoc, StringRef CodeSegName) {
2927 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(CodeSegName);
2928 if (!Error.empty()) {
2929 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error
2930 << 0 /*'code-seg'*/;
2931 return false;
2932 }
2933 return true;
2934}
2935
2936CodeSegAttr *Sema::mergeCodeSegAttr(Decl *D, SourceRange Range,
2937 StringRef Name,
2938 unsigned AttrSpellingListIndex) {
2939 // Explicit or partial specializations do not inherit
2940 // the code_seg attribute from the primary template.
2941 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2942 if (FD->isFunctionTemplateSpecialization())
2943 return nullptr;
2944 }
2945 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
2946 if (ExistingAttr->getName() == Name)
2947 return nullptr;
2948 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
2949 << 0 /*codeseg*/;
2950 Diag(Range.getBegin(), diag::note_previous_attribute);
2951 return nullptr;
2952 }
2953 return ::new (Context) CodeSegAttr(Range, Context, Name,
2954 AttrSpellingListIndex);
2955}
2956
2957static void handleCodeSegAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2958 StringRef Str;
2959 SourceLocation LiteralLoc;
2960 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
2961 return;
2962 if (!checkCodeSegName(S, LiteralLoc, Str))
2963 return;
2964 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
2965 if (!ExistingAttr->isImplicit()) {
2966 S.Diag(AL.getLoc(),
2967 ExistingAttr->getName() == Str
2968 ? diag::warn_duplicate_codeseg_attribute
2969 : diag::err_conflicting_codeseg_attribute);
2970 return;
2971 }
2972 D->dropAttr<CodeSegAttr>();
2973 }
2974 if (CodeSegAttr *CSA = S.mergeCodeSegAttr(D, AL.getRange(), Str,
2975 AL.getAttributeSpellingListIndex()))
2976 D->addAttr(CSA);
2977}
2978
Erich Keane57e15cd2017-07-19 22:06:33 +00002979// Check for things we'd like to warn about. Multiversioning issues are
2980// handled later in the process, once we know how many exist.
2981bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
2982 enum FirstParam { Unsupported, Duplicate };
2983 enum SecondParam { None, Architecture };
Eric Christopher789a7ad2015-06-12 01:36:05 +00002984 for (auto Str : {"tune=", "fpmath="})
2985 if (AttrStr.find(Str) != StringRef::npos)
Erich Keane57e15cd2017-07-19 22:06:33 +00002986 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
2987 << Unsupported << None << Str;
2988
2989 TargetAttr::ParsedTargetAttr ParsedAttrs = TargetAttr::parse(AttrStr);
2990
2991 if (!ParsedAttrs.Architecture.empty() &&
2992 !Context.getTargetInfo().isValidCPUName(ParsedAttrs.Architecture))
2993 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
2994 << Unsupported << Architecture << ParsedAttrs.Architecture;
2995
2996 if (ParsedAttrs.DuplicateArchitecture)
2997 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
2998 << Duplicate << None << "arch=";
2999
3000 for (const auto &Feature : ParsedAttrs.Features) {
3001 auto CurFeature = StringRef(Feature).drop_front(); // remove + or -.
3002 if (!Context.getTargetInfo().isValidFeatureName(CurFeature))
3003 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3004 << Unsupported << None << CurFeature;
3005 }
3006
Erich Keane29636aa2018-02-16 17:31:59 +00003007 return false;
Eric Christopher789a7ad2015-06-12 01:36:05 +00003008}
3009
Erich Keanee891aa92018-07-13 15:07:47 +00003010static void handleTargetAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Eric Christopher11acf732015-06-12 01:35:52 +00003011 StringRef Str;
3012 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003013 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc) ||
Erich Keane29636aa2018-02-16 17:31:59 +00003014 S.checkTargetAttr(LiteralLoc, Str))
Eric Christopher11acf732015-06-12 01:35:52 +00003015 return;
Erich Keane29636aa2018-02-16 17:31:59 +00003016
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003017 unsigned Index = AL.getAttributeSpellingListIndex();
Eric Christopher11acf732015-06-12 01:35:52 +00003018 TargetAttr *NewAttr =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003019 ::new (S.Context) TargetAttr(AL.getRange(), S.Context, Str, Index);
Eric Christopher11acf732015-06-12 01:35:52 +00003020 D->addAttr(NewAttr);
3021}
3022
Erich Keanee891aa92018-07-13 15:07:47 +00003023static void handleMinVectorWidthAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topper74c10e32018-07-09 19:00:16 +00003024 Expr *E = AL.getArgAsExpr(0);
3025 uint32_t VecWidth;
3026 if (!checkUInt32Argument(S, AL, E, VecWidth)) {
3027 AL.setInvalid();
3028 return;
3029 }
3030
3031 MinVectorWidthAttr *Existing = D->getAttr<MinVectorWidthAttr>();
3032 if (Existing && Existing->getVectorWidth() != VecWidth) {
3033 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL.getName();
3034 return;
3035 }
3036
3037 D->addAttr(::new (S.Context)
3038 MinVectorWidthAttr(AL.getRange(), S.Context, VecWidth,
3039 AL.getAttributeSpellingListIndex()));
3040}
3041
Erich Keanee891aa92018-07-13 15:07:47 +00003042static void handleCleanupAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003043 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003044 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00003045 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003046 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00003047
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003048 // gcc only allows for simple identifiers. Since we support more than gcc, we
3049 // will warn the user.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003050 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003051 if (DRE->hasQualifier())
3052 S.Diag(Loc, diag::warn_cleanup_ext);
3053 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
3054 NI = DRE->getNameInfo();
3055 if (!FD) {
3056 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
3057 << NI.getName();
3058 return;
3059 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003060 } else if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003061 if (ULE->hasExplicitTemplateArgs())
3062 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003063 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
3064 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00003065 if (!FD) {
3066 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
3067 << NI.getName();
3068 if (ULE->getType() == S.Context.OverloadTy)
3069 S.NoteAllOverloadCandidates(ULE);
3070 return;
3071 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003072 } else {
3073 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00003074 return;
3075 }
3076
Anders Carlssond277d792009-01-31 01:16:18 +00003077 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003078 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
3079 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00003080 return;
3081 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003082
Anders Carlsson723f55d2009-02-07 23:16:50 +00003083 // We're currently more strict than GCC about what function types we accept.
3084 // If this ever proves to be a problem it should be easy to fix.
Aaron Ballman3b70e752017-12-01 16:53:49 +00003085 QualType Ty = S.Context.getPointerType(cast<VarDecl>(D)->getType());
Anders Carlsson723f55d2009-02-07 23:16:50 +00003086 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00003087 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
3088 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003089 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
3090 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00003091 return;
3092 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003093
Michael Han99315932013-01-24 16:46:58 +00003094 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003095 CleanupAttr(AL.getRange(), S.Context, FD,
3096 AL.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00003097}
3098
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003099static void handleEnumExtensibilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00003100 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003101 if (!AL.isArgIdent(0)) {
3102 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
3103 << AL.getName() << 0 << AANT_ArgumentIdentifier;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003104 return;
3105 }
3106
3107 EnumExtensibilityAttr::Kind ExtensibilityKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003108 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003109 if (!EnumExtensibilityAttr::ConvertStrToKind(II->getName(),
3110 ExtensibilityKind)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003111 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
3112 << AL.getName() << II;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003113 return;
3114 }
3115
3116 D->addAttr(::new (S.Context) EnumExtensibilityAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003117 AL.getRange(), S.Context, ExtensibilityKind,
3118 AL.getAttributeSpellingListIndex()));
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003119}
3120
Mike Stumpd3bb5572009-07-24 19:02:52 +00003121/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00003122/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003123static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003124 Expr *IdxExpr = AL.getArgAsExpr(0);
Joel E. Denny81508102018-03-13 14:51:22 +00003125 ParamIdx Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003126 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003127 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00003128
Eric Christopherb64963e2015-08-13 21:34:35 +00003129 // Make sure the format string is really a string.
Joel E. Denny81508102018-03-13 14:51:22 +00003130 QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Mike Stumpd3bb5572009-07-24 19:02:52 +00003131
Eric Christopherb64963e2015-08-13 21:34:35 +00003132 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
3133 if (NotNSStringTy &&
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003134 !isCFStringType(Ty, S.Context) &&
3135 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003136 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003137 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003138 << "a string type" << IdxExpr->getSourceRange()
3139 << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003140 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003141 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003142 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003143 if (!isNSStringType(Ty, S.Context) &&
3144 !isCFStringType(Ty, S.Context) &&
3145 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003146 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003147 S.Diag(AL.getLoc(), diag::err_format_attribute_result_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003148 << (NotNSStringTy ? "string type" : "NSString")
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00003149 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003150 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003151 }
3152
Joel E. Denny81508102018-03-13 14:51:22 +00003153 D->addAttr(::new (S.Context) FormatArgAttr(
3154 AL.getRange(), S.Context, Idx, AL.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003155}
3156
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003157enum FormatAttrKind {
3158 CFStringFormat,
3159 NSStringFormat,
3160 StrftimeFormat,
3161 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003162 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003163 InvalidFormat
3164};
3165
3166/// getFormatAttrKind - Map from format attribute names to supported format
3167/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003168static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003169 return llvm::StringSwitch<FormatAttrKind>(Format)
Mehdi Amini06d367c2016-10-24 20:39:34 +00003170 // Check for formats that get handled specially.
3171 .Case("NSString", NSStringFormat)
3172 .Case("CFString", CFStringFormat)
3173 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003174
Mehdi Amini06d367c2016-10-24 20:39:34 +00003175 // Otherwise, check for supported formats.
3176 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3177 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3178 .Case("kprintf", SupportedFormat) // OpenBSD.
3179 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
3180 .Case("os_trace", SupportedFormat)
3181 .Case("os_log", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003182
Mehdi Amini06d367c2016-10-24 20:39:34 +00003183 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3184 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003185}
3186
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003187/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003188/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003189static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003190 if (!S.getLangOpts().CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003191 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL.getName();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003192 return;
3193 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003194
Aaron Ballman4a611152013-11-27 16:34:09 +00003195 if (S.getCurFunctionOrMethodDecl()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003196 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3197 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003198 return;
3199 }
Aaron Ballman4a611152013-11-27 16:34:09 +00003200 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003201 if (S.Context.getAsArrayType(T))
3202 T = S.Context.getBaseElementType(T);
3203 if (!T->getAs<RecordType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003204 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3205 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003206 return;
3207 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003208
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003209 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003210 uint32_t prioritynum;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003211 if (!checkUInt32Argument(S, AL, E, prioritynum)) {
3212 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003213 return;
3214 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003215
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003216 if (prioritynum < 101 || prioritynum > 65535) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003217 S.Diag(AL.getLoc(), diag::err_attribute_argument_outof_range)
3218 << E->getSourceRange() << AL.getName() << 101 << 65535;
3219 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003220 return;
3221 }
Michael Han99315932013-01-24 16:46:58 +00003222 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003223 InitPriorityAttr(AL.getRange(), S.Context, prioritynum,
3224 AL.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003225}
3226
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003227FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3228 IdentifierInfo *Format, int FormatIdx,
3229 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00003230 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003231 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003232 for (auto *F : D->specific_attrs<FormatAttr>()) {
3233 if (F->getType() == Format &&
3234 F->getFormatIdx() == FormatIdx &&
3235 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003236 // If we don't have a valid location for this attribute, adopt the
3237 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003238 if (F->getLocation().isInvalid())
3239 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00003240 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00003241 }
3242 }
3243
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003244 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3245 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003246}
3247
Mike Stumpd3bb5572009-07-24 19:02:52 +00003248/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003249/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003250static void handleFormatAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003251 if (!AL.isArgIdent(0)) {
3252 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
3253 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003254 return;
3255 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003256
Chandler Carruth743682b2010-11-16 08:35:43 +00003257 // In C++ the implicit 'this' function parameter also counts, and they are
3258 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003259 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00003260 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003261
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003262 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Aaron Ballman00e99962013-08-31 01:11:41 +00003263 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003264
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00003265 if (normalizeName(Format)) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003266 // If we've modified the string name, we need a new identifier for it.
3267 II = &S.Context.Idents.get(Format);
3268 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003269
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003270 // Check for supported formats.
3271 FormatAttrKind Kind = getFormatAttrKind(Format);
Fangrui Song6907ce22018-07-30 19:24:48 +00003272
Chris Lattner12161d32010-03-22 21:08:50 +00003273 if (Kind == IgnoredFormat)
3274 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00003275
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003276 if (Kind == InvalidFormat) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003277 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
3278 << AL.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003279 return;
3280 }
3281
3282 // checks for the 2nd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003283 Expr *IdxExpr = AL.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003284 uint32_t Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003285 if (!checkUInt32Argument(S, AL, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003286 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003287
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003288 if (Idx < 1 || Idx > NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003289 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3290 << AL.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003291 return;
3292 }
3293
3294 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003295 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003296
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003297 if (HasImplicitThisParam) {
3298 if (ArgIdx == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003299 S.Diag(AL.getLoc(),
Chandler Carruth743682b2010-11-16 08:35:43 +00003300 diag::err_format_attribute_implicit_this_format_string)
3301 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003302 return;
3303 }
3304 ArgIdx--;
3305 }
Mike Stump11289f42009-09-09 15:08:12 +00003306
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003307 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00003308 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003309
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003310 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003311 if (!isCFStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003312 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003313 << "a CFString" << IdxExpr->getSourceRange()
3314 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00003315 return;
3316 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003317 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003318 // FIXME: do we need to check if the type is NSString*? What are the
3319 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003320 if (!isNSStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003321 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003322 << "an NSString" << IdxExpr->getSourceRange()
3323 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003324 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003325 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003326 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003327 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003328 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003329 << "a string type" << IdxExpr->getSourceRange()
3330 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003331 return;
3332 }
3333
3334 // check the 3rd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003335 Expr *FirstArgExpr = AL.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003336 uint32_t FirstArg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003337 if (!checkUInt32Argument(S, AL, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003338 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003339
3340 // check if the function is variadic if the 3rd argument non-zero
3341 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003342 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003343 ++NumArgs; // +1 for ...
3344 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003345 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003346 return;
3347 }
3348 }
3349
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003350 // strftime requires FirstArg to be 0 because it doesn't read from any
3351 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003352 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003353 if (FirstArg != 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003354 S.Diag(AL.getLoc(), diag::err_format_strftime_third_parameter)
Chris Lattner3b054132008-11-19 05:08:23 +00003355 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003356 return;
3357 }
3358 // if 0 it disables parameter checking (to use with e.g. va_list)
3359 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003360 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3361 << AL.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003362 return;
3363 }
3364
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003365 FormatAttr *NewAttr = S.mergeFormatAttr(D, AL.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003366 Idx, FirstArg,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003367 AL.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003368 if (NewAttr)
3369 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003370}
3371
Erich Keanee891aa92018-07-13 15:07:47 +00003372static void handleTransparentUnionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003373 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00003374 RecordDecl *RD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003375 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003376 if (TD && TD->getUnderlyingType()->isUnionType())
3377 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3378 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003379 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003380
3381 if (!RD || !RD->isUnion()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003382 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
3383 << AL.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003384 return;
3385 }
3386
John McCallf937c022011-10-07 06:10:15 +00003387 if (!RD->isCompleteDefinition()) {
Erich Keane2fe684b2017-02-28 20:44:39 +00003388 if (!RD->isBeingDefined())
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003389 S.Diag(AL.getLoc(),
Erich Keane2fe684b2017-02-28 20:44:39 +00003390 diag::warn_transparent_union_attribute_not_definition);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003391 return;
3392 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003393
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003394 RecordDecl::field_iterator Field = RD->field_begin(),
3395 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003396 if (Field == FieldEnd) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003397 S.Diag(AL.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003398 return;
3399 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003400
David Blaikie40ed2972012-06-06 20:45:41 +00003401 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003402 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003403 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003404 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003405 diag::warn_transparent_union_attribute_floating)
3406 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003407 return;
3408 }
3409
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003410 if (FirstType->isIncompleteType())
3411 return;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003412 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3413 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3414 for (; Field != FieldEnd; ++Field) {
3415 QualType FieldType = Field->getType();
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003416 if (FieldType->isIncompleteType())
3417 return;
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003418 // FIXME: this isn't fully correct; we also need to test whether the
3419 // members of the union would all have the same calling convention as the
3420 // first member of the union. Checking just the size and alignment isn't
3421 // sufficient (consider structs passed on the stack instead of in registers
3422 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003423 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003424 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003425 // Warn if we drop the attribute.
3426 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003427 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003428 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003429 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003430 diag::warn_transparent_union_attribute_field_size_align)
3431 << isSize << Field->getDeclName() << FieldBits;
3432 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003433 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003434 diag::note_transparent_union_first_field_size_align)
3435 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003436 return;
3437 }
3438 }
3439
Michael Han99315932013-01-24 16:46:58 +00003440 RD->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003441 TransparentUnionAttr(AL.getRange(), S.Context,
3442 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003443}
3444
Erich Keanee891aa92018-07-13 15:07:47 +00003445static void handleAnnotateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003446 // Make sure that there is a string literal as the annotation's single
3447 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003448 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003449 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003450 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003451
3452 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003453 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
3454 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003455 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003456 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003457
Michael Han99315932013-01-24 16:46:58 +00003458 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003459 AnnotateAttr(AL.getRange(), S.Context, Str,
3460 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003461}
3462
Erich Keanee891aa92018-07-13 15:07:47 +00003463static void handleAlignValueAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003464 S.AddAlignValueAttr(AL.getRange(), D, AL.getArgAsExpr(0),
3465 AL.getAttributeSpellingListIndex());
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003466}
3467
3468void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
3469 unsigned SpellingListIndex) {
3470 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
3471 SourceLocation AttrLoc = AttrRange.getBegin();
3472
3473 QualType T;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003474 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003475 T = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003476 else if (const auto *VD = dyn_cast<ValueDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003477 T = VD->getType();
3478 else
3479 llvm_unreachable("Unknown decl type for align_value");
3480
3481 if (!T->isDependentType() && !T->isAnyPointerType() &&
3482 !T->isReferenceType() && !T->isMemberPointerType()) {
3483 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
3484 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
3485 return;
3486 }
3487
3488 if (!E->isValueDependent()) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003489 llvm::APSInt Alignment;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003490 ExprResult ICE
3491 = VerifyIntegerConstantExpression(E, &Alignment,
3492 diag::err_align_value_attribute_argument_not_int,
3493 /*AllowFold*/ false);
3494 if (ICE.isInvalid())
3495 return;
3496
3497 if (!Alignment.isPowerOf2()) {
3498 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3499 << E->getSourceRange();
3500 return;
3501 }
3502
3503 D->addAttr(::new (Context)
3504 AlignValueAttr(AttrRange, Context, ICE.get(),
3505 SpellingListIndex));
3506 return;
3507 }
3508
3509 // Save dependent expressions in the AST to be instantiated.
3510 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003511}
3512
Erich Keanee891aa92018-07-13 15:07:47 +00003513static void handleAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003514 // check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003515 if (AL.getNumArgs() > 1) {
3516 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
3517 << AL.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003518 return;
3519 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003520
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003521 if (AL.getNumArgs() == 0) {
3522 D->addAttr(::new (S.Context) AlignedAttr(AL.getRange(), S.Context,
3523 true, nullptr, AL.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00003524 return;
3525 }
3526
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003527 Expr *E = AL.getArgAsExpr(0);
3528 if (AL.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3529 S.Diag(AL.getEllipsisLoc(),
Richard Smith44c247f2013-02-22 08:32:16 +00003530 diag::err_pack_expansion_without_parameter_packs);
3531 return;
3532 }
3533
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003534 if (!AL.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
Richard Smith44c247f2013-02-22 08:32:16 +00003535 return;
3536
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003537 S.AddAlignedAttr(AL.getRange(), D, E, AL.getAttributeSpellingListIndex(),
3538 AL.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003539}
3540
3541void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003542 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003543 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3544 SourceLocation AttrLoc = AttrRange.getBegin();
3545
Richard Smith1dba27c2013-01-29 09:02:09 +00003546 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003547 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003548 // C++11 [dcl.align]p1:
3549 // An alignment-specifier may be applied to a variable or to a class
3550 // data member, but it shall not be applied to a bit-field, a function
3551 // parameter, the formal parameter of a catch clause, or a variable
3552 // declared with the register storage class specifier. An
3553 // alignment-specifier may also be applied to the declaration of a class
3554 // or enumeration type.
3555 // C11 6.7.5/2:
3556 // An alignment attribute shall not be specified in a declaration of
3557 // a typedef, or a bit-field, or a function, or a parameter, or an
3558 // object declared with the register storage-class specifier.
3559 int DiagKind = -1;
3560 if (isa<ParmVarDecl>(D)) {
3561 DiagKind = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003562 } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003563 if (VD->getStorageClass() == SC_Register)
3564 DiagKind = 1;
3565 if (VD->isExceptionVariable())
3566 DiagKind = 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003567 } else if (const auto *FD = dyn_cast<FieldDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003568 if (FD->isBitField())
3569 DiagKind = 3;
3570 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00003571 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00003572 << (TmpAttr.isC11() ? ExpectedVariableOrField
3573 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003574 return;
3575 }
3576 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003577 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00003578 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003579 return;
3580 }
3581 }
3582
Richard Smith90ae9672018-06-20 23:36:55 +00003583 if (E->isValueDependent()) {
3584 // We can't support a dependent alignment on a non-dependent type,
3585 // because we have no way to model that a type is "alignment-dependent"
3586 // but not dependent in any other way.
3587 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3588 if (!TND->getUnderlyingType()->isDependentType()) {
3589 Diag(AttrLoc, diag::err_alignment_dependent_typedef_name)
3590 << E->getSourceRange();
3591 return;
3592 }
3593 }
3594
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003595 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003596 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3597 AA->setPackExpansion(IsPackExpansion);
3598 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003599 return;
3600 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003601
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003602 // FIXME: Cache the number on the AL object?
David Majnemer0be6bd02015-07-26 09:02:21 +00003603 llvm::APSInt Alignment;
Douglas Gregore2b37442012-05-04 22:38:52 +00003604 ExprResult ICE
3605 = VerifyIntegerConstantExpression(E, &Alignment,
3606 diag::err_aligned_attribute_argument_not_int,
3607 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003608 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003609 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003610
David Majnemer0be6bd02015-07-26 09:02:21 +00003611 uint64_t AlignVal = Alignment.getZExtValue();
3612
Richard Smith848e1f12013-02-01 08:12:08 +00003613 // C++11 [dcl.align]p2:
3614 // -- if the constant expression evaluates to zero, the alignment
3615 // specifier shall have no effect
3616 // C11 6.7.5p6:
3617 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003618 if (!(TmpAttr.isAlignas() && !Alignment)) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003619 if (!llvm::isPowerOf2_64(AlignVal)) {
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003620 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3621 << E->getSourceRange();
3622 return;
3623 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003624 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003625
David Majnemerabecae72014-02-12 20:36:10 +00003626 // Alignment calculations can wrap around if it's greater than 2**28.
David Majnemer29c69db2015-07-26 01:48:59 +00003627 unsigned MaxValidAlignment =
3628 Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192
3629 : 268435456;
David Majnemer0be6bd02015-07-26 09:02:21 +00003630 if (AlignVal > MaxValidAlignment) {
David Majnemerabecae72014-02-12 20:36:10 +00003631 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
3632 << E->getSourceRange();
3633 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003634 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003635
David Majnemer0be6bd02015-07-26 09:02:21 +00003636 if (Context.getTargetInfo().isTLSSupported()) {
3637 unsigned MaxTLSAlign =
3638 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3639 .getQuantity();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003640 const auto *VD = dyn_cast<VarDecl>(D);
David Majnemer0be6bd02015-07-26 09:02:21 +00003641 if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD &&
3642 VD->getTLSKind() != VarDecl::TLS_None) {
3643 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3644 << (unsigned)AlignVal << VD << MaxTLSAlign;
3645 return;
3646 }
3647 }
3648
Richard Smith44c247f2013-02-22 08:32:16 +00003649 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003650 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00003651 AA->setPackExpansion(IsPackExpansion);
3652 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003653}
3654
Michael Hanaf02bbe2013-02-01 01:19:17 +00003655void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003656 unsigned SpellingListIndex, bool IsPackExpansion) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003657 // FIXME: Cache the number on the AL object if non-dependent?
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003658 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003659 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3660 SpellingListIndex);
3661 AA->setPackExpansion(IsPackExpansion);
3662 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003663}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003664
Richard Smith848e1f12013-02-01 08:12:08 +00003665void Sema::CheckAlignasUnderalignment(Decl *D) {
3666 assert(D->hasAttrs() && "no attributes on decl");
3667
David Majnemer475b25e2015-01-21 10:54:38 +00003668 QualType UnderlyingTy, DiagTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003669 if (const auto *VD = dyn_cast<ValueDecl>(D)) {
David Majnemer475b25e2015-01-21 10:54:38 +00003670 UnderlyingTy = DiagTy = VD->getType();
3671 } else {
3672 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003673 if (const auto *ED = dyn_cast<EnumDecl>(D))
David Majnemer475b25e2015-01-21 10:54:38 +00003674 UnderlyingTy = ED->getIntegerType();
3675 }
3676 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003677 return;
3678
3679 // C++11 [dcl.align]p5, C11 6.7.5/4:
3680 // The combined effect of all alignment attributes in a declaration shall
3681 // not specify an alignment that is less strict than the alignment that
3682 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003683 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003684 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003685 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003686 if (I->isAlignmentDependent())
3687 return;
3688 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003689 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003690 Align = std::max(Align, I->getAlignment(Context));
3691 }
3692
3693 if (AlignasAttr && Align) {
3694 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003695 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003696 if (NaturalAlign > RequestedAlign)
3697 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003698 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003699 }
3700}
3701
David Majnemer2c4e00a2014-01-29 22:07:36 +00003702bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003703 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003704 MSInheritanceAttr::Spelling SemanticSpelling) {
3705 assert(RD->hasDefinition() && "RD has no definition!");
3706
David Majnemer98c9ee22014-02-07 00:43:07 +00003707 // We may not have seen base specifiers or any virtual methods yet. We will
3708 // have to wait until the record is defined to catch any mismatches.
3709 if (!RD->getDefinition()->isCompleteDefinition())
3710 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003711
David Majnemer98c9ee22014-02-07 00:43:07 +00003712 // The unspecified model never matches what a definition could need.
3713 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
3714 return false;
3715
David Majnemer4bb09802014-02-10 19:50:15 +00003716 if (BestCase) {
3717 if (RD->calculateInheritanceModel() == SemanticSpelling)
3718 return false;
3719 } else {
3720 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3721 return false;
3722 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003723
3724 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3725 << 0 /*definition*/;
3726 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3727 << RD->getNameAsString();
3728 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003729}
3730
Alexey Bataevf278eb12015-11-19 10:13:11 +00003731/// parseModeAttrArg - Parses attribute mode string and returns parsed type
3732/// attribute.
3733static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
3734 bool &IntegerMode, bool &ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003735 IntegerMode = true;
3736 ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003737 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003738 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003739 switch (Str[0]) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003740 case 'Q':
3741 DestWidth = 8;
3742 break;
3743 case 'H':
3744 DestWidth = 16;
3745 break;
3746 case 'S':
3747 DestWidth = 32;
3748 break;
3749 case 'D':
3750 DestWidth = 64;
3751 break;
3752 case 'X':
3753 DestWidth = 96;
3754 break;
3755 case 'T':
3756 DestWidth = 128;
3757 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003758 }
3759 if (Str[1] == 'F') {
3760 IntegerMode = false;
3761 } else if (Str[1] == 'C') {
3762 IntegerMode = false;
3763 ComplexMode = true;
3764 } else if (Str[1] != 'I') {
3765 DestWidth = 0;
3766 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003767 break;
3768 case 4:
3769 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3770 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003771 if (Str == "word")
Reid Klecknerf27e7522016-02-01 18:58:24 +00003772 DestWidth = S.Context.getTargetInfo().getRegisterWidth();
Daniel Dunbarafff4342009-10-18 02:09:24 +00003773 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003774 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003775 break;
3776 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003777 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003778 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003779 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003780 case 11:
3781 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003782 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003783 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003784 }
Alexey Bataevf278eb12015-11-19 10:13:11 +00003785}
3786
3787/// handleModeAttr - This attribute modifies the width of a decl with primitive
3788/// type.
3789///
3790/// Despite what would be logical, the mode attribute is a decl attribute, not a
3791/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3792/// HImode, not an intermediate pointer.
Erich Keanee891aa92018-07-13 15:07:47 +00003793static void handleModeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003794 // This attribute isn't documented, but glibc uses it. It changes
3795 // the width of an int or unsigned int to the specified size.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003796 if (!AL.isArgIdent(0)) {
3797 S.Diag(AL.getLoc(), diag::err_attribute_argument_type) << AL.getName()
Alexey Bataevf278eb12015-11-19 10:13:11 +00003798 << AANT_ArgumentIdentifier;
3799 return;
3800 }
3801
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003802 IdentifierInfo *Name = AL.getArgAsIdent(0)->Ident;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003803
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003804 S.AddModeAttr(AL.getRange(), D, Name, AL.getAttributeSpellingListIndex());
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003805}
3806
3807void Sema::AddModeAttr(SourceRange AttrRange, Decl *D, IdentifierInfo *Name,
3808 unsigned SpellingListIndex, bool InInstantiation) {
3809 StringRef Str = Name->getName();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003810 normalizeName(Str);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003811 SourceLocation AttrLoc = AttrRange.getBegin();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003812
3813 unsigned DestWidth = 0;
3814 bool IntegerMode = true;
3815 bool ComplexMode = false;
3816 llvm::APInt VectorSize(64, 0);
3817 if (Str.size() >= 4 && Str[0] == 'V') {
3818 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
3819 size_t StrSize = Str.size();
3820 size_t VectorStringLength = 0;
3821 while ((VectorStringLength + 1) < StrSize &&
3822 isdigit(Str[VectorStringLength + 1]))
3823 ++VectorStringLength;
3824 if (VectorStringLength &&
3825 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
3826 VectorSize.isPowerOf2()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003827 parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth,
Alexey Bataevf278eb12015-11-19 10:13:11 +00003828 IntegerMode, ComplexMode);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003829 // Avoid duplicate warning from template instantiation.
3830 if (!InInstantiation)
3831 Diag(AttrLoc, diag::warn_vector_mode_deprecated);
Alexey Bataevf278eb12015-11-19 10:13:11 +00003832 } else {
3833 VectorSize = 0;
3834 }
3835 }
3836
3837 if (!VectorSize)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003838 parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode);
3839
3840 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3841 // and friends, at least with glibc.
3842 // FIXME: Make sure floating-point mappings are accurate
3843 // FIXME: Support XF and TF types
3844 if (!DestWidth) {
3845 Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name;
3846 return;
3847 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003848
3849 QualType OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003850 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003851 OldTy = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003852 else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003853 // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'.
3854 // Try to get type from enum declaration, default to int.
3855 OldTy = ED->getIntegerType();
3856 if (OldTy.isNull())
3857 OldTy = Context.IntTy;
3858 } else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003859 OldTy = cast<ValueDecl>(D)->getType();
Eli Friedman4735374e2009-03-03 06:41:03 +00003860
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003861 if (OldTy->isDependentType()) {
3862 D->addAttr(::new (Context)
3863 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
3864 return;
3865 }
3866
Alexey Bataev326057d2015-06-19 07:46:21 +00003867 // Base type can also be a vector type (see PR17453).
3868 // Distinguish between base type and base element type.
3869 QualType OldElemTy = OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003870 if (const auto *VT = OldTy->getAs<VectorType>())
Alexey Bataev326057d2015-06-19 07:46:21 +00003871 OldElemTy = VT->getElementType();
3872
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003873 // GCC allows 'mode' attribute on enumeration types (even incomplete), except
3874 // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete
3875 // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected.
3876 if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) &&
3877 VectorSize.getBoolValue()) {
3878 Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << AttrRange;
3879 return;
3880 }
3881 bool IntegralOrAnyEnumType =
3882 OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>();
3883
3884 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() &&
3885 !IntegralOrAnyEnumType)
3886 Diag(AttrLoc, diag::err_mode_not_primitive);
Eli Friedman4735374e2009-03-03 06:41:03 +00003887 else if (IntegerMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003888 if (!IntegralOrAnyEnumType)
3889 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00003890 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003891 if (!OldElemTy->isComplexType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003892 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00003893 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00003894 if (!OldElemTy->isFloatingType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003895 Diag(AttrLoc, diag::err_mode_wrong_type);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003896 }
3897
Alexey Bataev326057d2015-06-19 07:46:21 +00003898 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003899
3900 if (IntegerMode)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003901 NewElemTy = Context.getIntTypeForBitwidth(DestWidth,
3902 OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003903 else
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003904 NewElemTy = Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003905
Alexey Bataev326057d2015-06-19 07:46:21 +00003906 if (NewElemTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003907 Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003908 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003909 }
3910
Eli Friedman4735374e2009-03-03 06:41:03 +00003911 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003912 NewElemTy = Context.getComplexType(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00003913 }
3914
3915 QualType NewTy = NewElemTy;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003916 if (VectorSize.getBoolValue()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003917 NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(),
3918 VectorType::GenericVector);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003919 } else if (const auto *OldVT = OldTy->getAs<VectorType>()) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003920 // Complex machine mode does not support base vector types.
3921 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003922 Diag(AttrLoc, diag::err_complex_mode_vector_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00003923 return;
3924 }
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003925 unsigned NumElements = Context.getTypeSize(OldElemTy) *
Alexey Bataev326057d2015-06-19 07:46:21 +00003926 OldVT->getNumElements() /
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003927 Context.getTypeSize(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00003928 NewTy =
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003929 Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
Alexey Bataev326057d2015-06-19 07:46:21 +00003930 }
3931
3932 if (NewTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003933 Diag(AttrLoc, diag::err_mode_wrong_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00003934 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003935 }
3936
3937 // Install the new type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003938 if (auto *TD = dyn_cast<TypedefNameDecl>(D))
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003939 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003940 else if (auto *ED = dyn_cast<EnumDecl>(D))
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003941 ED->setIntegerType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003942 else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003943 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003944
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003945 D->addAttr(::new (Context)
3946 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003947}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003948
Erich Keanee891aa92018-07-13 15:07:47 +00003949static void handleNoDebugAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Michael Han99315932013-01-24 16:46:58 +00003950 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003951 NoDebugAttr(AL.getRange(), S.Context,
3952 AL.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003953}
3954
Paul Robinson30e41fb2014-12-15 18:57:28 +00003955AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range,
Paul Robinson080b1f32015-01-13 18:34:56 +00003956 IdentifierInfo *Ident,
Paul Robinson30e41fb2014-12-15 18:57:28 +00003957 unsigned AttrSpellingListIndex) {
3958 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003959 Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00003960 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3961 return nullptr;
3962 }
3963
3964 if (D->hasAttr<AlwaysInlineAttr>())
3965 return nullptr;
3966
3967 return ::new (Context) AlwaysInlineAttr(Range, Context,
3968 AttrSpellingListIndex);
3969}
3970
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003971CommonAttr *Sema::mergeCommonAttr(Decl *D, SourceRange Range,
3972 IdentifierInfo *Ident,
3973 unsigned AttrSpellingListIndex) {
3974 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, Range, Ident))
3975 return nullptr;
3976
3977 return ::new (Context) CommonAttr(Range, Context, AttrSpellingListIndex);
3978}
3979
3980InternalLinkageAttr *
3981Sema::mergeInternalLinkageAttr(Decl *D, SourceRange Range,
3982 IdentifierInfo *Ident,
3983 unsigned AttrSpellingListIndex) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003984 if (const auto *VD = dyn_cast<VarDecl>(D)) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003985 // Attribute applies to Var but not any subclass of it (like ParmVar,
3986 // ImplicitParm or VarTemplateSpecialization).
3987 if (VD->getKind() != Decl::Var) {
3988 Diag(Range.getBegin(), diag::warn_attribute_wrong_decl_type)
3989 << Ident << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
3990 : ExpectedVariableOrFunction);
3991 return nullptr;
3992 }
3993 // Attribute does not apply to non-static local variables.
3994 if (VD->hasLocalStorage()) {
3995 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
3996 return nullptr;
3997 }
3998 }
3999
4000 if (checkAttrMutualExclusion<CommonAttr>(*this, D, Range, Ident))
4001 return nullptr;
4002
4003 return ::new (Context)
4004 InternalLinkageAttr(Range, Context, AttrSpellingListIndex);
4005}
4006
Paul Robinson30e41fb2014-12-15 18:57:28 +00004007MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range,
4008 unsigned AttrSpellingListIndex) {
4009 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
4010 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'";
4011 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4012 return nullptr;
4013 }
4014
4015 if (D->hasAttr<MinSizeAttr>())
4016 return nullptr;
4017
4018 return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex);
4019}
4020
4021OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range,
4022 unsigned AttrSpellingListIndex) {
4023 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
4024 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
4025 Diag(Range.getBegin(), diag::note_conflicting_attribute);
4026 D->dropAttr<AlwaysInlineAttr>();
4027 }
4028 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
4029 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
4030 Diag(Range.getBegin(), diag::note_conflicting_attribute);
4031 D->dropAttr<MinSizeAttr>();
4032 }
4033
4034 if (D->hasAttr<OptimizeNoneAttr>())
4035 return nullptr;
4036
4037 return ::new (Context) OptimizeNoneAttr(Range, Context,
4038 AttrSpellingListIndex);
4039}
4040
Erich Keanee891aa92018-07-13 15:07:47 +00004041static void handleAlwaysInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004042 if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, AL.getRange(),
4043 AL.getName()))
Akira Hatanakac8667622015-11-06 23:56:15 +00004044 return;
4045
Paul Robinson080b1f32015-01-13 18:34:56 +00004046 if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004047 D, AL.getRange(), AL.getName(),
4048 AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004049 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00004050}
4051
Erich Keanee891aa92018-07-13 15:07:47 +00004052static void handleMinSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004053 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004054 D, AL.getRange(), AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004055 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00004056}
4057
Erich Keanee891aa92018-07-13 15:07:47 +00004058static void handleOptimizeNoneAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004059 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004060 D, AL.getRange(), AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004061 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00004062}
4063
Erich Keanee891aa92018-07-13 15:07:47 +00004064static void handleConstantAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004065 if (checkAttrMutualExclusion<CUDASharedAttr>(S, D, AL.getRange(),
4066 AL.getName()))
Justin Lebare71b2fa2016-09-30 23:57:34 +00004067 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004068 const auto *VD = cast<VarDecl>(D);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004069 if (!VD->hasGlobalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004070 S.Diag(AL.getLoc(), diag::err_cuda_nonglobal_constant);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004071 return;
4072 }
4073 D->addAttr(::new (S.Context) CUDAConstantAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004074 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Justin Lebare71b2fa2016-09-30 23:57:34 +00004075}
4076
Erich Keanee891aa92018-07-13 15:07:47 +00004077static void handleSharedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004078 if (checkAttrMutualExclusion<CUDAConstantAttr>(S, D, AL.getRange(),
4079 AL.getName()))
Justin Lebar10411012016-09-30 23:57:30 +00004080 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004081 const auto *VD = cast<VarDecl>(D);
Justin Lebar281ce2a2016-10-02 15:24:50 +00004082 // extern __shared__ is only allowed on arrays with no length (e.g.
4083 // "int x[]").
Jonas Hahnfeldee47d8c2018-02-14 16:04:03 +00004084 if (!S.getLangOpts().CUDARelocatableDeviceCode && VD->hasExternalStorage() &&
4085 !isa<IncompleteArrayType>(VD->getType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004086 S.Diag(AL.getLoc(), diag::err_cuda_extern_shared) << VD;
Justin Lebar10411012016-09-30 23:57:30 +00004087 return;
4088 }
Justin Lebaraa370bd2016-10-13 18:45:13 +00004089 if (S.getLangOpts().CUDA && VD->hasLocalStorage() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004090 S.CUDADiagIfHostCode(AL.getLoc(), diag::err_cuda_host_shared)
Justin Lebaraa370bd2016-10-13 18:45:13 +00004091 << S.CurrentCUDATarget())
4092 return;
Justin Lebar10411012016-09-30 23:57:30 +00004093 D->addAttr(::new (S.Context) CUDASharedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004094 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Justin Lebar10411012016-09-30 23:57:30 +00004095}
4096
Erich Keanee891aa92018-07-13 15:07:47 +00004097static void handleGlobalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004098 if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, AL.getRange(),
4099 AL.getName()) ||
4100 checkAttrMutualExclusion<CUDAHostAttr>(S, D, AL.getRange(),
4101 AL.getName())) {
Justin Lebar3eaaf862016-01-13 01:07:35 +00004102 return;
4103 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004104 const auto *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00004105 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00004106 SourceRange RTRange = FD->getReturnTypeSourceRange();
4107 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004108 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00004109 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
4110 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00004111 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004112 }
Justin Lebarc66a1062016-01-20 00:26:57 +00004113 if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) {
4114 if (Method->isInstance()) {
4115 S.Diag(Method->getLocStart(), diag::err_kern_is_nonstatic_method)
4116 << Method;
4117 return;
4118 }
4119 S.Diag(Method->getLocStart(), diag::warn_kern_is_method) << Method;
4120 }
4121 // Only warn for "inline" when compiling for host, to cut down on noise.
4122 if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice)
4123 S.Diag(FD->getLocStart(), diag::warn_kern_is_inline) << FD;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004124
Aaron Ballman3aff6332013-12-02 19:30:36 +00004125 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004126 CUDAGlobalAttr(AL.getRange(), S.Context,
4127 AL.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004128}
4129
Erich Keanee891aa92018-07-13 15:07:47 +00004130static void handleGNUInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004131 const auto *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00004132 if (!Fn->isInlineSpecified()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004133 S.Diag(AL.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00004134 return;
4135 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004136
Michael Han99315932013-01-24 16:46:58 +00004137 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004138 GNUInlineAttr(AL.getRange(), S.Context,
4139 AL.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00004140}
4141
Erich Keanee891aa92018-07-13 15:07:47 +00004142static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004143 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004144
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004145 // Diagnostic is emitted elsewhere: here we store the (valid) AL
John McCall3882ace2011-01-05 12:14:39 +00004146 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
4147 CallingConv CC;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004148 if (S.CheckCallingConvAttr(AL, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00004149 return;
4150
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004151 if (!isa<ObjCMethodDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004152 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
4153 << AL.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00004154 return;
4155 }
4156
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004157 switch (AL.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +00004158 case ParsedAttr::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00004159 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004160 FastCallAttr(AL.getRange(), S.Context,
4161 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004162 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004163 case ParsedAttr::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00004164 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004165 StdCallAttr(AL.getRange(), S.Context,
4166 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004167 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004168 case ParsedAttr::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00004169 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004170 ThisCallAttr(AL.getRange(), S.Context,
4171 AL.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00004172 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004173 case ParsedAttr::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00004174 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004175 CDeclAttr(AL.getRange(), S.Context,
4176 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004177 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004178 case ParsedAttr::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00004179 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004180 PascalAttr(AL.getRange(), S.Context,
4181 AL.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00004182 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004183 case ParsedAttr::AT_SwiftCall:
John McCall477f2bb2016-03-03 06:39:32 +00004184 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004185 SwiftCallAttr(AL.getRange(), S.Context,
4186 AL.getAttributeSpellingListIndex()));
John McCall477f2bb2016-03-03 06:39:32 +00004187 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004188 case ParsedAttr::AT_VectorCall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00004189 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004190 VectorCallAttr(AL.getRange(), S.Context,
4191 AL.getAttributeSpellingListIndex()));
Reid Klecknerd7857f02014-10-24 17:42:17 +00004192 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004193 case ParsedAttr::AT_MSABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004194 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004195 MSABIAttr(AL.getRange(), S.Context,
4196 AL.getAttributeSpellingListIndex()));
Charles Davisb5a214e2013-08-30 04:39:01 +00004197 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004198 case ParsedAttr::AT_SysVABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004199 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004200 SysVABIAttr(AL.getRange(), S.Context,
4201 AL.getAttributeSpellingListIndex()));
Charles Davisb5a214e2013-08-30 04:39:01 +00004202 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004203 case ParsedAttr::AT_RegCall:
Erich Keane757d3172016-11-02 18:29:35 +00004204 D->addAttr(::new (S.Context) RegCallAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004205 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Erich Keane757d3172016-11-02 18:29:35 +00004206 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004207 case ParsedAttr::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004208 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004209 switch (CC) {
4210 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004211 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004212 break;
4213 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004214 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004215 break;
4216 default:
4217 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004218 }
4219
Michael Han99315932013-01-24 16:46:58 +00004220 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004221 PcsAttr(AL.getRange(), S.Context, PCS,
4222 AL.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00004223 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004224 }
Erich Keanee891aa92018-07-13 15:07:47 +00004225 case ParsedAttr::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00004226 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004227 IntelOclBiccAttr(AL.getRange(), S.Context,
4228 AL.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00004229 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004230 case ParsedAttr::AT_PreserveMost:
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004231 D->addAttr(::new (S.Context) PreserveMostAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004232 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004233 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004234 case ParsedAttr::AT_PreserveAll:
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004235 D->addAttr(::new (S.Context) PreserveAllAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004236 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004237 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004238 default:
4239 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00004240 }
4241}
4242
Erich Keanee891aa92018-07-13 15:07:47 +00004243static void handleSuppressAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004244 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Matthias Gehre01a63382017-03-27 19:45:24 +00004245 return;
4246
4247 std::vector<StringRef> DiagnosticIdentifiers;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004248 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Matthias Gehre01a63382017-03-27 19:45:24 +00004249 StringRef RuleName;
4250
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004251 if (!S.checkStringLiteralArgumentAttr(AL, I, RuleName, nullptr))
Matthias Gehre01a63382017-03-27 19:45:24 +00004252 return;
4253
4254 // FIXME: Warn if the rule name is unknown. This is tricky because only
4255 // clang-tidy knows about available rules.
4256 DiagnosticIdentifiers.push_back(RuleName);
4257 }
4258 D->addAttr(::new (S.Context) SuppressAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004259 AL.getRange(), S.Context, DiagnosticIdentifiers.data(),
4260 DiagnosticIdentifiers.size(), AL.getAttributeSpellingListIndex()));
Matthias Gehre01a63382017-03-27 19:45:24 +00004261}
4262
Erich Keanee891aa92018-07-13 15:07:47 +00004263bool Sema::CheckCallingConvAttr(const ParsedAttr &Attrs, CallingConv &CC,
Aaron Ballman02df2e02012-12-09 17:45:41 +00004264 const FunctionDecl *FD) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00004265 if (Attrs.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004266 return true;
4267
Erich Keaneb11ebc52017-09-27 03:20:13 +00004268 if (Attrs.hasProcessingCache()) {
4269 CC = (CallingConv) Attrs.getProcessingCache();
John McCall3b5a8f52016-03-03 00:10:03 +00004270 return false;
4271 }
4272
Erich Keanee891aa92018-07-13 15:07:47 +00004273 unsigned ReqArgs = Attrs.getKind() == ParsedAttr::AT_Pcs ? 1 : 0;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004274 if (!checkAttributeNumArgs(*this, Attrs, ReqArgs)) {
4275 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004276 return true;
4277 }
4278
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004279 // TODO: diagnose uses of these conventions on the wrong target.
Erich Keaneb11ebc52017-09-27 03:20:13 +00004280 switch (Attrs.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +00004281 case ParsedAttr::AT_CDecl:
4282 CC = CC_C;
4283 break;
4284 case ParsedAttr::AT_FastCall:
4285 CC = CC_X86FastCall;
4286 break;
4287 case ParsedAttr::AT_StdCall:
4288 CC = CC_X86StdCall;
4289 break;
4290 case ParsedAttr::AT_ThisCall:
4291 CC = CC_X86ThisCall;
4292 break;
4293 case ParsedAttr::AT_Pascal:
4294 CC = CC_X86Pascal;
4295 break;
4296 case ParsedAttr::AT_SwiftCall:
4297 CC = CC_Swift;
4298 break;
4299 case ParsedAttr::AT_VectorCall:
4300 CC = CC_X86VectorCall;
4301 break;
4302 case ParsedAttr::AT_RegCall:
4303 CC = CC_X86RegCall;
4304 break;
4305 case ParsedAttr::AT_MSABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004306 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
Martin Storsjo022e7822017-07-17 20:49:45 +00004307 CC_Win64;
Charles Davisb5a214e2013-08-30 04:39:01 +00004308 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004309 case ParsedAttr::AT_SysVABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004310 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
4311 CC_C;
4312 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004313 case ParsedAttr::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00004314 StringRef StrRef;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004315 if (!checkStringLiteralArgumentAttr(Attrs, 0, StrRef)) {
4316 Attrs.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004317 return true;
4318 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004319 if (StrRef == "aapcs") {
4320 CC = CC_AAPCS;
4321 break;
4322 } else if (StrRef == "aapcs-vfp") {
4323 CC = CC_AAPCS_VFP;
4324 break;
4325 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004326
Erich Keaneb11ebc52017-09-27 03:20:13 +00004327 Attrs.setInvalid();
4328 Diag(Attrs.getLoc(), diag::err_invalid_pcs);
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004329 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004330 }
Erich Keanee891aa92018-07-13 15:07:47 +00004331 case ParsedAttr::AT_IntelOclBicc:
4332 CC = CC_IntelOclBicc;
4333 break;
4334 case ParsedAttr::AT_PreserveMost:
4335 CC = CC_PreserveMost;
4336 break;
4337 case ParsedAttr::AT_PreserveAll:
4338 CC = CC_PreserveAll;
4339 break;
David Blaikie8a40f702012-01-17 06:56:22 +00004340 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00004341 }
4342
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004343 const TargetInfo &TI = Context.getTargetInfo();
4344 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004345 if (A != TargetInfo::CCCR_OK) {
4346 if (A == TargetInfo::CCCR_Warning)
Erich Keaneb11ebc52017-09-27 03:20:13 +00004347 Diag(Attrs.getLoc(), diag::warn_cconv_ignored) << Attrs.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00004348
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004349 // This convention is not valid for the target. Use the default function or
4350 // method calling convention.
Alexey Bataeva7547182016-05-18 09:06:38 +00004351 bool IsCXXMethod = false, IsVariadic = false;
4352 if (FD) {
4353 IsCXXMethod = FD->isCXXInstanceMember();
4354 IsVariadic = FD->isVariadic();
4355 }
4356 CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004357 }
4358
Erich Keaneb11ebc52017-09-27 03:20:13 +00004359 Attrs.setProcessingCache((unsigned) CC);
John McCall3882ace2011-01-05 12:14:39 +00004360 return false;
4361}
4362
John McCall477f2bb2016-03-03 06:39:32 +00004363/// Pointer-like types in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004364static bool isValidSwiftContextType(QualType Ty) {
4365 if (!Ty->hasPointerRepresentation())
4366 return Ty->isDependentType();
4367 return Ty->getPointeeType().getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004368}
4369
4370/// Pointers and references in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004371static bool isValidSwiftIndirectResultType(QualType Ty) {
4372 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4373 Ty = PtrType->getPointeeType();
4374 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4375 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004376 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004377 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004378 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004379 return Ty.getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004380}
4381
4382/// Pointers and references to pointers in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004383static bool isValidSwiftErrorResultType(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 if (!Ty.getQualifiers().empty())
John McCall477f2bb2016-03-03 06:39:32 +00004392 return false;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004393 return isValidSwiftContextType(Ty);
John McCall477f2bb2016-03-03 06:39:32 +00004394}
4395
Erich Keanee891aa92018-07-13 15:07:47 +00004396static void handleParameterABIAttr(Sema &S, Decl *D, const ParsedAttr &Attrs,
Erich Keaneb11ebc52017-09-27 03:20:13 +00004397 ParameterABI Abi) {
4398 S.AddParameterABIAttr(Attrs.getRange(), D, Abi,
4399 Attrs.getAttributeSpellingListIndex());
John McCall477f2bb2016-03-03 06:39:32 +00004400}
4401
4402void Sema::AddParameterABIAttr(SourceRange range, Decl *D, ParameterABI abi,
4403 unsigned spellingIndex) {
4404
4405 QualType type = cast<ParmVarDecl>(D)->getType();
4406
4407 if (auto existingAttr = D->getAttr<ParameterABIAttr>()) {
4408 if (existingAttr->getABI() != abi) {
4409 Diag(range.getBegin(), diag::err_attributes_are_not_compatible)
4410 << getParameterABISpelling(abi) << existingAttr;
4411 Diag(existingAttr->getLocation(), diag::note_conflicting_attribute);
4412 return;
4413 }
4414 }
4415
4416 switch (abi) {
4417 case ParameterABI::Ordinary:
4418 llvm_unreachable("explicit attribute for ordinary parameter ABI?");
4419
4420 case ParameterABI::SwiftContext:
4421 if (!isValidSwiftContextType(type)) {
4422 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4423 << getParameterABISpelling(abi)
4424 << /*pointer to pointer */ 0 << type;
4425 }
4426 D->addAttr(::new (Context)
4427 SwiftContextAttr(range, Context, spellingIndex));
4428 return;
4429
4430 case ParameterABI::SwiftErrorResult:
4431 if (!isValidSwiftErrorResultType(type)) {
4432 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4433 << getParameterABISpelling(abi)
4434 << /*pointer to pointer */ 1 << type;
4435 }
4436 D->addAttr(::new (Context)
4437 SwiftErrorResultAttr(range, Context, spellingIndex));
4438 return;
4439
4440 case ParameterABI::SwiftIndirectResult:
4441 if (!isValidSwiftIndirectResultType(type)) {
4442 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4443 << getParameterABISpelling(abi)
4444 << /*pointer*/ 0 << type;
4445 }
4446 D->addAttr(::new (Context)
4447 SwiftIndirectResultAttr(range, Context, spellingIndex));
4448 return;
4449 }
4450 llvm_unreachable("bad parameter ABI attribute");
4451}
4452
John McCall3882ace2011-01-05 12:14:39 +00004453/// Checks a regparm attribute, returning true if it is ill-formed and
4454/// otherwise setting numParams to the appropriate value.
Erich Keanee891aa92018-07-13 15:07:47 +00004455bool Sema::CheckRegparmAttr(const ParsedAttr &AL, unsigned &numParams) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004456 if (AL.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004457 return true;
4458
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004459 if (!checkAttributeNumArgs(*this, AL, 1)) {
4460 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004461 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004462 }
Eli Friedman7044b762009-03-27 21:06:47 +00004463
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004464 uint32_t NP;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004465 Expr *NumParamsExpr = AL.getArgAsExpr(0);
4466 if (!checkUInt32Argument(*this, AL, NumParamsExpr, NP)) {
4467 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004468 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004469 }
4470
Douglas Gregore8bbc122011-09-02 00:18:52 +00004471 if (Context.getTargetInfo().getRegParmMax() == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004472 Diag(AL.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00004473 << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004474 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004475 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004476 }
4477
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004478 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00004479 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004480 Diag(AL.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00004481 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004482 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004483 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004484 }
4485
John McCall3882ace2011-01-05 12:14:39 +00004486 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004487}
4488
Artem Belevichbcec9da2016-06-06 22:54:57 +00004489// Checks whether an argument of launch_bounds attribute is
4490// acceptable, performs implicit conversion to Rvalue, and returns
4491// non-nullptr Expr result on success. Otherwise, it returns nullptr
4492// and may output an error.
4493static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004494 const CUDALaunchBoundsAttr &AL,
Artem Belevichbcec9da2016-06-06 22:54:57 +00004495 const unsigned Idx) {
Artem Belevich7093e402015-04-21 22:55:54 +00004496 if (S.DiagnoseUnexpandedParameterPack(E))
Artem Belevichbcec9da2016-06-06 22:54:57 +00004497 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004498
4499 // Accept template arguments for now as they depend on something else.
4500 // We'll get to check them when they eventually get instantiated.
4501 if (E->isValueDependent())
Artem Belevichbcec9da2016-06-06 22:54:57 +00004502 return E;
Artem Belevich7093e402015-04-21 22:55:54 +00004503
4504 llvm::APSInt I(64);
4505 if (!E->isIntegerConstantExpr(I, S.Context)) {
4506 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004507 << &AL << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
Artem Belevichbcec9da2016-06-06 22:54:57 +00004508 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004509 }
4510 // Make sure we can fit it in 32 bits.
4511 if (!I.isIntN(32)) {
4512 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
4513 << 32 << /* Unsigned */ 1;
Artem Belevichbcec9da2016-06-06 22:54:57 +00004514 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004515 }
4516 if (I < 0)
4517 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004518 << &AL << Idx << E->getSourceRange();
Artem Belevich7093e402015-04-21 22:55:54 +00004519
Artem Belevichbcec9da2016-06-06 22:54:57 +00004520 // We may need to perform implicit conversion of the argument.
4521 InitializedEntity Entity = InitializedEntity::InitializeParameter(
4522 S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false);
4523 ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E);
4524 assert(!ValArg.isInvalid() &&
4525 "Unexpected PerformCopyInitialization() failure.");
4526
4527 return ValArg.getAs<Expr>();
Artem Belevich7093e402015-04-21 22:55:54 +00004528}
4529
4530void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads,
4531 Expr *MinBlocks, unsigned SpellingListIndex) {
4532 CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks,
4533 SpellingListIndex);
Artem Belevichbcec9da2016-06-06 22:54:57 +00004534 MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0);
4535 if (MaxThreads == nullptr)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004536 return;
4537
Artem Belevichbcec9da2016-06-06 22:54:57 +00004538 if (MinBlocks) {
4539 MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1);
4540 if (MinBlocks == nullptr)
4541 return;
4542 }
Artem Belevich7093e402015-04-21 22:55:54 +00004543
4544 D->addAttr(::new (Context) CUDALaunchBoundsAttr(
4545 AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex));
4546}
4547
Erich Keanee891aa92018-07-13 15:07:47 +00004548static void handleLaunchBoundsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004549 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
4550 !checkAttributeAtMostNumArgs(S, AL, 2))
Artem Belevich7093e402015-04-21 22:55:54 +00004551 return;
4552
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004553 S.AddLaunchBoundsAttr(AL.getRange(), D, AL.getArgAsExpr(0),
4554 AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr,
4555 AL.getAttributeSpellingListIndex());
Peter Collingbourne827301e2010-12-12 23:03:07 +00004556}
4557
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004558static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004559 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004560 if (!AL.isArgIdent(0)) {
4561 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
4562 << AL.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004563 return;
4564 }
Joel E. Denny81508102018-03-13 14:51:22 +00004565
4566 ParamIdx ArgumentIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004567 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 2, AL.getArgAsExpr(1),
Alp Toker601b22c2014-01-21 23:35:24 +00004568 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004569 return;
4570
Joel E. Denny81508102018-03-13 14:51:22 +00004571 ParamIdx TypeTagIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004572 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 3, AL.getArgAsExpr(2),
Alp Toker601b22c2014-01-21 23:35:24 +00004573 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004574 return;
4575
Aaron Ballmana26d8ee2018-02-25 14:01:04 +00004576 bool IsPointer = AL.getName()->getName() == "pointer_with_type_tag";
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004577 if (IsPointer) {
4578 // Ensure that buffer has a pointer type.
Joel E. Denny81508102018-03-13 14:51:22 +00004579 unsigned ArgumentIdxAST = ArgumentIdx.getASTIndex();
4580 if (ArgumentIdxAST >= getFunctionOrMethodNumParams(D) ||
4581 !getFunctionOrMethodParamType(D, ArgumentIdxAST)->isPointerType())
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004582 S.Diag(AL.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballmanbd0f6562018-02-25 20:28:10 +00004583 << AL.getName() << 0;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004584 }
4585
Aaron Ballmana26d8ee2018-02-25 14:01:04 +00004586 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(
4587 AL.getRange(), S.Context, AL.getArgAsIdent(0)->Ident, ArgumentIdx,
4588 TypeTagIdx, IsPointer, AL.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004589}
4590
4591static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004592 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004593 if (!AL.isArgIdent(0)) {
4594 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
4595 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004596 return;
4597 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004598
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004599 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00004600 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004601
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004602 if (!isa<VarDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004603 S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type)
4604 << AL.getName() << ExpectedVariable;
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004605 return;
4606 }
4607
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004608 IdentifierInfo *PointerKind = AL.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00004609 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004610 S.GetTypeFromParser(AL.getMatchingCType(), &MatchingCTypeLoc);
Richard Smithb87c4652013-10-31 21:23:20 +00004611 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004612
Michael Han99315932013-01-24 16:46:58 +00004613 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004614 TypeTagForDatatypeAttr(AL.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00004615 MatchingCTypeLoc,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004616 AL.getLayoutCompatible(),
4617 AL.getMustBeNull(),
4618 AL.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004619}
4620
Erich Keanee891aa92018-07-13 15:07:47 +00004621static void handleXRayLogArgsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Joel E. Denny81508102018-03-13 14:51:22 +00004622 ParamIdx ArgCount;
Dean Michael Berris7456a282017-06-16 03:22:09 +00004623
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004624 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, AL.getArgAsExpr(0),
Dean Michael Berris7456a282017-06-16 03:22:09 +00004625 ArgCount,
Joel E. Denny81508102018-03-13 14:51:22 +00004626 true /* CanIndexImplicitThis */))
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004627 return;
4628
Joel E. Denny81508102018-03-13 14:51:22 +00004629 // ArgCount isn't a parameter index [0;n), it's a count [1;n]
4630 D->addAttr(::new (S.Context) XRayLogArgsAttr(
4631 AL.getRange(), S.Context, ArgCount.getSourceIndex(),
4632 AL.getAttributeSpellingListIndex()));
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004633}
4634
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004635//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004636// Checker-specific attribute handlers.
4637//===----------------------------------------------------------------------===//
4638
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004639static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType QT) {
4640 return QT->isDependentType() || QT->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004641}
4642
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004643static bool isValidSubjectOfNSAttribute(Sema &S, QualType QT) {
4644 return QT->isDependentType() || QT->isObjCObjectPointerType() ||
4645 S.Context.isObjCNSObjectType(QT);
John McCalled433932011-01-25 03:31:58 +00004646}
Eugene Zelenko1ced5092016-02-12 22:53:10 +00004647
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004648static bool isValidSubjectOfCFAttribute(Sema &S, QualType QT) {
4649 return QT->isDependentType() || QT->isPointerType() ||
4650 isValidSubjectOfNSAttribute(S, QT);
John McCalled433932011-01-25 03:31:58 +00004651}
4652
Erich Keanee891aa92018-07-13 15:07:47 +00004653static void handleNSConsumedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004654 S.AddNSConsumedAttr(AL.getRange(), D, AL.getAttributeSpellingListIndex(),
Erich Keanee891aa92018-07-13 15:07:47 +00004655 AL.getKind() == ParsedAttr::AT_NSConsumed,
John McCall3b5a8f52016-03-03 00:10:03 +00004656 /*template instantiation*/ false);
4657}
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004658
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004659void Sema::AddNSConsumedAttr(SourceRange AttrRange, Decl *D,
4660 unsigned SpellingIndex, bool IsNSConsumed,
4661 bool IsTemplateInstantiation) {
4662 const auto *Param = cast<ParmVarDecl>(D);
4663 bool TypeOK;
John McCall3b5a8f52016-03-03 00:10:03 +00004664
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004665 if (IsNSConsumed)
4666 TypeOK = isValidSubjectOfNSAttribute(*this, Param->getType());
4667 else
4668 TypeOK = isValidSubjectOfCFAttribute(*this, Param->getType());
John McCalled433932011-01-25 03:31:58 +00004669
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004670 if (!TypeOK) {
John McCall3b5a8f52016-03-03 00:10:03 +00004671 // These attributes are normally just advisory, but in ARC, ns_consumed
4672 // is significant. Allow non-dependent code to contain inappropriate
4673 // attributes even in ARC, but require template instantiations to be
4674 // set up correctly.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004675 Diag(D->getLocStart(), (IsTemplateInstantiation && IsNSConsumed &&
4676 getLangOpts().ObjCAutoRefCount
4677 ? diag::err_ns_attribute_wrong_parameter_type
4678 : diag::warn_ns_attribute_wrong_parameter_type))
4679 << AttrRange << (IsNSConsumed ? "ns_consumed" : "cf_consumed")
4680 << (IsNSConsumed ? /*objc pointers*/ 0 : /*cf pointers*/ 1);
John McCalled433932011-01-25 03:31:58 +00004681 return;
4682 }
4683
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004684 if (IsNSConsumed)
4685 D->addAttr(::new (Context)
4686 NSConsumedAttr(AttrRange, Context, SpellingIndex));
John McCalled433932011-01-25 03:31:58 +00004687 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004688 D->addAttr(::new (Context)
4689 CFConsumedAttr(AttrRange, Context, SpellingIndex));
John McCalled433932011-01-25 03:31:58 +00004690}
4691
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004692bool Sema::checkNSReturnsRetainedReturnType(SourceLocation Loc, QualType QT) {
4693 if (isValidSubjectOfNSReturnsRetainedAttribute(QT))
John McCall12251882017-07-15 11:06:46 +00004694 return false;
4695
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004696 Diag(Loc, diag::warn_ns_attribute_wrong_return_type)
4697 << "'ns_returns_retained'" << 0 << 0;
John McCall12251882017-07-15 11:06:46 +00004698 return true;
4699}
4700
Chandler Carruthedc2c642011-07-02 00:01:44 +00004701static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004702 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004703 QualType ReturnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004704
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004705 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
4706 ReturnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004707 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Erich Keanee891aa92018-07-13 15:07:47 +00004708 (AL.getKind() == ParsedAttr::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004709 return; // ignore: was handled as a type attribute
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004710 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D))
4711 ReturnType = PD->getType();
4712 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
4713 ReturnType = FD->getReturnType();
4714 else if (const auto *Param = dyn_cast<ParmVarDecl>(D)) {
4715 ReturnType = Param->getType()->getPointeeType();
4716 if (ReturnType.isNull()) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004717 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004718 << AL.getName() << /*pointer-to-CF*/2
4719 << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004720 return;
4721 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004722 } else if (AL.isUsedAsTypeAttr()) {
John McCall12251882017-07-15 11:06:46 +00004723 return;
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004724 } else {
4725 AttributeDeclKind ExpectedDeclKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004726 switch (AL.getKind()) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004727 default: llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00004728 case ParsedAttr::AT_NSReturnsRetained:
4729 case ParsedAttr::AT_NSReturnsAutoreleased:
4730 case ParsedAttr::AT_NSReturnsNotRetained:
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004731 ExpectedDeclKind = ExpectedFunctionOrMethod;
4732 break;
4733
Erich Keanee891aa92018-07-13 15:07:47 +00004734 case ParsedAttr::AT_CFReturnsRetained:
4735 case ParsedAttr::AT_CFReturnsNotRetained:
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004736 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
4737 break;
4738 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004739 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004740 << AL.getRange() << AL.getName() << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004741 return;
4742 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004743
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004744 bool TypeOK;
4745 bool Cf;
4746 switch (AL.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004747 default: llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00004748 case ParsedAttr::AT_NSReturnsRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004749 TypeOK = isValidSubjectOfNSReturnsRetainedAttribute(ReturnType);
4750 Cf = false;
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004751 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004752
4753 case ParsedAttr::AT_NSReturnsAutoreleased:
4754 case ParsedAttr::AT_NSReturnsNotRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004755 TypeOK = isValidSubjectOfNSAttribute(S, ReturnType);
4756 Cf = false;
John McCalled433932011-01-25 03:31:58 +00004757 break;
4758
Erich Keanee891aa92018-07-13 15:07:47 +00004759 case ParsedAttr::AT_CFReturnsRetained:
4760 case ParsedAttr::AT_CFReturnsNotRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004761 TypeOK = isValidSubjectOfCFAttribute(S, ReturnType);
4762 Cf = true;
John McCalled433932011-01-25 03:31:58 +00004763 break;
4764 }
4765
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004766 if (!TypeOK) {
4767 if (AL.isUsedAsTypeAttr())
John McCall12251882017-07-15 11:06:46 +00004768 return;
4769
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004770 if (isa<ParmVarDecl>(D)) {
4771 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004772 << AL.getName() << /*pointer-to-CF*/2
4773 << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004774 } else {
4775 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
4776 enum : unsigned {
4777 Function,
4778 Method,
4779 Property
4780 } SubjectKind = Function;
4781 if (isa<ObjCMethodDecl>(D))
4782 SubjectKind = Method;
4783 else if (isa<ObjCPropertyDecl>(D))
4784 SubjectKind = Property;
4785 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004786 << AL.getName() << SubjectKind << Cf
4787 << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004788 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004789 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004790 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004791
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004792 switch (AL.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004793 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004794 llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00004795 case ParsedAttr::AT_NSReturnsAutoreleased:
Nico Weber462fd1e2015-01-07 23:50:05 +00004796 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004797 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004798 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004799 case ParsedAttr::AT_CFReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004800 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004801 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004802 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004803 case ParsedAttr::AT_NSReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004804 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004805 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004806 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004807 case ParsedAttr::AT_CFReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004808 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004809 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004810 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004811 case ParsedAttr::AT_NSReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004812 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004813 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004814 return;
4815 };
4816}
4817
John McCallcf166702011-07-22 08:53:00 +00004818static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004819 const ParsedAttr &Attrs) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00004820 const int EP_ObjCMethod = 1;
4821 const int EP_ObjCProperty = 2;
Fangrui Song6907ce22018-07-30 19:24:48 +00004822
Erich Keaneb11ebc52017-09-27 03:20:13 +00004823 SourceLocation loc = Attrs.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004824 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004825 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004826 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004827 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004828 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00004829
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004830 if (!resultType->isReferenceType() &&
4831 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004832 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00004833 << SourceRange(loc)
Erich Keaneb11ebc52017-09-27 03:20:13 +00004834 << Attrs.getName()
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004835 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004836 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00004837
4838 // Drop the attribute.
4839 return;
4840 }
4841
Nico Weber462fd1e2015-01-07 23:50:05 +00004842 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(
Erich Keaneb11ebc52017-09-27 03:20:13 +00004843 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004844}
4845
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004846static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004847 const ParsedAttr &Attrs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004848 const auto *Method = cast<ObjCMethodDecl>(D);
4849
4850 const DeclContext *DC = Method->getDeclContext();
4851 if (const auto *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004852 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004853 << Attrs.getName() << 0;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004854 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4855 return;
4856 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004857 if (Method->getMethodFamily() == OMF_dealloc) {
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004858 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004859 << Attrs.getName() << 1;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004860 return;
4861 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004862
4863 D->addAttr(::new (S.Context) ObjCRequiresSuperAttr(
4864 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004865}
4866
Erich Keanee891aa92018-07-13 15:07:47 +00004867static void handleObjCBridgeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004868 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004869
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004870 if (!Parm) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004871 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << AL.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004872 return;
4873 }
John McCall28592582015-02-01 22:34:06 +00004874
4875 // Typedefs only allow objc_bridge(id) and have some additional checking.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004876 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall28592582015-02-01 22:34:06 +00004877 if (!Parm->Ident->isStr("id")) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004878 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_id)
4879 << AL.getName();
John McCall28592582015-02-01 22:34:06 +00004880 return;
4881 }
4882
4883 // Only allow 'cv void *'.
4884 QualType T = TD->getUnderlyingType();
4885 if (!T->isVoidPointerType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004886 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
John McCall28592582015-02-01 22:34:06 +00004887 return;
4888 }
4889 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004890
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004891 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004892 ObjCBridgeAttr(AL.getRange(), S.Context, Parm->Ident,
4893 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004894}
4895
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004896static void handleObjCBridgeMutableAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004897 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004898 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Craig Topperc3ec1492014-05-26 06:22:03 +00004899
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004900 if (!Parm) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004901 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << AL.getName() << 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004902 return;
4903 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004904
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004905 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004906 ObjCBridgeMutableAttr(AL.getRange(), S.Context, Parm->Ident,
4907 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004908}
4909
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004910static void handleObjCBridgeRelatedAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004911 const ParsedAttr &AL) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004912 IdentifierInfo *RelatedClass =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004913 AL.isArgIdent(0) ? AL.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004914 if (!RelatedClass) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004915 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << AL.getName() << 0;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004916 return;
4917 }
4918 IdentifierInfo *ClassMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004919 AL.getArgAsIdent(1) ? AL.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004920 IdentifierInfo *InstanceMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004921 AL.getArgAsIdent(2) ? AL.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004922 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004923 ObjCBridgeRelatedAttr(AL.getRange(), S.Context, RelatedClass,
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004924 ClassMethod, InstanceMethod,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004925 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004926}
4927
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004928static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004929 const ParsedAttr &AL) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004930 ObjCInterfaceDecl *IFace;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004931 if (auto *CatDecl = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004932 IFace = CatDecl->getClassInterface();
4933 else
4934 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00004935
4936 if (!IFace)
4937 return;
4938
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00004939 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00004940 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004941 ObjCDesignatedInitializerAttr(AL.getRange(), S.Context,
4942 AL.getAttributeSpellingListIndex()));
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004943}
4944
Erich Keanee891aa92018-07-13 15:07:47 +00004945static void handleObjCRuntimeName(Sema &S, Decl *D, const ParsedAttr &AL) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004946 StringRef MetaDataName;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004947 if (!S.checkStringLiteralArgumentAttr(AL, 0, MetaDataName))
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004948 return;
4949 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004950 ObjCRuntimeNameAttr(AL.getRange(), S.Context,
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004951 MetaDataName,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004952 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004953}
4954
Nico Webera6916892016-06-10 18:53:04 +00004955// When a user wants to use objc_boxable with a union or struct
4956// but they don't have access to the declaration (legacy/third-party code)
4957// then they can 'enable' this feature with a typedef:
Alex Denisovfde64952015-06-26 05:28:36 +00004958// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
Erich Keanee891aa92018-07-13 15:07:47 +00004959static void handleObjCBoxable(Sema &S, Decl *D, const ParsedAttr &AL) {
Alex Denisovfde64952015-06-26 05:28:36 +00004960 bool notify = false;
4961
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004962 auto *RD = dyn_cast<RecordDecl>(D);
Alex Denisovfde64952015-06-26 05:28:36 +00004963 if (RD && RD->getDefinition()) {
4964 RD = RD->getDefinition();
4965 notify = true;
4966 }
4967
4968 if (RD) {
4969 ObjCBoxableAttr *BoxableAttr = ::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004970 ObjCBoxableAttr(AL.getRange(), S.Context,
4971 AL.getAttributeSpellingListIndex());
Alex Denisovfde64952015-06-26 05:28:36 +00004972 RD->addAttr(BoxableAttr);
4973 if (notify) {
4974 // we need to notify ASTReader/ASTWriter about
4975 // modification of existing declaration
4976 if (ASTMutationListener *L = S.getASTMutationListener())
4977 L->AddedAttributeToRecord(BoxableAttr, RD);
4978 }
4979 }
4980}
4981
Erich Keanee891aa92018-07-13 15:07:47 +00004982static void handleObjCOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004983 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004984
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004985 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004986 << AL.getRange() << AL.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004987}
4988
Chandler Carruthedc2c642011-07-02 00:01:44 +00004989static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004990 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004991 const auto *VD = cast<ValueDecl>(D);
4992 QualType QT = VD->getType();
John McCall31168b02011-06-15 23:02:42 +00004993
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004994 if (!QT->isDependentType() &&
4995 !QT->isObjCLifetimeType()) {
4996 S.Diag(AL.getLoc(), diag::err_objc_precise_lifetime_bad_type)
4997 << QT;
John McCall31168b02011-06-15 23:02:42 +00004998 return;
4999 }
5000
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005001 Qualifiers::ObjCLifetime Lifetime = QT.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +00005002
5003 // If we have no lifetime yet, check the lifetime we're presumably
5004 // going to infer.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005005 if (Lifetime == Qualifiers::OCL_None && !QT->isDependentType())
5006 Lifetime = QT->getObjCARCImplicitLifetime();
John McCall31168b02011-06-15 23:02:42 +00005007
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005008 switch (Lifetime) {
John McCall31168b02011-06-15 23:02:42 +00005009 case Qualifiers::OCL_None:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005010 assert(QT->isDependentType() &&
John McCall31168b02011-06-15 23:02:42 +00005011 "didn't infer lifetime for non-dependent type?");
5012 break;
5013
5014 case Qualifiers::OCL_Weak: // meaningful
5015 case Qualifiers::OCL_Strong: // meaningful
5016 break;
5017
5018 case Qualifiers::OCL_ExplicitNone:
5019 case Qualifiers::OCL_Autoreleasing:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005020 S.Diag(AL.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
5021 << (Lifetime == Qualifiers::OCL_Autoreleasing);
John McCall31168b02011-06-15 23:02:42 +00005022 break;
5023 }
5024
Chandler Carruthff4c4f02011-07-01 23:49:12 +00005025 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005026 ObjCPreciseLifetimeAttr(AL.getRange(), S.Context,
5027 AL.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00005028}
5029
Francois Picheta83957a2010-12-19 06:50:37 +00005030//===----------------------------------------------------------------------===//
5031// Microsoft specific attribute handlers.
5032//===----------------------------------------------------------------------===//
5033
Nico Weber88f5ed92016-09-13 18:55:26 +00005034UuidAttr *Sema::mergeUuidAttr(Decl *D, SourceRange Range,
5035 unsigned AttrSpellingListIndex, StringRef Uuid) {
5036 if (const auto *UA = D->getAttr<UuidAttr>()) {
Nico Weberd58c2602016-09-14 01:16:54 +00005037 if (UA->getGuid().equals_lower(Uuid))
Nico Weber88f5ed92016-09-13 18:55:26 +00005038 return nullptr;
5039 Diag(UA->getLocation(), diag::err_mismatched_uuid);
5040 Diag(Range.getBegin(), diag::note_previous_uuid);
5041 D->dropAttr<UuidAttr>();
5042 }
5043
5044 return ::new (Context) UuidAttr(Range, Context, Uuid, AttrSpellingListIndex);
5045}
5046
Erich Keanee891aa92018-07-13 15:07:47 +00005047static void handleUuidAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005048 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005049 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
5050 << AL.getName() << AttributeLangSupport::C;
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005051 return;
5052 }
5053
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005054 StringRef StrRef;
5055 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005056 if (!S.checkStringLiteralArgumentAttr(AL, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00005057 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005058
David Majnemer89085342013-08-09 08:56:20 +00005059 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
5060 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00005061 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
5062 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00005063
Reid Kleckner140c4a72013-05-17 14:04:52 +00005064 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00005065 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005066 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005067 return;
5068 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00005069
David Majnemer89085342013-08-09 08:56:20 +00005070 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00005071 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00005072 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005073 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00005074 return;
5075 }
David Majnemer89085342013-08-09 08:56:20 +00005076 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005077 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005078 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005079 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00005080 }
Francois Picheta83957a2010-12-19 06:50:37 +00005081
Nico Weber469891e2017-05-05 17:05:56 +00005082 // FIXME: It'd be nice to also emit a fixit removing uuid(...) (and, if it's
5083 // the only thing in the [] list, the [] too), and add an insertion of
5084 // __declspec(uuid(...)). But sadly, neither the SourceLocs of the commas
5085 // separating attributes nor of the [ and the ] are in the AST.
Nico Weber0a234042017-05-05 17:15:08 +00005086 // Cf "SourceLocations of attribute list delimiters - [[ ... , ... ]] etc"
Nico Weber469891e2017-05-05 17:05:56 +00005087 // on cfe-dev.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005088 if (AL.isMicrosoftAttribute()) // Check for [uuid(...)] spelling.
5089 S.Diag(AL.getLoc(), diag::warn_atl_uuid_deprecated);
Nico Weber469891e2017-05-05 17:05:56 +00005090
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005091 UuidAttr *UA = S.mergeUuidAttr(D, AL.getRange(),
5092 AL.getAttributeSpellingListIndex(), StrRef);
Nico Weber88f5ed92016-09-13 18:55:26 +00005093 if (UA)
5094 D->addAttr(UA);
Charles Davis163855f2010-02-16 18:27:26 +00005095}
5096
Erich Keanee891aa92018-07-13 15:07:47 +00005097static void handleMSInheritanceAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005098 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005099 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
5100 << AL.getName() << AttributeLangSupport::C;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005101 return;
5102 }
5103 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005104 D, AL.getRange(), /*BestCase=*/true,
5105 AL.getAttributeSpellingListIndex(),
5106 (MSInheritanceAttr::Spelling)AL.getSemanticSpelling());
David Majnemer929025d2016-01-26 19:30:26 +00005107 if (IA) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005108 D->addAttr(IA);
David Majnemer929025d2016-01-26 19:30:26 +00005109 S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D));
5110 }
David Majnemer2c4e00a2014-01-29 22:07:36 +00005111}
5112
Erich Keanee891aa92018-07-13 15:07:47 +00005113static void handleDeclspecThreadAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005114 const auto *VD = cast<VarDecl>(D);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005115 if (!S.Context.getTargetInfo().isTLSSupported()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005116 S.Diag(AL.getLoc(), diag::err_thread_unsupported);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005117 return;
5118 }
5119 if (VD->getTSCSpec() != TSCS_unspecified) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005120 S.Diag(AL.getLoc(), diag::err_declspec_thread_on_thread_variable);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005121 return;
5122 }
5123 if (VD->hasLocalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005124 S.Diag(AL.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005125 return;
5126 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005127 D->addAttr(::new (S.Context) ThreadAttr(AL.getRange(), S.Context,
5128 AL.getAttributeSpellingListIndex()));
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005129}
5130
Erich Keanee891aa92018-07-13 15:07:47 +00005131static void handleAbiTagAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005132 SmallVector<StringRef, 4> Tags;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005133 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005134 StringRef Tag;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005135 if (!S.checkStringLiteralArgumentAttr(AL, I, Tag))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005136 return;
5137 Tags.push_back(Tag);
5138 }
5139
5140 if (const auto *NS = dyn_cast<NamespaceDecl>(D)) {
5141 if (!NS->isInline()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005142 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 0;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005143 return;
5144 }
5145 if (NS->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005146 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 1;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005147 return;
5148 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005149 if (AL.getNumArgs() == 0)
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005150 Tags.push_back(NS->getName());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005151 } else if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005152 return;
5153
5154 // Store tags sorted and without duplicates.
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +00005155 llvm::sort(Tags.begin(), Tags.end());
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005156 Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end());
5157
5158 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005159 AbiTagAttr(AL.getRange(), S.Context, Tags.data(), Tags.size(),
5160 AL.getAttributeSpellingListIndex()));
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005161}
5162
Erich Keanee891aa92018-07-13 15:07:47 +00005163static void handleARMInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005164 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005165 if (AL.getNumArgs() > 1) {
5166 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
5167 << AL.getName() << 1;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005168 return;
5169 }
5170
5171 StringRef Str;
5172 SourceLocation ArgLoc;
5173
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005174 if (AL.getNumArgs() == 0)
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005175 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005176 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005177 return;
5178
5179 ARMInterruptAttr::InterruptType Kind;
5180 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005181 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
5182 << AL.getName() << Str << ArgLoc;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005183 return;
5184 }
5185
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005186 unsigned Index = AL.getAttributeSpellingListIndex();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005187 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005188 ARMInterruptAttr(AL.getLoc(), S.Context, Kind, Index));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005189}
5190
Erich Keanee891aa92018-07-13 15:07:47 +00005191static void handleMSP430InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005192 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005193 return;
5194
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005195 if (!AL.isArgExpr(0)) {
5196 S.Diag(AL.getLoc(), diag::err_attribute_argument_type) << AL.getName()
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005197 << AANT_ArgumentIntegerConstant;
Fangrui Song6907ce22018-07-30 19:24:48 +00005198 return;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005199 }
5200
5201 // FIXME: Check for decl - it should be void ()(void).
5202
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005203 Expr *NumParamsExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005204 llvm::APSInt NumParams(32);
5205 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005206 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
5207 << AL.getName() << AANT_ArgumentIntegerConstant
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005208 << NumParamsExpr->getSourceRange();
5209 return;
5210 }
5211
5212 unsigned Num = NumParams.getLimitedValue(255);
5213 if ((Num & 1) || Num > 30) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005214 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
5215 << AL.getName() << (int)NumParams.getSExtValue()
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005216 << NumParamsExpr->getSourceRange();
5217 return;
5218 }
5219
Aaron Ballman36a53502014-01-16 13:03:14 +00005220 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005221 MSP430InterruptAttr(AL.getLoc(), S.Context, Num,
5222 AL.getAttributeSpellingListIndex()));
Aaron Ballman36a53502014-01-16 13:03:14 +00005223 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005224}
5225
Erich Keanee891aa92018-07-13 15:07:47 +00005226static void handleMipsInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005227 // Only one optional argument permitted.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005228 if (AL.getNumArgs() > 1) {
5229 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
5230 << AL.getName() << 1;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005231 return;
5232 }
5233
5234 StringRef Str;
5235 SourceLocation ArgLoc;
5236
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005237 if (AL.getNumArgs() == 0)
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005238 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005239 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005240 return;
5241
5242 // Semantic checks for a function with the 'interrupt' attribute for MIPS:
5243 // a) Must be a function.
5244 // b) Must have no parameters.
5245 // c) Must have the 'void' return type.
5246 // d) Cannot have the 'mips16' attribute, as that instruction set
5247 // lacks the 'eret' instruction.
5248 // e) The attribute itself must either have no argument or one of the
5249 // valid interrupt types, see [MipsInterruptDocs].
5250
5251 if (!isFunctionOrMethod(D)) {
5252 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5253 << "'interrupt'" << ExpectedFunctionOrMethod;
5254 return;
5255 }
5256
5257 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
5258 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
5259 << 0;
5260 return;
5261 }
5262
5263 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5264 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
5265 << 1;
5266 return;
5267 }
5268
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005269 if (checkAttrMutualExclusion<Mips16Attr>(S, D, AL.getRange(),
5270 AL.getName()))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005271 return;
5272
5273 MipsInterruptAttr::InterruptType Kind;
5274 if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005275 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
5276 << AL.getName() << "'" + std::string(Str) + "'";
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005277 return;
5278 }
5279
5280 D->addAttr(::new (S.Context) MipsInterruptAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005281 AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex()));
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005282}
5283
Erich Keanee891aa92018-07-13 15:07:47 +00005284static void handleAnyX86InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Alexey Bataevd51e9932016-01-15 04:06:31 +00005285 // Semantic checks for a function with the 'interrupt' attribute.
5286 // a) Must be a function.
5287 // b) Must have the 'void' return type.
5288 // c) Must take 1 or 2 arguments.
5289 // d) The 1st argument must be a pointer.
5290 // e) The 2nd argument (if any) must be an unsigned integer.
5291 if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) ||
5292 CXXMethodDecl::isStaticOverloadedOperator(
5293 cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005294 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
5295 << AL.getName() << ExpectedFunctionWithProtoType;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005296 return;
5297 }
5298 // Interrupt handler must have void return type.
5299 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5300 S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(),
5301 diag::err_anyx86_interrupt_attribute)
5302 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5303 ? 0
5304 : 1)
5305 << 0;
5306 return;
5307 }
5308 // Interrupt handler must have 1 or 2 parameters.
5309 unsigned NumParams = getFunctionOrMethodNumParams(D);
5310 if (NumParams < 1 || NumParams > 2) {
5311 S.Diag(D->getLocStart(), diag::err_anyx86_interrupt_attribute)
5312 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5313 ? 0
5314 : 1)
5315 << 1;
5316 return;
5317 }
5318 // The first argument must be a pointer.
5319 if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) {
5320 S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(),
5321 diag::err_anyx86_interrupt_attribute)
5322 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5323 ? 0
5324 : 1)
5325 << 2;
5326 return;
5327 }
5328 // The second argument, if present, must be an unsigned integer.
5329 unsigned TypeSize =
5330 S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64
5331 ? 64
5332 : 32;
5333 if (NumParams == 2 &&
5334 (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() ||
5335 S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) {
5336 S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(),
5337 diag::err_anyx86_interrupt_attribute)
5338 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5339 ? 0
5340 : 1)
5341 << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false);
5342 return;
5343 }
5344 D->addAttr(::new (S.Context) AnyX86InterruptAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005345 AL.getLoc(), S.Context, AL.getAttributeSpellingListIndex()));
Alexey Bataevd51e9932016-01-15 04:06:31 +00005346 D->addAttr(UsedAttr::CreateImplicit(S.Context));
5347}
5348
Erich Keanee891aa92018-07-13 15:07:47 +00005349static void handleAVRInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005350 if (!isFunctionOrMethod(D)) {
5351 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5352 << "'interrupt'" << ExpectedFunction;
5353 return;
5354 }
5355
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005356 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005357 return;
5358
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005359 handleSimpleAttribute<AVRInterruptAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005360}
5361
Erich Keanee891aa92018-07-13 15:07:47 +00005362static void handleAVRSignalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005363 if (!isFunctionOrMethod(D)) {
5364 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5365 << "'signal'" << ExpectedFunction;
5366 return;
5367 }
5368
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005369 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005370 return;
5371
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005372 handleSimpleAttribute<AVRSignalAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005373}
5374
Ana Pazos1eee1b72018-07-26 17:37:45 +00005375
5376static void handleRISCVInterruptAttr(Sema &S, Decl *D,
5377 const ParsedAttr &AL) {
5378 // Warn about repeated attributes.
5379 if (const auto *A = D->getAttr<RISCVInterruptAttr>()) {
5380 S.Diag(AL.getRange().getBegin(),
5381 diag::warn_riscv_repeated_interrupt_attribute);
5382 S.Diag(A->getLocation(), diag::note_riscv_repeated_interrupt_attribute);
5383 return;
5384 }
5385
5386 // Check the attribute argument. Argument is optional.
5387 if (!checkAttributeAtMostNumArgs(S, AL, 1))
5388 return;
5389
5390 StringRef Str;
5391 SourceLocation ArgLoc;
5392
5393 // 'machine'is the default interrupt mode.
5394 if (AL.getNumArgs() == 0)
5395 Str = "machine";
5396 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5397 return;
5398
5399 // Semantic checks for a function with the 'interrupt' attribute:
5400 // - Must be a function.
5401 // - Must have no parameters.
5402 // - Must have the 'void' return type.
5403 // - The attribute itself must either have no argument or one of the
5404 // valid interrupt types, see [RISCVInterruptDocs].
5405
5406 if (D->getFunctionType() == nullptr) {
5407 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5408 << "'interrupt'" << ExpectedFunction;
5409 return;
5410 }
5411
5412 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
5413 S.Diag(D->getLocation(), diag::warn_riscv_interrupt_attribute) << 0;
5414 return;
5415 }
5416
5417 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5418 S.Diag(D->getLocation(), diag::warn_riscv_interrupt_attribute) << 1;
5419 return;
5420 }
5421
5422 RISCVInterruptAttr::InterruptType Kind;
5423 if (!RISCVInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
5424 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
5425 << AL.getName() << Str << ArgLoc;
5426 return;
5427 }
5428
5429 D->addAttr(::new (S.Context) RISCVInterruptAttr(
5430 AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex()));
5431}
5432
Erich Keanee891aa92018-07-13 15:07:47 +00005433static void handleInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005434 // Dispatch the interrupt attribute based on the current target.
Alexey Bataevd51e9932016-01-15 04:06:31 +00005435 switch (S.Context.getTargetInfo().getTriple().getArch()) {
5436 case llvm::Triple::msp430:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005437 handleMSP430InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005438 break;
5439 case llvm::Triple::mipsel:
5440 case llvm::Triple::mips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005441 handleMipsInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005442 break;
5443 case llvm::Triple::x86:
5444 case llvm::Triple::x86_64:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005445 handleAnyX86InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005446 break;
Dylan McKaye8232d72017-02-08 05:09:26 +00005447 case llvm::Triple::avr:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005448 handleAVRInterruptAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005449 break;
Ana Pazos1eee1b72018-07-26 17:37:45 +00005450 case llvm::Triple::riscv32:
5451 case llvm::Triple::riscv64:
5452 handleRISCVInterruptAttr(S, D, AL);
5453 break;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005454 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005455 handleARMInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005456 break;
5457 }
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005458}
5459
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005460static void handleAMDGPUFlatWorkGroupSizeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005461 const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005462 uint32_t Min = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005463 Expr *MinExpr = AL.getArgAsExpr(0);
5464 if (!checkUInt32Argument(S, AL, MinExpr, Min))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005465 return;
5466
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005467 uint32_t Max = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005468 Expr *MaxExpr = AL.getArgAsExpr(1);
5469 if (!checkUInt32Argument(S, AL, MaxExpr, Max))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005470 return;
5471
5472 if (Min == 0 && Max != 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005473 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid)
5474 << AL.getName() << 0;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005475 return;
5476 }
5477 if (Min > Max) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005478 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid)
5479 << AL.getName() << 1;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005480 return;
5481 }
5482
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005483 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005484 AMDGPUFlatWorkGroupSizeAttr(AL.getLoc(), S.Context, Min, Max,
5485 AL.getAttributeSpellingListIndex()));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005486}
5487
Erich Keanee891aa92018-07-13 15:07:47 +00005488static void handleAMDGPUWavesPerEUAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005489 uint32_t Min = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005490 Expr *MinExpr = AL.getArgAsExpr(0);
5491 if (!checkUInt32Argument(S, AL, MinExpr, Min))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005492 return;
5493
5494 uint32_t Max = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005495 if (AL.getNumArgs() == 2) {
5496 Expr *MaxExpr = AL.getArgAsExpr(1);
5497 if (!checkUInt32Argument(S, AL, MaxExpr, Max))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005498 return;
5499 }
5500
5501 if (Min == 0 && Max != 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005502 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid)
5503 << AL.getName() << 0;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005504 return;
5505 }
5506 if (Max != 0 && Min > Max) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005507 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid)
5508 << AL.getName() << 1;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005509 return;
5510 }
5511
5512 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005513 AMDGPUWavesPerEUAttr(AL.getLoc(), S.Context, Min, Max,
5514 AL.getAttributeSpellingListIndex()));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005515}
5516
Erich Keanee891aa92018-07-13 15:07:47 +00005517static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005518 uint32_t NumSGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005519 Expr *NumSGPRExpr = AL.getArgAsExpr(0);
5520 if (!checkUInt32Argument(S, AL, NumSGPRExpr, NumSGPR))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005521 return;
5522
5523 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005524 AMDGPUNumSGPRAttr(AL.getLoc(), S.Context, NumSGPR,
5525 AL.getAttributeSpellingListIndex()));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005526}
5527
Erich Keanee891aa92018-07-13 15:07:47 +00005528static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005529 uint32_t NumVGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005530 Expr *NumVGPRExpr = AL.getArgAsExpr(0);
5531 if (!checkUInt32Argument(S, AL, NumVGPRExpr, NumVGPR))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005532 return;
5533
5534 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005535 AMDGPUNumVGPRAttr(AL.getLoc(), S.Context, NumVGPR,
5536 AL.getAttributeSpellingListIndex()));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005537}
5538
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005539static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005540 const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005541 // If we try to apply it to a function pointer, don't warn, but don't
5542 // do anything, either. It doesn't matter anyway, because there's nothing
5543 // special about calling a force_align_arg_pointer function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005544 const auto *VD = dyn_cast<ValueDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005545 if (VD && VD->getType()->isFunctionPointerType())
5546 return;
5547 // Also don't warn on function pointer typedefs.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005548 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005549 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
5550 TD->getUnderlyingType()->isFunctionType()))
5551 return;
5552 // Attribute can only be applied to function types.
5553 if (!isa<FunctionDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005554 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
5555 << AL.getName() << ExpectedFunction;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005556 return;
5557 }
5558
Aaron Ballman36a53502014-01-16 13:03:14 +00005559 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005560 X86ForceAlignArgPointerAttr(AL.getRange(), S.Context,
5561 AL.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005562}
5563
Erich Keanee891aa92018-07-13 15:07:47 +00005564static void handleLayoutVersion(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemercd3ebfe2016-05-23 17:16:12 +00005565 uint32_t Version;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005566 Expr *VersionExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
5567 if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), Version))
David Majnemercd3ebfe2016-05-23 17:16:12 +00005568 return;
5569
5570 // TODO: Investigate what happens with the next major version of MSVC.
5571 if (Version != LangOptions::MSVC2015) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005572 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
5573 << AL.getName() << Version << VersionExpr->getSourceRange();
David Majnemercd3ebfe2016-05-23 17:16:12 +00005574 return;
5575 }
5576
5577 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005578 LayoutVersionAttr(AL.getRange(), S.Context, Version,
5579 AL.getAttributeSpellingListIndex()));
David Majnemercd3ebfe2016-05-23 17:16:12 +00005580}
5581
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005582DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
5583 unsigned AttrSpellingListIndex) {
5584 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00005585 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00005586 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005587 }
5588
5589 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00005590 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005591
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00005592 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005593}
5594
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005595DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
5596 unsigned AttrSpellingListIndex) {
5597 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00005598 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005599 D->dropAttr<DLLImportAttr>();
5600 }
5601
5602 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00005603 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005604
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00005605 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005606}
5607
Erich Keanee891aa92018-07-13 15:07:47 +00005608static void handleDLLAttr(Sema &S, Decl *D, const ParsedAttr &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00005609 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
5610 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
5611 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored)
5612 << A.getName();
5613 return;
5614 }
5615
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005616 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Erich Keanee891aa92018-07-13 15:07:47 +00005617 if (FD->isInlined() && A.getKind() == ParsedAttr::AT_DLLImport &&
Hans Wennborg606bd6d2014-11-03 14:24:45 +00005618 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
5619 // MinGW doesn't allow dllimport on inline functions.
5620 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
5621 << A.getName();
5622 return;
5623 }
5624 }
5625
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005626 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
Hans Wennborg5869ec42015-09-15 21:05:30 +00005627 if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
5628 MD->getParent()->isLambda()) {
5629 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A.getName();
5630 return;
5631 }
5632 }
5633
Hans Wennborge82f19c2014-06-24 23:57:05 +00005634 unsigned Index = A.getAttributeSpellingListIndex();
Erich Keanee891aa92018-07-13 15:07:47 +00005635 Attr *NewAttr = A.getKind() == ParsedAttr::AT_DLLExport
Hans Wennborge82f19c2014-06-24 23:57:05 +00005636 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
5637 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005638 if (NewAttr)
5639 D->addAttr(NewAttr);
5640}
5641
David Majnemer2c4e00a2014-01-29 22:07:36 +00005642MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00005643Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00005644 unsigned AttrSpellingListIndex,
5645 MSInheritanceAttr::Spelling SemanticSpelling) {
5646 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
5647 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00005648 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005649 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
5650 << 1 /*previous declaration*/;
5651 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
5652 D->dropAttr<MSInheritanceAttr>();
5653 }
5654
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005655 auto *RD = cast<CXXRecordDecl>(D);
David Majnemer2c4e00a2014-01-29 22:07:36 +00005656 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00005657 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
5658 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00005659 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005660 }
5661 } else {
5662 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
5663 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
5664 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00005665 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005666 }
5667 if (RD->getDescribedClassTemplate()) {
5668 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
5669 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00005670 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005671 }
5672 }
5673
5674 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00005675 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00005676}
5677
Erich Keanee891aa92018-07-13 15:07:47 +00005678static void handleCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005679 // The capability attributes take a single string parameter for the name of
5680 // the capability they represent. The lockable attribute does not take any
5681 // parameters. However, semantically, both attributes represent the same
5682 // concept, and so they use the same semantic attribute. Eventually, the
5683 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00005684 //
Alp Toker958027b2014-07-14 19:42:55 +00005685 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00005686 // literal will be considered a "mutex."
5687 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005688 SourceLocation LiteralLoc;
Erich Keanee891aa92018-07-13 15:07:47 +00005689 if (AL.getKind() == ParsedAttr::AT_Capability &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005690 !S.checkStringLiteralArgumentAttr(AL, 0, N, &LiteralLoc))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005691 return;
5692
Aaron Ballman6c810072014-03-05 21:47:13 +00005693 // Currently, there are only two names allowed for a capability: role and
5694 // mutex (case insensitive). Diagnose other capability names.
5695 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
5696 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
5697
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005698 D->addAttr(::new (S.Context) CapabilityAttr(AL.getRange(), S.Context, N,
5699 AL.getAttributeSpellingListIndex()));
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005700}
5701
Erich Keanee891aa92018-07-13 15:07:47 +00005702static void handleAssertCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Josh Gaoec1369e2017-08-08 19:44:34 +00005703 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005704 if (!checkLockFunAttrCommon(S, D, AL, Args))
Josh Gaoec1369e2017-08-08 19:44:34 +00005705 return;
5706
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005707 D->addAttr(::new (S.Context) AssertCapabilityAttr(AL.getRange(), S.Context,
Josh Gaoec1369e2017-08-08 19:44:34 +00005708 Args.data(), Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005709 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005710}
5711
5712static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005713 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005714 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005715 if (!checkLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005716 return;
5717
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005718 D->addAttr(::new (S.Context) AcquireCapabilityAttr(AL.getRange(),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005719 S.Context,
5720 Args.data(), Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005721 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005722}
5723
5724static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005725 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005726 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005727 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005728 return;
5729
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005730 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(AL.getRange(),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005731 S.Context,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005732 AL.getArgAsExpr(0),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005733 Args.data(),
5734 Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005735 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005736}
5737
5738static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005739 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005740 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00005741 SmallVector<Expr *, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005742 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005743
Aaron Ballman18d85ae2014-03-20 16:02:49 +00005744 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005745 AL.getRange(), S.Context, Args.data(), Args.size(),
5746 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005747}
5748
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005749static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005750 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005751 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005752 return;
5753
5754 // check that all arguments are lockable objects
5755 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005756 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005757 if (Args.empty())
5758 return;
5759
5760 RequiresCapabilityAttr *RCA = ::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005761 RequiresCapabilityAttr(AL.getRange(), S.Context, Args.data(),
5762 Args.size(), AL.getAttributeSpellingListIndex());
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005763
5764 D->addAttr(RCA);
5765}
5766
Erich Keanee891aa92018-07-13 15:07:47 +00005767static void handleDeprecatedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005768 if (const auto *NSD = dyn_cast<NamespaceDecl>(D)) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005769 if (NSD->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005770 S.Diag(AL.getLoc(), diag::warn_deprecated_anonymous_namespace);
Aaron Ballman43f40102014-11-14 22:34:56 +00005771 // Do not want to attach the attribute to the namespace because that will
5772 // cause confusing diagnostic reports for uses of declarations within the
5773 // namespace.
5774 return;
5775 }
5776 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00005777
Manman Renc7890fe2016-03-16 18:50:49 +00005778 // Handle the cases where the attribute has a text message.
5779 StringRef Str, Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005780 if (AL.isArgExpr(0) && AL.getArgAsExpr(0) &&
5781 !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Manman Renc7890fe2016-03-16 18:50:49 +00005782 return;
5783
5784 // Only support a single optional message for Declspec and CXX11.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005785 if (AL.isDeclspecAttribute() || AL.isCXX11Attribute())
5786 checkAttributeAtMostNumArgs(S, AL, 1);
5787 else if (AL.isArgExpr(1) && AL.getArgAsExpr(1) &&
5788 !S.checkStringLiteralArgumentAttr(AL, 1, Replacement))
Manman Renc7890fe2016-03-16 18:50:49 +00005789 return;
5790
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00005791 if (!S.getLangOpts().CPlusPlus14)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005792 if (AL.isCXX11Attribute() &&
5793 !(AL.hasScope() && AL.getScopeName()->isStr("gnu")))
5794 S.Diag(AL.getLoc(), diag::ext_cxx14_attr) << AL.getName();
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00005795
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005796 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005797 DeprecatedAttr(AL.getRange(), S.Context, Str, Replacement,
5798 AL.getAttributeSpellingListIndex()));
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005799}
5800
5801static bool isGlobalVar(const Decl *D) {
5802 if (const auto *S = dyn_cast<VarDecl>(D))
5803 return S->hasGlobalStorage();
5804 return false;
Aaron Ballman43f40102014-11-14 22:34:56 +00005805}
5806
Erich Keanee891aa92018-07-13 15:07:47 +00005807static void handleNoSanitizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005808 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Peter Collingbourne915df992015-05-15 18:33:32 +00005809 return;
5810
Benjamin Kramer1b582012016-02-13 18:11:49 +00005811 std::vector<StringRef> Sanitizers;
Peter Collingbourne915df992015-05-15 18:33:32 +00005812
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005813 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Peter Collingbourne915df992015-05-15 18:33:32 +00005814 StringRef SanitizerName;
5815 SourceLocation LiteralLoc;
5816
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005817 if (!S.checkStringLiteralArgumentAttr(AL, I, SanitizerName, &LiteralLoc))
Peter Collingbourne915df992015-05-15 18:33:32 +00005818 return;
5819
5820 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0)
5821 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005822 else if (isGlobalVar(D) && SanitizerName != "address")
5823 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005824 << AL.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne915df992015-05-15 18:33:32 +00005825 Sanitizers.push_back(SanitizerName);
5826 }
5827
5828 D->addAttr(::new (S.Context) NoSanitizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005829 AL.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(),
5830 AL.getAttributeSpellingListIndex()));
Peter Collingbourne915df992015-05-15 18:33:32 +00005831}
5832
5833static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005834 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005835 StringRef AttrName = AL.getName()->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00005836 normalizeName(AttrName);
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005837 StringRef SanitizerName = llvm::StringSwitch<StringRef>(AttrName)
5838 .Case("no_address_safety_analysis", "address")
5839 .Case("no_sanitize_address", "address")
5840 .Case("no_sanitize_thread", "thread")
5841 .Case("no_sanitize_memory", "memory");
5842 if (isGlobalVar(D) && SanitizerName != "address")
5843 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005844 << AL.getName() << ExpectedFunction;
Peter Collingbourne915df992015-05-15 18:33:32 +00005845 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005846 NoSanitizeAttr(AL.getRange(), S.Context, &SanitizerName, 1,
5847 AL.getAttributeSpellingListIndex()));
Peter Collingbourne915df992015-05-15 18:33:32 +00005848}
5849
Erich Keanee891aa92018-07-13 15:07:47 +00005850static void handleInternalLinkageAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005851 if (InternalLinkageAttr *Internal =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005852 S.mergeInternalLinkageAttr(D, AL.getRange(), AL.getName(),
5853 AL.getAttributeSpellingListIndex()))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005854 D->addAttr(Internal);
5855}
5856
Erich Keanee891aa92018-07-13 15:07:47 +00005857static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005858 if (S.LangOpts.OpenCLVersion != 200)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005859 S.Diag(AL.getLoc(), diag::err_attribute_requires_opencl_version)
5860 << AL.getName() << "2.0" << 0;
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005861 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005862 S.Diag(AL.getLoc(), diag::warn_opencl_attr_deprecated_ignored)
5863 << AL.getName() << "2.0";
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005864}
5865
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005866/// Handles semantic checking for features that are common to all attributes,
5867/// such as checking whether a parameter was properly specified, or the correct
5868/// number of arguments were passed, etc.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005869static bool handleCommonAttributeFeatures(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005870 const ParsedAttr &AL) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005871 // Several attributes carry different semantics than the parsing requires, so
Alex Lorenz24952fb2017-04-19 15:52:11 +00005872 // those are opted out of the common argument checks.
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005873 //
5874 // We also bail on unknown and ignored attributes because those are handled
5875 // as part of the target-specific handling logic.
Erich Keanee891aa92018-07-13 15:07:47 +00005876 if (AL.getKind() == ParsedAttr::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005877 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005878 // Check whether the attribute requires specific language extensions to be
5879 // enabled.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005880 if (!AL.diagnoseLangOpts(S))
Aaron Ballman3aff6332013-12-02 19:30:36 +00005881 return true;
Alex Lorenz24952fb2017-04-19 15:52:11 +00005882 // Check whether the attribute appertains to the given subject.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005883 if (!AL.diagnoseAppertainsTo(S, D))
Alex Lorenz24952fb2017-04-19 15:52:11 +00005884 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005885 if (AL.hasCustomParsing())
Alex Lorenz24952fb2017-04-19 15:52:11 +00005886 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005887
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005888 if (AL.getMinArgs() == AL.getMaxArgs()) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005889 // If there are no optional arguments, then checking for the argument count
5890 // is trivial.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005891 if (!checkAttributeNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005892 return true;
5893 } else {
5894 // There are optional arguments, so checking is slightly more involved.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005895 if (AL.getMinArgs() &&
5896 !checkAttributeAtLeastNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005897 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005898 else if (!AL.hasVariadicArg() && AL.getMaxArgs() &&
5899 !checkAttributeAtMostNumArgs(S, AL, AL.getMaxArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005900 return true;
5901 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005902
Oren Ben Simhon220671a2018-03-17 13:31:35 +00005903 if (S.CheckAttrTarget(AL))
5904 return true;
5905
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005906 return false;
5907}
5908
Erich Keanee891aa92018-07-13 15:07:47 +00005909static void handleOpenCLAccessAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00005910 if (D->isInvalidDecl())
5911 return;
5912
5913 // Check if there is only one access qualifier.
5914 if (D->hasAttr<OpenCLAccessAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005915 S.Diag(AL.getLoc(), diag::err_opencl_multiple_access_qualifiers)
Xiuli Pan11e13f62016-02-26 03:13:03 +00005916 << D->getSourceRange();
5917 D->setInvalidDecl(true);
5918 return;
5919 }
5920
5921 // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that an
5922 // image object can be read and written.
5923 // OpenCL v2.0 s6.13.6 - A kernel cannot read from and write to the same pipe
5924 // object. Using the read_write (or __read_write) qualifier with the pipe
5925 // qualifier is a compilation error.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005926 if (const auto *PDecl = dyn_cast<ParmVarDecl>(D)) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00005927 const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005928 if (AL.getName()->getName().find("read_write") != StringRef::npos) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00005929 if (S.getLangOpts().OpenCLVersion < 200 || DeclTy->isPipeType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005930 S.Diag(AL.getLoc(), diag::err_opencl_invalid_read_write)
5931 << AL.getName() << PDecl->getType() << DeclTy->isImageType();
Xiuli Pan11e13f62016-02-26 03:13:03 +00005932 D->setInvalidDecl(true);
5933 return;
5934 }
5935 }
5936 }
5937
5938 D->addAttr(::new (S.Context) OpenCLAccessAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005939 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Xiuli Pan11e13f62016-02-26 03:13:03 +00005940}
5941
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005942//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005943// Top Level Sema Entry Points
5944//===----------------------------------------------------------------------===//
5945
Richard Smithf8a75c32013-08-29 00:47:48 +00005946/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
5947/// the attribute applies to decls. If the attribute is a type attribute, just
5948/// silently ignore it if a GNU attribute.
5949static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005950 const ParsedAttr &AL,
Richard Smithf8a75c32013-08-29 00:47:48 +00005951 bool IncludeCXX11Attributes) {
Erich Keanee891aa92018-07-13 15:07:47 +00005952 if (AL.isInvalid() || AL.getKind() == ParsedAttr::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00005953 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00005954
Richard Smithf8a75c32013-08-29 00:47:48 +00005955 // Ignore C++11 attributes on declarator chunks: they appertain to the type
5956 // instead.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005957 if (AL.isCXX11Attribute() && !IncludeCXX11Attributes)
Richard Smithf8a75c32013-08-29 00:47:48 +00005958 return;
5959
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005960 // Unknown attributes are automatically warned on. Target-specific attributes
5961 // which do not apply to the current target architecture are treated as
5962 // though they were unknown attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00005963 if (AL.getKind() == ParsedAttr::UnknownAttribute ||
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005964 !AL.existsInTarget(S.Context.getTargetInfo())) {
5965 S.Diag(AL.getLoc(), AL.isDeclspecAttribute()
Erich Keanec480f302018-07-12 21:09:05 +00005966 ? diag::warn_unhandled_ms_attribute_ignored
5967 : diag::warn_unknown_attribute_ignored)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005968 << AL.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005969 return;
5970 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005971
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005972 if (handleCommonAttributeFeatures(S, D, AL))
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005973 return;
5974
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005975 switch (AL.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005976 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005977 if (!AL.isStmtAttr()) {
Richard Smith4f902c72016-03-08 00:32:55 +00005978 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005979 assert(AL.isTypeAttr() && "Non-type attribute not handled");
Richard Smith4f902c72016-03-08 00:32:55 +00005980 break;
5981 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005982 S.Diag(AL.getLoc(), diag::err_stmt_attribute_invalid_on_decl)
5983 << AL.getName() << D->getLocation();
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005984 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005985 case ParsedAttr::AT_Interrupt:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005986 handleInterruptAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005987 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005988 case ParsedAttr::AT_X86ForceAlignArgPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005989 handleX86ForceAlignArgPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005990 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005991 case ParsedAttr::AT_DLLExport:
5992 case ParsedAttr::AT_DLLImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005993 handleDLLAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005994 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005995 case ParsedAttr::AT_Mips16:
Simon Atanasyan2c87f532017-05-22 12:47:43 +00005996 handleSimpleAttributeWithExclusions<Mips16Attr, MicroMipsAttr,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005997 MipsInterruptAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005998 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005999 case ParsedAttr::AT_NoMips16:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006000 handleSimpleAttribute<NoMips16Attr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006001 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006002 case ParsedAttr::AT_MicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006003 handleSimpleAttributeWithExclusions<MicroMipsAttr, Mips16Attr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006004 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006005 case ParsedAttr::AT_NoMicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006006 handleSimpleAttribute<NoMicroMipsAttr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006007 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006008 case ParsedAttr::AT_MipsLongCall:
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006009 handleSimpleAttributeWithExclusions<MipsLongCallAttr, MipsShortCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006010 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006011 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006012 case ParsedAttr::AT_MipsShortCall:
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006013 handleSimpleAttributeWithExclusions<MipsShortCallAttr, MipsLongCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006014 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006015 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006016 case ParsedAttr::AT_AMDGPUFlatWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006017 handleAMDGPUFlatWorkGroupSizeAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006018 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006019 case ParsedAttr::AT_AMDGPUWavesPerEU:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006020 handleAMDGPUWavesPerEUAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006021 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006022 case ParsedAttr::AT_AMDGPUNumSGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006023 handleAMDGPUNumSGPRAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006024 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006025 case ParsedAttr::AT_AMDGPUNumVGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006026 handleAMDGPUNumVGPRAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006027 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006028 case ParsedAttr::AT_AVRSignal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006029 handleAVRSignalAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00006030 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006031 case ParsedAttr::AT_IBAction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006032 handleSimpleAttribute<IBActionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006033 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006034 case ParsedAttr::AT_IBOutlet:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006035 handleIBOutlet(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006036 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006037 case ParsedAttr::AT_IBOutletCollection:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006038 handleIBOutletCollection(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006039 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006040 case ParsedAttr::AT_IFunc:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006041 handleIFuncAttr(S, D, AL);
Dmitry Polukhin85eda122016-04-11 07:48:59 +00006042 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006043 case ParsedAttr::AT_Alias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006044 handleAliasAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006045 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006046 case ParsedAttr::AT_Aligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006047 handleAlignedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006048 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006049 case ParsedAttr::AT_AlignValue:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006050 handleAlignValueAttr(S, D, AL);
Hal Finkel1b0d24e2014-10-02 21:21:25 +00006051 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006052 case ParsedAttr::AT_AllocSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006053 handleAllocSizeAttr(S, D, AL);
George Burgess IVe3763372016-12-22 02:50:20 +00006054 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006055 case ParsedAttr::AT_AlwaysInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006056 handleAlwaysInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006057 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006058 case ParsedAttr::AT_Artificial:
Aaron Ballman736c09b2018-02-15 16:28:10 +00006059 handleSimpleAttribute<ArtificialAttr>(S, D, AL);
Erich Keane293a0552018-02-14 00:14:07 +00006060 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006061 case ParsedAttr::AT_AnalyzerNoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006062 handleAnalyzerNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006063 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006064 case ParsedAttr::AT_TLSModel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006065 handleTLSModelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006066 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006067 case ParsedAttr::AT_Annotate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006068 handleAnnotateAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006069 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006070 case ParsedAttr::AT_Availability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006071 handleAvailabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006072 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006073 case ParsedAttr::AT_CarriesDependency:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006074 handleDependencyAttr(S, scope, D, AL);
Richard Smithe233fbf2013-01-28 22:42:45 +00006075 break;
Erich Keane3efe0022018-07-20 14:13:28 +00006076 case ParsedAttr::AT_CPUDispatch:
6077 case ParsedAttr::AT_CPUSpecific:
6078 handleCPUSpecificAttr(S, D, AL);
6079 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006080 case ParsedAttr::AT_Common:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006081 handleCommonAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006082 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006083 case ParsedAttr::AT_CUDAConstant:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006084 handleConstantAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006085 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006086 case ParsedAttr::AT_PassObjectSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006087 handlePassObjectSizeAttr(S, D, AL);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00006088 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006089 case ParsedAttr::AT_Constructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006090 handleConstructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006091 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006092 case ParsedAttr::AT_CXX11NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006093 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006094 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006095 case ParsedAttr::AT_Deprecated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006096 handleDeprecatedAttr(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006097 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006098 case ParsedAttr::AT_Destructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006099 handleDestructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006100 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006101 case ParsedAttr::AT_EnableIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006102 handleEnableIfAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006103 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006104 case ParsedAttr::AT_DiagnoseIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006105 handleDiagnoseIfAttr(S, D, AL);
George Burgess IV177399e2017-01-09 04:12:14 +00006106 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006107 case ParsedAttr::AT_ExtVectorType:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006108 handleExtVectorTypeAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006109 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006110 case ParsedAttr::AT_ExternalSourceSymbol:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006111 handleExternalSourceSymbolAttr(S, D, AL);
Alex Lorenzd5d27e12017-03-01 18:06:25 +00006112 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006113 case ParsedAttr::AT_MinSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006114 handleMinSizeAttr(S, D, AL);
Quentin Colombet4e172062012-11-01 23:55:47 +00006115 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006116 case ParsedAttr::AT_OptimizeNone:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006117 handleOptimizeNoneAttr(S, D, AL);
Paul Robinsonf0674352014-03-31 22:29:15 +00006118 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006119 case ParsedAttr::AT_FlagEnum:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006120 handleSimpleAttribute<FlagEnumAttr>(S, D, AL);
Alexis Hunt724f14e2014-11-28 00:53:20 +00006121 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006122 case ParsedAttr::AT_EnumExtensibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006123 handleEnumExtensibilityAttr(S, D, AL);
Akira Hatanaka3c268af2017-03-21 02:23:00 +00006124 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006125 case ParsedAttr::AT_Flatten:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006126 handleSimpleAttribute<FlattenAttr>(S, D, AL);
Peter Collingbourne41af7c22014-05-20 17:12:51 +00006127 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006128 case ParsedAttr::AT_Format:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006129 handleFormatAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006130 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006131 case ParsedAttr::AT_FormatArg:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006132 handleFormatArgAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006133 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006134 case ParsedAttr::AT_CUDAGlobal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006135 handleGlobalAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006136 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006137 case ParsedAttr::AT_CUDADevice:
Justin Lebar3eaaf862016-01-13 01:07:35 +00006138 handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006139 AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006140 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006141 case ParsedAttr::AT_CUDAHost:
Erich Keanec480f302018-07-12 21:09:05 +00006142 handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006143 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006144 case ParsedAttr::AT_GNUInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006145 handleGNUInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006146 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006147 case ParsedAttr::AT_CUDALaunchBounds:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006148 handleLaunchBoundsAttr(S, D, AL);
Peter Collingbourne827301e2010-12-12 23:03:07 +00006149 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006150 case ParsedAttr::AT_Restrict:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006151 handleRestrictAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006152 break;
Richard Smithf4e248c2018-08-01 00:33:25 +00006153 case ParsedAttr::AT_LifetimeBound:
6154 handleSimpleAttribute<LifetimeBoundAttr>(S, D, AL);
6155 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006156 case ParsedAttr::AT_MayAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006157 handleSimpleAttribute<MayAliasAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006158 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006159 case ParsedAttr::AT_Mode:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006160 handleModeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006161 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006162 case ParsedAttr::AT_NoAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006163 handleSimpleAttribute<NoAliasAttr>(S, D, AL);
David Majnemer1bf0f8e2015-07-20 22:51:52 +00006164 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006165 case ParsedAttr::AT_NoCommon:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006166 handleSimpleAttribute<NoCommonAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006167 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006168 case ParsedAttr::AT_NoSplitStack:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006169 handleSimpleAttribute<NoSplitStackAttr>(S, D, AL);
Peter Collingbourneb4728c12014-05-19 22:14:34 +00006170 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006171 case ParsedAttr::AT_NonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006172 if (auto *PVD = dyn_cast<ParmVarDecl>(D))
6173 handleNonNullAttrParameter(S, PVD, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006174 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006175 handleNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006176 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006177 case ParsedAttr::AT_ReturnsNonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006178 handleReturnsNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006179 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006180 case ParsedAttr::AT_NoEscape:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006181 handleNoEscapeAttr(S, D, AL);
Akira Hatanaka98a49332017-09-22 00:41:05 +00006182 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006183 case ParsedAttr::AT_AssumeAligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006184 handleAssumeAlignedAttr(S, D, AL);
Hal Finkelee90a222014-09-26 05:04:30 +00006185 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006186 case ParsedAttr::AT_AllocAlign:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006187 handleAllocAlignAttr(S, D, AL);
Erich Keane623efd82017-03-30 21:48:55 +00006188 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006189 case ParsedAttr::AT_Overloadable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006190 handleSimpleAttribute<OverloadableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006191 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006192 case ParsedAttr::AT_Ownership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006193 handleOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006194 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006195 case ParsedAttr::AT_Cold:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006196 handleSimpleAttributeWithExclusions<ColdAttr, HotAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006197 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006198 case ParsedAttr::AT_Hot:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006199 handleSimpleAttributeWithExclusions<HotAttr, ColdAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006200 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006201 case ParsedAttr::AT_Naked:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006202 handleNakedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006203 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006204 case ParsedAttr::AT_NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006205 handleNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006206 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006207 case ParsedAttr::AT_AnyX86NoCfCheck:
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006208 handleNoCfCheckAttr(S, D, AL);
6209 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006210 case ParsedAttr::AT_NoThrow:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006211 handleSimpleAttribute<NoThrowAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006212 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006213 case ParsedAttr::AT_CUDAShared:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006214 handleSharedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006215 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006216 case ParsedAttr::AT_VecReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006217 handleVecReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006218 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006219 case ParsedAttr::AT_ObjCOwnership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006220 handleObjCOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006221 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006222 case ParsedAttr::AT_ObjCPreciseLifetime:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006223 handleObjCPreciseLifetimeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006224 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006225 case ParsedAttr::AT_ObjCReturnsInnerPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006226 handleObjCReturnsInnerPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006227 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006228 case ParsedAttr::AT_ObjCRequiresSuper:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006229 handleObjCRequiresSuperAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006230 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006231 case ParsedAttr::AT_ObjCBridge:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006232 handleObjCBridgeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006233 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006234 case ParsedAttr::AT_ObjCBridgeMutable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006235 handleObjCBridgeMutableAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006236 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006237 case ParsedAttr::AT_ObjCBridgeRelated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006238 handleObjCBridgeRelatedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006239 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006240 case ParsedAttr::AT_ObjCDesignatedInitializer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006241 handleObjCDesignatedInitializer(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006242 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006243 case ParsedAttr::AT_ObjCRuntimeName:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006244 handleObjCRuntimeName(S, D, AL);
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00006245 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006246 case ParsedAttr::AT_ObjCRuntimeVisible:
6247 handleSimpleAttribute<ObjCRuntimeVisibleAttr>(S, D, AL);
6248 break;
6249 case ParsedAttr::AT_ObjCBoxable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006250 handleObjCBoxable(S, D, AL);
Alex Denisovfde64952015-06-26 05:28:36 +00006251 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006252 case ParsedAttr::AT_CFAuditedTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006253 handleSimpleAttributeWithExclusions<CFAuditedTransferAttr,
6254 CFUnknownTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006255 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006256 case ParsedAttr::AT_CFUnknownTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006257 handleSimpleAttributeWithExclusions<CFUnknownTransferAttr,
6258 CFAuditedTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006259 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006260 case ParsedAttr::AT_CFConsumed:
6261 case ParsedAttr::AT_NSConsumed:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006262 handleNSConsumedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006263 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006264 case ParsedAttr::AT_NSConsumesSelf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006265 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006266 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006267 case ParsedAttr::AT_NSReturnsAutoreleased:
6268 case ParsedAttr::AT_NSReturnsNotRetained:
6269 case ParsedAttr::AT_CFReturnsNotRetained:
6270 case ParsedAttr::AT_NSReturnsRetained:
6271 case ParsedAttr::AT_CFReturnsRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006272 handleNSReturnsRetainedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006273 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006274 case ParsedAttr::AT_WorkGroupSizeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006275 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006276 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006277 case ParsedAttr::AT_ReqdWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006278 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006279 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006280 case ParsedAttr::AT_OpenCLIntelReqdSubGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006281 handleSubGroupSize(S, D, AL);
Xiuli Panbe6da4b2017-05-04 07:31:20 +00006282 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006283 case ParsedAttr::AT_VecTypeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006284 handleVecTypeHint(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006285 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006286 case ParsedAttr::AT_RequireConstantInit:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006287 handleSimpleAttribute<RequireConstantInitAttr>(S, D, AL);
Eric Fiselier341e8252016-09-02 18:53:31 +00006288 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006289 case ParsedAttr::AT_InitPriority:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006290 handleInitPriorityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006291 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006292 case ParsedAttr::AT_Packed:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006293 handlePackedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006294 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006295 case ParsedAttr::AT_Section:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006296 handleSectionAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006297 break;
Erich Keane7963e8b2018-07-18 20:04:48 +00006298 case ParsedAttr::AT_CodeSeg:
6299 handleCodeSegAttr(S, D, AL);
6300 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006301 case ParsedAttr::AT_Target:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006302 handleTargetAttr(S, D, AL);
Eric Christopher11acf732015-06-12 01:35:52 +00006303 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006304 case ParsedAttr::AT_MinVectorWidth:
Craig Topper74c10e32018-07-09 19:00:16 +00006305 handleMinVectorWidthAttr(S, D, AL);
6306 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006307 case ParsedAttr::AT_Unavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006308 handleAttrWithMessage<UnavailableAttr>(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006309 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006310 case ParsedAttr::AT_ArcWeakrefUnavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006311 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006312 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006313 case ParsedAttr::AT_ObjCRootClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006314 handleSimpleAttribute<ObjCRootClassAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006315 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006316 case ParsedAttr::AT_ObjCSubclassingRestricted:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006317 handleSimpleAttribute<ObjCSubclassingRestrictedAttr>(S, D, AL);
Alex Lorenza8c44ba2016-10-28 10:25:10 +00006318 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006319 case ParsedAttr::AT_ObjCExplicitProtocolImpl:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006320 handleObjCSuppresProtocolAttr(S, D, AL);
Ted Kremenek28eace62013-11-23 01:01:34 +00006321 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006322 case ParsedAttr::AT_ObjCRequiresPropertyDefs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006323 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006324 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006325 case ParsedAttr::AT_Unused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006326 handleUnusedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006327 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006328 case ParsedAttr::AT_ReturnsTwice:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006329 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006330 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006331 case ParsedAttr::AT_NotTailCalled:
Erich Keanec480f302018-07-12 21:09:05 +00006332 handleSimpleAttributeWithExclusions<NotTailCalledAttr, AlwaysInlineAttr>(
6333 S, D, AL);
Akira Hatanakac8667622015-11-06 23:56:15 +00006334 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006335 case ParsedAttr::AT_DisableTailCalls:
Erich Keanec480f302018-07-12 21:09:05 +00006336 handleSimpleAttributeWithExclusions<DisableTailCallsAttr, NakedAttr>(S, D,
6337 AL);
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00006338 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006339 case ParsedAttr::AT_Used:
Aaron Ballman1a3901c2018-03-03 21:02:09 +00006340 handleSimpleAttribute<UsedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006341 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006342 case ParsedAttr::AT_Visibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006343 handleVisibilityAttr(S, D, AL, false);
John McCalld041a9b2013-02-20 01:54:26 +00006344 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006345 case ParsedAttr::AT_TypeVisibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006346 handleVisibilityAttr(S, D, AL, true);
John McCalld041a9b2013-02-20 01:54:26 +00006347 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006348 case ParsedAttr::AT_WarnUnused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006349 handleSimpleAttribute<WarnUnusedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006350 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006351 case ParsedAttr::AT_WarnUnusedResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006352 handleWarnUnusedResult(S, D, AL);
Chris Lattner237f2752009-02-14 07:37:35 +00006353 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006354 case ParsedAttr::AT_Weak:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006355 handleSimpleAttribute<WeakAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006356 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006357 case ParsedAttr::AT_WeakRef:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006358 handleWeakRefAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006359 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006360 case ParsedAttr::AT_WeakImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006361 handleWeakImportAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006362 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006363 case ParsedAttr::AT_TransparentUnion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006364 handleTransparentUnionAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006365 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006366 case ParsedAttr::AT_ObjCException:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006367 handleSimpleAttribute<ObjCExceptionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006368 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006369 case ParsedAttr::AT_ObjCMethodFamily:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006370 handleObjCMethodFamilyAttr(S, D, AL);
John McCall86bc21f2011-03-02 11:33:24 +00006371 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006372 case ParsedAttr::AT_ObjCNSObject:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006373 handleObjCNSObject(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006374 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006375 case ParsedAttr::AT_ObjCIndependentClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006376 handleObjCIndependentClass(S, D, AL);
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00006377 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006378 case ParsedAttr::AT_Blocks:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006379 handleBlocksAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006380 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006381 case ParsedAttr::AT_Sentinel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006382 handleSentinelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006383 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006384 case ParsedAttr::AT_Const:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006385 handleSimpleAttribute<ConstAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006386 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006387 case ParsedAttr::AT_Pure:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006388 handleSimpleAttribute<PureAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006389 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006390 case ParsedAttr::AT_Cleanup:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006391 handleCleanupAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006392 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006393 case ParsedAttr::AT_NoDebug:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006394 handleNoDebugAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006395 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006396 case ParsedAttr::AT_NoDuplicate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006397 handleSimpleAttribute<NoDuplicateAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006398 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006399 case ParsedAttr::AT_Convergent:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006400 handleSimpleAttribute<ConvergentAttr>(S, D, AL);
Yaxun Liu7d07ae72016-11-01 18:45:32 +00006401 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006402 case ParsedAttr::AT_NoInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006403 handleSimpleAttribute<NoInlineAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006404 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006405 case ParsedAttr::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006406 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006407 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006408 case ParsedAttr::AT_NoStackProtector:
Manoj Gupta4fbf84c2018-05-09 21:41:18 +00006409 // Interacts with -fstack-protector options.
6410 handleSimpleAttribute<NoStackProtectorAttr>(S, D, AL);
6411 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006412 case ParsedAttr::AT_StdCall:
6413 case ParsedAttr::AT_CDecl:
6414 case ParsedAttr::AT_FastCall:
6415 case ParsedAttr::AT_ThisCall:
6416 case ParsedAttr::AT_Pascal:
6417 case ParsedAttr::AT_RegCall:
6418 case ParsedAttr::AT_SwiftCall:
6419 case ParsedAttr::AT_VectorCall:
6420 case ParsedAttr::AT_MSABI:
6421 case ParsedAttr::AT_SysVABI:
6422 case ParsedAttr::AT_Pcs:
6423 case ParsedAttr::AT_IntelOclBicc:
6424 case ParsedAttr::AT_PreserveMost:
6425 case ParsedAttr::AT_PreserveAll:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006426 handleCallConvAttr(S, D, AL);
John McCallab26cfa2010-02-05 21:31:56 +00006427 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006428 case ParsedAttr::AT_Suppress:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006429 handleSuppressAttr(S, D, AL);
Matthias Gehre01a63382017-03-27 19:45:24 +00006430 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006431 case ParsedAttr::AT_OpenCLKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006432 handleSimpleAttribute<OpenCLKernelAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006433 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006434 case ParsedAttr::AT_OpenCLAccess:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006435 handleOpenCLAccessAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006436 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006437 case ParsedAttr::AT_OpenCLNoSVM:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006438 handleOpenCLNoSVMAttr(S, D, AL);
Anastasia Stulovafde76222016-04-01 16:05:09 +00006439 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006440 case ParsedAttr::AT_SwiftContext:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006441 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftContext);
John McCall477f2bb2016-03-03 06:39:32 +00006442 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006443 case ParsedAttr::AT_SwiftErrorResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006444 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftErrorResult);
John McCall477f2bb2016-03-03 06:39:32 +00006445 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006446 case ParsedAttr::AT_SwiftIndirectResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006447 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftIndirectResult);
John McCall477f2bb2016-03-03 06:39:32 +00006448 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006449 case ParsedAttr::AT_InternalLinkage:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006450 handleInternalLinkageAttr(S, D, AL);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00006451 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006452 case ParsedAttr::AT_LTOVisibilityPublic:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006453 handleSimpleAttribute<LTOVisibilityPublicAttr>(S, D, AL);
Peter Collingbourne3afb2662016-04-28 17:09:37 +00006454 break;
John McCall8d32c052012-05-22 21:28:12 +00006455
6456 // Microsoft attributes:
Erich Keanee891aa92018-07-13 15:07:47 +00006457 case ParsedAttr::AT_EmptyBases:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006458 handleSimpleAttribute<EmptyBasesAttr>(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00006459 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006460 case ParsedAttr::AT_LayoutVersion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006461 handleLayoutVersion(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00006462 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006463 case ParsedAttr::AT_TrivialABI:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006464 handleSimpleAttribute<TrivialABIAttr>(S, D, AL);
Akira Hatanaka02914dc2018-02-05 20:23:22 +00006465 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006466 case ParsedAttr::AT_MSNoVTable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006467 handleSimpleAttribute<MSNoVTableAttr>(S, D, AL);
David Majnemer129f4172015-02-02 10:22:20 +00006468 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006469 case ParsedAttr::AT_MSStruct:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006470 handleSimpleAttribute<MSStructAttr>(S, D, AL);
John McCall8d32c052012-05-22 21:28:12 +00006471 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006472 case ParsedAttr::AT_Uuid:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006473 handleUuidAttr(S, D, AL);
Francois Picheta83957a2010-12-19 06:50:37 +00006474 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006475 case ParsedAttr::AT_MSInheritance:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006476 handleMSInheritanceAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006477 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006478 case ParsedAttr::AT_SelectAny:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006479 handleSimpleAttribute<SelectAnyAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006480 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006481 case ParsedAttr::AT_Thread:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006482 handleDeclspecThreadAttr(S, D, AL);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00006483 break;
David Majnemercd3ebfe2016-05-23 17:16:12 +00006484
Erich Keanee891aa92018-07-13 15:07:47 +00006485 case ParsedAttr::AT_AbiTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006486 handleAbiTagAttr(S, D, AL);
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00006487 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006488
6489 // Thread safety attributes:
Erich Keanee891aa92018-07-13 15:07:47 +00006490 case ParsedAttr::AT_AssertExclusiveLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006491 handleAssertExclusiveLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00006492 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006493 case ParsedAttr::AT_AssertSharedLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006494 handleAssertSharedLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00006495 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006496 case ParsedAttr::AT_GuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006497 handleSimpleAttribute<GuardedVarAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006498 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006499 case ParsedAttr::AT_PtGuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006500 handlePtGuardedVarAttr(S, D, AL);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006501 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006502 case ParsedAttr::AT_ScopedLockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006503 handleSimpleAttribute<ScopedLockableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006504 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006505 case ParsedAttr::AT_NoSanitize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006506 handleNoSanitizeAttr(S, D, AL);
Peter Collingbourne915df992015-05-15 18:33:32 +00006507 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006508 case ParsedAttr::AT_NoSanitizeSpecific:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006509 handleNoSanitizeSpecificAttr(S, D, AL);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00006510 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006511 case ParsedAttr::AT_NoThreadSafetyAnalysis:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006512 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, AL);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00006513 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006514 case ParsedAttr::AT_GuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006515 handleGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006516 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006517 case ParsedAttr::AT_PtGuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006518 handlePtGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006519 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006520 case ParsedAttr::AT_ExclusiveTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006521 handleExclusiveTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006522 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006523 case ParsedAttr::AT_LockReturned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006524 handleLockReturnedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006525 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006526 case ParsedAttr::AT_LocksExcluded:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006527 handleLocksExcludedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006528 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006529 case ParsedAttr::AT_SharedTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006530 handleSharedTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006531 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006532 case ParsedAttr::AT_AcquiredBefore:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006533 handleAcquiredBeforeAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006534 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006535 case ParsedAttr::AT_AcquiredAfter:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006536 handleAcquiredAfterAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006537 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006538
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006539 // Capability analysis attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006540 case ParsedAttr::AT_Capability:
6541 case ParsedAttr::AT_Lockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006542 handleCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006543 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006544 case ParsedAttr::AT_RequiresCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006545 handleRequiresCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006546 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006547
Erich Keanee891aa92018-07-13 15:07:47 +00006548 case ParsedAttr::AT_AssertCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006549 handleAssertCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006550 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006551 case ParsedAttr::AT_AcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006552 handleAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006553 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006554 case ParsedAttr::AT_ReleaseCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006555 handleReleaseCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006556 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006557 case ParsedAttr::AT_TryAcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006558 handleTryAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006559 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006560
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00006561 // Consumed analysis attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006562 case ParsedAttr::AT_Consumable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006563 handleConsumableAttr(S, D, AL);
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00006564 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006565 case ParsedAttr::AT_ConsumableAutoCast:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006566 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006567 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006568 case ParsedAttr::AT_ConsumableSetOnRead:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006569 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006570 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006571 case ParsedAttr::AT_CallableWhen:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006572 handleCallableWhenAttr(S, D, AL);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00006573 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006574 case ParsedAttr::AT_ParamTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006575 handleParamTypestateAttr(S, D, AL);
DeLesley Hutchins69391772013-10-17 23:23:53 +00006576 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006577 case ParsedAttr::AT_ReturnTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006578 handleReturnTypestateAttr(S, D, AL);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00006579 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006580 case ParsedAttr::AT_SetTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006581 handleSetTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00006582 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006583 case ParsedAttr::AT_TestTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006584 handleTestTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00006585 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00006586
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006587 // Type safety attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006588 case ParsedAttr::AT_ArgumentWithTypeTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006589 handleArgumentWithTypeTagAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006590 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006591 case ParsedAttr::AT_TypeTagForDatatype:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006592 handleTypeTagForDatatypeAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006593 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006594 case ParsedAttr::AT_AnyX86NoCallerSavedRegisters:
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006595 handleSimpleAttribute<AnyX86NoCallerSavedRegistersAttr>(S, D, AL);
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00006596 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006597 case ParsedAttr::AT_RenderScriptKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006598 handleSimpleAttribute<RenderScriptKernelAttr>(S, D, AL);
Pirama Arumuga Nainar8b788d02016-06-09 23:34:20 +00006599 break;
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00006600 // XRay attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006601 case ParsedAttr::AT_XRayInstrument:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006602 handleSimpleAttribute<XRayInstrumentAttr>(S, D, AL);
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00006603 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006604 case ParsedAttr::AT_XRayLogArgs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006605 handleXRayLogArgsAttr(S, D, AL);
Dean Michael Berris418da3f2017-03-06 07:08:21 +00006606 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006607 }
6608}
6609
6610/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
6611/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00006612void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Erich Keanec480f302018-07-12 21:09:05 +00006613 const ParsedAttributesView &AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00006614 bool IncludeCXX11Attributes) {
Erich Keanec480f302018-07-12 21:09:05 +00006615 if (AttrList.empty())
6616 return;
6617
Erich Keanee891aa92018-07-13 15:07:47 +00006618 for (const ParsedAttr &AL : AttrList)
Erich Keanec480f302018-07-12 21:09:05 +00006619 ProcessDeclAttribute(*this, S, D, AL, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00006620
Joey Gouly2cd9db12013-12-13 16:15:28 +00006621 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00006622 // GCC accepts
6623 // static int a9 __attribute__((weakref));
6624 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00006625 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Erich Keanec480f302018-07-12 21:09:05 +00006626 Diag(AttrList.begin()->getLoc(), diag::err_attribute_weakref_without_alias)
6627 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00006628 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00006629 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006630 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00006631
Aaron Ballmanbe243a72014-12-04 22:45:31 +00006632 // FIXME: We should be able to handle this in TableGen as well. It would be
6633 // good to have a way to specify "these attributes must appear as a group",
6634 // for these. Additionally, it would be good to have a way to specify "these
6635 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00006636 if (!D->hasAttr<OpenCLKernelAttr>()) {
6637 // These attributes cannot be applied to a non-kernel function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006638 if (const auto *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00006639 // FIXME: This emits a different error message than
6640 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00006641 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006642 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006643 } else if (const auto *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00006644 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006645 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006646 } else if (const auto *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00006647 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006648 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006649 } else if (const auto *A = D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00006650 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
6651 D->setInvalidDecl();
Yaxun Liuaa246012018-06-12 23:58:59 +00006652 } else if (!D->hasAttr<CUDAGlobalAttr>()) {
6653 if (const auto *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) {
6654 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6655 << A << ExpectedKernelFunction;
6656 D->setInvalidDecl();
6657 } else if (const auto *A = D->getAttr<AMDGPUWavesPerEUAttr>()) {
6658 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6659 << A << ExpectedKernelFunction;
6660 D->setInvalidDecl();
6661 } else if (const auto *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
6662 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6663 << A << ExpectedKernelFunction;
6664 D->setInvalidDecl();
6665 } else if (const auto *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
6666 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6667 << A << ExpectedKernelFunction;
6668 D->setInvalidDecl();
6669 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00006670 }
6671 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006672}
6673
Hiroshi Inoue939d9322017-06-30 05:40:31 +00006674// Helper for delayed processing TransparentUnion attribute.
Erich Keanec480f302018-07-12 21:09:05 +00006675void Sema::ProcessDeclAttributeDelayed(Decl *D,
6676 const ParsedAttributesView &AttrList) {
Erich Keanee891aa92018-07-13 15:07:47 +00006677 for (const ParsedAttr &AL : AttrList)
6678 if (AL.getKind() == ParsedAttr::AT_TransparentUnion) {
Erich Keanec480f302018-07-12 21:09:05 +00006679 handleTransparentUnionAttr(*this, D, AL);
Erich Keane2fe684b2017-02-28 20:44:39 +00006680 break;
6681 }
6682}
6683
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006684// Annotation attributes are the only attributes allowed after an access
6685// specifier.
Erich Keanec480f302018-07-12 21:09:05 +00006686bool Sema::ProcessAccessDeclAttributeList(
6687 AccessSpecDecl *ASDecl, const ParsedAttributesView &AttrList) {
Erich Keanee891aa92018-07-13 15:07:47 +00006688 for (const ParsedAttr &AL : AttrList) {
6689 if (AL.getKind() == ParsedAttr::AT_Annotate) {
Erich Keanec480f302018-07-12 21:09:05 +00006690 ProcessDeclAttribute(*this, nullptr, ASDecl, AL, AL.isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006691 } else {
Erich Keanec480f302018-07-12 21:09:05 +00006692 Diag(AL.getLoc(), diag::err_only_annotate_after_access_spec);
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006693 return true;
6694 }
6695 }
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006696 return false;
6697}
6698
John McCall42856de2011-10-01 05:17:03 +00006699/// checkUnusedDeclAttributes - Check a list of attributes to see if it
6700/// contains any decl attributes that we should warn about.
Erich Keanec480f302018-07-12 21:09:05 +00006701static void checkUnusedDeclAttributes(Sema &S, const ParsedAttributesView &A) {
Erich Keanee891aa92018-07-13 15:07:47 +00006702 for (const ParsedAttr &AL : A) {
John McCall42856de2011-10-01 05:17:03 +00006703 // Only warn if the attribute is an unignored, non-type attribute.
Erich Keanec480f302018-07-12 21:09:05 +00006704 if (AL.isUsedAsTypeAttr() || AL.isInvalid())
6705 continue;
Erich Keanee891aa92018-07-13 15:07:47 +00006706 if (AL.getKind() == ParsedAttr::IgnoredAttribute)
Erich Keanec480f302018-07-12 21:09:05 +00006707 continue;
John McCall42856de2011-10-01 05:17:03 +00006708
Erich Keanee891aa92018-07-13 15:07:47 +00006709 if (AL.getKind() == ParsedAttr::UnknownAttribute) {
Erich Keanec480f302018-07-12 21:09:05 +00006710 S.Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored)
6711 << AL.getName() << AL.getRange();
John McCall42856de2011-10-01 05:17:03 +00006712 } else {
Erich Keanec480f302018-07-12 21:09:05 +00006713 S.Diag(AL.getLoc(), diag::warn_attribute_not_on_decl)
6714 << AL.getName() << AL.getRange();
John McCall42856de2011-10-01 05:17:03 +00006715 }
6716 }
6717}
6718
6719/// checkUnusedDeclAttributes - Given a declarator which is not being
6720/// used to build a declaration, complain about any decl attributes
6721/// which might be lying around on it.
6722void Sema::checkUnusedDeclAttributes(Declarator &D) {
Erich Keanec480f302018-07-12 21:09:05 +00006723 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes());
John McCall42856de2011-10-01 05:17:03 +00006724 ::checkUnusedDeclAttributes(*this, D.getAttributes());
6725 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
6726 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
6727}
6728
Ryan Flynn7d470f32009-07-30 03:15:39 +00006729/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00006730/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00006731NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
6732 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00006733 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00006734 NamedDecl *NewD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006735 if (auto *FD = dyn_cast<FunctionDecl>(ND)) {
Alexander Kornienko061900f2015-12-03 11:37:28 +00006736 FunctionDecl *NewFD;
6737 // FIXME: Missing call to CheckFunctionDeclaration().
Eli Friedmance3e2c82011-09-07 04:05:06 +00006738 // FIXME: Mangling?
6739 // FIXME: Is the qualifier info correct?
6740 // FIXME: Is the DeclContext correct?
Alexander Kornienko061900f2015-12-03 11:37:28 +00006741 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
6742 Loc, Loc, DeclarationName(II),
6743 FD->getType(), FD->getTypeSourceInfo(),
6744 SC_None, false/*isInlineSpecified*/,
6745 FD->hasPrototype(),
6746 false/*isConstexprSpecified*/);
Eli Friedmance3e2c82011-09-07 04:05:06 +00006747 NewD = NewFD;
6748
6749 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00006750 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00006751
6752 // Fake up parameter variables; they are declared as if this were
6753 // a typedef.
6754 QualType FDTy = FD->getType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006755 if (const auto *FT = FDTy->getAs<FunctionProtoType>()) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00006756 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00006757 for (const auto &AI : FT->param_types()) {
6758 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00006759 Param->setScopeInfo(0, Params.size());
6760 Params.push_back(Param);
6761 }
David Blaikie9c70e042011-09-21 18:16:56 +00006762 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00006763 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006764 } else if (auto *VD = dyn_cast<VarDecl>(ND)) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00006765 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00006766 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00006767 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00006768 VD->getStorageClass());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006769 if (VD->getQualifier())
Fangrui Song99337e22018-07-20 08:19:20 +00006770 cast<VarDecl>(NewD)->setQualifierInfo(VD->getQualifierLoc());
Ryan Flynn7d470f32009-07-30 03:15:39 +00006771 }
6772 return NewD;
6773}
6774
James Dennett634962f2012-06-14 21:40:34 +00006775/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00006776/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00006777void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00006778 if (W.getUsed()) return; // only do this once
6779 W.setUsed(true);
6780 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
6781 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00006782 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00006783 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
6784 W.getLocation()));
6785 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00006786 WeakTopLevelDecl.push_back(NewD);
6787 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
6788 // to insert Decl at TU scope, sorry.
6789 DeclContext *SavedContext = CurContext;
6790 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00006791 NewD->setDeclContext(CurContext);
6792 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00006793 PushOnScopeChains(NewD, S);
6794 CurContext = SavedContext;
6795 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00006796 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00006797 }
6798}
6799
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006800void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
6801 // It's valid to "forward-declare" #pragma weak, in which case we
6802 // have to do this.
6803 LoadExternalWeakUndeclaredIdentifiers();
6804 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00006805 NamedDecl *ND = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006806 if (auto *VD = dyn_cast<VarDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006807 if (VD->isExternC())
6808 ND = VD;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006809 if (auto *FD = dyn_cast<FunctionDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006810 if (FD->isExternC())
6811 ND = FD;
6812 if (ND) {
6813 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00006814 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006815 if (I != WeakUndeclaredIdentifiers.end()) {
6816 WeakInfo W = I->second;
6817 DeclApplyPragmaWeak(S, ND, W);
6818 WeakUndeclaredIdentifiers[Id] = W;
6819 }
6820 }
6821 }
6822 }
6823}
6824
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006825/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
6826/// it, apply them to D. This is a bit tricky because PD can have attributes
6827/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00006828void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006829 // Apply decl attributes from the DeclSpec if present.
Erich Keanec480f302018-07-12 21:09:05 +00006830 if (!PD.getDeclSpec().getAttributes().empty())
6831 ProcessDeclAttributeList(S, D, PD.getDeclSpec().getAttributes());
Mike Stumpd3bb5572009-07-24 19:02:52 +00006832
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006833 // Walk the declarator structure, applying decl attributes that were in a type
6834 // position to the decl itself. This handles cases like:
6835 // int *__attr__(x)** D;
6836 // when X is a decl attribute.
6837 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
Erich Keanec480f302018-07-12 21:09:05 +00006838 ProcessDeclAttributeList(S, D, PD.getTypeObject(i).getAttrs(),
6839 /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00006840
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006841 // Finally, apply any attributes on the decl itself.
Erich Keanec480f302018-07-12 21:09:05 +00006842 ProcessDeclAttributeList(S, D, PD.getAttributes());
Alex Lorenz9e7bf162017-04-18 14:33:39 +00006843
6844 // Apply additional attributes specified by '#pragma clang attribute'.
6845 AddPragmaAttributes(S, D);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006846}
John McCall28a6aea2009-11-04 02:18:39 +00006847
John McCall31168b02011-06-15 23:02:42 +00006848/// Is the given declaration allowed to use a forbidden type?
John McCallb61e14e2015-10-27 04:54:50 +00006849/// If so, it'll still be annotated with an attribute that makes it
6850/// illegal to actually use.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006851static bool isForbiddenTypeAllowed(Sema &S, Decl *D,
John McCallb61e14e2015-10-27 04:54:50 +00006852 const DelayedDiagnostic &diag,
John McCallc6af8c62015-10-28 05:03:19 +00006853 UnavailableAttr::ImplicitReason &reason) {
John McCall31168b02011-06-15 23:02:42 +00006854 // Private ivars are always okay. Unfortunately, people don't
6855 // always properly make their ivars private, even in system headers.
6856 // Plus we need to make fields okay, too.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006857 if (!isa<FieldDecl>(D) && !isa<ObjCPropertyDecl>(D) &&
6858 !isa<FunctionDecl>(D))
John McCall31168b02011-06-15 23:02:42 +00006859 return false;
6860
John McCallc6af8c62015-10-28 05:03:19 +00006861 // Silently accept unsupported uses of __weak in both user and system
6862 // declarations when it's been disabled, for ease of integration with
6863 // -fno-objc-arc files. We do have to take some care against attempts
6864 // to define such things; for now, we've only done that for ivars
6865 // and properties.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006866 if ((isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) {
John McCallc6af8c62015-10-28 05:03:19 +00006867 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
6868 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
6869 reason = UnavailableAttr::IR_ForbiddenWeak;
6870 return true;
6871 }
John McCallb61e14e2015-10-27 04:54:50 +00006872 }
6873
John McCallc6af8c62015-10-28 05:03:19 +00006874 // Allow all sorts of things in system headers.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006875 if (S.Context.getSourceManager().isInSystemHeader(D->getLocation())) {
John McCallc6af8c62015-10-28 05:03:19 +00006876 // Currently, all the failures dealt with this way are due to ARC
6877 // restrictions.
6878 reason = UnavailableAttr::IR_ARCForbiddenType;
6879 return true;
John McCallb61e14e2015-10-27 04:54:50 +00006880 }
6881
6882 return false;
John McCall31168b02011-06-15 23:02:42 +00006883}
6884
6885/// Handle a delayed forbidden-type diagnostic.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006886static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &DD,
6887 Decl *D) {
6888 auto Reason = UnavailableAttr::IR_None;
6889 if (D && isForbiddenTypeAllowed(S, D, DD, Reason)) {
6890 assert(Reason && "didn't set reason?");
6891 D->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", Reason, DD.Loc));
John McCall31168b02011-06-15 23:02:42 +00006892 return;
6893 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00006894 if (S.getLangOpts().ObjCAutoRefCount)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006895 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00006896 // FIXME: we may want to suppress diagnostics for all
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006897 // kind of forbidden type messages on unavailable functions.
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00006898 if (FD->hasAttr<UnavailableAttr>() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006899 DD.getForbiddenTypeDiagnostic() ==
6900 diag::err_arc_array_param_no_ownership) {
6901 DD.Triggered = true;
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00006902 return;
6903 }
6904 }
John McCall31168b02011-06-15 23:02:42 +00006905
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006906 S.Diag(DD.Loc, DD.getForbiddenTypeDiagnostic())
6907 << DD.getForbiddenTypeOperand() << DD.getForbiddenTypeArgument();
6908 DD.Triggered = true;
John McCall31168b02011-06-15 23:02:42 +00006909}
6910
Manman Ren45b1ab12016-05-06 19:57:16 +00006911static const AvailabilityAttr *getAttrForPlatform(ASTContext &Context,
6912 const Decl *D) {
6913 // Check each AvailabilityAttr to find the one for this platform.
6914 for (const auto *A : D->attrs()) {
6915 if (const auto *Avail = dyn_cast<AvailabilityAttr>(A)) {
6916 // FIXME: this is copied from CheckAvailability. We should try to
6917 // de-duplicate.
6918
6919 // Check if this is an App Extension "platform", and if so chop off
6920 // the suffix for matching with the actual platform.
6921 StringRef ActualPlatform = Avail->getPlatform()->getName();
6922 StringRef RealizedPlatform = ActualPlatform;
6923 if (Context.getLangOpts().AppExt) {
6924 size_t suffix = RealizedPlatform.rfind("_app_extension");
6925 if (suffix != StringRef::npos)
6926 RealizedPlatform = RealizedPlatform.slice(0, suffix);
6927 }
6928
6929 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
6930
6931 // Match the platform name.
6932 if (RealizedPlatform == TargetPlatform)
6933 return Avail;
6934 }
6935 }
6936 return nullptr;
6937}
6938
Erik Pilkington9f866a72017-07-18 20:32:07 +00006939/// The diagnostic we should emit for \c D, and the declaration that
6940/// originated it, or \c AR_Available.
6941///
6942/// \param D The declaration to check.
6943/// \param Message If non-null, this will be populated with the message from
6944/// the availability attribute that is selected.
6945static std::pair<AvailabilityResult, const NamedDecl *>
6946ShouldDiagnoseAvailabilityOfDecl(const NamedDecl *D, std::string *Message) {
6947 AvailabilityResult Result = D->getAvailability(Message);
6948
6949 // For typedefs, if the typedef declaration appears available look
6950 // to the underlying type to see if it is more restrictive.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006951 while (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00006952 if (Result == AR_Available) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006953 if (const auto *TT = TD->getUnderlyingType()->getAs<TagType>()) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00006954 D = TT->getDecl();
6955 Result = D->getAvailability(Message);
6956 continue;
6957 }
6958 }
6959 break;
6960 }
6961
6962 // Forward class declarations get their attributes from their definition.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006963 if (const auto *IDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00006964 if (IDecl->getDefinition()) {
6965 D = IDecl->getDefinition();
6966 Result = D->getAvailability(Message);
6967 }
6968 }
6969
6970 if (const auto *ECD = dyn_cast<EnumConstantDecl>(D))
6971 if (Result == AR_Available) {
6972 const DeclContext *DC = ECD->getDeclContext();
6973 if (const auto *TheEnumDecl = dyn_cast<EnumDecl>(DC)) {
6974 Result = TheEnumDecl->getAvailability(Message);
6975 D = TheEnumDecl;
6976 }
6977 }
6978
6979 return {Result, D};
6980}
6981
6982
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006983/// whether we should emit a diagnostic for \c K and \c DeclVersion in
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00006984/// the context of \c Ctx. For example, we should emit an unavailable diagnostic
6985/// in a deprecated context, but not the other way around.
6986static bool ShouldDiagnoseAvailabilityInContext(Sema &S, AvailabilityResult K,
6987 VersionTuple DeclVersion,
6988 Decl *Ctx) {
6989 assert(K != AR_Available && "Expected an unavailable declaration here!");
6990
6991 // Checks if we should emit the availability diagnostic in the context of C.
6992 auto CheckContext = [&](const Decl *C) {
6993 if (K == AR_NotYetIntroduced) {
6994 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, C))
6995 if (AA->getIntroduced() >= DeclVersion)
6996 return true;
6997 } else if (K == AR_Deprecated)
6998 if (C->isDeprecated())
6999 return true;
7000
7001 if (C->isUnavailable())
7002 return true;
7003 return false;
7004 };
7005
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007006 do {
7007 if (CheckContext(Ctx))
7008 return false;
7009
7010 // An implementation implicitly has the availability of the interface.
Steven Wu3bb4aa52018-04-16 23:34:18 +00007011 // Unless it is "+load" method.
7012 if (const auto *MethodD = dyn_cast<ObjCMethodDecl>(Ctx))
7013 if (MethodD->isClassMethod() &&
7014 MethodD->getSelector().getAsString() == "load")
7015 return true;
7016
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007017 if (const auto *CatOrImpl = dyn_cast<ObjCImplDecl>(Ctx)) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007018 if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface())
7019 if (CheckContext(Interface))
7020 return false;
7021 }
7022 // A category implicitly has the availability of the interface.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007023 else if (const auto *CatD = dyn_cast<ObjCCategoryDecl>(Ctx))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007024 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
7025 if (CheckContext(Interface))
7026 return false;
7027 } while ((Ctx = cast_or_null<Decl>(Ctx->getDeclContext())));
7028
7029 return true;
7030}
7031
Alex Lorenzc9a369f2017-06-22 17:02:24 +00007032static bool
7033shouldDiagnoseAvailabilityByDefault(const ASTContext &Context,
7034 const VersionTuple &DeploymentVersion,
7035 const VersionTuple &DeclVersion) {
7036 const auto &Triple = Context.getTargetInfo().getTriple();
7037 VersionTuple ForceAvailabilityFromVersion;
7038 switch (Triple.getOS()) {
7039 case llvm::Triple::IOS:
7040 case llvm::Triple::TvOS:
7041 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/11);
7042 break;
7043 case llvm::Triple::WatchOS:
7044 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/4);
7045 break;
7046 case llvm::Triple::Darwin:
7047 case llvm::Triple::MacOSX:
7048 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/10, /*Minor=*/13);
7049 break;
7050 default:
7051 // New targets should always warn about availability.
7052 return Triple.getVendor() == llvm::Triple::Apple;
7053 }
7054 return DeploymentVersion >= ForceAvailabilityFromVersion ||
7055 DeclVersion >= ForceAvailabilityFromVersion;
7056}
7057
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007058static NamedDecl *findEnclosingDeclToAnnotate(Decl *OrigCtx) {
7059 for (Decl *Ctx = OrigCtx; Ctx;
7060 Ctx = cast_or_null<Decl>(Ctx->getDeclContext())) {
7061 if (isa<TagDecl>(Ctx) || isa<FunctionDecl>(Ctx) || isa<ObjCMethodDecl>(Ctx))
7062 return cast<NamedDecl>(Ctx);
7063 if (auto *CD = dyn_cast<ObjCContainerDecl>(Ctx)) {
7064 if (auto *Imp = dyn_cast<ObjCImplDecl>(Ctx))
7065 return Imp->getClassInterface();
7066 return CD;
7067 }
7068 }
7069
7070 return dyn_cast<NamedDecl>(OrigCtx);
7071}
7072
Alex Lorenz727c21e2017-07-26 13:58:02 +00007073namespace {
7074
7075struct AttributeInsertion {
7076 StringRef Prefix;
7077 SourceLocation Loc;
7078 StringRef Suffix;
7079
7080 static AttributeInsertion createInsertionAfter(const NamedDecl *D) {
7081 return {" ", D->getLocEnd(), ""};
7082 }
7083 static AttributeInsertion createInsertionAfter(SourceLocation Loc) {
7084 return {" ", Loc, ""};
7085 }
7086 static AttributeInsertion createInsertionBefore(const NamedDecl *D) {
7087 return {"", D->getLocStart(), "\n"};
7088 }
7089};
7090
7091} // end anonymous namespace
7092
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007093/// Tries to parse a string as ObjC method name.
7094///
7095/// \param Name The string to parse. Expected to originate from availability
7096/// attribute argument.
7097/// \param SlotNames The vector that will be populated with slot names. In case
7098/// of unsuccessful parsing can contain invalid data.
7099/// \returns A number of method parameters if parsing was successful, None
7100/// otherwise.
7101static Optional<unsigned>
7102tryParseObjCMethodName(StringRef Name, SmallVectorImpl<StringRef> &SlotNames,
7103 const LangOptions &LangOpts) {
7104 // Accept replacements starting with - or + as valid ObjC method names.
7105 if (!Name.empty() && (Name.front() == '-' || Name.front() == '+'))
7106 Name = Name.drop_front(1);
7107 if (Name.empty())
7108 return None;
7109 Name.split(SlotNames, ':');
7110 unsigned NumParams;
7111 if (Name.back() == ':') {
7112 // Remove an empty string at the end that doesn't represent any slot.
7113 SlotNames.pop_back();
7114 NumParams = SlotNames.size();
7115 } else {
7116 if (SlotNames.size() != 1)
7117 // Not a valid method name, just a colon-separated string.
7118 return None;
7119 NumParams = 0;
7120 }
7121 // Verify all slot names are valid.
7122 bool AllowDollar = LangOpts.DollarIdents;
7123 for (StringRef S : SlotNames) {
7124 if (S.empty())
7125 continue;
7126 if (!isValidIdentifier(S, AllowDollar))
7127 return None;
7128 }
7129 return NumParams;
7130}
7131
Alex Lorenz727c21e2017-07-26 13:58:02 +00007132/// Returns a source location in which it's appropriate to insert a new
7133/// attribute for the given declaration \D.
7134static Optional<AttributeInsertion>
7135createAttributeInsertion(const NamedDecl *D, const SourceManager &SM,
7136 const LangOptions &LangOpts) {
7137 if (isa<ObjCPropertyDecl>(D))
7138 return AttributeInsertion::createInsertionAfter(D);
7139 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
7140 if (MD->hasBody())
7141 return None;
7142 return AttributeInsertion::createInsertionAfter(D);
7143 }
7144 if (const auto *TD = dyn_cast<TagDecl>(D)) {
7145 SourceLocation Loc =
7146 Lexer::getLocForEndOfToken(TD->getInnerLocStart(), 0, SM, LangOpts);
7147 if (Loc.isInvalid())
7148 return None;
7149 // Insert after the 'struct'/whatever keyword.
7150 return AttributeInsertion::createInsertionAfter(Loc);
7151 }
7152 return AttributeInsertion::createInsertionBefore(D);
7153}
7154
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007155/// Actually emit an availability diagnostic for a reference to an unavailable
7156/// decl.
7157///
7158/// \param Ctx The context that the reference occurred in
7159/// \param ReferringDecl The exact declaration that was referenced.
7160/// \param OffendingDecl A related decl to \c ReferringDecl that has an
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00007161/// availability attribute corresponding to \c K attached to it. Note that this
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007162/// may not be the same as ReferringDecl, i.e. if an EnumDecl is annotated and
7163/// we refer to a member EnumConstantDecl, ReferringDecl is the EnumConstantDecl
7164/// and OffendingDecl is the EnumDecl.
Erik Pilkington796a3e22016-08-05 22:59:03 +00007165static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007166 Decl *Ctx, const NamedDecl *ReferringDecl,
7167 const NamedDecl *OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007168 StringRef Message,
7169 ArrayRef<SourceLocation> Locs,
Aaron Ballmanfb237522014-10-15 15:37:51 +00007170 const ObjCInterfaceDecl *UnknownObjCClass,
7171 const ObjCPropertyDecl *ObjCProperty,
7172 bool ObjCPropertyAccess) {
7173 // Diagnostics for deprecated or unavailable.
7174 unsigned diag, diag_message, diag_fwdclass_message;
John McCallb61e14e2015-10-27 04:54:50 +00007175 unsigned diag_available_here = diag::note_availability_specified_here;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007176 SourceLocation NoteLocation = OffendingDecl->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00007177
7178 // Matches 'diag::note_property_attribute' options.
7179 unsigned property_note_select;
7180
7181 // Matches diag::note_availability_specified_here.
7182 unsigned available_here_select_kind;
7183
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007184 VersionTuple DeclVersion;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007185 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, OffendingDecl))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007186 DeclVersion = AA->getIntroduced();
7187
7188 if (!ShouldDiagnoseAvailabilityInContext(S, K, DeclVersion, Ctx))
7189 return;
7190
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007191 SourceLocation Loc = Locs.front();
7192
Erik Pilkington8b352c42017-08-14 19:49:12 +00007193 // The declaration can have multiple availability attributes, we are looking
7194 // at one of them.
7195 const AvailabilityAttr *A = getAttrForPlatform(S.Context, OffendingDecl);
7196 if (A && A->isInherited()) {
7197 for (const Decl *Redecl = OffendingDecl->getMostRecentDecl(); Redecl;
7198 Redecl = Redecl->getPreviousDecl()) {
7199 const AvailabilityAttr *AForRedecl =
7200 getAttrForPlatform(S.Context, Redecl);
7201 if (AForRedecl && !AForRedecl->isInherited()) {
7202 // If D is a declaration with inherited attributes, the note should
7203 // point to the declaration with actual attributes.
7204 NoteLocation = Redecl->getLocation();
7205 break;
7206 }
7207 }
7208 }
7209
Aaron Ballmanfb237522014-10-15 15:37:51 +00007210 switch (K) {
Erik Pilkington8b352c42017-08-14 19:49:12 +00007211 case AR_NotYetIntroduced: {
7212 // We would like to emit the diagnostic even if -Wunguarded-availability is
7213 // not specified for deployment targets >= to iOS 11 or equivalent or
7214 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
7215 // later.
7216 const AvailabilityAttr *AA =
7217 getAttrForPlatform(S.getASTContext(), OffendingDecl);
7218 VersionTuple Introduced = AA->getIntroduced();
7219
7220 bool UseNewWarning = shouldDiagnoseAvailabilityByDefault(
7221 S.Context, S.Context.getTargetInfo().getPlatformMinVersion(),
7222 Introduced);
7223 unsigned Warning = UseNewWarning ? diag::warn_unguarded_availability_new
7224 : diag::warn_unguarded_availability;
7225
7226 S.Diag(Loc, Warning)
7227 << OffendingDecl
7228 << AvailabilityAttr::getPrettyPlatformName(
7229 S.getASTContext().getTargetInfo().getPlatformName())
7230 << Introduced.getAsString();
7231
7232 S.Diag(OffendingDecl->getLocation(), diag::note_availability_specified_here)
7233 << OffendingDecl << /* partial */ 3;
7234
7235 if (const auto *Enclosing = findEnclosingDeclToAnnotate(Ctx)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007236 if (const auto *TD = dyn_cast<TagDecl>(Enclosing))
Erik Pilkington8b352c42017-08-14 19:49:12 +00007237 if (TD->getDeclName().isEmpty()) {
7238 S.Diag(TD->getLocation(),
7239 diag::note_decl_unguarded_availability_silence)
7240 << /*Anonymous*/ 1 << TD->getKindName();
7241 return;
7242 }
7243 auto FixitNoteDiag =
7244 S.Diag(Enclosing->getLocation(),
7245 diag::note_decl_unguarded_availability_silence)
7246 << /*Named*/ 0 << Enclosing;
7247 // Don't offer a fixit for declarations with availability attributes.
7248 if (Enclosing->hasAttr<AvailabilityAttr>())
7249 return;
7250 if (!S.getPreprocessor().isMacroDefined("API_AVAILABLE"))
7251 return;
7252 Optional<AttributeInsertion> Insertion = createAttributeInsertion(
7253 Enclosing, S.getSourceManager(), S.getLangOpts());
7254 if (!Insertion)
7255 return;
7256 std::string PlatformName =
7257 AvailabilityAttr::getPlatformNameSourceSpelling(
7258 S.getASTContext().getTargetInfo().getPlatformName())
7259 .lower();
7260 std::string Introduced =
7261 OffendingDecl->getVersionIntroduced().getAsString();
7262 FixitNoteDiag << FixItHint::CreateInsertion(
7263 Insertion->Loc,
7264 (llvm::Twine(Insertion->Prefix) + "API_AVAILABLE(" + PlatformName +
7265 "(" + Introduced + "))" + Insertion->Suffix)
7266 .str());
7267 }
7268 return;
7269 }
Erik Pilkington796a3e22016-08-05 22:59:03 +00007270 case AR_Deprecated:
Aaron Ballmanfb237522014-10-15 15:37:51 +00007271 diag = !ObjCPropertyAccess ? diag::warn_deprecated
7272 : diag::warn_property_method_deprecated;
7273 diag_message = diag::warn_deprecated_message;
7274 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
7275 property_note_select = /* deprecated */ 0;
7276 available_here_select_kind = /* deprecated */ 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007277 if (const auto *AL = OffendingDecl->getAttr<DeprecatedAttr>())
7278 NoteLocation = AL->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00007279 break;
7280
Erik Pilkington796a3e22016-08-05 22:59:03 +00007281 case AR_Unavailable:
Aaron Ballmanfb237522014-10-15 15:37:51 +00007282 diag = !ObjCPropertyAccess ? diag::err_unavailable
7283 : diag::err_property_method_unavailable;
7284 diag_message = diag::err_unavailable_message;
7285 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
7286 property_note_select = /* unavailable */ 1;
7287 available_here_select_kind = /* unavailable */ 0;
John McCallb61e14e2015-10-27 04:54:50 +00007288
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007289 if (auto AL = OffendingDecl->getAttr<UnavailableAttr>()) {
7290 if (AL->isImplicit() && AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00007291 // Most of these failures are due to extra restrictions in ARC;
7292 // reflect that in the primary diagnostic when applicable.
7293 auto flagARCError = [&] {
7294 if (S.getLangOpts().ObjCAutoRefCount &&
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007295 S.getSourceManager().isInSystemHeader(
7296 OffendingDecl->getLocation()))
John McCallc6af8c62015-10-28 05:03:19 +00007297 diag = diag::err_unavailable_in_arc;
7298 };
7299
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007300 switch (AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00007301 case UnavailableAttr::IR_None: break;
7302
7303 case UnavailableAttr::IR_ARCForbiddenType:
7304 flagARCError();
7305 diag_available_here = diag::note_arc_forbidden_type;
7306 break;
7307
7308 case UnavailableAttr::IR_ForbiddenWeak:
7309 if (S.getLangOpts().ObjCWeakRuntime)
7310 diag_available_here = diag::note_arc_weak_disabled;
7311 else
7312 diag_available_here = diag::note_arc_weak_no_runtime;
7313 break;
7314
7315 case UnavailableAttr::IR_ARCForbiddenConversion:
7316 flagARCError();
7317 diag_available_here = diag::note_performs_forbidden_arc_conversion;
7318 break;
7319
7320 case UnavailableAttr::IR_ARCInitReturnsUnrelated:
7321 flagARCError();
7322 diag_available_here = diag::note_arc_init_returns_unrelated;
7323 break;
7324
7325 case UnavailableAttr::IR_ARCFieldWithOwnership:
7326 flagARCError();
7327 diag_available_here = diag::note_arc_field_with_ownership;
7328 break;
7329 }
7330 }
John McCallb61e14e2015-10-27 04:54:50 +00007331 }
Aaron Ballmanfb237522014-10-15 15:37:51 +00007332 break;
7333
Erik Pilkington796a3e22016-08-05 22:59:03 +00007334 case AR_Available:
7335 llvm_unreachable("Warning for availability of available declaration?");
Aaron Ballmanfb237522014-10-15 15:37:51 +00007336 }
7337
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007338 SmallVector<FixItHint, 12> FixIts;
Erik Pilkington796a3e22016-08-05 22:59:03 +00007339 if (K == AR_Deprecated) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007340 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007341 if (auto AL = OffendingDecl->getAttr<DeprecatedAttr>())
7342 Replacement = AL->getReplacement();
7343 if (auto AL = getAttrForPlatform(S.Context, OffendingDecl))
7344 Replacement = AL->getReplacement();
Manman Renc7890fe2016-03-16 18:50:49 +00007345
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007346 CharSourceRange UseRange;
Manman Renc7890fe2016-03-16 18:50:49 +00007347 if (!Replacement.empty())
7348 UseRange =
7349 CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc));
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007350 if (UseRange.isValid()) {
7351 if (const auto *MethodDecl = dyn_cast<ObjCMethodDecl>(ReferringDecl)) {
7352 Selector Sel = MethodDecl->getSelector();
7353 SmallVector<StringRef, 12> SelectorSlotNames;
7354 Optional<unsigned> NumParams = tryParseObjCMethodName(
7355 Replacement, SelectorSlotNames, S.getLangOpts());
7356 if (NumParams && NumParams.getValue() == Sel.getNumArgs()) {
7357 assert(SelectorSlotNames.size() == Locs.size());
7358 for (unsigned I = 0; I < Locs.size(); ++I) {
7359 if (!Sel.getNameForSlot(I).empty()) {
7360 CharSourceRange NameRange = CharSourceRange::getCharRange(
7361 Locs[I], S.getLocForEndOfToken(Locs[I]));
7362 FixIts.push_back(FixItHint::CreateReplacement(
7363 NameRange, SelectorSlotNames[I]));
7364 } else
7365 FixIts.push_back(
7366 FixItHint::CreateInsertion(Locs[I], SelectorSlotNames[I]));
7367 }
7368 } else
7369 FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement));
7370 } else
7371 FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement));
7372 }
Manman Renc7890fe2016-03-16 18:50:49 +00007373 }
7374
Aaron Ballmanfb237522014-10-15 15:37:51 +00007375 if (!Message.empty()) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007376 S.Diag(Loc, diag_message) << ReferringDecl << Message << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007377 if (ObjCProperty)
7378 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
7379 << ObjCProperty->getDeclName() << property_note_select;
7380 } else if (!UnknownObjCClass) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007381 S.Diag(Loc, diag) << ReferringDecl << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007382 if (ObjCProperty)
7383 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
7384 << ObjCProperty->getDeclName() << property_note_select;
7385 } else {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007386 S.Diag(Loc, diag_fwdclass_message) << ReferringDecl << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007387 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
7388 }
7389
Erik Pilkington8b352c42017-08-14 19:49:12 +00007390 S.Diag(NoteLocation, diag_available_here)
7391 << OffendingDecl << available_here_select_kind;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007392}
7393
7394static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
7395 Decl *Ctx) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00007396 assert(DD.Kind == DelayedDiagnostic::Availability &&
7397 "Expected an availability diagnostic here");
7398
Aaron Ballmanfb237522014-10-15 15:37:51 +00007399 DD.Triggered = true;
Nico Weber0055a192015-03-19 19:18:22 +00007400 DoEmitAvailabilityWarning(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007401 S, DD.getAvailabilityResult(), Ctx, DD.getAvailabilityReferringDecl(),
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007402 DD.getAvailabilityOffendingDecl(), DD.getAvailabilityMessage(),
7403 DD.getAvailabilitySelectorLocs(), DD.getUnknownObjCClass(),
7404 DD.getObjCProperty(), false);
Aaron Ballmanfb237522014-10-15 15:37:51 +00007405}
7406
John McCall2ec85372012-05-07 06:16:41 +00007407void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
7408 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00007409 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00007410 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00007411
John McCall2ec85372012-05-07 06:16:41 +00007412 // When delaying diagnostics to run in the context of a parsed
7413 // declaration, we only want to actually emit anything if parsing
7414 // succeeds.
7415 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00007416
John McCall2ec85372012-05-07 06:16:41 +00007417 // We emit all the active diagnostics in this pool or any of its
7418 // parents. In general, we'll get one pool for the decl spec
7419 // and a child pool for each declarator; in a decl group like:
7420 // deprecated_typedef foo, *bar, baz();
7421 // only the declarator pops will be passed decls. This is correct;
7422 // we really do need to consider delayed diagnostics from the decl spec
7423 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00007424 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00007425 do {
John McCall6347b682012-05-07 06:16:58 +00007426 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00007427 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
7428 // This const_cast is a bit lame. Really, Triggered should be mutable.
7429 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00007430 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00007431 continue;
7432
John McCallc1465822011-02-14 07:13:47 +00007433 switch (diag.Kind) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00007434 case DelayedDiagnostic::Availability:
Ted Kremenekb79ee572013-12-18 23:30:06 +00007435 // Don't bother giving deprecation/unavailable diagnostics if
7436 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00007437 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00007438 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00007439 break;
7440
7441 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00007442 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00007443 break;
John McCall31168b02011-06-15 23:02:42 +00007444
7445 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00007446 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00007447 break;
John McCall86121512010-01-27 03:50:35 +00007448 }
7449 }
John McCall2ec85372012-05-07 06:16:41 +00007450 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00007451}
7452
John McCall6347b682012-05-07 06:16:58 +00007453/// Given a set of delayed diagnostics, re-emit them as if they had
7454/// been delayed in the current context instead of in the given pool.
7455/// Essentially, this just moves them to the current pool.
7456void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
7457 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
7458 assert(curPool && "re-emitting in undelayed context not supported");
7459 curPool->steal(pool);
7460}
7461
Erik Pilkington9f866a72017-07-18 20:32:07 +00007462static void EmitAvailabilityWarning(Sema &S, AvailabilityResult AR,
7463 const NamedDecl *ReferringDecl,
7464 const NamedDecl *OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007465 StringRef Message,
7466 ArrayRef<SourceLocation> Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00007467 const ObjCInterfaceDecl *UnknownObjCClass,
7468 const ObjCPropertyDecl *ObjCProperty,
7469 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00007470 // Delay if we're currently parsing a declaration.
Erik Pilkington9f866a72017-07-18 20:32:07 +00007471 if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
7472 S.DelayedDiagnostics.add(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007473 DelayedDiagnostic::makeAvailability(
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007474 AR, Locs, ReferringDecl, OffendingDecl, UnknownObjCClass,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007475 ObjCProperty, Message, ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00007476 return;
7477 }
7478
Erik Pilkington9f866a72017-07-18 20:32:07 +00007479 Decl *Ctx = cast<Decl>(S.getCurLexicalContext());
7480 DoEmitAvailabilityWarning(S, AR, Ctx, ReferringDecl, OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007481 Message, Locs, UnknownObjCClass, ObjCProperty,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007482 ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00007483}
Erik Pilkington48c7cc92016-07-29 17:37:38 +00007484
Erik Pilkington5cd57172016-08-16 17:44:11 +00007485namespace {
7486
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007487/// Returns true if the given statement can be a body-like child of \p Parent.
7488bool isBodyLikeChildStmt(const Stmt *S, const Stmt *Parent) {
7489 switch (Parent->getStmtClass()) {
7490 case Stmt::IfStmtClass:
7491 return cast<IfStmt>(Parent)->getThen() == S ||
7492 cast<IfStmt>(Parent)->getElse() == S;
7493 case Stmt::WhileStmtClass:
7494 return cast<WhileStmt>(Parent)->getBody() == S;
7495 case Stmt::DoStmtClass:
7496 return cast<DoStmt>(Parent)->getBody() == S;
7497 case Stmt::ForStmtClass:
7498 return cast<ForStmt>(Parent)->getBody() == S;
7499 case Stmt::CXXForRangeStmtClass:
7500 return cast<CXXForRangeStmt>(Parent)->getBody() == S;
7501 case Stmt::ObjCForCollectionStmtClass:
7502 return cast<ObjCForCollectionStmt>(Parent)->getBody() == S;
7503 case Stmt::CaseStmtClass:
7504 case Stmt::DefaultStmtClass:
7505 return cast<SwitchCase>(Parent)->getSubStmt() == S;
7506 default:
7507 return false;
7508 }
7509}
7510
7511class StmtUSEFinder : public RecursiveASTVisitor<StmtUSEFinder> {
7512 const Stmt *Target;
7513
7514public:
7515 bool VisitStmt(Stmt *S) { return S != Target; }
7516
7517 /// Returns true if the given statement is present in the given declaration.
7518 static bool isContained(const Stmt *Target, const Decl *D) {
7519 StmtUSEFinder Visitor;
7520 Visitor.Target = Target;
7521 return !Visitor.TraverseDecl(const_cast<Decl *>(D));
7522 }
7523};
7524
7525/// Traverses the AST and finds the last statement that used a given
7526/// declaration.
7527class LastDeclUSEFinder : public RecursiveASTVisitor<LastDeclUSEFinder> {
7528 const Decl *D;
7529
7530public:
7531 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
7532 if (DRE->getDecl() == D)
7533 return false;
7534 return true;
7535 }
7536
7537 static const Stmt *findLastStmtThatUsesDecl(const Decl *D,
7538 const CompoundStmt *Scope) {
7539 LastDeclUSEFinder Visitor;
7540 Visitor.D = D;
7541 for (auto I = Scope->body_rbegin(), E = Scope->body_rend(); I != E; ++I) {
7542 const Stmt *S = *I;
7543 if (!Visitor.TraverseStmt(const_cast<Stmt *>(S)))
7544 return S;
7545 }
7546 return nullptr;
7547 }
7548};
7549
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007550/// This class implements -Wunguarded-availability.
Erik Pilkington5cd57172016-08-16 17:44:11 +00007551///
7552/// This is done with a traversal of the AST of a function that makes reference
7553/// to a partially available declaration. Whenever we encounter an \c if of the
7554/// form: \c if(@available(...)), we use the version from the condition to visit
7555/// the then statement.
7556class DiagnoseUnguardedAvailability
7557 : public RecursiveASTVisitor<DiagnoseUnguardedAvailability> {
7558 typedef RecursiveASTVisitor<DiagnoseUnguardedAvailability> Base;
7559
7560 Sema &SemaRef;
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007561 Decl *Ctx;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007562
7563 /// Stack of potentially nested 'if (@available(...))'s.
7564 SmallVector<VersionTuple, 8> AvailabilityStack;
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007565 SmallVector<const Stmt *, 16> StmtStack;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007566
7567 void DiagnoseDeclAvailability(NamedDecl *D, SourceRange Range);
7568
7569public:
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007570 DiagnoseUnguardedAvailability(Sema &SemaRef, Decl *Ctx)
7571 : SemaRef(SemaRef), Ctx(Ctx) {
7572 AvailabilityStack.push_back(
7573 SemaRef.Context.getTargetInfo().getPlatformMinVersion());
Erik Pilkington5cd57172016-08-16 17:44:11 +00007574 }
7575
Alex Lorenz6f911122017-05-16 13:58:53 +00007576 bool TraverseDecl(Decl *D) {
7577 // Avoid visiting nested functions to prevent duplicate warnings.
7578 if (!D || isa<FunctionDecl>(D))
7579 return true;
7580 return Base::TraverseDecl(D);
7581 }
7582
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007583 bool TraverseStmt(Stmt *S) {
7584 if (!S)
7585 return true;
7586 StmtStack.push_back(S);
7587 bool Result = Base::TraverseStmt(S);
7588 StmtStack.pop_back();
7589 return Result;
7590 }
7591
Erik Pilkington5cd57172016-08-16 17:44:11 +00007592 void IssueDiagnostics(Stmt *S) { TraverseStmt(S); }
7593
7594 bool TraverseIfStmt(IfStmt *If);
7595
Alex Lorenz6f911122017-05-16 13:58:53 +00007596 bool TraverseLambdaExpr(LambdaExpr *E) { return true; }
7597
Erik Pilkingtonba87c622017-08-18 20:20:56 +00007598 // for 'case X:' statements, don't bother looking at the 'X'; it can't lead
7599 // to any useful diagnostics.
7600 bool TraverseCaseStmt(CaseStmt *CS) { return TraverseStmt(CS->getSubStmt()); }
7601
Erik Pilkington6ac77a62017-05-22 15:41:12 +00007602 bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *PRE) {
7603 if (PRE->isClassReceiver())
7604 DiagnoseDeclAvailability(PRE->getClassReceiver(), PRE->getReceiverLocation());
7605 return true;
7606 }
7607
Erik Pilkington5cd57172016-08-16 17:44:11 +00007608 bool VisitObjCMessageExpr(ObjCMessageExpr *Msg) {
7609 if (ObjCMethodDecl *D = Msg->getMethodDecl())
7610 DiagnoseDeclAvailability(
7611 D, SourceRange(Msg->getSelectorStartLoc(), Msg->getLocEnd()));
7612 return true;
7613 }
7614
7615 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
7616 DiagnoseDeclAvailability(DRE->getDecl(),
7617 SourceRange(DRE->getLocStart(), DRE->getLocEnd()));
7618 return true;
7619 }
7620
7621 bool VisitMemberExpr(MemberExpr *ME) {
7622 DiagnoseDeclAvailability(ME->getMemberDecl(),
7623 SourceRange(ME->getLocStart(), ME->getLocEnd()));
7624 return true;
7625 }
7626
Alex Lorenz0a484ba2017-05-24 15:15:29 +00007627 bool VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr *E) {
7628 SemaRef.Diag(E->getLocStart(), diag::warn_at_available_unchecked_use)
7629 << (!SemaRef.getLangOpts().ObjC1);
7630 return true;
7631 }
7632
Erik Pilkington5cd57172016-08-16 17:44:11 +00007633 bool VisitTypeLoc(TypeLoc Ty);
7634};
7635
7636void DiagnoseUnguardedAvailability::DiagnoseDeclAvailability(
7637 NamedDecl *D, SourceRange Range) {
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007638 AvailabilityResult Result;
7639 const NamedDecl *OffendingDecl;
7640 std::tie(Result, OffendingDecl) =
Erik Pilkington9f866a72017-07-18 20:32:07 +00007641 ShouldDiagnoseAvailabilityOfDecl(D, nullptr);
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007642 if (Result != AR_Available) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00007643 // All other diagnostic kinds have already been handled in
7644 // DiagnoseAvailabilityOfDecl.
7645 if (Result != AR_NotYetIntroduced)
7646 return;
7647
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007648 const AvailabilityAttr *AA =
7649 getAttrForPlatform(SemaRef.getASTContext(), OffendingDecl);
Erik Pilkington5cd57172016-08-16 17:44:11 +00007650 VersionTuple Introduced = AA->getIntroduced();
7651
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007652 if (AvailabilityStack.back() >= Introduced)
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007653 return;
7654
7655 // If the context of this function is less available than D, we should not
7656 // emit a diagnostic.
7657 if (!ShouldDiagnoseAvailabilityInContext(SemaRef, Result, Introduced, Ctx))
7658 return;
7659
Alex Lorenzc9a369f2017-06-22 17:02:24 +00007660 // We would like to emit the diagnostic even if -Wunguarded-availability is
7661 // not specified for deployment targets >= to iOS 11 or equivalent or
7662 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
7663 // later.
7664 unsigned DiagKind =
7665 shouldDiagnoseAvailabilityByDefault(
7666 SemaRef.Context,
7667 SemaRef.Context.getTargetInfo().getPlatformMinVersion(), Introduced)
7668 ? diag::warn_unguarded_availability_new
7669 : diag::warn_unguarded_availability;
7670
7671 SemaRef.Diag(Range.getBegin(), DiagKind)
Erik Pilkington5cd57172016-08-16 17:44:11 +00007672 << Range << D
7673 << AvailabilityAttr::getPrettyPlatformName(
7674 SemaRef.getASTContext().getTargetInfo().getPlatformName())
7675 << Introduced.getAsString();
7676
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007677 SemaRef.Diag(OffendingDecl->getLocation(),
7678 diag::note_availability_specified_here)
7679 << OffendingDecl << /* partial */ 3;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007680
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007681 auto FixitDiag =
7682 SemaRef.Diag(Range.getBegin(), diag::note_unguarded_available_silence)
7683 << Range << D
7684 << (SemaRef.getLangOpts().ObjC1 ? /*@available*/ 0
7685 : /*__builtin_available*/ 1);
7686
7687 // Find the statement which should be enclosed in the if @available check.
7688 if (StmtStack.empty())
7689 return;
7690 const Stmt *StmtOfUse = StmtStack.back();
7691 const CompoundStmt *Scope = nullptr;
7692 for (const Stmt *S : llvm::reverse(StmtStack)) {
7693 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
7694 Scope = CS;
7695 break;
7696 }
7697 if (isBodyLikeChildStmt(StmtOfUse, S)) {
7698 // The declaration won't be seen outside of the statement, so we don't
7699 // have to wrap the uses of any declared variables in if (@available).
7700 // Therefore we can avoid setting Scope here.
7701 break;
7702 }
7703 StmtOfUse = S;
7704 }
7705 const Stmt *LastStmtOfUse = nullptr;
7706 if (isa<DeclStmt>(StmtOfUse) && Scope) {
7707 for (const Decl *D : cast<DeclStmt>(StmtOfUse)->decls()) {
7708 if (StmtUSEFinder::isContained(StmtStack.back(), D)) {
7709 LastStmtOfUse = LastDeclUSEFinder::findLastStmtThatUsesDecl(D, Scope);
7710 break;
7711 }
7712 }
7713 }
7714
7715 const SourceManager &SM = SemaRef.getSourceManager();
7716 SourceLocation IfInsertionLoc =
7717 SM.getExpansionLoc(StmtOfUse->getLocStart());
7718 SourceLocation StmtEndLoc =
7719 SM.getExpansionRange(
7720 (LastStmtOfUse ? LastStmtOfUse : StmtOfUse)->getLocEnd())
Richard Smithb5f81712018-04-30 05:25:48 +00007721 .getEnd();
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007722 if (SM.getFileID(IfInsertionLoc) != SM.getFileID(StmtEndLoc))
7723 return;
7724
7725 StringRef Indentation = Lexer::getIndentationForLine(IfInsertionLoc, SM);
7726 const char *ExtraIndentation = " ";
7727 std::string FixItString;
7728 llvm::raw_string_ostream FixItOS(FixItString);
7729 FixItOS << "if (" << (SemaRef.getLangOpts().ObjC1 ? "@available"
7730 : "__builtin_available")
Alex Lorenze1fb64e2017-05-09 15:34:46 +00007731 << "("
7732 << AvailabilityAttr::getPlatformNameSourceSpelling(
7733 SemaRef.getASTContext().getTargetInfo().getPlatformName())
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007734 << " " << Introduced.getAsString() << ", *)) {\n"
7735 << Indentation << ExtraIndentation;
7736 FixitDiag << FixItHint::CreateInsertion(IfInsertionLoc, FixItOS.str());
7737 SourceLocation ElseInsertionLoc = Lexer::findLocationAfterToken(
7738 StmtEndLoc, tok::semi, SM, SemaRef.getLangOpts(),
7739 /*SkipTrailingWhitespaceAndNewLine=*/false);
7740 if (ElseInsertionLoc.isInvalid())
7741 ElseInsertionLoc =
7742 Lexer::getLocForEndOfToken(StmtEndLoc, 0, SM, SemaRef.getLangOpts());
7743 FixItOS.str().clear();
7744 FixItOS << "\n"
7745 << Indentation << "} else {\n"
7746 << Indentation << ExtraIndentation
7747 << "// Fallback on earlier versions\n"
7748 << Indentation << "}";
7749 FixitDiag << FixItHint::CreateInsertion(ElseInsertionLoc, FixItOS.str());
Erik Pilkington5cd57172016-08-16 17:44:11 +00007750 }
7751}
7752
7753bool DiagnoseUnguardedAvailability::VisitTypeLoc(TypeLoc Ty) {
7754 const Type *TyPtr = Ty.getTypePtr();
7755 SourceRange Range{Ty.getBeginLoc(), Ty.getEndLoc()};
7756
Erik Pilkington6ac77a62017-05-22 15:41:12 +00007757 if (Range.isInvalid())
7758 return true;
7759
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007760 if (const auto *TT = dyn_cast<TagType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00007761 TagDecl *TD = TT->getDecl();
7762 DiagnoseDeclAvailability(TD, Range);
7763
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007764 } else if (const auto *TD = dyn_cast<TypedefType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00007765 TypedefNameDecl *D = TD->getDecl();
7766 DiagnoseDeclAvailability(D, Range);
7767
7768 } else if (const auto *ObjCO = dyn_cast<ObjCObjectType>(TyPtr)) {
7769 if (NamedDecl *D = ObjCO->getInterface())
7770 DiagnoseDeclAvailability(D, Range);
7771 }
7772
7773 return true;
7774}
7775
7776bool DiagnoseUnguardedAvailability::TraverseIfStmt(IfStmt *If) {
7777 VersionTuple CondVersion;
7778 if (auto *E = dyn_cast<ObjCAvailabilityCheckExpr>(If->getCond())) {
7779 CondVersion = E->getVersion();
7780
7781 // If we're using the '*' case here or if this check is redundant, then we
7782 // use the enclosing version to check both branches.
7783 if (CondVersion.empty() || CondVersion <= AvailabilityStack.back())
Alex Lorenz98f9fcd2017-08-17 14:22:27 +00007784 return TraverseStmt(If->getThen()) && TraverseStmt(If->getElse());
Erik Pilkington5cd57172016-08-16 17:44:11 +00007785 } else {
7786 // This isn't an availability checking 'if', we can just continue.
7787 return Base::TraverseIfStmt(If);
7788 }
7789
7790 AvailabilityStack.push_back(CondVersion);
7791 bool ShouldContinue = TraverseStmt(If->getThen());
7792 AvailabilityStack.pop_back();
7793
7794 return ShouldContinue && TraverseStmt(If->getElse());
7795}
7796
7797} // end anonymous namespace
7798
7799void Sema::DiagnoseUnguardedAvailabilityViolations(Decl *D) {
7800 Stmt *Body = nullptr;
7801
7802 if (auto *FD = D->getAsFunction()) {
7803 // FIXME: We only examine the pattern decl for availability violations now,
7804 // but we should also examine instantiated templates.
7805 if (FD->isTemplateInstantiation())
7806 return;
7807
7808 Body = FD->getBody();
7809 } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
7810 Body = MD->getBody();
Alex Lorenz28559ce2017-04-26 14:20:02 +00007811 else if (auto *BD = dyn_cast<BlockDecl>(D))
7812 Body = BD->getBody();
Erik Pilkington5cd57172016-08-16 17:44:11 +00007813
7814 assert(Body && "Need a body here!");
7815
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007816 DiagnoseUnguardedAvailability(*this, D).IssueDiagnostics(Body);
Erik Pilkington5cd57172016-08-16 17:44:11 +00007817}
Erik Pilkington9f866a72017-07-18 20:32:07 +00007818
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007819void Sema::DiagnoseAvailabilityOfDecl(NamedDecl *D,
7820 ArrayRef<SourceLocation> Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00007821 const ObjCInterfaceDecl *UnknownObjCClass,
7822 bool ObjCPropertyAccess,
7823 bool AvoidPartialAvailabilityChecks) {
7824 std::string Message;
7825 AvailabilityResult Result;
7826 const NamedDecl* OffendingDecl;
7827 // See if this declaration is unavailable, deprecated, or partial.
7828 std::tie(Result, OffendingDecl) = ShouldDiagnoseAvailabilityOfDecl(D, &Message);
7829 if (Result == AR_Available)
7830 return;
7831
7832 if (Result == AR_NotYetIntroduced) {
7833 if (AvoidPartialAvailabilityChecks)
7834 return;
7835
7836 // We need to know the @available context in the current function to
7837 // diagnose this use, let DiagnoseUnguardedAvailabilityViolations do that
7838 // when we're done parsing the current function.
7839 if (getCurFunctionOrMethodDecl()) {
7840 getEnclosingFunction()->HasPotentialAvailabilityViolations = true;
7841 return;
7842 } else if (getCurBlock() || getCurLambda()) {
7843 getCurFunction()->HasPotentialAvailabilityViolations = true;
7844 return;
7845 }
7846 }
7847
7848 const ObjCPropertyDecl *ObjCPDecl = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007849 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007850 if (const ObjCPropertyDecl *PD = MD->findPropertyDecl()) {
7851 AvailabilityResult PDeclResult = PD->getAvailability(nullptr);
7852 if (PDeclResult == Result)
7853 ObjCPDecl = PD;
7854 }
7855 }
7856
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007857 EmitAvailabilityWarning(*this, Result, D, OffendingDecl, Message, Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00007858 UnknownObjCClass, ObjCPDecl, ObjCPropertyAccess);
7859}