blob: f549aa0468ba0ce66fb276f40dc6682d8f86d638 [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 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001054
1055 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();
1064
1065 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();
1069
1070 return false;
1071 }
1072 }
1073
1074 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;
DeLesley Hutchins48a31762013-08-12 21:20:55 +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;
1083
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;
1087
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 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001105
1106 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001107 }
1108
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;
1116
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 }
1132
1133 // 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 //}
1144
1145 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;
1152
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 }
1166
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();
1178 //
1179 //} else {
1180 //
1181 // 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;
1200
1201 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 }
1215
1216 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;
1224
Chris Wailes9385f9f2013-10-29 20:28:41 +00001225 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 }
1239
1240 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 Keanee891aa92018-07-13 15:07:47 +00001852static void handleCommonAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001853 if (S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001854 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
1855 << AL.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001856 return;
1857 }
1858
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001859 if (CommonAttr *CA = S.mergeCommonAttr(D, AL.getRange(), AL.getName(),
1860 AL.getAttributeSpellingListIndex()))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001861 D->addAttr(CA);
Eric Christopher8a2ee392010-12-02 02:45:55 +00001862}
1863
Erich Keanee891aa92018-07-13 15:07:47 +00001864static void handleNakedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001865 if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, AL.getRange(),
1866 AL.getName()))
Charles Davis0e379112016-08-08 21:19:08 +00001867 return;
1868
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001869 if (AL.isDeclspecAttribute()) {
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00001870 const auto &Triple = S.getASTContext().getTargetInfo().getTriple();
1871 const auto &Arch = Triple.getArch();
1872 if (Arch != llvm::Triple::x86 &&
1873 (Arch != llvm::Triple::arm && Arch != llvm::Triple::thumb)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001874 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_on_arch)
1875 << AL.getName() << Triple.getArchName();
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00001876 return;
1877 }
1878 }
1879
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001880 D->addAttr(::new (S.Context) NakedAttr(AL.getRange(), S.Context,
1881 AL.getAttributeSpellingListIndex()));
Charles Davis0e379112016-08-08 21:19:08 +00001882}
1883
Erich Keanee891aa92018-07-13 15:07:47 +00001884static void handleNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001885 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001886
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001887 if (!isa<ObjCMethodDecl>(D)) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00001888 S.Diag(Attrs.getLoc(), diag::warn_attribute_wrong_decl_type)
1889 << Attrs.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001890 return;
1891 }
1892
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001893 D->addAttr(::new (S.Context) NoReturnAttr(
Erich Keaneb11ebc52017-09-27 03:20:13 +00001894 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001895}
1896
Erich Keanee891aa92018-07-13 15:07:47 +00001897static void handleNoCfCheckAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
Oren Ben Simhon220671a2018-03-17 13:31:35 +00001898 if (!S.getLangOpts().CFProtectionBranch)
1899 S.Diag(Attrs.getLoc(), diag::warn_nocf_check_attribute_ignored);
1900 else
1901 handleSimpleAttribute<AnyX86NoCfCheckAttr>(S, D, Attrs);
John McCall3882ace2011-01-05 12:14:39 +00001902}
1903
Erich Keanee891aa92018-07-13 15:07:47 +00001904bool Sema::CheckAttrNoArgs(const ParsedAttr &Attrs) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00001905 if (!checkAttributeNumArgs(*this, Attrs, 0)) {
1906 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00001907 return true;
1908 }
1909
1910 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001911}
1912
Erich Keanee891aa92018-07-13 15:07:47 +00001913bool Sema::CheckAttrTarget(const ParsedAttr &AL) {
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001914 // Check whether the attribute is valid on the current target.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001915 if (!AL.existsInTarget(Context.getTargetInfo())) {
1916 Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) << AL.getName();
1917 AL.setInvalid();
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001918 return true;
1919 }
1920
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001921 return false;
1922}
1923
Erich Keanee891aa92018-07-13 15:07:47 +00001924static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1925
Ted Kremenek5295ce82010-08-19 00:51:58 +00001926 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1927 // because 'analyzer_noreturn' does not impact the type.
David Majnemer06864812015-04-07 06:01:53 +00001928 if (!isFunctionOrMethodOrBlock(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001929 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00001930 if (!VD || (!VD->getType()->isBlockPointerType() &&
1931 !VD->getType()->isFunctionPointerType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001932 S.Diag(AL.getLoc(),
1933 AL.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Craig Topper8f7f3ea2015-11-17 05:40:05 +00001934 : diag::warn_attribute_wrong_decl_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001935 << AL.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001936 return;
1937 }
1938 }
1939
Michael Han99315932013-01-24 16:46:58 +00001940 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001941 AnalyzerNoReturnAttr(AL.getRange(), S.Context,
1942 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001943}
1944
John Thompsoncdb847ba2010-08-09 21:53:52 +00001945// PS3 PPU-specific.
Erich Keanee891aa92018-07-13 15:07:47 +00001946static void handleVecReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1947 /*
1948 Returning a Vector Class in Registers
1949
1950 According to the PPU ABI specifications, a class with a single member of
1951 vector type is returned in memory when used as the return value of a
1952 function.
1953 This results in inefficient code when implementing vector classes. To return
1954 the value in a single vector register, add the vecreturn attribute to the
1955 class definition. This attribute is also applicable to struct types.
1956
1957 Example:
1958
1959 struct Vector
1960 {
1961 __vector float xyzw;
1962 } __attribute__((vecreturn));
1963
1964 Vector Add(Vector lhs, Vector rhs)
1965 {
1966 Vector result;
1967 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1968 return result; // This will be returned in a register
1969 }
1970 */
Aaron Ballman3e424b52013-12-26 18:30:57 +00001971 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001972 S.Diag(AL.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001973 return;
1974 }
1975
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001976 const auto *R = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001977 int count = 0;
1978
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001979 if (!isa<CXXRecordDecl>(R)) {
1980 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00001981 return;
1982 }
1983
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001984 if (!cast<CXXRecordDecl>(R)->isPOD()) {
1985 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
John Thompson9a587aaa2010-09-18 01:12:07 +00001986 return;
1987 }
1988
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001989 for (const auto *I : R->fields()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001990 if ((count == 1) || !I->getType()->isVectorType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001991 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00001992 return;
1993 }
1994 count++;
1995 }
1996
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001997 D->addAttr(::new (S.Context) VecReturnAttr(
1998 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001999}
2000
Richard Smithe233fbf2013-01-28 22:42:45 +00002001static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002002 const ParsedAttr &AL) {
Richard Smithe233fbf2013-01-28 22:42:45 +00002003 if (isa<ParmVarDecl>(D)) {
2004 // [[carries_dependency]] can only be applied to a parameter if it is a
2005 // parameter of a function declaration or lambda.
2006 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002007 S.Diag(AL.getLoc(),
Richard Smithe233fbf2013-01-28 22:42:45 +00002008 diag::err_carries_dependency_param_not_function_decl);
2009 return;
2010 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002011 }
Richard Smithe233fbf2013-01-28 22:42:45 +00002012
2013 D->addAttr(::new (S.Context) CarriesDependencyAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002014 AL.getRange(), S.Context,
2015 AL.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002016}
2017
Erich Keanee891aa92018-07-13 15:07:47 +00002018static void handleUnusedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002019 bool IsCXX17Attr = AL.isCXX11Attribute() && !AL.getScopeName();
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002020
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002021 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002022 // about using it as an extension.
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002023 if (!S.getLangOpts().CPlusPlus17 && IsCXX17Attr)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002024 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL.getName();
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002025
2026 D->addAttr(::new (S.Context) UnusedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002027 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002028}
2029
Erich Keanee891aa92018-07-13 15:07:47 +00002030static void handleConstructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002031 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002032 if (AL.getNumArgs() &&
2033 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002034 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002035
Michael Han99315932013-01-24 16:46:58 +00002036 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002037 ConstructorAttr(AL.getRange(), S.Context, priority,
2038 AL.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002039}
2040
Erich Keanee891aa92018-07-13 15:07:47 +00002041static void handleDestructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00002042 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002043 if (AL.getNumArgs() &&
2044 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002045 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002046
Michael Han99315932013-01-24 16:46:58 +00002047 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002048 DestructorAttr(AL.getRange(), S.Context, priority,
2049 AL.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002050}
2051
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002052template <typename AttrTy>
Erich Keanee891aa92018-07-13 15:07:47 +00002053static void handleAttrWithMessage(Sema &S, Decl *D, const ParsedAttr &AL) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002054 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002055 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002056 if (AL.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002057 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002058
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002059 D->addAttr(::new (S.Context) AttrTy(AL.getRange(), S.Context, Str,
2060 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00002061}
2062
Ted Kremenek438f8db2014-02-22 01:06:05 +00002063static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002064 const ParsedAttr &AL) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00002065 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002066 S.Diag(AL.getLoc(), diag::err_objc_attr_protocol_requires_definition)
2067 << AL.getName() << AL.getRange();
Ted Kremenek27cfe102014-02-21 22:49:04 +00002068 return;
2069 }
2070
Ted Kremenek28eace62013-11-23 01:01:34 +00002071 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002072 ObjCExplicitProtocolImplAttr(AL.getRange(), S.Context,
2073 AL.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00002074}
2075
Jordy Rose740b0c22012-05-08 03:27:22 +00002076static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2077 IdentifierInfo *Platform,
2078 VersionTuple Introduced,
2079 VersionTuple Deprecated,
2080 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002081 StringRef PlatformName
2082 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2083 if (PlatformName.empty())
2084 PlatformName = Platform->getName();
2085
2086 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2087 // of these steps are needed).
2088 if (!Introduced.empty() && !Deprecated.empty() &&
2089 !(Introduced <= Deprecated)) {
2090 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2091 << 1 << PlatformName << Deprecated.getAsString()
2092 << 0 << Introduced.getAsString();
2093 return true;
2094 }
2095
2096 if (!Introduced.empty() && !Obsoleted.empty() &&
2097 !(Introduced <= Obsoleted)) {
2098 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2099 << 2 << PlatformName << Obsoleted.getAsString()
2100 << 0 << Introduced.getAsString();
2101 return true;
2102 }
2103
2104 if (!Deprecated.empty() && !Obsoleted.empty() &&
2105 !(Deprecated <= Obsoleted)) {
2106 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2107 << 2 << PlatformName << Obsoleted.getAsString()
2108 << 1 << Deprecated.getAsString();
2109 return true;
2110 }
2111
2112 return false;
2113}
2114
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002115/// Check whether the two versions match.
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002116///
2117/// If either version tuple is empty, then they are assumed to match. If
2118/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2119static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2120 bool BeforeIsOkay) {
2121 if (X.empty() || Y.empty())
2122 return true;
2123
2124 if (X == Y)
2125 return true;
2126
2127 if (BeforeIsOkay && X < Y)
2128 return true;
2129
2130 return false;
2131}
2132
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002133AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002134 IdentifierInfo *Platform,
Manman Ren719a8642016-05-06 21:04:01 +00002135 bool Implicit,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002136 VersionTuple Introduced,
2137 VersionTuple Deprecated,
2138 VersionTuple Obsoleted,
2139 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002140 StringRef Message,
Manman Rend8039df2016-02-22 04:47:24 +00002141 bool IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002142 StringRef Replacement,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002143 AvailabilityMergeKind AMK,
Michael Han99315932013-01-24 16:46:58 +00002144 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002145 VersionTuple MergedIntroduced = Introduced;
2146 VersionTuple MergedDeprecated = Deprecated;
2147 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002148 bool FoundAny = false;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002149 bool OverrideOrImpl = false;
2150 switch (AMK) {
2151 case AMK_None:
2152 case AMK_Redeclaration:
2153 OverrideOrImpl = false;
2154 break;
2155
2156 case AMK_Override:
2157 case AMK_ProtocolImplementation:
2158 OverrideOrImpl = true;
2159 break;
2160 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002161
Rafael Espindolac67f2232012-05-10 02:50:16 +00002162 if (D->hasAttrs()) {
2163 AttrVec &Attrs = D->getAttrs();
2164 for (unsigned i = 0, e = Attrs.size(); i != e;) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002165 const auto *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002166 if (!OldAA) {
2167 ++i;
2168 continue;
2169 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002170
Rafael Espindolac67f2232012-05-10 02:50:16 +00002171 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2172 if (OldPlatform != Platform) {
2173 ++i;
2174 continue;
2175 }
2176
Tim Northover7a73cc72015-10-30 16:30:49 +00002177 // If there is an existing availability attribute for this platform that
2178 // is explicit and the new one is implicit use the explicit one and
2179 // discard the new implicit attribute.
Manman Ren719a8642016-05-06 21:04:01 +00002180 if (!OldAA->isImplicit() && Implicit) {
Tim Northover7a73cc72015-10-30 16:30:49 +00002181 return nullptr;
2182 }
2183
2184 // If there is an existing attribute for this platform that is implicit
2185 // and the new attribute is explicit then erase the old one and
2186 // continue processing the attributes.
Manman Ren719a8642016-05-06 21:04:01 +00002187 if (!Implicit && OldAA->isImplicit()) {
Tim Northover7a73cc72015-10-30 16:30:49 +00002188 Attrs.erase(Attrs.begin() + i);
2189 --e;
2190 continue;
2191 }
2192
Rafael Espindolac67f2232012-05-10 02:50:16 +00002193 FoundAny = true;
2194 VersionTuple OldIntroduced = OldAA->getIntroduced();
2195 VersionTuple OldDeprecated = OldAA->getDeprecated();
2196 VersionTuple OldObsoleted = OldAA->getObsoleted();
2197 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002198
Douglas Gregord2a713e2015-09-30 21:27:42 +00002199 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
2200 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
2201 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002202 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregord2a713e2015-09-30 21:27:42 +00002203 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
2204 if (OverrideOrImpl) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002205 int Which = -1;
2206 VersionTuple FirstVersion;
2207 VersionTuple SecondVersion;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002208 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002209 Which = 0;
2210 FirstVersion = OldIntroduced;
2211 SecondVersion = Introduced;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002212 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002213 Which = 1;
2214 FirstVersion = Deprecated;
2215 SecondVersion = OldDeprecated;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002216 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002217 Which = 2;
2218 FirstVersion = Obsoleted;
2219 SecondVersion = OldObsoleted;
2220 }
2221
2222 if (Which == -1) {
2223 Diag(OldAA->getLocation(),
2224 diag::warn_mismatched_availability_override_unavail)
Douglas Gregord2a713e2015-09-30 21:27:42 +00002225 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2226 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002227 } else {
2228 Diag(OldAA->getLocation(),
2229 diag::warn_mismatched_availability_override)
2230 << Which
2231 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
Douglas Gregord2a713e2015-09-30 21:27:42 +00002232 << FirstVersion.getAsString() << SecondVersion.getAsString()
2233 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002234 }
Douglas Gregord2a713e2015-09-30 21:27:42 +00002235 if (AMK == AMK_Override)
2236 Diag(Range.getBegin(), diag::note_overridden_method);
2237 else
2238 Diag(Range.getBegin(), diag::note_protocol_method);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002239 } else {
2240 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2241 Diag(Range.getBegin(), diag::note_previous_attribute);
2242 }
2243
Rafael Espindolac67f2232012-05-10 02:50:16 +00002244 Attrs.erase(Attrs.begin() + i);
2245 --e;
2246 continue;
2247 }
2248
2249 VersionTuple MergedIntroduced2 = MergedIntroduced;
2250 VersionTuple MergedDeprecated2 = MergedDeprecated;
2251 VersionTuple MergedObsoleted2 = MergedObsoleted;
2252
2253 if (MergedIntroduced2.empty())
2254 MergedIntroduced2 = OldIntroduced;
2255 if (MergedDeprecated2.empty())
2256 MergedDeprecated2 = OldDeprecated;
2257 if (MergedObsoleted2.empty())
2258 MergedObsoleted2 = OldObsoleted;
2259
2260 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2261 MergedIntroduced2, MergedDeprecated2,
2262 MergedObsoleted2)) {
2263 Attrs.erase(Attrs.begin() + i);
2264 --e;
2265 continue;
2266 }
2267
2268 MergedIntroduced = MergedIntroduced2;
2269 MergedDeprecated = MergedDeprecated2;
2270 MergedObsoleted = MergedObsoleted2;
2271 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002272 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002273 }
2274
2275 if (FoundAny &&
2276 MergedIntroduced == Introduced &&
2277 MergedDeprecated == Deprecated &&
2278 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00002279 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002280
Douglas Gregord2a713e2015-09-30 21:27:42 +00002281 // Only create a new attribute if !OverrideOrImpl, but we want to do
Ted Kremenekb5445722013-04-06 00:34:27 +00002282 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002283 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002284 MergedDeprecated, MergedObsoleted) &&
Douglas Gregord2a713e2015-09-30 21:27:42 +00002285 !OverrideOrImpl) {
Manman Ren719a8642016-05-06 21:04:01 +00002286 auto *Avail = ::new (Context) AvailabilityAttr(Range, Context, Platform,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002287 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002288 Obsoleted, IsUnavailable, Message,
Manman Ren75bc6762016-03-21 17:30:55 +00002289 IsStrict, Replacement,
2290 AttrSpellingListIndex);
Manman Ren719a8642016-05-06 21:04:01 +00002291 Avail->setImplicit(Implicit);
2292 return Avail;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002293 }
Craig Topperc3ec1492014-05-26 06:22:03 +00002294 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002295}
2296
Erich Keanee891aa92018-07-13 15:07:47 +00002297static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002298 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00002299 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002300 IdentifierLoc *Platform = AL.getArgAsIdent(0);
2301 unsigned Index = AL.getAttributeSpellingListIndex();
Michael Han99315932013-01-24 16:46:58 +00002302
Aaron Ballman00e99962013-08-31 01:11:41 +00002303 IdentifierInfo *II = Platform->Ident;
2304 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2305 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2306 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002307
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002308 auto *ND = dyn_cast<NamedDecl>(D);
Alex Lorenz472cc792017-04-20 09:35:02 +00002309 if (!ND) // We warned about this already, so just return.
Rafael Espindolac231fab2013-01-08 21:30:32 +00002310 return;
Rafael Espindolac231fab2013-01-08 21:30:32 +00002311
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002312 AvailabilityChange Introduced = AL.getAvailabilityIntroduced();
2313 AvailabilityChange Deprecated = AL.getAvailabilityDeprecated();
2314 AvailabilityChange Obsoleted = AL.getAvailabilityObsoleted();
2315 bool IsUnavailable = AL.getUnavailableLoc().isValid();
2316 bool IsStrict = AL.getStrictLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002317 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002318 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002319 Str = SE->getString();
Manman Ren75bc6762016-03-21 17:30:55 +00002320 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002321 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getReplacementExpr()))
Manman Ren75bc6762016-03-21 17:30:55 +00002322 Replacement = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002323
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002324 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, AL.getRange(), II,
Manman Ren719a8642016-05-06 21:04:01 +00002325 false/*Implicit*/,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002326 Introduced.Version,
2327 Deprecated.Version,
2328 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002329 IsUnavailable, Str,
Manman Ren75bc6762016-03-21 17:30:55 +00002330 IsStrict, Replacement,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002331 Sema::AMK_None,
Michael Han99315932013-01-24 16:46:58 +00002332 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002333 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002334 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002335
2336 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2337 // matches before the start of the watchOS platform.
2338 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2339 IdentifierInfo *NewII = nullptr;
2340 if (II->getName() == "ios")
2341 NewII = &S.Context.Idents.get("watchos");
2342 else if (II->getName() == "ios_app_extension")
2343 NewII = &S.Context.Idents.get("watchos_app_extension");
2344
2345 if (NewII) {
2346 auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple {
2347 if (Version.empty())
2348 return Version;
2349 auto Major = Version.getMajor();
2350 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2351 if (NewMajor >= 2) {
2352 if (Version.getMinor().hasValue()) {
2353 if (Version.getSubminor().hasValue())
2354 return VersionTuple(NewMajor, Version.getMinor().getValue(),
2355 Version.getSubminor().getValue());
2356 else
2357 return VersionTuple(NewMajor, Version.getMinor().getValue());
2358 }
2359 }
2360
2361 return VersionTuple(2, 0);
2362 };
2363
2364 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2365 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2366 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2367
2368 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002369 AL.getRange(),
Tim Northover7a73cc72015-10-30 16:30:49 +00002370 NewII,
Manman Ren719a8642016-05-06 21:04:01 +00002371 true/*Implicit*/,
Tim Northover7a73cc72015-10-30 16:30:49 +00002372 NewIntroduced,
2373 NewDeprecated,
2374 NewObsoleted,
2375 IsUnavailable, Str,
Manman Rend8039df2016-02-22 04:47:24 +00002376 IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002377 Replacement,
Tim Northover7a73cc72015-10-30 16:30:49 +00002378 Sema::AMK_None,
2379 Index);
2380 if (NewAttr)
2381 D->addAttr(NewAttr);
2382 }
2383 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2384 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2385 // matches before the start of the tvOS platform.
2386 IdentifierInfo *NewII = nullptr;
2387 if (II->getName() == "ios")
2388 NewII = &S.Context.Idents.get("tvos");
2389 else if (II->getName() == "ios_app_extension")
2390 NewII = &S.Context.Idents.get("tvos_app_extension");
2391
2392 if (NewII) {
2393 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002394 AL.getRange(),
Tim Northover7a73cc72015-10-30 16:30:49 +00002395 NewII,
Manman Ren719a8642016-05-06 21:04:01 +00002396 true/*Implicit*/,
Tim Northover7a73cc72015-10-30 16:30:49 +00002397 Introduced.Version,
2398 Deprecated.Version,
2399 Obsoleted.Version,
2400 IsUnavailable, Str,
Manman Rend8039df2016-02-22 04:47:24 +00002401 IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002402 Replacement,
Tim Northover7a73cc72015-10-30 16:30:49 +00002403 Sema::AMK_None,
2404 Index);
2405 if (NewAttr)
2406 D->addAttr(NewAttr);
2407 }
2408 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002409}
2410
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002411static void handleExternalSourceSymbolAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00002412 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002413 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002414 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002415 assert(checkAttributeAtMostNumArgs(S, AL, 3) &&
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002416 "Invalid number of arguments in an external_source_symbol attribute");
2417
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002418 StringRef Language;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002419 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(0)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002420 Language = SE->getString();
2421 StringRef DefinedIn;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002422 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(1)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002423 DefinedIn = SE->getString();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002424 bool IsGeneratedDeclaration = AL.getArgAsIdent(2) != nullptr;
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002425
2426 D->addAttr(::new (S.Context) ExternalSourceSymbolAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002427 AL.getRange(), S.Context, Language, DefinedIn, IsGeneratedDeclaration,
2428 AL.getAttributeSpellingListIndex()));
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002429}
2430
John McCalld041a9b2013-02-20 01:54:26 +00002431template <class T>
2432static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2433 typename T::VisibilityType value,
2434 unsigned attrSpellingListIndex) {
2435 T *existingAttr = D->getAttr<T>();
2436 if (existingAttr) {
2437 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2438 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00002439 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00002440 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2441 S.Diag(range.getBegin(), diag::note_previous_attribute);
2442 D->dropAttr<T>();
2443 }
2444 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2445}
2446
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002447VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002448 VisibilityAttr::VisibilityType Vis,
2449 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002450 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2451 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002452}
2453
John McCalld041a9b2013-02-20 01:54:26 +00002454TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2455 TypeVisibilityAttr::VisibilityType Vis,
2456 unsigned AttrSpellingListIndex) {
2457 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2458 AttrSpellingListIndex);
2459}
2460
Erich Keanee891aa92018-07-13 15:07:47 +00002461static void handleVisibilityAttr(Sema &S, Decl *D, const ParsedAttr &AL,
John McCalld041a9b2013-02-20 01:54:26 +00002462 bool isTypeVisibility) {
2463 // Visibility attributes don't mean anything on a typedef.
2464 if (isa<TypedefNameDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002465 S.Diag(AL.getRange().getBegin(), diag::warn_attribute_ignored)
2466 << AL.getName();
John McCalld041a9b2013-02-20 01:54:26 +00002467 return;
2468 }
2469
2470 // 'type_visibility' can only go on a type or namespace.
2471 if (isTypeVisibility &&
2472 !(isa<TagDecl>(D) ||
2473 isa<ObjCInterfaceDecl>(D) ||
2474 isa<NamespaceDecl>(D))) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002475 S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2476 << AL.getName() << ExpectedTypeOrNamespace;
John McCalld041a9b2013-02-20 01:54:26 +00002477 return;
2478 }
2479
Benjamin Kramer70370212013-09-09 15:08:57 +00002480 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002481 StringRef TypeStr;
2482 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002483 if (!S.checkStringLiteralArgumentAttr(AL, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002484 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002485
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002486 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002487 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002488 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002489 << AL.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002490 return;
2491 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002492
2493 // Complain about attempts to use protected visibility on targets
2494 // (like Darwin) that don't support it.
2495 if (type == VisibilityAttr::Protected &&
2496 !S.Context.getTargetInfo().hasProtectedVisibility()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002497 S.Diag(AL.getLoc(), diag::warn_attribute_protected_visibility);
Aaron Ballman682ee422013-09-11 19:47:58 +00002498 type = VisibilityAttr::Default;
2499 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002500
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002501 unsigned Index = AL.getAttributeSpellingListIndex();
2502 Attr *newAttr;
John McCalld041a9b2013-02-20 01:54:26 +00002503 if (isTypeVisibility) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002504 newAttr = S.mergeTypeVisibilityAttr(D, AL.getRange(),
John McCalld041a9b2013-02-20 01:54:26 +00002505 (TypeVisibilityAttr::VisibilityType) type,
2506 Index);
2507 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002508 newAttr = S.mergeVisibilityAttr(D, AL.getRange(), type, Index);
John McCalld041a9b2013-02-20 01:54:26 +00002509 }
2510 if (newAttr)
2511 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002512}
2513
Erich Keanee891aa92018-07-13 15:07:47 +00002514static void handleObjCMethodFamilyAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002515 const auto *M = cast<ObjCMethodDecl>(D);
2516 if (!AL.isArgIdent(0)) {
2517 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
2518 << AL.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002519 return;
2520 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002521
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002522 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00002523 ObjCMethodFamilyAttr::FamilyKind F;
2524 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002525 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
2526 << AL.getName() << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002527 return;
2528 }
2529
Alp Toker314cc812014-01-25 16:55:45 +00002530 if (F == ObjCMethodFamilyAttr::OMF_init &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002531 !M->getReturnType()->isObjCObjectPointerType()) {
2532 S.Diag(M->getLocation(), diag::err_init_method_bad_return_type)
2533 << M->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002534 // Ignore the attribute.
2535 return;
2536 }
2537
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002538 D->addAttr(new (S.Context) ObjCMethodFamilyAttr(
2539 AL.getRange(), S.Context, F, AL.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002540}
2541
Erich Keanee891aa92018-07-13 15:07:47 +00002542static void handleObjCNSObject(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002543 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002544 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002545 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002546 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2547 return;
2548 }
2549 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002550 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002551 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002552 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002553 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2554 return;
2555 }
2556 }
2557 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002558 // It is okay to include this attribute on properties, e.g.:
2559 //
2560 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2561 //
2562 // In this case it follows tradition and suppresses an error in the above
2563 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002564 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002565 }
Michael Han99315932013-01-24 16:46:58 +00002566 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002567 ObjCNSObjectAttr(AL.getRange(), S.Context,
2568 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002569}
2570
Erich Keanee891aa92018-07-13 15:07:47 +00002571static void handleObjCIndependentClass(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002572 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002573 QualType T = TD->getUnderlyingType();
2574 if (!T->isObjCObjectPointerType()) {
2575 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2576 return;
2577 }
2578 } else {
2579 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2580 return;
2581 }
2582 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002583 ObjCIndependentClassAttr(AL.getRange(), S.Context,
2584 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002585}
2586
Erich Keanee891aa92018-07-13 15:07:47 +00002587static void handleBlocksAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002588 if (!AL.isArgIdent(0)) {
2589 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
2590 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002591 return;
2592 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002593
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002594 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002595 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002596 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002597 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
2598 << AL.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002599 return;
2600 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002601
Michael Han99315932013-01-24 16:46:58 +00002602 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002603 BlocksAttr(AL.getRange(), S.Context, type,
2604 AL.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002605}
2606
Erich Keanee891aa92018-07-13 15:07:47 +00002607static void handleSentinelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002608 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002609 if (AL.getNumArgs() > 0) {
2610 Expr *E = AL.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002611 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002612 if (E->isTypeDependent() || E->isValueDependent() ||
2613 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002614 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
2615 << AL.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002616 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002617 return;
2618 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002619
John McCallb46f2872011-09-09 07:56:05 +00002620 if (Idx.isSigned() && Idx.isNegative()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002621 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_less_than_zero)
Chris Lattner3b054132008-11-19 05:08:23 +00002622 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002623 return;
2624 }
John McCallb46f2872011-09-09 07:56:05 +00002625
2626 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002627 }
2628
Aaron Ballman18a78382013-11-21 00:28:23 +00002629 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002630 if (AL.getNumArgs() > 1) {
2631 Expr *E = AL.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002632 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002633 if (E->isTypeDependent() || E->isValueDependent() ||
2634 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002635 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
2636 << AL.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002637 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002638 return;
2639 }
2640 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002641
John McCallb46f2872011-09-09 07:56:05 +00002642 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002643 // FIXME: This error message could be improved, it would be nice
2644 // to say what the bounds actually are.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002645 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
Chris Lattner3b054132008-11-19 05:08:23 +00002646 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002647 return;
2648 }
2649 }
2650
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002651 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002652 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002653 if (isa<FunctionNoProtoType>(FT)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002654 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_named_arguments);
Chris Lattner9363e312009-03-17 23:03:47 +00002655 return;
2656 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002657
Chris Lattner9363e312009-03-17 23:03:47 +00002658 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002659 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002660 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002661 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002662 } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002663 if (!MD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002664 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002665 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002666 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002667 } else if (const auto *BD = dyn_cast<BlockDecl>(D)) {
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002668 if (!BD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002669 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002670 return;
2671 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002672 } else if (const auto *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002673 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002674 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002675 const FunctionType *FT = Ty->isFunctionPointerType()
2676 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002677 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002678 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002679 int m = Ty->isFunctionPointerType() ? 0 : 1;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002680 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002681 return;
2682 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002683 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002684 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
2685 << AL.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002686 return;
2687 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002688 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002689 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
2690 << AL.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002691 return;
2692 }
Michael Han99315932013-01-24 16:46:58 +00002693 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002694 SentinelAttr(AL.getRange(), S.Context, sentinel, nullPos,
2695 AL.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002696}
2697
Erich Keanee891aa92018-07-13 15:07:47 +00002698static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL) {
Alp Toker314cc812014-01-25 16:55:45 +00002699 if (D->getFunctionType() &&
2700 D->getFunctionType()->getReturnType()->isVoidType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002701 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method)
2702 << AL.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002703 return;
2704 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002705 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002706 if (MD->getReturnType()->isVoidType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002707 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method)
2708 << AL.getName() << 1;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002709 return;
2710 }
2711
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002712 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
Aaron Ballmane7964782016-03-07 22:44:55 +00002713 // about using it as an extension.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002714 if (!S.getLangOpts().CPlusPlus17 && AL.isCXX11Attribute() &&
2715 !AL.getScopeName())
2716 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL.getName();
Aaron Ballmane7964782016-03-07 22:44:55 +00002717
Michael Han99315932013-01-24 16:46:58 +00002718 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002719 WarnUnusedResultAttr(AL.getRange(), S.Context,
2720 AL.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002721}
2722
Erich Keanee891aa92018-07-13 15:07:47 +00002723static void handleWeakImportAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002724 // weak_import only applies to variable & function declarations.
2725 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002726 if (!D->canBeWeakImported(isDef)) {
2727 if (isDef)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002728 S.Diag(AL.getLoc(), diag::warn_attribute_invalid_on_definition)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002729 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002730 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002731 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002732 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002733 // Nothing to warn about here.
2734 } else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002735 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
2736 << AL.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002737
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002738 return;
2739 }
2740
Michael Han99315932013-01-24 16:46:58 +00002741 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002742 WeakImportAttr(AL.getRange(), S.Context,
2743 AL.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002744}
2745
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002746// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002747template <typename WorkGroupAttr>
Erich Keanee891aa92018-07-13 15:07:47 +00002748static void handleWorkGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002749 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002750 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002751 const Expr *E = AL.getArgAsExpr(i);
2752 if (!checkUInt32Argument(S, AL, E, WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002753 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002754 if (WGSize[i] == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002755 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
2756 << AL.getName() << E->getSourceRange();
Joey Goulyb1d23a82014-05-19 14:41:38 +00002757 return;
2758 }
2759 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002760
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002761 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2762 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2763 Existing->getYDim() == WGSize[1] &&
2764 Existing->getZDim() == WGSize[2]))
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002765 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002766
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002767 D->addAttr(::new (S.Context) WorkGroupAttr(AL.getRange(), S.Context,
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002768 WGSize[0], WGSize[1], WGSize[2],
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002769 AL.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002770}
2771
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002772// Handles intel_reqd_sub_group_size.
Erich Keanee891aa92018-07-13 15:07:47 +00002773static void handleSubGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002774 uint32_t SGSize;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002775 const Expr *E = AL.getArgAsExpr(0);
2776 if (!checkUInt32Argument(S, AL, E, SGSize))
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002777 return;
2778 if (SGSize == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002779 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
2780 << AL.getName() << E->getSourceRange();
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002781 return;
2782 }
2783
2784 OpenCLIntelReqdSubGroupSizeAttr *Existing =
2785 D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>();
2786 if (Existing && Existing->getSubGroupSize() != SGSize)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002787 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL.getName();
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002788
2789 D->addAttr(::new (S.Context) OpenCLIntelReqdSubGroupSizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002790 AL.getRange(), S.Context, SGSize,
2791 AL.getAttributeSpellingListIndex()));
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002792}
2793
Erich Keanee891aa92018-07-13 15:07:47 +00002794static void handleVecTypeHint(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002795 if (!AL.hasParsedType()) {
2796 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
2797 << AL.getName() << 1;
Aaron Ballman00e99962013-08-31 01:11:41 +00002798 return;
2799 }
2800
Craig Topperc3ec1492014-05-26 06:22:03 +00002801 TypeSourceInfo *ParmTSI = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002802 QualType ParmType = S.GetTypeFromParser(AL.getTypeArg(), &ParmTSI);
Richard Smithb87c4652013-10-31 21:23:20 +00002803 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002804
2805 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2806 (ParmType->isBooleanType() ||
2807 !ParmType->isIntegralType(S.getASTContext()))) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002808 S.Diag(AL.getLoc(), diag::err_attribute_argument_vec_type_hint)
Joey Goulyaba589c2013-03-08 09:42:32 +00002809 << ParmType;
2810 return;
2811 }
2812
Aaron Ballmana9e05402013-12-02 22:16:55 +00002813 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002814 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002815 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL.getName();
Joey Goulyaba589c2013-03-08 09:42:32 +00002816 return;
2817 }
2818 }
2819
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002820 D->addAttr(::new (S.Context) VecTypeHintAttr(AL.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002821 ParmTSI,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002822 AL.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002823}
2824
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002825SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002826 StringRef Name,
2827 unsigned AttrSpellingListIndex) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002828 // Explicit or partial specializations do not inherit
2829 // the section attribute from the primary template.
2830 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2831 if (AttrSpellingListIndex == SectionAttr::Declspec_allocate &&
2832 FD->isFunctionTemplateSpecialization())
2833 return nullptr;
2834 }
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002835 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2836 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002837 return nullptr;
Erich Keane7963e8b2018-07-18 20:04:48 +00002838 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
2839 << 1 /*section*/;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002840 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002841 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002842 }
Michael Han99315932013-01-24 16:46:58 +00002843 return ::new (Context) SectionAttr(Range, Context, Name,
2844 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002845}
2846
Reid Kleckner2a133222015-03-04 23:39:17 +00002847bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2848 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2849 if (!Error.empty()) {
Erich Keane7963e8b2018-07-18 20:04:48 +00002850 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error
2851 << 1 /*'section'*/;
Reid Kleckner2a133222015-03-04 23:39:17 +00002852 return false;
2853 }
2854 return true;
2855}
2856
Erich Keanee891aa92018-07-13 15:07:47 +00002857static void handleSectionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002858 // Make sure that there is a string literal as the sections's single
2859 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002860 StringRef Str;
2861 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002862 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002863 return;
Mike Stump11289f42009-09-09 15:08:12 +00002864
Reid Kleckner2a133222015-03-04 23:39:17 +00002865 if (!S.checkSectionName(LiteralLoc, Str))
2866 return;
2867
Chris Lattner30ba6742009-08-10 19:03:04 +00002868 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002869 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002870 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002871 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002872 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002873 return;
2874 }
Mike Stump11289f42009-09-09 15:08:12 +00002875
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002876 unsigned Index = AL.getAttributeSpellingListIndex();
2877 SectionAttr *NewAttr = S.mergeSectionAttr(D, AL.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002878 if (NewAttr)
2879 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002880}
2881
Erich Keane7963e8b2018-07-18 20:04:48 +00002882static bool checkCodeSegName(Sema&S, SourceLocation LiteralLoc, StringRef CodeSegName) {
2883 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(CodeSegName);
2884 if (!Error.empty()) {
2885 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error
2886 << 0 /*'code-seg'*/;
2887 return false;
2888 }
2889 return true;
2890}
2891
2892CodeSegAttr *Sema::mergeCodeSegAttr(Decl *D, SourceRange Range,
2893 StringRef Name,
2894 unsigned AttrSpellingListIndex) {
2895 // Explicit or partial specializations do not inherit
2896 // the code_seg attribute from the primary template.
2897 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2898 if (FD->isFunctionTemplateSpecialization())
2899 return nullptr;
2900 }
2901 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
2902 if (ExistingAttr->getName() == Name)
2903 return nullptr;
2904 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
2905 << 0 /*codeseg*/;
2906 Diag(Range.getBegin(), diag::note_previous_attribute);
2907 return nullptr;
2908 }
2909 return ::new (Context) CodeSegAttr(Range, Context, Name,
2910 AttrSpellingListIndex);
2911}
2912
2913static void handleCodeSegAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2914 StringRef Str;
2915 SourceLocation LiteralLoc;
2916 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
2917 return;
2918 if (!checkCodeSegName(S, LiteralLoc, Str))
2919 return;
2920 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
2921 if (!ExistingAttr->isImplicit()) {
2922 S.Diag(AL.getLoc(),
2923 ExistingAttr->getName() == Str
2924 ? diag::warn_duplicate_codeseg_attribute
2925 : diag::err_conflicting_codeseg_attribute);
2926 return;
2927 }
2928 D->dropAttr<CodeSegAttr>();
2929 }
2930 if (CodeSegAttr *CSA = S.mergeCodeSegAttr(D, AL.getRange(), Str,
2931 AL.getAttributeSpellingListIndex()))
2932 D->addAttr(CSA);
2933}
2934
Erich Keane57e15cd2017-07-19 22:06:33 +00002935// Check for things we'd like to warn about. Multiversioning issues are
2936// handled later in the process, once we know how many exist.
2937bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
2938 enum FirstParam { Unsupported, Duplicate };
2939 enum SecondParam { None, Architecture };
Eric Christopher789a7ad2015-06-12 01:36:05 +00002940 for (auto Str : {"tune=", "fpmath="})
2941 if (AttrStr.find(Str) != StringRef::npos)
Erich Keane57e15cd2017-07-19 22:06:33 +00002942 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
2943 << Unsupported << None << Str;
2944
2945 TargetAttr::ParsedTargetAttr ParsedAttrs = TargetAttr::parse(AttrStr);
2946
2947 if (!ParsedAttrs.Architecture.empty() &&
2948 !Context.getTargetInfo().isValidCPUName(ParsedAttrs.Architecture))
2949 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
2950 << Unsupported << Architecture << ParsedAttrs.Architecture;
2951
2952 if (ParsedAttrs.DuplicateArchitecture)
2953 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
2954 << Duplicate << None << "arch=";
2955
2956 for (const auto &Feature : ParsedAttrs.Features) {
2957 auto CurFeature = StringRef(Feature).drop_front(); // remove + or -.
2958 if (!Context.getTargetInfo().isValidFeatureName(CurFeature))
2959 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
2960 << Unsupported << None << CurFeature;
2961 }
2962
Erich Keane29636aa2018-02-16 17:31:59 +00002963 return false;
Eric Christopher789a7ad2015-06-12 01:36:05 +00002964}
2965
Erich Keanee891aa92018-07-13 15:07:47 +00002966static void handleTargetAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Eric Christopher11acf732015-06-12 01:35:52 +00002967 StringRef Str;
2968 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002969 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc) ||
Erich Keane29636aa2018-02-16 17:31:59 +00002970 S.checkTargetAttr(LiteralLoc, Str))
Eric Christopher11acf732015-06-12 01:35:52 +00002971 return;
Erich Keane29636aa2018-02-16 17:31:59 +00002972
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002973 unsigned Index = AL.getAttributeSpellingListIndex();
Eric Christopher11acf732015-06-12 01:35:52 +00002974 TargetAttr *NewAttr =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002975 ::new (S.Context) TargetAttr(AL.getRange(), S.Context, Str, Index);
Eric Christopher11acf732015-06-12 01:35:52 +00002976 D->addAttr(NewAttr);
2977}
2978
Erich Keanee891aa92018-07-13 15:07:47 +00002979static void handleMinVectorWidthAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Craig Topper74c10e32018-07-09 19:00:16 +00002980 Expr *E = AL.getArgAsExpr(0);
2981 uint32_t VecWidth;
2982 if (!checkUInt32Argument(S, AL, E, VecWidth)) {
2983 AL.setInvalid();
2984 return;
2985 }
2986
2987 MinVectorWidthAttr *Existing = D->getAttr<MinVectorWidthAttr>();
2988 if (Existing && Existing->getVectorWidth() != VecWidth) {
2989 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL.getName();
2990 return;
2991 }
2992
2993 D->addAttr(::new (S.Context)
2994 MinVectorWidthAttr(AL.getRange(), S.Context, VecWidth,
2995 AL.getAttributeSpellingListIndex()));
2996}
2997
Erich Keanee891aa92018-07-13 15:07:47 +00002998static void handleCleanupAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002999 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003000 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00003001 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003002 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00003003
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003004 // gcc only allows for simple identifiers. Since we support more than gcc, we
3005 // will warn the user.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003006 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003007 if (DRE->hasQualifier())
3008 S.Diag(Loc, diag::warn_cleanup_ext);
3009 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
3010 NI = DRE->getNameInfo();
3011 if (!FD) {
3012 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
3013 << NI.getName();
3014 return;
3015 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003016 } else if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003017 if (ULE->hasExplicitTemplateArgs())
3018 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003019 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
3020 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00003021 if (!FD) {
3022 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
3023 << NI.getName();
3024 if (ULE->getType() == S.Context.OverloadTy)
3025 S.NoteAllOverloadCandidates(ULE);
3026 return;
3027 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003028 } else {
3029 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00003030 return;
3031 }
3032
Anders Carlssond277d792009-01-31 01:16:18 +00003033 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003034 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
3035 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00003036 return;
3037 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003038
Anders Carlsson723f55d2009-02-07 23:16:50 +00003039 // We're currently more strict than GCC about what function types we accept.
3040 // If this ever proves to be a problem it should be easy to fix.
Aaron Ballman3b70e752017-12-01 16:53:49 +00003041 QualType Ty = S.Context.getPointerType(cast<VarDecl>(D)->getType());
Anders Carlsson723f55d2009-02-07 23:16:50 +00003042 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00003043 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
3044 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003045 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
3046 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00003047 return;
3048 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003049
Michael Han99315932013-01-24 16:46:58 +00003050 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003051 CleanupAttr(AL.getRange(), S.Context, FD,
3052 AL.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00003053}
3054
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003055static void handleEnumExtensibilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00003056 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003057 if (!AL.isArgIdent(0)) {
3058 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
3059 << AL.getName() << 0 << AANT_ArgumentIdentifier;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003060 return;
3061 }
3062
3063 EnumExtensibilityAttr::Kind ExtensibilityKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003064 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003065 if (!EnumExtensibilityAttr::ConvertStrToKind(II->getName(),
3066 ExtensibilityKind)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003067 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
3068 << AL.getName() << II;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003069 return;
3070 }
3071
3072 D->addAttr(::new (S.Context) EnumExtensibilityAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003073 AL.getRange(), S.Context, ExtensibilityKind,
3074 AL.getAttributeSpellingListIndex()));
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003075}
3076
Mike Stumpd3bb5572009-07-24 19:02:52 +00003077/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00003078/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003079static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003080 Expr *IdxExpr = AL.getArgAsExpr(0);
Joel E. Denny81508102018-03-13 14:51:22 +00003081 ParamIdx Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003082 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003083 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00003084
Eric Christopherb64963e2015-08-13 21:34:35 +00003085 // Make sure the format string is really a string.
Joel E. Denny81508102018-03-13 14:51:22 +00003086 QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Mike Stumpd3bb5572009-07-24 19:02:52 +00003087
Eric Christopherb64963e2015-08-13 21:34:35 +00003088 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
3089 if (NotNSStringTy &&
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003090 !isCFStringType(Ty, S.Context) &&
3091 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003092 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003093 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003094 << "a string type" << IdxExpr->getSourceRange()
3095 << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003096 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003097 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003098 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003099 if (!isNSStringType(Ty, S.Context) &&
3100 !isCFStringType(Ty, S.Context) &&
3101 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003102 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003103 S.Diag(AL.getLoc(), diag::err_format_attribute_result_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003104 << (NotNSStringTy ? "string type" : "NSString")
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00003105 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003106 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003107 }
3108
Joel E. Denny81508102018-03-13 14:51:22 +00003109 D->addAttr(::new (S.Context) FormatArgAttr(
3110 AL.getRange(), S.Context, Idx, AL.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003111}
3112
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003113enum FormatAttrKind {
3114 CFStringFormat,
3115 NSStringFormat,
3116 StrftimeFormat,
3117 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003118 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003119 InvalidFormat
3120};
3121
3122/// getFormatAttrKind - Map from format attribute names to supported format
3123/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003124static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003125 return llvm::StringSwitch<FormatAttrKind>(Format)
Mehdi Amini06d367c2016-10-24 20:39:34 +00003126 // Check for formats that get handled specially.
3127 .Case("NSString", NSStringFormat)
3128 .Case("CFString", CFStringFormat)
3129 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003130
Mehdi Amini06d367c2016-10-24 20:39:34 +00003131 // Otherwise, check for supported formats.
3132 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3133 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3134 .Case("kprintf", SupportedFormat) // OpenBSD.
3135 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
3136 .Case("os_trace", SupportedFormat)
3137 .Case("os_log", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003138
Mehdi Amini06d367c2016-10-24 20:39:34 +00003139 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3140 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003141}
3142
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003143/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003144/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003145static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003146 if (!S.getLangOpts().CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003147 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL.getName();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003148 return;
3149 }
3150
Aaron Ballman4a611152013-11-27 16:34:09 +00003151 if (S.getCurFunctionOrMethodDecl()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003152 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3153 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003154 return;
3155 }
Aaron Ballman4a611152013-11-27 16:34:09 +00003156 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003157 if (S.Context.getAsArrayType(T))
3158 T = S.Context.getBaseElementType(T);
3159 if (!T->getAs<RecordType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003160 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3161 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003162 return;
3163 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003164
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003165 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003166 uint32_t prioritynum;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003167 if (!checkUInt32Argument(S, AL, E, prioritynum)) {
3168 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003169 return;
3170 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003171
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003172 if (prioritynum < 101 || prioritynum > 65535) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003173 S.Diag(AL.getLoc(), diag::err_attribute_argument_outof_range)
3174 << E->getSourceRange() << AL.getName() << 101 << 65535;
3175 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003176 return;
3177 }
Michael Han99315932013-01-24 16:46:58 +00003178 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003179 InitPriorityAttr(AL.getRange(), S.Context, prioritynum,
3180 AL.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003181}
3182
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003183FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3184 IdentifierInfo *Format, int FormatIdx,
3185 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00003186 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003187 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003188 for (auto *F : D->specific_attrs<FormatAttr>()) {
3189 if (F->getType() == Format &&
3190 F->getFormatIdx() == FormatIdx &&
3191 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003192 // If we don't have a valid location for this attribute, adopt the
3193 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003194 if (F->getLocation().isInvalid())
3195 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00003196 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00003197 }
3198 }
3199
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003200 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3201 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003202}
3203
Mike Stumpd3bb5572009-07-24 19:02:52 +00003204/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003205/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Erich Keanee891aa92018-07-13 15:07:47 +00003206static void handleFormatAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003207 if (!AL.isArgIdent(0)) {
3208 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
3209 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003210 return;
3211 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003212
Chandler Carruth743682b2010-11-16 08:35:43 +00003213 // In C++ the implicit 'this' function parameter also counts, and they are
3214 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003215 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00003216 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003217
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003218 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Aaron Ballman00e99962013-08-31 01:11:41 +00003219 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003220
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00003221 if (normalizeName(Format)) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003222 // If we've modified the string name, we need a new identifier for it.
3223 II = &S.Context.Idents.get(Format);
3224 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003225
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003226 // Check for supported formats.
3227 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00003228
3229 if (Kind == IgnoredFormat)
3230 return;
3231
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003232 if (Kind == InvalidFormat) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003233 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
3234 << AL.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003235 return;
3236 }
3237
3238 // checks for the 2nd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003239 Expr *IdxExpr = AL.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003240 uint32_t Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003241 if (!checkUInt32Argument(S, AL, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003242 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003243
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003244 if (Idx < 1 || Idx > NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003245 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3246 << AL.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003247 return;
3248 }
3249
3250 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003251 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003252
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003253 if (HasImplicitThisParam) {
3254 if (ArgIdx == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003255 S.Diag(AL.getLoc(),
Chandler Carruth743682b2010-11-16 08:35:43 +00003256 diag::err_format_attribute_implicit_this_format_string)
3257 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003258 return;
3259 }
3260 ArgIdx--;
3261 }
Mike Stump11289f42009-09-09 15:08:12 +00003262
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003263 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00003264 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003265
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003266 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003267 if (!isCFStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003268 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003269 << "a CFString" << IdxExpr->getSourceRange()
3270 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00003271 return;
3272 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003273 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003274 // FIXME: do we need to check if the type is NSString*? What are the
3275 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003276 if (!isNSStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003277 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003278 << "an NSString" << IdxExpr->getSourceRange()
3279 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003280 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003281 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003282 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003283 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003284 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003285 << "a string type" << IdxExpr->getSourceRange()
3286 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003287 return;
3288 }
3289
3290 // check the 3rd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003291 Expr *FirstArgExpr = AL.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003292 uint32_t FirstArg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003293 if (!checkUInt32Argument(S, AL, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003294 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003295
3296 // check if the function is variadic if the 3rd argument non-zero
3297 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003298 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003299 ++NumArgs; // +1 for ...
3300 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003301 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003302 return;
3303 }
3304 }
3305
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003306 // strftime requires FirstArg to be 0 because it doesn't read from any
3307 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003308 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003309 if (FirstArg != 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003310 S.Diag(AL.getLoc(), diag::err_format_strftime_third_parameter)
Chris Lattner3b054132008-11-19 05:08:23 +00003311 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003312 return;
3313 }
3314 // if 0 it disables parameter checking (to use with e.g. va_list)
3315 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003316 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3317 << AL.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003318 return;
3319 }
3320
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003321 FormatAttr *NewAttr = S.mergeFormatAttr(D, AL.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003322 Idx, FirstArg,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003323 AL.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003324 if (NewAttr)
3325 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003326}
3327
Erich Keanee891aa92018-07-13 15:07:47 +00003328static void handleTransparentUnionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003329 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00003330 RecordDecl *RD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003331 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003332 if (TD && TD->getUnderlyingType()->isUnionType())
3333 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3334 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003335 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003336
3337 if (!RD || !RD->isUnion()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003338 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
3339 << AL.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003340 return;
3341 }
3342
John McCallf937c022011-10-07 06:10:15 +00003343 if (!RD->isCompleteDefinition()) {
Erich Keane2fe684b2017-02-28 20:44:39 +00003344 if (!RD->isBeingDefined())
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003345 S.Diag(AL.getLoc(),
Erich Keane2fe684b2017-02-28 20:44:39 +00003346 diag::warn_transparent_union_attribute_not_definition);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003347 return;
3348 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003349
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003350 RecordDecl::field_iterator Field = RD->field_begin(),
3351 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003352 if (Field == FieldEnd) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003353 S.Diag(AL.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003354 return;
3355 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003356
David Blaikie40ed2972012-06-06 20:45:41 +00003357 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003358 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003359 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003360 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003361 diag::warn_transparent_union_attribute_floating)
3362 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003363 return;
3364 }
3365
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003366 if (FirstType->isIncompleteType())
3367 return;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003368 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3369 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3370 for (; Field != FieldEnd; ++Field) {
3371 QualType FieldType = Field->getType();
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003372 if (FieldType->isIncompleteType())
3373 return;
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003374 // FIXME: this isn't fully correct; we also need to test whether the
3375 // members of the union would all have the same calling convention as the
3376 // first member of the union. Checking just the size and alignment isn't
3377 // sufficient (consider structs passed on the stack instead of in registers
3378 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003379 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003380 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003381 // Warn if we drop the attribute.
3382 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003383 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003384 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003385 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003386 diag::warn_transparent_union_attribute_field_size_align)
3387 << isSize << Field->getDeclName() << FieldBits;
3388 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003389 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003390 diag::note_transparent_union_first_field_size_align)
3391 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003392 return;
3393 }
3394 }
3395
Michael Han99315932013-01-24 16:46:58 +00003396 RD->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003397 TransparentUnionAttr(AL.getRange(), S.Context,
3398 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003399}
3400
Erich Keanee891aa92018-07-13 15:07:47 +00003401static void handleAnnotateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003402 // Make sure that there is a string literal as the annotation's single
3403 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003404 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003405 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003406 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003407
3408 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003409 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
3410 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003411 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003412 }
Michael Han99315932013-01-24 16:46:58 +00003413
3414 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003415 AnnotateAttr(AL.getRange(), S.Context, Str,
3416 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003417}
3418
Erich Keanee891aa92018-07-13 15:07:47 +00003419static void handleAlignValueAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003420 S.AddAlignValueAttr(AL.getRange(), D, AL.getArgAsExpr(0),
3421 AL.getAttributeSpellingListIndex());
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003422}
3423
3424void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
3425 unsigned SpellingListIndex) {
3426 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
3427 SourceLocation AttrLoc = AttrRange.getBegin();
3428
3429 QualType T;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003430 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003431 T = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003432 else if (const auto *VD = dyn_cast<ValueDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003433 T = VD->getType();
3434 else
3435 llvm_unreachable("Unknown decl type for align_value");
3436
3437 if (!T->isDependentType() && !T->isAnyPointerType() &&
3438 !T->isReferenceType() && !T->isMemberPointerType()) {
3439 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
3440 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
3441 return;
3442 }
3443
3444 if (!E->isValueDependent()) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003445 llvm::APSInt Alignment;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003446 ExprResult ICE
3447 = VerifyIntegerConstantExpression(E, &Alignment,
3448 diag::err_align_value_attribute_argument_not_int,
3449 /*AllowFold*/ false);
3450 if (ICE.isInvalid())
3451 return;
3452
3453 if (!Alignment.isPowerOf2()) {
3454 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3455 << E->getSourceRange();
3456 return;
3457 }
3458
3459 D->addAttr(::new (Context)
3460 AlignValueAttr(AttrRange, Context, ICE.get(),
3461 SpellingListIndex));
3462 return;
3463 }
3464
3465 // Save dependent expressions in the AST to be instantiated.
3466 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003467}
3468
Erich Keanee891aa92018-07-13 15:07:47 +00003469static void handleAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003470 // check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003471 if (AL.getNumArgs() > 1) {
3472 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
3473 << AL.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003474 return;
3475 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003476
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003477 if (AL.getNumArgs() == 0) {
3478 D->addAttr(::new (S.Context) AlignedAttr(AL.getRange(), S.Context,
3479 true, nullptr, AL.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00003480 return;
3481 }
3482
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003483 Expr *E = AL.getArgAsExpr(0);
3484 if (AL.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3485 S.Diag(AL.getEllipsisLoc(),
Richard Smith44c247f2013-02-22 08:32:16 +00003486 diag::err_pack_expansion_without_parameter_packs);
3487 return;
3488 }
3489
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003490 if (!AL.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
Richard Smith44c247f2013-02-22 08:32:16 +00003491 return;
3492
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003493 S.AddAlignedAttr(AL.getRange(), D, E, AL.getAttributeSpellingListIndex(),
3494 AL.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003495}
3496
3497void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003498 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003499 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3500 SourceLocation AttrLoc = AttrRange.getBegin();
3501
Richard Smith1dba27c2013-01-29 09:02:09 +00003502 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003503 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003504 // C++11 [dcl.align]p1:
3505 // An alignment-specifier may be applied to a variable or to a class
3506 // data member, but it shall not be applied to a bit-field, a function
3507 // parameter, the formal parameter of a catch clause, or a variable
3508 // declared with the register storage class specifier. An
3509 // alignment-specifier may also be applied to the declaration of a class
3510 // or enumeration type.
3511 // C11 6.7.5/2:
3512 // An alignment attribute shall not be specified in a declaration of
3513 // a typedef, or a bit-field, or a function, or a parameter, or an
3514 // object declared with the register storage-class specifier.
3515 int DiagKind = -1;
3516 if (isa<ParmVarDecl>(D)) {
3517 DiagKind = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003518 } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003519 if (VD->getStorageClass() == SC_Register)
3520 DiagKind = 1;
3521 if (VD->isExceptionVariable())
3522 DiagKind = 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003523 } else if (const auto *FD = dyn_cast<FieldDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003524 if (FD->isBitField())
3525 DiagKind = 3;
3526 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00003527 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00003528 << (TmpAttr.isC11() ? ExpectedVariableOrField
3529 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003530 return;
3531 }
3532 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003533 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00003534 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003535 return;
3536 }
3537 }
3538
Richard Smith90ae9672018-06-20 23:36:55 +00003539 if (E->isValueDependent()) {
3540 // We can't support a dependent alignment on a non-dependent type,
3541 // because we have no way to model that a type is "alignment-dependent"
3542 // but not dependent in any other way.
3543 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3544 if (!TND->getUnderlyingType()->isDependentType()) {
3545 Diag(AttrLoc, diag::err_alignment_dependent_typedef_name)
3546 << E->getSourceRange();
3547 return;
3548 }
3549 }
3550
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003551 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003552 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3553 AA->setPackExpansion(IsPackExpansion);
3554 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003555 return;
3556 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003557
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003558 // FIXME: Cache the number on the AL object?
David Majnemer0be6bd02015-07-26 09:02:21 +00003559 llvm::APSInt Alignment;
Douglas Gregore2b37442012-05-04 22:38:52 +00003560 ExprResult ICE
3561 = VerifyIntegerConstantExpression(E, &Alignment,
3562 diag::err_aligned_attribute_argument_not_int,
3563 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003564 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003565 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003566
David Majnemer0be6bd02015-07-26 09:02:21 +00003567 uint64_t AlignVal = Alignment.getZExtValue();
3568
Richard Smith848e1f12013-02-01 08:12:08 +00003569 // C++11 [dcl.align]p2:
3570 // -- if the constant expression evaluates to zero, the alignment
3571 // specifier shall have no effect
3572 // C11 6.7.5p6:
3573 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003574 if (!(TmpAttr.isAlignas() && !Alignment)) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003575 if (!llvm::isPowerOf2_64(AlignVal)) {
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003576 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3577 << E->getSourceRange();
3578 return;
3579 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003580 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003581
David Majnemerabecae72014-02-12 20:36:10 +00003582 // Alignment calculations can wrap around if it's greater than 2**28.
David Majnemer29c69db2015-07-26 01:48:59 +00003583 unsigned MaxValidAlignment =
3584 Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192
3585 : 268435456;
David Majnemer0be6bd02015-07-26 09:02:21 +00003586 if (AlignVal > MaxValidAlignment) {
David Majnemerabecae72014-02-12 20:36:10 +00003587 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
3588 << E->getSourceRange();
3589 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003590 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003591
David Majnemer0be6bd02015-07-26 09:02:21 +00003592 if (Context.getTargetInfo().isTLSSupported()) {
3593 unsigned MaxTLSAlign =
3594 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3595 .getQuantity();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003596 const auto *VD = dyn_cast<VarDecl>(D);
David Majnemer0be6bd02015-07-26 09:02:21 +00003597 if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD &&
3598 VD->getTLSKind() != VarDecl::TLS_None) {
3599 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3600 << (unsigned)AlignVal << VD << MaxTLSAlign;
3601 return;
3602 }
3603 }
3604
Richard Smith44c247f2013-02-22 08:32:16 +00003605 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003606 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00003607 AA->setPackExpansion(IsPackExpansion);
3608 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003609}
3610
Michael Hanaf02bbe2013-02-01 01:19:17 +00003611void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003612 unsigned SpellingListIndex, bool IsPackExpansion) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003613 // FIXME: Cache the number on the AL object if non-dependent?
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003614 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003615 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3616 SpellingListIndex);
3617 AA->setPackExpansion(IsPackExpansion);
3618 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003619}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003620
Richard Smith848e1f12013-02-01 08:12:08 +00003621void Sema::CheckAlignasUnderalignment(Decl *D) {
3622 assert(D->hasAttrs() && "no attributes on decl");
3623
David Majnemer475b25e2015-01-21 10:54:38 +00003624 QualType UnderlyingTy, DiagTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003625 if (const auto *VD = dyn_cast<ValueDecl>(D)) {
David Majnemer475b25e2015-01-21 10:54:38 +00003626 UnderlyingTy = DiagTy = VD->getType();
3627 } else {
3628 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003629 if (const auto *ED = dyn_cast<EnumDecl>(D))
David Majnemer475b25e2015-01-21 10:54:38 +00003630 UnderlyingTy = ED->getIntegerType();
3631 }
3632 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003633 return;
3634
3635 // C++11 [dcl.align]p5, C11 6.7.5/4:
3636 // The combined effect of all alignment attributes in a declaration shall
3637 // not specify an alignment that is less strict than the alignment that
3638 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003639 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003640 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003641 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003642 if (I->isAlignmentDependent())
3643 return;
3644 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003645 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003646 Align = std::max(Align, I->getAlignment(Context));
3647 }
3648
3649 if (AlignasAttr && Align) {
3650 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003651 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003652 if (NaturalAlign > RequestedAlign)
3653 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003654 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003655 }
3656}
3657
David Majnemer2c4e00a2014-01-29 22:07:36 +00003658bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003659 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003660 MSInheritanceAttr::Spelling SemanticSpelling) {
3661 assert(RD->hasDefinition() && "RD has no definition!");
3662
David Majnemer98c9ee22014-02-07 00:43:07 +00003663 // We may not have seen base specifiers or any virtual methods yet. We will
3664 // have to wait until the record is defined to catch any mismatches.
3665 if (!RD->getDefinition()->isCompleteDefinition())
3666 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003667
David Majnemer98c9ee22014-02-07 00:43:07 +00003668 // The unspecified model never matches what a definition could need.
3669 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
3670 return false;
3671
David Majnemer4bb09802014-02-10 19:50:15 +00003672 if (BestCase) {
3673 if (RD->calculateInheritanceModel() == SemanticSpelling)
3674 return false;
3675 } else {
3676 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3677 return false;
3678 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003679
3680 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3681 << 0 /*definition*/;
3682 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3683 << RD->getNameAsString();
3684 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003685}
3686
Alexey Bataevf278eb12015-11-19 10:13:11 +00003687/// parseModeAttrArg - Parses attribute mode string and returns parsed type
3688/// attribute.
3689static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
3690 bool &IntegerMode, bool &ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003691 IntegerMode = true;
3692 ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003693 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003694 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003695 switch (Str[0]) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003696 case 'Q':
3697 DestWidth = 8;
3698 break;
3699 case 'H':
3700 DestWidth = 16;
3701 break;
3702 case 'S':
3703 DestWidth = 32;
3704 break;
3705 case 'D':
3706 DestWidth = 64;
3707 break;
3708 case 'X':
3709 DestWidth = 96;
3710 break;
3711 case 'T':
3712 DestWidth = 128;
3713 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003714 }
3715 if (Str[1] == 'F') {
3716 IntegerMode = false;
3717 } else if (Str[1] == 'C') {
3718 IntegerMode = false;
3719 ComplexMode = true;
3720 } else if (Str[1] != 'I') {
3721 DestWidth = 0;
3722 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003723 break;
3724 case 4:
3725 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3726 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003727 if (Str == "word")
Reid Klecknerf27e7522016-02-01 18:58:24 +00003728 DestWidth = S.Context.getTargetInfo().getRegisterWidth();
Daniel Dunbarafff4342009-10-18 02:09:24 +00003729 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003730 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003731 break;
3732 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003733 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003734 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003735 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003736 case 11:
3737 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003738 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003739 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003740 }
Alexey Bataevf278eb12015-11-19 10:13:11 +00003741}
3742
3743/// handleModeAttr - This attribute modifies the width of a decl with primitive
3744/// type.
3745///
3746/// Despite what would be logical, the mode attribute is a decl attribute, not a
3747/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3748/// HImode, not an intermediate pointer.
Erich Keanee891aa92018-07-13 15:07:47 +00003749static void handleModeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003750 // This attribute isn't documented, but glibc uses it. It changes
3751 // the width of an int or unsigned int to the specified size.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003752 if (!AL.isArgIdent(0)) {
3753 S.Diag(AL.getLoc(), diag::err_attribute_argument_type) << AL.getName()
Alexey Bataevf278eb12015-11-19 10:13:11 +00003754 << AANT_ArgumentIdentifier;
3755 return;
3756 }
3757
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003758 IdentifierInfo *Name = AL.getArgAsIdent(0)->Ident;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003759
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003760 S.AddModeAttr(AL.getRange(), D, Name, AL.getAttributeSpellingListIndex());
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003761}
3762
3763void Sema::AddModeAttr(SourceRange AttrRange, Decl *D, IdentifierInfo *Name,
3764 unsigned SpellingListIndex, bool InInstantiation) {
3765 StringRef Str = Name->getName();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003766 normalizeName(Str);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003767 SourceLocation AttrLoc = AttrRange.getBegin();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003768
3769 unsigned DestWidth = 0;
3770 bool IntegerMode = true;
3771 bool ComplexMode = false;
3772 llvm::APInt VectorSize(64, 0);
3773 if (Str.size() >= 4 && Str[0] == 'V') {
3774 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
3775 size_t StrSize = Str.size();
3776 size_t VectorStringLength = 0;
3777 while ((VectorStringLength + 1) < StrSize &&
3778 isdigit(Str[VectorStringLength + 1]))
3779 ++VectorStringLength;
3780 if (VectorStringLength &&
3781 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
3782 VectorSize.isPowerOf2()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003783 parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth,
Alexey Bataevf278eb12015-11-19 10:13:11 +00003784 IntegerMode, ComplexMode);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003785 // Avoid duplicate warning from template instantiation.
3786 if (!InInstantiation)
3787 Diag(AttrLoc, diag::warn_vector_mode_deprecated);
Alexey Bataevf278eb12015-11-19 10:13:11 +00003788 } else {
3789 VectorSize = 0;
3790 }
3791 }
3792
3793 if (!VectorSize)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003794 parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode);
3795
3796 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3797 // and friends, at least with glibc.
3798 // FIXME: Make sure floating-point mappings are accurate
3799 // FIXME: Support XF and TF types
3800 if (!DestWidth) {
3801 Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name;
3802 return;
3803 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003804
3805 QualType OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003806 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003807 OldTy = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003808 else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003809 // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'.
3810 // Try to get type from enum declaration, default to int.
3811 OldTy = ED->getIntegerType();
3812 if (OldTy.isNull())
3813 OldTy = Context.IntTy;
3814 } else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003815 OldTy = cast<ValueDecl>(D)->getType();
Eli Friedman4735374e2009-03-03 06:41:03 +00003816
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003817 if (OldTy->isDependentType()) {
3818 D->addAttr(::new (Context)
3819 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
3820 return;
3821 }
3822
Alexey Bataev326057d2015-06-19 07:46:21 +00003823 // Base type can also be a vector type (see PR17453).
3824 // Distinguish between base type and base element type.
3825 QualType OldElemTy = OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003826 if (const auto *VT = OldTy->getAs<VectorType>())
Alexey Bataev326057d2015-06-19 07:46:21 +00003827 OldElemTy = VT->getElementType();
3828
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003829 // GCC allows 'mode' attribute on enumeration types (even incomplete), except
3830 // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete
3831 // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected.
3832 if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) &&
3833 VectorSize.getBoolValue()) {
3834 Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << AttrRange;
3835 return;
3836 }
3837 bool IntegralOrAnyEnumType =
3838 OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>();
3839
3840 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() &&
3841 !IntegralOrAnyEnumType)
3842 Diag(AttrLoc, diag::err_mode_not_primitive);
Eli Friedman4735374e2009-03-03 06:41:03 +00003843 else if (IntegerMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003844 if (!IntegralOrAnyEnumType)
3845 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00003846 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003847 if (!OldElemTy->isComplexType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003848 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00003849 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00003850 if (!OldElemTy->isFloatingType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003851 Diag(AttrLoc, diag::err_mode_wrong_type);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003852 }
3853
Alexey Bataev326057d2015-06-19 07:46:21 +00003854 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003855
3856 if (IntegerMode)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003857 NewElemTy = Context.getIntTypeForBitwidth(DestWidth,
3858 OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003859 else
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003860 NewElemTy = Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003861
Alexey Bataev326057d2015-06-19 07:46:21 +00003862 if (NewElemTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003863 Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003864 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003865 }
3866
Eli Friedman4735374e2009-03-03 06:41:03 +00003867 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003868 NewElemTy = Context.getComplexType(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00003869 }
3870
3871 QualType NewTy = NewElemTy;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003872 if (VectorSize.getBoolValue()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003873 NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(),
3874 VectorType::GenericVector);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003875 } else if (const auto *OldVT = OldTy->getAs<VectorType>()) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003876 // Complex machine mode does not support base vector types.
3877 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003878 Diag(AttrLoc, diag::err_complex_mode_vector_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00003879 return;
3880 }
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003881 unsigned NumElements = Context.getTypeSize(OldElemTy) *
Alexey Bataev326057d2015-06-19 07:46:21 +00003882 OldVT->getNumElements() /
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003883 Context.getTypeSize(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00003884 NewTy =
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003885 Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
Alexey Bataev326057d2015-06-19 07:46:21 +00003886 }
3887
3888 if (NewTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003889 Diag(AttrLoc, diag::err_mode_wrong_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00003890 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003891 }
3892
3893 // Install the new type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003894 if (auto *TD = dyn_cast<TypedefNameDecl>(D))
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003895 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003896 else if (auto *ED = dyn_cast<EnumDecl>(D))
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003897 ED->setIntegerType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003898 else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003899 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003900
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003901 D->addAttr(::new (Context)
3902 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003903}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003904
Erich Keanee891aa92018-07-13 15:07:47 +00003905static void handleNoDebugAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Michael Han99315932013-01-24 16:46:58 +00003906 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003907 NoDebugAttr(AL.getRange(), S.Context,
3908 AL.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003909}
3910
Paul Robinson30e41fb2014-12-15 18:57:28 +00003911AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range,
Paul Robinson080b1f32015-01-13 18:34:56 +00003912 IdentifierInfo *Ident,
Paul Robinson30e41fb2014-12-15 18:57:28 +00003913 unsigned AttrSpellingListIndex) {
3914 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003915 Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00003916 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3917 return nullptr;
3918 }
3919
3920 if (D->hasAttr<AlwaysInlineAttr>())
3921 return nullptr;
3922
3923 return ::new (Context) AlwaysInlineAttr(Range, Context,
3924 AttrSpellingListIndex);
3925}
3926
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003927CommonAttr *Sema::mergeCommonAttr(Decl *D, SourceRange Range,
3928 IdentifierInfo *Ident,
3929 unsigned AttrSpellingListIndex) {
3930 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, Range, Ident))
3931 return nullptr;
3932
3933 return ::new (Context) CommonAttr(Range, Context, AttrSpellingListIndex);
3934}
3935
3936InternalLinkageAttr *
3937Sema::mergeInternalLinkageAttr(Decl *D, SourceRange Range,
3938 IdentifierInfo *Ident,
3939 unsigned AttrSpellingListIndex) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003940 if (const auto *VD = dyn_cast<VarDecl>(D)) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003941 // Attribute applies to Var but not any subclass of it (like ParmVar,
3942 // ImplicitParm or VarTemplateSpecialization).
3943 if (VD->getKind() != Decl::Var) {
3944 Diag(Range.getBegin(), diag::warn_attribute_wrong_decl_type)
3945 << Ident << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
3946 : ExpectedVariableOrFunction);
3947 return nullptr;
3948 }
3949 // Attribute does not apply to non-static local variables.
3950 if (VD->hasLocalStorage()) {
3951 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
3952 return nullptr;
3953 }
3954 }
3955
3956 if (checkAttrMutualExclusion<CommonAttr>(*this, D, Range, Ident))
3957 return nullptr;
3958
3959 return ::new (Context)
3960 InternalLinkageAttr(Range, Context, AttrSpellingListIndex);
3961}
3962
Paul Robinson30e41fb2014-12-15 18:57:28 +00003963MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range,
3964 unsigned AttrSpellingListIndex) {
3965 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
3966 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'";
3967 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3968 return nullptr;
3969 }
3970
3971 if (D->hasAttr<MinSizeAttr>())
3972 return nullptr;
3973
3974 return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex);
3975}
3976
3977OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range,
3978 unsigned AttrSpellingListIndex) {
3979 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
3980 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
3981 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3982 D->dropAttr<AlwaysInlineAttr>();
3983 }
3984 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
3985 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
3986 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3987 D->dropAttr<MinSizeAttr>();
3988 }
3989
3990 if (D->hasAttr<OptimizeNoneAttr>())
3991 return nullptr;
3992
3993 return ::new (Context) OptimizeNoneAttr(Range, Context,
3994 AttrSpellingListIndex);
3995}
3996
Erich Keanee891aa92018-07-13 15:07:47 +00003997static void handleAlwaysInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003998 if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, AL.getRange(),
3999 AL.getName()))
Akira Hatanakac8667622015-11-06 23:56:15 +00004000 return;
4001
Paul Robinson080b1f32015-01-13 18:34:56 +00004002 if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004003 D, AL.getRange(), AL.getName(),
4004 AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004005 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00004006}
4007
Erich Keanee891aa92018-07-13 15:07:47 +00004008static void handleMinSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004009 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004010 D, AL.getRange(), AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004011 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00004012}
4013
Erich Keanee891aa92018-07-13 15:07:47 +00004014static void handleOptimizeNoneAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004015 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004016 D, AL.getRange(), AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004017 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00004018}
4019
Erich Keanee891aa92018-07-13 15:07:47 +00004020static void handleConstantAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004021 if (checkAttrMutualExclusion<CUDASharedAttr>(S, D, AL.getRange(),
4022 AL.getName()))
Justin Lebare71b2fa2016-09-30 23:57:34 +00004023 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004024 const auto *VD = cast<VarDecl>(D);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004025 if (!VD->hasGlobalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004026 S.Diag(AL.getLoc(), diag::err_cuda_nonglobal_constant);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004027 return;
4028 }
4029 D->addAttr(::new (S.Context) CUDAConstantAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004030 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Justin Lebare71b2fa2016-09-30 23:57:34 +00004031}
4032
Erich Keanee891aa92018-07-13 15:07:47 +00004033static void handleSharedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004034 if (checkAttrMutualExclusion<CUDAConstantAttr>(S, D, AL.getRange(),
4035 AL.getName()))
Justin Lebar10411012016-09-30 23:57:30 +00004036 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004037 const auto *VD = cast<VarDecl>(D);
Justin Lebar281ce2a2016-10-02 15:24:50 +00004038 // extern __shared__ is only allowed on arrays with no length (e.g.
4039 // "int x[]").
Jonas Hahnfeldee47d8c2018-02-14 16:04:03 +00004040 if (!S.getLangOpts().CUDARelocatableDeviceCode && VD->hasExternalStorage() &&
4041 !isa<IncompleteArrayType>(VD->getType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004042 S.Diag(AL.getLoc(), diag::err_cuda_extern_shared) << VD;
Justin Lebar10411012016-09-30 23:57:30 +00004043 return;
4044 }
Justin Lebaraa370bd2016-10-13 18:45:13 +00004045 if (S.getLangOpts().CUDA && VD->hasLocalStorage() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004046 S.CUDADiagIfHostCode(AL.getLoc(), diag::err_cuda_host_shared)
Justin Lebaraa370bd2016-10-13 18:45:13 +00004047 << S.CurrentCUDATarget())
4048 return;
Justin Lebar10411012016-09-30 23:57:30 +00004049 D->addAttr(::new (S.Context) CUDASharedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004050 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Justin Lebar10411012016-09-30 23:57:30 +00004051}
4052
Erich Keanee891aa92018-07-13 15:07:47 +00004053static void handleGlobalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004054 if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, AL.getRange(),
4055 AL.getName()) ||
4056 checkAttrMutualExclusion<CUDAHostAttr>(S, D, AL.getRange(),
4057 AL.getName())) {
Justin Lebar3eaaf862016-01-13 01:07:35 +00004058 return;
4059 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004060 const auto *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00004061 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00004062 SourceRange RTRange = FD->getReturnTypeSourceRange();
4063 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004064 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00004065 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
4066 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00004067 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004068 }
Justin Lebarc66a1062016-01-20 00:26:57 +00004069 if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) {
4070 if (Method->isInstance()) {
4071 S.Diag(Method->getLocStart(), diag::err_kern_is_nonstatic_method)
4072 << Method;
4073 return;
4074 }
4075 S.Diag(Method->getLocStart(), diag::warn_kern_is_method) << Method;
4076 }
4077 // Only warn for "inline" when compiling for host, to cut down on noise.
4078 if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice)
4079 S.Diag(FD->getLocStart(), diag::warn_kern_is_inline) << FD;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004080
Aaron Ballman3aff6332013-12-02 19:30:36 +00004081 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004082 CUDAGlobalAttr(AL.getRange(), S.Context,
4083 AL.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004084}
4085
Erich Keanee891aa92018-07-13 15:07:47 +00004086static void handleGNUInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004087 const auto *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00004088 if (!Fn->isInlineSpecified()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004089 S.Diag(AL.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00004090 return;
4091 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004092
Michael Han99315932013-01-24 16:46:58 +00004093 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004094 GNUInlineAttr(AL.getRange(), S.Context,
4095 AL.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00004096}
4097
Erich Keanee891aa92018-07-13 15:07:47 +00004098static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004099 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004100
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004101 // Diagnostic is emitted elsewhere: here we store the (valid) AL
John McCall3882ace2011-01-05 12:14:39 +00004102 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
4103 CallingConv CC;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004104 if (S.CheckCallingConvAttr(AL, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00004105 return;
4106
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004107 if (!isa<ObjCMethodDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004108 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
4109 << AL.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00004110 return;
4111 }
4112
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004113 switch (AL.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +00004114 case ParsedAttr::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00004115 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004116 FastCallAttr(AL.getRange(), S.Context,
4117 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004118 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004119 case ParsedAttr::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00004120 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004121 StdCallAttr(AL.getRange(), S.Context,
4122 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004123 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004124 case ParsedAttr::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00004125 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004126 ThisCallAttr(AL.getRange(), S.Context,
4127 AL.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00004128 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004129 case ParsedAttr::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00004130 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004131 CDeclAttr(AL.getRange(), S.Context,
4132 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004133 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004134 case ParsedAttr::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00004135 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004136 PascalAttr(AL.getRange(), S.Context,
4137 AL.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00004138 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004139 case ParsedAttr::AT_SwiftCall:
John McCall477f2bb2016-03-03 06:39:32 +00004140 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004141 SwiftCallAttr(AL.getRange(), S.Context,
4142 AL.getAttributeSpellingListIndex()));
John McCall477f2bb2016-03-03 06:39:32 +00004143 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004144 case ParsedAttr::AT_VectorCall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00004145 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004146 VectorCallAttr(AL.getRange(), S.Context,
4147 AL.getAttributeSpellingListIndex()));
Reid Klecknerd7857f02014-10-24 17:42:17 +00004148 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004149 case ParsedAttr::AT_MSABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004150 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004151 MSABIAttr(AL.getRange(), S.Context,
4152 AL.getAttributeSpellingListIndex()));
Charles Davisb5a214e2013-08-30 04:39:01 +00004153 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004154 case ParsedAttr::AT_SysVABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004155 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004156 SysVABIAttr(AL.getRange(), S.Context,
4157 AL.getAttributeSpellingListIndex()));
Charles Davisb5a214e2013-08-30 04:39:01 +00004158 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004159 case ParsedAttr::AT_RegCall:
Erich Keane757d3172016-11-02 18:29:35 +00004160 D->addAttr(::new (S.Context) RegCallAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004161 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Erich Keane757d3172016-11-02 18:29:35 +00004162 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004163 case ParsedAttr::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004164 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004165 switch (CC) {
4166 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004167 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004168 break;
4169 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004170 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004171 break;
4172 default:
4173 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004174 }
4175
Michael Han99315932013-01-24 16:46:58 +00004176 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004177 PcsAttr(AL.getRange(), S.Context, PCS,
4178 AL.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00004179 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004180 }
Erich Keanee891aa92018-07-13 15:07:47 +00004181 case ParsedAttr::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00004182 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004183 IntelOclBiccAttr(AL.getRange(), S.Context,
4184 AL.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00004185 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004186 case ParsedAttr::AT_PreserveMost:
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004187 D->addAttr(::new (S.Context) PreserveMostAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004188 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004189 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004190 case ParsedAttr::AT_PreserveAll:
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004191 D->addAttr(::new (S.Context) PreserveAllAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004192 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004193 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004194 default:
4195 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00004196 }
4197}
4198
Erich Keanee891aa92018-07-13 15:07:47 +00004199static void handleSuppressAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004200 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Matthias Gehre01a63382017-03-27 19:45:24 +00004201 return;
4202
4203 std::vector<StringRef> DiagnosticIdentifiers;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004204 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Matthias Gehre01a63382017-03-27 19:45:24 +00004205 StringRef RuleName;
4206
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004207 if (!S.checkStringLiteralArgumentAttr(AL, I, RuleName, nullptr))
Matthias Gehre01a63382017-03-27 19:45:24 +00004208 return;
4209
4210 // FIXME: Warn if the rule name is unknown. This is tricky because only
4211 // clang-tidy knows about available rules.
4212 DiagnosticIdentifiers.push_back(RuleName);
4213 }
4214 D->addAttr(::new (S.Context) SuppressAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004215 AL.getRange(), S.Context, DiagnosticIdentifiers.data(),
4216 DiagnosticIdentifiers.size(), AL.getAttributeSpellingListIndex()));
Matthias Gehre01a63382017-03-27 19:45:24 +00004217}
4218
Erich Keanee891aa92018-07-13 15:07:47 +00004219bool Sema::CheckCallingConvAttr(const ParsedAttr &Attrs, CallingConv &CC,
Aaron Ballman02df2e02012-12-09 17:45:41 +00004220 const FunctionDecl *FD) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00004221 if (Attrs.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004222 return true;
4223
Erich Keaneb11ebc52017-09-27 03:20:13 +00004224 if (Attrs.hasProcessingCache()) {
4225 CC = (CallingConv) Attrs.getProcessingCache();
John McCall3b5a8f52016-03-03 00:10:03 +00004226 return false;
4227 }
4228
Erich Keanee891aa92018-07-13 15:07:47 +00004229 unsigned ReqArgs = Attrs.getKind() == ParsedAttr::AT_Pcs ? 1 : 0;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004230 if (!checkAttributeNumArgs(*this, Attrs, ReqArgs)) {
4231 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004232 return true;
4233 }
4234
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004235 // TODO: diagnose uses of these conventions on the wrong target.
Erich Keaneb11ebc52017-09-27 03:20:13 +00004236 switch (Attrs.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +00004237 case ParsedAttr::AT_CDecl:
4238 CC = CC_C;
4239 break;
4240 case ParsedAttr::AT_FastCall:
4241 CC = CC_X86FastCall;
4242 break;
4243 case ParsedAttr::AT_StdCall:
4244 CC = CC_X86StdCall;
4245 break;
4246 case ParsedAttr::AT_ThisCall:
4247 CC = CC_X86ThisCall;
4248 break;
4249 case ParsedAttr::AT_Pascal:
4250 CC = CC_X86Pascal;
4251 break;
4252 case ParsedAttr::AT_SwiftCall:
4253 CC = CC_Swift;
4254 break;
4255 case ParsedAttr::AT_VectorCall:
4256 CC = CC_X86VectorCall;
4257 break;
4258 case ParsedAttr::AT_RegCall:
4259 CC = CC_X86RegCall;
4260 break;
4261 case ParsedAttr::AT_MSABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004262 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
Martin Storsjo022e7822017-07-17 20:49:45 +00004263 CC_Win64;
Charles Davisb5a214e2013-08-30 04:39:01 +00004264 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004265 case ParsedAttr::AT_SysVABI:
Charles Davisb5a214e2013-08-30 04:39:01 +00004266 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
4267 CC_C;
4268 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004269 case ParsedAttr::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00004270 StringRef StrRef;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004271 if (!checkStringLiteralArgumentAttr(Attrs, 0, StrRef)) {
4272 Attrs.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004273 return true;
4274 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004275 if (StrRef == "aapcs") {
4276 CC = CC_AAPCS;
4277 break;
4278 } else if (StrRef == "aapcs-vfp") {
4279 CC = CC_AAPCS_VFP;
4280 break;
4281 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004282
Erich Keaneb11ebc52017-09-27 03:20:13 +00004283 Attrs.setInvalid();
4284 Diag(Attrs.getLoc(), diag::err_invalid_pcs);
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004285 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004286 }
Erich Keanee891aa92018-07-13 15:07:47 +00004287 case ParsedAttr::AT_IntelOclBicc:
4288 CC = CC_IntelOclBicc;
4289 break;
4290 case ParsedAttr::AT_PreserveMost:
4291 CC = CC_PreserveMost;
4292 break;
4293 case ParsedAttr::AT_PreserveAll:
4294 CC = CC_PreserveAll;
4295 break;
David Blaikie8a40f702012-01-17 06:56:22 +00004296 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00004297 }
4298
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004299 const TargetInfo &TI = Context.getTargetInfo();
4300 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004301 if (A != TargetInfo::CCCR_OK) {
4302 if (A == TargetInfo::CCCR_Warning)
Erich Keaneb11ebc52017-09-27 03:20:13 +00004303 Diag(Attrs.getLoc(), diag::warn_cconv_ignored) << Attrs.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00004304
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004305 // This convention is not valid for the target. Use the default function or
4306 // method calling convention.
Alexey Bataeva7547182016-05-18 09:06:38 +00004307 bool IsCXXMethod = false, IsVariadic = false;
4308 if (FD) {
4309 IsCXXMethod = FD->isCXXInstanceMember();
4310 IsVariadic = FD->isVariadic();
4311 }
4312 CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004313 }
4314
Erich Keaneb11ebc52017-09-27 03:20:13 +00004315 Attrs.setProcessingCache((unsigned) CC);
John McCall3882ace2011-01-05 12:14:39 +00004316 return false;
4317}
4318
John McCall477f2bb2016-03-03 06:39:32 +00004319/// Pointer-like types in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004320static bool isValidSwiftContextType(QualType Ty) {
4321 if (!Ty->hasPointerRepresentation())
4322 return Ty->isDependentType();
4323 return Ty->getPointeeType().getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004324}
4325
4326/// Pointers and references in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004327static bool isValidSwiftIndirectResultType(QualType Ty) {
4328 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4329 Ty = PtrType->getPointeeType();
4330 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4331 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004332 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004333 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004334 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004335 return Ty.getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004336}
4337
4338/// Pointers and references to pointers in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004339static bool isValidSwiftErrorResultType(QualType Ty) {
4340 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4341 Ty = PtrType->getPointeeType();
4342 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4343 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004344 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004345 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004346 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004347 if (!Ty.getQualifiers().empty())
John McCall477f2bb2016-03-03 06:39:32 +00004348 return false;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004349 return isValidSwiftContextType(Ty);
John McCall477f2bb2016-03-03 06:39:32 +00004350}
4351
Erich Keanee891aa92018-07-13 15:07:47 +00004352static void handleParameterABIAttr(Sema &S, Decl *D, const ParsedAttr &Attrs,
Erich Keaneb11ebc52017-09-27 03:20:13 +00004353 ParameterABI Abi) {
4354 S.AddParameterABIAttr(Attrs.getRange(), D, Abi,
4355 Attrs.getAttributeSpellingListIndex());
John McCall477f2bb2016-03-03 06:39:32 +00004356}
4357
4358void Sema::AddParameterABIAttr(SourceRange range, Decl *D, ParameterABI abi,
4359 unsigned spellingIndex) {
4360
4361 QualType type = cast<ParmVarDecl>(D)->getType();
4362
4363 if (auto existingAttr = D->getAttr<ParameterABIAttr>()) {
4364 if (existingAttr->getABI() != abi) {
4365 Diag(range.getBegin(), diag::err_attributes_are_not_compatible)
4366 << getParameterABISpelling(abi) << existingAttr;
4367 Diag(existingAttr->getLocation(), diag::note_conflicting_attribute);
4368 return;
4369 }
4370 }
4371
4372 switch (abi) {
4373 case ParameterABI::Ordinary:
4374 llvm_unreachable("explicit attribute for ordinary parameter ABI?");
4375
4376 case ParameterABI::SwiftContext:
4377 if (!isValidSwiftContextType(type)) {
4378 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4379 << getParameterABISpelling(abi)
4380 << /*pointer to pointer */ 0 << type;
4381 }
4382 D->addAttr(::new (Context)
4383 SwiftContextAttr(range, Context, spellingIndex));
4384 return;
4385
4386 case ParameterABI::SwiftErrorResult:
4387 if (!isValidSwiftErrorResultType(type)) {
4388 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4389 << getParameterABISpelling(abi)
4390 << /*pointer to pointer */ 1 << type;
4391 }
4392 D->addAttr(::new (Context)
4393 SwiftErrorResultAttr(range, Context, spellingIndex));
4394 return;
4395
4396 case ParameterABI::SwiftIndirectResult:
4397 if (!isValidSwiftIndirectResultType(type)) {
4398 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4399 << getParameterABISpelling(abi)
4400 << /*pointer*/ 0 << type;
4401 }
4402 D->addAttr(::new (Context)
4403 SwiftIndirectResultAttr(range, Context, spellingIndex));
4404 return;
4405 }
4406 llvm_unreachable("bad parameter ABI attribute");
4407}
4408
John McCall3882ace2011-01-05 12:14:39 +00004409/// Checks a regparm attribute, returning true if it is ill-formed and
4410/// otherwise setting numParams to the appropriate value.
Erich Keanee891aa92018-07-13 15:07:47 +00004411bool Sema::CheckRegparmAttr(const ParsedAttr &AL, unsigned &numParams) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004412 if (AL.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004413 return true;
4414
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004415 if (!checkAttributeNumArgs(*this, AL, 1)) {
4416 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004417 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004418 }
Eli Friedman7044b762009-03-27 21:06:47 +00004419
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004420 uint32_t NP;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004421 Expr *NumParamsExpr = AL.getArgAsExpr(0);
4422 if (!checkUInt32Argument(*this, AL, NumParamsExpr, NP)) {
4423 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004424 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004425 }
4426
Douglas Gregore8bbc122011-09-02 00:18:52 +00004427 if (Context.getTargetInfo().getRegParmMax() == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004428 Diag(AL.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00004429 << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004430 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004431 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004432 }
4433
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004434 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00004435 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004436 Diag(AL.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00004437 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004438 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004439 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004440 }
4441
John McCall3882ace2011-01-05 12:14:39 +00004442 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004443}
4444
Artem Belevichbcec9da2016-06-06 22:54:57 +00004445// Checks whether an argument of launch_bounds attribute is
4446// acceptable, performs implicit conversion to Rvalue, and returns
4447// non-nullptr Expr result on success. Otherwise, it returns nullptr
4448// and may output an error.
4449static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004450 const CUDALaunchBoundsAttr &AL,
Artem Belevichbcec9da2016-06-06 22:54:57 +00004451 const unsigned Idx) {
Artem Belevich7093e402015-04-21 22:55:54 +00004452 if (S.DiagnoseUnexpandedParameterPack(E))
Artem Belevichbcec9da2016-06-06 22:54:57 +00004453 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004454
4455 // Accept template arguments for now as they depend on something else.
4456 // We'll get to check them when they eventually get instantiated.
4457 if (E->isValueDependent())
Artem Belevichbcec9da2016-06-06 22:54:57 +00004458 return E;
Artem Belevich7093e402015-04-21 22:55:54 +00004459
4460 llvm::APSInt I(64);
4461 if (!E->isIntegerConstantExpr(I, S.Context)) {
4462 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004463 << &AL << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
Artem Belevichbcec9da2016-06-06 22:54:57 +00004464 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004465 }
4466 // Make sure we can fit it in 32 bits.
4467 if (!I.isIntN(32)) {
4468 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
4469 << 32 << /* Unsigned */ 1;
Artem Belevichbcec9da2016-06-06 22:54:57 +00004470 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004471 }
4472 if (I < 0)
4473 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004474 << &AL << Idx << E->getSourceRange();
Artem Belevich7093e402015-04-21 22:55:54 +00004475
Artem Belevichbcec9da2016-06-06 22:54:57 +00004476 // We may need to perform implicit conversion of the argument.
4477 InitializedEntity Entity = InitializedEntity::InitializeParameter(
4478 S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false);
4479 ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E);
4480 assert(!ValArg.isInvalid() &&
4481 "Unexpected PerformCopyInitialization() failure.");
4482
4483 return ValArg.getAs<Expr>();
Artem Belevich7093e402015-04-21 22:55:54 +00004484}
4485
4486void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads,
4487 Expr *MinBlocks, unsigned SpellingListIndex) {
4488 CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks,
4489 SpellingListIndex);
Artem Belevichbcec9da2016-06-06 22:54:57 +00004490 MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0);
4491 if (MaxThreads == nullptr)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004492 return;
4493
Artem Belevichbcec9da2016-06-06 22:54:57 +00004494 if (MinBlocks) {
4495 MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1);
4496 if (MinBlocks == nullptr)
4497 return;
4498 }
Artem Belevich7093e402015-04-21 22:55:54 +00004499
4500 D->addAttr(::new (Context) CUDALaunchBoundsAttr(
4501 AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex));
4502}
4503
Erich Keanee891aa92018-07-13 15:07:47 +00004504static void handleLaunchBoundsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004505 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
4506 !checkAttributeAtMostNumArgs(S, AL, 2))
Artem Belevich7093e402015-04-21 22:55:54 +00004507 return;
4508
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004509 S.AddLaunchBoundsAttr(AL.getRange(), D, AL.getArgAsExpr(0),
4510 AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr,
4511 AL.getAttributeSpellingListIndex());
Peter Collingbourne827301e2010-12-12 23:03:07 +00004512}
4513
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004514static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004515 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004516 if (!AL.isArgIdent(0)) {
4517 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
4518 << AL.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004519 return;
4520 }
Joel E. Denny81508102018-03-13 14:51:22 +00004521
4522 ParamIdx ArgumentIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004523 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 2, AL.getArgAsExpr(1),
Alp Toker601b22c2014-01-21 23:35:24 +00004524 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004525 return;
4526
Joel E. Denny81508102018-03-13 14:51:22 +00004527 ParamIdx TypeTagIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004528 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 3, AL.getArgAsExpr(2),
Alp Toker601b22c2014-01-21 23:35:24 +00004529 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004530 return;
4531
Aaron Ballmana26d8ee2018-02-25 14:01:04 +00004532 bool IsPointer = AL.getName()->getName() == "pointer_with_type_tag";
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004533 if (IsPointer) {
4534 // Ensure that buffer has a pointer type.
Joel E. Denny81508102018-03-13 14:51:22 +00004535 unsigned ArgumentIdxAST = ArgumentIdx.getASTIndex();
4536 if (ArgumentIdxAST >= getFunctionOrMethodNumParams(D) ||
4537 !getFunctionOrMethodParamType(D, ArgumentIdxAST)->isPointerType())
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004538 S.Diag(AL.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballmanbd0f6562018-02-25 20:28:10 +00004539 << AL.getName() << 0;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004540 }
4541
Aaron Ballmana26d8ee2018-02-25 14:01:04 +00004542 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(
4543 AL.getRange(), S.Context, AL.getArgAsIdent(0)->Ident, ArgumentIdx,
4544 TypeTagIdx, IsPointer, AL.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004545}
4546
4547static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004548 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004549 if (!AL.isArgIdent(0)) {
4550 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
4551 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004552 return;
4553 }
Aaron Ballman00e99962013-08-31 01:11:41 +00004554
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004555 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00004556 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004557
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004558 if (!isa<VarDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004559 S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type)
4560 << AL.getName() << ExpectedVariable;
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004561 return;
4562 }
4563
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004564 IdentifierInfo *PointerKind = AL.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00004565 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004566 S.GetTypeFromParser(AL.getMatchingCType(), &MatchingCTypeLoc);
Richard Smithb87c4652013-10-31 21:23:20 +00004567 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004568
Michael Han99315932013-01-24 16:46:58 +00004569 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004570 TypeTagForDatatypeAttr(AL.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00004571 MatchingCTypeLoc,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004572 AL.getLayoutCompatible(),
4573 AL.getMustBeNull(),
4574 AL.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004575}
4576
Erich Keanee891aa92018-07-13 15:07:47 +00004577static void handleXRayLogArgsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Joel E. Denny81508102018-03-13 14:51:22 +00004578 ParamIdx ArgCount;
Dean Michael Berris7456a282017-06-16 03:22:09 +00004579
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004580 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, AL.getArgAsExpr(0),
Dean Michael Berris7456a282017-06-16 03:22:09 +00004581 ArgCount,
Joel E. Denny81508102018-03-13 14:51:22 +00004582 true /* CanIndexImplicitThis */))
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004583 return;
4584
Joel E. Denny81508102018-03-13 14:51:22 +00004585 // ArgCount isn't a parameter index [0;n), it's a count [1;n]
4586 D->addAttr(::new (S.Context) XRayLogArgsAttr(
4587 AL.getRange(), S.Context, ArgCount.getSourceIndex(),
4588 AL.getAttributeSpellingListIndex()));
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004589}
4590
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004591//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004592// Checker-specific attribute handlers.
4593//===----------------------------------------------------------------------===//
4594
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004595static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType QT) {
4596 return QT->isDependentType() || QT->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004597}
4598
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004599static bool isValidSubjectOfNSAttribute(Sema &S, QualType QT) {
4600 return QT->isDependentType() || QT->isObjCObjectPointerType() ||
4601 S.Context.isObjCNSObjectType(QT);
John McCalled433932011-01-25 03:31:58 +00004602}
Eugene Zelenko1ced5092016-02-12 22:53:10 +00004603
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004604static bool isValidSubjectOfCFAttribute(Sema &S, QualType QT) {
4605 return QT->isDependentType() || QT->isPointerType() ||
4606 isValidSubjectOfNSAttribute(S, QT);
John McCalled433932011-01-25 03:31:58 +00004607}
4608
Erich Keanee891aa92018-07-13 15:07:47 +00004609static void handleNSConsumedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004610 S.AddNSConsumedAttr(AL.getRange(), D, AL.getAttributeSpellingListIndex(),
Erich Keanee891aa92018-07-13 15:07:47 +00004611 AL.getKind() == ParsedAttr::AT_NSConsumed,
John McCall3b5a8f52016-03-03 00:10:03 +00004612 /*template instantiation*/ false);
4613}
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004614
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004615void Sema::AddNSConsumedAttr(SourceRange AttrRange, Decl *D,
4616 unsigned SpellingIndex, bool IsNSConsumed,
4617 bool IsTemplateInstantiation) {
4618 const auto *Param = cast<ParmVarDecl>(D);
4619 bool TypeOK;
John McCall3b5a8f52016-03-03 00:10:03 +00004620
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004621 if (IsNSConsumed)
4622 TypeOK = isValidSubjectOfNSAttribute(*this, Param->getType());
4623 else
4624 TypeOK = isValidSubjectOfCFAttribute(*this, Param->getType());
John McCalled433932011-01-25 03:31:58 +00004625
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004626 if (!TypeOK) {
John McCall3b5a8f52016-03-03 00:10:03 +00004627 // These attributes are normally just advisory, but in ARC, ns_consumed
4628 // is significant. Allow non-dependent code to contain inappropriate
4629 // attributes even in ARC, but require template instantiations to be
4630 // set up correctly.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004631 Diag(D->getLocStart(), (IsTemplateInstantiation && IsNSConsumed &&
4632 getLangOpts().ObjCAutoRefCount
4633 ? diag::err_ns_attribute_wrong_parameter_type
4634 : diag::warn_ns_attribute_wrong_parameter_type))
4635 << AttrRange << (IsNSConsumed ? "ns_consumed" : "cf_consumed")
4636 << (IsNSConsumed ? /*objc pointers*/ 0 : /*cf pointers*/ 1);
John McCalled433932011-01-25 03:31:58 +00004637 return;
4638 }
4639
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004640 if (IsNSConsumed)
4641 D->addAttr(::new (Context)
4642 NSConsumedAttr(AttrRange, Context, SpellingIndex));
John McCalled433932011-01-25 03:31:58 +00004643 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004644 D->addAttr(::new (Context)
4645 CFConsumedAttr(AttrRange, Context, SpellingIndex));
John McCalled433932011-01-25 03:31:58 +00004646}
4647
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004648bool Sema::checkNSReturnsRetainedReturnType(SourceLocation Loc, QualType QT) {
4649 if (isValidSubjectOfNSReturnsRetainedAttribute(QT))
John McCall12251882017-07-15 11:06:46 +00004650 return false;
4651
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004652 Diag(Loc, diag::warn_ns_attribute_wrong_return_type)
4653 << "'ns_returns_retained'" << 0 << 0;
John McCall12251882017-07-15 11:06:46 +00004654 return true;
4655}
4656
Chandler Carruthedc2c642011-07-02 00:01:44 +00004657static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004658 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004659 QualType ReturnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004660
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004661 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
4662 ReturnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004663 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Erich Keanee891aa92018-07-13 15:07:47 +00004664 (AL.getKind() == ParsedAttr::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004665 return; // ignore: was handled as a type attribute
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004666 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D))
4667 ReturnType = PD->getType();
4668 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
4669 ReturnType = FD->getReturnType();
4670 else if (const auto *Param = dyn_cast<ParmVarDecl>(D)) {
4671 ReturnType = Param->getType()->getPointeeType();
4672 if (ReturnType.isNull()) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004673 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004674 << AL.getName() << /*pointer-to-CF*/2
4675 << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004676 return;
4677 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004678 } else if (AL.isUsedAsTypeAttr()) {
John McCall12251882017-07-15 11:06:46 +00004679 return;
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004680 } else {
4681 AttributeDeclKind ExpectedDeclKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004682 switch (AL.getKind()) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004683 default: llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00004684 case ParsedAttr::AT_NSReturnsRetained:
4685 case ParsedAttr::AT_NSReturnsAutoreleased:
4686 case ParsedAttr::AT_NSReturnsNotRetained:
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004687 ExpectedDeclKind = ExpectedFunctionOrMethod;
4688 break;
4689
Erich Keanee891aa92018-07-13 15:07:47 +00004690 case ParsedAttr::AT_CFReturnsRetained:
4691 case ParsedAttr::AT_CFReturnsNotRetained:
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004692 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
4693 break;
4694 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004695 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004696 << AL.getRange() << AL.getName() << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004697 return;
4698 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004699
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004700 bool TypeOK;
4701 bool Cf;
4702 switch (AL.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004703 default: llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00004704 case ParsedAttr::AT_NSReturnsRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004705 TypeOK = isValidSubjectOfNSReturnsRetainedAttribute(ReturnType);
4706 Cf = false;
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004707 break;
Erich Keanee891aa92018-07-13 15:07:47 +00004708
4709 case ParsedAttr::AT_NSReturnsAutoreleased:
4710 case ParsedAttr::AT_NSReturnsNotRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004711 TypeOK = isValidSubjectOfNSAttribute(S, ReturnType);
4712 Cf = false;
John McCalled433932011-01-25 03:31:58 +00004713 break;
4714
Erich Keanee891aa92018-07-13 15:07:47 +00004715 case ParsedAttr::AT_CFReturnsRetained:
4716 case ParsedAttr::AT_CFReturnsNotRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004717 TypeOK = isValidSubjectOfCFAttribute(S, ReturnType);
4718 Cf = true;
John McCalled433932011-01-25 03:31:58 +00004719 break;
4720 }
4721
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004722 if (!TypeOK) {
4723 if (AL.isUsedAsTypeAttr())
John McCall12251882017-07-15 11:06:46 +00004724 return;
4725
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004726 if (isa<ParmVarDecl>(D)) {
4727 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004728 << AL.getName() << /*pointer-to-CF*/2
4729 << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004730 } else {
4731 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
4732 enum : unsigned {
4733 Function,
4734 Method,
4735 Property
4736 } SubjectKind = Function;
4737 if (isa<ObjCMethodDecl>(D))
4738 SubjectKind = Method;
4739 else if (isa<ObjCPropertyDecl>(D))
4740 SubjectKind = Property;
4741 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004742 << AL.getName() << SubjectKind << Cf
4743 << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004744 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004745 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004746 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004747
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004748 switch (AL.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004749 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004750 llvm_unreachable("invalid ownership attribute");
Erich Keanee891aa92018-07-13 15:07:47 +00004751 case ParsedAttr::AT_NSReturnsAutoreleased:
Nico Weber462fd1e2015-01-07 23:50:05 +00004752 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004753 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004754 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004755 case ParsedAttr::AT_CFReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004756 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004757 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004758 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004759 case ParsedAttr::AT_NSReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004760 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004761 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004762 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004763 case ParsedAttr::AT_CFReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004764 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004765 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004766 return;
Erich Keanee891aa92018-07-13 15:07:47 +00004767 case ParsedAttr::AT_NSReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004768 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004769 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004770 return;
4771 };
4772}
4773
John McCallcf166702011-07-22 08:53:00 +00004774static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004775 const ParsedAttr &Attrs) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00004776 const int EP_ObjCMethod = 1;
4777 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004778
Erich Keaneb11ebc52017-09-27 03:20:13 +00004779 SourceLocation loc = Attrs.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004780 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004781 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004782 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004783 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004784 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00004785
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004786 if (!resultType->isReferenceType() &&
4787 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004788 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00004789 << SourceRange(loc)
Erich Keaneb11ebc52017-09-27 03:20:13 +00004790 << Attrs.getName()
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004791 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004792 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00004793
4794 // Drop the attribute.
4795 return;
4796 }
4797
Nico Weber462fd1e2015-01-07 23:50:05 +00004798 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(
Erich Keaneb11ebc52017-09-27 03:20:13 +00004799 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004800}
4801
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004802static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004803 const ParsedAttr &Attrs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004804 const auto *Method = cast<ObjCMethodDecl>(D);
4805
4806 const DeclContext *DC = Method->getDeclContext();
4807 if (const auto *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004808 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004809 << Attrs.getName() << 0;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004810 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4811 return;
4812 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004813 if (Method->getMethodFamily() == OMF_dealloc) {
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004814 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004815 << Attrs.getName() << 1;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004816 return;
4817 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004818
4819 D->addAttr(::new (S.Context) ObjCRequiresSuperAttr(
4820 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004821}
4822
Erich Keanee891aa92018-07-13 15:07:47 +00004823static void handleObjCBridgeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004824 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004825
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004826 if (!Parm) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004827 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << AL.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004828 return;
4829 }
John McCall28592582015-02-01 22:34:06 +00004830
4831 // Typedefs only allow objc_bridge(id) and have some additional checking.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004832 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall28592582015-02-01 22:34:06 +00004833 if (!Parm->Ident->isStr("id")) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004834 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_id)
4835 << AL.getName();
John McCall28592582015-02-01 22:34:06 +00004836 return;
4837 }
4838
4839 // Only allow 'cv void *'.
4840 QualType T = TD->getUnderlyingType();
4841 if (!T->isVoidPointerType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004842 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
John McCall28592582015-02-01 22:34:06 +00004843 return;
4844 }
4845 }
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004846
4847 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004848 ObjCBridgeAttr(AL.getRange(), S.Context, Parm->Ident,
4849 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004850}
4851
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004852static void handleObjCBridgeMutableAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004853 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004854 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Craig Topperc3ec1492014-05-26 06:22:03 +00004855
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004856 if (!Parm) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004857 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << AL.getName() << 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004858 return;
4859 }
4860
4861 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004862 ObjCBridgeMutableAttr(AL.getRange(), S.Context, Parm->Ident,
4863 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004864}
4865
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004866static void handleObjCBridgeRelatedAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004867 const ParsedAttr &AL) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004868 IdentifierInfo *RelatedClass =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004869 AL.isArgIdent(0) ? AL.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004870 if (!RelatedClass) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004871 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << AL.getName() << 0;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004872 return;
4873 }
4874 IdentifierInfo *ClassMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004875 AL.getArgAsIdent(1) ? AL.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004876 IdentifierInfo *InstanceMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004877 AL.getArgAsIdent(2) ? AL.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004878 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004879 ObjCBridgeRelatedAttr(AL.getRange(), S.Context, RelatedClass,
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004880 ClassMethod, InstanceMethod,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004881 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004882}
4883
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004884static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004885 const ParsedAttr &AL) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004886 ObjCInterfaceDecl *IFace;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004887 if (auto *CatDecl = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004888 IFace = CatDecl->getClassInterface();
4889 else
4890 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00004891
4892 if (!IFace)
4893 return;
4894
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00004895 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00004896 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004897 ObjCDesignatedInitializerAttr(AL.getRange(), S.Context,
4898 AL.getAttributeSpellingListIndex()));
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004899}
4900
Erich Keanee891aa92018-07-13 15:07:47 +00004901static void handleObjCRuntimeName(Sema &S, Decl *D, const ParsedAttr &AL) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004902 StringRef MetaDataName;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004903 if (!S.checkStringLiteralArgumentAttr(AL, 0, MetaDataName))
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004904 return;
4905 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004906 ObjCRuntimeNameAttr(AL.getRange(), S.Context,
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004907 MetaDataName,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004908 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004909}
4910
Nico Webera6916892016-06-10 18:53:04 +00004911// When a user wants to use objc_boxable with a union or struct
4912// but they don't have access to the declaration (legacy/third-party code)
4913// then they can 'enable' this feature with a typedef:
Alex Denisovfde64952015-06-26 05:28:36 +00004914// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
Erich Keanee891aa92018-07-13 15:07:47 +00004915static void handleObjCBoxable(Sema &S, Decl *D, const ParsedAttr &AL) {
Alex Denisovfde64952015-06-26 05:28:36 +00004916 bool notify = false;
4917
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004918 auto *RD = dyn_cast<RecordDecl>(D);
Alex Denisovfde64952015-06-26 05:28:36 +00004919 if (RD && RD->getDefinition()) {
4920 RD = RD->getDefinition();
4921 notify = true;
4922 }
4923
4924 if (RD) {
4925 ObjCBoxableAttr *BoxableAttr = ::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004926 ObjCBoxableAttr(AL.getRange(), S.Context,
4927 AL.getAttributeSpellingListIndex());
Alex Denisovfde64952015-06-26 05:28:36 +00004928 RD->addAttr(BoxableAttr);
4929 if (notify) {
4930 // we need to notify ASTReader/ASTWriter about
4931 // modification of existing declaration
4932 if (ASTMutationListener *L = S.getASTMutationListener())
4933 L->AddedAttributeToRecord(BoxableAttr, RD);
4934 }
4935 }
4936}
4937
Erich Keanee891aa92018-07-13 15:07:47 +00004938static void handleObjCOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004939 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004940
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004941 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004942 << AL.getRange() << AL.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004943}
4944
Chandler Carruthedc2c642011-07-02 00:01:44 +00004945static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00004946 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004947 const auto *VD = cast<ValueDecl>(D);
4948 QualType QT = VD->getType();
John McCall31168b02011-06-15 23:02:42 +00004949
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004950 if (!QT->isDependentType() &&
4951 !QT->isObjCLifetimeType()) {
4952 S.Diag(AL.getLoc(), diag::err_objc_precise_lifetime_bad_type)
4953 << QT;
John McCall31168b02011-06-15 23:02:42 +00004954 return;
4955 }
4956
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004957 Qualifiers::ObjCLifetime Lifetime = QT.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +00004958
4959 // If we have no lifetime yet, check the lifetime we're presumably
4960 // going to infer.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004961 if (Lifetime == Qualifiers::OCL_None && !QT->isDependentType())
4962 Lifetime = QT->getObjCARCImplicitLifetime();
John McCall31168b02011-06-15 23:02:42 +00004963
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004964 switch (Lifetime) {
John McCall31168b02011-06-15 23:02:42 +00004965 case Qualifiers::OCL_None:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004966 assert(QT->isDependentType() &&
John McCall31168b02011-06-15 23:02:42 +00004967 "didn't infer lifetime for non-dependent type?");
4968 break;
4969
4970 case Qualifiers::OCL_Weak: // meaningful
4971 case Qualifiers::OCL_Strong: // meaningful
4972 break;
4973
4974 case Qualifiers::OCL_ExplicitNone:
4975 case Qualifiers::OCL_Autoreleasing:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004976 S.Diag(AL.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
4977 << (Lifetime == Qualifiers::OCL_Autoreleasing);
John McCall31168b02011-06-15 23:02:42 +00004978 break;
4979 }
4980
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004981 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004982 ObjCPreciseLifetimeAttr(AL.getRange(), S.Context,
4983 AL.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004984}
4985
Francois Picheta83957a2010-12-19 06:50:37 +00004986//===----------------------------------------------------------------------===//
4987// Microsoft specific attribute handlers.
4988//===----------------------------------------------------------------------===//
4989
Nico Weber88f5ed92016-09-13 18:55:26 +00004990UuidAttr *Sema::mergeUuidAttr(Decl *D, SourceRange Range,
4991 unsigned AttrSpellingListIndex, StringRef Uuid) {
4992 if (const auto *UA = D->getAttr<UuidAttr>()) {
Nico Weberd58c2602016-09-14 01:16:54 +00004993 if (UA->getGuid().equals_lower(Uuid))
Nico Weber88f5ed92016-09-13 18:55:26 +00004994 return nullptr;
4995 Diag(UA->getLocation(), diag::err_mismatched_uuid);
4996 Diag(Range.getBegin(), diag::note_previous_uuid);
4997 D->dropAttr<UuidAttr>();
4998 }
4999
5000 return ::new (Context) UuidAttr(Range, Context, Uuid, AttrSpellingListIndex);
5001}
5002
Erich Keanee891aa92018-07-13 15:07:47 +00005003static void handleUuidAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005004 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005005 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
5006 << AL.getName() << AttributeLangSupport::C;
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005007 return;
5008 }
5009
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005010 StringRef StrRef;
5011 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005012 if (!S.checkStringLiteralArgumentAttr(AL, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00005013 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005014
David Majnemer89085342013-08-09 08:56:20 +00005015 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
5016 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00005017 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
5018 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00005019
Reid Kleckner140c4a72013-05-17 14:04:52 +00005020 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00005021 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005022 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005023 return;
5024 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00005025
David Majnemer89085342013-08-09 08:56:20 +00005026 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00005027 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00005028 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005029 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00005030 return;
5031 }
David Majnemer89085342013-08-09 08:56:20 +00005032 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005033 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005034 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005035 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00005036 }
Francois Picheta83957a2010-12-19 06:50:37 +00005037
Nico Weber469891e2017-05-05 17:05:56 +00005038 // FIXME: It'd be nice to also emit a fixit removing uuid(...) (and, if it's
5039 // the only thing in the [] list, the [] too), and add an insertion of
5040 // __declspec(uuid(...)). But sadly, neither the SourceLocs of the commas
5041 // separating attributes nor of the [ and the ] are in the AST.
Nico Weber0a234042017-05-05 17:15:08 +00005042 // Cf "SourceLocations of attribute list delimiters - [[ ... , ... ]] etc"
Nico Weber469891e2017-05-05 17:05:56 +00005043 // on cfe-dev.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005044 if (AL.isMicrosoftAttribute()) // Check for [uuid(...)] spelling.
5045 S.Diag(AL.getLoc(), diag::warn_atl_uuid_deprecated);
Nico Weber469891e2017-05-05 17:05:56 +00005046
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005047 UuidAttr *UA = S.mergeUuidAttr(D, AL.getRange(),
5048 AL.getAttributeSpellingListIndex(), StrRef);
Nico Weber88f5ed92016-09-13 18:55:26 +00005049 if (UA)
5050 D->addAttr(UA);
Charles Davis163855f2010-02-16 18:27:26 +00005051}
5052
Erich Keanee891aa92018-07-13 15:07:47 +00005053static void handleMSInheritanceAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005054 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005055 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
5056 << AL.getName() << AttributeLangSupport::C;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005057 return;
5058 }
5059 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005060 D, AL.getRange(), /*BestCase=*/true,
5061 AL.getAttributeSpellingListIndex(),
5062 (MSInheritanceAttr::Spelling)AL.getSemanticSpelling());
David Majnemer929025d2016-01-26 19:30:26 +00005063 if (IA) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005064 D->addAttr(IA);
David Majnemer929025d2016-01-26 19:30:26 +00005065 S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D));
5066 }
David Majnemer2c4e00a2014-01-29 22:07:36 +00005067}
5068
Erich Keanee891aa92018-07-13 15:07:47 +00005069static void handleDeclspecThreadAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005070 const auto *VD = cast<VarDecl>(D);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005071 if (!S.Context.getTargetInfo().isTLSSupported()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005072 S.Diag(AL.getLoc(), diag::err_thread_unsupported);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005073 return;
5074 }
5075 if (VD->getTSCSpec() != TSCS_unspecified) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005076 S.Diag(AL.getLoc(), diag::err_declspec_thread_on_thread_variable);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005077 return;
5078 }
5079 if (VD->hasLocalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005080 S.Diag(AL.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005081 return;
5082 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005083 D->addAttr(::new (S.Context) ThreadAttr(AL.getRange(), S.Context,
5084 AL.getAttributeSpellingListIndex()));
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005085}
5086
Erich Keanee891aa92018-07-13 15:07:47 +00005087static void handleAbiTagAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005088 SmallVector<StringRef, 4> Tags;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005089 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005090 StringRef Tag;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005091 if (!S.checkStringLiteralArgumentAttr(AL, I, Tag))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005092 return;
5093 Tags.push_back(Tag);
5094 }
5095
5096 if (const auto *NS = dyn_cast<NamespaceDecl>(D)) {
5097 if (!NS->isInline()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005098 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 0;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005099 return;
5100 }
5101 if (NS->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005102 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 1;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005103 return;
5104 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005105 if (AL.getNumArgs() == 0)
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005106 Tags.push_back(NS->getName());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005107 } else if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005108 return;
5109
5110 // Store tags sorted and without duplicates.
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +00005111 llvm::sort(Tags.begin(), Tags.end());
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005112 Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end());
5113
5114 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005115 AbiTagAttr(AL.getRange(), S.Context, Tags.data(), Tags.size(),
5116 AL.getAttributeSpellingListIndex()));
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005117}
5118
Erich Keanee891aa92018-07-13 15:07:47 +00005119static void handleARMInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005120 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005121 if (AL.getNumArgs() > 1) {
5122 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
5123 << AL.getName() << 1;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005124 return;
5125 }
5126
5127 StringRef Str;
5128 SourceLocation ArgLoc;
5129
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005130 if (AL.getNumArgs() == 0)
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005131 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005132 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005133 return;
5134
5135 ARMInterruptAttr::InterruptType Kind;
5136 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005137 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
5138 << AL.getName() << Str << ArgLoc;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005139 return;
5140 }
5141
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005142 unsigned Index = AL.getAttributeSpellingListIndex();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005143 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005144 ARMInterruptAttr(AL.getLoc(), S.Context, Kind, Index));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005145}
5146
Erich Keanee891aa92018-07-13 15:07:47 +00005147static void handleMSP430InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005148 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005149 return;
5150
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005151 if (!AL.isArgExpr(0)) {
5152 S.Diag(AL.getLoc(), diag::err_attribute_argument_type) << AL.getName()
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005153 << AANT_ArgumentIntegerConstant;
5154 return;
5155 }
5156
5157 // FIXME: Check for decl - it should be void ()(void).
5158
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005159 Expr *NumParamsExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005160 llvm::APSInt NumParams(32);
5161 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005162 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
5163 << AL.getName() << AANT_ArgumentIntegerConstant
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005164 << NumParamsExpr->getSourceRange();
5165 return;
5166 }
5167
5168 unsigned Num = NumParams.getLimitedValue(255);
5169 if ((Num & 1) || Num > 30) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005170 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
5171 << AL.getName() << (int)NumParams.getSExtValue()
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005172 << NumParamsExpr->getSourceRange();
5173 return;
5174 }
5175
Aaron Ballman36a53502014-01-16 13:03:14 +00005176 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005177 MSP430InterruptAttr(AL.getLoc(), S.Context, Num,
5178 AL.getAttributeSpellingListIndex()));
Aaron Ballman36a53502014-01-16 13:03:14 +00005179 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005180}
5181
Erich Keanee891aa92018-07-13 15:07:47 +00005182static void handleMipsInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005183 // Only one optional argument permitted.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005184 if (AL.getNumArgs() > 1) {
5185 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
5186 << AL.getName() << 1;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005187 return;
5188 }
5189
5190 StringRef Str;
5191 SourceLocation ArgLoc;
5192
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005193 if (AL.getNumArgs() == 0)
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005194 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005195 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005196 return;
5197
5198 // Semantic checks for a function with the 'interrupt' attribute for MIPS:
5199 // a) Must be a function.
5200 // b) Must have no parameters.
5201 // c) Must have the 'void' return type.
5202 // d) Cannot have the 'mips16' attribute, as that instruction set
5203 // lacks the 'eret' instruction.
5204 // e) The attribute itself must either have no argument or one of the
5205 // valid interrupt types, see [MipsInterruptDocs].
5206
5207 if (!isFunctionOrMethod(D)) {
5208 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5209 << "'interrupt'" << ExpectedFunctionOrMethod;
5210 return;
5211 }
5212
5213 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
5214 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
5215 << 0;
5216 return;
5217 }
5218
5219 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5220 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
5221 << 1;
5222 return;
5223 }
5224
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005225 if (checkAttrMutualExclusion<Mips16Attr>(S, D, AL.getRange(),
5226 AL.getName()))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005227 return;
5228
5229 MipsInterruptAttr::InterruptType Kind;
5230 if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005231 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
5232 << AL.getName() << "'" + std::string(Str) + "'";
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005233 return;
5234 }
5235
5236 D->addAttr(::new (S.Context) MipsInterruptAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005237 AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex()));
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005238}
5239
Erich Keanee891aa92018-07-13 15:07:47 +00005240static void handleAnyX86InterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Alexey Bataevd51e9932016-01-15 04:06:31 +00005241 // Semantic checks for a function with the 'interrupt' attribute.
5242 // a) Must be a function.
5243 // b) Must have the 'void' return type.
5244 // c) Must take 1 or 2 arguments.
5245 // d) The 1st argument must be a pointer.
5246 // e) The 2nd argument (if any) must be an unsigned integer.
5247 if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) ||
5248 CXXMethodDecl::isStaticOverloadedOperator(
5249 cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005250 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
5251 << AL.getName() << ExpectedFunctionWithProtoType;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005252 return;
5253 }
5254 // Interrupt handler must have void return type.
5255 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5256 S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(),
5257 diag::err_anyx86_interrupt_attribute)
5258 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5259 ? 0
5260 : 1)
5261 << 0;
5262 return;
5263 }
5264 // Interrupt handler must have 1 or 2 parameters.
5265 unsigned NumParams = getFunctionOrMethodNumParams(D);
5266 if (NumParams < 1 || NumParams > 2) {
5267 S.Diag(D->getLocStart(), diag::err_anyx86_interrupt_attribute)
5268 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5269 ? 0
5270 : 1)
5271 << 1;
5272 return;
5273 }
5274 // The first argument must be a pointer.
5275 if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) {
5276 S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(),
5277 diag::err_anyx86_interrupt_attribute)
5278 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5279 ? 0
5280 : 1)
5281 << 2;
5282 return;
5283 }
5284 // The second argument, if present, must be an unsigned integer.
5285 unsigned TypeSize =
5286 S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64
5287 ? 64
5288 : 32;
5289 if (NumParams == 2 &&
5290 (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() ||
5291 S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) {
5292 S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(),
5293 diag::err_anyx86_interrupt_attribute)
5294 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5295 ? 0
5296 : 1)
5297 << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false);
5298 return;
5299 }
5300 D->addAttr(::new (S.Context) AnyX86InterruptAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005301 AL.getLoc(), S.Context, AL.getAttributeSpellingListIndex()));
Alexey Bataevd51e9932016-01-15 04:06:31 +00005302 D->addAttr(UsedAttr::CreateImplicit(S.Context));
5303}
5304
Erich Keanee891aa92018-07-13 15:07:47 +00005305static void handleAVRInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005306 if (!isFunctionOrMethod(D)) {
5307 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5308 << "'interrupt'" << ExpectedFunction;
5309 return;
5310 }
5311
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005312 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005313 return;
5314
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005315 handleSimpleAttribute<AVRInterruptAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005316}
5317
Erich Keanee891aa92018-07-13 15:07:47 +00005318static void handleAVRSignalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005319 if (!isFunctionOrMethod(D)) {
5320 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5321 << "'signal'" << ExpectedFunction;
5322 return;
5323 }
5324
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005325 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005326 return;
5327
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005328 handleSimpleAttribute<AVRSignalAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005329}
5330
Erich Keanee891aa92018-07-13 15:07:47 +00005331static void handleInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005332 // Dispatch the interrupt attribute based on the current target.
Alexey Bataevd51e9932016-01-15 04:06:31 +00005333 switch (S.Context.getTargetInfo().getTriple().getArch()) {
5334 case llvm::Triple::msp430:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005335 handleMSP430InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005336 break;
5337 case llvm::Triple::mipsel:
5338 case llvm::Triple::mips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005339 handleMipsInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005340 break;
5341 case llvm::Triple::x86:
5342 case llvm::Triple::x86_64:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005343 handleAnyX86InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005344 break;
Dylan McKaye8232d72017-02-08 05:09:26 +00005345 case llvm::Triple::avr:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005346 handleAVRInterruptAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005347 break;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005348 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005349 handleARMInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005350 break;
5351 }
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005352}
5353
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005354static void handleAMDGPUFlatWorkGroupSizeAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005355 const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005356 uint32_t Min = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005357 Expr *MinExpr = AL.getArgAsExpr(0);
5358 if (!checkUInt32Argument(S, AL, MinExpr, Min))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005359 return;
5360
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005361 uint32_t Max = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005362 Expr *MaxExpr = AL.getArgAsExpr(1);
5363 if (!checkUInt32Argument(S, AL, MaxExpr, Max))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005364 return;
5365
5366 if (Min == 0 && Max != 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005367 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid)
5368 << AL.getName() << 0;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005369 return;
5370 }
5371 if (Min > Max) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005372 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid)
5373 << AL.getName() << 1;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005374 return;
5375 }
5376
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005377 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005378 AMDGPUFlatWorkGroupSizeAttr(AL.getLoc(), S.Context, Min, Max,
5379 AL.getAttributeSpellingListIndex()));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005380}
5381
Erich Keanee891aa92018-07-13 15:07:47 +00005382static void handleAMDGPUWavesPerEUAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005383 uint32_t Min = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005384 Expr *MinExpr = AL.getArgAsExpr(0);
5385 if (!checkUInt32Argument(S, AL, MinExpr, Min))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005386 return;
5387
5388 uint32_t Max = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005389 if (AL.getNumArgs() == 2) {
5390 Expr *MaxExpr = AL.getArgAsExpr(1);
5391 if (!checkUInt32Argument(S, AL, MaxExpr, Max))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005392 return;
5393 }
5394
5395 if (Min == 0 && Max != 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005396 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid)
5397 << AL.getName() << 0;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005398 return;
5399 }
5400 if (Max != 0 && Min > Max) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005401 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid)
5402 << AL.getName() << 1;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005403 return;
5404 }
5405
5406 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005407 AMDGPUWavesPerEUAttr(AL.getLoc(), S.Context, Min, Max,
5408 AL.getAttributeSpellingListIndex()));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005409}
5410
Erich Keanee891aa92018-07-13 15:07:47 +00005411static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005412 uint32_t NumSGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005413 Expr *NumSGPRExpr = AL.getArgAsExpr(0);
5414 if (!checkUInt32Argument(S, AL, NumSGPRExpr, NumSGPR))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005415 return;
5416
5417 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005418 AMDGPUNumSGPRAttr(AL.getLoc(), S.Context, NumSGPR,
5419 AL.getAttributeSpellingListIndex()));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005420}
5421
Erich Keanee891aa92018-07-13 15:07:47 +00005422static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005423 uint32_t NumVGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005424 Expr *NumVGPRExpr = AL.getArgAsExpr(0);
5425 if (!checkUInt32Argument(S, AL, NumVGPRExpr, NumVGPR))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005426 return;
5427
5428 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005429 AMDGPUNumVGPRAttr(AL.getLoc(), S.Context, NumVGPR,
5430 AL.getAttributeSpellingListIndex()));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005431}
5432
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005433static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005434 const ParsedAttr &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005435 // If we try to apply it to a function pointer, don't warn, but don't
5436 // do anything, either. It doesn't matter anyway, because there's nothing
5437 // special about calling a force_align_arg_pointer function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005438 const auto *VD = dyn_cast<ValueDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005439 if (VD && VD->getType()->isFunctionPointerType())
5440 return;
5441 // Also don't warn on function pointer typedefs.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005442 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005443 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
5444 TD->getUnderlyingType()->isFunctionType()))
5445 return;
5446 // Attribute can only be applied to function types.
5447 if (!isa<FunctionDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005448 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
5449 << AL.getName() << ExpectedFunction;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005450 return;
5451 }
5452
Aaron Ballman36a53502014-01-16 13:03:14 +00005453 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005454 X86ForceAlignArgPointerAttr(AL.getRange(), S.Context,
5455 AL.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005456}
5457
Erich Keanee891aa92018-07-13 15:07:47 +00005458static void handleLayoutVersion(Sema &S, Decl *D, const ParsedAttr &AL) {
David Majnemercd3ebfe2016-05-23 17:16:12 +00005459 uint32_t Version;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005460 Expr *VersionExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
5461 if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), Version))
David Majnemercd3ebfe2016-05-23 17:16:12 +00005462 return;
5463
5464 // TODO: Investigate what happens with the next major version of MSVC.
5465 if (Version != LangOptions::MSVC2015) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005466 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
5467 << AL.getName() << Version << VersionExpr->getSourceRange();
David Majnemercd3ebfe2016-05-23 17:16:12 +00005468 return;
5469 }
5470
5471 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005472 LayoutVersionAttr(AL.getRange(), S.Context, Version,
5473 AL.getAttributeSpellingListIndex()));
David Majnemercd3ebfe2016-05-23 17:16:12 +00005474}
5475
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005476DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
5477 unsigned AttrSpellingListIndex) {
5478 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00005479 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00005480 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005481 }
5482
5483 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00005484 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005485
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00005486 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005487}
5488
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005489DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
5490 unsigned AttrSpellingListIndex) {
5491 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00005492 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005493 D->dropAttr<DLLImportAttr>();
5494 }
5495
5496 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00005497 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005498
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00005499 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005500}
5501
Erich Keanee891aa92018-07-13 15:07:47 +00005502static void handleDLLAttr(Sema &S, Decl *D, const ParsedAttr &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00005503 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
5504 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
5505 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored)
5506 << A.getName();
5507 return;
5508 }
5509
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005510 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Erich Keanee891aa92018-07-13 15:07:47 +00005511 if (FD->isInlined() && A.getKind() == ParsedAttr::AT_DLLImport &&
Hans Wennborg606bd6d2014-11-03 14:24:45 +00005512 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
5513 // MinGW doesn't allow dllimport on inline functions.
5514 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
5515 << A.getName();
5516 return;
5517 }
5518 }
5519
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005520 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
Hans Wennborg5869ec42015-09-15 21:05:30 +00005521 if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
5522 MD->getParent()->isLambda()) {
5523 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A.getName();
5524 return;
5525 }
5526 }
5527
Hans Wennborge82f19c2014-06-24 23:57:05 +00005528 unsigned Index = A.getAttributeSpellingListIndex();
Erich Keanee891aa92018-07-13 15:07:47 +00005529 Attr *NewAttr = A.getKind() == ParsedAttr::AT_DLLExport
Hans Wennborge82f19c2014-06-24 23:57:05 +00005530 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
5531 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005532 if (NewAttr)
5533 D->addAttr(NewAttr);
5534}
5535
David Majnemer2c4e00a2014-01-29 22:07:36 +00005536MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00005537Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00005538 unsigned AttrSpellingListIndex,
5539 MSInheritanceAttr::Spelling SemanticSpelling) {
5540 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
5541 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00005542 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005543 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
5544 << 1 /*previous declaration*/;
5545 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
5546 D->dropAttr<MSInheritanceAttr>();
5547 }
5548
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005549 auto *RD = cast<CXXRecordDecl>(D);
David Majnemer2c4e00a2014-01-29 22:07:36 +00005550 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00005551 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
5552 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00005553 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005554 }
5555 } else {
5556 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
5557 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
5558 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00005559 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005560 }
5561 if (RD->getDescribedClassTemplate()) {
5562 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
5563 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00005564 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005565 }
5566 }
5567
5568 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00005569 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00005570}
5571
Erich Keanee891aa92018-07-13 15:07:47 +00005572static void handleCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005573 // The capability attributes take a single string parameter for the name of
5574 // the capability they represent. The lockable attribute does not take any
5575 // parameters. However, semantically, both attributes represent the same
5576 // concept, and so they use the same semantic attribute. Eventually, the
5577 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00005578 //
Alp Toker958027b2014-07-14 19:42:55 +00005579 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00005580 // literal will be considered a "mutex."
5581 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005582 SourceLocation LiteralLoc;
Erich Keanee891aa92018-07-13 15:07:47 +00005583 if (AL.getKind() == ParsedAttr::AT_Capability &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005584 !S.checkStringLiteralArgumentAttr(AL, 0, N, &LiteralLoc))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005585 return;
5586
Aaron Ballman6c810072014-03-05 21:47:13 +00005587 // Currently, there are only two names allowed for a capability: role and
5588 // mutex (case insensitive). Diagnose other capability names.
5589 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
5590 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
5591
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005592 D->addAttr(::new (S.Context) CapabilityAttr(AL.getRange(), S.Context, N,
5593 AL.getAttributeSpellingListIndex()));
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005594}
5595
Erich Keanee891aa92018-07-13 15:07:47 +00005596static void handleAssertCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Josh Gaoec1369e2017-08-08 19:44:34 +00005597 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005598 if (!checkLockFunAttrCommon(S, D, AL, Args))
Josh Gaoec1369e2017-08-08 19:44:34 +00005599 return;
5600
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005601 D->addAttr(::new (S.Context) AssertCapabilityAttr(AL.getRange(), S.Context,
Josh Gaoec1369e2017-08-08 19:44:34 +00005602 Args.data(), Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005603 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005604}
5605
5606static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005607 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005608 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005609 if (!checkLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005610 return;
5611
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005612 D->addAttr(::new (S.Context) AcquireCapabilityAttr(AL.getRange(),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005613 S.Context,
5614 Args.data(), Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005615 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005616}
5617
5618static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005619 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005620 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005621 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005622 return;
5623
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005624 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(AL.getRange(),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005625 S.Context,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005626 AL.getArgAsExpr(0),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005627 Args.data(),
5628 Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005629 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005630}
5631
5632static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005633 const ParsedAttr &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005634 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00005635 SmallVector<Expr *, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005636 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005637
Aaron Ballman18d85ae2014-03-20 16:02:49 +00005638 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005639 AL.getRange(), S.Context, Args.data(), Args.size(),
5640 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005641}
5642
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005643static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005644 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005645 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005646 return;
5647
5648 // check that all arguments are lockable objects
5649 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005650 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005651 if (Args.empty())
5652 return;
5653
5654 RequiresCapabilityAttr *RCA = ::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005655 RequiresCapabilityAttr(AL.getRange(), S.Context, Args.data(),
5656 Args.size(), AL.getAttributeSpellingListIndex());
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005657
5658 D->addAttr(RCA);
5659}
5660
Erich Keanee891aa92018-07-13 15:07:47 +00005661static void handleDeprecatedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005662 if (const auto *NSD = dyn_cast<NamespaceDecl>(D)) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005663 if (NSD->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005664 S.Diag(AL.getLoc(), diag::warn_deprecated_anonymous_namespace);
Aaron Ballman43f40102014-11-14 22:34:56 +00005665 // Do not want to attach the attribute to the namespace because that will
5666 // cause confusing diagnostic reports for uses of declarations within the
5667 // namespace.
5668 return;
5669 }
5670 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00005671
Manman Renc7890fe2016-03-16 18:50:49 +00005672 // Handle the cases where the attribute has a text message.
5673 StringRef Str, Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005674 if (AL.isArgExpr(0) && AL.getArgAsExpr(0) &&
5675 !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Manman Renc7890fe2016-03-16 18:50:49 +00005676 return;
5677
5678 // Only support a single optional message for Declspec and CXX11.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005679 if (AL.isDeclspecAttribute() || AL.isCXX11Attribute())
5680 checkAttributeAtMostNumArgs(S, AL, 1);
5681 else if (AL.isArgExpr(1) && AL.getArgAsExpr(1) &&
5682 !S.checkStringLiteralArgumentAttr(AL, 1, Replacement))
Manman Renc7890fe2016-03-16 18:50:49 +00005683 return;
5684
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00005685 if (!S.getLangOpts().CPlusPlus14)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005686 if (AL.isCXX11Attribute() &&
5687 !(AL.hasScope() && AL.getScopeName()->isStr("gnu")))
5688 S.Diag(AL.getLoc(), diag::ext_cxx14_attr) << AL.getName();
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00005689
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005690 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005691 DeprecatedAttr(AL.getRange(), S.Context, Str, Replacement,
5692 AL.getAttributeSpellingListIndex()));
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005693}
5694
5695static bool isGlobalVar(const Decl *D) {
5696 if (const auto *S = dyn_cast<VarDecl>(D))
5697 return S->hasGlobalStorage();
5698 return false;
Aaron Ballman43f40102014-11-14 22:34:56 +00005699}
5700
Erich Keanee891aa92018-07-13 15:07:47 +00005701static void handleNoSanitizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005702 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Peter Collingbourne915df992015-05-15 18:33:32 +00005703 return;
5704
Benjamin Kramer1b582012016-02-13 18:11:49 +00005705 std::vector<StringRef> Sanitizers;
Peter Collingbourne915df992015-05-15 18:33:32 +00005706
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005707 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Peter Collingbourne915df992015-05-15 18:33:32 +00005708 StringRef SanitizerName;
5709 SourceLocation LiteralLoc;
5710
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005711 if (!S.checkStringLiteralArgumentAttr(AL, I, SanitizerName, &LiteralLoc))
Peter Collingbourne915df992015-05-15 18:33:32 +00005712 return;
5713
5714 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0)
5715 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005716 else if (isGlobalVar(D) && SanitizerName != "address")
5717 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005718 << AL.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne915df992015-05-15 18:33:32 +00005719 Sanitizers.push_back(SanitizerName);
5720 }
5721
5722 D->addAttr(::new (S.Context) NoSanitizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005723 AL.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(),
5724 AL.getAttributeSpellingListIndex()));
Peter Collingbourne915df992015-05-15 18:33:32 +00005725}
5726
5727static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005728 const ParsedAttr &AL) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005729 StringRef AttrName = AL.getName()->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00005730 normalizeName(AttrName);
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005731 StringRef SanitizerName = llvm::StringSwitch<StringRef>(AttrName)
5732 .Case("no_address_safety_analysis", "address")
5733 .Case("no_sanitize_address", "address")
5734 .Case("no_sanitize_thread", "thread")
5735 .Case("no_sanitize_memory", "memory");
5736 if (isGlobalVar(D) && SanitizerName != "address")
5737 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005738 << AL.getName() << ExpectedFunction;
Peter Collingbourne915df992015-05-15 18:33:32 +00005739 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005740 NoSanitizeAttr(AL.getRange(), S.Context, &SanitizerName, 1,
5741 AL.getAttributeSpellingListIndex()));
Peter Collingbourne915df992015-05-15 18:33:32 +00005742}
5743
Erich Keanee891aa92018-07-13 15:07:47 +00005744static void handleInternalLinkageAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005745 if (InternalLinkageAttr *Internal =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005746 S.mergeInternalLinkageAttr(D, AL.getRange(), AL.getName(),
5747 AL.getAttributeSpellingListIndex()))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005748 D->addAttr(Internal);
5749}
5750
Erich Keanee891aa92018-07-13 15:07:47 +00005751static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005752 if (S.LangOpts.OpenCLVersion != 200)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005753 S.Diag(AL.getLoc(), diag::err_attribute_requires_opencl_version)
5754 << AL.getName() << "2.0" << 0;
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005755 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005756 S.Diag(AL.getLoc(), diag::warn_opencl_attr_deprecated_ignored)
5757 << AL.getName() << "2.0";
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005758}
5759
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005760/// Handles semantic checking for features that are common to all attributes,
5761/// such as checking whether a parameter was properly specified, or the correct
5762/// number of arguments were passed, etc.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005763static bool handleCommonAttributeFeatures(Sema &S, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005764 const ParsedAttr &AL) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005765 // Several attributes carry different semantics than the parsing requires, so
Alex Lorenz24952fb2017-04-19 15:52:11 +00005766 // those are opted out of the common argument checks.
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005767 //
5768 // We also bail on unknown and ignored attributes because those are handled
5769 // as part of the target-specific handling logic.
Erich Keanee891aa92018-07-13 15:07:47 +00005770 if (AL.getKind() == ParsedAttr::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005771 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005772 // Check whether the attribute requires specific language extensions to be
5773 // enabled.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005774 if (!AL.diagnoseLangOpts(S))
Aaron Ballman3aff6332013-12-02 19:30:36 +00005775 return true;
Alex Lorenz24952fb2017-04-19 15:52:11 +00005776 // Check whether the attribute appertains to the given subject.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005777 if (!AL.diagnoseAppertainsTo(S, D))
Alex Lorenz24952fb2017-04-19 15:52:11 +00005778 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005779 if (AL.hasCustomParsing())
Alex Lorenz24952fb2017-04-19 15:52:11 +00005780 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005781
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005782 if (AL.getMinArgs() == AL.getMaxArgs()) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005783 // If there are no optional arguments, then checking for the argument count
5784 // is trivial.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005785 if (!checkAttributeNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005786 return true;
5787 } else {
5788 // There are optional arguments, so checking is slightly more involved.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005789 if (AL.getMinArgs() &&
5790 !checkAttributeAtLeastNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005791 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005792 else if (!AL.hasVariadicArg() && AL.getMaxArgs() &&
5793 !checkAttributeAtMostNumArgs(S, AL, AL.getMaxArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005794 return true;
5795 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005796
Oren Ben Simhon220671a2018-03-17 13:31:35 +00005797 if (S.CheckAttrTarget(AL))
5798 return true;
5799
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005800 return false;
5801}
5802
Erich Keanee891aa92018-07-13 15:07:47 +00005803static void handleOpenCLAccessAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00005804 if (D->isInvalidDecl())
5805 return;
5806
5807 // Check if there is only one access qualifier.
5808 if (D->hasAttr<OpenCLAccessAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005809 S.Diag(AL.getLoc(), diag::err_opencl_multiple_access_qualifiers)
Xiuli Pan11e13f62016-02-26 03:13:03 +00005810 << D->getSourceRange();
5811 D->setInvalidDecl(true);
5812 return;
5813 }
5814
5815 // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that an
5816 // image object can be read and written.
5817 // OpenCL v2.0 s6.13.6 - A kernel cannot read from and write to the same pipe
5818 // object. Using the read_write (or __read_write) qualifier with the pipe
5819 // qualifier is a compilation error.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005820 if (const auto *PDecl = dyn_cast<ParmVarDecl>(D)) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00005821 const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005822 if (AL.getName()->getName().find("read_write") != StringRef::npos) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00005823 if (S.getLangOpts().OpenCLVersion < 200 || DeclTy->isPipeType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005824 S.Diag(AL.getLoc(), diag::err_opencl_invalid_read_write)
5825 << AL.getName() << PDecl->getType() << DeclTy->isImageType();
Xiuli Pan11e13f62016-02-26 03:13:03 +00005826 D->setInvalidDecl(true);
5827 return;
5828 }
5829 }
5830 }
5831
5832 D->addAttr(::new (S.Context) OpenCLAccessAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005833 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Xiuli Pan11e13f62016-02-26 03:13:03 +00005834}
5835
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005836//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005837// Top Level Sema Entry Points
5838//===----------------------------------------------------------------------===//
5839
Richard Smithf8a75c32013-08-29 00:47:48 +00005840/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
5841/// the attribute applies to decls. If the attribute is a type attribute, just
5842/// silently ignore it if a GNU attribute.
5843static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
Erich Keanee891aa92018-07-13 15:07:47 +00005844 const ParsedAttr &AL,
Richard Smithf8a75c32013-08-29 00:47:48 +00005845 bool IncludeCXX11Attributes) {
Erich Keanee891aa92018-07-13 15:07:47 +00005846 if (AL.isInvalid() || AL.getKind() == ParsedAttr::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00005847 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00005848
Richard Smithf8a75c32013-08-29 00:47:48 +00005849 // Ignore C++11 attributes on declarator chunks: they appertain to the type
5850 // instead.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005851 if (AL.isCXX11Attribute() && !IncludeCXX11Attributes)
Richard Smithf8a75c32013-08-29 00:47:48 +00005852 return;
5853
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005854 // Unknown attributes are automatically warned on. Target-specific attributes
5855 // which do not apply to the current target architecture are treated as
5856 // though they were unknown attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00005857 if (AL.getKind() == ParsedAttr::UnknownAttribute ||
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005858 !AL.existsInTarget(S.Context.getTargetInfo())) {
5859 S.Diag(AL.getLoc(), AL.isDeclspecAttribute()
Erich Keanec480f302018-07-12 21:09:05 +00005860 ? diag::warn_unhandled_ms_attribute_ignored
5861 : diag::warn_unknown_attribute_ignored)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005862 << AL.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005863 return;
5864 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005865
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005866 if (handleCommonAttributeFeatures(S, D, AL))
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005867 return;
5868
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005869 switch (AL.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005870 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005871 if (!AL.isStmtAttr()) {
Richard Smith4f902c72016-03-08 00:32:55 +00005872 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005873 assert(AL.isTypeAttr() && "Non-type attribute not handled");
Richard Smith4f902c72016-03-08 00:32:55 +00005874 break;
5875 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005876 S.Diag(AL.getLoc(), diag::err_stmt_attribute_invalid_on_decl)
5877 << AL.getName() << D->getLocation();
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005878 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005879 case ParsedAttr::AT_Interrupt:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005880 handleInterruptAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005881 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005882 case ParsedAttr::AT_X86ForceAlignArgPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005883 handleX86ForceAlignArgPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005884 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005885 case ParsedAttr::AT_DLLExport:
5886 case ParsedAttr::AT_DLLImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005887 handleDLLAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005888 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005889 case ParsedAttr::AT_Mips16:
Simon Atanasyan2c87f532017-05-22 12:47:43 +00005890 handleSimpleAttributeWithExclusions<Mips16Attr, MicroMipsAttr,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005891 MipsInterruptAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005892 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005893 case ParsedAttr::AT_NoMips16:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005894 handleSimpleAttribute<NoMips16Attr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005895 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005896 case ParsedAttr::AT_MicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005897 handleSimpleAttributeWithExclusions<MicroMipsAttr, Mips16Attr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00005898 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005899 case ParsedAttr::AT_NoMicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005900 handleSimpleAttribute<NoMicroMipsAttr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00005901 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005902 case ParsedAttr::AT_MipsLongCall:
Simon Atanasyan1a116db2017-07-20 20:34:18 +00005903 handleSimpleAttributeWithExclusions<MipsLongCallAttr, MipsShortCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005904 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00005905 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005906 case ParsedAttr::AT_MipsShortCall:
Simon Atanasyan1a116db2017-07-20 20:34:18 +00005907 handleSimpleAttributeWithExclusions<MipsShortCallAttr, MipsLongCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005908 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00005909 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005910 case ParsedAttr::AT_AMDGPUFlatWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005911 handleAMDGPUFlatWorkGroupSizeAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005912 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005913 case ParsedAttr::AT_AMDGPUWavesPerEU:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005914 handleAMDGPUWavesPerEUAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005915 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005916 case ParsedAttr::AT_AMDGPUNumSGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005917 handleAMDGPUNumSGPRAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005918 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005919 case ParsedAttr::AT_AMDGPUNumVGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005920 handleAMDGPUNumVGPRAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005921 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005922 case ParsedAttr::AT_AVRSignal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005923 handleAVRSignalAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005924 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005925 case ParsedAttr::AT_IBAction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005926 handleSimpleAttribute<IBActionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005927 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005928 case ParsedAttr::AT_IBOutlet:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005929 handleIBOutlet(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005930 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005931 case ParsedAttr::AT_IBOutletCollection:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005932 handleIBOutletCollection(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005933 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005934 case ParsedAttr::AT_IFunc:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005935 handleIFuncAttr(S, D, AL);
Dmitry Polukhin85eda122016-04-11 07:48:59 +00005936 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005937 case ParsedAttr::AT_Alias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005938 handleAliasAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005939 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005940 case ParsedAttr::AT_Aligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005941 handleAlignedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005942 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005943 case ParsedAttr::AT_AlignValue:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005944 handleAlignValueAttr(S, D, AL);
Hal Finkel1b0d24e2014-10-02 21:21:25 +00005945 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005946 case ParsedAttr::AT_AllocSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005947 handleAllocSizeAttr(S, D, AL);
George Burgess IVe3763372016-12-22 02:50:20 +00005948 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005949 case ParsedAttr::AT_AlwaysInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005950 handleAlwaysInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005951 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005952 case ParsedAttr::AT_Artificial:
Aaron Ballman736c09b2018-02-15 16:28:10 +00005953 handleSimpleAttribute<ArtificialAttr>(S, D, AL);
Erich Keane293a0552018-02-14 00:14:07 +00005954 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005955 case ParsedAttr::AT_AnalyzerNoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005956 handleAnalyzerNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005957 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005958 case ParsedAttr::AT_TLSModel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005959 handleTLSModelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005960 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005961 case ParsedAttr::AT_Annotate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005962 handleAnnotateAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005963 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005964 case ParsedAttr::AT_Availability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005965 handleAvailabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005966 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005967 case ParsedAttr::AT_CarriesDependency:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005968 handleDependencyAttr(S, scope, D, AL);
Richard Smithe233fbf2013-01-28 22:42:45 +00005969 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005970 case ParsedAttr::AT_Common:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005971 handleCommonAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005972 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005973 case ParsedAttr::AT_CUDAConstant:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005974 handleConstantAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005975 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005976 case ParsedAttr::AT_PassObjectSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005977 handlePassObjectSizeAttr(S, D, AL);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00005978 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005979 case ParsedAttr::AT_Constructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005980 handleConstructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005981 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005982 case ParsedAttr::AT_CXX11NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005983 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005984 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005985 case ParsedAttr::AT_Deprecated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005986 handleDeprecatedAttr(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00005987 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005988 case ParsedAttr::AT_Destructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005989 handleDestructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005990 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005991 case ParsedAttr::AT_EnableIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005992 handleEnableIfAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005993 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005994 case ParsedAttr::AT_DiagnoseIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005995 handleDiagnoseIfAttr(S, D, AL);
George Burgess IV177399e2017-01-09 04:12:14 +00005996 break;
Erich Keanee891aa92018-07-13 15:07:47 +00005997 case ParsedAttr::AT_ExtVectorType:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005998 handleExtVectorTypeAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005999 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006000 case ParsedAttr::AT_ExternalSourceSymbol:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006001 handleExternalSourceSymbolAttr(S, D, AL);
Alex Lorenzd5d27e12017-03-01 18:06:25 +00006002 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006003 case ParsedAttr::AT_MinSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006004 handleMinSizeAttr(S, D, AL);
Quentin Colombet4e172062012-11-01 23:55:47 +00006005 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006006 case ParsedAttr::AT_OptimizeNone:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006007 handleOptimizeNoneAttr(S, D, AL);
Paul Robinsonf0674352014-03-31 22:29:15 +00006008 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006009 case ParsedAttr::AT_FlagEnum:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006010 handleSimpleAttribute<FlagEnumAttr>(S, D, AL);
Alexis Hunt724f14e2014-11-28 00:53:20 +00006011 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006012 case ParsedAttr::AT_EnumExtensibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006013 handleEnumExtensibilityAttr(S, D, AL);
Akira Hatanaka3c268af2017-03-21 02:23:00 +00006014 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006015 case ParsedAttr::AT_Flatten:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006016 handleSimpleAttribute<FlattenAttr>(S, D, AL);
Peter Collingbourne41af7c22014-05-20 17:12:51 +00006017 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006018 case ParsedAttr::AT_Format:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006019 handleFormatAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006020 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006021 case ParsedAttr::AT_FormatArg:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006022 handleFormatArgAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006023 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006024 case ParsedAttr::AT_CUDAGlobal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006025 handleGlobalAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006026 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006027 case ParsedAttr::AT_CUDADevice:
Justin Lebar3eaaf862016-01-13 01:07:35 +00006028 handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006029 AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006030 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006031 case ParsedAttr::AT_CUDAHost:
Erich Keanec480f302018-07-12 21:09:05 +00006032 handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006033 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006034 case ParsedAttr::AT_GNUInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006035 handleGNUInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006036 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006037 case ParsedAttr::AT_CUDALaunchBounds:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006038 handleLaunchBoundsAttr(S, D, AL);
Peter Collingbourne827301e2010-12-12 23:03:07 +00006039 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006040 case ParsedAttr::AT_Restrict:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006041 handleRestrictAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006042 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006043 case ParsedAttr::AT_MayAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006044 handleSimpleAttribute<MayAliasAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006045 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006046 case ParsedAttr::AT_Mode:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006047 handleModeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006048 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006049 case ParsedAttr::AT_NoAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006050 handleSimpleAttribute<NoAliasAttr>(S, D, AL);
David Majnemer1bf0f8e2015-07-20 22:51:52 +00006051 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006052 case ParsedAttr::AT_NoCommon:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006053 handleSimpleAttribute<NoCommonAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006054 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006055 case ParsedAttr::AT_NoSplitStack:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006056 handleSimpleAttribute<NoSplitStackAttr>(S, D, AL);
Peter Collingbourneb4728c12014-05-19 22:14:34 +00006057 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006058 case ParsedAttr::AT_NonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006059 if (auto *PVD = dyn_cast<ParmVarDecl>(D))
6060 handleNonNullAttrParameter(S, PVD, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006061 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006062 handleNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006063 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006064 case ParsedAttr::AT_ReturnsNonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006065 handleReturnsNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006066 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006067 case ParsedAttr::AT_NoEscape:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006068 handleNoEscapeAttr(S, D, AL);
Akira Hatanaka98a49332017-09-22 00:41:05 +00006069 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006070 case ParsedAttr::AT_AssumeAligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006071 handleAssumeAlignedAttr(S, D, AL);
Hal Finkelee90a222014-09-26 05:04:30 +00006072 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006073 case ParsedAttr::AT_AllocAlign:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006074 handleAllocAlignAttr(S, D, AL);
Erich Keane623efd82017-03-30 21:48:55 +00006075 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006076 case ParsedAttr::AT_Overloadable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006077 handleSimpleAttribute<OverloadableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006078 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006079 case ParsedAttr::AT_Ownership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006080 handleOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006081 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006082 case ParsedAttr::AT_Cold:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006083 handleSimpleAttributeWithExclusions<ColdAttr, HotAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006084 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006085 case ParsedAttr::AT_Hot:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006086 handleSimpleAttributeWithExclusions<HotAttr, ColdAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006087 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006088 case ParsedAttr::AT_Naked:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006089 handleNakedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006090 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006091 case ParsedAttr::AT_NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006092 handleNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006093 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006094 case ParsedAttr::AT_AnyX86NoCfCheck:
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006095 handleNoCfCheckAttr(S, D, AL);
6096 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006097 case ParsedAttr::AT_NoThrow:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006098 handleSimpleAttribute<NoThrowAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006099 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006100 case ParsedAttr::AT_CUDAShared:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006101 handleSharedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006102 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006103 case ParsedAttr::AT_VecReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006104 handleVecReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006105 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006106 case ParsedAttr::AT_ObjCOwnership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006107 handleObjCOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006108 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006109 case ParsedAttr::AT_ObjCPreciseLifetime:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006110 handleObjCPreciseLifetimeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006111 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006112 case ParsedAttr::AT_ObjCReturnsInnerPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006113 handleObjCReturnsInnerPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006114 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006115 case ParsedAttr::AT_ObjCRequiresSuper:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006116 handleObjCRequiresSuperAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006117 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006118 case ParsedAttr::AT_ObjCBridge:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006119 handleObjCBridgeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006120 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006121 case ParsedAttr::AT_ObjCBridgeMutable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006122 handleObjCBridgeMutableAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006123 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006124 case ParsedAttr::AT_ObjCBridgeRelated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006125 handleObjCBridgeRelatedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006126 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006127 case ParsedAttr::AT_ObjCDesignatedInitializer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006128 handleObjCDesignatedInitializer(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006129 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006130 case ParsedAttr::AT_ObjCRuntimeName:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006131 handleObjCRuntimeName(S, D, AL);
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00006132 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006133 case ParsedAttr::AT_ObjCRuntimeVisible:
6134 handleSimpleAttribute<ObjCRuntimeVisibleAttr>(S, D, AL);
6135 break;
6136 case ParsedAttr::AT_ObjCBoxable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006137 handleObjCBoxable(S, D, AL);
Alex Denisovfde64952015-06-26 05:28:36 +00006138 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006139 case ParsedAttr::AT_CFAuditedTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006140 handleSimpleAttributeWithExclusions<CFAuditedTransferAttr,
6141 CFUnknownTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006142 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006143 case ParsedAttr::AT_CFUnknownTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006144 handleSimpleAttributeWithExclusions<CFUnknownTransferAttr,
6145 CFAuditedTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006146 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006147 case ParsedAttr::AT_CFConsumed:
6148 case ParsedAttr::AT_NSConsumed:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006149 handleNSConsumedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006150 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006151 case ParsedAttr::AT_NSConsumesSelf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006152 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006153 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006154 case ParsedAttr::AT_NSReturnsAutoreleased:
6155 case ParsedAttr::AT_NSReturnsNotRetained:
6156 case ParsedAttr::AT_CFReturnsNotRetained:
6157 case ParsedAttr::AT_NSReturnsRetained:
6158 case ParsedAttr::AT_CFReturnsRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006159 handleNSReturnsRetainedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006160 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006161 case ParsedAttr::AT_WorkGroupSizeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006162 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006163 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006164 case ParsedAttr::AT_ReqdWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006165 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006166 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006167 case ParsedAttr::AT_OpenCLIntelReqdSubGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006168 handleSubGroupSize(S, D, AL);
Xiuli Panbe6da4b2017-05-04 07:31:20 +00006169 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006170 case ParsedAttr::AT_VecTypeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006171 handleVecTypeHint(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006172 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006173 case ParsedAttr::AT_RequireConstantInit:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006174 handleSimpleAttribute<RequireConstantInitAttr>(S, D, AL);
Eric Fiselier341e8252016-09-02 18:53:31 +00006175 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006176 case ParsedAttr::AT_InitPriority:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006177 handleInitPriorityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006178 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006179 case ParsedAttr::AT_Packed:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006180 handlePackedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006181 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006182 case ParsedAttr::AT_Section:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006183 handleSectionAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006184 break;
Erich Keane7963e8b2018-07-18 20:04:48 +00006185 case ParsedAttr::AT_CodeSeg:
6186 handleCodeSegAttr(S, D, AL);
6187 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006188 case ParsedAttr::AT_Target:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006189 handleTargetAttr(S, D, AL);
Eric Christopher11acf732015-06-12 01:35:52 +00006190 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006191 case ParsedAttr::AT_MinVectorWidth:
Craig Topper74c10e32018-07-09 19:00:16 +00006192 handleMinVectorWidthAttr(S, D, AL);
6193 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006194 case ParsedAttr::AT_Unavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006195 handleAttrWithMessage<UnavailableAttr>(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006196 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006197 case ParsedAttr::AT_ArcWeakrefUnavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006198 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006199 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006200 case ParsedAttr::AT_ObjCRootClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006201 handleSimpleAttribute<ObjCRootClassAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006202 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006203 case ParsedAttr::AT_ObjCSubclassingRestricted:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006204 handleSimpleAttribute<ObjCSubclassingRestrictedAttr>(S, D, AL);
Alex Lorenza8c44ba2016-10-28 10:25:10 +00006205 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006206 case ParsedAttr::AT_ObjCExplicitProtocolImpl:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006207 handleObjCSuppresProtocolAttr(S, D, AL);
Ted Kremenek28eace62013-11-23 01:01:34 +00006208 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006209 case ParsedAttr::AT_ObjCRequiresPropertyDefs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006210 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006211 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006212 case ParsedAttr::AT_Unused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006213 handleUnusedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006214 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006215 case ParsedAttr::AT_ReturnsTwice:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006216 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006217 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006218 case ParsedAttr::AT_NotTailCalled:
Erich Keanec480f302018-07-12 21:09:05 +00006219 handleSimpleAttributeWithExclusions<NotTailCalledAttr, AlwaysInlineAttr>(
6220 S, D, AL);
Akira Hatanakac8667622015-11-06 23:56:15 +00006221 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006222 case ParsedAttr::AT_DisableTailCalls:
Erich Keanec480f302018-07-12 21:09:05 +00006223 handleSimpleAttributeWithExclusions<DisableTailCallsAttr, NakedAttr>(S, D,
6224 AL);
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00006225 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006226 case ParsedAttr::AT_Used:
Aaron Ballman1a3901c2018-03-03 21:02:09 +00006227 handleSimpleAttribute<UsedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006228 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006229 case ParsedAttr::AT_Visibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006230 handleVisibilityAttr(S, D, AL, false);
John McCalld041a9b2013-02-20 01:54:26 +00006231 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006232 case ParsedAttr::AT_TypeVisibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006233 handleVisibilityAttr(S, D, AL, true);
John McCalld041a9b2013-02-20 01:54:26 +00006234 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006235 case ParsedAttr::AT_WarnUnused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006236 handleSimpleAttribute<WarnUnusedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006237 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006238 case ParsedAttr::AT_WarnUnusedResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006239 handleWarnUnusedResult(S, D, AL);
Chris Lattner237f2752009-02-14 07:37:35 +00006240 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006241 case ParsedAttr::AT_Weak:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006242 handleSimpleAttribute<WeakAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006243 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006244 case ParsedAttr::AT_WeakRef:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006245 handleWeakRefAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006246 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006247 case ParsedAttr::AT_WeakImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006248 handleWeakImportAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006249 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006250 case ParsedAttr::AT_TransparentUnion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006251 handleTransparentUnionAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006252 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006253 case ParsedAttr::AT_ObjCException:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006254 handleSimpleAttribute<ObjCExceptionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006255 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006256 case ParsedAttr::AT_ObjCMethodFamily:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006257 handleObjCMethodFamilyAttr(S, D, AL);
John McCall86bc21f2011-03-02 11:33:24 +00006258 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006259 case ParsedAttr::AT_ObjCNSObject:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006260 handleObjCNSObject(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006261 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006262 case ParsedAttr::AT_ObjCIndependentClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006263 handleObjCIndependentClass(S, D, AL);
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00006264 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006265 case ParsedAttr::AT_Blocks:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006266 handleBlocksAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006267 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006268 case ParsedAttr::AT_Sentinel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006269 handleSentinelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006270 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006271 case ParsedAttr::AT_Const:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006272 handleSimpleAttribute<ConstAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006273 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006274 case ParsedAttr::AT_Pure:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006275 handleSimpleAttribute<PureAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006276 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006277 case ParsedAttr::AT_Cleanup:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006278 handleCleanupAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006279 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006280 case ParsedAttr::AT_NoDebug:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006281 handleNoDebugAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006282 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006283 case ParsedAttr::AT_NoDuplicate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006284 handleSimpleAttribute<NoDuplicateAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006285 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006286 case ParsedAttr::AT_Convergent:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006287 handleSimpleAttribute<ConvergentAttr>(S, D, AL);
Yaxun Liu7d07ae72016-11-01 18:45:32 +00006288 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006289 case ParsedAttr::AT_NoInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006290 handleSimpleAttribute<NoInlineAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006291 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006292 case ParsedAttr::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006293 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006294 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006295 case ParsedAttr::AT_NoStackProtector:
Manoj Gupta4fbf84c2018-05-09 21:41:18 +00006296 // Interacts with -fstack-protector options.
6297 handleSimpleAttribute<NoStackProtectorAttr>(S, D, AL);
6298 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006299 case ParsedAttr::AT_StdCall:
6300 case ParsedAttr::AT_CDecl:
6301 case ParsedAttr::AT_FastCall:
6302 case ParsedAttr::AT_ThisCall:
6303 case ParsedAttr::AT_Pascal:
6304 case ParsedAttr::AT_RegCall:
6305 case ParsedAttr::AT_SwiftCall:
6306 case ParsedAttr::AT_VectorCall:
6307 case ParsedAttr::AT_MSABI:
6308 case ParsedAttr::AT_SysVABI:
6309 case ParsedAttr::AT_Pcs:
6310 case ParsedAttr::AT_IntelOclBicc:
6311 case ParsedAttr::AT_PreserveMost:
6312 case ParsedAttr::AT_PreserveAll:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006313 handleCallConvAttr(S, D, AL);
John McCallab26cfa2010-02-05 21:31:56 +00006314 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006315 case ParsedAttr::AT_Suppress:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006316 handleSuppressAttr(S, D, AL);
Matthias Gehre01a63382017-03-27 19:45:24 +00006317 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006318 case ParsedAttr::AT_OpenCLKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006319 handleSimpleAttribute<OpenCLKernelAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006320 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006321 case ParsedAttr::AT_OpenCLAccess:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006322 handleOpenCLAccessAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006323 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006324 case ParsedAttr::AT_OpenCLNoSVM:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006325 handleOpenCLNoSVMAttr(S, D, AL);
Anastasia Stulovafde76222016-04-01 16:05:09 +00006326 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006327 case ParsedAttr::AT_SwiftContext:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006328 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftContext);
John McCall477f2bb2016-03-03 06:39:32 +00006329 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006330 case ParsedAttr::AT_SwiftErrorResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006331 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftErrorResult);
John McCall477f2bb2016-03-03 06:39:32 +00006332 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006333 case ParsedAttr::AT_SwiftIndirectResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006334 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftIndirectResult);
John McCall477f2bb2016-03-03 06:39:32 +00006335 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006336 case ParsedAttr::AT_InternalLinkage:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006337 handleInternalLinkageAttr(S, D, AL);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00006338 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006339 case ParsedAttr::AT_LTOVisibilityPublic:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006340 handleSimpleAttribute<LTOVisibilityPublicAttr>(S, D, AL);
Peter Collingbourne3afb2662016-04-28 17:09:37 +00006341 break;
John McCall8d32c052012-05-22 21:28:12 +00006342
6343 // Microsoft attributes:
Erich Keanee891aa92018-07-13 15:07:47 +00006344 case ParsedAttr::AT_EmptyBases:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006345 handleSimpleAttribute<EmptyBasesAttr>(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00006346 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006347 case ParsedAttr::AT_LayoutVersion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006348 handleLayoutVersion(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00006349 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006350 case ParsedAttr::AT_TrivialABI:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006351 handleSimpleAttribute<TrivialABIAttr>(S, D, AL);
Akira Hatanaka02914dc2018-02-05 20:23:22 +00006352 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006353 case ParsedAttr::AT_MSNoVTable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006354 handleSimpleAttribute<MSNoVTableAttr>(S, D, AL);
David Majnemer129f4172015-02-02 10:22:20 +00006355 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006356 case ParsedAttr::AT_MSStruct:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006357 handleSimpleAttribute<MSStructAttr>(S, D, AL);
John McCall8d32c052012-05-22 21:28:12 +00006358 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006359 case ParsedAttr::AT_Uuid:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006360 handleUuidAttr(S, D, AL);
Francois Picheta83957a2010-12-19 06:50:37 +00006361 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006362 case ParsedAttr::AT_MSInheritance:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006363 handleMSInheritanceAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006364 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006365 case ParsedAttr::AT_SelectAny:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006366 handleSimpleAttribute<SelectAnyAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006367 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006368 case ParsedAttr::AT_Thread:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006369 handleDeclspecThreadAttr(S, D, AL);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00006370 break;
David Majnemercd3ebfe2016-05-23 17:16:12 +00006371
Erich Keanee891aa92018-07-13 15:07:47 +00006372 case ParsedAttr::AT_AbiTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006373 handleAbiTagAttr(S, D, AL);
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00006374 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006375
6376 // Thread safety attributes:
Erich Keanee891aa92018-07-13 15:07:47 +00006377 case ParsedAttr::AT_AssertExclusiveLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006378 handleAssertExclusiveLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00006379 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006380 case ParsedAttr::AT_AssertSharedLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006381 handleAssertSharedLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00006382 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006383 case ParsedAttr::AT_GuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006384 handleSimpleAttribute<GuardedVarAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006385 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006386 case ParsedAttr::AT_PtGuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006387 handlePtGuardedVarAttr(S, D, AL);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006388 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006389 case ParsedAttr::AT_ScopedLockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006390 handleSimpleAttribute<ScopedLockableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006391 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006392 case ParsedAttr::AT_NoSanitize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006393 handleNoSanitizeAttr(S, D, AL);
Peter Collingbourne915df992015-05-15 18:33:32 +00006394 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006395 case ParsedAttr::AT_NoSanitizeSpecific:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006396 handleNoSanitizeSpecificAttr(S, D, AL);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00006397 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006398 case ParsedAttr::AT_NoThreadSafetyAnalysis:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006399 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, AL);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00006400 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006401 case ParsedAttr::AT_GuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006402 handleGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006403 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006404 case ParsedAttr::AT_PtGuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006405 handlePtGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006406 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006407 case ParsedAttr::AT_ExclusiveTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006408 handleExclusiveTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006409 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006410 case ParsedAttr::AT_LockReturned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006411 handleLockReturnedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006412 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006413 case ParsedAttr::AT_LocksExcluded:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006414 handleLocksExcludedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006415 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006416 case ParsedAttr::AT_SharedTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006417 handleSharedTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006418 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006419 case ParsedAttr::AT_AcquiredBefore:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006420 handleAcquiredBeforeAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006421 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006422 case ParsedAttr::AT_AcquiredAfter:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006423 handleAcquiredAfterAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006424 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006425
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006426 // Capability analysis attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006427 case ParsedAttr::AT_Capability:
6428 case ParsedAttr::AT_Lockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006429 handleCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006430 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006431 case ParsedAttr::AT_RequiresCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006432 handleRequiresCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006433 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006434
Erich Keanee891aa92018-07-13 15:07:47 +00006435 case ParsedAttr::AT_AssertCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006436 handleAssertCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006437 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006438 case ParsedAttr::AT_AcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006439 handleAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006440 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006441 case ParsedAttr::AT_ReleaseCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006442 handleReleaseCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006443 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006444 case ParsedAttr::AT_TryAcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006445 handleTryAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006446 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006447
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00006448 // Consumed analysis attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006449 case ParsedAttr::AT_Consumable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006450 handleConsumableAttr(S, D, AL);
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00006451 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006452 case ParsedAttr::AT_ConsumableAutoCast:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006453 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006454 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006455 case ParsedAttr::AT_ConsumableSetOnRead:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006456 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006457 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006458 case ParsedAttr::AT_CallableWhen:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006459 handleCallableWhenAttr(S, D, AL);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00006460 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006461 case ParsedAttr::AT_ParamTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006462 handleParamTypestateAttr(S, D, AL);
DeLesley Hutchins69391772013-10-17 23:23:53 +00006463 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006464 case ParsedAttr::AT_ReturnTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006465 handleReturnTypestateAttr(S, D, AL);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00006466 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006467 case ParsedAttr::AT_SetTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006468 handleSetTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00006469 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006470 case ParsedAttr::AT_TestTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006471 handleTestTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00006472 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00006473
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006474 // Type safety attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006475 case ParsedAttr::AT_ArgumentWithTypeTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006476 handleArgumentWithTypeTagAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006477 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006478 case ParsedAttr::AT_TypeTagForDatatype:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006479 handleTypeTagForDatatypeAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006480 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006481 case ParsedAttr::AT_AnyX86NoCallerSavedRegisters:
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006482 handleSimpleAttribute<AnyX86NoCallerSavedRegistersAttr>(S, D, AL);
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00006483 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006484 case ParsedAttr::AT_RenderScriptKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006485 handleSimpleAttribute<RenderScriptKernelAttr>(S, D, AL);
Pirama Arumuga Nainar8b788d02016-06-09 23:34:20 +00006486 break;
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00006487 // XRay attributes.
Erich Keanee891aa92018-07-13 15:07:47 +00006488 case ParsedAttr::AT_XRayInstrument:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006489 handleSimpleAttribute<XRayInstrumentAttr>(S, D, AL);
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00006490 break;
Erich Keanee891aa92018-07-13 15:07:47 +00006491 case ParsedAttr::AT_XRayLogArgs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006492 handleXRayLogArgsAttr(S, D, AL);
Dean Michael Berris418da3f2017-03-06 07:08:21 +00006493 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006494 }
6495}
6496
6497/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
6498/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00006499void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Erich Keanec480f302018-07-12 21:09:05 +00006500 const ParsedAttributesView &AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00006501 bool IncludeCXX11Attributes) {
Erich Keanec480f302018-07-12 21:09:05 +00006502 if (AttrList.empty())
6503 return;
6504
Erich Keanee891aa92018-07-13 15:07:47 +00006505 for (const ParsedAttr &AL : AttrList)
Erich Keanec480f302018-07-12 21:09:05 +00006506 ProcessDeclAttribute(*this, S, D, AL, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00006507
Joey Gouly2cd9db12013-12-13 16:15:28 +00006508 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00006509 // GCC accepts
6510 // static int a9 __attribute__((weakref));
6511 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00006512 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Erich Keanec480f302018-07-12 21:09:05 +00006513 Diag(AttrList.begin()->getLoc(), diag::err_attribute_weakref_without_alias)
6514 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00006515 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00006516 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006517 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00006518
Aaron Ballmanbe243a72014-12-04 22:45:31 +00006519 // FIXME: We should be able to handle this in TableGen as well. It would be
6520 // good to have a way to specify "these attributes must appear as a group",
6521 // for these. Additionally, it would be good to have a way to specify "these
6522 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00006523 if (!D->hasAttr<OpenCLKernelAttr>()) {
6524 // These attributes cannot be applied to a non-kernel function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006525 if (const auto *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00006526 // FIXME: This emits a different error message than
6527 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00006528 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006529 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006530 } else if (const auto *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00006531 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006532 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006533 } else if (const auto *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00006534 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006535 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006536 } else if (const auto *A = D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00006537 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
6538 D->setInvalidDecl();
Yaxun Liuaa246012018-06-12 23:58:59 +00006539 } else if (!D->hasAttr<CUDAGlobalAttr>()) {
6540 if (const auto *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) {
6541 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6542 << A << ExpectedKernelFunction;
6543 D->setInvalidDecl();
6544 } else if (const auto *A = D->getAttr<AMDGPUWavesPerEUAttr>()) {
6545 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6546 << A << ExpectedKernelFunction;
6547 D->setInvalidDecl();
6548 } else if (const auto *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
6549 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6550 << A << ExpectedKernelFunction;
6551 D->setInvalidDecl();
6552 } else if (const auto *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
6553 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6554 << A << ExpectedKernelFunction;
6555 D->setInvalidDecl();
6556 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00006557 }
6558 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006559}
6560
Hiroshi Inoue939d9322017-06-30 05:40:31 +00006561// Helper for delayed processing TransparentUnion attribute.
Erich Keanec480f302018-07-12 21:09:05 +00006562void Sema::ProcessDeclAttributeDelayed(Decl *D,
6563 const ParsedAttributesView &AttrList) {
Erich Keanee891aa92018-07-13 15:07:47 +00006564 for (const ParsedAttr &AL : AttrList)
6565 if (AL.getKind() == ParsedAttr::AT_TransparentUnion) {
Erich Keanec480f302018-07-12 21:09:05 +00006566 handleTransparentUnionAttr(*this, D, AL);
Erich Keane2fe684b2017-02-28 20:44:39 +00006567 break;
6568 }
6569}
6570
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006571// Annotation attributes are the only attributes allowed after an access
6572// specifier.
Erich Keanec480f302018-07-12 21:09:05 +00006573bool Sema::ProcessAccessDeclAttributeList(
6574 AccessSpecDecl *ASDecl, const ParsedAttributesView &AttrList) {
Erich Keanee891aa92018-07-13 15:07:47 +00006575 for (const ParsedAttr &AL : AttrList) {
6576 if (AL.getKind() == ParsedAttr::AT_Annotate) {
Erich Keanec480f302018-07-12 21:09:05 +00006577 ProcessDeclAttribute(*this, nullptr, ASDecl, AL, AL.isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006578 } else {
Erich Keanec480f302018-07-12 21:09:05 +00006579 Diag(AL.getLoc(), diag::err_only_annotate_after_access_spec);
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006580 return true;
6581 }
6582 }
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006583 return false;
6584}
6585
John McCall42856de2011-10-01 05:17:03 +00006586/// checkUnusedDeclAttributes - Check a list of attributes to see if it
6587/// contains any decl attributes that we should warn about.
Erich Keanec480f302018-07-12 21:09:05 +00006588static void checkUnusedDeclAttributes(Sema &S, const ParsedAttributesView &A) {
Erich Keanee891aa92018-07-13 15:07:47 +00006589 for (const ParsedAttr &AL : A) {
John McCall42856de2011-10-01 05:17:03 +00006590 // Only warn if the attribute is an unignored, non-type attribute.
Erich Keanec480f302018-07-12 21:09:05 +00006591 if (AL.isUsedAsTypeAttr() || AL.isInvalid())
6592 continue;
Erich Keanee891aa92018-07-13 15:07:47 +00006593 if (AL.getKind() == ParsedAttr::IgnoredAttribute)
Erich Keanec480f302018-07-12 21:09:05 +00006594 continue;
John McCall42856de2011-10-01 05:17:03 +00006595
Erich Keanee891aa92018-07-13 15:07:47 +00006596 if (AL.getKind() == ParsedAttr::UnknownAttribute) {
Erich Keanec480f302018-07-12 21:09:05 +00006597 S.Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored)
6598 << AL.getName() << AL.getRange();
John McCall42856de2011-10-01 05:17:03 +00006599 } else {
Erich Keanec480f302018-07-12 21:09:05 +00006600 S.Diag(AL.getLoc(), diag::warn_attribute_not_on_decl)
6601 << AL.getName() << AL.getRange();
John McCall42856de2011-10-01 05:17:03 +00006602 }
6603 }
6604}
6605
6606/// checkUnusedDeclAttributes - Given a declarator which is not being
6607/// used to build a declaration, complain about any decl attributes
6608/// which might be lying around on it.
6609void Sema::checkUnusedDeclAttributes(Declarator &D) {
Erich Keanec480f302018-07-12 21:09:05 +00006610 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes());
John McCall42856de2011-10-01 05:17:03 +00006611 ::checkUnusedDeclAttributes(*this, D.getAttributes());
6612 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
6613 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
6614}
6615
Ryan Flynn7d470f32009-07-30 03:15:39 +00006616/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00006617/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00006618NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
6619 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00006620 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00006621 NamedDecl *NewD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006622 if (auto *FD = dyn_cast<FunctionDecl>(ND)) {
Alexander Kornienko061900f2015-12-03 11:37:28 +00006623 FunctionDecl *NewFD;
6624 // FIXME: Missing call to CheckFunctionDeclaration().
Eli Friedmance3e2c82011-09-07 04:05:06 +00006625 // FIXME: Mangling?
6626 // FIXME: Is the qualifier info correct?
6627 // FIXME: Is the DeclContext correct?
Alexander Kornienko061900f2015-12-03 11:37:28 +00006628 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
6629 Loc, Loc, DeclarationName(II),
6630 FD->getType(), FD->getTypeSourceInfo(),
6631 SC_None, false/*isInlineSpecified*/,
6632 FD->hasPrototype(),
6633 false/*isConstexprSpecified*/);
Eli Friedmance3e2c82011-09-07 04:05:06 +00006634 NewD = NewFD;
6635
6636 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00006637 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00006638
6639 // Fake up parameter variables; they are declared as if this were
6640 // a typedef.
6641 QualType FDTy = FD->getType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006642 if (const auto *FT = FDTy->getAs<FunctionProtoType>()) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00006643 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00006644 for (const auto &AI : FT->param_types()) {
6645 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00006646 Param->setScopeInfo(0, Params.size());
6647 Params.push_back(Param);
6648 }
David Blaikie9c70e042011-09-21 18:16:56 +00006649 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00006650 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006651 } else if (auto *VD = dyn_cast<VarDecl>(ND)) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00006652 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00006653 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00006654 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00006655 VD->getStorageClass());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006656 if (VD->getQualifier())
6657 cast<VarDecl>(NewD)->setQualifierInfo(VD->getQualifierLoc());
Ryan Flynn7d470f32009-07-30 03:15:39 +00006658 }
6659 return NewD;
6660}
6661
James Dennett634962f2012-06-14 21:40:34 +00006662/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00006663/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00006664void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00006665 if (W.getUsed()) return; // only do this once
6666 W.setUsed(true);
6667 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
6668 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00006669 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00006670 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
6671 W.getLocation()));
6672 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00006673 WeakTopLevelDecl.push_back(NewD);
6674 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
6675 // to insert Decl at TU scope, sorry.
6676 DeclContext *SavedContext = CurContext;
6677 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00006678 NewD->setDeclContext(CurContext);
6679 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00006680 PushOnScopeChains(NewD, S);
6681 CurContext = SavedContext;
6682 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00006683 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00006684 }
6685}
6686
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006687void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
6688 // It's valid to "forward-declare" #pragma weak, in which case we
6689 // have to do this.
6690 LoadExternalWeakUndeclaredIdentifiers();
6691 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00006692 NamedDecl *ND = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006693 if (auto *VD = dyn_cast<VarDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006694 if (VD->isExternC())
6695 ND = VD;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006696 if (auto *FD = dyn_cast<FunctionDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006697 if (FD->isExternC())
6698 ND = FD;
6699 if (ND) {
6700 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00006701 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006702 if (I != WeakUndeclaredIdentifiers.end()) {
6703 WeakInfo W = I->second;
6704 DeclApplyPragmaWeak(S, ND, W);
6705 WeakUndeclaredIdentifiers[Id] = W;
6706 }
6707 }
6708 }
6709 }
6710}
6711
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006712/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
6713/// it, apply them to D. This is a bit tricky because PD can have attributes
6714/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00006715void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006716 // Apply decl attributes from the DeclSpec if present.
Erich Keanec480f302018-07-12 21:09:05 +00006717 if (!PD.getDeclSpec().getAttributes().empty())
6718 ProcessDeclAttributeList(S, D, PD.getDeclSpec().getAttributes());
Mike Stumpd3bb5572009-07-24 19:02:52 +00006719
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006720 // Walk the declarator structure, applying decl attributes that were in a type
6721 // position to the decl itself. This handles cases like:
6722 // int *__attr__(x)** D;
6723 // when X is a decl attribute.
6724 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
Erich Keanec480f302018-07-12 21:09:05 +00006725 ProcessDeclAttributeList(S, D, PD.getTypeObject(i).getAttrs(),
6726 /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00006727
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006728 // Finally, apply any attributes on the decl itself.
Erich Keanec480f302018-07-12 21:09:05 +00006729 ProcessDeclAttributeList(S, D, PD.getAttributes());
Alex Lorenz9e7bf162017-04-18 14:33:39 +00006730
6731 // Apply additional attributes specified by '#pragma clang attribute'.
6732 AddPragmaAttributes(S, D);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006733}
John McCall28a6aea2009-11-04 02:18:39 +00006734
John McCall31168b02011-06-15 23:02:42 +00006735/// Is the given declaration allowed to use a forbidden type?
John McCallb61e14e2015-10-27 04:54:50 +00006736/// If so, it'll still be annotated with an attribute that makes it
6737/// illegal to actually use.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006738static bool isForbiddenTypeAllowed(Sema &S, Decl *D,
John McCallb61e14e2015-10-27 04:54:50 +00006739 const DelayedDiagnostic &diag,
John McCallc6af8c62015-10-28 05:03:19 +00006740 UnavailableAttr::ImplicitReason &reason) {
John McCall31168b02011-06-15 23:02:42 +00006741 // Private ivars are always okay. Unfortunately, people don't
6742 // always properly make their ivars private, even in system headers.
6743 // Plus we need to make fields okay, too.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006744 if (!isa<FieldDecl>(D) && !isa<ObjCPropertyDecl>(D) &&
6745 !isa<FunctionDecl>(D))
John McCall31168b02011-06-15 23:02:42 +00006746 return false;
6747
John McCallc6af8c62015-10-28 05:03:19 +00006748 // Silently accept unsupported uses of __weak in both user and system
6749 // declarations when it's been disabled, for ease of integration with
6750 // -fno-objc-arc files. We do have to take some care against attempts
6751 // to define such things; for now, we've only done that for ivars
6752 // and properties.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006753 if ((isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) {
John McCallc6af8c62015-10-28 05:03:19 +00006754 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
6755 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
6756 reason = UnavailableAttr::IR_ForbiddenWeak;
6757 return true;
6758 }
John McCallb61e14e2015-10-27 04:54:50 +00006759 }
6760
John McCallc6af8c62015-10-28 05:03:19 +00006761 // Allow all sorts of things in system headers.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006762 if (S.Context.getSourceManager().isInSystemHeader(D->getLocation())) {
John McCallc6af8c62015-10-28 05:03:19 +00006763 // Currently, all the failures dealt with this way are due to ARC
6764 // restrictions.
6765 reason = UnavailableAttr::IR_ARCForbiddenType;
6766 return true;
John McCallb61e14e2015-10-27 04:54:50 +00006767 }
6768
6769 return false;
John McCall31168b02011-06-15 23:02:42 +00006770}
6771
6772/// Handle a delayed forbidden-type diagnostic.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006773static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &DD,
6774 Decl *D) {
6775 auto Reason = UnavailableAttr::IR_None;
6776 if (D && isForbiddenTypeAllowed(S, D, DD, Reason)) {
6777 assert(Reason && "didn't set reason?");
6778 D->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", Reason, DD.Loc));
John McCall31168b02011-06-15 23:02:42 +00006779 return;
6780 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00006781 if (S.getLangOpts().ObjCAutoRefCount)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006782 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00006783 // FIXME: we may want to suppress diagnostics for all
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006784 // kind of forbidden type messages on unavailable functions.
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00006785 if (FD->hasAttr<UnavailableAttr>() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006786 DD.getForbiddenTypeDiagnostic() ==
6787 diag::err_arc_array_param_no_ownership) {
6788 DD.Triggered = true;
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00006789 return;
6790 }
6791 }
John McCall31168b02011-06-15 23:02:42 +00006792
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006793 S.Diag(DD.Loc, DD.getForbiddenTypeDiagnostic())
6794 << DD.getForbiddenTypeOperand() << DD.getForbiddenTypeArgument();
6795 DD.Triggered = true;
John McCall31168b02011-06-15 23:02:42 +00006796}
6797
Manman Ren45b1ab12016-05-06 19:57:16 +00006798static const AvailabilityAttr *getAttrForPlatform(ASTContext &Context,
6799 const Decl *D) {
6800 // Check each AvailabilityAttr to find the one for this platform.
6801 for (const auto *A : D->attrs()) {
6802 if (const auto *Avail = dyn_cast<AvailabilityAttr>(A)) {
6803 // FIXME: this is copied from CheckAvailability. We should try to
6804 // de-duplicate.
6805
6806 // Check if this is an App Extension "platform", and if so chop off
6807 // the suffix for matching with the actual platform.
6808 StringRef ActualPlatform = Avail->getPlatform()->getName();
6809 StringRef RealizedPlatform = ActualPlatform;
6810 if (Context.getLangOpts().AppExt) {
6811 size_t suffix = RealizedPlatform.rfind("_app_extension");
6812 if (suffix != StringRef::npos)
6813 RealizedPlatform = RealizedPlatform.slice(0, suffix);
6814 }
6815
6816 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
6817
6818 // Match the platform name.
6819 if (RealizedPlatform == TargetPlatform)
6820 return Avail;
6821 }
6822 }
6823 return nullptr;
6824}
6825
Erik Pilkington9f866a72017-07-18 20:32:07 +00006826/// The diagnostic we should emit for \c D, and the declaration that
6827/// originated it, or \c AR_Available.
6828///
6829/// \param D The declaration to check.
6830/// \param Message If non-null, this will be populated with the message from
6831/// the availability attribute that is selected.
6832static std::pair<AvailabilityResult, const NamedDecl *>
6833ShouldDiagnoseAvailabilityOfDecl(const NamedDecl *D, std::string *Message) {
6834 AvailabilityResult Result = D->getAvailability(Message);
6835
6836 // For typedefs, if the typedef declaration appears available look
6837 // to the underlying type to see if it is more restrictive.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006838 while (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00006839 if (Result == AR_Available) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006840 if (const auto *TT = TD->getUnderlyingType()->getAs<TagType>()) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00006841 D = TT->getDecl();
6842 Result = D->getAvailability(Message);
6843 continue;
6844 }
6845 }
6846 break;
6847 }
6848
6849 // Forward class declarations get their attributes from their definition.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006850 if (const auto *IDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00006851 if (IDecl->getDefinition()) {
6852 D = IDecl->getDefinition();
6853 Result = D->getAvailability(Message);
6854 }
6855 }
6856
6857 if (const auto *ECD = dyn_cast<EnumConstantDecl>(D))
6858 if (Result == AR_Available) {
6859 const DeclContext *DC = ECD->getDeclContext();
6860 if (const auto *TheEnumDecl = dyn_cast<EnumDecl>(DC)) {
6861 Result = TheEnumDecl->getAvailability(Message);
6862 D = TheEnumDecl;
6863 }
6864 }
6865
6866 return {Result, D};
6867}
6868
6869
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006870/// whether we should emit a diagnostic for \c K and \c DeclVersion in
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00006871/// the context of \c Ctx. For example, we should emit an unavailable diagnostic
6872/// in a deprecated context, but not the other way around.
6873static bool ShouldDiagnoseAvailabilityInContext(Sema &S, AvailabilityResult K,
6874 VersionTuple DeclVersion,
6875 Decl *Ctx) {
6876 assert(K != AR_Available && "Expected an unavailable declaration here!");
6877
6878 // Checks if we should emit the availability diagnostic in the context of C.
6879 auto CheckContext = [&](const Decl *C) {
6880 if (K == AR_NotYetIntroduced) {
6881 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, C))
6882 if (AA->getIntroduced() >= DeclVersion)
6883 return true;
6884 } else if (K == AR_Deprecated)
6885 if (C->isDeprecated())
6886 return true;
6887
6888 if (C->isUnavailable())
6889 return true;
6890 return false;
6891 };
6892
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00006893 do {
6894 if (CheckContext(Ctx))
6895 return false;
6896
6897 // An implementation implicitly has the availability of the interface.
Steven Wu3bb4aa52018-04-16 23:34:18 +00006898 // Unless it is "+load" method.
6899 if (const auto *MethodD = dyn_cast<ObjCMethodDecl>(Ctx))
6900 if (MethodD->isClassMethod() &&
6901 MethodD->getSelector().getAsString() == "load")
6902 return true;
6903
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006904 if (const auto *CatOrImpl = dyn_cast<ObjCImplDecl>(Ctx)) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00006905 if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface())
6906 if (CheckContext(Interface))
6907 return false;
6908 }
6909 // A category implicitly has the availability of the interface.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006910 else if (const auto *CatD = dyn_cast<ObjCCategoryDecl>(Ctx))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00006911 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
6912 if (CheckContext(Interface))
6913 return false;
6914 } while ((Ctx = cast_or_null<Decl>(Ctx->getDeclContext())));
6915
6916 return true;
6917}
6918
Alex Lorenzc9a369f2017-06-22 17:02:24 +00006919static bool
6920shouldDiagnoseAvailabilityByDefault(const ASTContext &Context,
6921 const VersionTuple &DeploymentVersion,
6922 const VersionTuple &DeclVersion) {
6923 const auto &Triple = Context.getTargetInfo().getTriple();
6924 VersionTuple ForceAvailabilityFromVersion;
6925 switch (Triple.getOS()) {
6926 case llvm::Triple::IOS:
6927 case llvm::Triple::TvOS:
6928 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/11);
6929 break;
6930 case llvm::Triple::WatchOS:
6931 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/4);
6932 break;
6933 case llvm::Triple::Darwin:
6934 case llvm::Triple::MacOSX:
6935 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/10, /*Minor=*/13);
6936 break;
6937 default:
6938 // New targets should always warn about availability.
6939 return Triple.getVendor() == llvm::Triple::Apple;
6940 }
6941 return DeploymentVersion >= ForceAvailabilityFromVersion ||
6942 DeclVersion >= ForceAvailabilityFromVersion;
6943}
6944
Erik Pilkington4042f3c2017-07-05 17:08:56 +00006945static NamedDecl *findEnclosingDeclToAnnotate(Decl *OrigCtx) {
6946 for (Decl *Ctx = OrigCtx; Ctx;
6947 Ctx = cast_or_null<Decl>(Ctx->getDeclContext())) {
6948 if (isa<TagDecl>(Ctx) || isa<FunctionDecl>(Ctx) || isa<ObjCMethodDecl>(Ctx))
6949 return cast<NamedDecl>(Ctx);
6950 if (auto *CD = dyn_cast<ObjCContainerDecl>(Ctx)) {
6951 if (auto *Imp = dyn_cast<ObjCImplDecl>(Ctx))
6952 return Imp->getClassInterface();
6953 return CD;
6954 }
6955 }
6956
6957 return dyn_cast<NamedDecl>(OrigCtx);
6958}
6959
Alex Lorenz727c21e2017-07-26 13:58:02 +00006960namespace {
6961
6962struct AttributeInsertion {
6963 StringRef Prefix;
6964 SourceLocation Loc;
6965 StringRef Suffix;
6966
6967 static AttributeInsertion createInsertionAfter(const NamedDecl *D) {
6968 return {" ", D->getLocEnd(), ""};
6969 }
6970 static AttributeInsertion createInsertionAfter(SourceLocation Loc) {
6971 return {" ", Loc, ""};
6972 }
6973 static AttributeInsertion createInsertionBefore(const NamedDecl *D) {
6974 return {"", D->getLocStart(), "\n"};
6975 }
6976};
6977
6978} // end anonymous namespace
6979
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00006980/// Tries to parse a string as ObjC method name.
6981///
6982/// \param Name The string to parse. Expected to originate from availability
6983/// attribute argument.
6984/// \param SlotNames The vector that will be populated with slot names. In case
6985/// of unsuccessful parsing can contain invalid data.
6986/// \returns A number of method parameters if parsing was successful, None
6987/// otherwise.
6988static Optional<unsigned>
6989tryParseObjCMethodName(StringRef Name, SmallVectorImpl<StringRef> &SlotNames,
6990 const LangOptions &LangOpts) {
6991 // Accept replacements starting with - or + as valid ObjC method names.
6992 if (!Name.empty() && (Name.front() == '-' || Name.front() == '+'))
6993 Name = Name.drop_front(1);
6994 if (Name.empty())
6995 return None;
6996 Name.split(SlotNames, ':');
6997 unsigned NumParams;
6998 if (Name.back() == ':') {
6999 // Remove an empty string at the end that doesn't represent any slot.
7000 SlotNames.pop_back();
7001 NumParams = SlotNames.size();
7002 } else {
7003 if (SlotNames.size() != 1)
7004 // Not a valid method name, just a colon-separated string.
7005 return None;
7006 NumParams = 0;
7007 }
7008 // Verify all slot names are valid.
7009 bool AllowDollar = LangOpts.DollarIdents;
7010 for (StringRef S : SlotNames) {
7011 if (S.empty())
7012 continue;
7013 if (!isValidIdentifier(S, AllowDollar))
7014 return None;
7015 }
7016 return NumParams;
7017}
7018
Alex Lorenz727c21e2017-07-26 13:58:02 +00007019/// Returns a source location in which it's appropriate to insert a new
7020/// attribute for the given declaration \D.
7021static Optional<AttributeInsertion>
7022createAttributeInsertion(const NamedDecl *D, const SourceManager &SM,
7023 const LangOptions &LangOpts) {
7024 if (isa<ObjCPropertyDecl>(D))
7025 return AttributeInsertion::createInsertionAfter(D);
7026 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
7027 if (MD->hasBody())
7028 return None;
7029 return AttributeInsertion::createInsertionAfter(D);
7030 }
7031 if (const auto *TD = dyn_cast<TagDecl>(D)) {
7032 SourceLocation Loc =
7033 Lexer::getLocForEndOfToken(TD->getInnerLocStart(), 0, SM, LangOpts);
7034 if (Loc.isInvalid())
7035 return None;
7036 // Insert after the 'struct'/whatever keyword.
7037 return AttributeInsertion::createInsertionAfter(Loc);
7038 }
7039 return AttributeInsertion::createInsertionBefore(D);
7040}
7041
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007042/// Actually emit an availability diagnostic for a reference to an unavailable
7043/// decl.
7044///
7045/// \param Ctx The context that the reference occurred in
7046/// \param ReferringDecl The exact declaration that was referenced.
7047/// \param OffendingDecl A related decl to \c ReferringDecl that has an
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00007048/// availability attribute corresponding to \c K attached to it. Note that this
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007049/// may not be the same as ReferringDecl, i.e. if an EnumDecl is annotated and
7050/// we refer to a member EnumConstantDecl, ReferringDecl is the EnumConstantDecl
7051/// and OffendingDecl is the EnumDecl.
Erik Pilkington796a3e22016-08-05 22:59:03 +00007052static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007053 Decl *Ctx, const NamedDecl *ReferringDecl,
7054 const NamedDecl *OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007055 StringRef Message,
7056 ArrayRef<SourceLocation> Locs,
Aaron Ballmanfb237522014-10-15 15:37:51 +00007057 const ObjCInterfaceDecl *UnknownObjCClass,
7058 const ObjCPropertyDecl *ObjCProperty,
7059 bool ObjCPropertyAccess) {
7060 // Diagnostics for deprecated or unavailable.
7061 unsigned diag, diag_message, diag_fwdclass_message;
John McCallb61e14e2015-10-27 04:54:50 +00007062 unsigned diag_available_here = diag::note_availability_specified_here;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007063 SourceLocation NoteLocation = OffendingDecl->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00007064
7065 // Matches 'diag::note_property_attribute' options.
7066 unsigned property_note_select;
7067
7068 // Matches diag::note_availability_specified_here.
7069 unsigned available_here_select_kind;
7070
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007071 VersionTuple DeclVersion;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007072 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, OffendingDecl))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007073 DeclVersion = AA->getIntroduced();
7074
7075 if (!ShouldDiagnoseAvailabilityInContext(S, K, DeclVersion, Ctx))
7076 return;
7077
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007078 SourceLocation Loc = Locs.front();
7079
Erik Pilkington8b352c42017-08-14 19:49:12 +00007080 // The declaration can have multiple availability attributes, we are looking
7081 // at one of them.
7082 const AvailabilityAttr *A = getAttrForPlatform(S.Context, OffendingDecl);
7083 if (A && A->isInherited()) {
7084 for (const Decl *Redecl = OffendingDecl->getMostRecentDecl(); Redecl;
7085 Redecl = Redecl->getPreviousDecl()) {
7086 const AvailabilityAttr *AForRedecl =
7087 getAttrForPlatform(S.Context, Redecl);
7088 if (AForRedecl && !AForRedecl->isInherited()) {
7089 // If D is a declaration with inherited attributes, the note should
7090 // point to the declaration with actual attributes.
7091 NoteLocation = Redecl->getLocation();
7092 break;
7093 }
7094 }
7095 }
7096
Aaron Ballmanfb237522014-10-15 15:37:51 +00007097 switch (K) {
Erik Pilkington8b352c42017-08-14 19:49:12 +00007098 case AR_NotYetIntroduced: {
7099 // We would like to emit the diagnostic even if -Wunguarded-availability is
7100 // not specified for deployment targets >= to iOS 11 or equivalent or
7101 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
7102 // later.
7103 const AvailabilityAttr *AA =
7104 getAttrForPlatform(S.getASTContext(), OffendingDecl);
7105 VersionTuple Introduced = AA->getIntroduced();
7106
7107 bool UseNewWarning = shouldDiagnoseAvailabilityByDefault(
7108 S.Context, S.Context.getTargetInfo().getPlatformMinVersion(),
7109 Introduced);
7110 unsigned Warning = UseNewWarning ? diag::warn_unguarded_availability_new
7111 : diag::warn_unguarded_availability;
7112
7113 S.Diag(Loc, Warning)
7114 << OffendingDecl
7115 << AvailabilityAttr::getPrettyPlatformName(
7116 S.getASTContext().getTargetInfo().getPlatformName())
7117 << Introduced.getAsString();
7118
7119 S.Diag(OffendingDecl->getLocation(), diag::note_availability_specified_here)
7120 << OffendingDecl << /* partial */ 3;
7121
7122 if (const auto *Enclosing = findEnclosingDeclToAnnotate(Ctx)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007123 if (const auto *TD = dyn_cast<TagDecl>(Enclosing))
Erik Pilkington8b352c42017-08-14 19:49:12 +00007124 if (TD->getDeclName().isEmpty()) {
7125 S.Diag(TD->getLocation(),
7126 diag::note_decl_unguarded_availability_silence)
7127 << /*Anonymous*/ 1 << TD->getKindName();
7128 return;
7129 }
7130 auto FixitNoteDiag =
7131 S.Diag(Enclosing->getLocation(),
7132 diag::note_decl_unguarded_availability_silence)
7133 << /*Named*/ 0 << Enclosing;
7134 // Don't offer a fixit for declarations with availability attributes.
7135 if (Enclosing->hasAttr<AvailabilityAttr>())
7136 return;
7137 if (!S.getPreprocessor().isMacroDefined("API_AVAILABLE"))
7138 return;
7139 Optional<AttributeInsertion> Insertion = createAttributeInsertion(
7140 Enclosing, S.getSourceManager(), S.getLangOpts());
7141 if (!Insertion)
7142 return;
7143 std::string PlatformName =
7144 AvailabilityAttr::getPlatformNameSourceSpelling(
7145 S.getASTContext().getTargetInfo().getPlatformName())
7146 .lower();
7147 std::string Introduced =
7148 OffendingDecl->getVersionIntroduced().getAsString();
7149 FixitNoteDiag << FixItHint::CreateInsertion(
7150 Insertion->Loc,
7151 (llvm::Twine(Insertion->Prefix) + "API_AVAILABLE(" + PlatformName +
7152 "(" + Introduced + "))" + Insertion->Suffix)
7153 .str());
7154 }
7155 return;
7156 }
Erik Pilkington796a3e22016-08-05 22:59:03 +00007157 case AR_Deprecated:
Aaron Ballmanfb237522014-10-15 15:37:51 +00007158 diag = !ObjCPropertyAccess ? diag::warn_deprecated
7159 : diag::warn_property_method_deprecated;
7160 diag_message = diag::warn_deprecated_message;
7161 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
7162 property_note_select = /* deprecated */ 0;
7163 available_here_select_kind = /* deprecated */ 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007164 if (const auto *AL = OffendingDecl->getAttr<DeprecatedAttr>())
7165 NoteLocation = AL->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00007166 break;
7167
Erik Pilkington796a3e22016-08-05 22:59:03 +00007168 case AR_Unavailable:
Aaron Ballmanfb237522014-10-15 15:37:51 +00007169 diag = !ObjCPropertyAccess ? diag::err_unavailable
7170 : diag::err_property_method_unavailable;
7171 diag_message = diag::err_unavailable_message;
7172 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
7173 property_note_select = /* unavailable */ 1;
7174 available_here_select_kind = /* unavailable */ 0;
John McCallb61e14e2015-10-27 04:54:50 +00007175
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007176 if (auto AL = OffendingDecl->getAttr<UnavailableAttr>()) {
7177 if (AL->isImplicit() && AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00007178 // Most of these failures are due to extra restrictions in ARC;
7179 // reflect that in the primary diagnostic when applicable.
7180 auto flagARCError = [&] {
7181 if (S.getLangOpts().ObjCAutoRefCount &&
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007182 S.getSourceManager().isInSystemHeader(
7183 OffendingDecl->getLocation()))
John McCallc6af8c62015-10-28 05:03:19 +00007184 diag = diag::err_unavailable_in_arc;
7185 };
7186
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007187 switch (AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00007188 case UnavailableAttr::IR_None: break;
7189
7190 case UnavailableAttr::IR_ARCForbiddenType:
7191 flagARCError();
7192 diag_available_here = diag::note_arc_forbidden_type;
7193 break;
7194
7195 case UnavailableAttr::IR_ForbiddenWeak:
7196 if (S.getLangOpts().ObjCWeakRuntime)
7197 diag_available_here = diag::note_arc_weak_disabled;
7198 else
7199 diag_available_here = diag::note_arc_weak_no_runtime;
7200 break;
7201
7202 case UnavailableAttr::IR_ARCForbiddenConversion:
7203 flagARCError();
7204 diag_available_here = diag::note_performs_forbidden_arc_conversion;
7205 break;
7206
7207 case UnavailableAttr::IR_ARCInitReturnsUnrelated:
7208 flagARCError();
7209 diag_available_here = diag::note_arc_init_returns_unrelated;
7210 break;
7211
7212 case UnavailableAttr::IR_ARCFieldWithOwnership:
7213 flagARCError();
7214 diag_available_here = diag::note_arc_field_with_ownership;
7215 break;
7216 }
7217 }
John McCallb61e14e2015-10-27 04:54:50 +00007218 }
Aaron Ballmanfb237522014-10-15 15:37:51 +00007219 break;
7220
Erik Pilkington796a3e22016-08-05 22:59:03 +00007221 case AR_Available:
7222 llvm_unreachable("Warning for availability of available declaration?");
Aaron Ballmanfb237522014-10-15 15:37:51 +00007223 }
7224
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007225 SmallVector<FixItHint, 12> FixIts;
Erik Pilkington796a3e22016-08-05 22:59:03 +00007226 if (K == AR_Deprecated) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007227 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007228 if (auto AL = OffendingDecl->getAttr<DeprecatedAttr>())
7229 Replacement = AL->getReplacement();
7230 if (auto AL = getAttrForPlatform(S.Context, OffendingDecl))
7231 Replacement = AL->getReplacement();
Manman Renc7890fe2016-03-16 18:50:49 +00007232
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007233 CharSourceRange UseRange;
Manman Renc7890fe2016-03-16 18:50:49 +00007234 if (!Replacement.empty())
7235 UseRange =
7236 CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc));
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007237 if (UseRange.isValid()) {
7238 if (const auto *MethodDecl = dyn_cast<ObjCMethodDecl>(ReferringDecl)) {
7239 Selector Sel = MethodDecl->getSelector();
7240 SmallVector<StringRef, 12> SelectorSlotNames;
7241 Optional<unsigned> NumParams = tryParseObjCMethodName(
7242 Replacement, SelectorSlotNames, S.getLangOpts());
7243 if (NumParams && NumParams.getValue() == Sel.getNumArgs()) {
7244 assert(SelectorSlotNames.size() == Locs.size());
7245 for (unsigned I = 0; I < Locs.size(); ++I) {
7246 if (!Sel.getNameForSlot(I).empty()) {
7247 CharSourceRange NameRange = CharSourceRange::getCharRange(
7248 Locs[I], S.getLocForEndOfToken(Locs[I]));
7249 FixIts.push_back(FixItHint::CreateReplacement(
7250 NameRange, SelectorSlotNames[I]));
7251 } else
7252 FixIts.push_back(
7253 FixItHint::CreateInsertion(Locs[I], SelectorSlotNames[I]));
7254 }
7255 } else
7256 FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement));
7257 } else
7258 FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement));
7259 }
Manman Renc7890fe2016-03-16 18:50:49 +00007260 }
7261
Aaron Ballmanfb237522014-10-15 15:37:51 +00007262 if (!Message.empty()) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007263 S.Diag(Loc, diag_message) << ReferringDecl << Message << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007264 if (ObjCProperty)
7265 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
7266 << ObjCProperty->getDeclName() << property_note_select;
7267 } else if (!UnknownObjCClass) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007268 S.Diag(Loc, diag) << ReferringDecl << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007269 if (ObjCProperty)
7270 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
7271 << ObjCProperty->getDeclName() << property_note_select;
7272 } else {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007273 S.Diag(Loc, diag_fwdclass_message) << ReferringDecl << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007274 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
7275 }
7276
Erik Pilkington8b352c42017-08-14 19:49:12 +00007277 S.Diag(NoteLocation, diag_available_here)
7278 << OffendingDecl << available_here_select_kind;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007279}
7280
7281static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
7282 Decl *Ctx) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00007283 assert(DD.Kind == DelayedDiagnostic::Availability &&
7284 "Expected an availability diagnostic here");
7285
Aaron Ballmanfb237522014-10-15 15:37:51 +00007286 DD.Triggered = true;
Nico Weber0055a192015-03-19 19:18:22 +00007287 DoEmitAvailabilityWarning(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007288 S, DD.getAvailabilityResult(), Ctx, DD.getAvailabilityReferringDecl(),
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007289 DD.getAvailabilityOffendingDecl(), DD.getAvailabilityMessage(),
7290 DD.getAvailabilitySelectorLocs(), DD.getUnknownObjCClass(),
7291 DD.getObjCProperty(), false);
Aaron Ballmanfb237522014-10-15 15:37:51 +00007292}
7293
John McCall2ec85372012-05-07 06:16:41 +00007294void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
7295 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00007296 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00007297 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00007298
John McCall2ec85372012-05-07 06:16:41 +00007299 // When delaying diagnostics to run in the context of a parsed
7300 // declaration, we only want to actually emit anything if parsing
7301 // succeeds.
7302 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00007303
John McCall2ec85372012-05-07 06:16:41 +00007304 // We emit all the active diagnostics in this pool or any of its
7305 // parents. In general, we'll get one pool for the decl spec
7306 // and a child pool for each declarator; in a decl group like:
7307 // deprecated_typedef foo, *bar, baz();
7308 // only the declarator pops will be passed decls. This is correct;
7309 // we really do need to consider delayed diagnostics from the decl spec
7310 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00007311 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00007312 do {
John McCall6347b682012-05-07 06:16:58 +00007313 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00007314 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
7315 // This const_cast is a bit lame. Really, Triggered should be mutable.
7316 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00007317 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00007318 continue;
7319
John McCallc1465822011-02-14 07:13:47 +00007320 switch (diag.Kind) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00007321 case DelayedDiagnostic::Availability:
Ted Kremenekb79ee572013-12-18 23:30:06 +00007322 // Don't bother giving deprecation/unavailable diagnostics if
7323 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00007324 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00007325 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00007326 break;
7327
7328 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00007329 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00007330 break;
John McCall31168b02011-06-15 23:02:42 +00007331
7332 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00007333 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00007334 break;
John McCall86121512010-01-27 03:50:35 +00007335 }
7336 }
John McCall2ec85372012-05-07 06:16:41 +00007337 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00007338}
7339
John McCall6347b682012-05-07 06:16:58 +00007340/// Given a set of delayed diagnostics, re-emit them as if they had
7341/// been delayed in the current context instead of in the given pool.
7342/// Essentially, this just moves them to the current pool.
7343void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
7344 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
7345 assert(curPool && "re-emitting in undelayed context not supported");
7346 curPool->steal(pool);
7347}
7348
Erik Pilkington9f866a72017-07-18 20:32:07 +00007349static void EmitAvailabilityWarning(Sema &S, AvailabilityResult AR,
7350 const NamedDecl *ReferringDecl,
7351 const NamedDecl *OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007352 StringRef Message,
7353 ArrayRef<SourceLocation> Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00007354 const ObjCInterfaceDecl *UnknownObjCClass,
7355 const ObjCPropertyDecl *ObjCProperty,
7356 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00007357 // Delay if we're currently parsing a declaration.
Erik Pilkington9f866a72017-07-18 20:32:07 +00007358 if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
7359 S.DelayedDiagnostics.add(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007360 DelayedDiagnostic::makeAvailability(
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007361 AR, Locs, ReferringDecl, OffendingDecl, UnknownObjCClass,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007362 ObjCProperty, Message, ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00007363 return;
7364 }
7365
Erik Pilkington9f866a72017-07-18 20:32:07 +00007366 Decl *Ctx = cast<Decl>(S.getCurLexicalContext());
7367 DoEmitAvailabilityWarning(S, AR, Ctx, ReferringDecl, OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007368 Message, Locs, UnknownObjCClass, ObjCProperty,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007369 ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00007370}
Erik Pilkington48c7cc92016-07-29 17:37:38 +00007371
Erik Pilkington5cd57172016-08-16 17:44:11 +00007372namespace {
7373
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007374/// Returns true if the given statement can be a body-like child of \p Parent.
7375bool isBodyLikeChildStmt(const Stmt *S, const Stmt *Parent) {
7376 switch (Parent->getStmtClass()) {
7377 case Stmt::IfStmtClass:
7378 return cast<IfStmt>(Parent)->getThen() == S ||
7379 cast<IfStmt>(Parent)->getElse() == S;
7380 case Stmt::WhileStmtClass:
7381 return cast<WhileStmt>(Parent)->getBody() == S;
7382 case Stmt::DoStmtClass:
7383 return cast<DoStmt>(Parent)->getBody() == S;
7384 case Stmt::ForStmtClass:
7385 return cast<ForStmt>(Parent)->getBody() == S;
7386 case Stmt::CXXForRangeStmtClass:
7387 return cast<CXXForRangeStmt>(Parent)->getBody() == S;
7388 case Stmt::ObjCForCollectionStmtClass:
7389 return cast<ObjCForCollectionStmt>(Parent)->getBody() == S;
7390 case Stmt::CaseStmtClass:
7391 case Stmt::DefaultStmtClass:
7392 return cast<SwitchCase>(Parent)->getSubStmt() == S;
7393 default:
7394 return false;
7395 }
7396}
7397
7398class StmtUSEFinder : public RecursiveASTVisitor<StmtUSEFinder> {
7399 const Stmt *Target;
7400
7401public:
7402 bool VisitStmt(Stmt *S) { return S != Target; }
7403
7404 /// Returns true if the given statement is present in the given declaration.
7405 static bool isContained(const Stmt *Target, const Decl *D) {
7406 StmtUSEFinder Visitor;
7407 Visitor.Target = Target;
7408 return !Visitor.TraverseDecl(const_cast<Decl *>(D));
7409 }
7410};
7411
7412/// Traverses the AST and finds the last statement that used a given
7413/// declaration.
7414class LastDeclUSEFinder : public RecursiveASTVisitor<LastDeclUSEFinder> {
7415 const Decl *D;
7416
7417public:
7418 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
7419 if (DRE->getDecl() == D)
7420 return false;
7421 return true;
7422 }
7423
7424 static const Stmt *findLastStmtThatUsesDecl(const Decl *D,
7425 const CompoundStmt *Scope) {
7426 LastDeclUSEFinder Visitor;
7427 Visitor.D = D;
7428 for (auto I = Scope->body_rbegin(), E = Scope->body_rend(); I != E; ++I) {
7429 const Stmt *S = *I;
7430 if (!Visitor.TraverseStmt(const_cast<Stmt *>(S)))
7431 return S;
7432 }
7433 return nullptr;
7434 }
7435};
7436
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007437/// This class implements -Wunguarded-availability.
Erik Pilkington5cd57172016-08-16 17:44:11 +00007438///
7439/// This is done with a traversal of the AST of a function that makes reference
7440/// to a partially available declaration. Whenever we encounter an \c if of the
7441/// form: \c if(@available(...)), we use the version from the condition to visit
7442/// the then statement.
7443class DiagnoseUnguardedAvailability
7444 : public RecursiveASTVisitor<DiagnoseUnguardedAvailability> {
7445 typedef RecursiveASTVisitor<DiagnoseUnguardedAvailability> Base;
7446
7447 Sema &SemaRef;
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007448 Decl *Ctx;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007449
7450 /// Stack of potentially nested 'if (@available(...))'s.
7451 SmallVector<VersionTuple, 8> AvailabilityStack;
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007452 SmallVector<const Stmt *, 16> StmtStack;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007453
7454 void DiagnoseDeclAvailability(NamedDecl *D, SourceRange Range);
7455
7456public:
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007457 DiagnoseUnguardedAvailability(Sema &SemaRef, Decl *Ctx)
7458 : SemaRef(SemaRef), Ctx(Ctx) {
7459 AvailabilityStack.push_back(
7460 SemaRef.Context.getTargetInfo().getPlatformMinVersion());
Erik Pilkington5cd57172016-08-16 17:44:11 +00007461 }
7462
Alex Lorenz6f911122017-05-16 13:58:53 +00007463 bool TraverseDecl(Decl *D) {
7464 // Avoid visiting nested functions to prevent duplicate warnings.
7465 if (!D || isa<FunctionDecl>(D))
7466 return true;
7467 return Base::TraverseDecl(D);
7468 }
7469
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007470 bool TraverseStmt(Stmt *S) {
7471 if (!S)
7472 return true;
7473 StmtStack.push_back(S);
7474 bool Result = Base::TraverseStmt(S);
7475 StmtStack.pop_back();
7476 return Result;
7477 }
7478
Erik Pilkington5cd57172016-08-16 17:44:11 +00007479 void IssueDiagnostics(Stmt *S) { TraverseStmt(S); }
7480
7481 bool TraverseIfStmt(IfStmt *If);
7482
Alex Lorenz6f911122017-05-16 13:58:53 +00007483 bool TraverseLambdaExpr(LambdaExpr *E) { return true; }
7484
Erik Pilkingtonba87c622017-08-18 20:20:56 +00007485 // for 'case X:' statements, don't bother looking at the 'X'; it can't lead
7486 // to any useful diagnostics.
7487 bool TraverseCaseStmt(CaseStmt *CS) { return TraverseStmt(CS->getSubStmt()); }
7488
Erik Pilkington6ac77a62017-05-22 15:41:12 +00007489 bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *PRE) {
7490 if (PRE->isClassReceiver())
7491 DiagnoseDeclAvailability(PRE->getClassReceiver(), PRE->getReceiverLocation());
7492 return true;
7493 }
7494
Erik Pilkington5cd57172016-08-16 17:44:11 +00007495 bool VisitObjCMessageExpr(ObjCMessageExpr *Msg) {
7496 if (ObjCMethodDecl *D = Msg->getMethodDecl())
7497 DiagnoseDeclAvailability(
7498 D, SourceRange(Msg->getSelectorStartLoc(), Msg->getLocEnd()));
7499 return true;
7500 }
7501
7502 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
7503 DiagnoseDeclAvailability(DRE->getDecl(),
7504 SourceRange(DRE->getLocStart(), DRE->getLocEnd()));
7505 return true;
7506 }
7507
7508 bool VisitMemberExpr(MemberExpr *ME) {
7509 DiagnoseDeclAvailability(ME->getMemberDecl(),
7510 SourceRange(ME->getLocStart(), ME->getLocEnd()));
7511 return true;
7512 }
7513
Alex Lorenz0a484ba2017-05-24 15:15:29 +00007514 bool VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr *E) {
7515 SemaRef.Diag(E->getLocStart(), diag::warn_at_available_unchecked_use)
7516 << (!SemaRef.getLangOpts().ObjC1);
7517 return true;
7518 }
7519
Erik Pilkington5cd57172016-08-16 17:44:11 +00007520 bool VisitTypeLoc(TypeLoc Ty);
7521};
7522
7523void DiagnoseUnguardedAvailability::DiagnoseDeclAvailability(
7524 NamedDecl *D, SourceRange Range) {
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007525 AvailabilityResult Result;
7526 const NamedDecl *OffendingDecl;
7527 std::tie(Result, OffendingDecl) =
Erik Pilkington9f866a72017-07-18 20:32:07 +00007528 ShouldDiagnoseAvailabilityOfDecl(D, nullptr);
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007529 if (Result != AR_Available) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00007530 // All other diagnostic kinds have already been handled in
7531 // DiagnoseAvailabilityOfDecl.
7532 if (Result != AR_NotYetIntroduced)
7533 return;
7534
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007535 const AvailabilityAttr *AA =
7536 getAttrForPlatform(SemaRef.getASTContext(), OffendingDecl);
Erik Pilkington5cd57172016-08-16 17:44:11 +00007537 VersionTuple Introduced = AA->getIntroduced();
7538
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007539 if (AvailabilityStack.back() >= Introduced)
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007540 return;
7541
7542 // If the context of this function is less available than D, we should not
7543 // emit a diagnostic.
7544 if (!ShouldDiagnoseAvailabilityInContext(SemaRef, Result, Introduced, Ctx))
7545 return;
7546
Alex Lorenzc9a369f2017-06-22 17:02:24 +00007547 // We would like to emit the diagnostic even if -Wunguarded-availability is
7548 // not specified for deployment targets >= to iOS 11 or equivalent or
7549 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
7550 // later.
7551 unsigned DiagKind =
7552 shouldDiagnoseAvailabilityByDefault(
7553 SemaRef.Context,
7554 SemaRef.Context.getTargetInfo().getPlatformMinVersion(), Introduced)
7555 ? diag::warn_unguarded_availability_new
7556 : diag::warn_unguarded_availability;
7557
7558 SemaRef.Diag(Range.getBegin(), DiagKind)
Erik Pilkington5cd57172016-08-16 17:44:11 +00007559 << Range << D
7560 << AvailabilityAttr::getPrettyPlatformName(
7561 SemaRef.getASTContext().getTargetInfo().getPlatformName())
7562 << Introduced.getAsString();
7563
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007564 SemaRef.Diag(OffendingDecl->getLocation(),
7565 diag::note_availability_specified_here)
7566 << OffendingDecl << /* partial */ 3;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007567
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007568 auto FixitDiag =
7569 SemaRef.Diag(Range.getBegin(), diag::note_unguarded_available_silence)
7570 << Range << D
7571 << (SemaRef.getLangOpts().ObjC1 ? /*@available*/ 0
7572 : /*__builtin_available*/ 1);
7573
7574 // Find the statement which should be enclosed in the if @available check.
7575 if (StmtStack.empty())
7576 return;
7577 const Stmt *StmtOfUse = StmtStack.back();
7578 const CompoundStmt *Scope = nullptr;
7579 for (const Stmt *S : llvm::reverse(StmtStack)) {
7580 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
7581 Scope = CS;
7582 break;
7583 }
7584 if (isBodyLikeChildStmt(StmtOfUse, S)) {
7585 // The declaration won't be seen outside of the statement, so we don't
7586 // have to wrap the uses of any declared variables in if (@available).
7587 // Therefore we can avoid setting Scope here.
7588 break;
7589 }
7590 StmtOfUse = S;
7591 }
7592 const Stmt *LastStmtOfUse = nullptr;
7593 if (isa<DeclStmt>(StmtOfUse) && Scope) {
7594 for (const Decl *D : cast<DeclStmt>(StmtOfUse)->decls()) {
7595 if (StmtUSEFinder::isContained(StmtStack.back(), D)) {
7596 LastStmtOfUse = LastDeclUSEFinder::findLastStmtThatUsesDecl(D, Scope);
7597 break;
7598 }
7599 }
7600 }
7601
7602 const SourceManager &SM = SemaRef.getSourceManager();
7603 SourceLocation IfInsertionLoc =
7604 SM.getExpansionLoc(StmtOfUse->getLocStart());
7605 SourceLocation StmtEndLoc =
7606 SM.getExpansionRange(
7607 (LastStmtOfUse ? LastStmtOfUse : StmtOfUse)->getLocEnd())
Richard Smithb5f81712018-04-30 05:25:48 +00007608 .getEnd();
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007609 if (SM.getFileID(IfInsertionLoc) != SM.getFileID(StmtEndLoc))
7610 return;
7611
7612 StringRef Indentation = Lexer::getIndentationForLine(IfInsertionLoc, SM);
7613 const char *ExtraIndentation = " ";
7614 std::string FixItString;
7615 llvm::raw_string_ostream FixItOS(FixItString);
7616 FixItOS << "if (" << (SemaRef.getLangOpts().ObjC1 ? "@available"
7617 : "__builtin_available")
Alex Lorenze1fb64e2017-05-09 15:34:46 +00007618 << "("
7619 << AvailabilityAttr::getPlatformNameSourceSpelling(
7620 SemaRef.getASTContext().getTargetInfo().getPlatformName())
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007621 << " " << Introduced.getAsString() << ", *)) {\n"
7622 << Indentation << ExtraIndentation;
7623 FixitDiag << FixItHint::CreateInsertion(IfInsertionLoc, FixItOS.str());
7624 SourceLocation ElseInsertionLoc = Lexer::findLocationAfterToken(
7625 StmtEndLoc, tok::semi, SM, SemaRef.getLangOpts(),
7626 /*SkipTrailingWhitespaceAndNewLine=*/false);
7627 if (ElseInsertionLoc.isInvalid())
7628 ElseInsertionLoc =
7629 Lexer::getLocForEndOfToken(StmtEndLoc, 0, SM, SemaRef.getLangOpts());
7630 FixItOS.str().clear();
7631 FixItOS << "\n"
7632 << Indentation << "} else {\n"
7633 << Indentation << ExtraIndentation
7634 << "// Fallback on earlier versions\n"
7635 << Indentation << "}";
7636 FixitDiag << FixItHint::CreateInsertion(ElseInsertionLoc, FixItOS.str());
Erik Pilkington5cd57172016-08-16 17:44:11 +00007637 }
7638}
7639
7640bool DiagnoseUnguardedAvailability::VisitTypeLoc(TypeLoc Ty) {
7641 const Type *TyPtr = Ty.getTypePtr();
7642 SourceRange Range{Ty.getBeginLoc(), Ty.getEndLoc()};
7643
Erik Pilkington6ac77a62017-05-22 15:41:12 +00007644 if (Range.isInvalid())
7645 return true;
7646
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007647 if (const auto *TT = dyn_cast<TagType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00007648 TagDecl *TD = TT->getDecl();
7649 DiagnoseDeclAvailability(TD, Range);
7650
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007651 } else if (const auto *TD = dyn_cast<TypedefType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00007652 TypedefNameDecl *D = TD->getDecl();
7653 DiagnoseDeclAvailability(D, Range);
7654
7655 } else if (const auto *ObjCO = dyn_cast<ObjCObjectType>(TyPtr)) {
7656 if (NamedDecl *D = ObjCO->getInterface())
7657 DiagnoseDeclAvailability(D, Range);
7658 }
7659
7660 return true;
7661}
7662
7663bool DiagnoseUnguardedAvailability::TraverseIfStmt(IfStmt *If) {
7664 VersionTuple CondVersion;
7665 if (auto *E = dyn_cast<ObjCAvailabilityCheckExpr>(If->getCond())) {
7666 CondVersion = E->getVersion();
7667
7668 // If we're using the '*' case here or if this check is redundant, then we
7669 // use the enclosing version to check both branches.
7670 if (CondVersion.empty() || CondVersion <= AvailabilityStack.back())
Alex Lorenz98f9fcd2017-08-17 14:22:27 +00007671 return TraverseStmt(If->getThen()) && TraverseStmt(If->getElse());
Erik Pilkington5cd57172016-08-16 17:44:11 +00007672 } else {
7673 // This isn't an availability checking 'if', we can just continue.
7674 return Base::TraverseIfStmt(If);
7675 }
7676
7677 AvailabilityStack.push_back(CondVersion);
7678 bool ShouldContinue = TraverseStmt(If->getThen());
7679 AvailabilityStack.pop_back();
7680
7681 return ShouldContinue && TraverseStmt(If->getElse());
7682}
7683
7684} // end anonymous namespace
7685
7686void Sema::DiagnoseUnguardedAvailabilityViolations(Decl *D) {
7687 Stmt *Body = nullptr;
7688
7689 if (auto *FD = D->getAsFunction()) {
7690 // FIXME: We only examine the pattern decl for availability violations now,
7691 // but we should also examine instantiated templates.
7692 if (FD->isTemplateInstantiation())
7693 return;
7694
7695 Body = FD->getBody();
7696 } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
7697 Body = MD->getBody();
Alex Lorenz28559ce2017-04-26 14:20:02 +00007698 else if (auto *BD = dyn_cast<BlockDecl>(D))
7699 Body = BD->getBody();
Erik Pilkington5cd57172016-08-16 17:44:11 +00007700
7701 assert(Body && "Need a body here!");
7702
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007703 DiagnoseUnguardedAvailability(*this, D).IssueDiagnostics(Body);
Erik Pilkington5cd57172016-08-16 17:44:11 +00007704}
Erik Pilkington9f866a72017-07-18 20:32:07 +00007705
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007706void Sema::DiagnoseAvailabilityOfDecl(NamedDecl *D,
7707 ArrayRef<SourceLocation> Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00007708 const ObjCInterfaceDecl *UnknownObjCClass,
7709 bool ObjCPropertyAccess,
7710 bool AvoidPartialAvailabilityChecks) {
7711 std::string Message;
7712 AvailabilityResult Result;
7713 const NamedDecl* OffendingDecl;
7714 // See if this declaration is unavailable, deprecated, or partial.
7715 std::tie(Result, OffendingDecl) = ShouldDiagnoseAvailabilityOfDecl(D, &Message);
7716 if (Result == AR_Available)
7717 return;
7718
7719 if (Result == AR_NotYetIntroduced) {
7720 if (AvoidPartialAvailabilityChecks)
7721 return;
7722
7723 // We need to know the @available context in the current function to
7724 // diagnose this use, let DiagnoseUnguardedAvailabilityViolations do that
7725 // when we're done parsing the current function.
7726 if (getCurFunctionOrMethodDecl()) {
7727 getEnclosingFunction()->HasPotentialAvailabilityViolations = true;
7728 return;
7729 } else if (getCurBlock() || getCurLambda()) {
7730 getCurFunction()->HasPotentialAvailabilityViolations = true;
7731 return;
7732 }
7733 }
7734
7735 const ObjCPropertyDecl *ObjCPDecl = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007736 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007737 if (const ObjCPropertyDecl *PD = MD->findPropertyDecl()) {
7738 AvailabilityResult PDeclResult = PD->getAvailability(nullptr);
7739 if (PDeclResult == Result)
7740 ObjCPDecl = PD;
7741 }
7742 }
7743
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007744 EmitAvailabilityWarning(*this, Result, D, OffendingDecl, Message, Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00007745 UnknownObjCClass, ObjCPDecl, ObjCPropertyAccess);
7746}