blob: 6126c7f516c249cc0d612ef577ac51126aa502d8 [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"
Mehdi Amini9670f842016-07-18 19:02:11 +000034#include "clang/Sema/SemaInternal.h"
George Burgess IV177399e2017-01-09 04:12:14 +000035#include "llvm/ADT/STLExtras.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000036#include "llvm/ADT/StringExtras.h"
Hal Finkelee90a222014-09-26 05:04:30 +000037#include "llvm/Support/MathExtras.h"
Eugene Zelenko1ced5092016-02-12 22:53:10 +000038
Chris Lattner2c6fcf52008-06-26 18:38:35 +000039using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000040using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000041
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000042namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000043 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000044 C,
45 Cpp,
46 ObjC
47 };
Eugene Zelenko1ced5092016-02-12 22:53:10 +000048} // end namespace AttributeLangSupport
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000049
Chris Lattner58418ff2008-06-29 00:16:31 +000050//===----------------------------------------------------------------------===//
51// Helper functions
52//===----------------------------------------------------------------------===//
53
Ted Kremenek527042b2009-08-14 20:49:40 +000054/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000055/// type (function or function-typed variable) or an Objective-C
56/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000057static bool isFunctionOrMethod(const Decl *D) {
Craig Topperc3ec1492014-05-26 06:22:03 +000058 return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000059}
Eugene Zelenko1ced5092016-02-12 22:53:10 +000060
David Majnemer06864812015-04-07 06:01:53 +000061/// \brief Return true if the given decl has function type (function or
62/// function-typed variable) or an Objective-C method or a block.
63static bool isFunctionOrMethodOrBlock(const Decl *D) {
64 return isFunctionOrMethod(D) || isa<BlockDecl>(D);
65}
Fariborz Jahanian4447e172009-05-15 23:15:03 +000066
John McCall3882ace2011-01-05 12:14:39 +000067/// Return true if the given decl has a declarator that should have
68/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000069static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000070 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000071 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
72 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +000073}
74
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000075/// hasFunctionProto - Return true if the given decl has a argument
76/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000077/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000078static bool hasFunctionProto(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000079 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000080 return isa<FunctionProtoType>(FnTy);
Aaron Ballman12b9f652014-01-16 13:55:42 +000081 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000082}
83
Alp Toker601b22c2014-01-21 23:35:24 +000084/// getFunctionOrMethodNumParams - Return number of function or method
85/// parameters. It is an error to call this on a K&R function (use
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000086/// hasFunctionProto first).
Alp Toker601b22c2014-01-21 23:35:24 +000087static unsigned getFunctionOrMethodNumParams(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000088 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000089 return cast<FunctionProtoType>(FnTy)->getNumParams();
Aaron Ballmana70c6b52018-02-15 16:20:20 +000090 if (const auto *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000091 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000092 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000093}
94
Alp Toker601b22c2014-01-21 23:35:24 +000095static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000096 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000097 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
Aaron Ballmana70c6b52018-02-15 16:20:20 +000098 if (const auto *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000099 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000100
Alp Toker03376dc2014-07-07 09:02:20 +0000101 return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000102}
103
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000104static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) {
105 if (const auto *FD = dyn_cast<FunctionDecl>(D))
106 return FD->getParamDecl(Idx)->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000107 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000108 return MD->parameters()[Idx]->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000109 if (const auto *BD = dyn_cast<BlockDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000110 return BD->getParamDecl(Idx)->getSourceRange();
111 return SourceRange();
112}
113
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000114static QualType getFunctionOrMethodResultType(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000115 if (const FunctionType *FnTy = D->getFunctionType())
Aaron Ballman6288d062014-07-11 16:31:29 +0000116 return cast<FunctionType>(FnTy)->getReturnType();
Alp Toker314cc812014-01-25 16:55:45 +0000117 return cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000118}
119
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000120static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) {
121 if (const auto *FD = dyn_cast<FunctionDecl>(D))
122 return FD->getReturnTypeSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000123 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000124 return MD->getReturnTypeSourceRange();
125 return SourceRange();
126}
127
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000128static bool isFunctionOrMethodVariadic(const Decl *D) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000129 if (const FunctionType *FnTy = D->getFunctionType())
130 return cast<FunctionProtoType>(FnTy)->isVariadic();
131 if (const auto *BD = dyn_cast<BlockDecl>(D))
Aaron Ballmane13d0092014-08-01 17:02:34 +0000132 return BD->isVariadic();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000133 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000134}
135
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000136static bool isInstanceMethod(const Decl *D) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000137 if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000138 return MethodDecl->isInstance();
139 return false;
140}
141
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000142static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000143 const auto *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000144 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000145 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000146
John McCall96fa4842010-05-17 21:00:27 +0000147 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
148 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000149 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000150
John McCall96fa4842010-05-17 21:00:27 +0000151 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000152
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000153 // FIXME: Should we walk the chain of classes?
154 return ClsName == &Ctx.Idents.get("NSString") ||
155 ClsName == &Ctx.Idents.get("NSMutableString");
156}
157
Daniel Dunbar980c6692008-09-26 03:32:58 +0000158static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000159 const auto *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000160 if (!PT)
161 return false;
162
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000163 const auto *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000164 if (!RT)
165 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000166
Daniel Dunbar980c6692008-09-26 03:32:58 +0000167 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000168 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000169 return false;
170
171 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
172}
173
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000174static unsigned getNumAttributeArgs(const AttributeList &AL) {
Richard Smithb87c4652013-10-31 21:23:20 +0000175 // FIXME: Include the type in the argument list.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000176 return AL.getNumArgs() + AL.hasParsedType();
Richard Smithb87c4652013-10-31 21:23:20 +0000177}
178
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000179template <typename Compare>
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000180static bool checkAttributeNumArgsImpl(Sema &S, const AttributeList &AL,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000181 unsigned Num, unsigned Diag,
182 Compare Comp) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000183 if (Comp(getNumAttributeArgs(AL), Num)) {
184 S.Diag(AL.getLoc(), Diag) << AL.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000185 return false;
186 }
187
188 return true;
189}
190
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000191/// \brief Check if the attribute has exactly as many args as Num. May
192/// output an error.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000193static bool checkAttributeNumArgs(Sema &S, const AttributeList &AL,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000194 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
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000200/// \brief Check if the attribute has at least as many args as Num. May
201/// output an error.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000202static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &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
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000209/// \brief Check if the attribute has at most as many args as Num. May
210/// output an error.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000211static bool checkAttributeAtMostNumArgs(Sema &S, const AttributeList &AL,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000212 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
Erich Keane623efd82017-03-30 21:48:55 +0000218/// \brief A helper function to provide Attribute Location for the Attr types
219/// AND the AttributeList.
220template <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}
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000226static SourceLocation getAttrLoc(const AttributeList &AL) {
227 return AL.getLoc();
Erich Keane623efd82017-03-30 21:48:55 +0000228}
229
230/// \brief A helper function to provide Attribute Name for the Attr types
231/// AND the AttributeList.
232template <typename AttrInfo>
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000233static typename std::enable_if<std::is_base_of<Attr, AttrInfo>::value,
Erich Keane623efd82017-03-30 21:48:55 +0000234 const AttrInfo *>::type
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000235getAttrName(const AttrInfo &AL) {
236 return &AL;
Erich Keane623efd82017-03-30 21:48:55 +0000237}
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000238static const IdentifierInfo *getAttrName(const AttributeList &AL) {
239 return AL.getName();
Erich Keane623efd82017-03-30 21:48:55 +0000240}
241
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000242/// \brief If Expr is a valid integer constant, get the value of the integer
243/// expression and return success or failure. May output an error.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000244template <typename AttrInfo>
245static bool checkUInt32Argument(Sema &S, const AttrInfo &AI, const Expr *Expr,
Erich Keane623efd82017-03-30 21:48:55 +0000246 uint32_t &Val, unsigned Idx = UINT_MAX) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000247 llvm::APSInt I(32);
248 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
249 !Expr->isIntegerConstantExpr(I, S.Context)) {
250 if (Idx != UINT_MAX)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000251 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
252 << getAttrName(AI) << Idx << AANT_ArgumentIntegerConstant
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000253 << Expr->getSourceRange();
254 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000255 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_type)
256 << getAttrName(AI) << AANT_ArgumentIntegerConstant
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000257 << Expr->getSourceRange();
258 return false;
259 }
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000260
261 if (!I.isIntN(32)) {
Aaron Ballman31f42312014-07-24 14:51:23 +0000262 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
263 << I.toString(10, false) << 32 << /* Unsigned */ 1;
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000264 return false;
265 }
266
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000267 Val = (uint32_t)I.getZExtValue();
268 return true;
269}
270
George Burgess IVe3763372016-12-22 02:50:20 +0000271/// \brief Wrapper around checkUInt32Argument, with an extra check to be sure
272/// that the result will fit into a regular (signed) int. All args have the same
273/// purpose as they do in checkUInt32Argument.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000274template <typename AttrInfo>
275static bool checkPositiveIntArgument(Sema &S, const AttrInfo &AI, const Expr *Expr,
Erich Keane623efd82017-03-30 21:48:55 +0000276 int &Val, unsigned Idx = UINT_MAX) {
George Burgess IVe3763372016-12-22 02:50:20 +0000277 uint32_t UVal;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000278 if (!checkUInt32Argument(S, AI, Expr, UVal, Idx))
George Burgess IVe3763372016-12-22 02:50:20 +0000279 return false;
280
George Burgess IVa8049572016-12-22 19:00:31 +0000281 if (UVal > (uint32_t)std::numeric_limits<int>::max()) {
George Burgess IVe3763372016-12-22 02:50:20 +0000282 llvm::APSInt I(32); // for toString
283 I = UVal;
284 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
285 << I.toString(10, false) << 32 << /* Unsigned */ 0;
286 return false;
287 }
288
289 Val = UVal;
290 return true;
291}
292
Aaron Ballmanfb763042013-12-02 18:05:46 +0000293/// \brief Diagnose mutually exclusive attributes when present on a given
294/// declaration. Returns true if diagnosed.
295template <typename AttrTy>
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +0000296static bool checkAttrMutualExclusion(Sema &S, Decl *D, SourceRange Range,
297 IdentifierInfo *Ident) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000298 if (const auto *A = D->getAttr<AttrTy>()) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +0000299 S.Diag(Range.getBegin(), diag::err_attributes_are_not_compatible) << Ident
300 << A;
301 S.Diag(A->getLocation(), diag::note_conflicting_attribute);
Aaron Ballmanfb763042013-12-02 18:05:46 +0000302 return true;
303 }
304 return false;
305}
306
Alp Toker601b22c2014-01-21 23:35:24 +0000307/// \brief Check if IdxExpr is a valid parameter index for a function or
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000308/// instance method D. May output an error.
309///
310/// \returns true if IdxExpr is a valid index.
Erich Keane623efd82017-03-30 21:48:55 +0000311template <typename AttrInfo>
312static bool checkFunctionOrMethodParameterIndex(
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000313 Sema &S, const Decl *D, const AttrInfo &AI, unsigned AttrArgNum,
Dean Michael Berris7456a282017-06-16 03:22:09 +0000314 const Expr *IdxExpr, uint64_t &Idx, bool AllowImplicitThis = false) {
David Majnemer06864812015-04-07 06:01:53 +0000315 assert(isFunctionOrMethodOrBlock(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000316
317 // In C++ the implicit 'this' function parameter also counts.
318 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000319 bool HP = hasFunctionProto(D);
320 bool HasImplicitThisParam = isInstanceMethod(D);
321 bool IV = HP && isFunctionOrMethodVariadic(D);
Alp Toker601b22c2014-01-21 23:35:24 +0000322 unsigned NumParams =
323 (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000324
325 llvm::APSInt IdxInt;
326 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
327 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000328 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
329 << getAttrName(AI) << AttrArgNum << AANT_ArgumentIntegerConstant
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000330 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000331 return false;
332 }
333
334 Idx = IdxInt.getLimitedValue();
Alp Toker601b22c2014-01-21 23:35:24 +0000335 if (Idx < 1 || (!IV && Idx > NumParams)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000336 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_out_of_bounds)
337 << getAttrName(AI) << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000338 return false;
339 }
340 Idx--; // Convert to zero-based.
Dean Michael Berris7456a282017-06-16 03:22:09 +0000341 if (HasImplicitThisParam && !AllowImplicitThis) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000342 if (Idx == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000343 S.Diag(getAttrLoc(AI),
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000344 diag::err_attribute_invalid_implicit_this_argument)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000345 << getAttrName(AI) << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000346 return false;
347 }
348 --Idx;
349 }
350
351 return true;
352}
353
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000354/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
355/// If not emit an error and return false. If the argument is an identifier it
356/// will emit an error with a fixit hint and treat it as if it was a string
357/// literal.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000358bool Sema::checkStringLiteralArgumentAttr(const AttributeList &AL,
Tim Northover6a6b63b2013-10-01 14:34:18 +0000359 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000360 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000361 // Look for identifiers. If we have one emit a hint to fix it to a literal.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000362 if (AL.isArgIdent(ArgNum)) {
363 IdentifierLoc *Loc = AL.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000364 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000365 << AL.getName() << AANT_ArgumentString
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000366 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Craig Topper07fa1762015-11-15 02:31:46 +0000367 << FixItHint::CreateInsertion(getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000368 Str = Loc->Ident->getName();
369 if (ArgLocation)
370 *ArgLocation = Loc->Loc;
371 return true;
372 }
373
374 // Now check for an actual string literal.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000375 Expr *ArgExpr = AL.getArgAsExpr(ArgNum);
376 const auto *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000377 if (ArgLocation)
378 *ArgLocation = ArgExpr->getLocStart();
379
380 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000381 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000382 << AL.getName() << AANT_ArgumentString;
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000383 return false;
384 }
385
386 Str = Literal->getString();
387 return true;
388}
389
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000390/// \brief Applies the given attribute to the Decl without performing any
391/// additional semantic checking.
392template <typename AttrType>
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000393static void handleSimpleAttribute(Sema &S, Decl *D, const AttributeList &AL) {
394 D->addAttr(::new (S.Context) AttrType(AL.getRange(), S.Context,
395 AL.getAttributeSpellingListIndex()));
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000396}
397
Justin Lebar3eaaf862016-01-13 01:07:35 +0000398template <typename AttrType>
399static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000400 const AttributeList &AL) {
401 handleSimpleAttribute<AttrType>(S, D, AL);
Justin Lebar3eaaf862016-01-13 01:07:35 +0000402}
403
404/// \brief Applies the given attribute to the Decl so long as the Decl doesn't
405/// already have one of the given incompatible attributes.
406template <typename AttrType, typename IncompatibleAttrType,
407 typename... IncompatibleAttrTypes>
408static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000409 const AttributeList &AL) {
410 if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL.getRange(),
411 AL.getName()))
Justin Lebar3eaaf862016-01-13 01:07:35 +0000412 return;
413 handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000414 AL);
Justin Lebar3eaaf862016-01-13 01:07:35 +0000415}
416
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000417/// \brief Check if the passed-in expression is of type int or bool.
418static bool isIntOrBool(Expr *Exp) {
419 QualType QT = Exp->getType();
420 return QT->isBooleanType() || QT->isIntegerType();
421}
422
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000423
424// Check to see if the type is a smart pointer of some kind. We assume
425// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000426static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
Richard Smithcf4bdde2015-02-21 02:45:19 +0000427 DeclContextLookupResult Res1 = RT->getDecl()->lookup(
428 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000429 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000430 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000431
Richard Smithcf4bdde2015-02-21 02:45:19 +0000432 DeclContextLookupResult Res2 = RT->getDecl()->lookup(
433 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000434 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000435 return false;
436
437 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000438}
439
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000440/// \brief Check if passed in Decl is a pointer type.
441/// Note that this function may produce an error message.
442/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000443static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000444 const AttributeList &AL) {
445 const auto *VD = cast<ValueDecl>(D);
446 QualType QT = VD->getType();
Aaron Ballman553e6812013-12-26 14:54:11 +0000447 if (QT->isAnyPointerType())
448 return true;
449
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000450 if (const auto *RT = QT->getAs<RecordType>()) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000451 // If it's an incomplete type, it could be a smart pointer; skip it.
452 // (We don't want to force template instantiation if we can avoid it,
453 // since that would alter the order in which templates are instantiated.)
454 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000455 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000456
Aaron Ballman553e6812013-12-26 14:54:11 +0000457 if (threadSafetyCheckIsSmartPointer(S, RT))
458 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000459 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000460
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000461 S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
462 << AL.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000463 return false;
464}
465
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000466/// \brief Checks that the passed in QualType either is of RecordType or points
467/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000468static const RecordType *getRecordType(QualType QT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000469 if (const auto *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000470 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000471
472 // Now check if we point to record type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000473 if (const auto *PT = QT->getAs<PointerType>())
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000474 return PT->getPointeeType()->getAs<RecordType>();
475
Craig Topperc3ec1492014-05-26 06:22:03 +0000476 return nullptr;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000477}
478
Josh Gao55afa752017-08-11 07:54:35 +0000479static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000480 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000481
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000482 if (!RT)
483 return false;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000484
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000485 // Don't check for the capability if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000486 if (RT->isIncompleteType())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000487 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000488
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000489 // Allow smart pointers to be used as capability objects.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000490 // FIXME -- Check the type that the smart pointer points to.
491 if (threadSafetyCheckIsSmartPointer(S, RT))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000492 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000493
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000494 // Check if the record itself has a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000495 RecordDecl *RD = RT->getDecl();
Josh Gao55afa752017-08-11 07:54:35 +0000496 if (RD->hasAttr<CapabilityAttr>())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000497 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000498
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000499 // Else check if any base classes have a capability.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000500 if (const auto *CRD = dyn_cast<CXXRecordDecl>(RD)) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000501 CXXBasePaths BPaths(false, false);
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000502 if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &) {
503 const auto *Type = BS->getType()->getAs<RecordType>();
Josh Gao55afa752017-08-11 07:54:35 +0000504 return Type->getDecl()->hasAttr<CapabilityAttr>();
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000505 }, BPaths))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000506 return true;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000507 }
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000508 return false;
509}
510
Josh Gao55afa752017-08-11 07:54:35 +0000511static bool checkTypedefTypeForCapability(QualType Ty) {
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000512 const auto *TD = Ty->getAs<TypedefType>();
513 if (!TD)
514 return false;
515
516 TypedefNameDecl *TN = TD->getDecl();
517 if (!TN)
518 return false;
519
Josh Gao55afa752017-08-11 07:54:35 +0000520 return TN->hasAttr<CapabilityAttr>();
Aaron Ballman76050722014-04-04 15:13:57 +0000521}
522
Josh Gaob40c1772017-08-08 19:44:35 +0000523static bool typeHasCapability(Sema &S, QualType Ty) {
Josh Gao55afa752017-08-11 07:54:35 +0000524 if (checkTypedefTypeForCapability(Ty))
525 return true;
Josh Gaob40c1772017-08-08 19:44:35 +0000526
Josh Gao55afa752017-08-11 07:54:35 +0000527 if (checkRecordTypeForCapability(S, Ty))
528 return true;
529
530 return false;
Josh Gaob40c1772017-08-08 19:44:35 +0000531}
532
Aaron Ballman76050722014-04-04 15:13:57 +0000533static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
534 // Capability expressions are simple expressions involving the boolean logic
535 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
536 // a DeclRefExpr is found, its type should be checked to determine whether it
537 // is a capability or not.
538
Yi Kong2d58d192017-12-14 22:24:45 +0000539 if (const auto *E = dyn_cast<CastExpr>(Ex))
Aaron Ballman76050722014-04-04 15:13:57 +0000540 return isCapabilityExpr(S, E->getSubExpr());
541 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
542 return isCapabilityExpr(S, E->getSubExpr());
543 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
Yi Kong2d58d192017-12-14 22:24:45 +0000544 if (E->getOpcode() == UO_LNot || E->getOpcode() == UO_AddrOf ||
545 E->getOpcode() == UO_Deref)
Aaron Ballman76050722014-04-04 15:13:57 +0000546 return isCapabilityExpr(S, E->getSubExpr());
547 return false;
548 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
549 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
550 return isCapabilityExpr(S, E->getLHS()) &&
551 isCapabilityExpr(S, E->getRHS());
552 return false;
553 }
554
Yi Kong2d58d192017-12-14 22:24:45 +0000555 return typeHasCapability(S, Ex->getType());
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000556}
557
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000558/// \brief Checks that all attribute arguments, starting from Sidx, resolve to
559/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000560/// \param Sidx The attribute argument index to start checking with.
561/// \param ParamIdxOk Whether an argument can be indexing into a function
562/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000563static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000564 const AttributeList &AL,
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000565 SmallVectorImpl<Expr *> &Args,
566 int Sidx = 0,
567 bool ParamIdxOk = false) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000568 for (unsigned Idx = Sidx; Idx < AL.getNumArgs(); ++Idx) {
569 Expr *ArgExp = AL.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000570
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000571 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000572 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000573 Args.push_back(ArgExp);
574 continue;
575 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000576
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000577 if (const auto *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000578 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000579 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000580 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000581 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000582 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000583 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000584 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000585
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000586 // We allow constant strings to be used as a placeholder for expressions
587 // that are not valid C++ syntax, but warn that they are ignored.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000588 S.Diag(AL.getLoc(), diag::warn_thread_attribute_ignored) << AL.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000589 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000590 continue;
591 }
592
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000593 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000594
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000595 // A pointer to member expression of the form &MyClass::mu is treated
596 // specially -- we need to look at the type of the member.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000597 if (const auto *UOp = dyn_cast<UnaryOperator>(ArgExp))
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000598 if (UOp->getOpcode() == UO_AddrOf)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000599 if (const auto *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000600 if (DRE->getDecl()->isCXXInstanceMember())
601 ArgTy = DRE->getDecl()->getType();
602
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000603 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000604 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000605
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000606 // Now check if we index into a record type function param.
Josh Gao55afa752017-08-11 07:54:35 +0000607 if(!RT && ParamIdxOk) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000608 const auto *FD = dyn_cast<FunctionDecl>(D);
609 const auto *IL = dyn_cast<IntegerLiteral>(ArgExp);
Josh Gao55afa752017-08-11 07:54:35 +0000610 if(FD && IL) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000611 unsigned int NumParams = FD->getNumParams();
612 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000613 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
614 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000615 if (!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
616 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_range)
617 << AL.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000618 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000619 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000620 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000621 }
622 }
623
Aaron Ballman76050722014-04-04 15:13:57 +0000624 // If the type does not have a capability, see if the components of the
625 // expression have capabilities. This allows for writing C code where the
626 // capability may be on the type, and the expression is a capability
627 // boolean logic expression. Eg) requires_capability(A || B && !C)
628 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000629 S.Diag(AL.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
630 << AL.getName() << ArgTy;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000631
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000632 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000633 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000634}
635
Chris Lattner58418ff2008-06-29 00:16:31 +0000636//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000637// Attribute Implementations
638//===----------------------------------------------------------------------===//
639
Michael Hana9171bc2012-08-03 17:40:43 +0000640static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000641 const AttributeList &AL) {
642 if (!threadSafetyCheckIsPointer(S, D, AL))
Michael Han3be3b442012-07-23 18:48:41 +0000643 return;
644
Michael Han99315932013-01-24 16:46:58 +0000645 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000646 PtGuardedVarAttr(AL.getRange(), S.Context,
647 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000648}
649
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000650static bool checkGuardedByAttrCommon(Sema &S, Decl *D, const AttributeList &AL,
651 Expr *&Arg) {
652 SmallVector<Expr *, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000653 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000654 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000655 unsigned Size = Args.size();
656 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000657 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000658
Michael Han3be3b442012-07-23 18:48:41 +0000659 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000660
Michael Han3be3b442012-07-23 18:48:41 +0000661 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000662}
663
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000664static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &AL) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000665 Expr *Arg = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000666 if (!checkGuardedByAttrCommon(S, D, AL, Arg))
Michael Han3be3b442012-07-23 18:48:41 +0000667 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000668
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000669 D->addAttr(::new (S.Context) GuardedByAttr(
670 AL.getRange(), S.Context, Arg, AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000671}
672
Michael Hana9171bc2012-08-03 17:40:43 +0000673static void handlePtGuardedByAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000674 const AttributeList &AL) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000675 Expr *Arg = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000676 if (!checkGuardedByAttrCommon(S, D, AL, Arg))
Michael Han3be3b442012-07-23 18:48:41 +0000677 return;
678
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000679 if (!threadSafetyCheckIsPointer(S, D, AL))
Michael Han3be3b442012-07-23 18:48:41 +0000680 return;
681
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000682 D->addAttr(::new (S.Context) PtGuardedByAttr(
683 AL.getRange(), S.Context, Arg, AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000684}
685
Michael Hana9171bc2012-08-03 17:40:43 +0000686static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000687 const AttributeList &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000688 SmallVectorImpl<Expr *> &Args) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000689 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000690 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000691
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000692 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000693 QualType QT = cast<ValueDecl>(D)->getType();
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000694 if (!QT->isDependentType() && !typeHasCapability(S, QT)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000695 S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
696 << AL.getName();
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000697 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000698 }
699
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000700 // Check that all arguments are lockable objects.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000701 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000702 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000703 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000704
Michael Han3be3b442012-07-23 18:48:41 +0000705 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000706}
707
Michael Hana9171bc2012-08-03 17:40:43 +0000708static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000709 const AttributeList &AL) {
710 SmallVector<Expr *, 1> Args;
711 if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000712 return;
713
714 Expr **StartArg = &Args[0];
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000715 D->addAttr(::new (S.Context) AcquiredAfterAttr(
716 AL.getRange(), S.Context, StartArg, Args.size(),
717 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000718}
719
Michael Hana9171bc2012-08-03 17:40:43 +0000720static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000721 const AttributeList &AL) {
722 SmallVector<Expr *, 1> Args;
723 if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000724 return;
725
726 Expr **StartArg = &Args[0];
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000727 D->addAttr(::new (S.Context) AcquiredBeforeAttr(
728 AL.getRange(), S.Context, StartArg, Args.size(),
729 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000730}
731
Michael Hana9171bc2012-08-03 17:40:43 +0000732static bool checkLockFunAttrCommon(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000733 const AttributeList &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000734 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000735 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000736 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000737 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000738
Michael Han3be3b442012-07-23 18:48:41 +0000739 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000740}
741
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000742static void handleAssertSharedLockAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000743 const AttributeList &AL) {
744 SmallVector<Expr *, 1> Args;
745 if (!checkLockFunAttrCommon(S, D, AL, Args))
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000746 return;
747
748 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000749 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000750 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000751 AssertSharedLockAttr(AL.getRange(), S.Context, StartArg, Size,
752 AL.getAttributeSpellingListIndex()));
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000753}
754
755static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000756 const AttributeList &AL) {
757 SmallVector<Expr *, 1> Args;
758 if (!checkLockFunAttrCommon(S, D, AL, Args))
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000759 return;
760
761 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000762 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000763 D->addAttr(::new (S.Context) AssertExclusiveLockAttr(
764 AL.getRange(), S.Context, StartArg, Size,
765 AL.getAttributeSpellingListIndex()));
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000766}
767
Erich Keane623efd82017-03-30 21:48:55 +0000768/// \brief Checks to be sure that the given parameter number is in bounds, and is
769/// an integral type. Will emit appropriate diagnostics if this returns
George Burgess IVe3763372016-12-22 02:50:20 +0000770/// false.
771///
772/// FuncParamNo is expected to be from the user, so is base-1. AttrArgNo is used
773/// to actually retrieve the argument, so it's base-0.
Erich Keane623efd82017-03-30 21:48:55 +0000774template <typename AttrInfo>
775static bool checkParamIsIntegerType(Sema &S, const FunctionDecl *FD,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000776 const AttrInfo &AI, Expr *AttrArg,
Erich Keane623efd82017-03-30 21:48:55 +0000777 unsigned FuncParamNo, unsigned AttrArgNo,
778 bool AllowDependentType = false) {
779 uint64_t Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000780 if (!checkFunctionOrMethodParameterIndex(S, FD, AI, FuncParamNo, AttrArg,
Erich Keane623efd82017-03-30 21:48:55 +0000781 Idx))
782 return false;
783
784 const ParmVarDecl *Param = FD->getParamDecl(Idx);
785 if (AllowDependentType && Param->getType()->isDependentType())
786 return true;
787 if (!Param->getType()->isIntegerType() && !Param->getType()->isCharType()) {
788 SourceLocation SrcLoc = AttrArg->getLocStart();
789 S.Diag(SrcLoc, diag::err_attribute_integers_only)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000790 << getAttrName(AI) << Param->getSourceRange();
Erich Keane623efd82017-03-30 21:48:55 +0000791 return false;
792 }
793 return true;
794}
795
796/// \brief Checks to be sure that the given parameter number is in bounds, and is
797/// an integral type. Will emit appropriate diagnostics if this returns false.
798///
799/// FuncParamNo is expected to be from the user, so is base-1. AttrArgNo is used
800/// to actually retrieve the argument, so it's base-0.
George Burgess IVe3763372016-12-22 02:50:20 +0000801static bool checkParamIsIntegerType(Sema &S, const FunctionDecl *FD,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000802 const AttributeList &AL,
Erich Keane623efd82017-03-30 21:48:55 +0000803 unsigned FuncParamNo, unsigned AttrArgNo,
804 bool AllowDependentType = false) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000805 assert(AL.isArgExpr(AttrArgNo) && "Expected expression argument");
806 return checkParamIsIntegerType(S, FD, AL, AL.getArgAsExpr(AttrArgNo),
Erich Keane623efd82017-03-30 21:48:55 +0000807 FuncParamNo, AttrArgNo, AllowDependentType);
George Burgess IVe3763372016-12-22 02:50:20 +0000808}
809
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000810static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &AL) {
811 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
812 !checkAttributeAtMostNumArgs(S, AL, 2))
George Burgess IVe3763372016-12-22 02:50:20 +0000813 return;
814
815 const auto *FD = cast<FunctionDecl>(D);
816 if (!FD->getReturnType()->isPointerType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000817 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
818 << AL.getName();
George Burgess IVe3763372016-12-22 02:50:20 +0000819 return;
820 }
821
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000822 const Expr *SizeExpr = AL.getArgAsExpr(0);
George Burgess IVe3763372016-12-22 02:50:20 +0000823 int SizeArgNo;
Simon Pilgrim27cc0542017-02-15 15:12:06 +0000824 // Parameter indices are 1-indexed, hence Index=1
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000825 if (!checkPositiveIntArgument(S, AL, SizeExpr, SizeArgNo, /*Index=*/1))
George Burgess IVe3763372016-12-22 02:50:20 +0000826 return;
827
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000828 if (!checkParamIsIntegerType(S, FD, AL, SizeArgNo, /*AttrArgNo=*/0))
George Burgess IVe3763372016-12-22 02:50:20 +0000829 return;
830
831 // Args are 1-indexed, so 0 implies that the arg was not present
832 int NumberArgNo = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000833 if (AL.getNumArgs() == 2) {
834 const Expr *NumberExpr = AL.getArgAsExpr(1);
Simon Pilgrim27cc0542017-02-15 15:12:06 +0000835 // Parameter indices are 1-based, hence Index=2
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000836 if (!checkPositiveIntArgument(S, AL, NumberExpr, NumberArgNo,
George Burgess IVe3763372016-12-22 02:50:20 +0000837 /*Index=*/2))
838 return;
839
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000840 if (!checkParamIsIntegerType(S, FD, AL, NumberArgNo, /*AttrArgNo=*/1))
George Burgess IVe3763372016-12-22 02:50:20 +0000841 return;
842 }
843
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000844 D->addAttr(::new (S.Context)
845 AllocSizeAttr(AL.getRange(), S.Context, SizeArgNo, NumberArgNo,
846 AL.getAttributeSpellingListIndex()));
George Burgess IVe3763372016-12-22 02:50:20 +0000847}
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000848
Michael Hana9171bc2012-08-03 17:40:43 +0000849static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000850 const AttributeList &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000851 SmallVectorImpl<Expr *> &Args) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000852 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000853 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000854
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000855 if (!isIntOrBool(AL.getArgAsExpr(0))) {
856 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
857 << AL.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000858 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000859 }
860
861 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000862 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000863
Michael Han3be3b442012-07-23 18:48:41 +0000864 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000865}
866
Michael Hana9171bc2012-08-03 17:40:43 +0000867static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000868 const AttributeList &AL) {
Michael Han3be3b442012-07-23 18:48:41 +0000869 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000870 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000871 return;
872
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000873 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(
874 AL.getRange(), S.Context, AL.getArgAsExpr(0), Args.data(), Args.size(),
875 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000876}
877
Michael Hana9171bc2012-08-03 17:40:43 +0000878static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000879 const AttributeList &AL) {
Michael Han3be3b442012-07-23 18:48:41 +0000880 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000881 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000882 return;
883
Nico Weber462fd1e2015-01-07 23:50:05 +0000884 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000885 AL.getRange(), S.Context, AL.getArgAsExpr(0), Args.data(),
886 Args.size(), AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000887}
888
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000889static void handleLockReturnedAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000890 const AttributeList &AL) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000891 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000892 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000893 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000894 unsigned Size = Args.size();
895 if (Size == 0)
896 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000897
Michael Han99315932013-01-24 16:46:58 +0000898 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000899 LockReturnedAttr(AL.getRange(), S.Context, Args[0],
900 AL.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000901}
902
903static void handleLocksExcludedAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000904 const AttributeList &AL) {
905 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000906 return;
907
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000908 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000909 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000910 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000911 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000912 if (Size == 0)
913 return;
914 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000915
Michael Han99315932013-01-24 16:46:58 +0000916 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000917 LocksExcludedAttr(AL.getRange(), S.Context, StartArg, Size,
918 AL.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000919}
920
George Burgess IV177399e2017-01-09 04:12:14 +0000921static bool checkFunctionConditionAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000922 const AttributeList &AL,
George Burgess IV177399e2017-01-09 04:12:14 +0000923 Expr *&Cond, StringRef &Msg) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000924 Cond = AL.getArgAsExpr(0);
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000925 if (!Cond->isTypeDependent()) {
926 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
927 if (Converted.isInvalid())
George Burgess IV177399e2017-01-09 04:12:14 +0000928 return false;
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000929 Cond = Converted.get();
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000930 }
931
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000932 if (!S.checkStringLiteralArgumentAttr(AL, 1, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +0000933 return false;
934
935 if (Msg.empty())
936 Msg = "<no message provided>";
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000937
938 SmallVector<PartialDiagnosticAt, 8> Diags;
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +0000939 if (isa<FunctionDecl>(D) && !Cond->isValueDependent() &&
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000940 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
941 Diags)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000942 S.Diag(AL.getLoc(), diag::err_attr_cond_never_constant_expr)
943 << AL.getName();
George Burgess IV0d546532016-11-10 21:47:12 +0000944 for (const PartialDiagnosticAt &PDiag : Diags)
945 S.Diag(PDiag.first, PDiag.second);
George Burgess IV177399e2017-01-09 04:12:14 +0000946 return false;
947 }
948 return true;
949}
950
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000951static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &AL) {
952 S.Diag(AL.getLoc(), diag::ext_clang_enable_if);
George Burgess IV177399e2017-01-09 04:12:14 +0000953
954 Expr *Cond;
955 StringRef Msg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000956 if (checkFunctionConditionAttr(S, D, AL, Cond, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +0000957 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000958 EnableIfAttr(AL.getRange(), S.Context, Cond, Msg,
959 AL.getAttributeSpellingListIndex()));
George Burgess IV177399e2017-01-09 04:12:14 +0000960}
961
962namespace {
963/// Determines if a given Expr references any of the given function's
964/// ParmVarDecls, or the function's implicit `this` parameter (if applicable).
965class ArgumentDependenceChecker
966 : public RecursiveASTVisitor<ArgumentDependenceChecker> {
967#ifndef NDEBUG
968 const CXXRecordDecl *ClassType;
969#endif
970 llvm::SmallPtrSet<const ParmVarDecl *, 16> Parms;
971 bool Result;
972
973public:
974 ArgumentDependenceChecker(const FunctionDecl *FD) {
975#ifndef NDEBUG
976 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
977 ClassType = MD->getParent();
978 else
979 ClassType = nullptr;
980#endif
981 Parms.insert(FD->param_begin(), FD->param_end());
982 }
983
984 bool referencesArgs(Expr *E) {
985 Result = false;
986 TraverseStmt(E);
987 return Result;
988 }
989
990 bool VisitCXXThisExpr(CXXThisExpr *E) {
991 assert(E->getType()->getPointeeCXXRecordDecl() == ClassType &&
992 "`this` doesn't refer to the enclosing class?");
993 Result = true;
994 return false;
995 }
996
997 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
998 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
999 if (Parms.count(PVD)) {
1000 Result = true;
1001 return false;
1002 }
1003 return true;
1004 }
1005};
1006}
1007
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001008static void handleDiagnoseIfAttr(Sema &S, Decl *D, const AttributeList &AL) {
1009 S.Diag(AL.getLoc(), diag::ext_clang_diagnose_if);
George Burgess IV177399e2017-01-09 04:12:14 +00001010
1011 Expr *Cond;
1012 StringRef Msg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001013 if (!checkFunctionConditionAttr(S, D, AL, Cond, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +00001014 return;
1015
1016 StringRef DiagTypeStr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001017 if (!S.checkStringLiteralArgumentAttr(AL, 2, DiagTypeStr))
George Burgess IV177399e2017-01-09 04:12:14 +00001018 return;
1019
1020 DiagnoseIfAttr::DiagnosticType DiagType;
1021 if (!DiagnoseIfAttr::ConvertStrToDiagnosticType(DiagTypeStr, DiagType)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001022 S.Diag(AL.getArgAsExpr(2)->getLocStart(),
George Burgess IV177399e2017-01-09 04:12:14 +00001023 diag::err_diagnose_if_invalid_diagnostic_type);
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001024 return;
1025 }
1026
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00001027 bool ArgDependent = false;
Argyrios Kyrtzidis5f0c0aa2017-05-24 18:35:01 +00001028 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00001029 ArgDependent = ArgumentDependenceChecker(FD).referencesArgs(Cond);
George Burgess IV177399e2017-01-09 04:12:14 +00001030 D->addAttr(::new (S.Context) DiagnoseIfAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001031 AL.getRange(), S.Context, Cond, Msg, DiagType, ArgDependent,
1032 cast<NamedDecl>(D), AL.getAttributeSpellingListIndex()));
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001033}
1034
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001035static void handlePassObjectSizeAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001036 const AttributeList &AL) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001037 if (D->hasAttr<PassObjectSizeAttr>()) {
1038 S.Diag(D->getLocStart(), diag::err_attribute_only_once_per_parameter)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001039 << AL.getName();
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001040 return;
1041 }
1042
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001043 Expr *E = AL.getArgAsExpr(0);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001044 uint32_t Type;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001045 if (!checkUInt32Argument(S, AL, E, Type, /*Idx=*/1))
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001046 return;
1047
1048 // pass_object_size's argument is passed in as the second argument of
1049 // __builtin_object_size. So, it has the same constraints as that second
1050 // argument; namely, it must be in the range [0, 3].
1051 if (Type > 3) {
1052 S.Diag(E->getLocStart(), diag::err_attribute_argument_outof_range)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001053 << AL.getName() << 0 << 3 << E->getSourceRange();
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001054 return;
1055 }
1056
1057 // pass_object_size is only supported on constant pointer parameters; as a
1058 // kindness to users, we allow the parameter to be non-const for declarations.
1059 // At this point, we have no clue if `D` belongs to a function declaration or
1060 // definition, so we defer the constness check until later.
1061 if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) {
1062 S.Diag(D->getLocStart(), diag::err_attribute_pointers_only)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001063 << AL.getName() << 1;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001064 return;
1065 }
1066
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001067 D->addAttr(::new (S.Context) PassObjectSizeAttr(
1068 AL.getRange(), S.Context, (int)Type, AL.getAttributeSpellingListIndex()));
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001069}
1070
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001071static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &AL) {
David Blaikie16f76d22013-09-06 01:28:43 +00001072 ConsumableAttr::ConsumedState DefaultState;
1073
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001074 if (AL.isArgIdent(0)) {
1075 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00001076 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1077 DefaultState)) {
1078 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001079 << AL.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +00001080 return;
1081 }
David Blaikie16f76d22013-09-06 01:28:43 +00001082 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001083 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1084 << AL.getName() << AANT_ArgumentIdentifier;
David Blaikie16f76d22013-09-06 01:28:43 +00001085 return;
1086 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001087
1088 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001089 ConsumableAttr(AL.getRange(), S.Context, DefaultState,
1090 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001091}
1092
1093static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001094 const AttributeList &AL) {
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001095 ASTContext &CurrContext = S.getASTContext();
1096 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
1097
1098 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1099 if (!RD->hasAttr<ConsumableAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001100 S.Diag(AL.getLoc(), diag::warn_attr_on_unconsumable_class) <<
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001101 RD->getNameAsString();
1102
1103 return false;
1104 }
1105 }
1106
1107 return true;
1108}
1109
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001110static void handleCallableWhenAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001111 const AttributeList &AL) {
1112 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001113 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001114
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001115 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001116 return;
1117
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001118 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001119 for (unsigned ArgIndex = 0; ArgIndex < AL.getNumArgs(); ++ArgIndex) {
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001120 CallableWhenAttr::ConsumedState CallableState;
1121
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001122 StringRef StateString;
1123 SourceLocation Loc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001124 if (AL.isArgIdent(ArgIndex)) {
1125 IdentifierLoc *Ident = AL.getArgAsIdent(ArgIndex);
Aaron Ballman55ef1512014-12-19 16:42:04 +00001126 StateString = Ident->Ident->getName();
1127 Loc = Ident->Loc;
1128 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001129 if (!S.checkStringLiteralArgumentAttr(AL, ArgIndex, StateString, &Loc))
Aaron Ballman55ef1512014-12-19 16:42:04 +00001130 return;
1131 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001132
1133 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +00001134 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001135 S.Diag(Loc, diag::warn_attribute_type_not_supported)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001136 << AL.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001137 return;
1138 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001139
1140 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001141 }
1142
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001143 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001144 CallableWhenAttr(AL.getRange(), S.Context, States.data(),
1145 States.size(), AL.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001146}
1147
DeLesley Hutchins69391772013-10-17 23:23:53 +00001148static void handleParamTypestateAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001149 const AttributeList &AL) {
DeLesley Hutchins69391772013-10-17 23:23:53 +00001150 ParamTypestateAttr::ConsumedState ParamState;
1151
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001152 if (AL.isArgIdent(0)) {
1153 IdentifierLoc *Ident = AL.getArgAsIdent(0);
DeLesley Hutchins69391772013-10-17 23:23:53 +00001154 StringRef StateString = Ident->Ident->getName();
1155
1156 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
1157 ParamState)) {
1158 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001159 << AL.getName() << StateString;
DeLesley Hutchins69391772013-10-17 23:23:53 +00001160 return;
1161 }
1162 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001163 S.Diag(AL.getLoc(), diag::err_attribute_argument_type) <<
1164 AL.getName() << AANT_ArgumentIdentifier;
DeLesley Hutchins69391772013-10-17 23:23:53 +00001165 return;
1166 }
1167
1168 // FIXME: This check is currently being done in the analysis. It can be
1169 // enabled here only after the parser propagates attributes at
1170 // template specialization definition, not declaration.
1171 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
1172 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1173 //
1174 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001175 // S.Diag(AL.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
DeLesley Hutchins69391772013-10-17 23:23:53 +00001176 // ReturnType.getAsString();
1177 // return;
1178 //}
1179
1180 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001181 ParamTypestateAttr(AL.getRange(), S.Context, ParamState,
1182 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins69391772013-10-17 23:23:53 +00001183}
1184
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001185static void handleReturnTypestateAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001186 const AttributeList &AL) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001187 ReturnTypestateAttr::ConsumedState ReturnState;
1188
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001189 if (AL.isArgIdent(0)) {
1190 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00001191 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1192 ReturnState)) {
1193 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001194 << AL.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001195 return;
1196 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001197 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001198 S.Diag(AL.getLoc(), diag::err_attribute_argument_type) <<
1199 AL.getName() << AANT_ArgumentIdentifier;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001200 return;
1201 }
1202
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001203 // FIXME: This check is currently being done in the analysis. It can be
1204 // enabled here only after the parser propagates attributes at
1205 // template specialization definition, not declaration.
1206 //QualType ReturnType;
1207 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001208 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1209 // ReturnType = Param->getType();
1210 //
1211 //} else if (const CXXConstructorDecl *Constructor =
1212 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001213 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1214 //
1215 //} else {
1216 //
1217 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1218 //}
1219 //
1220 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1221 //
1222 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1223 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1224 // ReturnType.getAsString();
1225 // return;
1226 //}
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001227
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001228 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001229 ReturnTypestateAttr(AL.getRange(), S.Context, ReturnState,
1230 AL.getAttributeSpellingListIndex()));
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001231}
1232
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001233static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &AL) {
1234 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001235 return;
1236
1237 SetTypestateAttr::ConsumedState NewState;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001238 if (AL.isArgIdent(0)) {
1239 IdentifierLoc *Ident = AL.getArgAsIdent(0);
Aaron Ballman91c98e12013-10-14 23:22:37 +00001240 StringRef Param = Ident->Ident->getName();
1241 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1242 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001243 << AL.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001244 return;
1245 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001246 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001247 S.Diag(AL.getLoc(), diag::err_attribute_argument_type) <<
1248 AL.getName() << AANT_ArgumentIdentifier;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001249 return;
1250 }
1251
1252 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001253 SetTypestateAttr(AL.getRange(), S.Context, NewState,
1254 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001255}
1256
Chris Wailes9385f9f2013-10-29 20:28:41 +00001257static void handleTestTypestateAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001258 const AttributeList &AL) {
1259 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001260 return;
1261
Chris Wailes9385f9f2013-10-29 20:28:41 +00001262 TestTypestateAttr::ConsumedState TestState;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001263 if (AL.isArgIdent(0)) {
1264 IdentifierLoc *Ident = AL.getArgAsIdent(0);
Aaron Ballman91c98e12013-10-14 23:22:37 +00001265 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001266 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001267 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001268 << AL.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001269 return;
1270 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001271 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001272 S.Diag(AL.getLoc(), diag::err_attribute_argument_type) <<
1273 AL.getName() << AANT_ArgumentIdentifier;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001274 return;
1275 }
1276
1277 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001278 TestTypestateAttr(AL.getRange(), S.Context, TestState,
1279 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001280}
1281
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001282static void handleExtVectorTypeAttr(Sema &S, Decl *D, const AttributeList &AL) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001283 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001284 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001285}
1286
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001287static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &AL) {
1288 if (auto *TD = dyn_cast<TagDecl>(D))
1289 TD->addAttr(::new (S.Context) PackedAttr(AL.getRange(), S.Context,
1290 AL.getAttributeSpellingListIndex()));
1291 else if (auto *FD = dyn_cast<FieldDecl>(D)) {
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001292 bool BitfieldByteAligned = (!FD->getType()->isDependentType() &&
1293 !FD->getType()->isIncompleteType() &&
1294 FD->isBitField() &&
1295 S.Context.getTypeAlign(FD->getType()) <= 8);
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001296
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001297 if (S.getASTContext().getTargetInfo().getTriple().isPS4()) {
1298 if (BitfieldByteAligned)
1299 // The PS4 target needs to maintain ABI backwards compatibility.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001300 S.Diag(AL.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
1301 << AL.getName() << FD->getType();
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001302 else
1303 FD->addAttr(::new (S.Context) PackedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001304 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001305 } else {
1306 // Report warning about changed offset in the newer compiler versions.
1307 if (BitfieldByteAligned)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001308 S.Diag(AL.getLoc(), diag::warn_attribute_packed_for_bitfield);
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001309
1310 FD->addAttr(::new (S.Context) PackedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001311 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001312 }
1313
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001314 } else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001315 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001316}
1317
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001318static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001319 // The IBOutlet/IBOutletCollection attributes only apply to instance
1320 // variables or properties of Objective-C classes. The outlet must also
1321 // have an object reference type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001322 if (const auto *VD = dyn_cast<ObjCIvarDecl>(D)) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001323 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001324 S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
1325 << AL.getName() << VD->getType() << 0;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001326 return false;
1327 }
1328 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001329 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001330 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001331 S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
1332 << AL.getName() << PD->getType() << 1;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001333 return false;
1334 }
1335 }
1336 else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001337 S.Diag(AL.getLoc(), diag::warn_attribute_iboutlet) << AL.getName();
Ted Kremenek7fd17232011-09-29 07:02:25 +00001338 return false;
1339 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001340
Ted Kremenek7fd17232011-09-29 07:02:25 +00001341 return true;
1342}
1343
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001344static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &AL) {
1345 if (!checkIBOutletCommon(S, D, AL))
Ted Kremenek1f672822010-02-18 03:08:58 +00001346 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001347
Michael Han99315932013-01-24 16:46:58 +00001348 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001349 IBOutletAttr(AL.getRange(), S.Context,
1350 AL.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001351}
1352
Chandler Carruthedc2c642011-07-02 00:01:44 +00001353static void handleIBOutletCollection(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001354 const AttributeList &AL) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001355
1356 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001357 if (AL.getNumArgs() > 1) {
1358 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1359 << AL.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001360 return;
1361 }
1362
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001363 if (!checkIBOutletCommon(S, D, AL))
Ted Kremenek26bde772010-05-19 17:38:06 +00001364 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001365
Richard Smithb1f9a282013-10-31 01:56:18 +00001366 ParsedType PT;
1367
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001368 if (AL.hasParsedType())
1369 PT = AL.getTypeArg();
Richard Smithb1f9a282013-10-31 01:56:18 +00001370 else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001371 PT = S.getTypeName(S.Context.Idents.get("NSObject"), AL.getLoc(),
Richard Smithb1f9a282013-10-31 01:56:18 +00001372 S.getScopeForContext(D->getDeclContext()->getParent()));
1373 if (!PT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001374 S.Diag(AL.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
Richard Smithb1f9a282013-10-31 01:56:18 +00001375 return;
1376 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001377 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001378
Craig Topperc3ec1492014-05-26 06:22:03 +00001379 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001380 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1381 if (!QTLoc)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001382 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, AL.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001383
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001384 // Diagnose use of non-object type in iboutletcollection attribute.
1385 // FIXME. Gnu attribute extension ignores use of builtin types in
1386 // attributes. So, __attribute__((iboutletcollection(char))) will be
1387 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001388 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001389 S.Diag(AL.getLoc(),
Richard Smithb1f9a282013-10-31 01:56:18 +00001390 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1391 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001392 return;
1393 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001394
Michael Han99315932013-01-24 16:46:58 +00001395 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001396 IBOutletCollectionAttr(AL.getRange(), S.Context, QTLoc,
1397 AL.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001398}
1399
Hal Finkelee90a222014-09-26 05:04:30 +00001400bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1401 if (RefOkay) {
1402 if (T->isReferenceType())
1403 return true;
1404 } else {
1405 T = T.getNonReferenceType();
1406 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001407
Hal Finkelee90a222014-09-26 05:04:30 +00001408 // The nonnull attribute, and other similar attributes, can be applied to a
1409 // transparent union that contains a pointer type.
Richard Smith588bd9b2014-08-27 04:59:42 +00001410 if (const RecordType *UT = T->getAsUnionType()) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001411 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1412 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001413 for (const auto *I : UD->fields()) {
1414 QualType QT = I->getType();
Richard Smith588bd9b2014-08-27 04:59:42 +00001415 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1416 return true;
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001417 }
1418 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001419 }
1420
1421 return T->isAnyPointerType() || T->isBlockPointerType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001422}
1423
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001424static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &AL,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001425 SourceRange AttrParmRange,
Hal Finkelee90a222014-09-26 05:04:30 +00001426 SourceRange TypeRange,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001427 bool isReturnValue = false) {
Hal Finkelee90a222014-09-26 05:04:30 +00001428 if (!S.isValidPointerAttrType(T)) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001429 if (isReturnValue)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001430 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
1431 << AL.getName() << AttrParmRange << TypeRange;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001432 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001433 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
1434 << AL.getName() << AttrParmRange << TypeRange << 0;
Ted Kremenek9aedc152014-01-17 06:24:56 +00001435 return false;
1436 }
1437 return true;
1438}
1439
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001440static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001441 SmallVector<unsigned, 8> NonNullArgs;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001442 for (unsigned I = 0; I < AL.getNumArgs(); ++I) {
1443 Expr *Ex = AL.getArgAsExpr(I);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001444 uint64_t Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001445 if (!checkFunctionOrMethodParameterIndex(S, D, AL, I + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001446 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001447
1448 // Is the function argument a pointer type?
Richard Smith588bd9b2014-08-27 04:59:42 +00001449 if (Idx < getFunctionOrMethodNumParams(D) &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001450 !attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), AL,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001451 Ex->getSourceRange(),
1452 getFunctionOrMethodParamRange(D, Idx)))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001453 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001454
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001455 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001456 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001457
1458 // If no arguments were specified to __attribute__((nonnull)) then all pointer
Richard Smith588bd9b2014-08-27 04:59:42 +00001459 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1460 // check if the attribute came from a macro expansion or a template
1461 // instantiation.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001462 if (NonNullArgs.empty() && AL.getLoc().isFileID() &&
Richard Smith51ec0cf2017-02-21 01:17:38 +00001463 !S.inTemplateInstantiation()) {
Richard Smith588bd9b2014-08-27 04:59:42 +00001464 bool AnyPointers = isFunctionOrMethodVariadic(D);
1465 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1466 I != E && !AnyPointers; ++I) {
1467 QualType T = getFunctionOrMethodParamType(D, I);
Hal Finkelee90a222014-09-26 05:04:30 +00001468 if (T->isDependentType() || S.isValidPointerAttrType(T))
Richard Smith588bd9b2014-08-27 04:59:42 +00001469 AnyPointers = true;
Ted Kremenek5fa50522008-11-18 06:52:58 +00001470 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001471
Richard Smith588bd9b2014-08-27 04:59:42 +00001472 if (!AnyPointers)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001473 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001474 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001475
Richard Smith588bd9b2014-08-27 04:59:42 +00001476 unsigned *Start = NonNullArgs.data();
1477 unsigned Size = NonNullArgs.size();
1478 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001479 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001480 NonNullAttr(AL.getRange(), S.Context, Start, Size,
1481 AL.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001482}
1483
Jordan Rosec9399072014-02-11 17:27:59 +00001484static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001485 const AttributeList &AL) {
1486 if (AL.getNumArgs() > 0) {
Jordan Rosec9399072014-02-11 17:27:59 +00001487 if (D->getFunctionType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001488 handleNonNullAttr(S, D, AL);
Jordan Rosec9399072014-02-11 17:27:59 +00001489 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001490 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
Jordan Rosec9399072014-02-11 17:27:59 +00001491 << D->getSourceRange();
1492 }
1493 return;
1494 }
1495
1496 // Is the argument a pointer type?
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001497 if (!attrNonNullArgCheck(S, D->getType(), AL, SourceRange(),
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001498 D->getSourceRange()))
Jordan Rosec9399072014-02-11 17:27:59 +00001499 return;
1500
1501 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001502 NonNullAttr(AL.getRange(), S.Context, nullptr, 0,
1503 AL.getAttributeSpellingListIndex()));
Jordan Rosec9399072014-02-11 17:27:59 +00001504}
1505
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001506static void handleReturnsNonNullAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001507 const AttributeList &AL) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001508 QualType ResultType = getFunctionOrMethodResultType(D);
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001509 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001510 if (!attrNonNullArgCheck(S, ResultType, AL, SourceRange(), SR,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001511 /* isReturnValue */ true))
1512 return;
1513
1514 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001515 ReturnsNonNullAttr(AL.getRange(), S.Context,
1516 AL.getAttributeSpellingListIndex()));
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001517}
1518
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001519static void handleNoEscapeAttr(Sema &S, Decl *D, const AttributeList &AL) {
Akira Hatanaka98a49332017-09-22 00:41:05 +00001520 if (D->isInvalidDecl())
1521 return;
1522
1523 // noescape only applies to pointer types.
1524 QualType T = cast<ParmVarDecl>(D)->getType();
1525 if (!S.isValidPointerAttrType(T, /* RefOkay */ true)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001526 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
1527 << AL.getName() << AL.getRange() << 0;
Akira Hatanaka98a49332017-09-22 00:41:05 +00001528 return;
1529 }
1530
1531 D->addAttr(::new (S.Context) NoEscapeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001532 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Akira Hatanaka98a49332017-09-22 00:41:05 +00001533}
1534
Hal Finkelee90a222014-09-26 05:04:30 +00001535static void handleAssumeAlignedAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001536 const AttributeList &AL) {
1537 Expr *E = AL.getArgAsExpr(0),
1538 *OE = AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr;
1539 S.AddAssumeAlignedAttr(AL.getRange(), D, E, OE,
1540 AL.getAttributeSpellingListIndex());
Hal Finkelee90a222014-09-26 05:04:30 +00001541}
1542
Erich Keane623efd82017-03-30 21:48:55 +00001543static void handleAllocAlignAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001544 const AttributeList &AL) {
1545 S.AddAllocAlignAttr(AL.getRange(), D, AL.getArgAsExpr(0),
1546 AL.getAttributeSpellingListIndex());
Erich Keane623efd82017-03-30 21:48:55 +00001547}
1548
Hal Finkelee90a222014-09-26 05:04:30 +00001549void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
1550 Expr *OE, unsigned SpellingListIndex) {
1551 QualType ResultType = getFunctionOrMethodResultType(D);
1552 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1553
1554 AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex);
1555 SourceLocation AttrLoc = AttrRange.getBegin();
1556
1557 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1558 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1559 << &TmpAttr << AttrRange << SR;
1560 return;
1561 }
1562
1563 if (!E->isValueDependent()) {
1564 llvm::APSInt I(64);
1565 if (!E->isIntegerConstantExpr(I, Context)) {
1566 if (OE)
1567 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1568 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1569 << E->getSourceRange();
1570 else
1571 Diag(AttrLoc, diag::err_attribute_argument_type)
1572 << &TmpAttr << AANT_ArgumentIntegerConstant
1573 << E->getSourceRange();
1574 return;
1575 }
1576
1577 if (!I.isPowerOf2()) {
1578 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1579 << E->getSourceRange();
1580 return;
1581 }
1582 }
1583
1584 if (OE) {
1585 if (!OE->isValueDependent()) {
1586 llvm::APSInt I(64);
1587 if (!OE->isIntegerConstantExpr(I, Context)) {
1588 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1589 << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1590 << OE->getSourceRange();
1591 return;
1592 }
1593 }
1594 }
1595
1596 D->addAttr(::new (Context)
1597 AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex));
1598}
1599
Erich Keane623efd82017-03-30 21:48:55 +00001600void Sema::AddAllocAlignAttr(SourceRange AttrRange, Decl *D, Expr *ParamExpr,
1601 unsigned SpellingListIndex) {
1602 QualType ResultType = getFunctionOrMethodResultType(D);
1603
1604 AllocAlignAttr TmpAttr(AttrRange, Context, 0, SpellingListIndex);
1605 SourceLocation AttrLoc = AttrRange.getBegin();
1606
1607 if (!ResultType->isDependentType() &&
1608 !isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1609 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1610 << &TmpAttr << AttrRange << getFunctionOrMethodResultSourceRange(D);
1611 return;
1612 }
1613
1614 uint64_t IndexVal;
1615 const auto *FuncDecl = cast<FunctionDecl>(D);
1616 if (!checkFunctionOrMethodParameterIndex(*this, FuncDecl, TmpAttr,
1617 /*AttrArgNo=*/1, ParamExpr,
1618 IndexVal))
1619 return;
1620
1621 QualType Ty = getFunctionOrMethodParamType(D, IndexVal);
1622 if (!Ty->isDependentType() && !Ty->isIntegralType(Context)) {
1623 Diag(ParamExpr->getLocStart(), diag::err_attribute_integers_only)
1624 << &TmpAttr << FuncDecl->getParamDecl(IndexVal)->getSourceRange();
1625 return;
1626 }
1627
1628 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
1629 // because that has corrected for the implicit this parameter, and is zero-
1630 // based. The attribute expects what the user wrote explicitly.
1631 llvm::APSInt Val;
1632 ParamExpr->EvaluateAsInt(Val, Context);
1633
1634 D->addAttr(::new (Context) AllocAlignAttr(
1635 AttrRange, Context, Val.getZExtValue(), SpellingListIndex));
1636}
1637
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001638/// Normalize the attribute, __foo__ becomes foo.
1639/// Returns true if normalization was applied.
1640static bool normalizeName(StringRef &AttrName) {
Aaron Ballman62692362015-10-09 13:53:24 +00001641 if (AttrName.size() > 4 && AttrName.startswith("__") &&
1642 AttrName.endswith("__")) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001643 AttrName = AttrName.drop_front(2).drop_back(2);
1644 return true;
1645 }
1646 return false;
1647}
1648
Chandler Carruthedc2c642011-07-02 00:01:44 +00001649static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001650 // This attribute must be applied to a function declaration. The first
1651 // argument to the attribute must be an identifier, the name of the resource,
1652 // for example: malloc. The following arguments must be argument indexes, the
1653 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001654 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001655 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001656 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001657
Aaron Ballman00e99962013-08-31 01:11:41 +00001658 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001659 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001660 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001661 return;
1662 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001663
Richard Smith852e9ce2013-11-27 01:46:48 +00001664 // Figure out our Kind.
1665 OwnershipAttr::OwnershipKind K =
Craig Topperc3ec1492014-05-26 06:22:03 +00001666 OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0,
Richard Smith852e9ce2013-11-27 01:46:48 +00001667 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001668
Richard Smith852e9ce2013-11-27 01:46:48 +00001669 // Check arguments.
1670 switch (K) {
1671 case OwnershipAttr::Takes:
1672 case OwnershipAttr::Holds:
1673 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001674 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1675 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001676 return;
1677 }
1678 break;
1679 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001680 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001681 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1682 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001683 return;
1684 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001685 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001686 }
1687
Richard Smith852e9ce2013-11-27 01:46:48 +00001688 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001689
Richard Smith852e9ce2013-11-27 01:46:48 +00001690 StringRef ModuleName = Module->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001691 if (normalizeName(ModuleName)) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001692 Module = &S.PP.getIdentifierTable().get(ModuleName);
1693 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001694
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001695 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001696 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1697 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001698 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001699 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001700 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001701
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001702 // Is the function argument a pointer type?
Alp Toker601b22c2014-01-21 23:35:24 +00001703 QualType T = getFunctionOrMethodParamType(D, Idx);
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001704 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001705 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001706 case OwnershipAttr::Takes:
1707 case OwnershipAttr::Holds:
1708 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1709 Err = 0;
1710 break;
1711 case OwnershipAttr::Returns:
1712 if (!T->isIntegerType())
1713 Err = 1;
1714 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001715 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001716 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001717 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001718 << Ex->getSourceRange();
1719 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001720 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001721
1722 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001723 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001724 // Cannot have two ownership attributes of different kinds for the same
1725 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001726 if (I->getOwnKind() != K && I->args_end() !=
1727 std::find(I->args_begin(), I->args_end(), Idx)) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001728 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001729 << AL.getName() << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001730 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001731 } else if (K == OwnershipAttr::Returns &&
1732 I->getOwnKind() == OwnershipAttr::Returns) {
1733 // A returns attribute conflicts with any other returns attribute using
1734 // a different index. Note, diagnostic reporting is 1-based, but stored
1735 // argument indexes are 0-based.
1736 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1737 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
1738 << *(I->args_begin()) + 1;
1739 if (I->args_size())
1740 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
1741 << (unsigned)Idx + 1 << Ex->getSourceRange();
1742 return;
1743 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001744 }
1745 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001746 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001747 }
1748
1749 unsigned* start = OwnershipArgs.data();
1750 unsigned size = OwnershipArgs.size();
1751 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001752
Michael Han99315932013-01-24 16:46:58 +00001753 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001754 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001755 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001756}
1757
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001758static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &AL) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001759 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001760 if (AL.getNumArgs() > 1) {
1761 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1762 << AL.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001763 return;
1764 }
1765
1766 // gcc rejects
1767 // class c {
1768 // static int a __attribute__((weakref ("v2")));
1769 // static int b() __attribute__((weakref ("f3")));
1770 // };
1771 // and ignores the attributes of
1772 // void f(void) {
1773 // static int a __attribute__((weakref ("v2")));
1774 // }
1775 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001776 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001777 if (!Ctx->isFileContext()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001778 S.Diag(AL.getLoc(), diag::err_attribute_weakref_not_global_context)
1779 << cast<NamedDecl>(D);
Sebastian Redl50c68252010-08-31 00:36:30 +00001780 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001781 }
1782
1783 // The GCC manual says
1784 //
1785 // At present, a declaration to which `weakref' is attached can only
1786 // be `static'.
1787 //
1788 // It also says
1789 //
1790 // Without a TARGET,
1791 // given as an argument to `weakref' or to `alias', `weakref' is
1792 // equivalent to `weak'.
1793 //
1794 // gcc 4.4.1 will accept
1795 // int a7 __attribute__((weakref));
1796 // as
1797 // int a7 __attribute__((weak));
1798 // This looks like a bug in gcc. We reject that for now. We should revisit
1799 // it if this behaviour is actually used.
1800
Rafael Espindolac18086a2010-02-23 22:00:30 +00001801 // GCC rejects
1802 // static ((alias ("y"), weakref)).
1803 // Should we? How to check that weakref is before or after alias?
1804
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001805 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1806 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1807 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001808 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001809 if (AL.getNumArgs() && S.checkStringLiteralArgumentAttr(AL, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001810 // GCC will accept anything as the argument of weakref. Should we
1811 // check for an existing decl?
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001812 D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str,
1813 AL.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001814
Michael Han99315932013-01-24 16:46:58 +00001815 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001816 WeakRefAttr(AL.getRange(), S.Context,
1817 AL.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001818}
1819
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001820static void handleIFuncAttr(Sema &S, Decl *D, const AttributeList &AL) {
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001821 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001822 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001823 return;
1824
1825 // Aliases should be on declarations, not definitions.
1826 const auto *FD = cast<FunctionDecl>(D);
1827 if (FD->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001828 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 1;
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001829 return;
1830 }
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001831
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001832 D->addAttr(::new (S.Context) IFuncAttr(AL.getRange(), S.Context, Str,
1833 AL.getAttributeSpellingListIndex()));
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001834}
1835
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001836static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &AL) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001837 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001838 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001839 return;
1840
Douglas Gregore8bbc122011-09-02 00:18:52 +00001841 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001842 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_darwin);
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001843 return;
1844 }
Justin Lebara8f0254b2016-01-23 21:28:10 +00001845 if (S.Context.getTargetInfo().getTriple().isNVPTX()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001846 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_nvptx);
Justin Lebara8f0254b2016-01-23 21:28:10 +00001847 }
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001848
David Majnemer2dc81462015-01-19 09:00:28 +00001849 // Aliases should be on declarations, not definitions.
1850 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1851 if (FD->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001852 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 0;
David Majnemer2dc81462015-01-19 09:00:28 +00001853 return;
1854 }
1855 } else {
1856 const auto *VD = cast<VarDecl>(D);
1857 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001858 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << VD << 0;
David Majnemer2dc81462015-01-19 09:00:28 +00001859 return;
1860 }
1861 }
1862
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001863 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001864
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001865 D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str,
1866 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001867}
1868
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001869static void handleColdAttr(Sema &S, Decl *D, const AttributeList &AL) {
1870 if (checkAttrMutualExclusion<HotAttr>(S, D, AL.getRange(), AL.getName()))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001871 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001872
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001873 D->addAttr(::new (S.Context) ColdAttr(AL.getRange(), S.Context,
1874 AL.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001875}
1876
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001877static void handleHotAttr(Sema &S, Decl *D, const AttributeList &AL) {
1878 if (checkAttrMutualExclusion<ColdAttr>(S, D, AL.getRange(), AL.getName()))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001879 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001880
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001881 D->addAttr(::new (S.Context) HotAttr(AL.getRange(), S.Context,
1882 AL.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001883}
1884
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001885static void handleTLSModelAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001886 const AttributeList &AL) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001887 StringRef Model;
1888 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001889 // Check that it is a string.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001890 if (!S.checkStringLiteralArgumentAttr(AL, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001891 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001892
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001893 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001894 if (Model != "global-dynamic" && Model != "local-dynamic"
1895 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001896 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001897 return;
1898 }
1899
Michael Han99315932013-01-24 16:46:58 +00001900 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001901 TLSModelAttr(AL.getRange(), S.Context, Model,
1902 AL.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001903}
1904
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001905static void handleRestrictAttr(Sema &S, Decl *D, const AttributeList &AL) {
David Majnemer631a90b2015-02-04 07:23:21 +00001906 QualType ResultType = getFunctionOrMethodResultType(D);
1907 if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
1908 D->addAttr(::new (S.Context) RestrictAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001909 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
David Majnemer631a90b2015-02-04 07:23:21 +00001910 return;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001911 }
1912
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001913 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
1914 << AL.getName() << getFunctionOrMethodResultSourceRange(D);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001915}
1916
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001917static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &AL) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001918 if (S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001919 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
1920 << AL.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001921 return;
1922 }
1923
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001924 if (CommonAttr *CA = S.mergeCommonAttr(D, AL.getRange(), AL.getName(),
1925 AL.getAttributeSpellingListIndex()))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001926 D->addAttr(CA);
Eric Christopher8a2ee392010-12-02 02:45:55 +00001927}
1928
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001929static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &AL) {
1930 if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, AL.getRange(),
1931 AL.getName()))
Charles Davis0e379112016-08-08 21:19:08 +00001932 return;
1933
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001934 if (AL.isDeclspecAttribute()) {
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00001935 const auto &Triple = S.getASTContext().getTargetInfo().getTriple();
1936 const auto &Arch = Triple.getArch();
1937 if (Arch != llvm::Triple::x86 &&
1938 (Arch != llvm::Triple::arm && Arch != llvm::Triple::thumb)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001939 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_on_arch)
1940 << AL.getName() << Triple.getArchName();
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00001941 return;
1942 }
1943 }
1944
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001945 D->addAttr(::new (S.Context) NakedAttr(AL.getRange(), S.Context,
1946 AL.getAttributeSpellingListIndex()));
Charles Davis0e379112016-08-08 21:19:08 +00001947}
1948
Erich Keaneb11ebc52017-09-27 03:20:13 +00001949static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &Attrs) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001950 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001951
Erich Keaneb11ebc52017-09-27 03:20:13 +00001952 if (S.CheckNoReturnAttr(Attrs))
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001953 return;
John McCall3882ace2011-01-05 12:14:39 +00001954
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001955 if (!isa<ObjCMethodDecl>(D)) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00001956 S.Diag(Attrs.getLoc(), diag::warn_attribute_wrong_decl_type)
1957 << Attrs.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001958 return;
1959 }
1960
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001961 D->addAttr(::new (S.Context) NoReturnAttr(
Erich Keaneb11ebc52017-09-27 03:20:13 +00001962 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001963}
1964
1965static void handleNoCallerSavedRegsAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001966 const AttributeList &AL) {
1967 if (S.CheckNoCallerSavedRegsAttr(AL))
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001968 return;
1969
1970 D->addAttr(::new (S.Context) AnyX86NoCallerSavedRegistersAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001971 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001972}
1973
Erich Keaneb11ebc52017-09-27 03:20:13 +00001974bool Sema::CheckNoReturnAttr(const AttributeList &Attrs) {
1975 if (!checkAttributeNumArgs(*this, Attrs, 0)) {
1976 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00001977 return true;
1978 }
1979
1980 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001981}
1982
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001983bool Sema::CheckNoCallerSavedRegsAttr(const AttributeList &AL) {
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001984 // Check whether the attribute is valid on the current target.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001985 if (!AL.existsInTarget(Context.getTargetInfo())) {
1986 Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) << AL.getName();
1987 AL.setInvalid();
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001988 return true;
1989 }
1990
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001991 if (!checkAttributeNumArgs(*this, AL, 0)) {
1992 AL.setInvalid();
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001993 return true;
1994 }
1995
1996 return false;
1997}
1998
Chandler Carruthedc2c642011-07-02 00:01:44 +00001999static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002000 const AttributeList &AL) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00002001
2002 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
2003 // because 'analyzer_noreturn' does not impact the type.
David Majnemer06864812015-04-07 06:01:53 +00002004 if (!isFunctionOrMethodOrBlock(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002005 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00002006 if (!VD || (!VD->getType()->isBlockPointerType() &&
2007 !VD->getType()->isFunctionPointerType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002008 S.Diag(AL.getLoc(),
2009 AL.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Craig Topper8f7f3ea2015-11-17 05:40:05 +00002010 : diag::warn_attribute_wrong_decl_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002011 << AL.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00002012 return;
2013 }
2014 }
2015
Michael Han99315932013-01-24 16:46:58 +00002016 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002017 AnalyzerNoReturnAttr(AL.getRange(), S.Context,
2018 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002019}
2020
John Thompsoncdb847ba2010-08-09 21:53:52 +00002021// PS3 PPU-specific.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002022static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &AL) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00002023/*
2024 Returning a Vector Class in Registers
2025
Eric Christopherbc638a82010-12-01 22:13:54 +00002026 According to the PPU ABI specifications, a class with a single member of
2027 vector type is returned in memory when used as the return value of a function.
2028 This results in inefficient code when implementing vector classes. To return
2029 the value in a single vector register, add the vecreturn attribute to the
2030 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00002031
2032 Example:
2033
2034 struct Vector
2035 {
2036 __vector float xyzw;
2037 } __attribute__((vecreturn));
2038
2039 Vector Add(Vector lhs, Vector rhs)
2040 {
2041 Vector result;
2042 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
2043 return result; // This will be returned in a register
2044 }
2045*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00002046 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002047 S.Diag(AL.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00002048 return;
2049 }
2050
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002051 const auto *R = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00002052 int count = 0;
2053
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002054 if (!isa<CXXRecordDecl>(R)) {
2055 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00002056 return;
2057 }
2058
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002059 if (!cast<CXXRecordDecl>(R)->isPOD()) {
2060 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
John Thompson9a587aaa2010-09-18 01:12:07 +00002061 return;
2062 }
2063
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002064 for (const auto *I : R->fields()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002065 if ((count == 1) || !I->getType()->isVectorType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002066 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00002067 return;
2068 }
2069 count++;
2070 }
2071
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002072 D->addAttr(::new (S.Context) VecReturnAttr(
2073 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00002074}
2075
Richard Smithe233fbf2013-01-28 22:42:45 +00002076static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002077 const AttributeList &AL) {
Richard Smithe233fbf2013-01-28 22:42:45 +00002078 if (isa<ParmVarDecl>(D)) {
2079 // [[carries_dependency]] can only be applied to a parameter if it is a
2080 // parameter of a function declaration or lambda.
2081 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002082 S.Diag(AL.getLoc(),
Richard Smithe233fbf2013-01-28 22:42:45 +00002083 diag::err_carries_dependency_param_not_function_decl);
2084 return;
2085 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002086 }
Richard Smithe233fbf2013-01-28 22:42:45 +00002087
2088 D->addAttr(::new (S.Context) CarriesDependencyAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002089 AL.getRange(), S.Context,
2090 AL.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002091}
2092
Akira Hatanakac8667622015-11-06 23:56:15 +00002093static void handleNotTailCalledAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002094 const AttributeList &AL) {
2095 if (checkAttrMutualExclusion<AlwaysInlineAttr>(S, D, AL.getRange(),
2096 AL.getName()))
Akira Hatanakac8667622015-11-06 23:56:15 +00002097 return;
2098
2099 D->addAttr(::new (S.Context) NotTailCalledAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002100 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Akira Hatanakac8667622015-11-06 23:56:15 +00002101}
2102
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00002103static void handleDisableTailCallsAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002104 const AttributeList &AL) {
2105 if (checkAttrMutualExclusion<NakedAttr>(S, D, AL.getRange(),
2106 AL.getName()))
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00002107 return;
2108
2109 D->addAttr(::new (S.Context) DisableTailCallsAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002110 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00002111}
2112
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002113static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &AL) {
2114 if (const auto *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00002115 if (VD->hasLocalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002116 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002117 return;
2118 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002119 } else if (!isFunctionOrMethod(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002120 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
2121 << AL.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002122 return;
2123 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002124
Michael Han99315932013-01-24 16:46:58 +00002125 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002126 UsedAttr(AL.getRange(), S.Context,
2127 AL.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002128}
2129
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002130static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &AL) {
2131 bool IsCXX17Attr = AL.isCXX11Attribute() && !AL.getScopeName();
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002132
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002133 if (IsCXX17Attr && isa<VarDecl>(D)) {
2134 // The C++17 spelling of this attribute cannot be applied to a static data
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002135 // member per [dcl.attr.unused]p2.
2136 if (cast<VarDecl>(D)->isStaticDataMember()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002137 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
2138 << AL.getName() << ExpectedForMaybeUnused;
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002139 return;
2140 }
2141 }
2142
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002143 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002144 // about using it as an extension.
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002145 if (!S.getLangOpts().CPlusPlus17 && IsCXX17Attr)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002146 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL.getName();
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002147
2148 D->addAttr(::new (S.Context) UnusedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002149 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002150}
2151
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002152static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002153 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002154 if (AL.getNumArgs() &&
2155 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002156 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002157
Michael Han99315932013-01-24 16:46:58 +00002158 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002159 ConstructorAttr(AL.getRange(), S.Context, priority,
2160 AL.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002161}
2162
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002163static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00002164 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002165 if (AL.getNumArgs() &&
2166 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002167 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002168
Michael Han99315932013-01-24 16:46:58 +00002169 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002170 DestructorAttr(AL.getRange(), S.Context, priority,
2171 AL.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002172}
2173
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002174template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00002175static void handleAttrWithMessage(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002176 const AttributeList &AL) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002177 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002178 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002179 if (AL.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002180 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002181
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002182 D->addAttr(::new (S.Context) AttrTy(AL.getRange(), S.Context, Str,
2183 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00002184}
2185
Ted Kremenek438f8db2014-02-22 01:06:05 +00002186static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002187 const AttributeList &AL) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00002188 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002189 S.Diag(AL.getLoc(), diag::err_objc_attr_protocol_requires_definition)
2190 << AL.getName() << AL.getRange();
Ted Kremenek27cfe102014-02-21 22:49:04 +00002191 return;
2192 }
2193
Ted Kremenek28eace62013-11-23 01:01:34 +00002194 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002195 ObjCExplicitProtocolImplAttr(AL.getRange(), S.Context,
2196 AL.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00002197}
2198
Jordy Rose740b0c22012-05-08 03:27:22 +00002199static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2200 IdentifierInfo *Platform,
2201 VersionTuple Introduced,
2202 VersionTuple Deprecated,
2203 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002204 StringRef PlatformName
2205 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2206 if (PlatformName.empty())
2207 PlatformName = Platform->getName();
2208
2209 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2210 // of these steps are needed).
2211 if (!Introduced.empty() && !Deprecated.empty() &&
2212 !(Introduced <= Deprecated)) {
2213 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2214 << 1 << PlatformName << Deprecated.getAsString()
2215 << 0 << Introduced.getAsString();
2216 return true;
2217 }
2218
2219 if (!Introduced.empty() && !Obsoleted.empty() &&
2220 !(Introduced <= Obsoleted)) {
2221 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2222 << 2 << PlatformName << Obsoleted.getAsString()
2223 << 0 << Introduced.getAsString();
2224 return true;
2225 }
2226
2227 if (!Deprecated.empty() && !Obsoleted.empty() &&
2228 !(Deprecated <= Obsoleted)) {
2229 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2230 << 2 << PlatformName << Obsoleted.getAsString()
2231 << 1 << Deprecated.getAsString();
2232 return true;
2233 }
2234
2235 return false;
2236}
2237
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002238/// \brief Check whether the two versions match.
2239///
2240/// If either version tuple is empty, then they are assumed to match. If
2241/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2242static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2243 bool BeforeIsOkay) {
2244 if (X.empty() || Y.empty())
2245 return true;
2246
2247 if (X == Y)
2248 return true;
2249
2250 if (BeforeIsOkay && X < Y)
2251 return true;
2252
2253 return false;
2254}
2255
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002256AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002257 IdentifierInfo *Platform,
Manman Ren719a8642016-05-06 21:04:01 +00002258 bool Implicit,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002259 VersionTuple Introduced,
2260 VersionTuple Deprecated,
2261 VersionTuple Obsoleted,
2262 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002263 StringRef Message,
Manman Rend8039df2016-02-22 04:47:24 +00002264 bool IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002265 StringRef Replacement,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002266 AvailabilityMergeKind AMK,
Michael Han99315932013-01-24 16:46:58 +00002267 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002268 VersionTuple MergedIntroduced = Introduced;
2269 VersionTuple MergedDeprecated = Deprecated;
2270 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002271 bool FoundAny = false;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002272 bool OverrideOrImpl = false;
2273 switch (AMK) {
2274 case AMK_None:
2275 case AMK_Redeclaration:
2276 OverrideOrImpl = false;
2277 break;
2278
2279 case AMK_Override:
2280 case AMK_ProtocolImplementation:
2281 OverrideOrImpl = true;
2282 break;
2283 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002284
Rafael Espindolac67f2232012-05-10 02:50:16 +00002285 if (D->hasAttrs()) {
2286 AttrVec &Attrs = D->getAttrs();
2287 for (unsigned i = 0, e = Attrs.size(); i != e;) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002288 const auto *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002289 if (!OldAA) {
2290 ++i;
2291 continue;
2292 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002293
Rafael Espindolac67f2232012-05-10 02:50:16 +00002294 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2295 if (OldPlatform != Platform) {
2296 ++i;
2297 continue;
2298 }
2299
Tim Northover7a73cc72015-10-30 16:30:49 +00002300 // If there is an existing availability attribute for this platform that
2301 // is explicit and the new one is implicit use the explicit one and
2302 // discard the new implicit attribute.
Manman Ren719a8642016-05-06 21:04:01 +00002303 if (!OldAA->isImplicit() && Implicit) {
Tim Northover7a73cc72015-10-30 16:30:49 +00002304 return nullptr;
2305 }
2306
2307 // If there is an existing attribute for this platform that is implicit
2308 // and the new attribute is explicit then erase the old one and
2309 // continue processing the attributes.
Manman Ren719a8642016-05-06 21:04:01 +00002310 if (!Implicit && OldAA->isImplicit()) {
Tim Northover7a73cc72015-10-30 16:30:49 +00002311 Attrs.erase(Attrs.begin() + i);
2312 --e;
2313 continue;
2314 }
2315
Rafael Espindolac67f2232012-05-10 02:50:16 +00002316 FoundAny = true;
2317 VersionTuple OldIntroduced = OldAA->getIntroduced();
2318 VersionTuple OldDeprecated = OldAA->getDeprecated();
2319 VersionTuple OldObsoleted = OldAA->getObsoleted();
2320 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002321
Douglas Gregord2a713e2015-09-30 21:27:42 +00002322 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
2323 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
2324 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002325 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregord2a713e2015-09-30 21:27:42 +00002326 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
2327 if (OverrideOrImpl) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002328 int Which = -1;
2329 VersionTuple FirstVersion;
2330 VersionTuple SecondVersion;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002331 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002332 Which = 0;
2333 FirstVersion = OldIntroduced;
2334 SecondVersion = Introduced;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002335 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002336 Which = 1;
2337 FirstVersion = Deprecated;
2338 SecondVersion = OldDeprecated;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002339 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002340 Which = 2;
2341 FirstVersion = Obsoleted;
2342 SecondVersion = OldObsoleted;
2343 }
2344
2345 if (Which == -1) {
2346 Diag(OldAA->getLocation(),
2347 diag::warn_mismatched_availability_override_unavail)
Douglas Gregord2a713e2015-09-30 21:27:42 +00002348 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2349 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002350 } else {
2351 Diag(OldAA->getLocation(),
2352 diag::warn_mismatched_availability_override)
2353 << Which
2354 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
Douglas Gregord2a713e2015-09-30 21:27:42 +00002355 << FirstVersion.getAsString() << SecondVersion.getAsString()
2356 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002357 }
Douglas Gregord2a713e2015-09-30 21:27:42 +00002358 if (AMK == AMK_Override)
2359 Diag(Range.getBegin(), diag::note_overridden_method);
2360 else
2361 Diag(Range.getBegin(), diag::note_protocol_method);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002362 } else {
2363 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2364 Diag(Range.getBegin(), diag::note_previous_attribute);
2365 }
2366
Rafael Espindolac67f2232012-05-10 02:50:16 +00002367 Attrs.erase(Attrs.begin() + i);
2368 --e;
2369 continue;
2370 }
2371
2372 VersionTuple MergedIntroduced2 = MergedIntroduced;
2373 VersionTuple MergedDeprecated2 = MergedDeprecated;
2374 VersionTuple MergedObsoleted2 = MergedObsoleted;
2375
2376 if (MergedIntroduced2.empty())
2377 MergedIntroduced2 = OldIntroduced;
2378 if (MergedDeprecated2.empty())
2379 MergedDeprecated2 = OldDeprecated;
2380 if (MergedObsoleted2.empty())
2381 MergedObsoleted2 = OldObsoleted;
2382
2383 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2384 MergedIntroduced2, MergedDeprecated2,
2385 MergedObsoleted2)) {
2386 Attrs.erase(Attrs.begin() + i);
2387 --e;
2388 continue;
2389 }
2390
2391 MergedIntroduced = MergedIntroduced2;
2392 MergedDeprecated = MergedDeprecated2;
2393 MergedObsoleted = MergedObsoleted2;
2394 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002395 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002396 }
2397
2398 if (FoundAny &&
2399 MergedIntroduced == Introduced &&
2400 MergedDeprecated == Deprecated &&
2401 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00002402 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002403
Douglas Gregord2a713e2015-09-30 21:27:42 +00002404 // Only create a new attribute if !OverrideOrImpl, but we want to do
Ted Kremenekb5445722013-04-06 00:34:27 +00002405 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002406 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002407 MergedDeprecated, MergedObsoleted) &&
Douglas Gregord2a713e2015-09-30 21:27:42 +00002408 !OverrideOrImpl) {
Manman Ren719a8642016-05-06 21:04:01 +00002409 auto *Avail = ::new (Context) AvailabilityAttr(Range, Context, Platform,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002410 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002411 Obsoleted, IsUnavailable, Message,
Manman Ren75bc6762016-03-21 17:30:55 +00002412 IsStrict, Replacement,
2413 AttrSpellingListIndex);
Manman Ren719a8642016-05-06 21:04:01 +00002414 Avail->setImplicit(Implicit);
2415 return Avail;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002416 }
Craig Topperc3ec1492014-05-26 06:22:03 +00002417 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002418}
2419
Chandler Carruthedc2c642011-07-02 00:01:44 +00002420static void handleAvailabilityAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002421 const AttributeList &AL) {
2422 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00002423 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002424 IdentifierLoc *Platform = AL.getArgAsIdent(0);
2425 unsigned Index = AL.getAttributeSpellingListIndex();
Michael Han99315932013-01-24 16:46:58 +00002426
Aaron Ballman00e99962013-08-31 01:11:41 +00002427 IdentifierInfo *II = Platform->Ident;
2428 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2429 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2430 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002431
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002432 auto *ND = dyn_cast<NamedDecl>(D);
Alex Lorenz472cc792017-04-20 09:35:02 +00002433 if (!ND) // We warned about this already, so just return.
Rafael Espindolac231fab2013-01-08 21:30:32 +00002434 return;
Rafael Espindolac231fab2013-01-08 21:30:32 +00002435
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002436 AvailabilityChange Introduced = AL.getAvailabilityIntroduced();
2437 AvailabilityChange Deprecated = AL.getAvailabilityDeprecated();
2438 AvailabilityChange Obsoleted = AL.getAvailabilityObsoleted();
2439 bool IsUnavailable = AL.getUnavailableLoc().isValid();
2440 bool IsStrict = AL.getStrictLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002441 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002442 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002443 Str = SE->getString();
Manman Ren75bc6762016-03-21 17:30:55 +00002444 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002445 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getReplacementExpr()))
Manman Ren75bc6762016-03-21 17:30:55 +00002446 Replacement = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002447
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002448 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, AL.getRange(), II,
Manman Ren719a8642016-05-06 21:04:01 +00002449 false/*Implicit*/,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002450 Introduced.Version,
2451 Deprecated.Version,
2452 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002453 IsUnavailable, Str,
Manman Ren75bc6762016-03-21 17:30:55 +00002454 IsStrict, Replacement,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002455 Sema::AMK_None,
Michael Han99315932013-01-24 16:46:58 +00002456 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002457 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002458 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002459
2460 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2461 // matches before the start of the watchOS platform.
2462 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2463 IdentifierInfo *NewII = nullptr;
2464 if (II->getName() == "ios")
2465 NewII = &S.Context.Idents.get("watchos");
2466 else if (II->getName() == "ios_app_extension")
2467 NewII = &S.Context.Idents.get("watchos_app_extension");
2468
2469 if (NewII) {
2470 auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple {
2471 if (Version.empty())
2472 return Version;
2473 auto Major = Version.getMajor();
2474 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2475 if (NewMajor >= 2) {
2476 if (Version.getMinor().hasValue()) {
2477 if (Version.getSubminor().hasValue())
2478 return VersionTuple(NewMajor, Version.getMinor().getValue(),
2479 Version.getSubminor().getValue());
2480 else
2481 return VersionTuple(NewMajor, Version.getMinor().getValue());
2482 }
2483 }
2484
2485 return VersionTuple(2, 0);
2486 };
2487
2488 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2489 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2490 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2491
2492 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002493 AL.getRange(),
Tim Northover7a73cc72015-10-30 16:30:49 +00002494 NewII,
Manman Ren719a8642016-05-06 21:04:01 +00002495 true/*Implicit*/,
Tim Northover7a73cc72015-10-30 16:30:49 +00002496 NewIntroduced,
2497 NewDeprecated,
2498 NewObsoleted,
2499 IsUnavailable, Str,
Manman Rend8039df2016-02-22 04:47:24 +00002500 IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002501 Replacement,
Tim Northover7a73cc72015-10-30 16:30:49 +00002502 Sema::AMK_None,
2503 Index);
2504 if (NewAttr)
2505 D->addAttr(NewAttr);
2506 }
2507 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2508 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2509 // matches before the start of the tvOS platform.
2510 IdentifierInfo *NewII = nullptr;
2511 if (II->getName() == "ios")
2512 NewII = &S.Context.Idents.get("tvos");
2513 else if (II->getName() == "ios_app_extension")
2514 NewII = &S.Context.Idents.get("tvos_app_extension");
2515
2516 if (NewII) {
2517 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002518 AL.getRange(),
Tim Northover7a73cc72015-10-30 16:30:49 +00002519 NewII,
Manman Ren719a8642016-05-06 21:04:01 +00002520 true/*Implicit*/,
Tim Northover7a73cc72015-10-30 16:30:49 +00002521 Introduced.Version,
2522 Deprecated.Version,
2523 Obsoleted.Version,
2524 IsUnavailable, Str,
Manman Rend8039df2016-02-22 04:47:24 +00002525 IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002526 Replacement,
Tim Northover7a73cc72015-10-30 16:30:49 +00002527 Sema::AMK_None,
2528 Index);
2529 if (NewAttr)
2530 D->addAttr(NewAttr);
2531 }
2532 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002533}
2534
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002535static void handleExternalSourceSymbolAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002536 const AttributeList &AL) {
2537 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002538 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002539 assert(checkAttributeAtMostNumArgs(S, AL, 3) &&
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002540 "Invalid number of arguments in an external_source_symbol attribute");
2541
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002542 StringRef Language;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002543 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(0)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002544 Language = SE->getString();
2545 StringRef DefinedIn;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002546 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(1)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002547 DefinedIn = SE->getString();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002548 bool IsGeneratedDeclaration = AL.getArgAsIdent(2) != nullptr;
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002549
2550 D->addAttr(::new (S.Context) ExternalSourceSymbolAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002551 AL.getRange(), S.Context, Language, DefinedIn, IsGeneratedDeclaration,
2552 AL.getAttributeSpellingListIndex()));
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002553}
2554
John McCalld041a9b2013-02-20 01:54:26 +00002555template <class T>
2556static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2557 typename T::VisibilityType value,
2558 unsigned attrSpellingListIndex) {
2559 T *existingAttr = D->getAttr<T>();
2560 if (existingAttr) {
2561 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2562 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00002563 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00002564 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2565 S.Diag(range.getBegin(), diag::note_previous_attribute);
2566 D->dropAttr<T>();
2567 }
2568 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2569}
2570
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002571VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002572 VisibilityAttr::VisibilityType Vis,
2573 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002574 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2575 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002576}
2577
John McCalld041a9b2013-02-20 01:54:26 +00002578TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2579 TypeVisibilityAttr::VisibilityType Vis,
2580 unsigned AttrSpellingListIndex) {
2581 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2582 AttrSpellingListIndex);
2583}
2584
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002585static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &AL,
John McCalld041a9b2013-02-20 01:54:26 +00002586 bool isTypeVisibility) {
2587 // Visibility attributes don't mean anything on a typedef.
2588 if (isa<TypedefNameDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002589 S.Diag(AL.getRange().getBegin(), diag::warn_attribute_ignored)
2590 << AL.getName();
John McCalld041a9b2013-02-20 01:54:26 +00002591 return;
2592 }
2593
2594 // 'type_visibility' can only go on a type or namespace.
2595 if (isTypeVisibility &&
2596 !(isa<TagDecl>(D) ||
2597 isa<ObjCInterfaceDecl>(D) ||
2598 isa<NamespaceDecl>(D))) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002599 S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2600 << AL.getName() << ExpectedTypeOrNamespace;
John McCalld041a9b2013-02-20 01:54:26 +00002601 return;
2602 }
2603
Benjamin Kramer70370212013-09-09 15:08:57 +00002604 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002605 StringRef TypeStr;
2606 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002607 if (!S.checkStringLiteralArgumentAttr(AL, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002608 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002609
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002610 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002611 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002612 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002613 << AL.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002614 return;
2615 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002616
2617 // Complain about attempts to use protected visibility on targets
2618 // (like Darwin) that don't support it.
2619 if (type == VisibilityAttr::Protected &&
2620 !S.Context.getTargetInfo().hasProtectedVisibility()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002621 S.Diag(AL.getLoc(), diag::warn_attribute_protected_visibility);
Aaron Ballman682ee422013-09-11 19:47:58 +00002622 type = VisibilityAttr::Default;
2623 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002624
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002625 unsigned Index = AL.getAttributeSpellingListIndex();
2626 Attr *newAttr;
John McCalld041a9b2013-02-20 01:54:26 +00002627 if (isTypeVisibility) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002628 newAttr = S.mergeTypeVisibilityAttr(D, AL.getRange(),
John McCalld041a9b2013-02-20 01:54:26 +00002629 (TypeVisibilityAttr::VisibilityType) type,
2630 Index);
2631 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002632 newAttr = S.mergeVisibilityAttr(D, AL.getRange(), type, Index);
John McCalld041a9b2013-02-20 01:54:26 +00002633 }
2634 if (newAttr)
2635 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002636}
2637
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002638static void handleObjCMethodFamilyAttr(Sema &S, Decl *D,
2639 const AttributeList &AL) {
2640 const auto *M = cast<ObjCMethodDecl>(D);
2641 if (!AL.isArgIdent(0)) {
2642 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
2643 << AL.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002644 return;
2645 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002646
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002647 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00002648 ObjCMethodFamilyAttr::FamilyKind F;
2649 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002650 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
2651 << AL.getName() << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002652 return;
2653 }
2654
Alp Toker314cc812014-01-25 16:55:45 +00002655 if (F == ObjCMethodFamilyAttr::OMF_init &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002656 !M->getReturnType()->isObjCObjectPointerType()) {
2657 S.Diag(M->getLocation(), diag::err_init_method_bad_return_type)
2658 << M->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002659 // Ignore the attribute.
2660 return;
2661 }
2662
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002663 D->addAttr(new (S.Context) ObjCMethodFamilyAttr(
2664 AL.getRange(), S.Context, F, AL.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002665}
2666
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002667static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &AL) {
2668 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002669 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002670 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002671 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2672 return;
2673 }
2674 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002675 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002676 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002677 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002678 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2679 return;
2680 }
2681 }
2682 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002683 // It is okay to include this attribute on properties, e.g.:
2684 //
2685 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2686 //
2687 // In this case it follows tradition and suppresses an error in the above
2688 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002689 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002690 }
Michael Han99315932013-01-24 16:46:58 +00002691 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002692 ObjCNSObjectAttr(AL.getRange(), S.Context,
2693 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002694}
2695
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002696static void handleObjCIndependentClass(Sema &S, Decl *D, const AttributeList &AL) {
2697 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002698 QualType T = TD->getUnderlyingType();
2699 if (!T->isObjCObjectPointerType()) {
2700 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2701 return;
2702 }
2703 } else {
2704 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2705 return;
2706 }
2707 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002708 ObjCIndependentClassAttr(AL.getRange(), S.Context,
2709 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002710}
2711
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002712static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &AL) {
2713 if (!AL.isArgIdent(0)) {
2714 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
2715 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002716 return;
2717 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002718
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002719 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002720 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002721 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002722 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
2723 << AL.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002724 return;
2725 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002726
Michael Han99315932013-01-24 16:46:58 +00002727 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002728 BlocksAttr(AL.getRange(), S.Context, type,
2729 AL.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002730}
2731
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002732static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002733 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002734 if (AL.getNumArgs() > 0) {
2735 Expr *E = AL.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002736 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002737 if (E->isTypeDependent() || E->isValueDependent() ||
2738 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002739 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
2740 << AL.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002741 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002742 return;
2743 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002744
John McCallb46f2872011-09-09 07:56:05 +00002745 if (Idx.isSigned() && Idx.isNegative()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002746 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_less_than_zero)
Chris Lattner3b054132008-11-19 05:08:23 +00002747 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002748 return;
2749 }
John McCallb46f2872011-09-09 07:56:05 +00002750
2751 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002752 }
2753
Aaron Ballman18a78382013-11-21 00:28:23 +00002754 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002755 if (AL.getNumArgs() > 1) {
2756 Expr *E = AL.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002757 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002758 if (E->isTypeDependent() || E->isValueDependent() ||
2759 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002760 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
2761 << AL.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002762 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002763 return;
2764 }
2765 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002766
John McCallb46f2872011-09-09 07:56:05 +00002767 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002768 // FIXME: This error message could be improved, it would be nice
2769 // to say what the bounds actually are.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002770 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
Chris Lattner3b054132008-11-19 05:08:23 +00002771 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002772 return;
2773 }
2774 }
2775
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002776 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002777 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002778 if (isa<FunctionNoProtoType>(FT)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002779 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_named_arguments);
Chris Lattner9363e312009-03-17 23:03:47 +00002780 return;
2781 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002782
Chris Lattner9363e312009-03-17 23:03:47 +00002783 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002784 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002785 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002786 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002787 } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002788 if (!MD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002789 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002790 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002791 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002792 } else if (const auto *BD = dyn_cast<BlockDecl>(D)) {
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002793 if (!BD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002794 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002795 return;
2796 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002797 } else if (const auto *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002798 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002799 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002800 const FunctionType *FT = Ty->isFunctionPointerType()
2801 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002802 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002803 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002804 int m = Ty->isFunctionPointerType() ? 0 : 1;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002805 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002806 return;
2807 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002808 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002809 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
2810 << AL.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002811 return;
2812 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002813 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002814 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
2815 << AL.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002816 return;
2817 }
Michael Han99315932013-01-24 16:46:58 +00002818 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002819 SentinelAttr(AL.getRange(), S.Context, sentinel, nullPos,
2820 AL.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002821}
2822
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002823static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &AL) {
Alp Toker314cc812014-01-25 16:55:45 +00002824 if (D->getFunctionType() &&
2825 D->getFunctionType()->getReturnType()->isVoidType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002826 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method)
2827 << AL.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002828 return;
2829 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002830 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002831 if (MD->getReturnType()->isVoidType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002832 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method)
2833 << AL.getName() << 1;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002834 return;
2835 }
2836
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002837 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
Aaron Ballmane7964782016-03-07 22:44:55 +00002838 // about using it as an extension.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002839 if (!S.getLangOpts().CPlusPlus17 && AL.isCXX11Attribute() &&
2840 !AL.getScopeName())
2841 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL.getName();
Aaron Ballmane7964782016-03-07 22:44:55 +00002842
Michael Han99315932013-01-24 16:46:58 +00002843 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002844 WarnUnusedResultAttr(AL.getRange(), S.Context,
2845 AL.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002846}
2847
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002848static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &AL) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002849 // weak_import only applies to variable & function declarations.
2850 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002851 if (!D->canBeWeakImported(isDef)) {
2852 if (isDef)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002853 S.Diag(AL.getLoc(), diag::warn_attribute_invalid_on_definition)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002854 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002855 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002856 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002857 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002858 // Nothing to warn about here.
2859 } else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002860 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
2861 << AL.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002862
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002863 return;
2864 }
2865
Michael Han99315932013-01-24 16:46:58 +00002866 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002867 WeakImportAttr(AL.getRange(), S.Context,
2868 AL.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002869}
2870
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002871// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002872template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002873static void handleWorkGroupSize(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002874 const AttributeList &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002875 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002876 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002877 const Expr *E = AL.getArgAsExpr(i);
2878 if (!checkUInt32Argument(S, AL, E, WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002879 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002880 if (WGSize[i] == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002881 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
2882 << AL.getName() << E->getSourceRange();
Joey Goulyb1d23a82014-05-19 14:41:38 +00002883 return;
2884 }
2885 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002886
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002887 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2888 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2889 Existing->getYDim() == WGSize[1] &&
2890 Existing->getZDim() == WGSize[2]))
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002891 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002892
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002893 D->addAttr(::new (S.Context) WorkGroupAttr(AL.getRange(), S.Context,
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002894 WGSize[0], WGSize[1], WGSize[2],
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002895 AL.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002896}
2897
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002898// Handles intel_reqd_sub_group_size.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002899static void handleSubGroupSize(Sema &S, Decl *D, const AttributeList &AL) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002900 uint32_t SGSize;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002901 const Expr *E = AL.getArgAsExpr(0);
2902 if (!checkUInt32Argument(S, AL, E, SGSize))
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002903 return;
2904 if (SGSize == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002905 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
2906 << AL.getName() << E->getSourceRange();
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002907 return;
2908 }
2909
2910 OpenCLIntelReqdSubGroupSizeAttr *Existing =
2911 D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>();
2912 if (Existing && Existing->getSubGroupSize() != SGSize)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002913 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL.getName();
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002914
2915 D->addAttr(::new (S.Context) OpenCLIntelReqdSubGroupSizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002916 AL.getRange(), S.Context, SGSize,
2917 AL.getAttributeSpellingListIndex()));
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002918}
2919
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002920static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &AL) {
2921 if (!AL.hasParsedType()) {
2922 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
2923 << AL.getName() << 1;
Aaron Ballman00e99962013-08-31 01:11:41 +00002924 return;
2925 }
2926
Craig Topperc3ec1492014-05-26 06:22:03 +00002927 TypeSourceInfo *ParmTSI = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002928 QualType ParmType = S.GetTypeFromParser(AL.getTypeArg(), &ParmTSI);
Richard Smithb87c4652013-10-31 21:23:20 +00002929 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002930
2931 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2932 (ParmType->isBooleanType() ||
2933 !ParmType->isIntegralType(S.getASTContext()))) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002934 S.Diag(AL.getLoc(), diag::err_attribute_argument_vec_type_hint)
Joey Goulyaba589c2013-03-08 09:42:32 +00002935 << ParmType;
2936 return;
2937 }
2938
Aaron Ballmana9e05402013-12-02 22:16:55 +00002939 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002940 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002941 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL.getName();
Joey Goulyaba589c2013-03-08 09:42:32 +00002942 return;
2943 }
2944 }
2945
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002946 D->addAttr(::new (S.Context) VecTypeHintAttr(AL.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002947 ParmTSI,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002948 AL.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002949}
2950
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002951SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002952 StringRef Name,
2953 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002954 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2955 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002956 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002957 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2958 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002959 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002960 }
Michael Han99315932013-01-24 16:46:58 +00002961 return ::new (Context) SectionAttr(Range, Context, Name,
2962 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002963}
2964
Reid Kleckner2a133222015-03-04 23:39:17 +00002965bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2966 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2967 if (!Error.empty()) {
2968 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error;
2969 return false;
2970 }
2971 return true;
2972}
2973
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002974static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &AL) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002975 // Make sure that there is a string literal as the sections's single
2976 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002977 StringRef Str;
2978 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002979 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002980 return;
Mike Stump11289f42009-09-09 15:08:12 +00002981
Reid Kleckner2a133222015-03-04 23:39:17 +00002982 if (!S.checkSectionName(LiteralLoc, Str))
2983 return;
2984
Chris Lattner30ba6742009-08-10 19:03:04 +00002985 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002986 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002987 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002988 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002989 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002990 return;
2991 }
Mike Stump11289f42009-09-09 15:08:12 +00002992
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002993 unsigned Index = AL.getAttributeSpellingListIndex();
2994 SectionAttr *NewAttr = S.mergeSectionAttr(D, AL.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002995 if (NewAttr)
2996 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002997}
2998
Erich Keane57e15cd2017-07-19 22:06:33 +00002999// Check for things we'd like to warn about. Multiversioning issues are
3000// handled later in the process, once we know how many exist.
3001bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
3002 enum FirstParam { Unsupported, Duplicate };
3003 enum SecondParam { None, Architecture };
Eric Christopher789a7ad2015-06-12 01:36:05 +00003004 for (auto Str : {"tune=", "fpmath="})
3005 if (AttrStr.find(Str) != StringRef::npos)
Erich Keane57e15cd2017-07-19 22:06:33 +00003006 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3007 << Unsupported << None << Str;
3008
3009 TargetAttr::ParsedTargetAttr ParsedAttrs = TargetAttr::parse(AttrStr);
3010
3011 if (!ParsedAttrs.Architecture.empty() &&
3012 !Context.getTargetInfo().isValidCPUName(ParsedAttrs.Architecture))
3013 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3014 << Unsupported << Architecture << ParsedAttrs.Architecture;
3015
3016 if (ParsedAttrs.DuplicateArchitecture)
3017 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3018 << Duplicate << None << "arch=";
3019
3020 for (const auto &Feature : ParsedAttrs.Features) {
3021 auto CurFeature = StringRef(Feature).drop_front(); // remove + or -.
3022 if (!Context.getTargetInfo().isValidFeatureName(CurFeature))
3023 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3024 << Unsupported << None << CurFeature;
3025 }
3026
3027 return true;
Eric Christopher789a7ad2015-06-12 01:36:05 +00003028}
3029
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003030static void handleTargetAttr(Sema &S, Decl *D, const AttributeList &AL) {
Eric Christopher11acf732015-06-12 01:35:52 +00003031 StringRef Str;
3032 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003033 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc) ||
Erich Keane57e15cd2017-07-19 22:06:33 +00003034 !S.checkTargetAttr(LiteralLoc, Str))
Eric Christopher11acf732015-06-12 01:35:52 +00003035 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003036 unsigned Index = AL.getAttributeSpellingListIndex();
Eric Christopher11acf732015-06-12 01:35:52 +00003037 TargetAttr *NewAttr =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003038 ::new (S.Context) TargetAttr(AL.getRange(), S.Context, Str, Index);
Eric Christopher11acf732015-06-12 01:35:52 +00003039 D->addAttr(NewAttr);
3040}
3041
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003042static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &AL) {
3043 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003044 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00003045 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003046 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00003047
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003048 // gcc only allows for simple identifiers. Since we support more than gcc, we
3049 // will warn the user.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003050 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003051 if (DRE->hasQualifier())
3052 S.Diag(Loc, diag::warn_cleanup_ext);
3053 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
3054 NI = DRE->getNameInfo();
3055 if (!FD) {
3056 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
3057 << NI.getName();
3058 return;
3059 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003060 } else if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003061 if (ULE->hasExplicitTemplateArgs())
3062 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003063 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
3064 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00003065 if (!FD) {
3066 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
3067 << NI.getName();
3068 if (ULE->getType() == S.Context.OverloadTy)
3069 S.NoteAllOverloadCandidates(ULE);
3070 return;
3071 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003072 } else {
3073 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00003074 return;
3075 }
3076
Anders Carlssond277d792009-01-31 01:16:18 +00003077 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003078 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
3079 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00003080 return;
3081 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003082
Anders Carlsson723f55d2009-02-07 23:16:50 +00003083 // We're currently more strict than GCC about what function types we accept.
3084 // If this ever proves to be a problem it should be easy to fix.
Aaron Ballman3b70e752017-12-01 16:53:49 +00003085 QualType Ty = S.Context.getPointerType(cast<VarDecl>(D)->getType());
Anders Carlsson723f55d2009-02-07 23:16:50 +00003086 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00003087 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
3088 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003089 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
3090 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00003091 return;
3092 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003093
Michael Han99315932013-01-24 16:46:58 +00003094 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003095 CleanupAttr(AL.getRange(), S.Context, FD,
3096 AL.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00003097}
3098
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003099static void handleEnumExtensibilityAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003100 const AttributeList &AL) {
3101 if (!AL.isArgIdent(0)) {
3102 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
3103 << AL.getName() << 0 << AANT_ArgumentIdentifier;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003104 return;
3105 }
3106
3107 EnumExtensibilityAttr::Kind ExtensibilityKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003108 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003109 if (!EnumExtensibilityAttr::ConvertStrToKind(II->getName(),
3110 ExtensibilityKind)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003111 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
3112 << AL.getName() << II;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003113 return;
3114 }
3115
3116 D->addAttr(::new (S.Context) EnumExtensibilityAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003117 AL.getRange(), S.Context, ExtensibilityKind,
3118 AL.getAttributeSpellingListIndex()));
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003119}
3120
Mike Stumpd3bb5572009-07-24 19:02:52 +00003121/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00003122/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003123static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &AL) {
3124 Expr *IdxExpr = AL.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00003125 uint64_t Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003126 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003127 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00003128
Eric Christopherb64963e2015-08-13 21:34:35 +00003129 // Make sure the format string is really a string.
Alp Toker601b22c2014-01-21 23:35:24 +00003130 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003131
Eric Christopherb64963e2015-08-13 21:34:35 +00003132 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
3133 if (NotNSStringTy &&
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003134 !isCFStringType(Ty, S.Context) &&
3135 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003136 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003137 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003138 << "a string type" << IdxExpr->getSourceRange()
3139 << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003140 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003141 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003142 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003143 if (!isNSStringType(Ty, S.Context) &&
3144 !isCFStringType(Ty, S.Context) &&
3145 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003146 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003147 S.Diag(AL.getLoc(), diag::err_format_attribute_result_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003148 << (NotNSStringTy ? "string type" : "NSString")
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00003149 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003150 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003151 }
3152
Alp Toker601b22c2014-01-21 23:35:24 +00003153 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00003154 // because that has corrected for the implicit this parameter, and is zero-
3155 // based. The attribute expects what the user wrote explicitly.
3156 llvm::APSInt Val;
3157 IdxExpr->EvaluateAsInt(Val, S.Context);
3158
Michael Han99315932013-01-24 16:46:58 +00003159 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003160 FormatArgAttr(AL.getRange(), S.Context, Val.getZExtValue(),
3161 AL.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003162}
3163
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003164enum FormatAttrKind {
3165 CFStringFormat,
3166 NSStringFormat,
3167 StrftimeFormat,
3168 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003169 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003170 InvalidFormat
3171};
3172
3173/// getFormatAttrKind - Map from format attribute names to supported format
3174/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003175static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003176 return llvm::StringSwitch<FormatAttrKind>(Format)
Mehdi Amini06d367c2016-10-24 20:39:34 +00003177 // Check for formats that get handled specially.
3178 .Case("NSString", NSStringFormat)
3179 .Case("CFString", CFStringFormat)
3180 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003181
Mehdi Amini06d367c2016-10-24 20:39:34 +00003182 // Otherwise, check for supported formats.
3183 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3184 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3185 .Case("kprintf", SupportedFormat) // OpenBSD.
3186 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
3187 .Case("os_trace", SupportedFormat)
3188 .Case("os_log", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003189
Mehdi Amini06d367c2016-10-24 20:39:34 +00003190 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3191 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003192}
3193
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003194/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003195/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003196static void handleInitPriorityAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003197 const AttributeList &AL) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003198 if (!S.getLangOpts().CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003199 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL.getName();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003200 return;
3201 }
3202
Aaron Ballman4a611152013-11-27 16:34:09 +00003203 if (S.getCurFunctionOrMethodDecl()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003204 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3205 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003206 return;
3207 }
Aaron Ballman4a611152013-11-27 16:34:09 +00003208 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003209 if (S.Context.getAsArrayType(T))
3210 T = S.Context.getBaseElementType(T);
3211 if (!T->getAs<RecordType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003212 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3213 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003214 return;
3215 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003216
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003217 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003218 uint32_t prioritynum;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003219 if (!checkUInt32Argument(S, AL, E, prioritynum)) {
3220 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003221 return;
3222 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003223
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003224 if (prioritynum < 101 || prioritynum > 65535) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003225 S.Diag(AL.getLoc(), diag::err_attribute_argument_outof_range)
3226 << E->getSourceRange() << AL.getName() << 101 << 65535;
3227 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003228 return;
3229 }
Michael Han99315932013-01-24 16:46:58 +00003230 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003231 InitPriorityAttr(AL.getRange(), S.Context, prioritynum,
3232 AL.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003233}
3234
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003235FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3236 IdentifierInfo *Format, int FormatIdx,
3237 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00003238 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003239 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003240 for (auto *F : D->specific_attrs<FormatAttr>()) {
3241 if (F->getType() == Format &&
3242 F->getFormatIdx() == FormatIdx &&
3243 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003244 // If we don't have a valid location for this attribute, adopt the
3245 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003246 if (F->getLocation().isInvalid())
3247 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00003248 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00003249 }
3250 }
3251
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003252 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3253 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003254}
3255
Mike Stumpd3bb5572009-07-24 19:02:52 +00003256/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003257/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003258static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &AL) {
3259 if (!AL.isArgIdent(0)) {
3260 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
3261 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003262 return;
3263 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003264
Chandler Carruth743682b2010-11-16 08:35:43 +00003265 // In C++ the implicit 'this' function parameter also counts, and they are
3266 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003267 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00003268 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003269
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003270 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Aaron Ballman00e99962013-08-31 01:11:41 +00003271 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003272
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00003273 if (normalizeName(Format)) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003274 // If we've modified the string name, we need a new identifier for it.
3275 II = &S.Context.Idents.get(Format);
3276 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003277
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003278 // Check for supported formats.
3279 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00003280
3281 if (Kind == IgnoredFormat)
3282 return;
3283
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003284 if (Kind == InvalidFormat) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003285 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
3286 << AL.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003287 return;
3288 }
3289
3290 // checks for the 2nd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003291 Expr *IdxExpr = AL.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003292 uint32_t Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003293 if (!checkUInt32Argument(S, AL, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003294 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003295
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003296 if (Idx < 1 || Idx > NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003297 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3298 << AL.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003299 return;
3300 }
3301
3302 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003303 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003304
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003305 if (HasImplicitThisParam) {
3306 if (ArgIdx == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003307 S.Diag(AL.getLoc(),
Chandler Carruth743682b2010-11-16 08:35:43 +00003308 diag::err_format_attribute_implicit_this_format_string)
3309 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003310 return;
3311 }
3312 ArgIdx--;
3313 }
Mike Stump11289f42009-09-09 15:08:12 +00003314
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003315 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00003316 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003317
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003318 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003319 if (!isCFStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003320 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003321 << "a CFString" << IdxExpr->getSourceRange()
3322 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00003323 return;
3324 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003325 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003326 // FIXME: do we need to check if the type is NSString*? What are the
3327 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003328 if (!isNSStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003329 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003330 << "an NSString" << IdxExpr->getSourceRange()
3331 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003332 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003333 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003334 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003335 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003336 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003337 << "a string type" << IdxExpr->getSourceRange()
3338 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003339 return;
3340 }
3341
3342 // check the 3rd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003343 Expr *FirstArgExpr = AL.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003344 uint32_t FirstArg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003345 if (!checkUInt32Argument(S, AL, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003346 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003347
3348 // check if the function is variadic if the 3rd argument non-zero
3349 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003350 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003351 ++NumArgs; // +1 for ...
3352 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003353 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003354 return;
3355 }
3356 }
3357
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003358 // strftime requires FirstArg to be 0 because it doesn't read from any
3359 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003360 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003361 if (FirstArg != 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003362 S.Diag(AL.getLoc(), diag::err_format_strftime_third_parameter)
Chris Lattner3b054132008-11-19 05:08:23 +00003363 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003364 return;
3365 }
3366 // if 0 it disables parameter checking (to use with e.g. va_list)
3367 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003368 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3369 << AL.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003370 return;
3371 }
3372
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003373 FormatAttr *NewAttr = S.mergeFormatAttr(D, AL.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003374 Idx, FirstArg,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003375 AL.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003376 if (NewAttr)
3377 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003378}
3379
Chandler Carruthedc2c642011-07-02 00:01:44 +00003380static void handleTransparentUnionAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003381 const AttributeList &AL) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003382 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00003383 RecordDecl *RD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003384 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003385 if (TD && TD->getUnderlyingType()->isUnionType())
3386 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3387 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003388 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003389
3390 if (!RD || !RD->isUnion()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003391 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
3392 << AL.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003393 return;
3394 }
3395
John McCallf937c022011-10-07 06:10:15 +00003396 if (!RD->isCompleteDefinition()) {
Erich Keane2fe684b2017-02-28 20:44:39 +00003397 if (!RD->isBeingDefined())
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003398 S.Diag(AL.getLoc(),
Erich Keane2fe684b2017-02-28 20:44:39 +00003399 diag::warn_transparent_union_attribute_not_definition);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003400 return;
3401 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003402
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003403 RecordDecl::field_iterator Field = RD->field_begin(),
3404 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003405 if (Field == FieldEnd) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003406 S.Diag(AL.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003407 return;
3408 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003409
David Blaikie40ed2972012-06-06 20:45:41 +00003410 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003411 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003412 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003413 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003414 diag::warn_transparent_union_attribute_floating)
3415 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003416 return;
3417 }
3418
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003419 if (FirstType->isIncompleteType())
3420 return;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003421 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3422 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3423 for (; Field != FieldEnd; ++Field) {
3424 QualType FieldType = Field->getType();
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003425 if (FieldType->isIncompleteType())
3426 return;
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003427 // FIXME: this isn't fully correct; we also need to test whether the
3428 // members of the union would all have the same calling convention as the
3429 // first member of the union. Checking just the size and alignment isn't
3430 // sufficient (consider structs passed on the stack instead of in registers
3431 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003432 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003433 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003434 // Warn if we drop the attribute.
3435 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003436 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003437 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003438 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003439 diag::warn_transparent_union_attribute_field_size_align)
3440 << isSize << Field->getDeclName() << FieldBits;
3441 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003442 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003443 diag::note_transparent_union_first_field_size_align)
3444 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003445 return;
3446 }
3447 }
3448
Michael Han99315932013-01-24 16:46:58 +00003449 RD->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003450 TransparentUnionAttr(AL.getRange(), S.Context,
3451 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003452}
3453
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003454static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003455 // Make sure that there is a string literal as the annotation's single
3456 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003457 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003458 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003459 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003460
3461 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003462 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
3463 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003464 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003465 }
Michael Han99315932013-01-24 16:46:58 +00003466
3467 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003468 AnnotateAttr(AL.getRange(), S.Context, Str,
3469 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003470}
3471
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003472static void handleAlignValueAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003473 const AttributeList &AL) {
3474 S.AddAlignValueAttr(AL.getRange(), D, AL.getArgAsExpr(0),
3475 AL.getAttributeSpellingListIndex());
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003476}
3477
3478void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
3479 unsigned SpellingListIndex) {
3480 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
3481 SourceLocation AttrLoc = AttrRange.getBegin();
3482
3483 QualType T;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003484 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003485 T = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003486 else if (const auto *VD = dyn_cast<ValueDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003487 T = VD->getType();
3488 else
3489 llvm_unreachable("Unknown decl type for align_value");
3490
3491 if (!T->isDependentType() && !T->isAnyPointerType() &&
3492 !T->isReferenceType() && !T->isMemberPointerType()) {
3493 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
3494 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
3495 return;
3496 }
3497
3498 if (!E->isValueDependent()) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003499 llvm::APSInt Alignment;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003500 ExprResult ICE
3501 = VerifyIntegerConstantExpression(E, &Alignment,
3502 diag::err_align_value_attribute_argument_not_int,
3503 /*AllowFold*/ false);
3504 if (ICE.isInvalid())
3505 return;
3506
3507 if (!Alignment.isPowerOf2()) {
3508 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3509 << E->getSourceRange();
3510 return;
3511 }
3512
3513 D->addAttr(::new (Context)
3514 AlignValueAttr(AttrRange, Context, ICE.get(),
3515 SpellingListIndex));
3516 return;
3517 }
3518
3519 // Save dependent expressions in the AST to be instantiated.
3520 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003521}
3522
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003523static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003524 // check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003525 if (AL.getNumArgs() > 1) {
3526 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
3527 << AL.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003528 return;
3529 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003530
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003531 if (AL.getNumArgs() == 0) {
3532 D->addAttr(::new (S.Context) AlignedAttr(AL.getRange(), S.Context,
3533 true, nullptr, AL.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00003534 return;
3535 }
3536
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003537 Expr *E = AL.getArgAsExpr(0);
3538 if (AL.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3539 S.Diag(AL.getEllipsisLoc(),
Richard Smith44c247f2013-02-22 08:32:16 +00003540 diag::err_pack_expansion_without_parameter_packs);
3541 return;
3542 }
3543
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003544 if (!AL.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
Richard Smith44c247f2013-02-22 08:32:16 +00003545 return;
3546
David Majnemer26a1e0e2015-04-07 02:37:09 +00003547 if (E->isValueDependent()) {
3548 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3549 if (!TND->getUnderlyingType()->isDependentType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003550 S.Diag(AL.getLoc(), diag::err_alignment_dependent_typedef_name)
David Majnemer26a1e0e2015-04-07 02:37:09 +00003551 << E->getSourceRange();
3552 return;
3553 }
3554 }
3555 }
3556
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003557 S.AddAlignedAttr(AL.getRange(), D, E, AL.getAttributeSpellingListIndex(),
3558 AL.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003559}
3560
3561void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003562 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003563 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3564 SourceLocation AttrLoc = AttrRange.getBegin();
3565
Richard Smith1dba27c2013-01-29 09:02:09 +00003566 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003567 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003568 // C++11 [dcl.align]p1:
3569 // An alignment-specifier may be applied to a variable or to a class
3570 // data member, but it shall not be applied to a bit-field, a function
3571 // parameter, the formal parameter of a catch clause, or a variable
3572 // declared with the register storage class specifier. An
3573 // alignment-specifier may also be applied to the declaration of a class
3574 // or enumeration type.
3575 // C11 6.7.5/2:
3576 // An alignment attribute shall not be specified in a declaration of
3577 // a typedef, or a bit-field, or a function, or a parameter, or an
3578 // object declared with the register storage-class specifier.
3579 int DiagKind = -1;
3580 if (isa<ParmVarDecl>(D)) {
3581 DiagKind = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003582 } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003583 if (VD->getStorageClass() == SC_Register)
3584 DiagKind = 1;
3585 if (VD->isExceptionVariable())
3586 DiagKind = 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003587 } else if (const auto *FD = dyn_cast<FieldDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003588 if (FD->isBitField())
3589 DiagKind = 3;
3590 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00003591 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00003592 << (TmpAttr.isC11() ? ExpectedVariableOrField
3593 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003594 return;
3595 }
3596 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003597 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00003598 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003599 return;
3600 }
3601 }
3602
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003603 if (E->isTypeDependent() || E->isValueDependent()) {
3604 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003605 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3606 AA->setPackExpansion(IsPackExpansion);
3607 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003608 return;
3609 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003610
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003611 // FIXME: Cache the number on the AL object?
David Majnemer0be6bd02015-07-26 09:02:21 +00003612 llvm::APSInt Alignment;
Douglas Gregore2b37442012-05-04 22:38:52 +00003613 ExprResult ICE
3614 = VerifyIntegerConstantExpression(E, &Alignment,
3615 diag::err_aligned_attribute_argument_not_int,
3616 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003617 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003618 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003619
David Majnemer0be6bd02015-07-26 09:02:21 +00003620 uint64_t AlignVal = Alignment.getZExtValue();
3621
Richard Smith848e1f12013-02-01 08:12:08 +00003622 // C++11 [dcl.align]p2:
3623 // -- if the constant expression evaluates to zero, the alignment
3624 // specifier shall have no effect
3625 // C11 6.7.5p6:
3626 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003627 if (!(TmpAttr.isAlignas() && !Alignment)) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003628 if (!llvm::isPowerOf2_64(AlignVal)) {
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003629 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3630 << E->getSourceRange();
3631 return;
3632 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003633 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003634
David Majnemerabecae72014-02-12 20:36:10 +00003635 // Alignment calculations can wrap around if it's greater than 2**28.
David Majnemer29c69db2015-07-26 01:48:59 +00003636 unsigned MaxValidAlignment =
3637 Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192
3638 : 268435456;
David Majnemer0be6bd02015-07-26 09:02:21 +00003639 if (AlignVal > MaxValidAlignment) {
David Majnemerabecae72014-02-12 20:36:10 +00003640 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
3641 << E->getSourceRange();
3642 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003643 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003644
David Majnemer0be6bd02015-07-26 09:02:21 +00003645 if (Context.getTargetInfo().isTLSSupported()) {
3646 unsigned MaxTLSAlign =
3647 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3648 .getQuantity();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003649 const auto *VD = dyn_cast<VarDecl>(D);
David Majnemer0be6bd02015-07-26 09:02:21 +00003650 if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD &&
3651 VD->getTLSKind() != VarDecl::TLS_None) {
3652 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3653 << (unsigned)AlignVal << VD << MaxTLSAlign;
3654 return;
3655 }
3656 }
3657
Richard Smith44c247f2013-02-22 08:32:16 +00003658 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003659 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00003660 AA->setPackExpansion(IsPackExpansion);
3661 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003662}
3663
Michael Hanaf02bbe2013-02-01 01:19:17 +00003664void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003665 unsigned SpellingListIndex, bool IsPackExpansion) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003666 // FIXME: Cache the number on the AL object if non-dependent?
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003667 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003668 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3669 SpellingListIndex);
3670 AA->setPackExpansion(IsPackExpansion);
3671 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003672}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003673
Richard Smith848e1f12013-02-01 08:12:08 +00003674void Sema::CheckAlignasUnderalignment(Decl *D) {
3675 assert(D->hasAttrs() && "no attributes on decl");
3676
David Majnemer475b25e2015-01-21 10:54:38 +00003677 QualType UnderlyingTy, DiagTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003678 if (const auto *VD = dyn_cast<ValueDecl>(D)) {
David Majnemer475b25e2015-01-21 10:54:38 +00003679 UnderlyingTy = DiagTy = VD->getType();
3680 } else {
3681 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003682 if (const auto *ED = dyn_cast<EnumDecl>(D))
David Majnemer475b25e2015-01-21 10:54:38 +00003683 UnderlyingTy = ED->getIntegerType();
3684 }
3685 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003686 return;
3687
3688 // C++11 [dcl.align]p5, C11 6.7.5/4:
3689 // The combined effect of all alignment attributes in a declaration shall
3690 // not specify an alignment that is less strict than the alignment that
3691 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003692 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003693 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003694 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003695 if (I->isAlignmentDependent())
3696 return;
3697 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003698 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003699 Align = std::max(Align, I->getAlignment(Context));
3700 }
3701
3702 if (AlignasAttr && Align) {
3703 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003704 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003705 if (NaturalAlign > RequestedAlign)
3706 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003707 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003708 }
3709}
3710
David Majnemer2c4e00a2014-01-29 22:07:36 +00003711bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003712 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003713 MSInheritanceAttr::Spelling SemanticSpelling) {
3714 assert(RD->hasDefinition() && "RD has no definition!");
3715
David Majnemer98c9ee22014-02-07 00:43:07 +00003716 // We may not have seen base specifiers or any virtual methods yet. We will
3717 // have to wait until the record is defined to catch any mismatches.
3718 if (!RD->getDefinition()->isCompleteDefinition())
3719 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003720
David Majnemer98c9ee22014-02-07 00:43:07 +00003721 // The unspecified model never matches what a definition could need.
3722 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
3723 return false;
3724
David Majnemer4bb09802014-02-10 19:50:15 +00003725 if (BestCase) {
3726 if (RD->calculateInheritanceModel() == SemanticSpelling)
3727 return false;
3728 } else {
3729 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3730 return false;
3731 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003732
3733 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3734 << 0 /*definition*/;
3735 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3736 << RD->getNameAsString();
3737 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003738}
3739
Alexey Bataevf278eb12015-11-19 10:13:11 +00003740/// parseModeAttrArg - Parses attribute mode string and returns parsed type
3741/// attribute.
3742static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
3743 bool &IntegerMode, bool &ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003744 IntegerMode = true;
3745 ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003746 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003747 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003748 switch (Str[0]) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003749 case 'Q':
3750 DestWidth = 8;
3751 break;
3752 case 'H':
3753 DestWidth = 16;
3754 break;
3755 case 'S':
3756 DestWidth = 32;
3757 break;
3758 case 'D':
3759 DestWidth = 64;
3760 break;
3761 case 'X':
3762 DestWidth = 96;
3763 break;
3764 case 'T':
3765 DestWidth = 128;
3766 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003767 }
3768 if (Str[1] == 'F') {
3769 IntegerMode = false;
3770 } else if (Str[1] == 'C') {
3771 IntegerMode = false;
3772 ComplexMode = true;
3773 } else if (Str[1] != 'I') {
3774 DestWidth = 0;
3775 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003776 break;
3777 case 4:
3778 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3779 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003780 if (Str == "word")
Reid Klecknerf27e7522016-02-01 18:58:24 +00003781 DestWidth = S.Context.getTargetInfo().getRegisterWidth();
Daniel Dunbarafff4342009-10-18 02:09:24 +00003782 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003783 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003784 break;
3785 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003786 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003787 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003788 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003789 case 11:
3790 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003791 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003792 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003793 }
Alexey Bataevf278eb12015-11-19 10:13:11 +00003794}
3795
3796/// handleModeAttr - This attribute modifies the width of a decl with primitive
3797/// type.
3798///
3799/// Despite what would be logical, the mode attribute is a decl attribute, not a
3800/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3801/// HImode, not an intermediate pointer.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003802static void handleModeAttr(Sema &S, Decl *D, const AttributeList &AL) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003803 // This attribute isn't documented, but glibc uses it. It changes
3804 // the width of an int or unsigned int to the specified size.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003805 if (!AL.isArgIdent(0)) {
3806 S.Diag(AL.getLoc(), diag::err_attribute_argument_type) << AL.getName()
Alexey Bataevf278eb12015-11-19 10:13:11 +00003807 << AANT_ArgumentIdentifier;
3808 return;
3809 }
3810
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003811 IdentifierInfo *Name = AL.getArgAsIdent(0)->Ident;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003812
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003813 S.AddModeAttr(AL.getRange(), D, Name, AL.getAttributeSpellingListIndex());
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003814}
3815
3816void Sema::AddModeAttr(SourceRange AttrRange, Decl *D, IdentifierInfo *Name,
3817 unsigned SpellingListIndex, bool InInstantiation) {
3818 StringRef Str = Name->getName();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003819 normalizeName(Str);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003820 SourceLocation AttrLoc = AttrRange.getBegin();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003821
3822 unsigned DestWidth = 0;
3823 bool IntegerMode = true;
3824 bool ComplexMode = false;
3825 llvm::APInt VectorSize(64, 0);
3826 if (Str.size() >= 4 && Str[0] == 'V') {
3827 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
3828 size_t StrSize = Str.size();
3829 size_t VectorStringLength = 0;
3830 while ((VectorStringLength + 1) < StrSize &&
3831 isdigit(Str[VectorStringLength + 1]))
3832 ++VectorStringLength;
3833 if (VectorStringLength &&
3834 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
3835 VectorSize.isPowerOf2()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003836 parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth,
Alexey Bataevf278eb12015-11-19 10:13:11 +00003837 IntegerMode, ComplexMode);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003838 // Avoid duplicate warning from template instantiation.
3839 if (!InInstantiation)
3840 Diag(AttrLoc, diag::warn_vector_mode_deprecated);
Alexey Bataevf278eb12015-11-19 10:13:11 +00003841 } else {
3842 VectorSize = 0;
3843 }
3844 }
3845
3846 if (!VectorSize)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003847 parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode);
3848
3849 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3850 // and friends, at least with glibc.
3851 // FIXME: Make sure floating-point mappings are accurate
3852 // FIXME: Support XF and TF types
3853 if (!DestWidth) {
3854 Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name;
3855 return;
3856 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003857
3858 QualType OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003859 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003860 OldTy = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003861 else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003862 // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'.
3863 // Try to get type from enum declaration, default to int.
3864 OldTy = ED->getIntegerType();
3865 if (OldTy.isNull())
3866 OldTy = Context.IntTy;
3867 } else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003868 OldTy = cast<ValueDecl>(D)->getType();
Eli Friedman4735374e2009-03-03 06:41:03 +00003869
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003870 if (OldTy->isDependentType()) {
3871 D->addAttr(::new (Context)
3872 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
3873 return;
3874 }
3875
Alexey Bataev326057d2015-06-19 07:46:21 +00003876 // Base type can also be a vector type (see PR17453).
3877 // Distinguish between base type and base element type.
3878 QualType OldElemTy = OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003879 if (const auto *VT = OldTy->getAs<VectorType>())
Alexey Bataev326057d2015-06-19 07:46:21 +00003880 OldElemTy = VT->getElementType();
3881
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003882 // GCC allows 'mode' attribute on enumeration types (even incomplete), except
3883 // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete
3884 // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected.
3885 if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) &&
3886 VectorSize.getBoolValue()) {
3887 Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << AttrRange;
3888 return;
3889 }
3890 bool IntegralOrAnyEnumType =
3891 OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>();
3892
3893 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() &&
3894 !IntegralOrAnyEnumType)
3895 Diag(AttrLoc, diag::err_mode_not_primitive);
Eli Friedman4735374e2009-03-03 06:41:03 +00003896 else if (IntegerMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003897 if (!IntegralOrAnyEnumType)
3898 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00003899 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003900 if (!OldElemTy->isComplexType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003901 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00003902 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00003903 if (!OldElemTy->isFloatingType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003904 Diag(AttrLoc, diag::err_mode_wrong_type);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003905 }
3906
Alexey Bataev326057d2015-06-19 07:46:21 +00003907 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003908
3909 if (IntegerMode)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003910 NewElemTy = Context.getIntTypeForBitwidth(DestWidth,
3911 OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003912 else
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003913 NewElemTy = Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003914
Alexey Bataev326057d2015-06-19 07:46:21 +00003915 if (NewElemTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003916 Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003917 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003918 }
3919
Eli Friedman4735374e2009-03-03 06:41:03 +00003920 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003921 NewElemTy = Context.getComplexType(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00003922 }
3923
3924 QualType NewTy = NewElemTy;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003925 if (VectorSize.getBoolValue()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003926 NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(),
3927 VectorType::GenericVector);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003928 } else if (const auto *OldVT = OldTy->getAs<VectorType>()) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003929 // Complex machine mode does not support base vector types.
3930 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003931 Diag(AttrLoc, diag::err_complex_mode_vector_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00003932 return;
3933 }
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003934 unsigned NumElements = Context.getTypeSize(OldElemTy) *
Alexey Bataev326057d2015-06-19 07:46:21 +00003935 OldVT->getNumElements() /
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003936 Context.getTypeSize(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00003937 NewTy =
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003938 Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
Alexey Bataev326057d2015-06-19 07:46:21 +00003939 }
3940
3941 if (NewTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003942 Diag(AttrLoc, diag::err_mode_wrong_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00003943 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003944 }
3945
3946 // Install the new type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003947 if (auto *TD = dyn_cast<TypedefNameDecl>(D))
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003948 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003949 else if (auto *ED = dyn_cast<EnumDecl>(D))
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003950 ED->setIntegerType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003951 else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003952 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003953
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003954 D->addAttr(::new (Context)
3955 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003956}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003957
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003958static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &AL) {
Michael Han99315932013-01-24 16:46:58 +00003959 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003960 NoDebugAttr(AL.getRange(), S.Context,
3961 AL.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003962}
3963
Paul Robinson30e41fb2014-12-15 18:57:28 +00003964AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range,
Paul Robinson080b1f32015-01-13 18:34:56 +00003965 IdentifierInfo *Ident,
Paul Robinson30e41fb2014-12-15 18:57:28 +00003966 unsigned AttrSpellingListIndex) {
3967 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003968 Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00003969 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3970 return nullptr;
3971 }
3972
3973 if (D->hasAttr<AlwaysInlineAttr>())
3974 return nullptr;
3975
3976 return ::new (Context) AlwaysInlineAttr(Range, Context,
3977 AttrSpellingListIndex);
3978}
3979
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003980CommonAttr *Sema::mergeCommonAttr(Decl *D, SourceRange Range,
3981 IdentifierInfo *Ident,
3982 unsigned AttrSpellingListIndex) {
3983 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, Range, Ident))
3984 return nullptr;
3985
3986 return ::new (Context) CommonAttr(Range, Context, AttrSpellingListIndex);
3987}
3988
3989InternalLinkageAttr *
3990Sema::mergeInternalLinkageAttr(Decl *D, SourceRange Range,
3991 IdentifierInfo *Ident,
3992 unsigned AttrSpellingListIndex) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003993 if (const auto *VD = dyn_cast<VarDecl>(D)) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003994 // Attribute applies to Var but not any subclass of it (like ParmVar,
3995 // ImplicitParm or VarTemplateSpecialization).
3996 if (VD->getKind() != Decl::Var) {
3997 Diag(Range.getBegin(), diag::warn_attribute_wrong_decl_type)
3998 << Ident << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
3999 : ExpectedVariableOrFunction);
4000 return nullptr;
4001 }
4002 // Attribute does not apply to non-static local variables.
4003 if (VD->hasLocalStorage()) {
4004 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4005 return nullptr;
4006 }
4007 }
4008
4009 if (checkAttrMutualExclusion<CommonAttr>(*this, D, Range, Ident))
4010 return nullptr;
4011
4012 return ::new (Context)
4013 InternalLinkageAttr(Range, Context, AttrSpellingListIndex);
4014}
4015
Paul Robinson30e41fb2014-12-15 18:57:28 +00004016MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range,
4017 unsigned AttrSpellingListIndex) {
4018 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
4019 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'";
4020 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4021 return nullptr;
4022 }
4023
4024 if (D->hasAttr<MinSizeAttr>())
4025 return nullptr;
4026
4027 return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex);
4028}
4029
4030OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range,
4031 unsigned AttrSpellingListIndex) {
4032 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
4033 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
4034 Diag(Range.getBegin(), diag::note_conflicting_attribute);
4035 D->dropAttr<AlwaysInlineAttr>();
4036 }
4037 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
4038 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
4039 Diag(Range.getBegin(), diag::note_conflicting_attribute);
4040 D->dropAttr<MinSizeAttr>();
4041 }
4042
4043 if (D->hasAttr<OptimizeNoneAttr>())
4044 return nullptr;
4045
4046 return ::new (Context) OptimizeNoneAttr(Range, Context,
4047 AttrSpellingListIndex);
4048}
4049
Paul Robinsonf0674352014-03-31 22:29:15 +00004050static void handleAlwaysInlineAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004051 const AttributeList &AL) {
4052 if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, AL.getRange(),
4053 AL.getName()))
Akira Hatanakac8667622015-11-06 23:56:15 +00004054 return;
4055
Paul Robinson080b1f32015-01-13 18:34:56 +00004056 if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004057 D, AL.getRange(), AL.getName(),
4058 AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004059 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00004060}
4061
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004062static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &AL) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004063 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004064 D, AL.getRange(), AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004065 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00004066}
4067
Paul Robinsonf0674352014-03-31 22:29:15 +00004068static void handleOptimizeNoneAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004069 const AttributeList &AL) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004070 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004071 D, AL.getRange(), AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004072 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00004073}
4074
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004075static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &AL) {
4076 if (checkAttrMutualExclusion<CUDASharedAttr>(S, D, AL.getRange(),
4077 AL.getName()))
Justin Lebare71b2fa2016-09-30 23:57:34 +00004078 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004079 const auto *VD = cast<VarDecl>(D);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004080 if (!VD->hasGlobalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004081 S.Diag(AL.getLoc(), diag::err_cuda_nonglobal_constant);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004082 return;
4083 }
4084 D->addAttr(::new (S.Context) CUDAConstantAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004085 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Justin Lebare71b2fa2016-09-30 23:57:34 +00004086}
4087
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004088static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &AL) {
4089 if (checkAttrMutualExclusion<CUDAConstantAttr>(S, D, AL.getRange(),
4090 AL.getName()))
Justin Lebar10411012016-09-30 23:57:30 +00004091 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004092 const auto *VD = cast<VarDecl>(D);
Justin Lebar281ce2a2016-10-02 15:24:50 +00004093 // extern __shared__ is only allowed on arrays with no length (e.g.
4094 // "int x[]").
Jonas Hahnfeldee47d8c2018-02-14 16:04:03 +00004095 if (!S.getLangOpts().CUDARelocatableDeviceCode && VD->hasExternalStorage() &&
4096 !isa<IncompleteArrayType>(VD->getType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004097 S.Diag(AL.getLoc(), diag::err_cuda_extern_shared) << VD;
Justin Lebar10411012016-09-30 23:57:30 +00004098 return;
4099 }
Justin Lebaraa370bd2016-10-13 18:45:13 +00004100 if (S.getLangOpts().CUDA && VD->hasLocalStorage() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004101 S.CUDADiagIfHostCode(AL.getLoc(), diag::err_cuda_host_shared)
Justin Lebaraa370bd2016-10-13 18:45:13 +00004102 << S.CurrentCUDATarget())
4103 return;
Justin Lebar10411012016-09-30 23:57:30 +00004104 D->addAttr(::new (S.Context) CUDASharedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004105 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Justin Lebar10411012016-09-30 23:57:30 +00004106}
4107
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004108static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &AL) {
4109 if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, AL.getRange(),
4110 AL.getName()) ||
4111 checkAttrMutualExclusion<CUDAHostAttr>(S, D, AL.getRange(),
4112 AL.getName())) {
Justin Lebar3eaaf862016-01-13 01:07:35 +00004113 return;
4114 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004115 const auto *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00004116 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00004117 SourceRange RTRange = FD->getReturnTypeSourceRange();
4118 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004119 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00004120 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
4121 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00004122 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004123 }
Justin Lebarc66a1062016-01-20 00:26:57 +00004124 if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) {
4125 if (Method->isInstance()) {
4126 S.Diag(Method->getLocStart(), diag::err_kern_is_nonstatic_method)
4127 << Method;
4128 return;
4129 }
4130 S.Diag(Method->getLocStart(), diag::warn_kern_is_method) << Method;
4131 }
4132 // Only warn for "inline" when compiling for host, to cut down on noise.
4133 if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice)
4134 S.Diag(FD->getLocStart(), diag::warn_kern_is_inline) << FD;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004135
Aaron Ballman3aff6332013-12-02 19:30:36 +00004136 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004137 CUDAGlobalAttr(AL.getRange(), S.Context,
4138 AL.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004139}
4140
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004141static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &AL) {
4142 const auto *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00004143 if (!Fn->isInlineSpecified()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004144 S.Diag(AL.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00004145 return;
4146 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004147
Michael Han99315932013-01-24 16:46:58 +00004148 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004149 GNUInlineAttr(AL.getRange(), S.Context,
4150 AL.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00004151}
4152
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004153static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004154 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004155
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004156 // Diagnostic is emitted elsewhere: here we store the (valid) AL
John McCall3882ace2011-01-05 12:14:39 +00004157 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
4158 CallingConv CC;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004159 if (S.CheckCallingConvAttr(AL, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00004160 return;
4161
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004162 if (!isa<ObjCMethodDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004163 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
4164 << AL.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00004165 return;
4166 }
4167
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004168 switch (AL.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004169 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00004170 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004171 FastCallAttr(AL.getRange(), S.Context,
4172 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004173 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004174 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00004175 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004176 StdCallAttr(AL.getRange(), S.Context,
4177 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004178 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004179 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00004180 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004181 ThisCallAttr(AL.getRange(), S.Context,
4182 AL.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00004183 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004184 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00004185 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004186 CDeclAttr(AL.getRange(), S.Context,
4187 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004188 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004189 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00004190 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004191 PascalAttr(AL.getRange(), S.Context,
4192 AL.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00004193 return;
John McCall477f2bb2016-03-03 06:39:32 +00004194 case AttributeList::AT_SwiftCall:
4195 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004196 SwiftCallAttr(AL.getRange(), S.Context,
4197 AL.getAttributeSpellingListIndex()));
John McCall477f2bb2016-03-03 06:39:32 +00004198 return;
Reid Klecknerd7857f02014-10-24 17:42:17 +00004199 case AttributeList::AT_VectorCall:
4200 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004201 VectorCallAttr(AL.getRange(), S.Context,
4202 AL.getAttributeSpellingListIndex()));
Reid Klecknerd7857f02014-10-24 17:42:17 +00004203 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00004204 case AttributeList::AT_MSABI:
4205 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004206 MSABIAttr(AL.getRange(), S.Context,
4207 AL.getAttributeSpellingListIndex()));
Charles Davisb5a214e2013-08-30 04:39:01 +00004208 return;
4209 case AttributeList::AT_SysVABI:
4210 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004211 SysVABIAttr(AL.getRange(), S.Context,
4212 AL.getAttributeSpellingListIndex()));
Charles Davisb5a214e2013-08-30 04:39:01 +00004213 return;
Erich Keane757d3172016-11-02 18:29:35 +00004214 case AttributeList::AT_RegCall:
4215 D->addAttr(::new (S.Context) RegCallAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004216 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Erich Keane757d3172016-11-02 18:29:35 +00004217 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004218 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004219 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004220 switch (CC) {
4221 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004222 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004223 break;
4224 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004225 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004226 break;
4227 default:
4228 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004229 }
4230
Michael Han99315932013-01-24 16:46:58 +00004231 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004232 PcsAttr(AL.getRange(), S.Context, PCS,
4233 AL.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00004234 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004235 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00004236 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00004237 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004238 IntelOclBiccAttr(AL.getRange(), S.Context,
4239 AL.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00004240 return;
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004241 case AttributeList::AT_PreserveMost:
4242 D->addAttr(::new (S.Context) PreserveMostAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004243 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004244 return;
4245 case AttributeList::AT_PreserveAll:
4246 D->addAttr(::new (S.Context) PreserveAllAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004247 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004248 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004249 default:
4250 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00004251 }
4252}
4253
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004254static void handleSuppressAttr(Sema &S, Decl *D, const AttributeList &AL) {
4255 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Matthias Gehre01a63382017-03-27 19:45:24 +00004256 return;
4257
4258 std::vector<StringRef> DiagnosticIdentifiers;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004259 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Matthias Gehre01a63382017-03-27 19:45:24 +00004260 StringRef RuleName;
4261
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004262 if (!S.checkStringLiteralArgumentAttr(AL, I, RuleName, nullptr))
Matthias Gehre01a63382017-03-27 19:45:24 +00004263 return;
4264
4265 // FIXME: Warn if the rule name is unknown. This is tricky because only
4266 // clang-tidy knows about available rules.
4267 DiagnosticIdentifiers.push_back(RuleName);
4268 }
4269 D->addAttr(::new (S.Context) SuppressAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004270 AL.getRange(), S.Context, DiagnosticIdentifiers.data(),
4271 DiagnosticIdentifiers.size(), AL.getAttributeSpellingListIndex()));
Matthias Gehre01a63382017-03-27 19:45:24 +00004272}
4273
Erich Keaneb11ebc52017-09-27 03:20:13 +00004274bool Sema::CheckCallingConvAttr(const AttributeList &Attrs, CallingConv &CC,
Aaron Ballman02df2e02012-12-09 17:45:41 +00004275 const FunctionDecl *FD) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00004276 if (Attrs.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004277 return true;
4278
Erich Keaneb11ebc52017-09-27 03:20:13 +00004279 if (Attrs.hasProcessingCache()) {
4280 CC = (CallingConv) Attrs.getProcessingCache();
John McCall3b5a8f52016-03-03 00:10:03 +00004281 return false;
4282 }
4283
Erich Keaneb11ebc52017-09-27 03:20:13 +00004284 unsigned ReqArgs = Attrs.getKind() == AttributeList::AT_Pcs ? 1 : 0;
4285 if (!checkAttributeNumArgs(*this, Attrs, ReqArgs)) {
4286 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004287 return true;
4288 }
4289
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004290 // TODO: diagnose uses of these conventions on the wrong target.
Erich Keaneb11ebc52017-09-27 03:20:13 +00004291 switch (Attrs.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004292 case AttributeList::AT_CDecl: CC = CC_C; break;
4293 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
4294 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
4295 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
4296 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
John McCall477f2bb2016-03-03 06:39:32 +00004297 case AttributeList::AT_SwiftCall: CC = CC_Swift; break;
Reid Klecknerd7857f02014-10-24 17:42:17 +00004298 case AttributeList::AT_VectorCall: CC = CC_X86VectorCall; break;
Erich Keane757d3172016-11-02 18:29:35 +00004299 case AttributeList::AT_RegCall: CC = CC_X86RegCall; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00004300 case AttributeList::AT_MSABI:
4301 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
Martin Storsjo022e7822017-07-17 20:49:45 +00004302 CC_Win64;
Charles Davisb5a214e2013-08-30 04:39:01 +00004303 break;
4304 case AttributeList::AT_SysVABI:
4305 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
4306 CC_C;
4307 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004308 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00004309 StringRef StrRef;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004310 if (!checkStringLiteralArgumentAttr(Attrs, 0, StrRef)) {
4311 Attrs.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004312 return true;
4313 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004314 if (StrRef == "aapcs") {
4315 CC = CC_AAPCS;
4316 break;
4317 } else if (StrRef == "aapcs-vfp") {
4318 CC = CC_AAPCS_VFP;
4319 break;
4320 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004321
Erich Keaneb11ebc52017-09-27 03:20:13 +00004322 Attrs.setInvalid();
4323 Diag(Attrs.getLoc(), diag::err_invalid_pcs);
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004324 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004325 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00004326 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004327 case AttributeList::AT_PreserveMost: CC = CC_PreserveMost; break;
4328 case AttributeList::AT_PreserveAll: CC = CC_PreserveAll; break;
David Blaikie8a40f702012-01-17 06:56:22 +00004329 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00004330 }
4331
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004332 const TargetInfo &TI = Context.getTargetInfo();
4333 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004334 if (A != TargetInfo::CCCR_OK) {
4335 if (A == TargetInfo::CCCR_Warning)
Erich Keaneb11ebc52017-09-27 03:20:13 +00004336 Diag(Attrs.getLoc(), diag::warn_cconv_ignored) << Attrs.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00004337
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004338 // This convention is not valid for the target. Use the default function or
4339 // method calling convention.
Alexey Bataeva7547182016-05-18 09:06:38 +00004340 bool IsCXXMethod = false, IsVariadic = false;
4341 if (FD) {
4342 IsCXXMethod = FD->isCXXInstanceMember();
4343 IsVariadic = FD->isVariadic();
4344 }
4345 CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004346 }
4347
Erich Keaneb11ebc52017-09-27 03:20:13 +00004348 Attrs.setProcessingCache((unsigned) CC);
John McCall3882ace2011-01-05 12:14:39 +00004349 return false;
4350}
4351
John McCall477f2bb2016-03-03 06:39:32 +00004352/// Pointer-like types in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004353static bool isValidSwiftContextType(QualType Ty) {
4354 if (!Ty->hasPointerRepresentation())
4355 return Ty->isDependentType();
4356 return Ty->getPointeeType().getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004357}
4358
4359/// Pointers and references in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004360static bool isValidSwiftIndirectResultType(QualType Ty) {
4361 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4362 Ty = PtrType->getPointeeType();
4363 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4364 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004365 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004366 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004367 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004368 return Ty.getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004369}
4370
4371/// Pointers and references to pointers in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004372static bool isValidSwiftErrorResultType(QualType Ty) {
4373 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4374 Ty = PtrType->getPointeeType();
4375 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4376 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004377 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004378 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004379 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004380 if (!Ty.getQualifiers().empty())
John McCall477f2bb2016-03-03 06:39:32 +00004381 return false;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004382 return isValidSwiftContextType(Ty);
John McCall477f2bb2016-03-03 06:39:32 +00004383}
4384
Erich Keaneb11ebc52017-09-27 03:20:13 +00004385static void handleParameterABIAttr(Sema &S, Decl *D, const AttributeList &Attrs,
4386 ParameterABI Abi) {
4387 S.AddParameterABIAttr(Attrs.getRange(), D, Abi,
4388 Attrs.getAttributeSpellingListIndex());
John McCall477f2bb2016-03-03 06:39:32 +00004389}
4390
4391void Sema::AddParameterABIAttr(SourceRange range, Decl *D, ParameterABI abi,
4392 unsigned spellingIndex) {
4393
4394 QualType type = cast<ParmVarDecl>(D)->getType();
4395
4396 if (auto existingAttr = D->getAttr<ParameterABIAttr>()) {
4397 if (existingAttr->getABI() != abi) {
4398 Diag(range.getBegin(), diag::err_attributes_are_not_compatible)
4399 << getParameterABISpelling(abi) << existingAttr;
4400 Diag(existingAttr->getLocation(), diag::note_conflicting_attribute);
4401 return;
4402 }
4403 }
4404
4405 switch (abi) {
4406 case ParameterABI::Ordinary:
4407 llvm_unreachable("explicit attribute for ordinary parameter ABI?");
4408
4409 case ParameterABI::SwiftContext:
4410 if (!isValidSwiftContextType(type)) {
4411 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4412 << getParameterABISpelling(abi)
4413 << /*pointer to pointer */ 0 << type;
4414 }
4415 D->addAttr(::new (Context)
4416 SwiftContextAttr(range, Context, spellingIndex));
4417 return;
4418
4419 case ParameterABI::SwiftErrorResult:
4420 if (!isValidSwiftErrorResultType(type)) {
4421 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4422 << getParameterABISpelling(abi)
4423 << /*pointer to pointer */ 1 << type;
4424 }
4425 D->addAttr(::new (Context)
4426 SwiftErrorResultAttr(range, Context, spellingIndex));
4427 return;
4428
4429 case ParameterABI::SwiftIndirectResult:
4430 if (!isValidSwiftIndirectResultType(type)) {
4431 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4432 << getParameterABISpelling(abi)
4433 << /*pointer*/ 0 << type;
4434 }
4435 D->addAttr(::new (Context)
4436 SwiftIndirectResultAttr(range, Context, spellingIndex));
4437 return;
4438 }
4439 llvm_unreachable("bad parameter ABI attribute");
4440}
4441
John McCall3882ace2011-01-05 12:14:39 +00004442/// Checks a regparm attribute, returning true if it is ill-formed and
4443/// otherwise setting numParams to the appropriate value.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004444bool Sema::CheckRegparmAttr(const AttributeList &AL, unsigned &numParams) {
4445 if (AL.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004446 return true;
4447
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004448 if (!checkAttributeNumArgs(*this, AL, 1)) {
4449 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004450 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004451 }
Eli Friedman7044b762009-03-27 21:06:47 +00004452
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004453 uint32_t NP;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004454 Expr *NumParamsExpr = AL.getArgAsExpr(0);
4455 if (!checkUInt32Argument(*this, AL, NumParamsExpr, NP)) {
4456 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004457 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004458 }
4459
Douglas Gregore8bbc122011-09-02 00:18:52 +00004460 if (Context.getTargetInfo().getRegParmMax() == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004461 Diag(AL.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00004462 << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004463 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004464 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004465 }
4466
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004467 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00004468 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004469 Diag(AL.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00004470 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004471 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004472 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004473 }
4474
John McCall3882ace2011-01-05 12:14:39 +00004475 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004476}
4477
Artem Belevichbcec9da2016-06-06 22:54:57 +00004478// Checks whether an argument of launch_bounds attribute is
4479// acceptable, performs implicit conversion to Rvalue, and returns
4480// non-nullptr Expr result on success. Otherwise, it returns nullptr
4481// and may output an error.
4482static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004483 const CUDALaunchBoundsAttr &AL,
Artem Belevichbcec9da2016-06-06 22:54:57 +00004484 const unsigned Idx) {
Artem Belevich7093e402015-04-21 22:55:54 +00004485 if (S.DiagnoseUnexpandedParameterPack(E))
Artem Belevichbcec9da2016-06-06 22:54:57 +00004486 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004487
4488 // Accept template arguments for now as they depend on something else.
4489 // We'll get to check them when they eventually get instantiated.
4490 if (E->isValueDependent())
Artem Belevichbcec9da2016-06-06 22:54:57 +00004491 return E;
Artem Belevich7093e402015-04-21 22:55:54 +00004492
4493 llvm::APSInt I(64);
4494 if (!E->isIntegerConstantExpr(I, S.Context)) {
4495 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004496 << &AL << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
Artem Belevichbcec9da2016-06-06 22:54:57 +00004497 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004498 }
4499 // Make sure we can fit it in 32 bits.
4500 if (!I.isIntN(32)) {
4501 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
4502 << 32 << /* Unsigned */ 1;
Artem Belevichbcec9da2016-06-06 22:54:57 +00004503 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004504 }
4505 if (I < 0)
4506 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004507 << &AL << Idx << E->getSourceRange();
Artem Belevich7093e402015-04-21 22:55:54 +00004508
Artem Belevichbcec9da2016-06-06 22:54:57 +00004509 // We may need to perform implicit conversion of the argument.
4510 InitializedEntity Entity = InitializedEntity::InitializeParameter(
4511 S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false);
4512 ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E);
4513 assert(!ValArg.isInvalid() &&
4514 "Unexpected PerformCopyInitialization() failure.");
4515
4516 return ValArg.getAs<Expr>();
Artem Belevich7093e402015-04-21 22:55:54 +00004517}
4518
4519void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads,
4520 Expr *MinBlocks, unsigned SpellingListIndex) {
4521 CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks,
4522 SpellingListIndex);
Artem Belevichbcec9da2016-06-06 22:54:57 +00004523 MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0);
4524 if (MaxThreads == nullptr)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004525 return;
4526
Artem Belevichbcec9da2016-06-06 22:54:57 +00004527 if (MinBlocks) {
4528 MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1);
4529 if (MinBlocks == nullptr)
4530 return;
4531 }
Artem Belevich7093e402015-04-21 22:55:54 +00004532
4533 D->addAttr(::new (Context) CUDALaunchBoundsAttr(
4534 AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex));
4535}
4536
4537static void handleLaunchBoundsAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004538 const AttributeList &AL) {
4539 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
4540 !checkAttributeAtMostNumArgs(S, AL, 2))
Artem Belevich7093e402015-04-21 22:55:54 +00004541 return;
4542
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004543 S.AddLaunchBoundsAttr(AL.getRange(), D, AL.getArgAsExpr(0),
4544 AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr,
4545 AL.getAttributeSpellingListIndex());
Peter Collingbourne827301e2010-12-12 23:03:07 +00004546}
4547
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004548static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004549 const AttributeList &AL) {
4550 if (!AL.isArgIdent(0)) {
4551 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
4552 << AL.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004553 return;
4554 }
Aaron Ballman00e99962013-08-31 01:11:41 +00004555
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004556 if (!checkAttributeNumArgs(S, AL, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004557 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004558
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004559 IdentifierInfo *ArgumentKind = AL.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004560
4561 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004562 S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type)
4563 << AL.getName() << ExpectedFunctionOrMethod;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004564 return;
4565 }
4566
4567 uint64_t ArgumentIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004568 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 2, AL.getArgAsExpr(1),
Alp Toker601b22c2014-01-21 23:35:24 +00004569 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004570 return;
4571
4572 uint64_t TypeTagIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004573 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 3, AL.getArgAsExpr(2),
Alp Toker601b22c2014-01-21 23:35:24 +00004574 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004575 return;
4576
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004577 bool IsPointer = (AL.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004578 if (IsPointer) {
4579 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00004580 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004581 if (!BufferTy->isPointerType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004582 S.Diag(AL.getLoc(), diag::err_attribute_pointers_only)
4583 << AL.getName() << 0;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004584 }
4585 }
4586
Michael Han99315932013-01-24 16:46:58 +00004587 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004588 ArgumentWithTypeTagAttr(AL.getRange(), S.Context, ArgumentKind,
Michael Han99315932013-01-24 16:46:58 +00004589 ArgumentIdx, TypeTagIdx, IsPointer,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004590 AL.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004591}
4592
4593static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004594 const AttributeList &AL) {
4595 if (!AL.isArgIdent(0)) {
4596 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
4597 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004598 return;
4599 }
Aaron Ballman00e99962013-08-31 01:11:41 +00004600
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004601 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00004602 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004603
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004604 if (!isa<VarDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004605 S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type)
4606 << AL.getName() << ExpectedVariable;
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004607 return;
4608 }
4609
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004610 IdentifierInfo *PointerKind = AL.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00004611 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004612 S.GetTypeFromParser(AL.getMatchingCType(), &MatchingCTypeLoc);
Richard Smithb87c4652013-10-31 21:23:20 +00004613 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004614
Michael Han99315932013-01-24 16:46:58 +00004615 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004616 TypeTagForDatatypeAttr(AL.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00004617 MatchingCTypeLoc,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004618 AL.getLayoutCompatible(),
4619 AL.getMustBeNull(),
4620 AL.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004621}
4622
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004623static void handleXRayLogArgsAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004624 const AttributeList &AL) {
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004625 uint64_t ArgCount;
Dean Michael Berris7456a282017-06-16 03:22:09 +00004626
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004627 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, AL.getArgAsExpr(0),
Dean Michael Berris7456a282017-06-16 03:22:09 +00004628 ArgCount,
4629 true /* AllowImplicitThis*/))
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004630 return;
4631
4632 // ArgCount isn't a parameter index [0;n), it's a count [1;n] - hence + 1.
4633 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004634 XRayLogArgsAttr(AL.getRange(), S.Context, ++ArgCount,
4635 AL.getAttributeSpellingListIndex()));
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004636}
4637
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004638//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004639// Checker-specific attribute handlers.
4640//===----------------------------------------------------------------------===//
4641
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004642static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType QT) {
4643 return QT->isDependentType() || QT->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004644}
4645
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004646static bool isValidSubjectOfNSAttribute(Sema &S, QualType QT) {
4647 return QT->isDependentType() || QT->isObjCObjectPointerType() ||
4648 S.Context.isObjCNSObjectType(QT);
John McCalled433932011-01-25 03:31:58 +00004649}
Eugene Zelenko1ced5092016-02-12 22:53:10 +00004650
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004651static bool isValidSubjectOfCFAttribute(Sema &S, QualType QT) {
4652 return QT->isDependentType() || QT->isPointerType() ||
4653 isValidSubjectOfNSAttribute(S, QT);
John McCalled433932011-01-25 03:31:58 +00004654}
4655
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004656static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &AL) {
4657 S.AddNSConsumedAttr(AL.getRange(), D, AL.getAttributeSpellingListIndex(),
4658 AL.getKind() == AttributeList::AT_NSConsumed,
John McCall3b5a8f52016-03-03 00:10:03 +00004659 /*template instantiation*/ false);
4660}
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004661
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004662void Sema::AddNSConsumedAttr(SourceRange AttrRange, Decl *D,
4663 unsigned SpellingIndex, bool IsNSConsumed,
4664 bool IsTemplateInstantiation) {
4665 const auto *Param = cast<ParmVarDecl>(D);
4666 bool TypeOK;
John McCall3b5a8f52016-03-03 00:10:03 +00004667
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004668 if (IsNSConsumed)
4669 TypeOK = isValidSubjectOfNSAttribute(*this, Param->getType());
4670 else
4671 TypeOK = isValidSubjectOfCFAttribute(*this, Param->getType());
John McCalled433932011-01-25 03:31:58 +00004672
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004673 if (!TypeOK) {
John McCall3b5a8f52016-03-03 00:10:03 +00004674 // These attributes are normally just advisory, but in ARC, ns_consumed
4675 // is significant. Allow non-dependent code to contain inappropriate
4676 // attributes even in ARC, but require template instantiations to be
4677 // set up correctly.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004678 Diag(D->getLocStart(), (IsTemplateInstantiation && IsNSConsumed &&
4679 getLangOpts().ObjCAutoRefCount
4680 ? diag::err_ns_attribute_wrong_parameter_type
4681 : diag::warn_ns_attribute_wrong_parameter_type))
4682 << AttrRange << (IsNSConsumed ? "ns_consumed" : "cf_consumed")
4683 << (IsNSConsumed ? /*objc pointers*/ 0 : /*cf pointers*/ 1);
John McCalled433932011-01-25 03:31:58 +00004684 return;
4685 }
4686
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004687 if (IsNSConsumed)
4688 D->addAttr(::new (Context)
4689 NSConsumedAttr(AttrRange, Context, SpellingIndex));
John McCalled433932011-01-25 03:31:58 +00004690 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004691 D->addAttr(::new (Context)
4692 CFConsumedAttr(AttrRange, Context, SpellingIndex));
John McCalled433932011-01-25 03:31:58 +00004693}
4694
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004695bool Sema::checkNSReturnsRetainedReturnType(SourceLocation Loc, QualType QT) {
4696 if (isValidSubjectOfNSReturnsRetainedAttribute(QT))
John McCall12251882017-07-15 11:06:46 +00004697 return false;
4698
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004699 Diag(Loc, diag::warn_ns_attribute_wrong_return_type)
4700 << "'ns_returns_retained'" << 0 << 0;
John McCall12251882017-07-15 11:06:46 +00004701 return true;
4702}
4703
Chandler Carruthedc2c642011-07-02 00:01:44 +00004704static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004705 const AttributeList &AL) {
4706 QualType ReturnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004707
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004708 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
4709 ReturnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004710 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004711 (AL.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004712 return; // ignore: was handled as a type attribute
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004713 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D))
4714 ReturnType = PD->getType();
4715 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
4716 ReturnType = FD->getReturnType();
4717 else if (const auto *Param = dyn_cast<ParmVarDecl>(D)) {
4718 ReturnType = Param->getType()->getPointeeType();
4719 if (ReturnType.isNull()) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004720 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004721 << AL.getName() << /*pointer-to-CF*/2
4722 << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004723 return;
4724 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004725 } else if (AL.isUsedAsTypeAttr()) {
John McCall12251882017-07-15 11:06:46 +00004726 return;
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004727 } else {
4728 AttributeDeclKind ExpectedDeclKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004729 switch (AL.getKind()) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004730 default: llvm_unreachable("invalid ownership attribute");
4731 case AttributeList::AT_NSReturnsRetained:
4732 case AttributeList::AT_NSReturnsAutoreleased:
4733 case AttributeList::AT_NSReturnsNotRetained:
4734 ExpectedDeclKind = ExpectedFunctionOrMethod;
4735 break;
4736
4737 case AttributeList::AT_CFReturnsRetained:
4738 case AttributeList::AT_CFReturnsNotRetained:
4739 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
4740 break;
4741 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004742 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004743 << AL.getRange() << AL.getName() << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004744 return;
4745 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004746
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004747 bool TypeOK;
4748 bool Cf;
4749 switch (AL.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004750 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004751 case AttributeList::AT_NSReturnsRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004752 TypeOK = isValidSubjectOfNSReturnsRetainedAttribute(ReturnType);
4753 Cf = false;
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004754 break;
4755
4756 case AttributeList::AT_NSReturnsAutoreleased:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004757 case AttributeList::AT_NSReturnsNotRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004758 TypeOK = isValidSubjectOfNSAttribute(S, ReturnType);
4759 Cf = false;
John McCalled433932011-01-25 03:31:58 +00004760 break;
4761
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004762 case AttributeList::AT_CFReturnsRetained:
4763 case AttributeList::AT_CFReturnsNotRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004764 TypeOK = isValidSubjectOfCFAttribute(S, ReturnType);
4765 Cf = true;
John McCalled433932011-01-25 03:31:58 +00004766 break;
4767 }
4768
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004769 if (!TypeOK) {
4770 if (AL.isUsedAsTypeAttr())
John McCall12251882017-07-15 11:06:46 +00004771 return;
4772
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004773 if (isa<ParmVarDecl>(D)) {
4774 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004775 << AL.getName() << /*pointer-to-CF*/2
4776 << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004777 } else {
4778 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
4779 enum : unsigned {
4780 Function,
4781 Method,
4782 Property
4783 } SubjectKind = Function;
4784 if (isa<ObjCMethodDecl>(D))
4785 SubjectKind = Method;
4786 else if (isa<ObjCPropertyDecl>(D))
4787 SubjectKind = Property;
4788 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004789 << AL.getName() << SubjectKind << Cf
4790 << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004791 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004792 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004793 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004794
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004795 switch (AL.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004796 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004797 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004798 case AttributeList::AT_NSReturnsAutoreleased:
Nico Weber462fd1e2015-01-07 23:50:05 +00004799 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004800 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004801 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004802 case AttributeList::AT_CFReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004803 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004804 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004805 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004806 case AttributeList::AT_NSReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004807 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004808 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004809 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004810 case AttributeList::AT_CFReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004811 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004812 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004813 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004814 case AttributeList::AT_NSReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004815 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004816 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004817 return;
4818 };
4819}
4820
John McCallcf166702011-07-22 08:53:00 +00004821static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
Erich Keaneb11ebc52017-09-27 03:20:13 +00004822 const AttributeList &Attrs) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00004823 const int EP_ObjCMethod = 1;
4824 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004825
Erich Keaneb11ebc52017-09-27 03:20:13 +00004826 SourceLocation loc = Attrs.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004827 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004828 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004829 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004830 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004831 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00004832
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004833 if (!resultType->isReferenceType() &&
4834 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004835 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00004836 << SourceRange(loc)
Erich Keaneb11ebc52017-09-27 03:20:13 +00004837 << Attrs.getName()
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004838 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004839 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00004840
4841 // Drop the attribute.
4842 return;
4843 }
4844
Nico Weber462fd1e2015-01-07 23:50:05 +00004845 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(
Erich Keaneb11ebc52017-09-27 03:20:13 +00004846 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004847}
4848
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004849static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
Erich Keaneb11ebc52017-09-27 03:20:13 +00004850 const AttributeList &Attrs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004851 const auto *Method = cast<ObjCMethodDecl>(D);
4852
4853 const DeclContext *DC = Method->getDeclContext();
4854 if (const auto *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004855 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004856 << Attrs.getName() << 0;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004857 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4858 return;
4859 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004860 if (Method->getMethodFamily() == OMF_dealloc) {
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004861 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004862 << Attrs.getName() << 1;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004863 return;
4864 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004865
4866 D->addAttr(::new (S.Context) ObjCRequiresSuperAttr(
4867 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004868}
4869
Aaron Ballmanfb763042013-12-02 18:05:46 +00004870static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004871 const AttributeList &AL) {
4872 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, AL.getRange(),
4873 AL.getName()))
John McCall32f5fe12011-09-30 05:12:12 +00004874 return;
John McCall32f5fe12011-09-30 05:12:12 +00004875
Aaron Ballmanfb763042013-12-02 18:05:46 +00004876 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004877 CFAuditedTransferAttr(AL.getRange(), S.Context,
4878 AL.getAttributeSpellingListIndex()));
Aaron Ballmanfb763042013-12-02 18:05:46 +00004879}
4880
4881static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004882 const AttributeList &AL) {
4883 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, AL.getRange(),
4884 AL.getName()))
Aaron Ballmanfb763042013-12-02 18:05:46 +00004885 return;
4886
4887 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004888 CFUnknownTransferAttr(AL.getRange(), S.Context,
4889 AL.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004890}
4891
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004892static void handleObjCBridgeAttr(Sema &S, Decl *D, const AttributeList &AL) {
4893 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004894
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004895 if (!Parm) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004896 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << AL.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004897 return;
4898 }
John McCall28592582015-02-01 22:34:06 +00004899
4900 // Typedefs only allow objc_bridge(id) and have some additional checking.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004901 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall28592582015-02-01 22:34:06 +00004902 if (!Parm->Ident->isStr("id")) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004903 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_id)
4904 << AL.getName();
John McCall28592582015-02-01 22:34:06 +00004905 return;
4906 }
4907
4908 // Only allow 'cv void *'.
4909 QualType T = TD->getUnderlyingType();
4910 if (!T->isVoidPointerType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004911 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
John McCall28592582015-02-01 22:34:06 +00004912 return;
4913 }
4914 }
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004915
4916 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004917 ObjCBridgeAttr(AL.getRange(), S.Context, Parm->Ident,
4918 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004919}
4920
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004921static void handleObjCBridgeMutableAttr(Sema &S, Decl *D,
4922 const AttributeList &AL) {
4923 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Craig Topperc3ec1492014-05-26 06:22:03 +00004924
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004925 if (!Parm) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004926 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << AL.getName() << 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004927 return;
4928 }
4929
4930 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004931 ObjCBridgeMutableAttr(AL.getRange(), S.Context, Parm->Ident,
4932 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004933}
4934
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004935static void handleObjCBridgeRelatedAttr(Sema &S, Decl *D,
4936 const AttributeList &AL) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004937 IdentifierInfo *RelatedClass =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004938 AL.isArgIdent(0) ? AL.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004939 if (!RelatedClass) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004940 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << AL.getName() << 0;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004941 return;
4942 }
4943 IdentifierInfo *ClassMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004944 AL.getArgAsIdent(1) ? AL.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004945 IdentifierInfo *InstanceMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004946 AL.getArgAsIdent(2) ? AL.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004947 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004948 ObjCBridgeRelatedAttr(AL.getRange(), S.Context, RelatedClass,
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004949 ClassMethod, InstanceMethod,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004950 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004951}
4952
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004953static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004954 const AttributeList &AL) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004955 ObjCInterfaceDecl *IFace;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004956 if (auto *CatDecl = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004957 IFace = CatDecl->getClassInterface();
4958 else
4959 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00004960
4961 if (!IFace)
4962 return;
4963
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00004964 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00004965 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004966 ObjCDesignatedInitializerAttr(AL.getRange(), S.Context,
4967 AL.getAttributeSpellingListIndex()));
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004968}
4969
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004970static void handleObjCRuntimeName(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004971 const AttributeList &AL) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004972 StringRef MetaDataName;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004973 if (!S.checkStringLiteralArgumentAttr(AL, 0, MetaDataName))
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004974 return;
4975 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004976 ObjCRuntimeNameAttr(AL.getRange(), S.Context,
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004977 MetaDataName,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004978 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004979}
4980
Nico Webera6916892016-06-10 18:53:04 +00004981// When a user wants to use objc_boxable with a union or struct
4982// but they don't have access to the declaration (legacy/third-party code)
4983// then they can 'enable' this feature with a typedef:
Alex Denisovfde64952015-06-26 05:28:36 +00004984// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004985static void handleObjCBoxable(Sema &S, Decl *D, const AttributeList &AL) {
Alex Denisovfde64952015-06-26 05:28:36 +00004986 bool notify = false;
4987
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004988 auto *RD = dyn_cast<RecordDecl>(D);
Alex Denisovfde64952015-06-26 05:28:36 +00004989 if (RD && RD->getDefinition()) {
4990 RD = RD->getDefinition();
4991 notify = true;
4992 }
4993
4994 if (RD) {
4995 ObjCBoxableAttr *BoxableAttr = ::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004996 ObjCBoxableAttr(AL.getRange(), S.Context,
4997 AL.getAttributeSpellingListIndex());
Alex Denisovfde64952015-06-26 05:28:36 +00004998 RD->addAttr(BoxableAttr);
4999 if (notify) {
5000 // we need to notify ASTReader/ASTWriter about
5001 // modification of existing declaration
5002 if (ASTMutationListener *L = S.getASTMutationListener())
5003 L->AddedAttributeToRecord(BoxableAttr, RD);
5004 }
5005 }
5006}
5007
Chandler Carruthedc2c642011-07-02 00:01:44 +00005008static void handleObjCOwnershipAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005009 const AttributeList &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00005010 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00005011
Chandler Carruthff4c4f02011-07-01 23:49:12 +00005012 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005013 << AL.getRange() << AL.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00005014}
5015
Chandler Carruthedc2c642011-07-02 00:01:44 +00005016static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005017 const AttributeList &AL) {
5018 const auto *VD = cast<ValueDecl>(D);
5019 QualType QT = VD->getType();
John McCall31168b02011-06-15 23:02:42 +00005020
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005021 if (!QT->isDependentType() &&
5022 !QT->isObjCLifetimeType()) {
5023 S.Diag(AL.getLoc(), diag::err_objc_precise_lifetime_bad_type)
5024 << QT;
John McCall31168b02011-06-15 23:02:42 +00005025 return;
5026 }
5027
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005028 Qualifiers::ObjCLifetime Lifetime = QT.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +00005029
5030 // If we have no lifetime yet, check the lifetime we're presumably
5031 // going to infer.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005032 if (Lifetime == Qualifiers::OCL_None && !QT->isDependentType())
5033 Lifetime = QT->getObjCARCImplicitLifetime();
John McCall31168b02011-06-15 23:02:42 +00005034
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005035 switch (Lifetime) {
John McCall31168b02011-06-15 23:02:42 +00005036 case Qualifiers::OCL_None:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005037 assert(QT->isDependentType() &&
John McCall31168b02011-06-15 23:02:42 +00005038 "didn't infer lifetime for non-dependent type?");
5039 break;
5040
5041 case Qualifiers::OCL_Weak: // meaningful
5042 case Qualifiers::OCL_Strong: // meaningful
5043 break;
5044
5045 case Qualifiers::OCL_ExplicitNone:
5046 case Qualifiers::OCL_Autoreleasing:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005047 S.Diag(AL.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
5048 << (Lifetime == Qualifiers::OCL_Autoreleasing);
John McCall31168b02011-06-15 23:02:42 +00005049 break;
5050 }
5051
Chandler Carruthff4c4f02011-07-01 23:49:12 +00005052 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005053 ObjCPreciseLifetimeAttr(AL.getRange(), S.Context,
5054 AL.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00005055}
5056
Francois Picheta83957a2010-12-19 06:50:37 +00005057//===----------------------------------------------------------------------===//
5058// Microsoft specific attribute handlers.
5059//===----------------------------------------------------------------------===//
5060
Nico Weber88f5ed92016-09-13 18:55:26 +00005061UuidAttr *Sema::mergeUuidAttr(Decl *D, SourceRange Range,
5062 unsigned AttrSpellingListIndex, StringRef Uuid) {
5063 if (const auto *UA = D->getAttr<UuidAttr>()) {
Nico Weberd58c2602016-09-14 01:16:54 +00005064 if (UA->getGuid().equals_lower(Uuid))
Nico Weber88f5ed92016-09-13 18:55:26 +00005065 return nullptr;
5066 Diag(UA->getLocation(), diag::err_mismatched_uuid);
5067 Diag(Range.getBegin(), diag::note_previous_uuid);
5068 D->dropAttr<UuidAttr>();
5069 }
5070
5071 return ::new (Context) UuidAttr(Range, Context, Uuid, AttrSpellingListIndex);
5072}
5073
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005074static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005075 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005076 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
5077 << AL.getName() << AttributeLangSupport::C;
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005078 return;
5079 }
5080
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005081 StringRef StrRef;
5082 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005083 if (!S.checkStringLiteralArgumentAttr(AL, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00005084 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005085
David Majnemer89085342013-08-09 08:56:20 +00005086 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
5087 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00005088 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
5089 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00005090
Reid Kleckner140c4a72013-05-17 14:04:52 +00005091 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00005092 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005093 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005094 return;
5095 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00005096
David Majnemer89085342013-08-09 08:56:20 +00005097 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00005098 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00005099 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005100 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00005101 return;
5102 }
David Majnemer89085342013-08-09 08:56:20 +00005103 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005104 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005105 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005106 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00005107 }
Francois Picheta83957a2010-12-19 06:50:37 +00005108
Nico Weber469891e2017-05-05 17:05:56 +00005109 // FIXME: It'd be nice to also emit a fixit removing uuid(...) (and, if it's
5110 // the only thing in the [] list, the [] too), and add an insertion of
5111 // __declspec(uuid(...)). But sadly, neither the SourceLocs of the commas
5112 // separating attributes nor of the [ and the ] are in the AST.
Nico Weber0a234042017-05-05 17:15:08 +00005113 // Cf "SourceLocations of attribute list delimiters - [[ ... , ... ]] etc"
Nico Weber469891e2017-05-05 17:05:56 +00005114 // on cfe-dev.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005115 if (AL.isMicrosoftAttribute()) // Check for [uuid(...)] spelling.
5116 S.Diag(AL.getLoc(), diag::warn_atl_uuid_deprecated);
Nico Weber469891e2017-05-05 17:05:56 +00005117
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005118 UuidAttr *UA = S.mergeUuidAttr(D, AL.getRange(),
5119 AL.getAttributeSpellingListIndex(), StrRef);
Nico Weber88f5ed92016-09-13 18:55:26 +00005120 if (UA)
5121 D->addAttr(UA);
Charles Davis163855f2010-02-16 18:27:26 +00005122}
5123
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005124static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &AL) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005125 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005126 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
5127 << AL.getName() << AttributeLangSupport::C;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005128 return;
5129 }
5130 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005131 D, AL.getRange(), /*BestCase=*/true,
5132 AL.getAttributeSpellingListIndex(),
5133 (MSInheritanceAttr::Spelling)AL.getSemanticSpelling());
David Majnemer929025d2016-01-26 19:30:26 +00005134 if (IA) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005135 D->addAttr(IA);
David Majnemer929025d2016-01-26 19:30:26 +00005136 S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D));
5137 }
David Majnemer2c4e00a2014-01-29 22:07:36 +00005138}
5139
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005140static void handleDeclspecThreadAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005141 const AttributeList &AL) {
5142 const auto *VD = cast<VarDecl>(D);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005143 if (!S.Context.getTargetInfo().isTLSSupported()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005144 S.Diag(AL.getLoc(), diag::err_thread_unsupported);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005145 return;
5146 }
5147 if (VD->getTSCSpec() != TSCS_unspecified) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005148 S.Diag(AL.getLoc(), diag::err_declspec_thread_on_thread_variable);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005149 return;
5150 }
5151 if (VD->hasLocalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005152 S.Diag(AL.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005153 return;
5154 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005155 D->addAttr(::new (S.Context) ThreadAttr(AL.getRange(), S.Context,
5156 AL.getAttributeSpellingListIndex()));
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005157}
5158
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005159static void handleAbiTagAttr(Sema &S, Decl *D, const AttributeList &AL) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005160 SmallVector<StringRef, 4> Tags;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005161 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005162 StringRef Tag;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005163 if (!S.checkStringLiteralArgumentAttr(AL, I, Tag))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005164 return;
5165 Tags.push_back(Tag);
5166 }
5167
5168 if (const auto *NS = dyn_cast<NamespaceDecl>(D)) {
5169 if (!NS->isInline()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005170 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 0;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005171 return;
5172 }
5173 if (NS->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005174 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 1;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005175 return;
5176 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005177 if (AL.getNumArgs() == 0)
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005178 Tags.push_back(NS->getName());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005179 } else if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005180 return;
5181
5182 // Store tags sorted and without duplicates.
5183 std::sort(Tags.begin(), Tags.end());
5184 Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end());
5185
5186 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005187 AbiTagAttr(AL.getRange(), S.Context, Tags.data(), Tags.size(),
5188 AL.getAttributeSpellingListIndex()));
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005189}
5190
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005191static void handleARMInterruptAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005192 const AttributeList &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005193 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005194 if (AL.getNumArgs() > 1) {
5195 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
5196 << AL.getName() << 1;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005197 return;
5198 }
5199
5200 StringRef Str;
5201 SourceLocation ArgLoc;
5202
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005203 if (AL.getNumArgs() == 0)
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005204 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005205 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005206 return;
5207
5208 ARMInterruptAttr::InterruptType Kind;
5209 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005210 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
5211 << AL.getName() << Str << ArgLoc;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005212 return;
5213 }
5214
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005215 unsigned Index = AL.getAttributeSpellingListIndex();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005216 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005217 ARMInterruptAttr(AL.getLoc(), S.Context, Kind, Index));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005218}
5219
5220static void handleMSP430InterruptAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005221 const AttributeList &AL) {
5222 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005223 return;
5224
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005225 if (!AL.isArgExpr(0)) {
5226 S.Diag(AL.getLoc(), diag::err_attribute_argument_type) << AL.getName()
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005227 << AANT_ArgumentIntegerConstant;
5228 return;
5229 }
5230
5231 // FIXME: Check for decl - it should be void ()(void).
5232
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005233 Expr *NumParamsExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005234 llvm::APSInt NumParams(32);
5235 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005236 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
5237 << AL.getName() << AANT_ArgumentIntegerConstant
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005238 << NumParamsExpr->getSourceRange();
5239 return;
5240 }
5241
5242 unsigned Num = NumParams.getLimitedValue(255);
5243 if ((Num & 1) || Num > 30) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005244 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
5245 << AL.getName() << (int)NumParams.getSExtValue()
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005246 << NumParamsExpr->getSourceRange();
5247 return;
5248 }
5249
Aaron Ballman36a53502014-01-16 13:03:14 +00005250 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005251 MSP430InterruptAttr(AL.getLoc(), S.Context, Num,
5252 AL.getAttributeSpellingListIndex()));
Aaron Ballman36a53502014-01-16 13:03:14 +00005253 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005254}
5255
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005256static void handleMipsInterruptAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005257 const AttributeList &AL) {
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005258 // Only one optional argument permitted.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005259 if (AL.getNumArgs() > 1) {
5260 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
5261 << AL.getName() << 1;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005262 return;
5263 }
5264
5265 StringRef Str;
5266 SourceLocation ArgLoc;
5267
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005268 if (AL.getNumArgs() == 0)
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005269 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005270 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005271 return;
5272
5273 // Semantic checks for a function with the 'interrupt' attribute for MIPS:
5274 // a) Must be a function.
5275 // b) Must have no parameters.
5276 // c) Must have the 'void' return type.
5277 // d) Cannot have the 'mips16' attribute, as that instruction set
5278 // lacks the 'eret' instruction.
5279 // e) The attribute itself must either have no argument or one of the
5280 // valid interrupt types, see [MipsInterruptDocs].
5281
5282 if (!isFunctionOrMethod(D)) {
5283 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5284 << "'interrupt'" << ExpectedFunctionOrMethod;
5285 return;
5286 }
5287
5288 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
5289 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
5290 << 0;
5291 return;
5292 }
5293
5294 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5295 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
5296 << 1;
5297 return;
5298 }
5299
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005300 if (checkAttrMutualExclusion<Mips16Attr>(S, D, AL.getRange(),
5301 AL.getName()))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005302 return;
5303
5304 MipsInterruptAttr::InterruptType Kind;
5305 if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005306 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
5307 << AL.getName() << "'" + std::string(Str) + "'";
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005308 return;
5309 }
5310
5311 D->addAttr(::new (S.Context) MipsInterruptAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005312 AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex()));
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005313}
5314
Alexey Bataevd51e9932016-01-15 04:06:31 +00005315static void handleAnyX86InterruptAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005316 const AttributeList &AL) {
Alexey Bataevd51e9932016-01-15 04:06:31 +00005317 // Semantic checks for a function with the 'interrupt' attribute.
5318 // a) Must be a function.
5319 // b) Must have the 'void' return type.
5320 // c) Must take 1 or 2 arguments.
5321 // d) The 1st argument must be a pointer.
5322 // e) The 2nd argument (if any) must be an unsigned integer.
5323 if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) ||
5324 CXXMethodDecl::isStaticOverloadedOperator(
5325 cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005326 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
5327 << AL.getName() << ExpectedFunctionWithProtoType;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005328 return;
5329 }
5330 // Interrupt handler must have void return type.
5331 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5332 S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(),
5333 diag::err_anyx86_interrupt_attribute)
5334 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5335 ? 0
5336 : 1)
5337 << 0;
5338 return;
5339 }
5340 // Interrupt handler must have 1 or 2 parameters.
5341 unsigned NumParams = getFunctionOrMethodNumParams(D);
5342 if (NumParams < 1 || NumParams > 2) {
5343 S.Diag(D->getLocStart(), diag::err_anyx86_interrupt_attribute)
5344 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5345 ? 0
5346 : 1)
5347 << 1;
5348 return;
5349 }
5350 // The first argument must be a pointer.
5351 if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) {
5352 S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(),
5353 diag::err_anyx86_interrupt_attribute)
5354 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5355 ? 0
5356 : 1)
5357 << 2;
5358 return;
5359 }
5360 // The second argument, if present, must be an unsigned integer.
5361 unsigned TypeSize =
5362 S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64
5363 ? 64
5364 : 32;
5365 if (NumParams == 2 &&
5366 (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() ||
5367 S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) {
5368 S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(),
5369 diag::err_anyx86_interrupt_attribute)
5370 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5371 ? 0
5372 : 1)
5373 << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false);
5374 return;
5375 }
5376 D->addAttr(::new (S.Context) AnyX86InterruptAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005377 AL.getLoc(), S.Context, AL.getAttributeSpellingListIndex()));
Alexey Bataevd51e9932016-01-15 04:06:31 +00005378 D->addAttr(UsedAttr::CreateImplicit(S.Context));
5379}
5380
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005381static void handleAVRInterruptAttr(Sema &S, Decl *D, const AttributeList &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005382 if (!isFunctionOrMethod(D)) {
5383 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5384 << "'interrupt'" << ExpectedFunction;
5385 return;
5386 }
5387
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005388 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005389 return;
5390
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005391 handleSimpleAttribute<AVRInterruptAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005392}
5393
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005394static void handleAVRSignalAttr(Sema &S, Decl *D, const AttributeList &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005395 if (!isFunctionOrMethod(D)) {
5396 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5397 << "'signal'" << ExpectedFunction;
5398 return;
5399 }
5400
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005401 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005402 return;
5403
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005404 handleSimpleAttribute<AVRSignalAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005405}
5406
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005407static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005408 // Dispatch the interrupt attribute based on the current target.
Alexey Bataevd51e9932016-01-15 04:06:31 +00005409 switch (S.Context.getTargetInfo().getTriple().getArch()) {
5410 case llvm::Triple::msp430:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005411 handleMSP430InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005412 break;
5413 case llvm::Triple::mipsel:
5414 case llvm::Triple::mips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005415 handleMipsInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005416 break;
5417 case llvm::Triple::x86:
5418 case llvm::Triple::x86_64:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005419 handleAnyX86InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005420 break;
Dylan McKaye8232d72017-02-08 05:09:26 +00005421 case llvm::Triple::avr:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005422 handleAVRInterruptAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005423 break;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005424 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005425 handleARMInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005426 break;
5427 }
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005428}
5429
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005430static void handleAMDGPUFlatWorkGroupSizeAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005431 const AttributeList &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005432 uint32_t Min = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005433 Expr *MinExpr = AL.getArgAsExpr(0);
5434 if (!checkUInt32Argument(S, AL, MinExpr, Min))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005435 return;
5436
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005437 uint32_t Max = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005438 Expr *MaxExpr = AL.getArgAsExpr(1);
5439 if (!checkUInt32Argument(S, AL, MaxExpr, Max))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005440 return;
5441
5442 if (Min == 0 && Max != 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005443 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid)
5444 << AL.getName() << 0;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005445 return;
5446 }
5447 if (Min > Max) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005448 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid)
5449 << AL.getName() << 1;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005450 return;
5451 }
5452
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005453 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005454 AMDGPUFlatWorkGroupSizeAttr(AL.getLoc(), S.Context, Min, Max,
5455 AL.getAttributeSpellingListIndex()));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005456}
5457
5458static void handleAMDGPUWavesPerEUAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005459 const AttributeList &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005460 uint32_t Min = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005461 Expr *MinExpr = AL.getArgAsExpr(0);
5462 if (!checkUInt32Argument(S, AL, MinExpr, Min))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005463 return;
5464
5465 uint32_t Max = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005466 if (AL.getNumArgs() == 2) {
5467 Expr *MaxExpr = AL.getArgAsExpr(1);
5468 if (!checkUInt32Argument(S, AL, MaxExpr, Max))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005469 return;
5470 }
5471
5472 if (Min == 0 && Max != 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005473 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid)
5474 << AL.getName() << 0;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005475 return;
5476 }
5477 if (Max != 0 && Min > Max) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005478 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid)
5479 << AL.getName() << 1;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005480 return;
5481 }
5482
5483 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005484 AMDGPUWavesPerEUAttr(AL.getLoc(), S.Context, Min, Max,
5485 AL.getAttributeSpellingListIndex()));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005486}
5487
5488static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005489 const AttributeList &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005490 uint32_t NumSGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005491 Expr *NumSGPRExpr = AL.getArgAsExpr(0);
5492 if (!checkUInt32Argument(S, AL, NumSGPRExpr, NumSGPR))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005493 return;
5494
5495 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005496 AMDGPUNumSGPRAttr(AL.getLoc(), S.Context, NumSGPR,
5497 AL.getAttributeSpellingListIndex()));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005498}
5499
5500static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005501 const AttributeList &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005502 uint32_t NumVGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005503 Expr *NumVGPRExpr = AL.getArgAsExpr(0);
5504 if (!checkUInt32Argument(S, AL, NumVGPRExpr, NumVGPR))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005505 return;
5506
5507 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005508 AMDGPUNumVGPRAttr(AL.getLoc(), S.Context, NumVGPR,
5509 AL.getAttributeSpellingListIndex()));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005510}
5511
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005512static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005513 const AttributeList& AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005514 // If we try to apply it to a function pointer, don't warn, but don't
5515 // do anything, either. It doesn't matter anyway, because there's nothing
5516 // special about calling a force_align_arg_pointer function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005517 const auto *VD = dyn_cast<ValueDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005518 if (VD && VD->getType()->isFunctionPointerType())
5519 return;
5520 // Also don't warn on function pointer typedefs.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005521 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005522 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
5523 TD->getUnderlyingType()->isFunctionType()))
5524 return;
5525 // Attribute can only be applied to function types.
5526 if (!isa<FunctionDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005527 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
5528 << AL.getName() << ExpectedFunction;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005529 return;
5530 }
5531
Aaron Ballman36a53502014-01-16 13:03:14 +00005532 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005533 X86ForceAlignArgPointerAttr(AL.getRange(), S.Context,
5534 AL.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005535}
5536
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005537static void handleLayoutVersion(Sema &S, Decl *D, const AttributeList &AL) {
David Majnemercd3ebfe2016-05-23 17:16:12 +00005538 uint32_t Version;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005539 Expr *VersionExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
5540 if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), Version))
David Majnemercd3ebfe2016-05-23 17:16:12 +00005541 return;
5542
5543 // TODO: Investigate what happens with the next major version of MSVC.
5544 if (Version != LangOptions::MSVC2015) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005545 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
5546 << AL.getName() << Version << VersionExpr->getSourceRange();
David Majnemercd3ebfe2016-05-23 17:16:12 +00005547 return;
5548 }
5549
5550 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005551 LayoutVersionAttr(AL.getRange(), S.Context, Version,
5552 AL.getAttributeSpellingListIndex()));
David Majnemercd3ebfe2016-05-23 17:16:12 +00005553}
5554
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005555DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
5556 unsigned AttrSpellingListIndex) {
5557 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00005558 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00005559 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005560 }
5561
5562 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00005563 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005564
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00005565 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005566}
5567
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005568DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
5569 unsigned AttrSpellingListIndex) {
5570 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00005571 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005572 D->dropAttr<DLLImportAttr>();
5573 }
5574
5575 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00005576 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005577
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00005578 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005579}
5580
Hans Wennborge82f19c2014-06-24 23:57:05 +00005581static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00005582 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
5583 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
5584 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored)
5585 << A.getName();
5586 return;
5587 }
5588
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005589 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Hans Wennborg606bd6d2014-11-03 14:24:45 +00005590 if (FD->isInlined() && A.getKind() == AttributeList::AT_DLLImport &&
5591 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
5592 // MinGW doesn't allow dllimport on inline functions.
5593 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
5594 << A.getName();
5595 return;
5596 }
5597 }
5598
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005599 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
Hans Wennborg5869ec42015-09-15 21:05:30 +00005600 if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
5601 MD->getParent()->isLambda()) {
5602 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A.getName();
5603 return;
5604 }
5605 }
5606
Hans Wennborge82f19c2014-06-24 23:57:05 +00005607 unsigned Index = A.getAttributeSpellingListIndex();
5608 Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport
5609 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
5610 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005611 if (NewAttr)
5612 D->addAttr(NewAttr);
5613}
5614
David Majnemer2c4e00a2014-01-29 22:07:36 +00005615MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00005616Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00005617 unsigned AttrSpellingListIndex,
5618 MSInheritanceAttr::Spelling SemanticSpelling) {
5619 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
5620 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00005621 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005622 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
5623 << 1 /*previous declaration*/;
5624 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
5625 D->dropAttr<MSInheritanceAttr>();
5626 }
5627
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005628 auto *RD = cast<CXXRecordDecl>(D);
David Majnemer2c4e00a2014-01-29 22:07:36 +00005629 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00005630 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
5631 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00005632 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005633 }
5634 } else {
5635 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
5636 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
5637 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00005638 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005639 }
5640 if (RD->getDescribedClassTemplate()) {
5641 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
5642 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00005643 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005644 }
5645 }
5646
5647 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00005648 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00005649}
5650
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005651static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005652 // The capability attributes take a single string parameter for the name of
5653 // the capability they represent. The lockable attribute does not take any
5654 // parameters. However, semantically, both attributes represent the same
5655 // concept, and so they use the same semantic attribute. Eventually, the
5656 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00005657 //
Alp Toker958027b2014-07-14 19:42:55 +00005658 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00005659 // literal will be considered a "mutex."
5660 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005661 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005662 if (AL.getKind() == AttributeList::AT_Capability &&
5663 !S.checkStringLiteralArgumentAttr(AL, 0, N, &LiteralLoc))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005664 return;
5665
Aaron Ballman6c810072014-03-05 21:47:13 +00005666 // Currently, there are only two names allowed for a capability: role and
5667 // mutex (case insensitive). Diagnose other capability names.
5668 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
5669 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
5670
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005671 D->addAttr(::new (S.Context) CapabilityAttr(AL.getRange(), S.Context, N,
5672 AL.getAttributeSpellingListIndex()));
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005673}
5674
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005675static void handleAssertCapabilityAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005676 const AttributeList &AL) {
Josh Gaoec1369e2017-08-08 19:44:34 +00005677 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005678 if (!checkLockFunAttrCommon(S, D, AL, Args))
Josh Gaoec1369e2017-08-08 19:44:34 +00005679 return;
5680
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005681 D->addAttr(::new (S.Context) AssertCapabilityAttr(AL.getRange(), S.Context,
Josh Gaoec1369e2017-08-08 19:44:34 +00005682 Args.data(), Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005683 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005684}
5685
5686static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005687 const AttributeList &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005688 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005689 if (!checkLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005690 return;
5691
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005692 D->addAttr(::new (S.Context) AcquireCapabilityAttr(AL.getRange(),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005693 S.Context,
5694 Args.data(), Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005695 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005696}
5697
5698static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005699 const AttributeList &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005700 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005701 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005702 return;
5703
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005704 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(AL.getRange(),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005705 S.Context,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005706 AL.getArgAsExpr(0),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005707 Args.data(),
5708 Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005709 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005710}
5711
5712static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005713 const AttributeList &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005714 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00005715 SmallVector<Expr *, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005716 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005717
Aaron Ballman18d85ae2014-03-20 16:02:49 +00005718 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005719 AL.getRange(), S.Context, Args.data(), Args.size(),
5720 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005721}
5722
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005723static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005724 const AttributeList &AL) {
5725 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005726 return;
5727
5728 // check that all arguments are lockable objects
5729 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005730 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005731 if (Args.empty())
5732 return;
5733
5734 RequiresCapabilityAttr *RCA = ::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005735 RequiresCapabilityAttr(AL.getRange(), S.Context, Args.data(),
5736 Args.size(), AL.getAttributeSpellingListIndex());
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005737
5738 D->addAttr(RCA);
5739}
5740
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005741static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &AL) {
5742 if (const auto *NSD = dyn_cast<NamespaceDecl>(D)) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005743 if (NSD->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005744 S.Diag(AL.getLoc(), diag::warn_deprecated_anonymous_namespace);
Aaron Ballman43f40102014-11-14 22:34:56 +00005745 // Do not want to attach the attribute to the namespace because that will
5746 // cause confusing diagnostic reports for uses of declarations within the
5747 // namespace.
5748 return;
5749 }
5750 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00005751
Manman Renc7890fe2016-03-16 18:50:49 +00005752 // Handle the cases where the attribute has a text message.
5753 StringRef Str, Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005754 if (AL.isArgExpr(0) && AL.getArgAsExpr(0) &&
5755 !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Manman Renc7890fe2016-03-16 18:50:49 +00005756 return;
5757
5758 // Only support a single optional message for Declspec and CXX11.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005759 if (AL.isDeclspecAttribute() || AL.isCXX11Attribute())
5760 checkAttributeAtMostNumArgs(S, AL, 1);
5761 else if (AL.isArgExpr(1) && AL.getArgAsExpr(1) &&
5762 !S.checkStringLiteralArgumentAttr(AL, 1, Replacement))
Manman Renc7890fe2016-03-16 18:50:49 +00005763 return;
5764
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00005765 if (!S.getLangOpts().CPlusPlus14)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005766 if (AL.isCXX11Attribute() &&
5767 !(AL.hasScope() && AL.getScopeName()->isStr("gnu")))
5768 S.Diag(AL.getLoc(), diag::ext_cxx14_attr) << AL.getName();
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00005769
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005770 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005771 DeprecatedAttr(AL.getRange(), S.Context, Str, Replacement,
5772 AL.getAttributeSpellingListIndex()));
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005773}
5774
5775static bool isGlobalVar(const Decl *D) {
5776 if (const auto *S = dyn_cast<VarDecl>(D))
5777 return S->hasGlobalStorage();
5778 return false;
Aaron Ballman43f40102014-11-14 22:34:56 +00005779}
5780
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005781static void handleNoSanitizeAttr(Sema &S, Decl *D, const AttributeList &AL) {
5782 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Peter Collingbourne915df992015-05-15 18:33:32 +00005783 return;
5784
Benjamin Kramer1b582012016-02-13 18:11:49 +00005785 std::vector<StringRef> Sanitizers;
Peter Collingbourne915df992015-05-15 18:33:32 +00005786
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005787 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Peter Collingbourne915df992015-05-15 18:33:32 +00005788 StringRef SanitizerName;
5789 SourceLocation LiteralLoc;
5790
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005791 if (!S.checkStringLiteralArgumentAttr(AL, I, SanitizerName, &LiteralLoc))
Peter Collingbourne915df992015-05-15 18:33:32 +00005792 return;
5793
5794 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0)
5795 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005796 else if (isGlobalVar(D) && SanitizerName != "address")
5797 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005798 << AL.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne915df992015-05-15 18:33:32 +00005799 Sanitizers.push_back(SanitizerName);
5800 }
5801
5802 D->addAttr(::new (S.Context) NoSanitizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005803 AL.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(),
5804 AL.getAttributeSpellingListIndex()));
Peter Collingbourne915df992015-05-15 18:33:32 +00005805}
5806
5807static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005808 const AttributeList &AL) {
5809 StringRef AttrName = AL.getName()->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00005810 normalizeName(AttrName);
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005811 StringRef SanitizerName = llvm::StringSwitch<StringRef>(AttrName)
5812 .Case("no_address_safety_analysis", "address")
5813 .Case("no_sanitize_address", "address")
5814 .Case("no_sanitize_thread", "thread")
5815 .Case("no_sanitize_memory", "memory");
5816 if (isGlobalVar(D) && SanitizerName != "address")
5817 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005818 << AL.getName() << ExpectedFunction;
Peter Collingbourne915df992015-05-15 18:33:32 +00005819 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005820 NoSanitizeAttr(AL.getRange(), S.Context, &SanitizerName, 1,
5821 AL.getAttributeSpellingListIndex()));
Peter Collingbourne915df992015-05-15 18:33:32 +00005822}
5823
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005824static void handleInternalLinkageAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005825 const AttributeList &AL) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005826 if (InternalLinkageAttr *Internal =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005827 S.mergeInternalLinkageAttr(D, AL.getRange(), AL.getName(),
5828 AL.getAttributeSpellingListIndex()))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005829 D->addAttr(Internal);
5830}
5831
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005832static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const AttributeList &AL) {
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005833 if (S.LangOpts.OpenCLVersion != 200)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005834 S.Diag(AL.getLoc(), diag::err_attribute_requires_opencl_version)
5835 << AL.getName() << "2.0" << 0;
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005836 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005837 S.Diag(AL.getLoc(), diag::warn_opencl_attr_deprecated_ignored)
5838 << AL.getName() << "2.0";
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005839}
5840
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005841/// Handles semantic checking for features that are common to all attributes,
5842/// such as checking whether a parameter was properly specified, or the correct
5843/// number of arguments were passed, etc.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005844static bool handleCommonAttributeFeatures(Sema &S, Decl *D,
5845 const AttributeList &AL) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005846 // Several attributes carry different semantics than the parsing requires, so
Alex Lorenz24952fb2017-04-19 15:52:11 +00005847 // those are opted out of the common argument checks.
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005848 //
5849 // We also bail on unknown and ignored attributes because those are handled
5850 // as part of the target-specific handling logic.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005851 if (AL.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005852 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005853 // Check whether the attribute requires specific language extensions to be
5854 // enabled.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005855 if (!AL.diagnoseLangOpts(S))
Aaron Ballman3aff6332013-12-02 19:30:36 +00005856 return true;
Alex Lorenz24952fb2017-04-19 15:52:11 +00005857 // Check whether the attribute appertains to the given subject.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005858 if (!AL.diagnoseAppertainsTo(S, D))
Alex Lorenz24952fb2017-04-19 15:52:11 +00005859 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005860 if (AL.hasCustomParsing())
Alex Lorenz24952fb2017-04-19 15:52:11 +00005861 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005862
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005863 if (AL.getMinArgs() == AL.getMaxArgs()) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005864 // If there are no optional arguments, then checking for the argument count
5865 // is trivial.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005866 if (!checkAttributeNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005867 return true;
5868 } else {
5869 // There are optional arguments, so checking is slightly more involved.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005870 if (AL.getMinArgs() &&
5871 !checkAttributeAtLeastNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005872 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005873 else if (!AL.hasVariadicArg() && AL.getMaxArgs() &&
5874 !checkAttributeAtMostNumArgs(S, AL, AL.getMaxArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005875 return true;
5876 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005877
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005878 return false;
5879}
5880
Xiuli Pan11e13f62016-02-26 03:13:03 +00005881static void handleOpenCLAccessAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005882 const AttributeList &AL) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00005883 if (D->isInvalidDecl())
5884 return;
5885
5886 // Check if there is only one access qualifier.
5887 if (D->hasAttr<OpenCLAccessAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005888 S.Diag(AL.getLoc(), diag::err_opencl_multiple_access_qualifiers)
Xiuli Pan11e13f62016-02-26 03:13:03 +00005889 << D->getSourceRange();
5890 D->setInvalidDecl(true);
5891 return;
5892 }
5893
5894 // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that an
5895 // image object can be read and written.
5896 // OpenCL v2.0 s6.13.6 - A kernel cannot read from and write to the same pipe
5897 // object. Using the read_write (or __read_write) qualifier with the pipe
5898 // qualifier is a compilation error.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005899 if (const auto *PDecl = dyn_cast<ParmVarDecl>(D)) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00005900 const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005901 if (AL.getName()->getName().find("read_write") != StringRef::npos) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00005902 if (S.getLangOpts().OpenCLVersion < 200 || DeclTy->isPipeType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005903 S.Diag(AL.getLoc(), diag::err_opencl_invalid_read_write)
5904 << AL.getName() << PDecl->getType() << DeclTy->isImageType();
Xiuli Pan11e13f62016-02-26 03:13:03 +00005905 D->setInvalidDecl(true);
5906 return;
5907 }
5908 }
5909 }
5910
5911 D->addAttr(::new (S.Context) OpenCLAccessAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005912 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Xiuli Pan11e13f62016-02-26 03:13:03 +00005913}
5914
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005915//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005916// Top Level Sema Entry Points
5917//===----------------------------------------------------------------------===//
5918
Richard Smithf8a75c32013-08-29 00:47:48 +00005919/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
5920/// the attribute applies to decls. If the attribute is a type attribute, just
5921/// silently ignore it if a GNU attribute.
5922static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005923 const AttributeList &AL,
Richard Smithf8a75c32013-08-29 00:47:48 +00005924 bool IncludeCXX11Attributes) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005925 if (AL.isInvalid() || AL.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00005926 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00005927
Richard Smithf8a75c32013-08-29 00:47:48 +00005928 // Ignore C++11 attributes on declarator chunks: they appertain to the type
5929 // instead.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005930 if (AL.isCXX11Attribute() && !IncludeCXX11Attributes)
Richard Smithf8a75c32013-08-29 00:47:48 +00005931 return;
5932
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005933 // Unknown attributes are automatically warned on. Target-specific attributes
5934 // which do not apply to the current target architecture are treated as
5935 // though they were unknown attributes.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005936 if (AL.getKind() == AttributeList::UnknownAttribute ||
5937 !AL.existsInTarget(S.Context.getTargetInfo())) {
5938 S.Diag(AL.getLoc(), AL.isDeclspecAttribute()
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005939 ? diag::warn_unhandled_ms_attribute_ignored
5940 : diag::warn_unknown_attribute_ignored)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005941 << AL.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005942 return;
5943 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005944
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005945 if (handleCommonAttributeFeatures(S, D, AL))
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005946 return;
5947
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005948 switch (AL.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005949 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005950 if (!AL.isStmtAttr()) {
Richard Smith4f902c72016-03-08 00:32:55 +00005951 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005952 assert(AL.isTypeAttr() && "Non-type attribute not handled");
Richard Smith4f902c72016-03-08 00:32:55 +00005953 break;
5954 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005955 S.Diag(AL.getLoc(), diag::err_stmt_attribute_invalid_on_decl)
5956 << AL.getName() << D->getLocation();
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005957 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005958 case AttributeList::AT_Interrupt:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005959 handleInterruptAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005960 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005961 case AttributeList::AT_X86ForceAlignArgPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005962 handleX86ForceAlignArgPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005963 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005964 case AttributeList::AT_DLLExport:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005965 case AttributeList::AT_DLLImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005966 handleDLLAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005967 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005968 case AttributeList::AT_Mips16:
Simon Atanasyan2c87f532017-05-22 12:47:43 +00005969 handleSimpleAttributeWithExclusions<Mips16Attr, MicroMipsAttr,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005970 MipsInterruptAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005971 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005972 case AttributeList::AT_NoMips16:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005973 handleSimpleAttribute<NoMips16Attr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005974 break;
Simon Atanasyan2c87f532017-05-22 12:47:43 +00005975 case AttributeList::AT_MicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005976 handleSimpleAttributeWithExclusions<MicroMipsAttr, Mips16Attr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00005977 break;
5978 case AttributeList::AT_NoMicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005979 handleSimpleAttribute<NoMicroMipsAttr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00005980 break;
Simon Atanasyan1a116db2017-07-20 20:34:18 +00005981 case AttributeList::AT_MipsLongCall:
5982 handleSimpleAttributeWithExclusions<MipsLongCallAttr, MipsShortCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005983 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00005984 break;
5985 case AttributeList::AT_MipsShortCall:
5986 handleSimpleAttributeWithExclusions<MipsShortCallAttr, MipsLongCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005987 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00005988 break;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005989 case AttributeList::AT_AMDGPUFlatWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005990 handleAMDGPUFlatWorkGroupSizeAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005991 break;
5992 case AttributeList::AT_AMDGPUWavesPerEU:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005993 handleAMDGPUWavesPerEUAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005994 break;
5995 case AttributeList::AT_AMDGPUNumSGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005996 handleAMDGPUNumSGPRAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005997 break;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005998 case AttributeList::AT_AMDGPUNumVGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005999 handleAMDGPUNumVGPRAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006000 break;
Dylan McKaye8232d72017-02-08 05:09:26 +00006001 case AttributeList::AT_AVRSignal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006002 handleAVRSignalAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00006003 break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00006004 case AttributeList::AT_IBAction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006005 handleSimpleAttribute<IBActionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006006 break;
6007 case AttributeList::AT_IBOutlet:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006008 handleIBOutlet(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006009 break;
Richard Smith10876ef2013-01-17 01:30:42 +00006010 case AttributeList::AT_IBOutletCollection:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006011 handleIBOutletCollection(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006012 break;
Dmitry Polukhin85eda122016-04-11 07:48:59 +00006013 case AttributeList::AT_IFunc:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006014 handleIFuncAttr(S, D, AL);
Dmitry Polukhin85eda122016-04-11 07:48:59 +00006015 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006016 case AttributeList::AT_Alias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006017 handleAliasAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006018 break;
6019 case AttributeList::AT_Aligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006020 handleAlignedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006021 break;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00006022 case AttributeList::AT_AlignValue:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006023 handleAlignValueAttr(S, D, AL);
Hal Finkel1b0d24e2014-10-02 21:21:25 +00006024 break;
George Burgess IVe3763372016-12-22 02:50:20 +00006025 case AttributeList::AT_AllocSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006026 handleAllocSizeAttr(S, D, AL);
George Burgess IVe3763372016-12-22 02:50:20 +00006027 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006028 case AttributeList::AT_AlwaysInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006029 handleAlwaysInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006030 break;
Erich Keane293a0552018-02-14 00:14:07 +00006031 case AttributeList::AT_Artificial:
Aaron Ballman736c09b2018-02-15 16:28:10 +00006032 handleSimpleAttribute<ArtificialAttr>(S, D, AL);
Erich Keane293a0552018-02-14 00:14:07 +00006033 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006034 case AttributeList::AT_AnalyzerNoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006035 handleAnalyzerNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006036 break;
6037 case AttributeList::AT_TLSModel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006038 handleTLSModelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006039 break;
6040 case AttributeList::AT_Annotate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006041 handleAnnotateAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006042 break;
6043 case AttributeList::AT_Availability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006044 handleAvailabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006045 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006046 case AttributeList::AT_CarriesDependency:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006047 handleDependencyAttr(S, scope, D, AL);
Richard Smithe233fbf2013-01-28 22:42:45 +00006048 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006049 case AttributeList::AT_Common:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006050 handleCommonAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006051 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00006052 case AttributeList::AT_CUDAConstant:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006053 handleConstantAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006054 break;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00006055 case AttributeList::AT_PassObjectSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006056 handlePassObjectSizeAttr(S, D, AL);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00006057 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006058 case AttributeList::AT_Constructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006059 handleConstructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006060 break;
Richard Smith10876ef2013-01-17 01:30:42 +00006061 case AttributeList::AT_CXX11NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006062 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006063 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006064 case AttributeList::AT_Deprecated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006065 handleDeprecatedAttr(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006066 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006067 case AttributeList::AT_Destructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006068 handleDestructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006069 break;
6070 case AttributeList::AT_EnableIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006071 handleEnableIfAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006072 break;
George Burgess IV177399e2017-01-09 04:12:14 +00006073 case AttributeList::AT_DiagnoseIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006074 handleDiagnoseIfAttr(S, D, AL);
George Burgess IV177399e2017-01-09 04:12:14 +00006075 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006076 case AttributeList::AT_ExtVectorType:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006077 handleExtVectorTypeAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006078 break;
Alex Lorenzd5d27e12017-03-01 18:06:25 +00006079 case AttributeList::AT_ExternalSourceSymbol:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006080 handleExternalSourceSymbolAttr(S, D, AL);
Alex Lorenzd5d27e12017-03-01 18:06:25 +00006081 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00006082 case AttributeList::AT_MinSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006083 handleMinSizeAttr(S, D, AL);
Quentin Colombet4e172062012-11-01 23:55:47 +00006084 break;
Paul Robinsonf0674352014-03-31 22:29:15 +00006085 case AttributeList::AT_OptimizeNone:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006086 handleOptimizeNoneAttr(S, D, AL);
Paul Robinsonf0674352014-03-31 22:29:15 +00006087 break;
Alexis Hunt724f14e2014-11-28 00:53:20 +00006088 case AttributeList::AT_FlagEnum:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006089 handleSimpleAttribute<FlagEnumAttr>(S, D, AL);
Alexis Hunt724f14e2014-11-28 00:53:20 +00006090 break;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00006091 case AttributeList::AT_EnumExtensibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006092 handleEnumExtensibilityAttr(S, D, AL);
Akira Hatanaka3c268af2017-03-21 02:23:00 +00006093 break;
Peter Collingbourne41af7c22014-05-20 17:12:51 +00006094 case AttributeList::AT_Flatten:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006095 handleSimpleAttribute<FlattenAttr>(S, D, AL);
Peter Collingbourne41af7c22014-05-20 17:12:51 +00006096 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006097 case AttributeList::AT_Format:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006098 handleFormatAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006099 break;
6100 case AttributeList::AT_FormatArg:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006101 handleFormatArgAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006102 break;
6103 case AttributeList::AT_CUDAGlobal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006104 handleGlobalAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006105 break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00006106 case AttributeList::AT_CUDADevice:
Justin Lebar3eaaf862016-01-13 01:07:35 +00006107 handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006108 AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006109 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00006110 case AttributeList::AT_CUDAHost:
Justin Lebar3eaaf862016-01-13 01:07:35 +00006111 handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006112 AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006113 break;
6114 case AttributeList::AT_GNUInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006115 handleGNUInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006116 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006117 case AttributeList::AT_CUDALaunchBounds:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006118 handleLaunchBoundsAttr(S, D, AL);
Peter Collingbourne827301e2010-12-12 23:03:07 +00006119 break;
David Majnemer631a90b2015-02-04 07:23:21 +00006120 case AttributeList::AT_Restrict:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006121 handleRestrictAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006122 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00006123 case AttributeList::AT_MayAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006124 handleSimpleAttribute<MayAliasAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006125 break;
6126 case AttributeList::AT_Mode:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006127 handleModeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006128 break;
David Majnemer1bf0f8e2015-07-20 22:51:52 +00006129 case AttributeList::AT_NoAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006130 handleSimpleAttribute<NoAliasAttr>(S, D, AL);
David Majnemer1bf0f8e2015-07-20 22:51:52 +00006131 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00006132 case AttributeList::AT_NoCommon:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006133 handleSimpleAttribute<NoCommonAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006134 break;
Peter Collingbourneb4728c12014-05-19 22:14:34 +00006135 case AttributeList::AT_NoSplitStack:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006136 handleSimpleAttribute<NoSplitStackAttr>(S, D, AL);
Peter Collingbourneb4728c12014-05-19 22:14:34 +00006137 break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00006138 case AttributeList::AT_NonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006139 if (auto *PVD = dyn_cast<ParmVarDecl>(D))
6140 handleNonNullAttrParameter(S, PVD, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006141 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006142 handleNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006143 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00006144 case AttributeList::AT_ReturnsNonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006145 handleReturnsNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006146 break;
Akira Hatanaka98a49332017-09-22 00:41:05 +00006147 case AttributeList::AT_NoEscape:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006148 handleNoEscapeAttr(S, D, AL);
Akira Hatanaka98a49332017-09-22 00:41:05 +00006149 break;
Hal Finkelee90a222014-09-26 05:04:30 +00006150 case AttributeList::AT_AssumeAligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006151 handleAssumeAlignedAttr(S, D, AL);
Hal Finkelee90a222014-09-26 05:04:30 +00006152 break;
Erich Keane623efd82017-03-30 21:48:55 +00006153 case AttributeList::AT_AllocAlign:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006154 handleAllocAlignAttr(S, D, AL);
Erich Keane623efd82017-03-30 21:48:55 +00006155 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00006156 case AttributeList::AT_Overloadable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006157 handleSimpleAttribute<OverloadableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006158 break;
6159 case AttributeList::AT_Ownership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006160 handleOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006161 break;
6162 case AttributeList::AT_Cold:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006163 handleColdAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006164 break;
6165 case AttributeList::AT_Hot:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006166 handleHotAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006167 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00006168 case AttributeList::AT_Naked:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006169 handleNakedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006170 break;
6171 case AttributeList::AT_NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006172 handleNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006173 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00006174 case AttributeList::AT_NoThrow:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006175 handleSimpleAttribute<NoThrowAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006176 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00006177 case AttributeList::AT_CUDAShared:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006178 handleSharedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006179 break;
6180 case AttributeList::AT_VecReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006181 handleVecReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006182 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006183 case AttributeList::AT_ObjCOwnership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006184 handleObjCOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006185 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006186 case AttributeList::AT_ObjCPreciseLifetime:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006187 handleObjCPreciseLifetimeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006188 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006189 case AttributeList::AT_ObjCReturnsInnerPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006190 handleObjCReturnsInnerPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006191 break;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00006192 case AttributeList::AT_ObjCRequiresSuper:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006193 handleObjCRequiresSuperAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006194 break;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00006195 case AttributeList::AT_ObjCBridge:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006196 handleObjCBridgeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006197 break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00006198 case AttributeList::AT_ObjCBridgeMutable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006199 handleObjCBridgeMutableAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006200 break;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00006201 case AttributeList::AT_ObjCBridgeRelated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006202 handleObjCBridgeRelatedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006203 break;
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00006204 case AttributeList::AT_ObjCDesignatedInitializer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006205 handleObjCDesignatedInitializer(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006206 break;
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00006207 case AttributeList::AT_ObjCRuntimeName:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006208 handleObjCRuntimeName(S, D, AL);
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00006209 break;
Douglas Gregor24ae22c2016-04-01 23:23:52 +00006210 case AttributeList::AT_ObjCRuntimeVisible:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006211 handleSimpleAttribute<ObjCRuntimeVisibleAttr>(S, D, AL);
Douglas Gregor24ae22c2016-04-01 23:23:52 +00006212 break;
Alex Denisovfde64952015-06-26 05:28:36 +00006213 case AttributeList::AT_ObjCBoxable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006214 handleObjCBoxable(S, D, AL);
Alex Denisovfde64952015-06-26 05:28:36 +00006215 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006216 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006217 handleCFAuditedTransferAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006218 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006219 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006220 handleCFUnknownTransferAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006221 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006222 case AttributeList::AT_CFConsumed:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006223 case AttributeList::AT_NSConsumed:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006224 handleNSConsumedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006225 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006226 case AttributeList::AT_NSConsumesSelf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006227 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006228 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006229 case AttributeList::AT_NSReturnsAutoreleased:
6230 case AttributeList::AT_NSReturnsNotRetained:
6231 case AttributeList::AT_CFReturnsNotRetained:
6232 case AttributeList::AT_NSReturnsRetained:
6233 case AttributeList::AT_CFReturnsRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006234 handleNSReturnsRetainedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006235 break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00006236 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006237 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006238 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006239 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006240 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006241 break;
Xiuli Panbe6da4b2017-05-04 07:31:20 +00006242 case AttributeList::AT_OpenCLIntelReqdSubGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006243 handleSubGroupSize(S, D, AL);
Xiuli Panbe6da4b2017-05-04 07:31:20 +00006244 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00006245 case AttributeList::AT_VecTypeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006246 handleVecTypeHint(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006247 break;
Eric Fiselier341e8252016-09-02 18:53:31 +00006248 case AttributeList::AT_RequireConstantInit:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006249 handleSimpleAttribute<RequireConstantInitAttr>(S, D, AL);
Eric Fiselier341e8252016-09-02 18:53:31 +00006250 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006251 case AttributeList::AT_InitPriority:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006252 handleInitPriorityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006253 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006254 case AttributeList::AT_Packed:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006255 handlePackedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006256 break;
6257 case AttributeList::AT_Section:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006258 handleSectionAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006259 break;
Eric Christopher11acf732015-06-12 01:35:52 +00006260 case AttributeList::AT_Target:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006261 handleTargetAttr(S, D, AL);
Eric Christopher11acf732015-06-12 01:35:52 +00006262 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006263 case AttributeList::AT_Unavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006264 handleAttrWithMessage<UnavailableAttr>(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006265 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006266 case AttributeList::AT_ArcWeakrefUnavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006267 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006268 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006269 case AttributeList::AT_ObjCRootClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006270 handleSimpleAttribute<ObjCRootClassAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006271 break;
Alex Lorenza8c44ba2016-10-28 10:25:10 +00006272 case AttributeList::AT_ObjCSubclassingRestricted:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006273 handleSimpleAttribute<ObjCSubclassingRestrictedAttr>(S, D, AL);
Alex Lorenza8c44ba2016-10-28 10:25:10 +00006274 break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00006275 case AttributeList::AT_ObjCExplicitProtocolImpl:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006276 handleObjCSuppresProtocolAttr(S, D, AL);
Ted Kremenek28eace62013-11-23 01:01:34 +00006277 break;
6278 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006279 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006280 break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00006281 case AttributeList::AT_Unused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006282 handleUnusedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006283 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006284 case AttributeList::AT_ReturnsTwice:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006285 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006286 break;
Akira Hatanakac8667622015-11-06 23:56:15 +00006287 case AttributeList::AT_NotTailCalled:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006288 handleNotTailCalledAttr(S, D, AL);
Akira Hatanakac8667622015-11-06 23:56:15 +00006289 break;
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00006290 case AttributeList::AT_DisableTailCalls:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006291 handleDisableTailCallsAttr(S, D, AL);
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00006292 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006293 case AttributeList::AT_Used:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006294 handleUsedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006295 break;
John McCalld041a9b2013-02-20 01:54:26 +00006296 case AttributeList::AT_Visibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006297 handleVisibilityAttr(S, D, AL, false);
John McCalld041a9b2013-02-20 01:54:26 +00006298 break;
6299 case AttributeList::AT_TypeVisibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006300 handleVisibilityAttr(S, D, AL, true);
John McCalld041a9b2013-02-20 01:54:26 +00006301 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00006302 case AttributeList::AT_WarnUnused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006303 handleSimpleAttribute<WarnUnusedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006304 break;
6305 case AttributeList::AT_WarnUnusedResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006306 handleWarnUnusedResult(S, D, AL);
Chris Lattner237f2752009-02-14 07:37:35 +00006307 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00006308 case AttributeList::AT_Weak:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006309 handleSimpleAttribute<WeakAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006310 break;
6311 case AttributeList::AT_WeakRef:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006312 handleWeakRefAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006313 break;
6314 case AttributeList::AT_WeakImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006315 handleWeakImportAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006316 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006317 case AttributeList::AT_TransparentUnion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006318 handleTransparentUnionAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006319 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006320 case AttributeList::AT_ObjCException:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006321 handleSimpleAttribute<ObjCExceptionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006322 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006323 case AttributeList::AT_ObjCMethodFamily:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006324 handleObjCMethodFamilyAttr(S, D, AL);
John McCall86bc21f2011-03-02 11:33:24 +00006325 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006326 case AttributeList::AT_ObjCNSObject:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006327 handleObjCNSObject(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006328 break;
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00006329 case AttributeList::AT_ObjCIndependentClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006330 handleObjCIndependentClass(S, D, AL);
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00006331 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006332 case AttributeList::AT_Blocks:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006333 handleBlocksAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006334 break;
6335 case AttributeList::AT_Sentinel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006336 handleSentinelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006337 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00006338 case AttributeList::AT_Const:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006339 handleSimpleAttribute<ConstAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006340 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00006341 case AttributeList::AT_Pure:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006342 handleSimpleAttribute<PureAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006343 break;
6344 case AttributeList::AT_Cleanup:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006345 handleCleanupAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006346 break;
6347 case AttributeList::AT_NoDebug:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006348 handleNoDebugAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006349 break;
Aaron Ballman7c19ab12014-02-22 16:59:24 +00006350 case AttributeList::AT_NoDuplicate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006351 handleSimpleAttribute<NoDuplicateAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006352 break;
Yaxun Liu7d07ae72016-11-01 18:45:32 +00006353 case AttributeList::AT_Convergent:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006354 handleSimpleAttribute<ConvergentAttr>(S, D, AL);
Yaxun Liu7d07ae72016-11-01 18:45:32 +00006355 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00006356 case AttributeList::AT_NoInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006357 handleSimpleAttribute<NoInlineAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006358 break;
6359 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006360 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006361 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006362 case AttributeList::AT_StdCall:
6363 case AttributeList::AT_CDecl:
6364 case AttributeList::AT_FastCall:
6365 case AttributeList::AT_ThisCall:
6366 case AttributeList::AT_Pascal:
Erich Keane757d3172016-11-02 18:29:35 +00006367 case AttributeList::AT_RegCall:
John McCall477f2bb2016-03-03 06:39:32 +00006368 case AttributeList::AT_SwiftCall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00006369 case AttributeList::AT_VectorCall:
Charles Davisb5a214e2013-08-30 04:39:01 +00006370 case AttributeList::AT_MSABI:
6371 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006372 case AttributeList::AT_Pcs:
Guy Benyeif0a014b2012-12-25 08:53:55 +00006373 case AttributeList::AT_IntelOclBicc:
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00006374 case AttributeList::AT_PreserveMost:
6375 case AttributeList::AT_PreserveAll:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006376 handleCallConvAttr(S, D, AL);
John McCallab26cfa2010-02-05 21:31:56 +00006377 break;
Matthias Gehre01a63382017-03-27 19:45:24 +00006378 case AttributeList::AT_Suppress:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006379 handleSuppressAttr(S, D, AL);
Matthias Gehre01a63382017-03-27 19:45:24 +00006380 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006381 case AttributeList::AT_OpenCLKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006382 handleSimpleAttribute<OpenCLKernelAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006383 break;
Xiuli Pan11e13f62016-02-26 03:13:03 +00006384 case AttributeList::AT_OpenCLAccess:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006385 handleOpenCLAccessAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006386 break;
Anastasia Stulovafde76222016-04-01 16:05:09 +00006387 case AttributeList::AT_OpenCLNoSVM:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006388 handleOpenCLNoSVMAttr(S, D, AL);
Anastasia Stulovafde76222016-04-01 16:05:09 +00006389 break;
John McCall477f2bb2016-03-03 06:39:32 +00006390 case AttributeList::AT_SwiftContext:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006391 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftContext);
John McCall477f2bb2016-03-03 06:39:32 +00006392 break;
6393 case AttributeList::AT_SwiftErrorResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006394 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftErrorResult);
John McCall477f2bb2016-03-03 06:39:32 +00006395 break;
6396 case AttributeList::AT_SwiftIndirectResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006397 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftIndirectResult);
John McCall477f2bb2016-03-03 06:39:32 +00006398 break;
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00006399 case AttributeList::AT_InternalLinkage:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006400 handleInternalLinkageAttr(S, D, AL);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00006401 break;
Peter Collingbourne3afb2662016-04-28 17:09:37 +00006402 case AttributeList::AT_LTOVisibilityPublic:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006403 handleSimpleAttribute<LTOVisibilityPublicAttr>(S, D, AL);
Peter Collingbourne3afb2662016-04-28 17:09:37 +00006404 break;
John McCall8d32c052012-05-22 21:28:12 +00006405
6406 // Microsoft attributes:
David Majnemercd3ebfe2016-05-23 17:16:12 +00006407 case AttributeList::AT_EmptyBases:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006408 handleSimpleAttribute<EmptyBasesAttr>(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00006409 break;
6410 case AttributeList::AT_LayoutVersion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006411 handleLayoutVersion(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00006412 break;
Akira Hatanaka02914dc2018-02-05 20:23:22 +00006413 case AttributeList::AT_TrivialABI:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006414 handleSimpleAttribute<TrivialABIAttr>(S, D, AL);
Akira Hatanaka02914dc2018-02-05 20:23:22 +00006415 break;
David Majnemer8ab003a2015-02-02 19:30:52 +00006416 case AttributeList::AT_MSNoVTable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006417 handleSimpleAttribute<MSNoVTableAttr>(S, D, AL);
David Majnemer129f4172015-02-02 10:22:20 +00006418 break;
David Majnemer8ab003a2015-02-02 19:30:52 +00006419 case AttributeList::AT_MSStruct:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006420 handleSimpleAttribute<MSStructAttr>(S, D, AL);
John McCall8d32c052012-05-22 21:28:12 +00006421 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006422 case AttributeList::AT_Uuid:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006423 handleUuidAttr(S, D, AL);
Francois Picheta83957a2010-12-19 06:50:37 +00006424 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00006425 case AttributeList::AT_MSInheritance:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006426 handleMSInheritanceAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006427 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00006428 case AttributeList::AT_SelectAny:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006429 handleSimpleAttribute<SelectAnyAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006430 break;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00006431 case AttributeList::AT_Thread:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006432 handleDeclspecThreadAttr(S, D, AL);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00006433 break;
David Majnemercd3ebfe2016-05-23 17:16:12 +00006434
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00006435 case AttributeList::AT_AbiTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006436 handleAbiTagAttr(S, D, AL);
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00006437 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006438
6439 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00006440 case AttributeList::AT_AssertExclusiveLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006441 handleAssertExclusiveLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00006442 break;
6443 case AttributeList::AT_AssertSharedLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006444 handleAssertSharedLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00006445 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006446 case AttributeList::AT_GuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006447 handleSimpleAttribute<GuardedVarAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006448 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006449 case AttributeList::AT_PtGuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006450 handlePtGuardedVarAttr(S, D, AL);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006451 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006452 case AttributeList::AT_ScopedLockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006453 handleSimpleAttribute<ScopedLockableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006454 break;
Peter Collingbourne915df992015-05-15 18:33:32 +00006455 case AttributeList::AT_NoSanitize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006456 handleNoSanitizeAttr(S, D, AL);
Peter Collingbourne915df992015-05-15 18:33:32 +00006457 break;
6458 case AttributeList::AT_NoSanitizeSpecific:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006459 handleNoSanitizeSpecificAttr(S, D, AL);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00006460 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006461 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006462 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, AL);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00006463 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006464 case AttributeList::AT_GuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006465 handleGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006466 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006467 case AttributeList::AT_PtGuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006468 handlePtGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006469 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006470 case AttributeList::AT_ExclusiveTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006471 handleExclusiveTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006472 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006473 case AttributeList::AT_LockReturned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006474 handleLockReturnedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006475 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006476 case AttributeList::AT_LocksExcluded:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006477 handleLocksExcludedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006478 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006479 case AttributeList::AT_SharedTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006480 handleSharedTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006481 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006482 case AttributeList::AT_AcquiredBefore:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006483 handleAcquiredBeforeAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006484 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006485 case AttributeList::AT_AcquiredAfter:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006486 handleAcquiredAfterAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006487 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006488
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006489 // Capability analysis attributes.
6490 case AttributeList::AT_Capability:
6491 case AttributeList::AT_Lockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006492 handleCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006493 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006494 case AttributeList::AT_RequiresCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006495 handleRequiresCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006496 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006497
6498 case AttributeList::AT_AssertCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006499 handleAssertCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006500 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006501 case AttributeList::AT_AcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006502 handleAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006503 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006504 case AttributeList::AT_ReleaseCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006505 handleReleaseCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006506 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006507 case AttributeList::AT_TryAcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006508 handleTryAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006509 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006510
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00006511 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00006512 case AttributeList::AT_Consumable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006513 handleConsumableAttr(S, D, AL);
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00006514 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00006515 case AttributeList::AT_ConsumableAutoCast:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006516 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006517 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00006518 case AttributeList::AT_ConsumableSetOnRead:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006519 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006520 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00006521 case AttributeList::AT_CallableWhen:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006522 handleCallableWhenAttr(S, D, AL);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00006523 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00006524 case AttributeList::AT_ParamTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006525 handleParamTypestateAttr(S, D, AL);
DeLesley Hutchins69391772013-10-17 23:23:53 +00006526 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00006527 case AttributeList::AT_ReturnTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006528 handleReturnTypestateAttr(S, D, AL);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00006529 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00006530 case AttributeList::AT_SetTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006531 handleSetTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00006532 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00006533 case AttributeList::AT_TestTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006534 handleTestTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00006535 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00006536
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006537 // Type safety attributes.
6538 case AttributeList::AT_ArgumentWithTypeTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006539 handleArgumentWithTypeTagAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006540 break;
6541 case AttributeList::AT_TypeTagForDatatype:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006542 handleTypeTagForDatatypeAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006543 break;
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00006544 case AttributeList::AT_AnyX86NoCallerSavedRegisters:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006545 handleNoCallerSavedRegsAttr(S, D, AL);
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00006546 break;
Pirama Arumuga Nainare5d2d712016-06-10 21:51:18 +00006547 case AttributeList::AT_RenderScriptKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006548 handleSimpleAttribute<RenderScriptKernelAttr>(S, D, AL);
Pirama Arumuga Nainar8b788d02016-06-09 23:34:20 +00006549 break;
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00006550 // XRay attributes.
6551 case AttributeList::AT_XRayInstrument:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006552 handleSimpleAttribute<XRayInstrumentAttr>(S, D, AL);
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00006553 break;
Dean Michael Berris418da3f2017-03-06 07:08:21 +00006554 case AttributeList::AT_XRayLogArgs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006555 handleXRayLogArgsAttr(S, D, AL);
Dean Michael Berris418da3f2017-03-06 07:08:21 +00006556 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006557 }
6558}
6559
6560/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
6561/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00006562void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00006563 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00006564 bool IncludeCXX11Attributes) {
6565 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00006566 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00006567
Joey Gouly2cd9db12013-12-13 16:15:28 +00006568 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00006569 // GCC accepts
6570 // static int a9 __attribute__((weakref));
6571 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00006572 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00006573 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
6574 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00006575 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00006576 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006577 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00006578
Aaron Ballmanbe243a72014-12-04 22:45:31 +00006579 // FIXME: We should be able to handle this in TableGen as well. It would be
6580 // good to have a way to specify "these attributes must appear as a group",
6581 // for these. Additionally, it would be good to have a way to specify "these
6582 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00006583 if (!D->hasAttr<OpenCLKernelAttr>()) {
6584 // These attributes cannot be applied to a non-kernel function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006585 if (const auto *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00006586 // FIXME: This emits a different error message than
6587 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00006588 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006589 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006590 } else if (const auto *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00006591 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006592 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006593 } else if (const auto *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00006594 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006595 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006596 } else if (const auto *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006597 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6598 << A << ExpectedKernelFunction;
6599 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006600 } else if (const auto *A = D->getAttr<AMDGPUWavesPerEUAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00006601 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6602 << A << ExpectedKernelFunction;
6603 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006604 } else if (const auto *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00006605 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6606 << A << ExpectedKernelFunction;
6607 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006608 } else if (const auto *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006609 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6610 << A << ExpectedKernelFunction;
6611 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006612 } else if (const auto *A = D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00006613 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
6614 D->setInvalidDecl();
Joey Gouly2cd9db12013-12-13 16:15:28 +00006615 }
6616 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006617}
6618
Hiroshi Inoue939d9322017-06-30 05:40:31 +00006619// Helper for delayed processing TransparentUnion attribute.
Erich Keane2fe684b2017-02-28 20:44:39 +00006620void Sema::ProcessDeclAttributeDelayed(Decl *D, const AttributeList *AttrList) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006621 for (const AttributeList *AL = AttrList; AL; AL = AL->getNext())
6622 if (AL->getKind() == AttributeList::AT_TransparentUnion) {
6623 handleTransparentUnionAttr(*this, D, *AL);
Erich Keane2fe684b2017-02-28 20:44:39 +00006624 break;
6625 }
6626}
6627
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006628// Annotation attributes are the only attributes allowed after an access
6629// specifier.
6630bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
6631 const AttributeList *AttrList) {
6632 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006633 if (l->getKind() == AttributeList::AT_Annotate) {
David Majnemer706f3152014-12-14 01:05:01 +00006634 ProcessDeclAttribute(*this, nullptr, ASDecl, *l, l->isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006635 } else {
6636 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
6637 return true;
6638 }
6639 }
6640
6641 return false;
6642}
6643
John McCall42856de2011-10-01 05:17:03 +00006644/// checkUnusedDeclAttributes - Check a list of attributes to see if it
6645/// contains any decl attributes that we should warn about.
6646static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
6647 for ( ; A; A = A->getNext()) {
6648 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00006649 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00006650 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
6651
6652 if (A->getKind() == AttributeList::UnknownAttribute) {
6653 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
6654 << A->getName() << A->getRange();
6655 } else {
6656 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
6657 << A->getName() << A->getRange();
6658 }
6659 }
6660}
6661
6662/// checkUnusedDeclAttributes - Given a declarator which is not being
6663/// used to build a declaration, complain about any decl attributes
6664/// which might be lying around on it.
6665void Sema::checkUnusedDeclAttributes(Declarator &D) {
6666 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
6667 ::checkUnusedDeclAttributes(*this, D.getAttributes());
6668 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
6669 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
6670}
6671
Ryan Flynn7d470f32009-07-30 03:15:39 +00006672/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00006673/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00006674NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
6675 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00006676 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00006677 NamedDecl *NewD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006678 if (auto *FD = dyn_cast<FunctionDecl>(ND)) {
Alexander Kornienko061900f2015-12-03 11:37:28 +00006679 FunctionDecl *NewFD;
6680 // FIXME: Missing call to CheckFunctionDeclaration().
Eli Friedmance3e2c82011-09-07 04:05:06 +00006681 // FIXME: Mangling?
6682 // FIXME: Is the qualifier info correct?
6683 // FIXME: Is the DeclContext correct?
Alexander Kornienko061900f2015-12-03 11:37:28 +00006684 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
6685 Loc, Loc, DeclarationName(II),
6686 FD->getType(), FD->getTypeSourceInfo(),
6687 SC_None, false/*isInlineSpecified*/,
6688 FD->hasPrototype(),
6689 false/*isConstexprSpecified*/);
Eli Friedmance3e2c82011-09-07 04:05:06 +00006690 NewD = NewFD;
6691
6692 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00006693 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00006694
6695 // Fake up parameter variables; they are declared as if this were
6696 // a typedef.
6697 QualType FDTy = FD->getType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006698 if (const auto *FT = FDTy->getAs<FunctionProtoType>()) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00006699 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00006700 for (const auto &AI : FT->param_types()) {
6701 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00006702 Param->setScopeInfo(0, Params.size());
6703 Params.push_back(Param);
6704 }
David Blaikie9c70e042011-09-21 18:16:56 +00006705 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00006706 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006707 } else if (auto *VD = dyn_cast<VarDecl>(ND)) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00006708 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00006709 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00006710 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00006711 VD->getStorageClass());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006712 if (VD->getQualifier())
6713 cast<VarDecl>(NewD)->setQualifierInfo(VD->getQualifierLoc());
Ryan Flynn7d470f32009-07-30 03:15:39 +00006714 }
6715 return NewD;
6716}
6717
James Dennett634962f2012-06-14 21:40:34 +00006718/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00006719/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00006720void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00006721 if (W.getUsed()) return; // only do this once
6722 W.setUsed(true);
6723 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
6724 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00006725 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00006726 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
6727 W.getLocation()));
6728 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00006729 WeakTopLevelDecl.push_back(NewD);
6730 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
6731 // to insert Decl at TU scope, sorry.
6732 DeclContext *SavedContext = CurContext;
6733 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00006734 NewD->setDeclContext(CurContext);
6735 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00006736 PushOnScopeChains(NewD, S);
6737 CurContext = SavedContext;
6738 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00006739 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00006740 }
6741}
6742
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006743void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
6744 // It's valid to "forward-declare" #pragma weak, in which case we
6745 // have to do this.
6746 LoadExternalWeakUndeclaredIdentifiers();
6747 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00006748 NamedDecl *ND = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006749 if (auto *VD = dyn_cast<VarDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006750 if (VD->isExternC())
6751 ND = VD;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006752 if (auto *FD = dyn_cast<FunctionDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006753 if (FD->isExternC())
6754 ND = FD;
6755 if (ND) {
6756 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00006757 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006758 if (I != WeakUndeclaredIdentifiers.end()) {
6759 WeakInfo W = I->second;
6760 DeclApplyPragmaWeak(S, ND, W);
6761 WeakUndeclaredIdentifiers[Id] = W;
6762 }
6763 }
6764 }
6765 }
6766}
6767
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006768/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
6769/// it, apply them to D. This is a bit tricky because PD can have attributes
6770/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00006771void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006772 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00006773 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00006774 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00006775
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006776 // Walk the declarator structure, applying decl attributes that were in a type
6777 // position to the decl itself. This handles cases like:
6778 // int *__attr__(x)** D;
6779 // when X is a decl attribute.
6780 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
6781 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00006782 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00006783
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006784 // Finally, apply any attributes on the decl itself.
6785 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00006786 ProcessDeclAttributeList(S, D, Attrs);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00006787
6788 // Apply additional attributes specified by '#pragma clang attribute'.
6789 AddPragmaAttributes(S, D);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006790}
John McCall28a6aea2009-11-04 02:18:39 +00006791
John McCall31168b02011-06-15 23:02:42 +00006792/// Is the given declaration allowed to use a forbidden type?
John McCallb61e14e2015-10-27 04:54:50 +00006793/// If so, it'll still be annotated with an attribute that makes it
6794/// illegal to actually use.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006795static bool isForbiddenTypeAllowed(Sema &S, Decl *D,
John McCallb61e14e2015-10-27 04:54:50 +00006796 const DelayedDiagnostic &diag,
John McCallc6af8c62015-10-28 05:03:19 +00006797 UnavailableAttr::ImplicitReason &reason) {
John McCall31168b02011-06-15 23:02:42 +00006798 // Private ivars are always okay. Unfortunately, people don't
6799 // always properly make their ivars private, even in system headers.
6800 // Plus we need to make fields okay, too.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006801 if (!isa<FieldDecl>(D) && !isa<ObjCPropertyDecl>(D) &&
6802 !isa<FunctionDecl>(D))
John McCall31168b02011-06-15 23:02:42 +00006803 return false;
6804
John McCallc6af8c62015-10-28 05:03:19 +00006805 // Silently accept unsupported uses of __weak in both user and system
6806 // declarations when it's been disabled, for ease of integration with
6807 // -fno-objc-arc files. We do have to take some care against attempts
6808 // to define such things; for now, we've only done that for ivars
6809 // and properties.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006810 if ((isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) {
John McCallc6af8c62015-10-28 05:03:19 +00006811 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
6812 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
6813 reason = UnavailableAttr::IR_ForbiddenWeak;
6814 return true;
6815 }
John McCallb61e14e2015-10-27 04:54:50 +00006816 }
6817
John McCallc6af8c62015-10-28 05:03:19 +00006818 // Allow all sorts of things in system headers.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006819 if (S.Context.getSourceManager().isInSystemHeader(D->getLocation())) {
John McCallc6af8c62015-10-28 05:03:19 +00006820 // Currently, all the failures dealt with this way are due to ARC
6821 // restrictions.
6822 reason = UnavailableAttr::IR_ARCForbiddenType;
6823 return true;
John McCallb61e14e2015-10-27 04:54:50 +00006824 }
6825
6826 return false;
John McCall31168b02011-06-15 23:02:42 +00006827}
6828
6829/// Handle a delayed forbidden-type diagnostic.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006830static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &DD,
6831 Decl *D) {
6832 auto Reason = UnavailableAttr::IR_None;
6833 if (D && isForbiddenTypeAllowed(S, D, DD, Reason)) {
6834 assert(Reason && "didn't set reason?");
6835 D->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", Reason, DD.Loc));
John McCall31168b02011-06-15 23:02:42 +00006836 return;
6837 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00006838 if (S.getLangOpts().ObjCAutoRefCount)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006839 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00006840 // FIXME: we may want to suppress diagnostics for all
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006841 // kind of forbidden type messages on unavailable functions.
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00006842 if (FD->hasAttr<UnavailableAttr>() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006843 DD.getForbiddenTypeDiagnostic() ==
6844 diag::err_arc_array_param_no_ownership) {
6845 DD.Triggered = true;
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00006846 return;
6847 }
6848 }
John McCall31168b02011-06-15 23:02:42 +00006849
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006850 S.Diag(DD.Loc, DD.getForbiddenTypeDiagnostic())
6851 << DD.getForbiddenTypeOperand() << DD.getForbiddenTypeArgument();
6852 DD.Triggered = true;
John McCall31168b02011-06-15 23:02:42 +00006853}
6854
Manman Ren45b1ab12016-05-06 19:57:16 +00006855static const AvailabilityAttr *getAttrForPlatform(ASTContext &Context,
6856 const Decl *D) {
6857 // Check each AvailabilityAttr to find the one for this platform.
6858 for (const auto *A : D->attrs()) {
6859 if (const auto *Avail = dyn_cast<AvailabilityAttr>(A)) {
6860 // FIXME: this is copied from CheckAvailability. We should try to
6861 // de-duplicate.
6862
6863 // Check if this is an App Extension "platform", and if so chop off
6864 // the suffix for matching with the actual platform.
6865 StringRef ActualPlatform = Avail->getPlatform()->getName();
6866 StringRef RealizedPlatform = ActualPlatform;
6867 if (Context.getLangOpts().AppExt) {
6868 size_t suffix = RealizedPlatform.rfind("_app_extension");
6869 if (suffix != StringRef::npos)
6870 RealizedPlatform = RealizedPlatform.slice(0, suffix);
6871 }
6872
6873 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
6874
6875 // Match the platform name.
6876 if (RealizedPlatform == TargetPlatform)
6877 return Avail;
6878 }
6879 }
6880 return nullptr;
6881}
6882
Erik Pilkington9f866a72017-07-18 20:32:07 +00006883/// The diagnostic we should emit for \c D, and the declaration that
6884/// originated it, or \c AR_Available.
6885///
6886/// \param D The declaration to check.
6887/// \param Message If non-null, this will be populated with the message from
6888/// the availability attribute that is selected.
6889static std::pair<AvailabilityResult, const NamedDecl *>
6890ShouldDiagnoseAvailabilityOfDecl(const NamedDecl *D, std::string *Message) {
6891 AvailabilityResult Result = D->getAvailability(Message);
6892
6893 // For typedefs, if the typedef declaration appears available look
6894 // to the underlying type to see if it is more restrictive.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006895 while (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00006896 if (Result == AR_Available) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006897 if (const auto *TT = TD->getUnderlyingType()->getAs<TagType>()) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00006898 D = TT->getDecl();
6899 Result = D->getAvailability(Message);
6900 continue;
6901 }
6902 }
6903 break;
6904 }
6905
6906 // Forward class declarations get their attributes from their definition.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006907 if (const auto *IDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00006908 if (IDecl->getDefinition()) {
6909 D = IDecl->getDefinition();
6910 Result = D->getAvailability(Message);
6911 }
6912 }
6913
6914 if (const auto *ECD = dyn_cast<EnumConstantDecl>(D))
6915 if (Result == AR_Available) {
6916 const DeclContext *DC = ECD->getDeclContext();
6917 if (const auto *TheEnumDecl = dyn_cast<EnumDecl>(DC)) {
6918 Result = TheEnumDecl->getAvailability(Message);
6919 D = TheEnumDecl;
6920 }
6921 }
6922
6923 return {Result, D};
6924}
6925
6926
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00006927/// \brief whether we should emit a diagnostic for \c K and \c DeclVersion in
6928/// the context of \c Ctx. For example, we should emit an unavailable diagnostic
6929/// in a deprecated context, but not the other way around.
6930static bool ShouldDiagnoseAvailabilityInContext(Sema &S, AvailabilityResult K,
6931 VersionTuple DeclVersion,
6932 Decl *Ctx) {
6933 assert(K != AR_Available && "Expected an unavailable declaration here!");
6934
6935 // Checks if we should emit the availability diagnostic in the context of C.
6936 auto CheckContext = [&](const Decl *C) {
6937 if (K == AR_NotYetIntroduced) {
6938 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, C))
6939 if (AA->getIntroduced() >= DeclVersion)
6940 return true;
6941 } else if (K == AR_Deprecated)
6942 if (C->isDeprecated())
6943 return true;
6944
6945 if (C->isUnavailable())
6946 return true;
6947 return false;
6948 };
6949
6950 // FIXME: This is a temporary workaround! Some existing Apple headers depends
6951 // on nested declarations in an @interface having the availability of the
6952 // interface when they really shouldn't: they are members of the enclosing
6953 // context, and can referenced from there.
6954 if (S.OriginalLexicalContext && cast<Decl>(S.OriginalLexicalContext) != Ctx) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006955 const auto *OrigCtx = cast<Decl>(S.OriginalLexicalContext);
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00006956 if (CheckContext(OrigCtx))
6957 return false;
6958
6959 // An implementation implicitly has the availability of the interface.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006960 if (const auto *CatOrImpl = dyn_cast<ObjCImplDecl>(OrigCtx)) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00006961 if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface())
6962 if (CheckContext(Interface))
6963 return false;
6964 }
6965 // A category implicitly has the availability of the interface.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006966 else if (const auto *CatD = dyn_cast<ObjCCategoryDecl>(OrigCtx))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00006967 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
6968 if (CheckContext(Interface))
6969 return false;
6970 }
6971
6972 do {
6973 if (CheckContext(Ctx))
6974 return false;
6975
6976 // An implementation implicitly has the availability of the interface.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006977 if (const auto *CatOrImpl = dyn_cast<ObjCImplDecl>(Ctx)) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00006978 if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface())
6979 if (CheckContext(Interface))
6980 return false;
6981 }
6982 // A category implicitly has the availability of the interface.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006983 else if (const auto *CatD = dyn_cast<ObjCCategoryDecl>(Ctx))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00006984 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
6985 if (CheckContext(Interface))
6986 return false;
6987 } while ((Ctx = cast_or_null<Decl>(Ctx->getDeclContext())));
6988
6989 return true;
6990}
6991
Alex Lorenzc9a369f2017-06-22 17:02:24 +00006992static bool
6993shouldDiagnoseAvailabilityByDefault(const ASTContext &Context,
6994 const VersionTuple &DeploymentVersion,
6995 const VersionTuple &DeclVersion) {
6996 const auto &Triple = Context.getTargetInfo().getTriple();
6997 VersionTuple ForceAvailabilityFromVersion;
6998 switch (Triple.getOS()) {
6999 case llvm::Triple::IOS:
7000 case llvm::Triple::TvOS:
7001 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/11);
7002 break;
7003 case llvm::Triple::WatchOS:
7004 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/4);
7005 break;
7006 case llvm::Triple::Darwin:
7007 case llvm::Triple::MacOSX:
7008 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/10, /*Minor=*/13);
7009 break;
7010 default:
7011 // New targets should always warn about availability.
7012 return Triple.getVendor() == llvm::Triple::Apple;
7013 }
7014 return DeploymentVersion >= ForceAvailabilityFromVersion ||
7015 DeclVersion >= ForceAvailabilityFromVersion;
7016}
7017
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007018static NamedDecl *findEnclosingDeclToAnnotate(Decl *OrigCtx) {
7019 for (Decl *Ctx = OrigCtx; Ctx;
7020 Ctx = cast_or_null<Decl>(Ctx->getDeclContext())) {
7021 if (isa<TagDecl>(Ctx) || isa<FunctionDecl>(Ctx) || isa<ObjCMethodDecl>(Ctx))
7022 return cast<NamedDecl>(Ctx);
7023 if (auto *CD = dyn_cast<ObjCContainerDecl>(Ctx)) {
7024 if (auto *Imp = dyn_cast<ObjCImplDecl>(Ctx))
7025 return Imp->getClassInterface();
7026 return CD;
7027 }
7028 }
7029
7030 return dyn_cast<NamedDecl>(OrigCtx);
7031}
7032
Alex Lorenz727c21e2017-07-26 13:58:02 +00007033namespace {
7034
7035struct AttributeInsertion {
7036 StringRef Prefix;
7037 SourceLocation Loc;
7038 StringRef Suffix;
7039
7040 static AttributeInsertion createInsertionAfter(const NamedDecl *D) {
7041 return {" ", D->getLocEnd(), ""};
7042 }
7043 static AttributeInsertion createInsertionAfter(SourceLocation Loc) {
7044 return {" ", Loc, ""};
7045 }
7046 static AttributeInsertion createInsertionBefore(const NamedDecl *D) {
7047 return {"", D->getLocStart(), "\n"};
7048 }
7049};
7050
7051} // end anonymous namespace
7052
7053/// Returns a source location in which it's appropriate to insert a new
7054/// attribute for the given declaration \D.
7055static Optional<AttributeInsertion>
7056createAttributeInsertion(const NamedDecl *D, const SourceManager &SM,
7057 const LangOptions &LangOpts) {
7058 if (isa<ObjCPropertyDecl>(D))
7059 return AttributeInsertion::createInsertionAfter(D);
7060 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
7061 if (MD->hasBody())
7062 return None;
7063 return AttributeInsertion::createInsertionAfter(D);
7064 }
7065 if (const auto *TD = dyn_cast<TagDecl>(D)) {
7066 SourceLocation Loc =
7067 Lexer::getLocForEndOfToken(TD->getInnerLocStart(), 0, SM, LangOpts);
7068 if (Loc.isInvalid())
7069 return None;
7070 // Insert after the 'struct'/whatever keyword.
7071 return AttributeInsertion::createInsertionAfter(Loc);
7072 }
7073 return AttributeInsertion::createInsertionBefore(D);
7074}
7075
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007076/// Actually emit an availability diagnostic for a reference to an unavailable
7077/// decl.
7078///
7079/// \param Ctx The context that the reference occurred in
7080/// \param ReferringDecl The exact declaration that was referenced.
7081/// \param OffendingDecl A related decl to \c ReferringDecl that has an
7082/// availability attribute corrisponding to \c K attached to it. Note that this
7083/// may not be the same as ReferringDecl, i.e. if an EnumDecl is annotated and
7084/// we refer to a member EnumConstantDecl, ReferringDecl is the EnumConstantDecl
7085/// and OffendingDecl is the EnumDecl.
Erik Pilkington796a3e22016-08-05 22:59:03 +00007086static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007087 Decl *Ctx, const NamedDecl *ReferringDecl,
7088 const NamedDecl *OffendingDecl,
Aaron Ballmanfb237522014-10-15 15:37:51 +00007089 StringRef Message, SourceLocation Loc,
7090 const ObjCInterfaceDecl *UnknownObjCClass,
7091 const ObjCPropertyDecl *ObjCProperty,
7092 bool ObjCPropertyAccess) {
7093 // Diagnostics for deprecated or unavailable.
7094 unsigned diag, diag_message, diag_fwdclass_message;
John McCallb61e14e2015-10-27 04:54:50 +00007095 unsigned diag_available_here = diag::note_availability_specified_here;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007096 SourceLocation NoteLocation = OffendingDecl->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00007097
7098 // Matches 'diag::note_property_attribute' options.
7099 unsigned property_note_select;
7100
7101 // Matches diag::note_availability_specified_here.
7102 unsigned available_here_select_kind;
7103
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007104 VersionTuple DeclVersion;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007105 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, OffendingDecl))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007106 DeclVersion = AA->getIntroduced();
7107
7108 if (!ShouldDiagnoseAvailabilityInContext(S, K, DeclVersion, Ctx))
7109 return;
7110
Erik Pilkington8b352c42017-08-14 19:49:12 +00007111 // The declaration can have multiple availability attributes, we are looking
7112 // at one of them.
7113 const AvailabilityAttr *A = getAttrForPlatform(S.Context, OffendingDecl);
7114 if (A && A->isInherited()) {
7115 for (const Decl *Redecl = OffendingDecl->getMostRecentDecl(); Redecl;
7116 Redecl = Redecl->getPreviousDecl()) {
7117 const AvailabilityAttr *AForRedecl =
7118 getAttrForPlatform(S.Context, Redecl);
7119 if (AForRedecl && !AForRedecl->isInherited()) {
7120 // If D is a declaration with inherited attributes, the note should
7121 // point to the declaration with actual attributes.
7122 NoteLocation = Redecl->getLocation();
7123 break;
7124 }
7125 }
7126 }
7127
Aaron Ballmanfb237522014-10-15 15:37:51 +00007128 switch (K) {
Erik Pilkington8b352c42017-08-14 19:49:12 +00007129 case AR_NotYetIntroduced: {
7130 // We would like to emit the diagnostic even if -Wunguarded-availability is
7131 // not specified for deployment targets >= to iOS 11 or equivalent or
7132 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
7133 // later.
7134 const AvailabilityAttr *AA =
7135 getAttrForPlatform(S.getASTContext(), OffendingDecl);
7136 VersionTuple Introduced = AA->getIntroduced();
7137
7138 bool UseNewWarning = shouldDiagnoseAvailabilityByDefault(
7139 S.Context, S.Context.getTargetInfo().getPlatformMinVersion(),
7140 Introduced);
7141 unsigned Warning = UseNewWarning ? diag::warn_unguarded_availability_new
7142 : diag::warn_unguarded_availability;
7143
7144 S.Diag(Loc, Warning)
7145 << OffendingDecl
7146 << AvailabilityAttr::getPrettyPlatformName(
7147 S.getASTContext().getTargetInfo().getPlatformName())
7148 << Introduced.getAsString();
7149
7150 S.Diag(OffendingDecl->getLocation(), diag::note_availability_specified_here)
7151 << OffendingDecl << /* partial */ 3;
7152
7153 if (const auto *Enclosing = findEnclosingDeclToAnnotate(Ctx)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007154 if (const auto *TD = dyn_cast<TagDecl>(Enclosing))
Erik Pilkington8b352c42017-08-14 19:49:12 +00007155 if (TD->getDeclName().isEmpty()) {
7156 S.Diag(TD->getLocation(),
7157 diag::note_decl_unguarded_availability_silence)
7158 << /*Anonymous*/ 1 << TD->getKindName();
7159 return;
7160 }
7161 auto FixitNoteDiag =
7162 S.Diag(Enclosing->getLocation(),
7163 diag::note_decl_unguarded_availability_silence)
7164 << /*Named*/ 0 << Enclosing;
7165 // Don't offer a fixit for declarations with availability attributes.
7166 if (Enclosing->hasAttr<AvailabilityAttr>())
7167 return;
7168 if (!S.getPreprocessor().isMacroDefined("API_AVAILABLE"))
7169 return;
7170 Optional<AttributeInsertion> Insertion = createAttributeInsertion(
7171 Enclosing, S.getSourceManager(), S.getLangOpts());
7172 if (!Insertion)
7173 return;
7174 std::string PlatformName =
7175 AvailabilityAttr::getPlatformNameSourceSpelling(
7176 S.getASTContext().getTargetInfo().getPlatformName())
7177 .lower();
7178 std::string Introduced =
7179 OffendingDecl->getVersionIntroduced().getAsString();
7180 FixitNoteDiag << FixItHint::CreateInsertion(
7181 Insertion->Loc,
7182 (llvm::Twine(Insertion->Prefix) + "API_AVAILABLE(" + PlatformName +
7183 "(" + Introduced + "))" + Insertion->Suffix)
7184 .str());
7185 }
7186 return;
7187 }
Erik Pilkington796a3e22016-08-05 22:59:03 +00007188 case AR_Deprecated:
Aaron Ballmanfb237522014-10-15 15:37:51 +00007189 diag = !ObjCPropertyAccess ? diag::warn_deprecated
7190 : diag::warn_property_method_deprecated;
7191 diag_message = diag::warn_deprecated_message;
7192 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
7193 property_note_select = /* deprecated */ 0;
7194 available_here_select_kind = /* deprecated */ 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007195 if (const auto *AL = OffendingDecl->getAttr<DeprecatedAttr>())
7196 NoteLocation = AL->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00007197 break;
7198
Erik Pilkington796a3e22016-08-05 22:59:03 +00007199 case AR_Unavailable:
Aaron Ballmanfb237522014-10-15 15:37:51 +00007200 diag = !ObjCPropertyAccess ? diag::err_unavailable
7201 : diag::err_property_method_unavailable;
7202 diag_message = diag::err_unavailable_message;
7203 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
7204 property_note_select = /* unavailable */ 1;
7205 available_here_select_kind = /* unavailable */ 0;
John McCallb61e14e2015-10-27 04:54:50 +00007206
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007207 if (auto AL = OffendingDecl->getAttr<UnavailableAttr>()) {
7208 if (AL->isImplicit() && AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00007209 // Most of these failures are due to extra restrictions in ARC;
7210 // reflect that in the primary diagnostic when applicable.
7211 auto flagARCError = [&] {
7212 if (S.getLangOpts().ObjCAutoRefCount &&
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007213 S.getSourceManager().isInSystemHeader(
7214 OffendingDecl->getLocation()))
John McCallc6af8c62015-10-28 05:03:19 +00007215 diag = diag::err_unavailable_in_arc;
7216 };
7217
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007218 switch (AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00007219 case UnavailableAttr::IR_None: break;
7220
7221 case UnavailableAttr::IR_ARCForbiddenType:
7222 flagARCError();
7223 diag_available_here = diag::note_arc_forbidden_type;
7224 break;
7225
7226 case UnavailableAttr::IR_ForbiddenWeak:
7227 if (S.getLangOpts().ObjCWeakRuntime)
7228 diag_available_here = diag::note_arc_weak_disabled;
7229 else
7230 diag_available_here = diag::note_arc_weak_no_runtime;
7231 break;
7232
7233 case UnavailableAttr::IR_ARCForbiddenConversion:
7234 flagARCError();
7235 diag_available_here = diag::note_performs_forbidden_arc_conversion;
7236 break;
7237
7238 case UnavailableAttr::IR_ARCInitReturnsUnrelated:
7239 flagARCError();
7240 diag_available_here = diag::note_arc_init_returns_unrelated;
7241 break;
7242
7243 case UnavailableAttr::IR_ARCFieldWithOwnership:
7244 flagARCError();
7245 diag_available_here = diag::note_arc_field_with_ownership;
7246 break;
7247 }
7248 }
John McCallb61e14e2015-10-27 04:54:50 +00007249 }
Aaron Ballmanfb237522014-10-15 15:37:51 +00007250 break;
7251
Erik Pilkington796a3e22016-08-05 22:59:03 +00007252 case AR_Available:
7253 llvm_unreachable("Warning for availability of available declaration?");
Aaron Ballmanfb237522014-10-15 15:37:51 +00007254 }
7255
Manman Renc7890fe2016-03-16 18:50:49 +00007256 CharSourceRange UseRange;
7257 StringRef Replacement;
Erik Pilkington796a3e22016-08-05 22:59:03 +00007258 if (K == AR_Deprecated) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007259 if (auto AL = OffendingDecl->getAttr<DeprecatedAttr>())
7260 Replacement = AL->getReplacement();
7261 if (auto AL = getAttrForPlatform(S.Context, OffendingDecl))
7262 Replacement = AL->getReplacement();
Manman Renc7890fe2016-03-16 18:50:49 +00007263
7264 if (!Replacement.empty())
7265 UseRange =
7266 CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc));
7267 }
7268
Aaron Ballmanfb237522014-10-15 15:37:51 +00007269 if (!Message.empty()) {
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007270 S.Diag(Loc, diag_message) << ReferringDecl << Message
Manman Renc7890fe2016-03-16 18:50:49 +00007271 << (UseRange.isValid() ?
7272 FixItHint::CreateReplacement(UseRange, Replacement) : FixItHint());
Aaron Ballmanfb237522014-10-15 15:37:51 +00007273 if (ObjCProperty)
7274 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
7275 << ObjCProperty->getDeclName() << property_note_select;
7276 } else if (!UnknownObjCClass) {
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007277 S.Diag(Loc, diag) << ReferringDecl
Manman Renc7890fe2016-03-16 18:50:49 +00007278 << (UseRange.isValid() ?
7279 FixItHint::CreateReplacement(UseRange, Replacement) : FixItHint());
Aaron Ballmanfb237522014-10-15 15:37:51 +00007280 if (ObjCProperty)
7281 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
7282 << ObjCProperty->getDeclName() << property_note_select;
7283 } else {
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007284 S.Diag(Loc, diag_fwdclass_message) << ReferringDecl
Manman Renc7890fe2016-03-16 18:50:49 +00007285 << (UseRange.isValid() ?
7286 FixItHint::CreateReplacement(UseRange, Replacement) : FixItHint());
Aaron Ballmanfb237522014-10-15 15:37:51 +00007287 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
7288 }
7289
Erik Pilkington8b352c42017-08-14 19:49:12 +00007290 S.Diag(NoteLocation, diag_available_here)
7291 << OffendingDecl << available_here_select_kind;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007292}
7293
7294static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
7295 Decl *Ctx) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00007296 assert(DD.Kind == DelayedDiagnostic::Availability &&
7297 "Expected an availability diagnostic here");
7298
Aaron Ballmanfb237522014-10-15 15:37:51 +00007299 DD.Triggered = true;
Nico Weber0055a192015-03-19 19:18:22 +00007300 DoEmitAvailabilityWarning(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007301 S, DD.getAvailabilityResult(), Ctx, DD.getAvailabilityReferringDecl(),
7302 DD.getAvailabilityOffendingDecl(), DD.getAvailabilityMessage(), DD.Loc,
7303 DD.getUnknownObjCClass(), DD.getObjCProperty(), false);
Aaron Ballmanfb237522014-10-15 15:37:51 +00007304}
7305
John McCall2ec85372012-05-07 06:16:41 +00007306void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
7307 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00007308 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00007309 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00007310
John McCall2ec85372012-05-07 06:16:41 +00007311 // When delaying diagnostics to run in the context of a parsed
7312 // declaration, we only want to actually emit anything if parsing
7313 // succeeds.
7314 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00007315
John McCall2ec85372012-05-07 06:16:41 +00007316 // We emit all the active diagnostics in this pool or any of its
7317 // parents. In general, we'll get one pool for the decl spec
7318 // and a child pool for each declarator; in a decl group like:
7319 // deprecated_typedef foo, *bar, baz();
7320 // only the declarator pops will be passed decls. This is correct;
7321 // we really do need to consider delayed diagnostics from the decl spec
7322 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00007323 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00007324 do {
John McCall6347b682012-05-07 06:16:58 +00007325 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00007326 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
7327 // This const_cast is a bit lame. Really, Triggered should be mutable.
7328 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00007329 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00007330 continue;
7331
John McCallc1465822011-02-14 07:13:47 +00007332 switch (diag.Kind) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00007333 case DelayedDiagnostic::Availability:
Ted Kremenekb79ee572013-12-18 23:30:06 +00007334 // Don't bother giving deprecation/unavailable diagnostics if
7335 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00007336 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00007337 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00007338 break;
7339
7340 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00007341 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00007342 break;
John McCall31168b02011-06-15 23:02:42 +00007343
7344 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00007345 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00007346 break;
John McCall86121512010-01-27 03:50:35 +00007347 }
7348 }
John McCall2ec85372012-05-07 06:16:41 +00007349 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00007350}
7351
John McCall6347b682012-05-07 06:16:58 +00007352/// Given a set of delayed diagnostics, re-emit them as if they had
7353/// been delayed in the current context instead of in the given pool.
7354/// Essentially, this just moves them to the current pool.
7355void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
7356 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
7357 assert(curPool && "re-emitting in undelayed context not supported");
7358 curPool->steal(pool);
7359}
7360
Erik Pilkington9f866a72017-07-18 20:32:07 +00007361static void EmitAvailabilityWarning(Sema &S, AvailabilityResult AR,
7362 const NamedDecl *ReferringDecl,
7363 const NamedDecl *OffendingDecl,
7364 StringRef Message, SourceLocation Loc,
7365 const ObjCInterfaceDecl *UnknownObjCClass,
7366 const ObjCPropertyDecl *ObjCProperty,
7367 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00007368 // Delay if we're currently parsing a declaration.
Erik Pilkington9f866a72017-07-18 20:32:07 +00007369 if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
7370 S.DelayedDiagnostics.add(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007371 DelayedDiagnostic::makeAvailability(
7372 AR, Loc, ReferringDecl, OffendingDecl, UnknownObjCClass,
7373 ObjCProperty, Message, ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00007374 return;
7375 }
7376
Erik Pilkington9f866a72017-07-18 20:32:07 +00007377 Decl *Ctx = cast<Decl>(S.getCurLexicalContext());
7378 DoEmitAvailabilityWarning(S, AR, Ctx, ReferringDecl, OffendingDecl,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007379 Message, Loc, UnknownObjCClass, ObjCProperty,
7380 ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00007381}
Erik Pilkington48c7cc92016-07-29 17:37:38 +00007382
Erik Pilkington5cd57172016-08-16 17:44:11 +00007383namespace {
7384
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007385/// Returns true if the given statement can be a body-like child of \p Parent.
7386bool isBodyLikeChildStmt(const Stmt *S, const Stmt *Parent) {
7387 switch (Parent->getStmtClass()) {
7388 case Stmt::IfStmtClass:
7389 return cast<IfStmt>(Parent)->getThen() == S ||
7390 cast<IfStmt>(Parent)->getElse() == S;
7391 case Stmt::WhileStmtClass:
7392 return cast<WhileStmt>(Parent)->getBody() == S;
7393 case Stmt::DoStmtClass:
7394 return cast<DoStmt>(Parent)->getBody() == S;
7395 case Stmt::ForStmtClass:
7396 return cast<ForStmt>(Parent)->getBody() == S;
7397 case Stmt::CXXForRangeStmtClass:
7398 return cast<CXXForRangeStmt>(Parent)->getBody() == S;
7399 case Stmt::ObjCForCollectionStmtClass:
7400 return cast<ObjCForCollectionStmt>(Parent)->getBody() == S;
7401 case Stmt::CaseStmtClass:
7402 case Stmt::DefaultStmtClass:
7403 return cast<SwitchCase>(Parent)->getSubStmt() == S;
7404 default:
7405 return false;
7406 }
7407}
7408
7409class StmtUSEFinder : public RecursiveASTVisitor<StmtUSEFinder> {
7410 const Stmt *Target;
7411
7412public:
7413 bool VisitStmt(Stmt *S) { return S != Target; }
7414
7415 /// Returns true if the given statement is present in the given declaration.
7416 static bool isContained(const Stmt *Target, const Decl *D) {
7417 StmtUSEFinder Visitor;
7418 Visitor.Target = Target;
7419 return !Visitor.TraverseDecl(const_cast<Decl *>(D));
7420 }
7421};
7422
7423/// Traverses the AST and finds the last statement that used a given
7424/// declaration.
7425class LastDeclUSEFinder : public RecursiveASTVisitor<LastDeclUSEFinder> {
7426 const Decl *D;
7427
7428public:
7429 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
7430 if (DRE->getDecl() == D)
7431 return false;
7432 return true;
7433 }
7434
7435 static const Stmt *findLastStmtThatUsesDecl(const Decl *D,
7436 const CompoundStmt *Scope) {
7437 LastDeclUSEFinder Visitor;
7438 Visitor.D = D;
7439 for (auto I = Scope->body_rbegin(), E = Scope->body_rend(); I != E; ++I) {
7440 const Stmt *S = *I;
7441 if (!Visitor.TraverseStmt(const_cast<Stmt *>(S)))
7442 return S;
7443 }
7444 return nullptr;
7445 }
7446};
7447
Erik Pilkington5cd57172016-08-16 17:44:11 +00007448/// \brief This class implements -Wunguarded-availability.
7449///
7450/// This is done with a traversal of the AST of a function that makes reference
7451/// to a partially available declaration. Whenever we encounter an \c if of the
7452/// form: \c if(@available(...)), we use the version from the condition to visit
7453/// the then statement.
7454class DiagnoseUnguardedAvailability
7455 : public RecursiveASTVisitor<DiagnoseUnguardedAvailability> {
7456 typedef RecursiveASTVisitor<DiagnoseUnguardedAvailability> Base;
7457
7458 Sema &SemaRef;
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007459 Decl *Ctx;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007460
7461 /// Stack of potentially nested 'if (@available(...))'s.
7462 SmallVector<VersionTuple, 8> AvailabilityStack;
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007463 SmallVector<const Stmt *, 16> StmtStack;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007464
7465 void DiagnoseDeclAvailability(NamedDecl *D, SourceRange Range);
7466
7467public:
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007468 DiagnoseUnguardedAvailability(Sema &SemaRef, Decl *Ctx)
7469 : SemaRef(SemaRef), Ctx(Ctx) {
7470 AvailabilityStack.push_back(
7471 SemaRef.Context.getTargetInfo().getPlatformMinVersion());
Erik Pilkington5cd57172016-08-16 17:44:11 +00007472 }
7473
Alex Lorenz6f911122017-05-16 13:58:53 +00007474 bool TraverseDecl(Decl *D) {
7475 // Avoid visiting nested functions to prevent duplicate warnings.
7476 if (!D || isa<FunctionDecl>(D))
7477 return true;
7478 return Base::TraverseDecl(D);
7479 }
7480
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007481 bool TraverseStmt(Stmt *S) {
7482 if (!S)
7483 return true;
7484 StmtStack.push_back(S);
7485 bool Result = Base::TraverseStmt(S);
7486 StmtStack.pop_back();
7487 return Result;
7488 }
7489
Erik Pilkington5cd57172016-08-16 17:44:11 +00007490 void IssueDiagnostics(Stmt *S) { TraverseStmt(S); }
7491
7492 bool TraverseIfStmt(IfStmt *If);
7493
Alex Lorenz6f911122017-05-16 13:58:53 +00007494 bool TraverseLambdaExpr(LambdaExpr *E) { return true; }
7495
Erik Pilkingtonba87c622017-08-18 20:20:56 +00007496 // for 'case X:' statements, don't bother looking at the 'X'; it can't lead
7497 // to any useful diagnostics.
7498 bool TraverseCaseStmt(CaseStmt *CS) { return TraverseStmt(CS->getSubStmt()); }
7499
Erik Pilkington6ac77a62017-05-22 15:41:12 +00007500 bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *PRE) {
7501 if (PRE->isClassReceiver())
7502 DiagnoseDeclAvailability(PRE->getClassReceiver(), PRE->getReceiverLocation());
7503 return true;
7504 }
7505
Erik Pilkington5cd57172016-08-16 17:44:11 +00007506 bool VisitObjCMessageExpr(ObjCMessageExpr *Msg) {
7507 if (ObjCMethodDecl *D = Msg->getMethodDecl())
7508 DiagnoseDeclAvailability(
7509 D, SourceRange(Msg->getSelectorStartLoc(), Msg->getLocEnd()));
7510 return true;
7511 }
7512
7513 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
7514 DiagnoseDeclAvailability(DRE->getDecl(),
7515 SourceRange(DRE->getLocStart(), DRE->getLocEnd()));
7516 return true;
7517 }
7518
7519 bool VisitMemberExpr(MemberExpr *ME) {
7520 DiagnoseDeclAvailability(ME->getMemberDecl(),
7521 SourceRange(ME->getLocStart(), ME->getLocEnd()));
7522 return true;
7523 }
7524
Alex Lorenz0a484ba2017-05-24 15:15:29 +00007525 bool VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr *E) {
7526 SemaRef.Diag(E->getLocStart(), diag::warn_at_available_unchecked_use)
7527 << (!SemaRef.getLangOpts().ObjC1);
7528 return true;
7529 }
7530
Erik Pilkington5cd57172016-08-16 17:44:11 +00007531 bool VisitTypeLoc(TypeLoc Ty);
7532};
7533
7534void DiagnoseUnguardedAvailability::DiagnoseDeclAvailability(
7535 NamedDecl *D, SourceRange Range) {
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007536 AvailabilityResult Result;
7537 const NamedDecl *OffendingDecl;
7538 std::tie(Result, OffendingDecl) =
Erik Pilkington9f866a72017-07-18 20:32:07 +00007539 ShouldDiagnoseAvailabilityOfDecl(D, nullptr);
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007540 if (Result != AR_Available) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00007541 // All other diagnostic kinds have already been handled in
7542 // DiagnoseAvailabilityOfDecl.
7543 if (Result != AR_NotYetIntroduced)
7544 return;
7545
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007546 const AvailabilityAttr *AA =
7547 getAttrForPlatform(SemaRef.getASTContext(), OffendingDecl);
Erik Pilkington5cd57172016-08-16 17:44:11 +00007548 VersionTuple Introduced = AA->getIntroduced();
7549
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007550 if (AvailabilityStack.back() >= Introduced)
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007551 return;
7552
7553 // If the context of this function is less available than D, we should not
7554 // emit a diagnostic.
7555 if (!ShouldDiagnoseAvailabilityInContext(SemaRef, Result, Introduced, Ctx))
7556 return;
7557
Alex Lorenzc9a369f2017-06-22 17:02:24 +00007558 // We would like to emit the diagnostic even if -Wunguarded-availability is
7559 // not specified for deployment targets >= to iOS 11 or equivalent or
7560 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
7561 // later.
7562 unsigned DiagKind =
7563 shouldDiagnoseAvailabilityByDefault(
7564 SemaRef.Context,
7565 SemaRef.Context.getTargetInfo().getPlatformMinVersion(), Introduced)
7566 ? diag::warn_unguarded_availability_new
7567 : diag::warn_unguarded_availability;
7568
7569 SemaRef.Diag(Range.getBegin(), DiagKind)
Erik Pilkington5cd57172016-08-16 17:44:11 +00007570 << Range << D
7571 << AvailabilityAttr::getPrettyPlatformName(
7572 SemaRef.getASTContext().getTargetInfo().getPlatformName())
7573 << Introduced.getAsString();
7574
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007575 SemaRef.Diag(OffendingDecl->getLocation(),
7576 diag::note_availability_specified_here)
7577 << OffendingDecl << /* partial */ 3;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007578
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007579 auto FixitDiag =
7580 SemaRef.Diag(Range.getBegin(), diag::note_unguarded_available_silence)
7581 << Range << D
7582 << (SemaRef.getLangOpts().ObjC1 ? /*@available*/ 0
7583 : /*__builtin_available*/ 1);
7584
7585 // Find the statement which should be enclosed in the if @available check.
7586 if (StmtStack.empty())
7587 return;
7588 const Stmt *StmtOfUse = StmtStack.back();
7589 const CompoundStmt *Scope = nullptr;
7590 for (const Stmt *S : llvm::reverse(StmtStack)) {
7591 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
7592 Scope = CS;
7593 break;
7594 }
7595 if (isBodyLikeChildStmt(StmtOfUse, S)) {
7596 // The declaration won't be seen outside of the statement, so we don't
7597 // have to wrap the uses of any declared variables in if (@available).
7598 // Therefore we can avoid setting Scope here.
7599 break;
7600 }
7601 StmtOfUse = S;
7602 }
7603 const Stmt *LastStmtOfUse = nullptr;
7604 if (isa<DeclStmt>(StmtOfUse) && Scope) {
7605 for (const Decl *D : cast<DeclStmt>(StmtOfUse)->decls()) {
7606 if (StmtUSEFinder::isContained(StmtStack.back(), D)) {
7607 LastStmtOfUse = LastDeclUSEFinder::findLastStmtThatUsesDecl(D, Scope);
7608 break;
7609 }
7610 }
7611 }
7612
7613 const SourceManager &SM = SemaRef.getSourceManager();
7614 SourceLocation IfInsertionLoc =
7615 SM.getExpansionLoc(StmtOfUse->getLocStart());
7616 SourceLocation StmtEndLoc =
7617 SM.getExpansionRange(
7618 (LastStmtOfUse ? LastStmtOfUse : StmtOfUse)->getLocEnd())
7619 .second;
7620 if (SM.getFileID(IfInsertionLoc) != SM.getFileID(StmtEndLoc))
7621 return;
7622
7623 StringRef Indentation = Lexer::getIndentationForLine(IfInsertionLoc, SM);
7624 const char *ExtraIndentation = " ";
7625 std::string FixItString;
7626 llvm::raw_string_ostream FixItOS(FixItString);
7627 FixItOS << "if (" << (SemaRef.getLangOpts().ObjC1 ? "@available"
7628 : "__builtin_available")
Alex Lorenze1fb64e2017-05-09 15:34:46 +00007629 << "("
7630 << AvailabilityAttr::getPlatformNameSourceSpelling(
7631 SemaRef.getASTContext().getTargetInfo().getPlatformName())
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007632 << " " << Introduced.getAsString() << ", *)) {\n"
7633 << Indentation << ExtraIndentation;
7634 FixitDiag << FixItHint::CreateInsertion(IfInsertionLoc, FixItOS.str());
7635 SourceLocation ElseInsertionLoc = Lexer::findLocationAfterToken(
7636 StmtEndLoc, tok::semi, SM, SemaRef.getLangOpts(),
7637 /*SkipTrailingWhitespaceAndNewLine=*/false);
7638 if (ElseInsertionLoc.isInvalid())
7639 ElseInsertionLoc =
7640 Lexer::getLocForEndOfToken(StmtEndLoc, 0, SM, SemaRef.getLangOpts());
7641 FixItOS.str().clear();
7642 FixItOS << "\n"
7643 << Indentation << "} else {\n"
7644 << Indentation << ExtraIndentation
7645 << "// Fallback on earlier versions\n"
7646 << Indentation << "}";
7647 FixitDiag << FixItHint::CreateInsertion(ElseInsertionLoc, FixItOS.str());
Erik Pilkington5cd57172016-08-16 17:44:11 +00007648 }
7649}
7650
7651bool DiagnoseUnguardedAvailability::VisitTypeLoc(TypeLoc Ty) {
7652 const Type *TyPtr = Ty.getTypePtr();
7653 SourceRange Range{Ty.getBeginLoc(), Ty.getEndLoc()};
7654
Erik Pilkington6ac77a62017-05-22 15:41:12 +00007655 if (Range.isInvalid())
7656 return true;
7657
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007658 if (const auto *TT = dyn_cast<TagType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00007659 TagDecl *TD = TT->getDecl();
7660 DiagnoseDeclAvailability(TD, Range);
7661
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007662 } else if (const auto *TD = dyn_cast<TypedefType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00007663 TypedefNameDecl *D = TD->getDecl();
7664 DiagnoseDeclAvailability(D, Range);
7665
7666 } else if (const auto *ObjCO = dyn_cast<ObjCObjectType>(TyPtr)) {
7667 if (NamedDecl *D = ObjCO->getInterface())
7668 DiagnoseDeclAvailability(D, Range);
7669 }
7670
7671 return true;
7672}
7673
7674bool DiagnoseUnguardedAvailability::TraverseIfStmt(IfStmt *If) {
7675 VersionTuple CondVersion;
7676 if (auto *E = dyn_cast<ObjCAvailabilityCheckExpr>(If->getCond())) {
7677 CondVersion = E->getVersion();
7678
7679 // If we're using the '*' case here or if this check is redundant, then we
7680 // use the enclosing version to check both branches.
7681 if (CondVersion.empty() || CondVersion <= AvailabilityStack.back())
Alex Lorenz98f9fcd2017-08-17 14:22:27 +00007682 return TraverseStmt(If->getThen()) && TraverseStmt(If->getElse());
Erik Pilkington5cd57172016-08-16 17:44:11 +00007683 } else {
7684 // This isn't an availability checking 'if', we can just continue.
7685 return Base::TraverseIfStmt(If);
7686 }
7687
7688 AvailabilityStack.push_back(CondVersion);
7689 bool ShouldContinue = TraverseStmt(If->getThen());
7690 AvailabilityStack.pop_back();
7691
7692 return ShouldContinue && TraverseStmt(If->getElse());
7693}
7694
7695} // end anonymous namespace
7696
7697void Sema::DiagnoseUnguardedAvailabilityViolations(Decl *D) {
7698 Stmt *Body = nullptr;
7699
7700 if (auto *FD = D->getAsFunction()) {
7701 // FIXME: We only examine the pattern decl for availability violations now,
7702 // but we should also examine instantiated templates.
7703 if (FD->isTemplateInstantiation())
7704 return;
7705
7706 Body = FD->getBody();
7707 } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
7708 Body = MD->getBody();
Alex Lorenz28559ce2017-04-26 14:20:02 +00007709 else if (auto *BD = dyn_cast<BlockDecl>(D))
7710 Body = BD->getBody();
Erik Pilkington5cd57172016-08-16 17:44:11 +00007711
7712 assert(Body && "Need a body here!");
7713
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007714 DiagnoseUnguardedAvailability(*this, D).IssueDiagnostics(Body);
Erik Pilkington5cd57172016-08-16 17:44:11 +00007715}
Erik Pilkington9f866a72017-07-18 20:32:07 +00007716
7717void Sema::DiagnoseAvailabilityOfDecl(NamedDecl *D, SourceLocation Loc,
7718 const ObjCInterfaceDecl *UnknownObjCClass,
7719 bool ObjCPropertyAccess,
7720 bool AvoidPartialAvailabilityChecks) {
7721 std::string Message;
7722 AvailabilityResult Result;
7723 const NamedDecl* OffendingDecl;
7724 // See if this declaration is unavailable, deprecated, or partial.
7725 std::tie(Result, OffendingDecl) = ShouldDiagnoseAvailabilityOfDecl(D, &Message);
7726 if (Result == AR_Available)
7727 return;
7728
7729 if (Result == AR_NotYetIntroduced) {
7730 if (AvoidPartialAvailabilityChecks)
7731 return;
7732
7733 // We need to know the @available context in the current function to
7734 // diagnose this use, let DiagnoseUnguardedAvailabilityViolations do that
7735 // when we're done parsing the current function.
7736 if (getCurFunctionOrMethodDecl()) {
7737 getEnclosingFunction()->HasPotentialAvailabilityViolations = true;
7738 return;
7739 } else if (getCurBlock() || getCurLambda()) {
7740 getCurFunction()->HasPotentialAvailabilityViolations = true;
7741 return;
7742 }
7743 }
7744
7745 const ObjCPropertyDecl *ObjCPDecl = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007746 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007747 if (const ObjCPropertyDecl *PD = MD->findPropertyDecl()) {
7748 AvailabilityResult PDeclResult = PD->getAvailability(nullptr);
7749 if (PDeclResult == Result)
7750 ObjCPDecl = PD;
7751 }
7752 }
7753
7754 EmitAvailabilityWarning(*this, Result, D, OffendingDecl, Message, Loc,
7755 UnknownObjCClass, ObjCPDecl, ObjCPropertyAccess);
7756}