blob: 458c31881dc16e7b3da0dd6563814c790500f8ef [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
Erich Keane29636aa2018-02-16 17:31:59 +00003027 return false;
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 Keane29636aa2018-02-16 17:31:59 +00003034 S.checkTargetAttr(LiteralLoc, Str))
Eric Christopher11acf732015-06-12 01:35:52 +00003035 return;
Erich Keane29636aa2018-02-16 17:31:59 +00003036
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003037 unsigned Index = AL.getAttributeSpellingListIndex();
Eric Christopher11acf732015-06-12 01:35:52 +00003038 TargetAttr *NewAttr =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003039 ::new (S.Context) TargetAttr(AL.getRange(), S.Context, Str, Index);
Eric Christopher11acf732015-06-12 01:35:52 +00003040 D->addAttr(NewAttr);
3041}
3042
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003043static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &AL) {
3044 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003045 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00003046 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003047 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00003048
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003049 // gcc only allows for simple identifiers. Since we support more than gcc, we
3050 // will warn the user.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003051 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003052 if (DRE->hasQualifier())
3053 S.Diag(Loc, diag::warn_cleanup_ext);
3054 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
3055 NI = DRE->getNameInfo();
3056 if (!FD) {
3057 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
3058 << NI.getName();
3059 return;
3060 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003061 } else if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003062 if (ULE->hasExplicitTemplateArgs())
3063 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003064 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
3065 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00003066 if (!FD) {
3067 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
3068 << NI.getName();
3069 if (ULE->getType() == S.Context.OverloadTy)
3070 S.NoteAllOverloadCandidates(ULE);
3071 return;
3072 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003073 } else {
3074 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00003075 return;
3076 }
3077
Anders Carlssond277d792009-01-31 01:16:18 +00003078 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003079 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
3080 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00003081 return;
3082 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003083
Anders Carlsson723f55d2009-02-07 23:16:50 +00003084 // We're currently more strict than GCC about what function types we accept.
3085 // If this ever proves to be a problem it should be easy to fix.
Aaron Ballman3b70e752017-12-01 16:53:49 +00003086 QualType Ty = S.Context.getPointerType(cast<VarDecl>(D)->getType());
Anders Carlsson723f55d2009-02-07 23:16:50 +00003087 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00003088 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
3089 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003090 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
3091 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00003092 return;
3093 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003094
Michael Han99315932013-01-24 16:46:58 +00003095 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003096 CleanupAttr(AL.getRange(), S.Context, FD,
3097 AL.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00003098}
3099
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003100static void handleEnumExtensibilityAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003101 const AttributeList &AL) {
3102 if (!AL.isArgIdent(0)) {
3103 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
3104 << AL.getName() << 0 << AANT_ArgumentIdentifier;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003105 return;
3106 }
3107
3108 EnumExtensibilityAttr::Kind ExtensibilityKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003109 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003110 if (!EnumExtensibilityAttr::ConvertStrToKind(II->getName(),
3111 ExtensibilityKind)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003112 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
3113 << AL.getName() << II;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003114 return;
3115 }
3116
3117 D->addAttr(::new (S.Context) EnumExtensibilityAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003118 AL.getRange(), S.Context, ExtensibilityKind,
3119 AL.getAttributeSpellingListIndex()));
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003120}
3121
Mike Stumpd3bb5572009-07-24 19:02:52 +00003122/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00003123/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003124static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &AL) {
3125 Expr *IdxExpr = AL.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00003126 uint64_t Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003127 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003128 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00003129
Eric Christopherb64963e2015-08-13 21:34:35 +00003130 // Make sure the format string is really a string.
Alp Toker601b22c2014-01-21 23:35:24 +00003131 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003132
Eric Christopherb64963e2015-08-13 21:34:35 +00003133 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
3134 if (NotNSStringTy &&
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003135 !isCFStringType(Ty, S.Context) &&
3136 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003137 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003138 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003139 << "a string type" << IdxExpr->getSourceRange()
3140 << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003141 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003142 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003143 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003144 if (!isNSStringType(Ty, S.Context) &&
3145 !isCFStringType(Ty, S.Context) &&
3146 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003147 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003148 S.Diag(AL.getLoc(), diag::err_format_attribute_result_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003149 << (NotNSStringTy ? "string type" : "NSString")
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00003150 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003151 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003152 }
3153
Alp Toker601b22c2014-01-21 23:35:24 +00003154 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00003155 // because that has corrected for the implicit this parameter, and is zero-
3156 // based. The attribute expects what the user wrote explicitly.
3157 llvm::APSInt Val;
3158 IdxExpr->EvaluateAsInt(Val, S.Context);
3159
Michael Han99315932013-01-24 16:46:58 +00003160 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003161 FormatArgAttr(AL.getRange(), S.Context, Val.getZExtValue(),
3162 AL.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003163}
3164
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003165enum FormatAttrKind {
3166 CFStringFormat,
3167 NSStringFormat,
3168 StrftimeFormat,
3169 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003170 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003171 InvalidFormat
3172};
3173
3174/// getFormatAttrKind - Map from format attribute names to supported format
3175/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003176static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003177 return llvm::StringSwitch<FormatAttrKind>(Format)
Mehdi Amini06d367c2016-10-24 20:39:34 +00003178 // Check for formats that get handled specially.
3179 .Case("NSString", NSStringFormat)
3180 .Case("CFString", CFStringFormat)
3181 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003182
Mehdi Amini06d367c2016-10-24 20:39:34 +00003183 // Otherwise, check for supported formats.
3184 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3185 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3186 .Case("kprintf", SupportedFormat) // OpenBSD.
3187 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
3188 .Case("os_trace", SupportedFormat)
3189 .Case("os_log", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003190
Mehdi Amini06d367c2016-10-24 20:39:34 +00003191 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3192 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003193}
3194
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003195/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003196/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003197static void handleInitPriorityAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003198 const AttributeList &AL) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003199 if (!S.getLangOpts().CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003200 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL.getName();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003201 return;
3202 }
3203
Aaron Ballman4a611152013-11-27 16:34:09 +00003204 if (S.getCurFunctionOrMethodDecl()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003205 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3206 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003207 return;
3208 }
Aaron Ballman4a611152013-11-27 16:34:09 +00003209 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003210 if (S.Context.getAsArrayType(T))
3211 T = S.Context.getBaseElementType(T);
3212 if (!T->getAs<RecordType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003213 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3214 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003215 return;
3216 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003217
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003218 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003219 uint32_t prioritynum;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003220 if (!checkUInt32Argument(S, AL, E, prioritynum)) {
3221 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003222 return;
3223 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003224
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003225 if (prioritynum < 101 || prioritynum > 65535) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003226 S.Diag(AL.getLoc(), diag::err_attribute_argument_outof_range)
3227 << E->getSourceRange() << AL.getName() << 101 << 65535;
3228 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003229 return;
3230 }
Michael Han99315932013-01-24 16:46:58 +00003231 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003232 InitPriorityAttr(AL.getRange(), S.Context, prioritynum,
3233 AL.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003234}
3235
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003236FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3237 IdentifierInfo *Format, int FormatIdx,
3238 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00003239 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003240 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003241 for (auto *F : D->specific_attrs<FormatAttr>()) {
3242 if (F->getType() == Format &&
3243 F->getFormatIdx() == FormatIdx &&
3244 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003245 // If we don't have a valid location for this attribute, adopt the
3246 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003247 if (F->getLocation().isInvalid())
3248 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00003249 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00003250 }
3251 }
3252
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003253 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3254 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003255}
3256
Mike Stumpd3bb5572009-07-24 19:02:52 +00003257/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003258/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003259static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &AL) {
3260 if (!AL.isArgIdent(0)) {
3261 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
3262 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003263 return;
3264 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003265
Chandler Carruth743682b2010-11-16 08:35:43 +00003266 // In C++ the implicit 'this' function parameter also counts, and they are
3267 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003268 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00003269 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003270
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003271 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Aaron Ballman00e99962013-08-31 01:11:41 +00003272 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003273
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00003274 if (normalizeName(Format)) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003275 // If we've modified the string name, we need a new identifier for it.
3276 II = &S.Context.Idents.get(Format);
3277 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003278
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003279 // Check for supported formats.
3280 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00003281
3282 if (Kind == IgnoredFormat)
3283 return;
3284
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003285 if (Kind == InvalidFormat) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003286 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
3287 << AL.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003288 return;
3289 }
3290
3291 // checks for the 2nd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003292 Expr *IdxExpr = AL.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003293 uint32_t Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003294 if (!checkUInt32Argument(S, AL, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003295 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003296
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003297 if (Idx < 1 || Idx > NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003298 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3299 << AL.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003300 return;
3301 }
3302
3303 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003304 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003305
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003306 if (HasImplicitThisParam) {
3307 if (ArgIdx == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003308 S.Diag(AL.getLoc(),
Chandler Carruth743682b2010-11-16 08:35:43 +00003309 diag::err_format_attribute_implicit_this_format_string)
3310 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003311 return;
3312 }
3313 ArgIdx--;
3314 }
Mike Stump11289f42009-09-09 15:08:12 +00003315
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003316 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00003317 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003318
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003319 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003320 if (!isCFStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003321 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003322 << "a CFString" << IdxExpr->getSourceRange()
3323 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00003324 return;
3325 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003326 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003327 // FIXME: do we need to check if the type is NSString*? What are the
3328 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003329 if (!isNSStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003330 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003331 << "an NSString" << IdxExpr->getSourceRange()
3332 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003333 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003334 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003335 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003336 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003337 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003338 << "a string type" << IdxExpr->getSourceRange()
3339 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003340 return;
3341 }
3342
3343 // check the 3rd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003344 Expr *FirstArgExpr = AL.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003345 uint32_t FirstArg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003346 if (!checkUInt32Argument(S, AL, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003347 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003348
3349 // check if the function is variadic if the 3rd argument non-zero
3350 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003351 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003352 ++NumArgs; // +1 for ...
3353 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003354 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003355 return;
3356 }
3357 }
3358
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003359 // strftime requires FirstArg to be 0 because it doesn't read from any
3360 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003361 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003362 if (FirstArg != 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003363 S.Diag(AL.getLoc(), diag::err_format_strftime_third_parameter)
Chris Lattner3b054132008-11-19 05:08:23 +00003364 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003365 return;
3366 }
3367 // if 0 it disables parameter checking (to use with e.g. va_list)
3368 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003369 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3370 << AL.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003371 return;
3372 }
3373
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003374 FormatAttr *NewAttr = S.mergeFormatAttr(D, AL.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003375 Idx, FirstArg,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003376 AL.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003377 if (NewAttr)
3378 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003379}
3380
Chandler Carruthedc2c642011-07-02 00:01:44 +00003381static void handleTransparentUnionAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003382 const AttributeList &AL) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003383 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00003384 RecordDecl *RD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003385 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003386 if (TD && TD->getUnderlyingType()->isUnionType())
3387 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3388 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003389 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003390
3391 if (!RD || !RD->isUnion()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003392 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
3393 << AL.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003394 return;
3395 }
3396
John McCallf937c022011-10-07 06:10:15 +00003397 if (!RD->isCompleteDefinition()) {
Erich Keane2fe684b2017-02-28 20:44:39 +00003398 if (!RD->isBeingDefined())
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003399 S.Diag(AL.getLoc(),
Erich Keane2fe684b2017-02-28 20:44:39 +00003400 diag::warn_transparent_union_attribute_not_definition);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003401 return;
3402 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003403
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003404 RecordDecl::field_iterator Field = RD->field_begin(),
3405 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003406 if (Field == FieldEnd) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003407 S.Diag(AL.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003408 return;
3409 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003410
David Blaikie40ed2972012-06-06 20:45:41 +00003411 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003412 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003413 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003414 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003415 diag::warn_transparent_union_attribute_floating)
3416 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003417 return;
3418 }
3419
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003420 if (FirstType->isIncompleteType())
3421 return;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003422 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3423 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3424 for (; Field != FieldEnd; ++Field) {
3425 QualType FieldType = Field->getType();
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003426 if (FieldType->isIncompleteType())
3427 return;
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003428 // FIXME: this isn't fully correct; we also need to test whether the
3429 // members of the union would all have the same calling convention as the
3430 // first member of the union. Checking just the size and alignment isn't
3431 // sufficient (consider structs passed on the stack instead of in registers
3432 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003433 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003434 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003435 // Warn if we drop the attribute.
3436 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003437 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003438 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003439 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003440 diag::warn_transparent_union_attribute_field_size_align)
3441 << isSize << Field->getDeclName() << FieldBits;
3442 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003443 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003444 diag::note_transparent_union_first_field_size_align)
3445 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003446 return;
3447 }
3448 }
3449
Michael Han99315932013-01-24 16:46:58 +00003450 RD->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003451 TransparentUnionAttr(AL.getRange(), S.Context,
3452 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003453}
3454
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003455static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003456 // Make sure that there is a string literal as the annotation's single
3457 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003458 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003459 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003460 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003461
3462 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003463 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
3464 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003465 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003466 }
Michael Han99315932013-01-24 16:46:58 +00003467
3468 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003469 AnnotateAttr(AL.getRange(), S.Context, Str,
3470 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003471}
3472
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003473static void handleAlignValueAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003474 const AttributeList &AL) {
3475 S.AddAlignValueAttr(AL.getRange(), D, AL.getArgAsExpr(0),
3476 AL.getAttributeSpellingListIndex());
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003477}
3478
3479void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
3480 unsigned SpellingListIndex) {
3481 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
3482 SourceLocation AttrLoc = AttrRange.getBegin();
3483
3484 QualType T;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003485 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003486 T = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003487 else if (const auto *VD = dyn_cast<ValueDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003488 T = VD->getType();
3489 else
3490 llvm_unreachable("Unknown decl type for align_value");
3491
3492 if (!T->isDependentType() && !T->isAnyPointerType() &&
3493 !T->isReferenceType() && !T->isMemberPointerType()) {
3494 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
3495 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
3496 return;
3497 }
3498
3499 if (!E->isValueDependent()) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003500 llvm::APSInt Alignment;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003501 ExprResult ICE
3502 = VerifyIntegerConstantExpression(E, &Alignment,
3503 diag::err_align_value_attribute_argument_not_int,
3504 /*AllowFold*/ false);
3505 if (ICE.isInvalid())
3506 return;
3507
3508 if (!Alignment.isPowerOf2()) {
3509 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3510 << E->getSourceRange();
3511 return;
3512 }
3513
3514 D->addAttr(::new (Context)
3515 AlignValueAttr(AttrRange, Context, ICE.get(),
3516 SpellingListIndex));
3517 return;
3518 }
3519
3520 // Save dependent expressions in the AST to be instantiated.
3521 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003522}
3523
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003524static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003525 // check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003526 if (AL.getNumArgs() > 1) {
3527 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
3528 << AL.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003529 return;
3530 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003531
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003532 if (AL.getNumArgs() == 0) {
3533 D->addAttr(::new (S.Context) AlignedAttr(AL.getRange(), S.Context,
3534 true, nullptr, AL.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00003535 return;
3536 }
3537
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003538 Expr *E = AL.getArgAsExpr(0);
3539 if (AL.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3540 S.Diag(AL.getEllipsisLoc(),
Richard Smith44c247f2013-02-22 08:32:16 +00003541 diag::err_pack_expansion_without_parameter_packs);
3542 return;
3543 }
3544
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003545 if (!AL.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
Richard Smith44c247f2013-02-22 08:32:16 +00003546 return;
3547
David Majnemer26a1e0e2015-04-07 02:37:09 +00003548 if (E->isValueDependent()) {
3549 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3550 if (!TND->getUnderlyingType()->isDependentType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003551 S.Diag(AL.getLoc(), diag::err_alignment_dependent_typedef_name)
David Majnemer26a1e0e2015-04-07 02:37:09 +00003552 << E->getSourceRange();
3553 return;
3554 }
3555 }
3556 }
3557
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003558 S.AddAlignedAttr(AL.getRange(), D, E, AL.getAttributeSpellingListIndex(),
3559 AL.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003560}
3561
3562void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003563 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003564 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3565 SourceLocation AttrLoc = AttrRange.getBegin();
3566
Richard Smith1dba27c2013-01-29 09:02:09 +00003567 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003568 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003569 // C++11 [dcl.align]p1:
3570 // An alignment-specifier may be applied to a variable or to a class
3571 // data member, but it shall not be applied to a bit-field, a function
3572 // parameter, the formal parameter of a catch clause, or a variable
3573 // declared with the register storage class specifier. An
3574 // alignment-specifier may also be applied to the declaration of a class
3575 // or enumeration type.
3576 // C11 6.7.5/2:
3577 // An alignment attribute shall not be specified in a declaration of
3578 // a typedef, or a bit-field, or a function, or a parameter, or an
3579 // object declared with the register storage-class specifier.
3580 int DiagKind = -1;
3581 if (isa<ParmVarDecl>(D)) {
3582 DiagKind = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003583 } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003584 if (VD->getStorageClass() == SC_Register)
3585 DiagKind = 1;
3586 if (VD->isExceptionVariable())
3587 DiagKind = 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003588 } else if (const auto *FD = dyn_cast<FieldDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003589 if (FD->isBitField())
3590 DiagKind = 3;
3591 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00003592 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00003593 << (TmpAttr.isC11() ? ExpectedVariableOrField
3594 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003595 return;
3596 }
3597 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003598 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00003599 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003600 return;
3601 }
3602 }
3603
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003604 if (E->isTypeDependent() || E->isValueDependent()) {
3605 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003606 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3607 AA->setPackExpansion(IsPackExpansion);
3608 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003609 return;
3610 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003611
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003612 // FIXME: Cache the number on the AL object?
David Majnemer0be6bd02015-07-26 09:02:21 +00003613 llvm::APSInt Alignment;
Douglas Gregore2b37442012-05-04 22:38:52 +00003614 ExprResult ICE
3615 = VerifyIntegerConstantExpression(E, &Alignment,
3616 diag::err_aligned_attribute_argument_not_int,
3617 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003618 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003619 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003620
David Majnemer0be6bd02015-07-26 09:02:21 +00003621 uint64_t AlignVal = Alignment.getZExtValue();
3622
Richard Smith848e1f12013-02-01 08:12:08 +00003623 // C++11 [dcl.align]p2:
3624 // -- if the constant expression evaluates to zero, the alignment
3625 // specifier shall have no effect
3626 // C11 6.7.5p6:
3627 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003628 if (!(TmpAttr.isAlignas() && !Alignment)) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003629 if (!llvm::isPowerOf2_64(AlignVal)) {
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003630 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3631 << E->getSourceRange();
3632 return;
3633 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003634 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003635
David Majnemerabecae72014-02-12 20:36:10 +00003636 // Alignment calculations can wrap around if it's greater than 2**28.
David Majnemer29c69db2015-07-26 01:48:59 +00003637 unsigned MaxValidAlignment =
3638 Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192
3639 : 268435456;
David Majnemer0be6bd02015-07-26 09:02:21 +00003640 if (AlignVal > MaxValidAlignment) {
David Majnemerabecae72014-02-12 20:36:10 +00003641 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
3642 << E->getSourceRange();
3643 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003644 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003645
David Majnemer0be6bd02015-07-26 09:02:21 +00003646 if (Context.getTargetInfo().isTLSSupported()) {
3647 unsigned MaxTLSAlign =
3648 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3649 .getQuantity();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003650 const auto *VD = dyn_cast<VarDecl>(D);
David Majnemer0be6bd02015-07-26 09:02:21 +00003651 if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD &&
3652 VD->getTLSKind() != VarDecl::TLS_None) {
3653 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3654 << (unsigned)AlignVal << VD << MaxTLSAlign;
3655 return;
3656 }
3657 }
3658
Richard Smith44c247f2013-02-22 08:32:16 +00003659 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003660 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00003661 AA->setPackExpansion(IsPackExpansion);
3662 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003663}
3664
Michael Hanaf02bbe2013-02-01 01:19:17 +00003665void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003666 unsigned SpellingListIndex, bool IsPackExpansion) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003667 // FIXME: Cache the number on the AL object if non-dependent?
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003668 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003669 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3670 SpellingListIndex);
3671 AA->setPackExpansion(IsPackExpansion);
3672 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003673}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003674
Richard Smith848e1f12013-02-01 08:12:08 +00003675void Sema::CheckAlignasUnderalignment(Decl *D) {
3676 assert(D->hasAttrs() && "no attributes on decl");
3677
David Majnemer475b25e2015-01-21 10:54:38 +00003678 QualType UnderlyingTy, DiagTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003679 if (const auto *VD = dyn_cast<ValueDecl>(D)) {
David Majnemer475b25e2015-01-21 10:54:38 +00003680 UnderlyingTy = DiagTy = VD->getType();
3681 } else {
3682 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003683 if (const auto *ED = dyn_cast<EnumDecl>(D))
David Majnemer475b25e2015-01-21 10:54:38 +00003684 UnderlyingTy = ED->getIntegerType();
3685 }
3686 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003687 return;
3688
3689 // C++11 [dcl.align]p5, C11 6.7.5/4:
3690 // The combined effect of all alignment attributes in a declaration shall
3691 // not specify an alignment that is less strict than the alignment that
3692 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003693 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003694 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003695 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003696 if (I->isAlignmentDependent())
3697 return;
3698 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003699 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003700 Align = std::max(Align, I->getAlignment(Context));
3701 }
3702
3703 if (AlignasAttr && Align) {
3704 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003705 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003706 if (NaturalAlign > RequestedAlign)
3707 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003708 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003709 }
3710}
3711
David Majnemer2c4e00a2014-01-29 22:07:36 +00003712bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003713 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003714 MSInheritanceAttr::Spelling SemanticSpelling) {
3715 assert(RD->hasDefinition() && "RD has no definition!");
3716
David Majnemer98c9ee22014-02-07 00:43:07 +00003717 // We may not have seen base specifiers or any virtual methods yet. We will
3718 // have to wait until the record is defined to catch any mismatches.
3719 if (!RD->getDefinition()->isCompleteDefinition())
3720 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003721
David Majnemer98c9ee22014-02-07 00:43:07 +00003722 // The unspecified model never matches what a definition could need.
3723 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
3724 return false;
3725
David Majnemer4bb09802014-02-10 19:50:15 +00003726 if (BestCase) {
3727 if (RD->calculateInheritanceModel() == SemanticSpelling)
3728 return false;
3729 } else {
3730 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3731 return false;
3732 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003733
3734 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3735 << 0 /*definition*/;
3736 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3737 << RD->getNameAsString();
3738 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003739}
3740
Alexey Bataevf278eb12015-11-19 10:13:11 +00003741/// parseModeAttrArg - Parses attribute mode string and returns parsed type
3742/// attribute.
3743static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
3744 bool &IntegerMode, bool &ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003745 IntegerMode = true;
3746 ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003747 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003748 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003749 switch (Str[0]) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003750 case 'Q':
3751 DestWidth = 8;
3752 break;
3753 case 'H':
3754 DestWidth = 16;
3755 break;
3756 case 'S':
3757 DestWidth = 32;
3758 break;
3759 case 'D':
3760 DestWidth = 64;
3761 break;
3762 case 'X':
3763 DestWidth = 96;
3764 break;
3765 case 'T':
3766 DestWidth = 128;
3767 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003768 }
3769 if (Str[1] == 'F') {
3770 IntegerMode = false;
3771 } else if (Str[1] == 'C') {
3772 IntegerMode = false;
3773 ComplexMode = true;
3774 } else if (Str[1] != 'I') {
3775 DestWidth = 0;
3776 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003777 break;
3778 case 4:
3779 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3780 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003781 if (Str == "word")
Reid Klecknerf27e7522016-02-01 18:58:24 +00003782 DestWidth = S.Context.getTargetInfo().getRegisterWidth();
Daniel Dunbarafff4342009-10-18 02:09:24 +00003783 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003784 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003785 break;
3786 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003787 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003788 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003789 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003790 case 11:
3791 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003792 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003793 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003794 }
Alexey Bataevf278eb12015-11-19 10:13:11 +00003795}
3796
3797/// handleModeAttr - This attribute modifies the width of a decl with primitive
3798/// type.
3799///
3800/// Despite what would be logical, the mode attribute is a decl attribute, not a
3801/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3802/// HImode, not an intermediate pointer.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003803static void handleModeAttr(Sema &S, Decl *D, const AttributeList &AL) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003804 // This attribute isn't documented, but glibc uses it. It changes
3805 // the width of an int or unsigned int to the specified size.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003806 if (!AL.isArgIdent(0)) {
3807 S.Diag(AL.getLoc(), diag::err_attribute_argument_type) << AL.getName()
Alexey Bataevf278eb12015-11-19 10:13:11 +00003808 << AANT_ArgumentIdentifier;
3809 return;
3810 }
3811
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003812 IdentifierInfo *Name = AL.getArgAsIdent(0)->Ident;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003813
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003814 S.AddModeAttr(AL.getRange(), D, Name, AL.getAttributeSpellingListIndex());
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003815}
3816
3817void Sema::AddModeAttr(SourceRange AttrRange, Decl *D, IdentifierInfo *Name,
3818 unsigned SpellingListIndex, bool InInstantiation) {
3819 StringRef Str = Name->getName();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003820 normalizeName(Str);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003821 SourceLocation AttrLoc = AttrRange.getBegin();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003822
3823 unsigned DestWidth = 0;
3824 bool IntegerMode = true;
3825 bool ComplexMode = false;
3826 llvm::APInt VectorSize(64, 0);
3827 if (Str.size() >= 4 && Str[0] == 'V') {
3828 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
3829 size_t StrSize = Str.size();
3830 size_t VectorStringLength = 0;
3831 while ((VectorStringLength + 1) < StrSize &&
3832 isdigit(Str[VectorStringLength + 1]))
3833 ++VectorStringLength;
3834 if (VectorStringLength &&
3835 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
3836 VectorSize.isPowerOf2()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003837 parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth,
Alexey Bataevf278eb12015-11-19 10:13:11 +00003838 IntegerMode, ComplexMode);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003839 // Avoid duplicate warning from template instantiation.
3840 if (!InInstantiation)
3841 Diag(AttrLoc, diag::warn_vector_mode_deprecated);
Alexey Bataevf278eb12015-11-19 10:13:11 +00003842 } else {
3843 VectorSize = 0;
3844 }
3845 }
3846
3847 if (!VectorSize)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003848 parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode);
3849
3850 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3851 // and friends, at least with glibc.
3852 // FIXME: Make sure floating-point mappings are accurate
3853 // FIXME: Support XF and TF types
3854 if (!DestWidth) {
3855 Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name;
3856 return;
3857 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003858
3859 QualType OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003860 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003861 OldTy = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003862 else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003863 // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'.
3864 // Try to get type from enum declaration, default to int.
3865 OldTy = ED->getIntegerType();
3866 if (OldTy.isNull())
3867 OldTy = Context.IntTy;
3868 } else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003869 OldTy = cast<ValueDecl>(D)->getType();
Eli Friedman4735374e2009-03-03 06:41:03 +00003870
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003871 if (OldTy->isDependentType()) {
3872 D->addAttr(::new (Context)
3873 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
3874 return;
3875 }
3876
Alexey Bataev326057d2015-06-19 07:46:21 +00003877 // Base type can also be a vector type (see PR17453).
3878 // Distinguish between base type and base element type.
3879 QualType OldElemTy = OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003880 if (const auto *VT = OldTy->getAs<VectorType>())
Alexey Bataev326057d2015-06-19 07:46:21 +00003881 OldElemTy = VT->getElementType();
3882
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003883 // GCC allows 'mode' attribute on enumeration types (even incomplete), except
3884 // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete
3885 // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected.
3886 if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) &&
3887 VectorSize.getBoolValue()) {
3888 Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << AttrRange;
3889 return;
3890 }
3891 bool IntegralOrAnyEnumType =
3892 OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>();
3893
3894 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() &&
3895 !IntegralOrAnyEnumType)
3896 Diag(AttrLoc, diag::err_mode_not_primitive);
Eli Friedman4735374e2009-03-03 06:41:03 +00003897 else if (IntegerMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003898 if (!IntegralOrAnyEnumType)
3899 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00003900 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003901 if (!OldElemTy->isComplexType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003902 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00003903 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00003904 if (!OldElemTy->isFloatingType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003905 Diag(AttrLoc, diag::err_mode_wrong_type);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003906 }
3907
Alexey Bataev326057d2015-06-19 07:46:21 +00003908 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003909
3910 if (IntegerMode)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003911 NewElemTy = Context.getIntTypeForBitwidth(DestWidth,
3912 OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003913 else
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003914 NewElemTy = Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003915
Alexey Bataev326057d2015-06-19 07:46:21 +00003916 if (NewElemTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003917 Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003918 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003919 }
3920
Eli Friedman4735374e2009-03-03 06:41:03 +00003921 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003922 NewElemTy = Context.getComplexType(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00003923 }
3924
3925 QualType NewTy = NewElemTy;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003926 if (VectorSize.getBoolValue()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003927 NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(),
3928 VectorType::GenericVector);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003929 } else if (const auto *OldVT = OldTy->getAs<VectorType>()) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003930 // Complex machine mode does not support base vector types.
3931 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003932 Diag(AttrLoc, diag::err_complex_mode_vector_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00003933 return;
3934 }
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003935 unsigned NumElements = Context.getTypeSize(OldElemTy) *
Alexey Bataev326057d2015-06-19 07:46:21 +00003936 OldVT->getNumElements() /
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003937 Context.getTypeSize(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00003938 NewTy =
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003939 Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
Alexey Bataev326057d2015-06-19 07:46:21 +00003940 }
3941
3942 if (NewTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003943 Diag(AttrLoc, diag::err_mode_wrong_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00003944 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003945 }
3946
3947 // Install the new type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003948 if (auto *TD = dyn_cast<TypedefNameDecl>(D))
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003949 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003950 else if (auto *ED = dyn_cast<EnumDecl>(D))
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003951 ED->setIntegerType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003952 else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003953 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003954
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003955 D->addAttr(::new (Context)
3956 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003957}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003958
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003959static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &AL) {
Michael Han99315932013-01-24 16:46:58 +00003960 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003961 NoDebugAttr(AL.getRange(), S.Context,
3962 AL.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003963}
3964
Paul Robinson30e41fb2014-12-15 18:57:28 +00003965AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range,
Paul Robinson080b1f32015-01-13 18:34:56 +00003966 IdentifierInfo *Ident,
Paul Robinson30e41fb2014-12-15 18:57:28 +00003967 unsigned AttrSpellingListIndex) {
3968 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003969 Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00003970 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3971 return nullptr;
3972 }
3973
3974 if (D->hasAttr<AlwaysInlineAttr>())
3975 return nullptr;
3976
3977 return ::new (Context) AlwaysInlineAttr(Range, Context,
3978 AttrSpellingListIndex);
3979}
3980
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003981CommonAttr *Sema::mergeCommonAttr(Decl *D, SourceRange Range,
3982 IdentifierInfo *Ident,
3983 unsigned AttrSpellingListIndex) {
3984 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, Range, Ident))
3985 return nullptr;
3986
3987 return ::new (Context) CommonAttr(Range, Context, AttrSpellingListIndex);
3988}
3989
3990InternalLinkageAttr *
3991Sema::mergeInternalLinkageAttr(Decl *D, SourceRange Range,
3992 IdentifierInfo *Ident,
3993 unsigned AttrSpellingListIndex) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003994 if (const auto *VD = dyn_cast<VarDecl>(D)) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003995 // Attribute applies to Var but not any subclass of it (like ParmVar,
3996 // ImplicitParm or VarTemplateSpecialization).
3997 if (VD->getKind() != Decl::Var) {
3998 Diag(Range.getBegin(), diag::warn_attribute_wrong_decl_type)
3999 << Ident << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
4000 : ExpectedVariableOrFunction);
4001 return nullptr;
4002 }
4003 // Attribute does not apply to non-static local variables.
4004 if (VD->hasLocalStorage()) {
4005 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4006 return nullptr;
4007 }
4008 }
4009
4010 if (checkAttrMutualExclusion<CommonAttr>(*this, D, Range, Ident))
4011 return nullptr;
4012
4013 return ::new (Context)
4014 InternalLinkageAttr(Range, Context, AttrSpellingListIndex);
4015}
4016
Paul Robinson30e41fb2014-12-15 18:57:28 +00004017MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range,
4018 unsigned AttrSpellingListIndex) {
4019 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
4020 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'";
4021 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4022 return nullptr;
4023 }
4024
4025 if (D->hasAttr<MinSizeAttr>())
4026 return nullptr;
4027
4028 return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex);
4029}
4030
4031OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range,
4032 unsigned AttrSpellingListIndex) {
4033 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
4034 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
4035 Diag(Range.getBegin(), diag::note_conflicting_attribute);
4036 D->dropAttr<AlwaysInlineAttr>();
4037 }
4038 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
4039 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
4040 Diag(Range.getBegin(), diag::note_conflicting_attribute);
4041 D->dropAttr<MinSizeAttr>();
4042 }
4043
4044 if (D->hasAttr<OptimizeNoneAttr>())
4045 return nullptr;
4046
4047 return ::new (Context) OptimizeNoneAttr(Range, Context,
4048 AttrSpellingListIndex);
4049}
4050
Paul Robinsonf0674352014-03-31 22:29:15 +00004051static void handleAlwaysInlineAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004052 const AttributeList &AL) {
4053 if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, AL.getRange(),
4054 AL.getName()))
Akira Hatanakac8667622015-11-06 23:56:15 +00004055 return;
4056
Paul Robinson080b1f32015-01-13 18:34:56 +00004057 if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004058 D, AL.getRange(), AL.getName(),
4059 AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004060 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00004061}
4062
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004063static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &AL) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004064 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004065 D, AL.getRange(), AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004066 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00004067}
4068
Paul Robinsonf0674352014-03-31 22:29:15 +00004069static void handleOptimizeNoneAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004070 const AttributeList &AL) {
Paul Robinson080b1f32015-01-13 18:34:56 +00004071 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004072 D, AL.getRange(), AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00004073 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00004074}
4075
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004076static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &AL) {
4077 if (checkAttrMutualExclusion<CUDASharedAttr>(S, D, AL.getRange(),
4078 AL.getName()))
Justin Lebare71b2fa2016-09-30 23:57:34 +00004079 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004080 const auto *VD = cast<VarDecl>(D);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004081 if (!VD->hasGlobalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004082 S.Diag(AL.getLoc(), diag::err_cuda_nonglobal_constant);
Justin Lebare71b2fa2016-09-30 23:57:34 +00004083 return;
4084 }
4085 D->addAttr(::new (S.Context) CUDAConstantAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004086 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Justin Lebare71b2fa2016-09-30 23:57:34 +00004087}
4088
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004089static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &AL) {
4090 if (checkAttrMutualExclusion<CUDAConstantAttr>(S, D, AL.getRange(),
4091 AL.getName()))
Justin Lebar10411012016-09-30 23:57:30 +00004092 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004093 const auto *VD = cast<VarDecl>(D);
Justin Lebar281ce2a2016-10-02 15:24:50 +00004094 // extern __shared__ is only allowed on arrays with no length (e.g.
4095 // "int x[]").
Jonas Hahnfeldee47d8c2018-02-14 16:04:03 +00004096 if (!S.getLangOpts().CUDARelocatableDeviceCode && VD->hasExternalStorage() &&
4097 !isa<IncompleteArrayType>(VD->getType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004098 S.Diag(AL.getLoc(), diag::err_cuda_extern_shared) << VD;
Justin Lebar10411012016-09-30 23:57:30 +00004099 return;
4100 }
Justin Lebaraa370bd2016-10-13 18:45:13 +00004101 if (S.getLangOpts().CUDA && VD->hasLocalStorage() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004102 S.CUDADiagIfHostCode(AL.getLoc(), diag::err_cuda_host_shared)
Justin Lebaraa370bd2016-10-13 18:45:13 +00004103 << S.CurrentCUDATarget())
4104 return;
Justin Lebar10411012016-09-30 23:57:30 +00004105 D->addAttr(::new (S.Context) CUDASharedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004106 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Justin Lebar10411012016-09-30 23:57:30 +00004107}
4108
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004109static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &AL) {
4110 if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, AL.getRange(),
4111 AL.getName()) ||
4112 checkAttrMutualExclusion<CUDAHostAttr>(S, D, AL.getRange(),
4113 AL.getName())) {
Justin Lebar3eaaf862016-01-13 01:07:35 +00004114 return;
4115 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004116 const auto *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00004117 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00004118 SourceRange RTRange = FD->getReturnTypeSourceRange();
4119 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004120 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00004121 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
4122 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00004123 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004124 }
Justin Lebarc66a1062016-01-20 00:26:57 +00004125 if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) {
4126 if (Method->isInstance()) {
4127 S.Diag(Method->getLocStart(), diag::err_kern_is_nonstatic_method)
4128 << Method;
4129 return;
4130 }
4131 S.Diag(Method->getLocStart(), diag::warn_kern_is_method) << Method;
4132 }
4133 // Only warn for "inline" when compiling for host, to cut down on noise.
4134 if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice)
4135 S.Diag(FD->getLocStart(), diag::warn_kern_is_inline) << FD;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004136
Aaron Ballman3aff6332013-12-02 19:30:36 +00004137 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004138 CUDAGlobalAttr(AL.getRange(), S.Context,
4139 AL.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004140}
4141
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004142static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &AL) {
4143 const auto *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00004144 if (!Fn->isInlineSpecified()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004145 S.Diag(AL.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00004146 return;
4147 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004148
Michael Han99315932013-01-24 16:46:58 +00004149 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004150 GNUInlineAttr(AL.getRange(), S.Context,
4151 AL.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00004152}
4153
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004154static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004155 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004156
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004157 // Diagnostic is emitted elsewhere: here we store the (valid) AL
John McCall3882ace2011-01-05 12:14:39 +00004158 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
4159 CallingConv CC;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004160 if (S.CheckCallingConvAttr(AL, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00004161 return;
4162
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004163 if (!isa<ObjCMethodDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004164 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
4165 << AL.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00004166 return;
4167 }
4168
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004169 switch (AL.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004170 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00004171 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004172 FastCallAttr(AL.getRange(), S.Context,
4173 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004174 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004175 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00004176 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004177 StdCallAttr(AL.getRange(), S.Context,
4178 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004179 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004180 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00004181 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004182 ThisCallAttr(AL.getRange(), S.Context,
4183 AL.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00004184 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004185 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00004186 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004187 CDeclAttr(AL.getRange(), S.Context,
4188 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004189 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004190 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00004191 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004192 PascalAttr(AL.getRange(), S.Context,
4193 AL.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00004194 return;
John McCall477f2bb2016-03-03 06:39:32 +00004195 case AttributeList::AT_SwiftCall:
4196 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004197 SwiftCallAttr(AL.getRange(), S.Context,
4198 AL.getAttributeSpellingListIndex()));
John McCall477f2bb2016-03-03 06:39:32 +00004199 return;
Reid Klecknerd7857f02014-10-24 17:42:17 +00004200 case AttributeList::AT_VectorCall:
4201 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004202 VectorCallAttr(AL.getRange(), S.Context,
4203 AL.getAttributeSpellingListIndex()));
Reid Klecknerd7857f02014-10-24 17:42:17 +00004204 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00004205 case AttributeList::AT_MSABI:
4206 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004207 MSABIAttr(AL.getRange(), S.Context,
4208 AL.getAttributeSpellingListIndex()));
Charles Davisb5a214e2013-08-30 04:39:01 +00004209 return;
4210 case AttributeList::AT_SysVABI:
4211 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004212 SysVABIAttr(AL.getRange(), S.Context,
4213 AL.getAttributeSpellingListIndex()));
Charles Davisb5a214e2013-08-30 04:39:01 +00004214 return;
Erich Keane757d3172016-11-02 18:29:35 +00004215 case AttributeList::AT_RegCall:
4216 D->addAttr(::new (S.Context) RegCallAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004217 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Erich Keane757d3172016-11-02 18:29:35 +00004218 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004219 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004220 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004221 switch (CC) {
4222 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004223 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004224 break;
4225 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004226 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004227 break;
4228 default:
4229 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004230 }
4231
Michael Han99315932013-01-24 16:46:58 +00004232 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004233 PcsAttr(AL.getRange(), S.Context, PCS,
4234 AL.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00004235 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004236 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00004237 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00004238 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004239 IntelOclBiccAttr(AL.getRange(), S.Context,
4240 AL.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00004241 return;
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004242 case AttributeList::AT_PreserveMost:
4243 D->addAttr(::new (S.Context) PreserveMostAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004244 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004245 return;
4246 case AttributeList::AT_PreserveAll:
4247 D->addAttr(::new (S.Context) PreserveAllAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004248 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004249 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004250 default:
4251 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00004252 }
4253}
4254
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004255static void handleSuppressAttr(Sema &S, Decl *D, const AttributeList &AL) {
4256 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Matthias Gehre01a63382017-03-27 19:45:24 +00004257 return;
4258
4259 std::vector<StringRef> DiagnosticIdentifiers;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004260 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Matthias Gehre01a63382017-03-27 19:45:24 +00004261 StringRef RuleName;
4262
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004263 if (!S.checkStringLiteralArgumentAttr(AL, I, RuleName, nullptr))
Matthias Gehre01a63382017-03-27 19:45:24 +00004264 return;
4265
4266 // FIXME: Warn if the rule name is unknown. This is tricky because only
4267 // clang-tidy knows about available rules.
4268 DiagnosticIdentifiers.push_back(RuleName);
4269 }
4270 D->addAttr(::new (S.Context) SuppressAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004271 AL.getRange(), S.Context, DiagnosticIdentifiers.data(),
4272 DiagnosticIdentifiers.size(), AL.getAttributeSpellingListIndex()));
Matthias Gehre01a63382017-03-27 19:45:24 +00004273}
4274
Erich Keaneb11ebc52017-09-27 03:20:13 +00004275bool Sema::CheckCallingConvAttr(const AttributeList &Attrs, CallingConv &CC,
Aaron Ballman02df2e02012-12-09 17:45:41 +00004276 const FunctionDecl *FD) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00004277 if (Attrs.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004278 return true;
4279
Erich Keaneb11ebc52017-09-27 03:20:13 +00004280 if (Attrs.hasProcessingCache()) {
4281 CC = (CallingConv) Attrs.getProcessingCache();
John McCall3b5a8f52016-03-03 00:10:03 +00004282 return false;
4283 }
4284
Erich Keaneb11ebc52017-09-27 03:20:13 +00004285 unsigned ReqArgs = Attrs.getKind() == AttributeList::AT_Pcs ? 1 : 0;
4286 if (!checkAttributeNumArgs(*this, Attrs, ReqArgs)) {
4287 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004288 return true;
4289 }
4290
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004291 // TODO: diagnose uses of these conventions on the wrong target.
Erich Keaneb11ebc52017-09-27 03:20:13 +00004292 switch (Attrs.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004293 case AttributeList::AT_CDecl: CC = CC_C; break;
4294 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
4295 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
4296 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
4297 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
John McCall477f2bb2016-03-03 06:39:32 +00004298 case AttributeList::AT_SwiftCall: CC = CC_Swift; break;
Reid Klecknerd7857f02014-10-24 17:42:17 +00004299 case AttributeList::AT_VectorCall: CC = CC_X86VectorCall; break;
Erich Keane757d3172016-11-02 18:29:35 +00004300 case AttributeList::AT_RegCall: CC = CC_X86RegCall; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00004301 case AttributeList::AT_MSABI:
4302 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
Martin Storsjo022e7822017-07-17 20:49:45 +00004303 CC_Win64;
Charles Davisb5a214e2013-08-30 04:39:01 +00004304 break;
4305 case AttributeList::AT_SysVABI:
4306 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
4307 CC_C;
4308 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004309 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00004310 StringRef StrRef;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004311 if (!checkStringLiteralArgumentAttr(Attrs, 0, StrRef)) {
4312 Attrs.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004313 return true;
4314 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004315 if (StrRef == "aapcs") {
4316 CC = CC_AAPCS;
4317 break;
4318 } else if (StrRef == "aapcs-vfp") {
4319 CC = CC_AAPCS_VFP;
4320 break;
4321 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004322
Erich Keaneb11ebc52017-09-27 03:20:13 +00004323 Attrs.setInvalid();
4324 Diag(Attrs.getLoc(), diag::err_invalid_pcs);
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004325 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004326 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00004327 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004328 case AttributeList::AT_PreserveMost: CC = CC_PreserveMost; break;
4329 case AttributeList::AT_PreserveAll: CC = CC_PreserveAll; break;
David Blaikie8a40f702012-01-17 06:56:22 +00004330 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00004331 }
4332
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004333 const TargetInfo &TI = Context.getTargetInfo();
4334 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004335 if (A != TargetInfo::CCCR_OK) {
4336 if (A == TargetInfo::CCCR_Warning)
Erich Keaneb11ebc52017-09-27 03:20:13 +00004337 Diag(Attrs.getLoc(), diag::warn_cconv_ignored) << Attrs.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00004338
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004339 // This convention is not valid for the target. Use the default function or
4340 // method calling convention.
Alexey Bataeva7547182016-05-18 09:06:38 +00004341 bool IsCXXMethod = false, IsVariadic = false;
4342 if (FD) {
4343 IsCXXMethod = FD->isCXXInstanceMember();
4344 IsVariadic = FD->isVariadic();
4345 }
4346 CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004347 }
4348
Erich Keaneb11ebc52017-09-27 03:20:13 +00004349 Attrs.setProcessingCache((unsigned) CC);
John McCall3882ace2011-01-05 12:14:39 +00004350 return false;
4351}
4352
John McCall477f2bb2016-03-03 06:39:32 +00004353/// Pointer-like types in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004354static bool isValidSwiftContextType(QualType Ty) {
4355 if (!Ty->hasPointerRepresentation())
4356 return Ty->isDependentType();
4357 return Ty->getPointeeType().getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004358}
4359
4360/// Pointers and references in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004361static bool isValidSwiftIndirectResultType(QualType Ty) {
4362 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4363 Ty = PtrType->getPointeeType();
4364 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4365 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004366 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004367 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004368 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004369 return Ty.getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004370}
4371
4372/// Pointers and references to pointers in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004373static bool isValidSwiftErrorResultType(QualType Ty) {
4374 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4375 Ty = PtrType->getPointeeType();
4376 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4377 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004378 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004379 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004380 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004381 if (!Ty.getQualifiers().empty())
John McCall477f2bb2016-03-03 06:39:32 +00004382 return false;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004383 return isValidSwiftContextType(Ty);
John McCall477f2bb2016-03-03 06:39:32 +00004384}
4385
Erich Keaneb11ebc52017-09-27 03:20:13 +00004386static void handleParameterABIAttr(Sema &S, Decl *D, const AttributeList &Attrs,
4387 ParameterABI Abi) {
4388 S.AddParameterABIAttr(Attrs.getRange(), D, Abi,
4389 Attrs.getAttributeSpellingListIndex());
John McCall477f2bb2016-03-03 06:39:32 +00004390}
4391
4392void Sema::AddParameterABIAttr(SourceRange range, Decl *D, ParameterABI abi,
4393 unsigned spellingIndex) {
4394
4395 QualType type = cast<ParmVarDecl>(D)->getType();
4396
4397 if (auto existingAttr = D->getAttr<ParameterABIAttr>()) {
4398 if (existingAttr->getABI() != abi) {
4399 Diag(range.getBegin(), diag::err_attributes_are_not_compatible)
4400 << getParameterABISpelling(abi) << existingAttr;
4401 Diag(existingAttr->getLocation(), diag::note_conflicting_attribute);
4402 return;
4403 }
4404 }
4405
4406 switch (abi) {
4407 case ParameterABI::Ordinary:
4408 llvm_unreachable("explicit attribute for ordinary parameter ABI?");
4409
4410 case ParameterABI::SwiftContext:
4411 if (!isValidSwiftContextType(type)) {
4412 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4413 << getParameterABISpelling(abi)
4414 << /*pointer to pointer */ 0 << type;
4415 }
4416 D->addAttr(::new (Context)
4417 SwiftContextAttr(range, Context, spellingIndex));
4418 return;
4419
4420 case ParameterABI::SwiftErrorResult:
4421 if (!isValidSwiftErrorResultType(type)) {
4422 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4423 << getParameterABISpelling(abi)
4424 << /*pointer to pointer */ 1 << type;
4425 }
4426 D->addAttr(::new (Context)
4427 SwiftErrorResultAttr(range, Context, spellingIndex));
4428 return;
4429
4430 case ParameterABI::SwiftIndirectResult:
4431 if (!isValidSwiftIndirectResultType(type)) {
4432 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4433 << getParameterABISpelling(abi)
4434 << /*pointer*/ 0 << type;
4435 }
4436 D->addAttr(::new (Context)
4437 SwiftIndirectResultAttr(range, Context, spellingIndex));
4438 return;
4439 }
4440 llvm_unreachable("bad parameter ABI attribute");
4441}
4442
John McCall3882ace2011-01-05 12:14:39 +00004443/// Checks a regparm attribute, returning true if it is ill-formed and
4444/// otherwise setting numParams to the appropriate value.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004445bool Sema::CheckRegparmAttr(const AttributeList &AL, unsigned &numParams) {
4446 if (AL.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004447 return true;
4448
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004449 if (!checkAttributeNumArgs(*this, AL, 1)) {
4450 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004451 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004452 }
Eli Friedman7044b762009-03-27 21:06:47 +00004453
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004454 uint32_t NP;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004455 Expr *NumParamsExpr = AL.getArgAsExpr(0);
4456 if (!checkUInt32Argument(*this, AL, NumParamsExpr, NP)) {
4457 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004458 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004459 }
4460
Douglas Gregore8bbc122011-09-02 00:18:52 +00004461 if (Context.getTargetInfo().getRegParmMax() == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004462 Diag(AL.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00004463 << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004464 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004465 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004466 }
4467
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004468 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00004469 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004470 Diag(AL.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00004471 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004472 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004473 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004474 }
4475
John McCall3882ace2011-01-05 12:14:39 +00004476 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004477}
4478
Artem Belevichbcec9da2016-06-06 22:54:57 +00004479// Checks whether an argument of launch_bounds attribute is
4480// acceptable, performs implicit conversion to Rvalue, and returns
4481// non-nullptr Expr result on success. Otherwise, it returns nullptr
4482// and may output an error.
4483static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004484 const CUDALaunchBoundsAttr &AL,
Artem Belevichbcec9da2016-06-06 22:54:57 +00004485 const unsigned Idx) {
Artem Belevich7093e402015-04-21 22:55:54 +00004486 if (S.DiagnoseUnexpandedParameterPack(E))
Artem Belevichbcec9da2016-06-06 22:54:57 +00004487 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004488
4489 // Accept template arguments for now as they depend on something else.
4490 // We'll get to check them when they eventually get instantiated.
4491 if (E->isValueDependent())
Artem Belevichbcec9da2016-06-06 22:54:57 +00004492 return E;
Artem Belevich7093e402015-04-21 22:55:54 +00004493
4494 llvm::APSInt I(64);
4495 if (!E->isIntegerConstantExpr(I, S.Context)) {
4496 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004497 << &AL << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
Artem Belevichbcec9da2016-06-06 22:54:57 +00004498 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004499 }
4500 // Make sure we can fit it in 32 bits.
4501 if (!I.isIntN(32)) {
4502 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
4503 << 32 << /* Unsigned */ 1;
Artem Belevichbcec9da2016-06-06 22:54:57 +00004504 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004505 }
4506 if (I < 0)
4507 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004508 << &AL << Idx << E->getSourceRange();
Artem Belevich7093e402015-04-21 22:55:54 +00004509
Artem Belevichbcec9da2016-06-06 22:54:57 +00004510 // We may need to perform implicit conversion of the argument.
4511 InitializedEntity Entity = InitializedEntity::InitializeParameter(
4512 S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false);
4513 ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E);
4514 assert(!ValArg.isInvalid() &&
4515 "Unexpected PerformCopyInitialization() failure.");
4516
4517 return ValArg.getAs<Expr>();
Artem Belevich7093e402015-04-21 22:55:54 +00004518}
4519
4520void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads,
4521 Expr *MinBlocks, unsigned SpellingListIndex) {
4522 CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks,
4523 SpellingListIndex);
Artem Belevichbcec9da2016-06-06 22:54:57 +00004524 MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0);
4525 if (MaxThreads == nullptr)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004526 return;
4527
Artem Belevichbcec9da2016-06-06 22:54:57 +00004528 if (MinBlocks) {
4529 MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1);
4530 if (MinBlocks == nullptr)
4531 return;
4532 }
Artem Belevich7093e402015-04-21 22:55:54 +00004533
4534 D->addAttr(::new (Context) CUDALaunchBoundsAttr(
4535 AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex));
4536}
4537
4538static void handleLaunchBoundsAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004539 const AttributeList &AL) {
4540 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
4541 !checkAttributeAtMostNumArgs(S, AL, 2))
Artem Belevich7093e402015-04-21 22:55:54 +00004542 return;
4543
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004544 S.AddLaunchBoundsAttr(AL.getRange(), D, AL.getArgAsExpr(0),
4545 AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr,
4546 AL.getAttributeSpellingListIndex());
Peter Collingbourne827301e2010-12-12 23:03:07 +00004547}
4548
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004549static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004550 const AttributeList &AL) {
4551 if (!AL.isArgIdent(0)) {
4552 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
4553 << AL.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004554 return;
4555 }
Aaron Ballman00e99962013-08-31 01:11:41 +00004556
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004557 if (!checkAttributeNumArgs(S, AL, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004558 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004559
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004560 IdentifierInfo *ArgumentKind = AL.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004561
4562 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004563 S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type)
4564 << AL.getName() << ExpectedFunctionOrMethod;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004565 return;
4566 }
4567
4568 uint64_t ArgumentIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004569 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 2, AL.getArgAsExpr(1),
Alp Toker601b22c2014-01-21 23:35:24 +00004570 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004571 return;
4572
4573 uint64_t TypeTagIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004574 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 3, AL.getArgAsExpr(2),
Alp Toker601b22c2014-01-21 23:35:24 +00004575 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004576 return;
4577
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004578 bool IsPointer = (AL.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004579 if (IsPointer) {
4580 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00004581 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004582 if (!BufferTy->isPointerType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004583 S.Diag(AL.getLoc(), diag::err_attribute_pointers_only)
4584 << AL.getName() << 0;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004585 }
4586 }
4587
Michael Han99315932013-01-24 16:46:58 +00004588 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004589 ArgumentWithTypeTagAttr(AL.getRange(), S.Context, ArgumentKind,
Michael Han99315932013-01-24 16:46:58 +00004590 ArgumentIdx, TypeTagIdx, IsPointer,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004591 AL.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004592}
4593
4594static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004595 const AttributeList &AL) {
4596 if (!AL.isArgIdent(0)) {
4597 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
4598 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004599 return;
4600 }
Aaron Ballman00e99962013-08-31 01:11:41 +00004601
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004602 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00004603 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004604
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004605 if (!isa<VarDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004606 S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type)
4607 << AL.getName() << ExpectedVariable;
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004608 return;
4609 }
4610
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004611 IdentifierInfo *PointerKind = AL.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00004612 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004613 S.GetTypeFromParser(AL.getMatchingCType(), &MatchingCTypeLoc);
Richard Smithb87c4652013-10-31 21:23:20 +00004614 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004615
Michael Han99315932013-01-24 16:46:58 +00004616 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004617 TypeTagForDatatypeAttr(AL.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00004618 MatchingCTypeLoc,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004619 AL.getLayoutCompatible(),
4620 AL.getMustBeNull(),
4621 AL.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004622}
4623
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004624static void handleXRayLogArgsAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004625 const AttributeList &AL) {
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004626 uint64_t ArgCount;
Dean Michael Berris7456a282017-06-16 03:22:09 +00004627
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004628 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, AL.getArgAsExpr(0),
Dean Michael Berris7456a282017-06-16 03:22:09 +00004629 ArgCount,
4630 true /* AllowImplicitThis*/))
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004631 return;
4632
4633 // ArgCount isn't a parameter index [0;n), it's a count [1;n] - hence + 1.
4634 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004635 XRayLogArgsAttr(AL.getRange(), S.Context, ++ArgCount,
4636 AL.getAttributeSpellingListIndex()));
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004637}
4638
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004639//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004640// Checker-specific attribute handlers.
4641//===----------------------------------------------------------------------===//
4642
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004643static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType QT) {
4644 return QT->isDependentType() || QT->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004645}
4646
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004647static bool isValidSubjectOfNSAttribute(Sema &S, QualType QT) {
4648 return QT->isDependentType() || QT->isObjCObjectPointerType() ||
4649 S.Context.isObjCNSObjectType(QT);
John McCalled433932011-01-25 03:31:58 +00004650}
Eugene Zelenko1ced5092016-02-12 22:53:10 +00004651
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004652static bool isValidSubjectOfCFAttribute(Sema &S, QualType QT) {
4653 return QT->isDependentType() || QT->isPointerType() ||
4654 isValidSubjectOfNSAttribute(S, QT);
John McCalled433932011-01-25 03:31:58 +00004655}
4656
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004657static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &AL) {
4658 S.AddNSConsumedAttr(AL.getRange(), D, AL.getAttributeSpellingListIndex(),
4659 AL.getKind() == AttributeList::AT_NSConsumed,
John McCall3b5a8f52016-03-03 00:10:03 +00004660 /*template instantiation*/ false);
4661}
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004662
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004663void Sema::AddNSConsumedAttr(SourceRange AttrRange, Decl *D,
4664 unsigned SpellingIndex, bool IsNSConsumed,
4665 bool IsTemplateInstantiation) {
4666 const auto *Param = cast<ParmVarDecl>(D);
4667 bool TypeOK;
John McCall3b5a8f52016-03-03 00:10:03 +00004668
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004669 if (IsNSConsumed)
4670 TypeOK = isValidSubjectOfNSAttribute(*this, Param->getType());
4671 else
4672 TypeOK = isValidSubjectOfCFAttribute(*this, Param->getType());
John McCalled433932011-01-25 03:31:58 +00004673
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004674 if (!TypeOK) {
John McCall3b5a8f52016-03-03 00:10:03 +00004675 // These attributes are normally just advisory, but in ARC, ns_consumed
4676 // is significant. Allow non-dependent code to contain inappropriate
4677 // attributes even in ARC, but require template instantiations to be
4678 // set up correctly.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004679 Diag(D->getLocStart(), (IsTemplateInstantiation && IsNSConsumed &&
4680 getLangOpts().ObjCAutoRefCount
4681 ? diag::err_ns_attribute_wrong_parameter_type
4682 : diag::warn_ns_attribute_wrong_parameter_type))
4683 << AttrRange << (IsNSConsumed ? "ns_consumed" : "cf_consumed")
4684 << (IsNSConsumed ? /*objc pointers*/ 0 : /*cf pointers*/ 1);
John McCalled433932011-01-25 03:31:58 +00004685 return;
4686 }
4687
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004688 if (IsNSConsumed)
4689 D->addAttr(::new (Context)
4690 NSConsumedAttr(AttrRange, Context, SpellingIndex));
John McCalled433932011-01-25 03:31:58 +00004691 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004692 D->addAttr(::new (Context)
4693 CFConsumedAttr(AttrRange, Context, SpellingIndex));
John McCalled433932011-01-25 03:31:58 +00004694}
4695
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004696bool Sema::checkNSReturnsRetainedReturnType(SourceLocation Loc, QualType QT) {
4697 if (isValidSubjectOfNSReturnsRetainedAttribute(QT))
John McCall12251882017-07-15 11:06:46 +00004698 return false;
4699
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004700 Diag(Loc, diag::warn_ns_attribute_wrong_return_type)
4701 << "'ns_returns_retained'" << 0 << 0;
John McCall12251882017-07-15 11:06:46 +00004702 return true;
4703}
4704
Chandler Carruthedc2c642011-07-02 00:01:44 +00004705static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004706 const AttributeList &AL) {
4707 QualType ReturnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004708
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004709 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
4710 ReturnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004711 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004712 (AL.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004713 return; // ignore: was handled as a type attribute
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004714 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D))
4715 ReturnType = PD->getType();
4716 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
4717 ReturnType = FD->getReturnType();
4718 else if (const auto *Param = dyn_cast<ParmVarDecl>(D)) {
4719 ReturnType = Param->getType()->getPointeeType();
4720 if (ReturnType.isNull()) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004721 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004722 << AL.getName() << /*pointer-to-CF*/2
4723 << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004724 return;
4725 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004726 } else if (AL.isUsedAsTypeAttr()) {
John McCall12251882017-07-15 11:06:46 +00004727 return;
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004728 } else {
4729 AttributeDeclKind ExpectedDeclKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004730 switch (AL.getKind()) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004731 default: llvm_unreachable("invalid ownership attribute");
4732 case AttributeList::AT_NSReturnsRetained:
4733 case AttributeList::AT_NSReturnsAutoreleased:
4734 case AttributeList::AT_NSReturnsNotRetained:
4735 ExpectedDeclKind = ExpectedFunctionOrMethod;
4736 break;
4737
4738 case AttributeList::AT_CFReturnsRetained:
4739 case AttributeList::AT_CFReturnsNotRetained:
4740 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
4741 break;
4742 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004743 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004744 << AL.getRange() << AL.getName() << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004745 return;
4746 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004747
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004748 bool TypeOK;
4749 bool Cf;
4750 switch (AL.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004751 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004752 case AttributeList::AT_NSReturnsRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004753 TypeOK = isValidSubjectOfNSReturnsRetainedAttribute(ReturnType);
4754 Cf = false;
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004755 break;
4756
4757 case AttributeList::AT_NSReturnsAutoreleased:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004758 case AttributeList::AT_NSReturnsNotRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004759 TypeOK = isValidSubjectOfNSAttribute(S, ReturnType);
4760 Cf = false;
John McCalled433932011-01-25 03:31:58 +00004761 break;
4762
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004763 case AttributeList::AT_CFReturnsRetained:
4764 case AttributeList::AT_CFReturnsNotRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004765 TypeOK = isValidSubjectOfCFAttribute(S, ReturnType);
4766 Cf = true;
John McCalled433932011-01-25 03:31:58 +00004767 break;
4768 }
4769
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004770 if (!TypeOK) {
4771 if (AL.isUsedAsTypeAttr())
John McCall12251882017-07-15 11:06:46 +00004772 return;
4773
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004774 if (isa<ParmVarDecl>(D)) {
4775 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004776 << AL.getName() << /*pointer-to-CF*/2
4777 << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004778 } else {
4779 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
4780 enum : unsigned {
4781 Function,
4782 Method,
4783 Property
4784 } SubjectKind = Function;
4785 if (isa<ObjCMethodDecl>(D))
4786 SubjectKind = Method;
4787 else if (isa<ObjCPropertyDecl>(D))
4788 SubjectKind = Property;
4789 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004790 << AL.getName() << SubjectKind << Cf
4791 << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004792 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004793 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004794 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004795
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004796 switch (AL.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004797 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004798 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004799 case AttributeList::AT_NSReturnsAutoreleased:
Nico Weber462fd1e2015-01-07 23:50:05 +00004800 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004801 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004802 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004803 case AttributeList::AT_CFReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004804 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004805 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004806 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004807 case AttributeList::AT_NSReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004808 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004809 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004810 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004811 case AttributeList::AT_CFReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004812 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004813 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004814 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004815 case AttributeList::AT_NSReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004816 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004817 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004818 return;
4819 };
4820}
4821
John McCallcf166702011-07-22 08:53:00 +00004822static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
Erich Keaneb11ebc52017-09-27 03:20:13 +00004823 const AttributeList &Attrs) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00004824 const int EP_ObjCMethod = 1;
4825 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004826
Erich Keaneb11ebc52017-09-27 03:20:13 +00004827 SourceLocation loc = Attrs.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004828 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004829 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004830 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004831 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004832 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00004833
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004834 if (!resultType->isReferenceType() &&
4835 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004836 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00004837 << SourceRange(loc)
Erich Keaneb11ebc52017-09-27 03:20:13 +00004838 << Attrs.getName()
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004839 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004840 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00004841
4842 // Drop the attribute.
4843 return;
4844 }
4845
Nico Weber462fd1e2015-01-07 23:50:05 +00004846 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(
Erich Keaneb11ebc52017-09-27 03:20:13 +00004847 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004848}
4849
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004850static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
Erich Keaneb11ebc52017-09-27 03:20:13 +00004851 const AttributeList &Attrs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004852 const auto *Method = cast<ObjCMethodDecl>(D);
4853
4854 const DeclContext *DC = Method->getDeclContext();
4855 if (const auto *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004856 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004857 << Attrs.getName() << 0;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004858 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4859 return;
4860 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004861 if (Method->getMethodFamily() == OMF_dealloc) {
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004862 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004863 << Attrs.getName() << 1;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004864 return;
4865 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004866
4867 D->addAttr(::new (S.Context) ObjCRequiresSuperAttr(
4868 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004869}
4870
Aaron Ballmanfb763042013-12-02 18:05:46 +00004871static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004872 const AttributeList &AL) {
4873 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, AL.getRange(),
4874 AL.getName()))
John McCall32f5fe12011-09-30 05:12:12 +00004875 return;
John McCall32f5fe12011-09-30 05:12:12 +00004876
Aaron Ballmanfb763042013-12-02 18:05:46 +00004877 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004878 CFAuditedTransferAttr(AL.getRange(), S.Context,
4879 AL.getAttributeSpellingListIndex()));
Aaron Ballmanfb763042013-12-02 18:05:46 +00004880}
4881
4882static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004883 const AttributeList &AL) {
4884 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, AL.getRange(),
4885 AL.getName()))
Aaron Ballmanfb763042013-12-02 18:05:46 +00004886 return;
4887
4888 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004889 CFUnknownTransferAttr(AL.getRange(), S.Context,
4890 AL.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004891}
4892
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004893static void handleObjCBridgeAttr(Sema &S, Decl *D, const AttributeList &AL) {
4894 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004895
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004896 if (!Parm) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004897 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << AL.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004898 return;
4899 }
John McCall28592582015-02-01 22:34:06 +00004900
4901 // Typedefs only allow objc_bridge(id) and have some additional checking.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004902 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall28592582015-02-01 22:34:06 +00004903 if (!Parm->Ident->isStr("id")) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004904 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_id)
4905 << AL.getName();
John McCall28592582015-02-01 22:34:06 +00004906 return;
4907 }
4908
4909 // Only allow 'cv void *'.
4910 QualType T = TD->getUnderlyingType();
4911 if (!T->isVoidPointerType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004912 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
John McCall28592582015-02-01 22:34:06 +00004913 return;
4914 }
4915 }
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004916
4917 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004918 ObjCBridgeAttr(AL.getRange(), S.Context, Parm->Ident,
4919 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004920}
4921
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004922static void handleObjCBridgeMutableAttr(Sema &S, Decl *D,
4923 const AttributeList &AL) {
4924 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Craig Topperc3ec1492014-05-26 06:22:03 +00004925
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004926 if (!Parm) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004927 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << AL.getName() << 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004928 return;
4929 }
4930
4931 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004932 ObjCBridgeMutableAttr(AL.getRange(), S.Context, Parm->Ident,
4933 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004934}
4935
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004936static void handleObjCBridgeRelatedAttr(Sema &S, Decl *D,
4937 const AttributeList &AL) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004938 IdentifierInfo *RelatedClass =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004939 AL.isArgIdent(0) ? AL.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004940 if (!RelatedClass) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004941 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << AL.getName() << 0;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004942 return;
4943 }
4944 IdentifierInfo *ClassMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004945 AL.getArgAsIdent(1) ? AL.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004946 IdentifierInfo *InstanceMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004947 AL.getArgAsIdent(2) ? AL.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004948 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004949 ObjCBridgeRelatedAttr(AL.getRange(), S.Context, RelatedClass,
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004950 ClassMethod, InstanceMethod,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004951 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004952}
4953
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004954static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004955 const AttributeList &AL) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004956 ObjCInterfaceDecl *IFace;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004957 if (auto *CatDecl = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004958 IFace = CatDecl->getClassInterface();
4959 else
4960 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00004961
4962 if (!IFace)
4963 return;
4964
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00004965 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00004966 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004967 ObjCDesignatedInitializerAttr(AL.getRange(), S.Context,
4968 AL.getAttributeSpellingListIndex()));
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004969}
4970
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004971static void handleObjCRuntimeName(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004972 const AttributeList &AL) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004973 StringRef MetaDataName;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004974 if (!S.checkStringLiteralArgumentAttr(AL, 0, MetaDataName))
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004975 return;
4976 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004977 ObjCRuntimeNameAttr(AL.getRange(), S.Context,
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004978 MetaDataName,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004979 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004980}
4981
Nico Webera6916892016-06-10 18:53:04 +00004982// When a user wants to use objc_boxable with a union or struct
4983// but they don't have access to the declaration (legacy/third-party code)
4984// then they can 'enable' this feature with a typedef:
Alex Denisovfde64952015-06-26 05:28:36 +00004985// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004986static void handleObjCBoxable(Sema &S, Decl *D, const AttributeList &AL) {
Alex Denisovfde64952015-06-26 05:28:36 +00004987 bool notify = false;
4988
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004989 auto *RD = dyn_cast<RecordDecl>(D);
Alex Denisovfde64952015-06-26 05:28:36 +00004990 if (RD && RD->getDefinition()) {
4991 RD = RD->getDefinition();
4992 notify = true;
4993 }
4994
4995 if (RD) {
4996 ObjCBoxableAttr *BoxableAttr = ::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004997 ObjCBoxableAttr(AL.getRange(), S.Context,
4998 AL.getAttributeSpellingListIndex());
Alex Denisovfde64952015-06-26 05:28:36 +00004999 RD->addAttr(BoxableAttr);
5000 if (notify) {
5001 // we need to notify ASTReader/ASTWriter about
5002 // modification of existing declaration
5003 if (ASTMutationListener *L = S.getASTMutationListener())
5004 L->AddedAttributeToRecord(BoxableAttr, RD);
5005 }
5006 }
5007}
5008
Chandler Carruthedc2c642011-07-02 00:01:44 +00005009static void handleObjCOwnershipAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005010 const AttributeList &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00005011 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00005012
Chandler Carruthff4c4f02011-07-01 23:49:12 +00005013 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005014 << AL.getRange() << AL.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00005015}
5016
Chandler Carruthedc2c642011-07-02 00:01:44 +00005017static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005018 const AttributeList &AL) {
5019 const auto *VD = cast<ValueDecl>(D);
5020 QualType QT = VD->getType();
John McCall31168b02011-06-15 23:02:42 +00005021
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005022 if (!QT->isDependentType() &&
5023 !QT->isObjCLifetimeType()) {
5024 S.Diag(AL.getLoc(), diag::err_objc_precise_lifetime_bad_type)
5025 << QT;
John McCall31168b02011-06-15 23:02:42 +00005026 return;
5027 }
5028
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005029 Qualifiers::ObjCLifetime Lifetime = QT.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +00005030
5031 // If we have no lifetime yet, check the lifetime we're presumably
5032 // going to infer.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005033 if (Lifetime == Qualifiers::OCL_None && !QT->isDependentType())
5034 Lifetime = QT->getObjCARCImplicitLifetime();
John McCall31168b02011-06-15 23:02:42 +00005035
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005036 switch (Lifetime) {
John McCall31168b02011-06-15 23:02:42 +00005037 case Qualifiers::OCL_None:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005038 assert(QT->isDependentType() &&
John McCall31168b02011-06-15 23:02:42 +00005039 "didn't infer lifetime for non-dependent type?");
5040 break;
5041
5042 case Qualifiers::OCL_Weak: // meaningful
5043 case Qualifiers::OCL_Strong: // meaningful
5044 break;
5045
5046 case Qualifiers::OCL_ExplicitNone:
5047 case Qualifiers::OCL_Autoreleasing:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005048 S.Diag(AL.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
5049 << (Lifetime == Qualifiers::OCL_Autoreleasing);
John McCall31168b02011-06-15 23:02:42 +00005050 break;
5051 }
5052
Chandler Carruthff4c4f02011-07-01 23:49:12 +00005053 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005054 ObjCPreciseLifetimeAttr(AL.getRange(), S.Context,
5055 AL.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00005056}
5057
Francois Picheta83957a2010-12-19 06:50:37 +00005058//===----------------------------------------------------------------------===//
5059// Microsoft specific attribute handlers.
5060//===----------------------------------------------------------------------===//
5061
Nico Weber88f5ed92016-09-13 18:55:26 +00005062UuidAttr *Sema::mergeUuidAttr(Decl *D, SourceRange Range,
5063 unsigned AttrSpellingListIndex, StringRef Uuid) {
5064 if (const auto *UA = D->getAttr<UuidAttr>()) {
Nico Weberd58c2602016-09-14 01:16:54 +00005065 if (UA->getGuid().equals_lower(Uuid))
Nico Weber88f5ed92016-09-13 18:55:26 +00005066 return nullptr;
5067 Diag(UA->getLocation(), diag::err_mismatched_uuid);
5068 Diag(Range.getBegin(), diag::note_previous_uuid);
5069 D->dropAttr<UuidAttr>();
5070 }
5071
5072 return ::new (Context) UuidAttr(Range, Context, Uuid, AttrSpellingListIndex);
5073}
5074
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005075static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005076 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005077 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
5078 << AL.getName() << AttributeLangSupport::C;
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00005079 return;
5080 }
5081
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005082 StringRef StrRef;
5083 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005084 if (!S.checkStringLiteralArgumentAttr(AL, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00005085 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005086
David Majnemer89085342013-08-09 08:56:20 +00005087 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
5088 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00005089 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
5090 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00005091
Reid Kleckner140c4a72013-05-17 14:04:52 +00005092 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00005093 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005094 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005095 return;
5096 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00005097
David Majnemer89085342013-08-09 08:56:20 +00005098 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00005099 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00005100 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005101 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00005102 return;
5103 }
David Majnemer89085342013-08-09 08:56:20 +00005104 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00005105 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00005106 return;
Francois Pichet7da11662010-12-20 01:41:49 +00005107 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00005108 }
Francois Picheta83957a2010-12-19 06:50:37 +00005109
Nico Weber469891e2017-05-05 17:05:56 +00005110 // FIXME: It'd be nice to also emit a fixit removing uuid(...) (and, if it's
5111 // the only thing in the [] list, the [] too), and add an insertion of
5112 // __declspec(uuid(...)). But sadly, neither the SourceLocs of the commas
5113 // separating attributes nor of the [ and the ] are in the AST.
Nico Weber0a234042017-05-05 17:15:08 +00005114 // Cf "SourceLocations of attribute list delimiters - [[ ... , ... ]] etc"
Nico Weber469891e2017-05-05 17:05:56 +00005115 // on cfe-dev.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005116 if (AL.isMicrosoftAttribute()) // Check for [uuid(...)] spelling.
5117 S.Diag(AL.getLoc(), diag::warn_atl_uuid_deprecated);
Nico Weber469891e2017-05-05 17:05:56 +00005118
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005119 UuidAttr *UA = S.mergeUuidAttr(D, AL.getRange(),
5120 AL.getAttributeSpellingListIndex(), StrRef);
Nico Weber88f5ed92016-09-13 18:55:26 +00005121 if (UA)
5122 D->addAttr(UA);
Charles Davis163855f2010-02-16 18:27:26 +00005123}
5124
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005125static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &AL) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005126 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005127 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
5128 << AL.getName() << AttributeLangSupport::C;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005129 return;
5130 }
5131 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005132 D, AL.getRange(), /*BestCase=*/true,
5133 AL.getAttributeSpellingListIndex(),
5134 (MSInheritanceAttr::Spelling)AL.getSemanticSpelling());
David Majnemer929025d2016-01-26 19:30:26 +00005135 if (IA) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005136 D->addAttr(IA);
David Majnemer929025d2016-01-26 19:30:26 +00005137 S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D));
5138 }
David Majnemer2c4e00a2014-01-29 22:07:36 +00005139}
5140
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005141static void handleDeclspecThreadAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005142 const AttributeList &AL) {
5143 const auto *VD = cast<VarDecl>(D);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005144 if (!S.Context.getTargetInfo().isTLSSupported()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005145 S.Diag(AL.getLoc(), diag::err_thread_unsupported);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005146 return;
5147 }
5148 if (VD->getTSCSpec() != TSCS_unspecified) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005149 S.Diag(AL.getLoc(), diag::err_declspec_thread_on_thread_variable);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005150 return;
5151 }
5152 if (VD->hasLocalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005153 S.Diag(AL.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005154 return;
5155 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005156 D->addAttr(::new (S.Context) ThreadAttr(AL.getRange(), S.Context,
5157 AL.getAttributeSpellingListIndex()));
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005158}
5159
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005160static void handleAbiTagAttr(Sema &S, Decl *D, const AttributeList &AL) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005161 SmallVector<StringRef, 4> Tags;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005162 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005163 StringRef Tag;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005164 if (!S.checkStringLiteralArgumentAttr(AL, I, Tag))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005165 return;
5166 Tags.push_back(Tag);
5167 }
5168
5169 if (const auto *NS = dyn_cast<NamespaceDecl>(D)) {
5170 if (!NS->isInline()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005171 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 0;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005172 return;
5173 }
5174 if (NS->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005175 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 1;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005176 return;
5177 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005178 if (AL.getNumArgs() == 0)
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005179 Tags.push_back(NS->getName());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005180 } else if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005181 return;
5182
5183 // Store tags sorted and without duplicates.
5184 std::sort(Tags.begin(), Tags.end());
5185 Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end());
5186
5187 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005188 AbiTagAttr(AL.getRange(), S.Context, Tags.data(), Tags.size(),
5189 AL.getAttributeSpellingListIndex()));
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005190}
5191
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005192static void handleARMInterruptAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005193 const AttributeList &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005194 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005195 if (AL.getNumArgs() > 1) {
5196 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
5197 << AL.getName() << 1;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005198 return;
5199 }
5200
5201 StringRef Str;
5202 SourceLocation ArgLoc;
5203
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005204 if (AL.getNumArgs() == 0)
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005205 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005206 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005207 return;
5208
5209 ARMInterruptAttr::InterruptType Kind;
5210 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005211 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
5212 << AL.getName() << Str << ArgLoc;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005213 return;
5214 }
5215
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005216 unsigned Index = AL.getAttributeSpellingListIndex();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005217 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005218 ARMInterruptAttr(AL.getLoc(), S.Context, Kind, Index));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005219}
5220
5221static void handleMSP430InterruptAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005222 const AttributeList &AL) {
5223 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005224 return;
5225
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005226 if (!AL.isArgExpr(0)) {
5227 S.Diag(AL.getLoc(), diag::err_attribute_argument_type) << AL.getName()
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005228 << AANT_ArgumentIntegerConstant;
5229 return;
5230 }
5231
5232 // FIXME: Check for decl - it should be void ()(void).
5233
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005234 Expr *NumParamsExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005235 llvm::APSInt NumParams(32);
5236 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005237 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
5238 << AL.getName() << AANT_ArgumentIntegerConstant
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005239 << NumParamsExpr->getSourceRange();
5240 return;
5241 }
5242
5243 unsigned Num = NumParams.getLimitedValue(255);
5244 if ((Num & 1) || Num > 30) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005245 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
5246 << AL.getName() << (int)NumParams.getSExtValue()
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005247 << NumParamsExpr->getSourceRange();
5248 return;
5249 }
5250
Aaron Ballman36a53502014-01-16 13:03:14 +00005251 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005252 MSP430InterruptAttr(AL.getLoc(), S.Context, Num,
5253 AL.getAttributeSpellingListIndex()));
Aaron Ballman36a53502014-01-16 13:03:14 +00005254 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005255}
5256
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005257static void handleMipsInterruptAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005258 const AttributeList &AL) {
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005259 // Only one optional argument permitted.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005260 if (AL.getNumArgs() > 1) {
5261 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
5262 << AL.getName() << 1;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005263 return;
5264 }
5265
5266 StringRef Str;
5267 SourceLocation ArgLoc;
5268
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005269 if (AL.getNumArgs() == 0)
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005270 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005271 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005272 return;
5273
5274 // Semantic checks for a function with the 'interrupt' attribute for MIPS:
5275 // a) Must be a function.
5276 // b) Must have no parameters.
5277 // c) Must have the 'void' return type.
5278 // d) Cannot have the 'mips16' attribute, as that instruction set
5279 // lacks the 'eret' instruction.
5280 // e) The attribute itself must either have no argument or one of the
5281 // valid interrupt types, see [MipsInterruptDocs].
5282
5283 if (!isFunctionOrMethod(D)) {
5284 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5285 << "'interrupt'" << ExpectedFunctionOrMethod;
5286 return;
5287 }
5288
5289 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
5290 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
5291 << 0;
5292 return;
5293 }
5294
5295 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5296 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
5297 << 1;
5298 return;
5299 }
5300
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005301 if (checkAttrMutualExclusion<Mips16Attr>(S, D, AL.getRange(),
5302 AL.getName()))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005303 return;
5304
5305 MipsInterruptAttr::InterruptType Kind;
5306 if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005307 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
5308 << AL.getName() << "'" + std::string(Str) + "'";
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005309 return;
5310 }
5311
5312 D->addAttr(::new (S.Context) MipsInterruptAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005313 AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex()));
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005314}
5315
Alexey Bataevd51e9932016-01-15 04:06:31 +00005316static void handleAnyX86InterruptAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005317 const AttributeList &AL) {
Alexey Bataevd51e9932016-01-15 04:06:31 +00005318 // Semantic checks for a function with the 'interrupt' attribute.
5319 // a) Must be a function.
5320 // b) Must have the 'void' return type.
5321 // c) Must take 1 or 2 arguments.
5322 // d) The 1st argument must be a pointer.
5323 // e) The 2nd argument (if any) must be an unsigned integer.
5324 if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) ||
5325 CXXMethodDecl::isStaticOverloadedOperator(
5326 cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005327 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
5328 << AL.getName() << ExpectedFunctionWithProtoType;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005329 return;
5330 }
5331 // Interrupt handler must have void return type.
5332 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5333 S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(),
5334 diag::err_anyx86_interrupt_attribute)
5335 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5336 ? 0
5337 : 1)
5338 << 0;
5339 return;
5340 }
5341 // Interrupt handler must have 1 or 2 parameters.
5342 unsigned NumParams = getFunctionOrMethodNumParams(D);
5343 if (NumParams < 1 || NumParams > 2) {
5344 S.Diag(D->getLocStart(), diag::err_anyx86_interrupt_attribute)
5345 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5346 ? 0
5347 : 1)
5348 << 1;
5349 return;
5350 }
5351 // The first argument must be a pointer.
5352 if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) {
5353 S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(),
5354 diag::err_anyx86_interrupt_attribute)
5355 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5356 ? 0
5357 : 1)
5358 << 2;
5359 return;
5360 }
5361 // The second argument, if present, must be an unsigned integer.
5362 unsigned TypeSize =
5363 S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64
5364 ? 64
5365 : 32;
5366 if (NumParams == 2 &&
5367 (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() ||
5368 S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) {
5369 S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(),
5370 diag::err_anyx86_interrupt_attribute)
5371 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5372 ? 0
5373 : 1)
5374 << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false);
5375 return;
5376 }
5377 D->addAttr(::new (S.Context) AnyX86InterruptAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005378 AL.getLoc(), S.Context, AL.getAttributeSpellingListIndex()));
Alexey Bataevd51e9932016-01-15 04:06:31 +00005379 D->addAttr(UsedAttr::CreateImplicit(S.Context));
5380}
5381
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005382static void handleAVRInterruptAttr(Sema &S, Decl *D, const AttributeList &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005383 if (!isFunctionOrMethod(D)) {
5384 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5385 << "'interrupt'" << ExpectedFunction;
5386 return;
5387 }
5388
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005389 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005390 return;
5391
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005392 handleSimpleAttribute<AVRInterruptAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005393}
5394
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005395static void handleAVRSignalAttr(Sema &S, Decl *D, const AttributeList &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005396 if (!isFunctionOrMethod(D)) {
5397 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5398 << "'signal'" << ExpectedFunction;
5399 return;
5400 }
5401
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005402 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005403 return;
5404
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005405 handleSimpleAttribute<AVRSignalAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005406}
5407
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005408static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005409 // Dispatch the interrupt attribute based on the current target.
Alexey Bataevd51e9932016-01-15 04:06:31 +00005410 switch (S.Context.getTargetInfo().getTriple().getArch()) {
5411 case llvm::Triple::msp430:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005412 handleMSP430InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005413 break;
5414 case llvm::Triple::mipsel:
5415 case llvm::Triple::mips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005416 handleMipsInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005417 break;
5418 case llvm::Triple::x86:
5419 case llvm::Triple::x86_64:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005420 handleAnyX86InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005421 break;
Dylan McKaye8232d72017-02-08 05:09:26 +00005422 case llvm::Triple::avr:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005423 handleAVRInterruptAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005424 break;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005425 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005426 handleARMInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005427 break;
5428 }
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005429}
5430
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005431static void handleAMDGPUFlatWorkGroupSizeAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005432 const AttributeList &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005433 uint32_t Min = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005434 Expr *MinExpr = AL.getArgAsExpr(0);
5435 if (!checkUInt32Argument(S, AL, MinExpr, Min))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005436 return;
5437
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005438 uint32_t Max = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005439 Expr *MaxExpr = AL.getArgAsExpr(1);
5440 if (!checkUInt32Argument(S, AL, MaxExpr, Max))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005441 return;
5442
5443 if (Min == 0 && Max != 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005444 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid)
5445 << AL.getName() << 0;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005446 return;
5447 }
5448 if (Min > Max) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005449 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid)
5450 << AL.getName() << 1;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005451 return;
5452 }
5453
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005454 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005455 AMDGPUFlatWorkGroupSizeAttr(AL.getLoc(), S.Context, Min, Max,
5456 AL.getAttributeSpellingListIndex()));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005457}
5458
5459static void handleAMDGPUWavesPerEUAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005460 const AttributeList &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005461 uint32_t Min = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005462 Expr *MinExpr = AL.getArgAsExpr(0);
5463 if (!checkUInt32Argument(S, AL, MinExpr, Min))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005464 return;
5465
5466 uint32_t Max = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005467 if (AL.getNumArgs() == 2) {
5468 Expr *MaxExpr = AL.getArgAsExpr(1);
5469 if (!checkUInt32Argument(S, AL, MaxExpr, Max))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005470 return;
5471 }
5472
5473 if (Min == 0 && Max != 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005474 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid)
5475 << AL.getName() << 0;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005476 return;
5477 }
5478 if (Max != 0 && Min > Max) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005479 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid)
5480 << AL.getName() << 1;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005481 return;
5482 }
5483
5484 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005485 AMDGPUWavesPerEUAttr(AL.getLoc(), S.Context, Min, Max,
5486 AL.getAttributeSpellingListIndex()));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005487}
5488
5489static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005490 const AttributeList &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005491 uint32_t NumSGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005492 Expr *NumSGPRExpr = AL.getArgAsExpr(0);
5493 if (!checkUInt32Argument(S, AL, NumSGPRExpr, NumSGPR))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005494 return;
5495
5496 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005497 AMDGPUNumSGPRAttr(AL.getLoc(), S.Context, NumSGPR,
5498 AL.getAttributeSpellingListIndex()));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005499}
5500
5501static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005502 const AttributeList &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005503 uint32_t NumVGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005504 Expr *NumVGPRExpr = AL.getArgAsExpr(0);
5505 if (!checkUInt32Argument(S, AL, NumVGPRExpr, NumVGPR))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005506 return;
5507
5508 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005509 AMDGPUNumVGPRAttr(AL.getLoc(), S.Context, NumVGPR,
5510 AL.getAttributeSpellingListIndex()));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005511}
5512
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005513static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005514 const AttributeList& AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005515 // If we try to apply it to a function pointer, don't warn, but don't
5516 // do anything, either. It doesn't matter anyway, because there's nothing
5517 // special about calling a force_align_arg_pointer function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005518 const auto *VD = dyn_cast<ValueDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005519 if (VD && VD->getType()->isFunctionPointerType())
5520 return;
5521 // Also don't warn on function pointer typedefs.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005522 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005523 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
5524 TD->getUnderlyingType()->isFunctionType()))
5525 return;
5526 // Attribute can only be applied to function types.
5527 if (!isa<FunctionDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005528 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
5529 << AL.getName() << ExpectedFunction;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005530 return;
5531 }
5532
Aaron Ballman36a53502014-01-16 13:03:14 +00005533 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005534 X86ForceAlignArgPointerAttr(AL.getRange(), S.Context,
5535 AL.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005536}
5537
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005538static void handleLayoutVersion(Sema &S, Decl *D, const AttributeList &AL) {
David Majnemercd3ebfe2016-05-23 17:16:12 +00005539 uint32_t Version;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005540 Expr *VersionExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
5541 if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), Version))
David Majnemercd3ebfe2016-05-23 17:16:12 +00005542 return;
5543
5544 // TODO: Investigate what happens with the next major version of MSVC.
5545 if (Version != LangOptions::MSVC2015) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005546 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
5547 << AL.getName() << Version << VersionExpr->getSourceRange();
David Majnemercd3ebfe2016-05-23 17:16:12 +00005548 return;
5549 }
5550
5551 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005552 LayoutVersionAttr(AL.getRange(), S.Context, Version,
5553 AL.getAttributeSpellingListIndex()));
David Majnemercd3ebfe2016-05-23 17:16:12 +00005554}
5555
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005556DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
5557 unsigned AttrSpellingListIndex) {
5558 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00005559 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00005560 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005561 }
5562
5563 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00005564 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005565
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00005566 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005567}
5568
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005569DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
5570 unsigned AttrSpellingListIndex) {
5571 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00005572 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005573 D->dropAttr<DLLImportAttr>();
5574 }
5575
5576 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00005577 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005578
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00005579 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005580}
5581
Hans Wennborge82f19c2014-06-24 23:57:05 +00005582static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00005583 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
5584 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
5585 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored)
5586 << A.getName();
5587 return;
5588 }
5589
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005590 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Hans Wennborg606bd6d2014-11-03 14:24:45 +00005591 if (FD->isInlined() && A.getKind() == AttributeList::AT_DLLImport &&
5592 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
5593 // MinGW doesn't allow dllimport on inline functions.
5594 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
5595 << A.getName();
5596 return;
5597 }
5598 }
5599
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005600 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
Hans Wennborg5869ec42015-09-15 21:05:30 +00005601 if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
5602 MD->getParent()->isLambda()) {
5603 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A.getName();
5604 return;
5605 }
5606 }
5607
Hans Wennborge82f19c2014-06-24 23:57:05 +00005608 unsigned Index = A.getAttributeSpellingListIndex();
5609 Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport
5610 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
5611 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005612 if (NewAttr)
5613 D->addAttr(NewAttr);
5614}
5615
David Majnemer2c4e00a2014-01-29 22:07:36 +00005616MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00005617Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00005618 unsigned AttrSpellingListIndex,
5619 MSInheritanceAttr::Spelling SemanticSpelling) {
5620 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
5621 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00005622 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005623 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
5624 << 1 /*previous declaration*/;
5625 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
5626 D->dropAttr<MSInheritanceAttr>();
5627 }
5628
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005629 auto *RD = cast<CXXRecordDecl>(D);
David Majnemer2c4e00a2014-01-29 22:07:36 +00005630 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00005631 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
5632 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00005633 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005634 }
5635 } else {
5636 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
5637 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
5638 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00005639 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005640 }
5641 if (RD->getDescribedClassTemplate()) {
5642 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
5643 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00005644 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005645 }
5646 }
5647
5648 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00005649 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00005650}
5651
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005652static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005653 // The capability attributes take a single string parameter for the name of
5654 // the capability they represent. The lockable attribute does not take any
5655 // parameters. However, semantically, both attributes represent the same
5656 // concept, and so they use the same semantic attribute. Eventually, the
5657 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00005658 //
Alp Toker958027b2014-07-14 19:42:55 +00005659 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00005660 // literal will be considered a "mutex."
5661 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005662 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005663 if (AL.getKind() == AttributeList::AT_Capability &&
5664 !S.checkStringLiteralArgumentAttr(AL, 0, N, &LiteralLoc))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005665 return;
5666
Aaron Ballman6c810072014-03-05 21:47:13 +00005667 // Currently, there are only two names allowed for a capability: role and
5668 // mutex (case insensitive). Diagnose other capability names.
5669 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
5670 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
5671
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005672 D->addAttr(::new (S.Context) CapabilityAttr(AL.getRange(), S.Context, N,
5673 AL.getAttributeSpellingListIndex()));
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005674}
5675
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005676static void handleAssertCapabilityAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005677 const AttributeList &AL) {
Josh Gaoec1369e2017-08-08 19:44:34 +00005678 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005679 if (!checkLockFunAttrCommon(S, D, AL, Args))
Josh Gaoec1369e2017-08-08 19:44:34 +00005680 return;
5681
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005682 D->addAttr(::new (S.Context) AssertCapabilityAttr(AL.getRange(), S.Context,
Josh Gaoec1369e2017-08-08 19:44:34 +00005683 Args.data(), Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005684 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005685}
5686
5687static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005688 const AttributeList &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005689 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005690 if (!checkLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005691 return;
5692
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005693 D->addAttr(::new (S.Context) AcquireCapabilityAttr(AL.getRange(),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005694 S.Context,
5695 Args.data(), Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005696 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005697}
5698
5699static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005700 const AttributeList &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005701 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005702 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005703 return;
5704
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005705 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(AL.getRange(),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005706 S.Context,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005707 AL.getArgAsExpr(0),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005708 Args.data(),
5709 Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005710 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005711}
5712
5713static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005714 const AttributeList &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005715 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00005716 SmallVector<Expr *, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005717 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005718
Aaron Ballman18d85ae2014-03-20 16:02:49 +00005719 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005720 AL.getRange(), S.Context, Args.data(), Args.size(),
5721 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005722}
5723
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005724static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005725 const AttributeList &AL) {
5726 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005727 return;
5728
5729 // check that all arguments are lockable objects
5730 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005731 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005732 if (Args.empty())
5733 return;
5734
5735 RequiresCapabilityAttr *RCA = ::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005736 RequiresCapabilityAttr(AL.getRange(), S.Context, Args.data(),
5737 Args.size(), AL.getAttributeSpellingListIndex());
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005738
5739 D->addAttr(RCA);
5740}
5741
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005742static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &AL) {
5743 if (const auto *NSD = dyn_cast<NamespaceDecl>(D)) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005744 if (NSD->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005745 S.Diag(AL.getLoc(), diag::warn_deprecated_anonymous_namespace);
Aaron Ballman43f40102014-11-14 22:34:56 +00005746 // Do not want to attach the attribute to the namespace because that will
5747 // cause confusing diagnostic reports for uses of declarations within the
5748 // namespace.
5749 return;
5750 }
5751 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00005752
Manman Renc7890fe2016-03-16 18:50:49 +00005753 // Handle the cases where the attribute has a text message.
5754 StringRef Str, Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005755 if (AL.isArgExpr(0) && AL.getArgAsExpr(0) &&
5756 !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Manman Renc7890fe2016-03-16 18:50:49 +00005757 return;
5758
5759 // Only support a single optional message for Declspec and CXX11.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005760 if (AL.isDeclspecAttribute() || AL.isCXX11Attribute())
5761 checkAttributeAtMostNumArgs(S, AL, 1);
5762 else if (AL.isArgExpr(1) && AL.getArgAsExpr(1) &&
5763 !S.checkStringLiteralArgumentAttr(AL, 1, Replacement))
Manman Renc7890fe2016-03-16 18:50:49 +00005764 return;
5765
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00005766 if (!S.getLangOpts().CPlusPlus14)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005767 if (AL.isCXX11Attribute() &&
5768 !(AL.hasScope() && AL.getScopeName()->isStr("gnu")))
5769 S.Diag(AL.getLoc(), diag::ext_cxx14_attr) << AL.getName();
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00005770
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005771 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005772 DeprecatedAttr(AL.getRange(), S.Context, Str, Replacement,
5773 AL.getAttributeSpellingListIndex()));
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005774}
5775
5776static bool isGlobalVar(const Decl *D) {
5777 if (const auto *S = dyn_cast<VarDecl>(D))
5778 return S->hasGlobalStorage();
5779 return false;
Aaron Ballman43f40102014-11-14 22:34:56 +00005780}
5781
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005782static void handleNoSanitizeAttr(Sema &S, Decl *D, const AttributeList &AL) {
5783 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Peter Collingbourne915df992015-05-15 18:33:32 +00005784 return;
5785
Benjamin Kramer1b582012016-02-13 18:11:49 +00005786 std::vector<StringRef> Sanitizers;
Peter Collingbourne915df992015-05-15 18:33:32 +00005787
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005788 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Peter Collingbourne915df992015-05-15 18:33:32 +00005789 StringRef SanitizerName;
5790 SourceLocation LiteralLoc;
5791
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005792 if (!S.checkStringLiteralArgumentAttr(AL, I, SanitizerName, &LiteralLoc))
Peter Collingbourne915df992015-05-15 18:33:32 +00005793 return;
5794
5795 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0)
5796 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005797 else if (isGlobalVar(D) && SanitizerName != "address")
5798 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005799 << AL.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne915df992015-05-15 18:33:32 +00005800 Sanitizers.push_back(SanitizerName);
5801 }
5802
5803 D->addAttr(::new (S.Context) NoSanitizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005804 AL.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(),
5805 AL.getAttributeSpellingListIndex()));
Peter Collingbourne915df992015-05-15 18:33:32 +00005806}
5807
5808static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005809 const AttributeList &AL) {
5810 StringRef AttrName = AL.getName()->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00005811 normalizeName(AttrName);
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005812 StringRef SanitizerName = llvm::StringSwitch<StringRef>(AttrName)
5813 .Case("no_address_safety_analysis", "address")
5814 .Case("no_sanitize_address", "address")
5815 .Case("no_sanitize_thread", "thread")
5816 .Case("no_sanitize_memory", "memory");
5817 if (isGlobalVar(D) && SanitizerName != "address")
5818 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005819 << AL.getName() << ExpectedFunction;
Peter Collingbourne915df992015-05-15 18:33:32 +00005820 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005821 NoSanitizeAttr(AL.getRange(), S.Context, &SanitizerName, 1,
5822 AL.getAttributeSpellingListIndex()));
Peter Collingbourne915df992015-05-15 18:33:32 +00005823}
5824
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005825static void handleInternalLinkageAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005826 const AttributeList &AL) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005827 if (InternalLinkageAttr *Internal =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005828 S.mergeInternalLinkageAttr(D, AL.getRange(), AL.getName(),
5829 AL.getAttributeSpellingListIndex()))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005830 D->addAttr(Internal);
5831}
5832
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005833static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const AttributeList &AL) {
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005834 if (S.LangOpts.OpenCLVersion != 200)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005835 S.Diag(AL.getLoc(), diag::err_attribute_requires_opencl_version)
5836 << AL.getName() << "2.0" << 0;
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005837 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005838 S.Diag(AL.getLoc(), diag::warn_opencl_attr_deprecated_ignored)
5839 << AL.getName() << "2.0";
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005840}
5841
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005842/// Handles semantic checking for features that are common to all attributes,
5843/// such as checking whether a parameter was properly specified, or the correct
5844/// number of arguments were passed, etc.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005845static bool handleCommonAttributeFeatures(Sema &S, Decl *D,
5846 const AttributeList &AL) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005847 // Several attributes carry different semantics than the parsing requires, so
Alex Lorenz24952fb2017-04-19 15:52:11 +00005848 // those are opted out of the common argument checks.
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005849 //
5850 // We also bail on unknown and ignored attributes because those are handled
5851 // as part of the target-specific handling logic.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005852 if (AL.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005853 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005854 // Check whether the attribute requires specific language extensions to be
5855 // enabled.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005856 if (!AL.diagnoseLangOpts(S))
Aaron Ballman3aff6332013-12-02 19:30:36 +00005857 return true;
Alex Lorenz24952fb2017-04-19 15:52:11 +00005858 // Check whether the attribute appertains to the given subject.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005859 if (!AL.diagnoseAppertainsTo(S, D))
Alex Lorenz24952fb2017-04-19 15:52:11 +00005860 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005861 if (AL.hasCustomParsing())
Alex Lorenz24952fb2017-04-19 15:52:11 +00005862 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005863
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005864 if (AL.getMinArgs() == AL.getMaxArgs()) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005865 // If there are no optional arguments, then checking for the argument count
5866 // is trivial.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005867 if (!checkAttributeNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005868 return true;
5869 } else {
5870 // There are optional arguments, so checking is slightly more involved.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005871 if (AL.getMinArgs() &&
5872 !checkAttributeAtLeastNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005873 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005874 else if (!AL.hasVariadicArg() && AL.getMaxArgs() &&
5875 !checkAttributeAtMostNumArgs(S, AL, AL.getMaxArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005876 return true;
5877 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005878
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005879 return false;
5880}
5881
Xiuli Pan11e13f62016-02-26 03:13:03 +00005882static void handleOpenCLAccessAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005883 const AttributeList &AL) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00005884 if (D->isInvalidDecl())
5885 return;
5886
5887 // Check if there is only one access qualifier.
5888 if (D->hasAttr<OpenCLAccessAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005889 S.Diag(AL.getLoc(), diag::err_opencl_multiple_access_qualifiers)
Xiuli Pan11e13f62016-02-26 03:13:03 +00005890 << D->getSourceRange();
5891 D->setInvalidDecl(true);
5892 return;
5893 }
5894
5895 // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that an
5896 // image object can be read and written.
5897 // OpenCL v2.0 s6.13.6 - A kernel cannot read from and write to the same pipe
5898 // object. Using the read_write (or __read_write) qualifier with the pipe
5899 // qualifier is a compilation error.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005900 if (const auto *PDecl = dyn_cast<ParmVarDecl>(D)) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00005901 const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005902 if (AL.getName()->getName().find("read_write") != StringRef::npos) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00005903 if (S.getLangOpts().OpenCLVersion < 200 || DeclTy->isPipeType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005904 S.Diag(AL.getLoc(), diag::err_opencl_invalid_read_write)
5905 << AL.getName() << PDecl->getType() << DeclTy->isImageType();
Xiuli Pan11e13f62016-02-26 03:13:03 +00005906 D->setInvalidDecl(true);
5907 return;
5908 }
5909 }
5910 }
5911
5912 D->addAttr(::new (S.Context) OpenCLAccessAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005913 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Xiuli Pan11e13f62016-02-26 03:13:03 +00005914}
5915
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005916//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005917// Top Level Sema Entry Points
5918//===----------------------------------------------------------------------===//
5919
Richard Smithf8a75c32013-08-29 00:47:48 +00005920/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
5921/// the attribute applies to decls. If the attribute is a type attribute, just
5922/// silently ignore it if a GNU attribute.
5923static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005924 const AttributeList &AL,
Richard Smithf8a75c32013-08-29 00:47:48 +00005925 bool IncludeCXX11Attributes) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005926 if (AL.isInvalid() || AL.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00005927 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00005928
Richard Smithf8a75c32013-08-29 00:47:48 +00005929 // Ignore C++11 attributes on declarator chunks: they appertain to the type
5930 // instead.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005931 if (AL.isCXX11Attribute() && !IncludeCXX11Attributes)
Richard Smithf8a75c32013-08-29 00:47:48 +00005932 return;
5933
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005934 // Unknown attributes are automatically warned on. Target-specific attributes
5935 // which do not apply to the current target architecture are treated as
5936 // though they were unknown attributes.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005937 if (AL.getKind() == AttributeList::UnknownAttribute ||
5938 !AL.existsInTarget(S.Context.getTargetInfo())) {
5939 S.Diag(AL.getLoc(), AL.isDeclspecAttribute()
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005940 ? diag::warn_unhandled_ms_attribute_ignored
5941 : diag::warn_unknown_attribute_ignored)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005942 << AL.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005943 return;
5944 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005945
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005946 if (handleCommonAttributeFeatures(S, D, AL))
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005947 return;
5948
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005949 switch (AL.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005950 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005951 if (!AL.isStmtAttr()) {
Richard Smith4f902c72016-03-08 00:32:55 +00005952 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005953 assert(AL.isTypeAttr() && "Non-type attribute not handled");
Richard Smith4f902c72016-03-08 00:32:55 +00005954 break;
5955 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005956 S.Diag(AL.getLoc(), diag::err_stmt_attribute_invalid_on_decl)
5957 << AL.getName() << D->getLocation();
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005958 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005959 case AttributeList::AT_Interrupt:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005960 handleInterruptAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005961 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005962 case AttributeList::AT_X86ForceAlignArgPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005963 handleX86ForceAlignArgPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005964 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005965 case AttributeList::AT_DLLExport:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005966 case AttributeList::AT_DLLImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005967 handleDLLAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005968 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005969 case AttributeList::AT_Mips16:
Simon Atanasyan2c87f532017-05-22 12:47:43 +00005970 handleSimpleAttributeWithExclusions<Mips16Attr, MicroMipsAttr,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005971 MipsInterruptAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005972 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005973 case AttributeList::AT_NoMips16:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005974 handleSimpleAttribute<NoMips16Attr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005975 break;
Simon Atanasyan2c87f532017-05-22 12:47:43 +00005976 case AttributeList::AT_MicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005977 handleSimpleAttributeWithExclusions<MicroMipsAttr, Mips16Attr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00005978 break;
5979 case AttributeList::AT_NoMicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005980 handleSimpleAttribute<NoMicroMipsAttr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00005981 break;
Simon Atanasyan1a116db2017-07-20 20:34:18 +00005982 case AttributeList::AT_MipsLongCall:
5983 handleSimpleAttributeWithExclusions<MipsLongCallAttr, MipsShortCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005984 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00005985 break;
5986 case AttributeList::AT_MipsShortCall:
5987 handleSimpleAttributeWithExclusions<MipsShortCallAttr, MipsLongCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005988 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00005989 break;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005990 case AttributeList::AT_AMDGPUFlatWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005991 handleAMDGPUFlatWorkGroupSizeAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005992 break;
5993 case AttributeList::AT_AMDGPUWavesPerEU:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005994 handleAMDGPUWavesPerEUAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005995 break;
5996 case AttributeList::AT_AMDGPUNumSGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005997 handleAMDGPUNumSGPRAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005998 break;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005999 case AttributeList::AT_AMDGPUNumVGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006000 handleAMDGPUNumVGPRAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006001 break;
Dylan McKaye8232d72017-02-08 05:09:26 +00006002 case AttributeList::AT_AVRSignal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006003 handleAVRSignalAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00006004 break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00006005 case AttributeList::AT_IBAction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006006 handleSimpleAttribute<IBActionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006007 break;
6008 case AttributeList::AT_IBOutlet:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006009 handleIBOutlet(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006010 break;
Richard Smith10876ef2013-01-17 01:30:42 +00006011 case AttributeList::AT_IBOutletCollection:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006012 handleIBOutletCollection(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006013 break;
Dmitry Polukhin85eda122016-04-11 07:48:59 +00006014 case AttributeList::AT_IFunc:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006015 handleIFuncAttr(S, D, AL);
Dmitry Polukhin85eda122016-04-11 07:48:59 +00006016 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006017 case AttributeList::AT_Alias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006018 handleAliasAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006019 break;
6020 case AttributeList::AT_Aligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006021 handleAlignedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006022 break;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00006023 case AttributeList::AT_AlignValue:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006024 handleAlignValueAttr(S, D, AL);
Hal Finkel1b0d24e2014-10-02 21:21:25 +00006025 break;
George Burgess IVe3763372016-12-22 02:50:20 +00006026 case AttributeList::AT_AllocSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006027 handleAllocSizeAttr(S, D, AL);
George Burgess IVe3763372016-12-22 02:50:20 +00006028 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006029 case AttributeList::AT_AlwaysInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006030 handleAlwaysInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006031 break;
Erich Keane293a0552018-02-14 00:14:07 +00006032 case AttributeList::AT_Artificial:
Aaron Ballman736c09b2018-02-15 16:28:10 +00006033 handleSimpleAttribute<ArtificialAttr>(S, D, AL);
Erich Keane293a0552018-02-14 00:14:07 +00006034 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006035 case AttributeList::AT_AnalyzerNoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006036 handleAnalyzerNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006037 break;
6038 case AttributeList::AT_TLSModel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006039 handleTLSModelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006040 break;
6041 case AttributeList::AT_Annotate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006042 handleAnnotateAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006043 break;
6044 case AttributeList::AT_Availability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006045 handleAvailabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006046 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006047 case AttributeList::AT_CarriesDependency:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006048 handleDependencyAttr(S, scope, D, AL);
Richard Smithe233fbf2013-01-28 22:42:45 +00006049 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006050 case AttributeList::AT_Common:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006051 handleCommonAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006052 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00006053 case AttributeList::AT_CUDAConstant:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006054 handleConstantAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006055 break;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00006056 case AttributeList::AT_PassObjectSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006057 handlePassObjectSizeAttr(S, D, AL);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00006058 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006059 case AttributeList::AT_Constructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006060 handleConstructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006061 break;
Richard Smith10876ef2013-01-17 01:30:42 +00006062 case AttributeList::AT_CXX11NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006063 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006064 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006065 case AttributeList::AT_Deprecated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006066 handleDeprecatedAttr(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006067 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006068 case AttributeList::AT_Destructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006069 handleDestructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006070 break;
6071 case AttributeList::AT_EnableIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006072 handleEnableIfAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006073 break;
George Burgess IV177399e2017-01-09 04:12:14 +00006074 case AttributeList::AT_DiagnoseIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006075 handleDiagnoseIfAttr(S, D, AL);
George Burgess IV177399e2017-01-09 04:12:14 +00006076 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006077 case AttributeList::AT_ExtVectorType:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006078 handleExtVectorTypeAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006079 break;
Alex Lorenzd5d27e12017-03-01 18:06:25 +00006080 case AttributeList::AT_ExternalSourceSymbol:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006081 handleExternalSourceSymbolAttr(S, D, AL);
Alex Lorenzd5d27e12017-03-01 18:06:25 +00006082 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00006083 case AttributeList::AT_MinSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006084 handleMinSizeAttr(S, D, AL);
Quentin Colombet4e172062012-11-01 23:55:47 +00006085 break;
Paul Robinsonf0674352014-03-31 22:29:15 +00006086 case AttributeList::AT_OptimizeNone:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006087 handleOptimizeNoneAttr(S, D, AL);
Paul Robinsonf0674352014-03-31 22:29:15 +00006088 break;
Alexis Hunt724f14e2014-11-28 00:53:20 +00006089 case AttributeList::AT_FlagEnum:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006090 handleSimpleAttribute<FlagEnumAttr>(S, D, AL);
Alexis Hunt724f14e2014-11-28 00:53:20 +00006091 break;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00006092 case AttributeList::AT_EnumExtensibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006093 handleEnumExtensibilityAttr(S, D, AL);
Akira Hatanaka3c268af2017-03-21 02:23:00 +00006094 break;
Peter Collingbourne41af7c22014-05-20 17:12:51 +00006095 case AttributeList::AT_Flatten:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006096 handleSimpleAttribute<FlattenAttr>(S, D, AL);
Peter Collingbourne41af7c22014-05-20 17:12:51 +00006097 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006098 case AttributeList::AT_Format:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006099 handleFormatAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006100 break;
6101 case AttributeList::AT_FormatArg:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006102 handleFormatArgAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006103 break;
6104 case AttributeList::AT_CUDAGlobal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006105 handleGlobalAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006106 break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00006107 case AttributeList::AT_CUDADevice:
Justin Lebar3eaaf862016-01-13 01:07:35 +00006108 handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006109 AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006110 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00006111 case AttributeList::AT_CUDAHost:
Justin Lebar3eaaf862016-01-13 01:07:35 +00006112 handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006113 AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006114 break;
6115 case AttributeList::AT_GNUInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006116 handleGNUInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006117 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006118 case AttributeList::AT_CUDALaunchBounds:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006119 handleLaunchBoundsAttr(S, D, AL);
Peter Collingbourne827301e2010-12-12 23:03:07 +00006120 break;
David Majnemer631a90b2015-02-04 07:23:21 +00006121 case AttributeList::AT_Restrict:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006122 handleRestrictAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006123 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00006124 case AttributeList::AT_MayAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006125 handleSimpleAttribute<MayAliasAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006126 break;
6127 case AttributeList::AT_Mode:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006128 handleModeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006129 break;
David Majnemer1bf0f8e2015-07-20 22:51:52 +00006130 case AttributeList::AT_NoAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006131 handleSimpleAttribute<NoAliasAttr>(S, D, AL);
David Majnemer1bf0f8e2015-07-20 22:51:52 +00006132 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00006133 case AttributeList::AT_NoCommon:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006134 handleSimpleAttribute<NoCommonAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006135 break;
Peter Collingbourneb4728c12014-05-19 22:14:34 +00006136 case AttributeList::AT_NoSplitStack:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006137 handleSimpleAttribute<NoSplitStackAttr>(S, D, AL);
Peter Collingbourneb4728c12014-05-19 22:14:34 +00006138 break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00006139 case AttributeList::AT_NonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006140 if (auto *PVD = dyn_cast<ParmVarDecl>(D))
6141 handleNonNullAttrParameter(S, PVD, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006142 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006143 handleNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006144 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00006145 case AttributeList::AT_ReturnsNonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006146 handleReturnsNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006147 break;
Akira Hatanaka98a49332017-09-22 00:41:05 +00006148 case AttributeList::AT_NoEscape:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006149 handleNoEscapeAttr(S, D, AL);
Akira Hatanaka98a49332017-09-22 00:41:05 +00006150 break;
Hal Finkelee90a222014-09-26 05:04:30 +00006151 case AttributeList::AT_AssumeAligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006152 handleAssumeAlignedAttr(S, D, AL);
Hal Finkelee90a222014-09-26 05:04:30 +00006153 break;
Erich Keane623efd82017-03-30 21:48:55 +00006154 case AttributeList::AT_AllocAlign:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006155 handleAllocAlignAttr(S, D, AL);
Erich Keane623efd82017-03-30 21:48:55 +00006156 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00006157 case AttributeList::AT_Overloadable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006158 handleSimpleAttribute<OverloadableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006159 break;
6160 case AttributeList::AT_Ownership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006161 handleOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006162 break;
6163 case AttributeList::AT_Cold:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006164 handleColdAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006165 break;
6166 case AttributeList::AT_Hot:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006167 handleHotAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006168 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00006169 case AttributeList::AT_Naked:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006170 handleNakedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006171 break;
6172 case AttributeList::AT_NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006173 handleNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006174 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00006175 case AttributeList::AT_NoThrow:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006176 handleSimpleAttribute<NoThrowAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006177 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00006178 case AttributeList::AT_CUDAShared:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006179 handleSharedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006180 break;
6181 case AttributeList::AT_VecReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006182 handleVecReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006183 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006184 case AttributeList::AT_ObjCOwnership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006185 handleObjCOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006186 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006187 case AttributeList::AT_ObjCPreciseLifetime:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006188 handleObjCPreciseLifetimeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006189 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006190 case AttributeList::AT_ObjCReturnsInnerPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006191 handleObjCReturnsInnerPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006192 break;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00006193 case AttributeList::AT_ObjCRequiresSuper:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006194 handleObjCRequiresSuperAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006195 break;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00006196 case AttributeList::AT_ObjCBridge:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006197 handleObjCBridgeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006198 break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00006199 case AttributeList::AT_ObjCBridgeMutable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006200 handleObjCBridgeMutableAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006201 break;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00006202 case AttributeList::AT_ObjCBridgeRelated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006203 handleObjCBridgeRelatedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006204 break;
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00006205 case AttributeList::AT_ObjCDesignatedInitializer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006206 handleObjCDesignatedInitializer(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006207 break;
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00006208 case AttributeList::AT_ObjCRuntimeName:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006209 handleObjCRuntimeName(S, D, AL);
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00006210 break;
Douglas Gregor24ae22c2016-04-01 23:23:52 +00006211 case AttributeList::AT_ObjCRuntimeVisible:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006212 handleSimpleAttribute<ObjCRuntimeVisibleAttr>(S, D, AL);
Douglas Gregor24ae22c2016-04-01 23:23:52 +00006213 break;
Alex Denisovfde64952015-06-26 05:28:36 +00006214 case AttributeList::AT_ObjCBoxable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006215 handleObjCBoxable(S, D, AL);
Alex Denisovfde64952015-06-26 05:28:36 +00006216 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006217 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006218 handleCFAuditedTransferAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006219 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006220 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006221 handleCFUnknownTransferAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006222 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006223 case AttributeList::AT_CFConsumed:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006224 case AttributeList::AT_NSConsumed:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006225 handleNSConsumedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006226 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006227 case AttributeList::AT_NSConsumesSelf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006228 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006229 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006230 case AttributeList::AT_NSReturnsAutoreleased:
6231 case AttributeList::AT_NSReturnsNotRetained:
6232 case AttributeList::AT_CFReturnsNotRetained:
6233 case AttributeList::AT_NSReturnsRetained:
6234 case AttributeList::AT_CFReturnsRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006235 handleNSReturnsRetainedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006236 break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00006237 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006238 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006239 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006240 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006241 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006242 break;
Xiuli Panbe6da4b2017-05-04 07:31:20 +00006243 case AttributeList::AT_OpenCLIntelReqdSubGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006244 handleSubGroupSize(S, D, AL);
Xiuli Panbe6da4b2017-05-04 07:31:20 +00006245 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00006246 case AttributeList::AT_VecTypeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006247 handleVecTypeHint(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006248 break;
Eric Fiselier341e8252016-09-02 18:53:31 +00006249 case AttributeList::AT_RequireConstantInit:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006250 handleSimpleAttribute<RequireConstantInitAttr>(S, D, AL);
Eric Fiselier341e8252016-09-02 18:53:31 +00006251 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006252 case AttributeList::AT_InitPriority:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006253 handleInitPriorityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006254 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006255 case AttributeList::AT_Packed:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006256 handlePackedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006257 break;
6258 case AttributeList::AT_Section:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006259 handleSectionAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006260 break;
Eric Christopher11acf732015-06-12 01:35:52 +00006261 case AttributeList::AT_Target:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006262 handleTargetAttr(S, D, AL);
Eric Christopher11acf732015-06-12 01:35:52 +00006263 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006264 case AttributeList::AT_Unavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006265 handleAttrWithMessage<UnavailableAttr>(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006266 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006267 case AttributeList::AT_ArcWeakrefUnavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006268 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006269 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006270 case AttributeList::AT_ObjCRootClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006271 handleSimpleAttribute<ObjCRootClassAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006272 break;
Alex Lorenza8c44ba2016-10-28 10:25:10 +00006273 case AttributeList::AT_ObjCSubclassingRestricted:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006274 handleSimpleAttribute<ObjCSubclassingRestrictedAttr>(S, D, AL);
Alex Lorenza8c44ba2016-10-28 10:25:10 +00006275 break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00006276 case AttributeList::AT_ObjCExplicitProtocolImpl:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006277 handleObjCSuppresProtocolAttr(S, D, AL);
Ted Kremenek28eace62013-11-23 01:01:34 +00006278 break;
6279 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006280 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006281 break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00006282 case AttributeList::AT_Unused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006283 handleUnusedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006284 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006285 case AttributeList::AT_ReturnsTwice:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006286 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006287 break;
Akira Hatanakac8667622015-11-06 23:56:15 +00006288 case AttributeList::AT_NotTailCalled:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006289 handleNotTailCalledAttr(S, D, AL);
Akira Hatanakac8667622015-11-06 23:56:15 +00006290 break;
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00006291 case AttributeList::AT_DisableTailCalls:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006292 handleDisableTailCallsAttr(S, D, AL);
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00006293 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006294 case AttributeList::AT_Used:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006295 handleUsedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006296 break;
John McCalld041a9b2013-02-20 01:54:26 +00006297 case AttributeList::AT_Visibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006298 handleVisibilityAttr(S, D, AL, false);
John McCalld041a9b2013-02-20 01:54:26 +00006299 break;
6300 case AttributeList::AT_TypeVisibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006301 handleVisibilityAttr(S, D, AL, true);
John McCalld041a9b2013-02-20 01:54:26 +00006302 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00006303 case AttributeList::AT_WarnUnused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006304 handleSimpleAttribute<WarnUnusedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006305 break;
6306 case AttributeList::AT_WarnUnusedResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006307 handleWarnUnusedResult(S, D, AL);
Chris Lattner237f2752009-02-14 07:37:35 +00006308 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00006309 case AttributeList::AT_Weak:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006310 handleSimpleAttribute<WeakAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006311 break;
6312 case AttributeList::AT_WeakRef:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006313 handleWeakRefAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006314 break;
6315 case AttributeList::AT_WeakImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006316 handleWeakImportAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006317 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006318 case AttributeList::AT_TransparentUnion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006319 handleTransparentUnionAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006320 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006321 case AttributeList::AT_ObjCException:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006322 handleSimpleAttribute<ObjCExceptionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006323 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006324 case AttributeList::AT_ObjCMethodFamily:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006325 handleObjCMethodFamilyAttr(S, D, AL);
John McCall86bc21f2011-03-02 11:33:24 +00006326 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006327 case AttributeList::AT_ObjCNSObject:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006328 handleObjCNSObject(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006329 break;
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00006330 case AttributeList::AT_ObjCIndependentClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006331 handleObjCIndependentClass(S, D, AL);
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00006332 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006333 case AttributeList::AT_Blocks:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006334 handleBlocksAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006335 break;
6336 case AttributeList::AT_Sentinel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006337 handleSentinelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006338 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00006339 case AttributeList::AT_Const:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006340 handleSimpleAttribute<ConstAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006341 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00006342 case AttributeList::AT_Pure:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006343 handleSimpleAttribute<PureAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006344 break;
6345 case AttributeList::AT_Cleanup:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006346 handleCleanupAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006347 break;
6348 case AttributeList::AT_NoDebug:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006349 handleNoDebugAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006350 break;
Aaron Ballman7c19ab12014-02-22 16:59:24 +00006351 case AttributeList::AT_NoDuplicate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006352 handleSimpleAttribute<NoDuplicateAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006353 break;
Yaxun Liu7d07ae72016-11-01 18:45:32 +00006354 case AttributeList::AT_Convergent:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006355 handleSimpleAttribute<ConvergentAttr>(S, D, AL);
Yaxun Liu7d07ae72016-11-01 18:45:32 +00006356 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00006357 case AttributeList::AT_NoInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006358 handleSimpleAttribute<NoInlineAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006359 break;
6360 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006361 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006362 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006363 case AttributeList::AT_StdCall:
6364 case AttributeList::AT_CDecl:
6365 case AttributeList::AT_FastCall:
6366 case AttributeList::AT_ThisCall:
6367 case AttributeList::AT_Pascal:
Erich Keane757d3172016-11-02 18:29:35 +00006368 case AttributeList::AT_RegCall:
John McCall477f2bb2016-03-03 06:39:32 +00006369 case AttributeList::AT_SwiftCall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00006370 case AttributeList::AT_VectorCall:
Charles Davisb5a214e2013-08-30 04:39:01 +00006371 case AttributeList::AT_MSABI:
6372 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006373 case AttributeList::AT_Pcs:
Guy Benyeif0a014b2012-12-25 08:53:55 +00006374 case AttributeList::AT_IntelOclBicc:
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00006375 case AttributeList::AT_PreserveMost:
6376 case AttributeList::AT_PreserveAll:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006377 handleCallConvAttr(S, D, AL);
John McCallab26cfa2010-02-05 21:31:56 +00006378 break;
Matthias Gehre01a63382017-03-27 19:45:24 +00006379 case AttributeList::AT_Suppress:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006380 handleSuppressAttr(S, D, AL);
Matthias Gehre01a63382017-03-27 19:45:24 +00006381 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006382 case AttributeList::AT_OpenCLKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006383 handleSimpleAttribute<OpenCLKernelAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006384 break;
Xiuli Pan11e13f62016-02-26 03:13:03 +00006385 case AttributeList::AT_OpenCLAccess:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006386 handleOpenCLAccessAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006387 break;
Anastasia Stulovafde76222016-04-01 16:05:09 +00006388 case AttributeList::AT_OpenCLNoSVM:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006389 handleOpenCLNoSVMAttr(S, D, AL);
Anastasia Stulovafde76222016-04-01 16:05:09 +00006390 break;
John McCall477f2bb2016-03-03 06:39:32 +00006391 case AttributeList::AT_SwiftContext:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006392 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftContext);
John McCall477f2bb2016-03-03 06:39:32 +00006393 break;
6394 case AttributeList::AT_SwiftErrorResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006395 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftErrorResult);
John McCall477f2bb2016-03-03 06:39:32 +00006396 break;
6397 case AttributeList::AT_SwiftIndirectResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006398 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftIndirectResult);
John McCall477f2bb2016-03-03 06:39:32 +00006399 break;
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00006400 case AttributeList::AT_InternalLinkage:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006401 handleInternalLinkageAttr(S, D, AL);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00006402 break;
Peter Collingbourne3afb2662016-04-28 17:09:37 +00006403 case AttributeList::AT_LTOVisibilityPublic:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006404 handleSimpleAttribute<LTOVisibilityPublicAttr>(S, D, AL);
Peter Collingbourne3afb2662016-04-28 17:09:37 +00006405 break;
John McCall8d32c052012-05-22 21:28:12 +00006406
6407 // Microsoft attributes:
David Majnemercd3ebfe2016-05-23 17:16:12 +00006408 case AttributeList::AT_EmptyBases:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006409 handleSimpleAttribute<EmptyBasesAttr>(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00006410 break;
6411 case AttributeList::AT_LayoutVersion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006412 handleLayoutVersion(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00006413 break;
Akira Hatanaka02914dc2018-02-05 20:23:22 +00006414 case AttributeList::AT_TrivialABI:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006415 handleSimpleAttribute<TrivialABIAttr>(S, D, AL);
Akira Hatanaka02914dc2018-02-05 20:23:22 +00006416 break;
David Majnemer8ab003a2015-02-02 19:30:52 +00006417 case AttributeList::AT_MSNoVTable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006418 handleSimpleAttribute<MSNoVTableAttr>(S, D, AL);
David Majnemer129f4172015-02-02 10:22:20 +00006419 break;
David Majnemer8ab003a2015-02-02 19:30:52 +00006420 case AttributeList::AT_MSStruct:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006421 handleSimpleAttribute<MSStructAttr>(S, D, AL);
John McCall8d32c052012-05-22 21:28:12 +00006422 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006423 case AttributeList::AT_Uuid:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006424 handleUuidAttr(S, D, AL);
Francois Picheta83957a2010-12-19 06:50:37 +00006425 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00006426 case AttributeList::AT_MSInheritance:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006427 handleMSInheritanceAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006428 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00006429 case AttributeList::AT_SelectAny:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006430 handleSimpleAttribute<SelectAnyAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006431 break;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00006432 case AttributeList::AT_Thread:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006433 handleDeclspecThreadAttr(S, D, AL);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00006434 break;
David Majnemercd3ebfe2016-05-23 17:16:12 +00006435
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00006436 case AttributeList::AT_AbiTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006437 handleAbiTagAttr(S, D, AL);
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00006438 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006439
6440 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00006441 case AttributeList::AT_AssertExclusiveLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006442 handleAssertExclusiveLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00006443 break;
6444 case AttributeList::AT_AssertSharedLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006445 handleAssertSharedLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00006446 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006447 case AttributeList::AT_GuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006448 handleSimpleAttribute<GuardedVarAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006449 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006450 case AttributeList::AT_PtGuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006451 handlePtGuardedVarAttr(S, D, AL);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006452 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006453 case AttributeList::AT_ScopedLockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006454 handleSimpleAttribute<ScopedLockableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006455 break;
Peter Collingbourne915df992015-05-15 18:33:32 +00006456 case AttributeList::AT_NoSanitize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006457 handleNoSanitizeAttr(S, D, AL);
Peter Collingbourne915df992015-05-15 18:33:32 +00006458 break;
6459 case AttributeList::AT_NoSanitizeSpecific:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006460 handleNoSanitizeSpecificAttr(S, D, AL);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00006461 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006462 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006463 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, AL);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00006464 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006465 case AttributeList::AT_GuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006466 handleGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006467 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006468 case AttributeList::AT_PtGuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006469 handlePtGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006470 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006471 case AttributeList::AT_ExclusiveTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006472 handleExclusiveTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006473 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006474 case AttributeList::AT_LockReturned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006475 handleLockReturnedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006476 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006477 case AttributeList::AT_LocksExcluded:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006478 handleLocksExcludedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006479 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006480 case AttributeList::AT_SharedTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006481 handleSharedTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006482 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006483 case AttributeList::AT_AcquiredBefore:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006484 handleAcquiredBeforeAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006485 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006486 case AttributeList::AT_AcquiredAfter:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006487 handleAcquiredAfterAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006488 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006489
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006490 // Capability analysis attributes.
6491 case AttributeList::AT_Capability:
6492 case AttributeList::AT_Lockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006493 handleCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006494 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006495 case AttributeList::AT_RequiresCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006496 handleRequiresCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006497 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006498
6499 case AttributeList::AT_AssertCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006500 handleAssertCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006501 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006502 case AttributeList::AT_AcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006503 handleAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006504 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006505 case AttributeList::AT_ReleaseCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006506 handleReleaseCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006507 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006508 case AttributeList::AT_TryAcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006509 handleTryAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006510 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006511
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00006512 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00006513 case AttributeList::AT_Consumable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006514 handleConsumableAttr(S, D, AL);
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00006515 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00006516 case AttributeList::AT_ConsumableAutoCast:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006517 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006518 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00006519 case AttributeList::AT_ConsumableSetOnRead:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006520 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006521 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00006522 case AttributeList::AT_CallableWhen:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006523 handleCallableWhenAttr(S, D, AL);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00006524 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00006525 case AttributeList::AT_ParamTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006526 handleParamTypestateAttr(S, D, AL);
DeLesley Hutchins69391772013-10-17 23:23:53 +00006527 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00006528 case AttributeList::AT_ReturnTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006529 handleReturnTypestateAttr(S, D, AL);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00006530 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00006531 case AttributeList::AT_SetTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006532 handleSetTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00006533 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00006534 case AttributeList::AT_TestTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006535 handleTestTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00006536 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00006537
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006538 // Type safety attributes.
6539 case AttributeList::AT_ArgumentWithTypeTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006540 handleArgumentWithTypeTagAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006541 break;
6542 case AttributeList::AT_TypeTagForDatatype:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006543 handleTypeTagForDatatypeAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006544 break;
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00006545 case AttributeList::AT_AnyX86NoCallerSavedRegisters:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006546 handleNoCallerSavedRegsAttr(S, D, AL);
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00006547 break;
Pirama Arumuga Nainare5d2d712016-06-10 21:51:18 +00006548 case AttributeList::AT_RenderScriptKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006549 handleSimpleAttribute<RenderScriptKernelAttr>(S, D, AL);
Pirama Arumuga Nainar8b788d02016-06-09 23:34:20 +00006550 break;
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00006551 // XRay attributes.
6552 case AttributeList::AT_XRayInstrument:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006553 handleSimpleAttribute<XRayInstrumentAttr>(S, D, AL);
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00006554 break;
Dean Michael Berris418da3f2017-03-06 07:08:21 +00006555 case AttributeList::AT_XRayLogArgs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006556 handleXRayLogArgsAttr(S, D, AL);
Dean Michael Berris418da3f2017-03-06 07:08:21 +00006557 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006558 }
6559}
6560
6561/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
6562/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00006563void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00006564 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00006565 bool IncludeCXX11Attributes) {
6566 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00006567 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00006568
Joey Gouly2cd9db12013-12-13 16:15:28 +00006569 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00006570 // GCC accepts
6571 // static int a9 __attribute__((weakref));
6572 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00006573 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00006574 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
6575 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00006576 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00006577 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006578 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00006579
Aaron Ballmanbe243a72014-12-04 22:45:31 +00006580 // FIXME: We should be able to handle this in TableGen as well. It would be
6581 // good to have a way to specify "these attributes must appear as a group",
6582 // for these. Additionally, it would be good to have a way to specify "these
6583 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00006584 if (!D->hasAttr<OpenCLKernelAttr>()) {
6585 // These attributes cannot be applied to a non-kernel function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006586 if (const auto *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00006587 // FIXME: This emits a different error message than
6588 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00006589 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006590 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006591 } else if (const auto *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00006592 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006593 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006594 } else if (const auto *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00006595 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006596 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006597 } else if (const auto *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006598 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6599 << A << ExpectedKernelFunction;
6600 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006601 } else if (const auto *A = D->getAttr<AMDGPUWavesPerEUAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00006602 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6603 << A << ExpectedKernelFunction;
6604 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006605 } else if (const auto *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00006606 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6607 << A << ExpectedKernelFunction;
6608 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006609 } else if (const auto *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006610 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6611 << A << ExpectedKernelFunction;
6612 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006613 } else if (const auto *A = D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00006614 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
6615 D->setInvalidDecl();
Joey Gouly2cd9db12013-12-13 16:15:28 +00006616 }
6617 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006618}
6619
Hiroshi Inoue939d9322017-06-30 05:40:31 +00006620// Helper for delayed processing TransparentUnion attribute.
Erich Keane2fe684b2017-02-28 20:44:39 +00006621void Sema::ProcessDeclAttributeDelayed(Decl *D, const AttributeList *AttrList) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006622 for (const AttributeList *AL = AttrList; AL; AL = AL->getNext())
6623 if (AL->getKind() == AttributeList::AT_TransparentUnion) {
6624 handleTransparentUnionAttr(*this, D, *AL);
Erich Keane2fe684b2017-02-28 20:44:39 +00006625 break;
6626 }
6627}
6628
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006629// Annotation attributes are the only attributes allowed after an access
6630// specifier.
6631bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
6632 const AttributeList *AttrList) {
6633 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006634 if (l->getKind() == AttributeList::AT_Annotate) {
David Majnemer706f3152014-12-14 01:05:01 +00006635 ProcessDeclAttribute(*this, nullptr, ASDecl, *l, l->isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006636 } else {
6637 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
6638 return true;
6639 }
6640 }
6641
6642 return false;
6643}
6644
John McCall42856de2011-10-01 05:17:03 +00006645/// checkUnusedDeclAttributes - Check a list of attributes to see if it
6646/// contains any decl attributes that we should warn about.
6647static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
6648 for ( ; A; A = A->getNext()) {
6649 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00006650 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00006651 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
6652
6653 if (A->getKind() == AttributeList::UnknownAttribute) {
6654 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
6655 << A->getName() << A->getRange();
6656 } else {
6657 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
6658 << A->getName() << A->getRange();
6659 }
6660 }
6661}
6662
6663/// checkUnusedDeclAttributes - Given a declarator which is not being
6664/// used to build a declaration, complain about any decl attributes
6665/// which might be lying around on it.
6666void Sema::checkUnusedDeclAttributes(Declarator &D) {
6667 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
6668 ::checkUnusedDeclAttributes(*this, D.getAttributes());
6669 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
6670 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
6671}
6672
Ryan Flynn7d470f32009-07-30 03:15:39 +00006673/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00006674/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00006675NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
6676 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00006677 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00006678 NamedDecl *NewD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006679 if (auto *FD = dyn_cast<FunctionDecl>(ND)) {
Alexander Kornienko061900f2015-12-03 11:37:28 +00006680 FunctionDecl *NewFD;
6681 // FIXME: Missing call to CheckFunctionDeclaration().
Eli Friedmance3e2c82011-09-07 04:05:06 +00006682 // FIXME: Mangling?
6683 // FIXME: Is the qualifier info correct?
6684 // FIXME: Is the DeclContext correct?
Alexander Kornienko061900f2015-12-03 11:37:28 +00006685 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
6686 Loc, Loc, DeclarationName(II),
6687 FD->getType(), FD->getTypeSourceInfo(),
6688 SC_None, false/*isInlineSpecified*/,
6689 FD->hasPrototype(),
6690 false/*isConstexprSpecified*/);
Eli Friedmance3e2c82011-09-07 04:05:06 +00006691 NewD = NewFD;
6692
6693 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00006694 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00006695
6696 // Fake up parameter variables; they are declared as if this were
6697 // a typedef.
6698 QualType FDTy = FD->getType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006699 if (const auto *FT = FDTy->getAs<FunctionProtoType>()) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00006700 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00006701 for (const auto &AI : FT->param_types()) {
6702 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00006703 Param->setScopeInfo(0, Params.size());
6704 Params.push_back(Param);
6705 }
David Blaikie9c70e042011-09-21 18:16:56 +00006706 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00006707 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006708 } else if (auto *VD = dyn_cast<VarDecl>(ND)) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00006709 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00006710 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00006711 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00006712 VD->getStorageClass());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006713 if (VD->getQualifier())
6714 cast<VarDecl>(NewD)->setQualifierInfo(VD->getQualifierLoc());
Ryan Flynn7d470f32009-07-30 03:15:39 +00006715 }
6716 return NewD;
6717}
6718
James Dennett634962f2012-06-14 21:40:34 +00006719/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00006720/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00006721void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00006722 if (W.getUsed()) return; // only do this once
6723 W.setUsed(true);
6724 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
6725 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00006726 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00006727 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
6728 W.getLocation()));
6729 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00006730 WeakTopLevelDecl.push_back(NewD);
6731 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
6732 // to insert Decl at TU scope, sorry.
6733 DeclContext *SavedContext = CurContext;
6734 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00006735 NewD->setDeclContext(CurContext);
6736 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00006737 PushOnScopeChains(NewD, S);
6738 CurContext = SavedContext;
6739 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00006740 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00006741 }
6742}
6743
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006744void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
6745 // It's valid to "forward-declare" #pragma weak, in which case we
6746 // have to do this.
6747 LoadExternalWeakUndeclaredIdentifiers();
6748 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00006749 NamedDecl *ND = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006750 if (auto *VD = dyn_cast<VarDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006751 if (VD->isExternC())
6752 ND = VD;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006753 if (auto *FD = dyn_cast<FunctionDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006754 if (FD->isExternC())
6755 ND = FD;
6756 if (ND) {
6757 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00006758 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006759 if (I != WeakUndeclaredIdentifiers.end()) {
6760 WeakInfo W = I->second;
6761 DeclApplyPragmaWeak(S, ND, W);
6762 WeakUndeclaredIdentifiers[Id] = W;
6763 }
6764 }
6765 }
6766 }
6767}
6768
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006769/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
6770/// it, apply them to D. This is a bit tricky because PD can have attributes
6771/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00006772void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006773 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00006774 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00006775 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00006776
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006777 // Walk the declarator structure, applying decl attributes that were in a type
6778 // position to the decl itself. This handles cases like:
6779 // int *__attr__(x)** D;
6780 // when X is a decl attribute.
6781 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
6782 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00006783 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00006784
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006785 // Finally, apply any attributes on the decl itself.
6786 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00006787 ProcessDeclAttributeList(S, D, Attrs);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00006788
6789 // Apply additional attributes specified by '#pragma clang attribute'.
6790 AddPragmaAttributes(S, D);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006791}
John McCall28a6aea2009-11-04 02:18:39 +00006792
John McCall31168b02011-06-15 23:02:42 +00006793/// Is the given declaration allowed to use a forbidden type?
John McCallb61e14e2015-10-27 04:54:50 +00006794/// If so, it'll still be annotated with an attribute that makes it
6795/// illegal to actually use.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006796static bool isForbiddenTypeAllowed(Sema &S, Decl *D,
John McCallb61e14e2015-10-27 04:54:50 +00006797 const DelayedDiagnostic &diag,
John McCallc6af8c62015-10-28 05:03:19 +00006798 UnavailableAttr::ImplicitReason &reason) {
John McCall31168b02011-06-15 23:02:42 +00006799 // Private ivars are always okay. Unfortunately, people don't
6800 // always properly make their ivars private, even in system headers.
6801 // Plus we need to make fields okay, too.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006802 if (!isa<FieldDecl>(D) && !isa<ObjCPropertyDecl>(D) &&
6803 !isa<FunctionDecl>(D))
John McCall31168b02011-06-15 23:02:42 +00006804 return false;
6805
John McCallc6af8c62015-10-28 05:03:19 +00006806 // Silently accept unsupported uses of __weak in both user and system
6807 // declarations when it's been disabled, for ease of integration with
6808 // -fno-objc-arc files. We do have to take some care against attempts
6809 // to define such things; for now, we've only done that for ivars
6810 // and properties.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006811 if ((isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) {
John McCallc6af8c62015-10-28 05:03:19 +00006812 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
6813 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
6814 reason = UnavailableAttr::IR_ForbiddenWeak;
6815 return true;
6816 }
John McCallb61e14e2015-10-27 04:54:50 +00006817 }
6818
John McCallc6af8c62015-10-28 05:03:19 +00006819 // Allow all sorts of things in system headers.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006820 if (S.Context.getSourceManager().isInSystemHeader(D->getLocation())) {
John McCallc6af8c62015-10-28 05:03:19 +00006821 // Currently, all the failures dealt with this way are due to ARC
6822 // restrictions.
6823 reason = UnavailableAttr::IR_ARCForbiddenType;
6824 return true;
John McCallb61e14e2015-10-27 04:54:50 +00006825 }
6826
6827 return false;
John McCall31168b02011-06-15 23:02:42 +00006828}
6829
6830/// Handle a delayed forbidden-type diagnostic.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006831static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &DD,
6832 Decl *D) {
6833 auto Reason = UnavailableAttr::IR_None;
6834 if (D && isForbiddenTypeAllowed(S, D, DD, Reason)) {
6835 assert(Reason && "didn't set reason?");
6836 D->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", Reason, DD.Loc));
John McCall31168b02011-06-15 23:02:42 +00006837 return;
6838 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00006839 if (S.getLangOpts().ObjCAutoRefCount)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006840 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00006841 // FIXME: we may want to suppress diagnostics for all
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006842 // kind of forbidden type messages on unavailable functions.
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00006843 if (FD->hasAttr<UnavailableAttr>() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006844 DD.getForbiddenTypeDiagnostic() ==
6845 diag::err_arc_array_param_no_ownership) {
6846 DD.Triggered = true;
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00006847 return;
6848 }
6849 }
John McCall31168b02011-06-15 23:02:42 +00006850
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006851 S.Diag(DD.Loc, DD.getForbiddenTypeDiagnostic())
6852 << DD.getForbiddenTypeOperand() << DD.getForbiddenTypeArgument();
6853 DD.Triggered = true;
John McCall31168b02011-06-15 23:02:42 +00006854}
6855
Manman Ren45b1ab12016-05-06 19:57:16 +00006856static const AvailabilityAttr *getAttrForPlatform(ASTContext &Context,
6857 const Decl *D) {
6858 // Check each AvailabilityAttr to find the one for this platform.
6859 for (const auto *A : D->attrs()) {
6860 if (const auto *Avail = dyn_cast<AvailabilityAttr>(A)) {
6861 // FIXME: this is copied from CheckAvailability. We should try to
6862 // de-duplicate.
6863
6864 // Check if this is an App Extension "platform", and if so chop off
6865 // the suffix for matching with the actual platform.
6866 StringRef ActualPlatform = Avail->getPlatform()->getName();
6867 StringRef RealizedPlatform = ActualPlatform;
6868 if (Context.getLangOpts().AppExt) {
6869 size_t suffix = RealizedPlatform.rfind("_app_extension");
6870 if (suffix != StringRef::npos)
6871 RealizedPlatform = RealizedPlatform.slice(0, suffix);
6872 }
6873
6874 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
6875
6876 // Match the platform name.
6877 if (RealizedPlatform == TargetPlatform)
6878 return Avail;
6879 }
6880 }
6881 return nullptr;
6882}
6883
Erik Pilkington9f866a72017-07-18 20:32:07 +00006884/// The diagnostic we should emit for \c D, and the declaration that
6885/// originated it, or \c AR_Available.
6886///
6887/// \param D The declaration to check.
6888/// \param Message If non-null, this will be populated with the message from
6889/// the availability attribute that is selected.
6890static std::pair<AvailabilityResult, const NamedDecl *>
6891ShouldDiagnoseAvailabilityOfDecl(const NamedDecl *D, std::string *Message) {
6892 AvailabilityResult Result = D->getAvailability(Message);
6893
6894 // For typedefs, if the typedef declaration appears available look
6895 // to the underlying type to see if it is more restrictive.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006896 while (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00006897 if (Result == AR_Available) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006898 if (const auto *TT = TD->getUnderlyingType()->getAs<TagType>()) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00006899 D = TT->getDecl();
6900 Result = D->getAvailability(Message);
6901 continue;
6902 }
6903 }
6904 break;
6905 }
6906
6907 // Forward class declarations get their attributes from their definition.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006908 if (const auto *IDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00006909 if (IDecl->getDefinition()) {
6910 D = IDecl->getDefinition();
6911 Result = D->getAvailability(Message);
6912 }
6913 }
6914
6915 if (const auto *ECD = dyn_cast<EnumConstantDecl>(D))
6916 if (Result == AR_Available) {
6917 const DeclContext *DC = ECD->getDeclContext();
6918 if (const auto *TheEnumDecl = dyn_cast<EnumDecl>(DC)) {
6919 Result = TheEnumDecl->getAvailability(Message);
6920 D = TheEnumDecl;
6921 }
6922 }
6923
6924 return {Result, D};
6925}
6926
6927
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00006928/// \brief whether we should emit a diagnostic for \c K and \c DeclVersion in
6929/// the context of \c Ctx. For example, we should emit an unavailable diagnostic
6930/// in a deprecated context, but not the other way around.
6931static bool ShouldDiagnoseAvailabilityInContext(Sema &S, AvailabilityResult K,
6932 VersionTuple DeclVersion,
6933 Decl *Ctx) {
6934 assert(K != AR_Available && "Expected an unavailable declaration here!");
6935
6936 // Checks if we should emit the availability diagnostic in the context of C.
6937 auto CheckContext = [&](const Decl *C) {
6938 if (K == AR_NotYetIntroduced) {
6939 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, C))
6940 if (AA->getIntroduced() >= DeclVersion)
6941 return true;
6942 } else if (K == AR_Deprecated)
6943 if (C->isDeprecated())
6944 return true;
6945
6946 if (C->isUnavailable())
6947 return true;
6948 return false;
6949 };
6950
6951 // FIXME: This is a temporary workaround! Some existing Apple headers depends
6952 // on nested declarations in an @interface having the availability of the
6953 // interface when they really shouldn't: they are members of the enclosing
6954 // context, and can referenced from there.
6955 if (S.OriginalLexicalContext && cast<Decl>(S.OriginalLexicalContext) != Ctx) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006956 const auto *OrigCtx = cast<Decl>(S.OriginalLexicalContext);
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00006957 if (CheckContext(OrigCtx))
6958 return false;
6959
6960 // An implementation implicitly has the availability of the interface.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006961 if (const auto *CatOrImpl = dyn_cast<ObjCImplDecl>(OrigCtx)) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00006962 if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface())
6963 if (CheckContext(Interface))
6964 return false;
6965 }
6966 // A category implicitly has the availability of the interface.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006967 else if (const auto *CatD = dyn_cast<ObjCCategoryDecl>(OrigCtx))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00006968 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
6969 if (CheckContext(Interface))
6970 return false;
6971 }
6972
6973 do {
6974 if (CheckContext(Ctx))
6975 return false;
6976
6977 // An implementation implicitly has the availability of the interface.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006978 if (const auto *CatOrImpl = dyn_cast<ObjCImplDecl>(Ctx)) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00006979 if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface())
6980 if (CheckContext(Interface))
6981 return false;
6982 }
6983 // A category implicitly has the availability of the interface.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006984 else if (const auto *CatD = dyn_cast<ObjCCategoryDecl>(Ctx))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00006985 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
6986 if (CheckContext(Interface))
6987 return false;
6988 } while ((Ctx = cast_or_null<Decl>(Ctx->getDeclContext())));
6989
6990 return true;
6991}
6992
Alex Lorenzc9a369f2017-06-22 17:02:24 +00006993static bool
6994shouldDiagnoseAvailabilityByDefault(const ASTContext &Context,
6995 const VersionTuple &DeploymentVersion,
6996 const VersionTuple &DeclVersion) {
6997 const auto &Triple = Context.getTargetInfo().getTriple();
6998 VersionTuple ForceAvailabilityFromVersion;
6999 switch (Triple.getOS()) {
7000 case llvm::Triple::IOS:
7001 case llvm::Triple::TvOS:
7002 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/11);
7003 break;
7004 case llvm::Triple::WatchOS:
7005 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/4);
7006 break;
7007 case llvm::Triple::Darwin:
7008 case llvm::Triple::MacOSX:
7009 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/10, /*Minor=*/13);
7010 break;
7011 default:
7012 // New targets should always warn about availability.
7013 return Triple.getVendor() == llvm::Triple::Apple;
7014 }
7015 return DeploymentVersion >= ForceAvailabilityFromVersion ||
7016 DeclVersion >= ForceAvailabilityFromVersion;
7017}
7018
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007019static NamedDecl *findEnclosingDeclToAnnotate(Decl *OrigCtx) {
7020 for (Decl *Ctx = OrigCtx; Ctx;
7021 Ctx = cast_or_null<Decl>(Ctx->getDeclContext())) {
7022 if (isa<TagDecl>(Ctx) || isa<FunctionDecl>(Ctx) || isa<ObjCMethodDecl>(Ctx))
7023 return cast<NamedDecl>(Ctx);
7024 if (auto *CD = dyn_cast<ObjCContainerDecl>(Ctx)) {
7025 if (auto *Imp = dyn_cast<ObjCImplDecl>(Ctx))
7026 return Imp->getClassInterface();
7027 return CD;
7028 }
7029 }
7030
7031 return dyn_cast<NamedDecl>(OrigCtx);
7032}
7033
Alex Lorenz727c21e2017-07-26 13:58:02 +00007034namespace {
7035
7036struct AttributeInsertion {
7037 StringRef Prefix;
7038 SourceLocation Loc;
7039 StringRef Suffix;
7040
7041 static AttributeInsertion createInsertionAfter(const NamedDecl *D) {
7042 return {" ", D->getLocEnd(), ""};
7043 }
7044 static AttributeInsertion createInsertionAfter(SourceLocation Loc) {
7045 return {" ", Loc, ""};
7046 }
7047 static AttributeInsertion createInsertionBefore(const NamedDecl *D) {
7048 return {"", D->getLocStart(), "\n"};
7049 }
7050};
7051
7052} // end anonymous namespace
7053
7054/// Returns a source location in which it's appropriate to insert a new
7055/// attribute for the given declaration \D.
7056static Optional<AttributeInsertion>
7057createAttributeInsertion(const NamedDecl *D, const SourceManager &SM,
7058 const LangOptions &LangOpts) {
7059 if (isa<ObjCPropertyDecl>(D))
7060 return AttributeInsertion::createInsertionAfter(D);
7061 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
7062 if (MD->hasBody())
7063 return None;
7064 return AttributeInsertion::createInsertionAfter(D);
7065 }
7066 if (const auto *TD = dyn_cast<TagDecl>(D)) {
7067 SourceLocation Loc =
7068 Lexer::getLocForEndOfToken(TD->getInnerLocStart(), 0, SM, LangOpts);
7069 if (Loc.isInvalid())
7070 return None;
7071 // Insert after the 'struct'/whatever keyword.
7072 return AttributeInsertion::createInsertionAfter(Loc);
7073 }
7074 return AttributeInsertion::createInsertionBefore(D);
7075}
7076
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007077/// Actually emit an availability diagnostic for a reference to an unavailable
7078/// decl.
7079///
7080/// \param Ctx The context that the reference occurred in
7081/// \param ReferringDecl The exact declaration that was referenced.
7082/// \param OffendingDecl A related decl to \c ReferringDecl that has an
7083/// availability attribute corrisponding to \c K attached to it. Note that this
7084/// may not be the same as ReferringDecl, i.e. if an EnumDecl is annotated and
7085/// we refer to a member EnumConstantDecl, ReferringDecl is the EnumConstantDecl
7086/// and OffendingDecl is the EnumDecl.
Erik Pilkington796a3e22016-08-05 22:59:03 +00007087static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007088 Decl *Ctx, const NamedDecl *ReferringDecl,
7089 const NamedDecl *OffendingDecl,
Aaron Ballmanfb237522014-10-15 15:37:51 +00007090 StringRef Message, SourceLocation Loc,
7091 const ObjCInterfaceDecl *UnknownObjCClass,
7092 const ObjCPropertyDecl *ObjCProperty,
7093 bool ObjCPropertyAccess) {
7094 // Diagnostics for deprecated or unavailable.
7095 unsigned diag, diag_message, diag_fwdclass_message;
John McCallb61e14e2015-10-27 04:54:50 +00007096 unsigned diag_available_here = diag::note_availability_specified_here;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007097 SourceLocation NoteLocation = OffendingDecl->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00007098
7099 // Matches 'diag::note_property_attribute' options.
7100 unsigned property_note_select;
7101
7102 // Matches diag::note_availability_specified_here.
7103 unsigned available_here_select_kind;
7104
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007105 VersionTuple DeclVersion;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007106 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, OffendingDecl))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007107 DeclVersion = AA->getIntroduced();
7108
7109 if (!ShouldDiagnoseAvailabilityInContext(S, K, DeclVersion, Ctx))
7110 return;
7111
Erik Pilkington8b352c42017-08-14 19:49:12 +00007112 // The declaration can have multiple availability attributes, we are looking
7113 // at one of them.
7114 const AvailabilityAttr *A = getAttrForPlatform(S.Context, OffendingDecl);
7115 if (A && A->isInherited()) {
7116 for (const Decl *Redecl = OffendingDecl->getMostRecentDecl(); Redecl;
7117 Redecl = Redecl->getPreviousDecl()) {
7118 const AvailabilityAttr *AForRedecl =
7119 getAttrForPlatform(S.Context, Redecl);
7120 if (AForRedecl && !AForRedecl->isInherited()) {
7121 // If D is a declaration with inherited attributes, the note should
7122 // point to the declaration with actual attributes.
7123 NoteLocation = Redecl->getLocation();
7124 break;
7125 }
7126 }
7127 }
7128
Aaron Ballmanfb237522014-10-15 15:37:51 +00007129 switch (K) {
Erik Pilkington8b352c42017-08-14 19:49:12 +00007130 case AR_NotYetIntroduced: {
7131 // We would like to emit the diagnostic even if -Wunguarded-availability is
7132 // not specified for deployment targets >= to iOS 11 or equivalent or
7133 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
7134 // later.
7135 const AvailabilityAttr *AA =
7136 getAttrForPlatform(S.getASTContext(), OffendingDecl);
7137 VersionTuple Introduced = AA->getIntroduced();
7138
7139 bool UseNewWarning = shouldDiagnoseAvailabilityByDefault(
7140 S.Context, S.Context.getTargetInfo().getPlatformMinVersion(),
7141 Introduced);
7142 unsigned Warning = UseNewWarning ? diag::warn_unguarded_availability_new
7143 : diag::warn_unguarded_availability;
7144
7145 S.Diag(Loc, Warning)
7146 << OffendingDecl
7147 << AvailabilityAttr::getPrettyPlatformName(
7148 S.getASTContext().getTargetInfo().getPlatformName())
7149 << Introduced.getAsString();
7150
7151 S.Diag(OffendingDecl->getLocation(), diag::note_availability_specified_here)
7152 << OffendingDecl << /* partial */ 3;
7153
7154 if (const auto *Enclosing = findEnclosingDeclToAnnotate(Ctx)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007155 if (const auto *TD = dyn_cast<TagDecl>(Enclosing))
Erik Pilkington8b352c42017-08-14 19:49:12 +00007156 if (TD->getDeclName().isEmpty()) {
7157 S.Diag(TD->getLocation(),
7158 diag::note_decl_unguarded_availability_silence)
7159 << /*Anonymous*/ 1 << TD->getKindName();
7160 return;
7161 }
7162 auto FixitNoteDiag =
7163 S.Diag(Enclosing->getLocation(),
7164 diag::note_decl_unguarded_availability_silence)
7165 << /*Named*/ 0 << Enclosing;
7166 // Don't offer a fixit for declarations with availability attributes.
7167 if (Enclosing->hasAttr<AvailabilityAttr>())
7168 return;
7169 if (!S.getPreprocessor().isMacroDefined("API_AVAILABLE"))
7170 return;
7171 Optional<AttributeInsertion> Insertion = createAttributeInsertion(
7172 Enclosing, S.getSourceManager(), S.getLangOpts());
7173 if (!Insertion)
7174 return;
7175 std::string PlatformName =
7176 AvailabilityAttr::getPlatformNameSourceSpelling(
7177 S.getASTContext().getTargetInfo().getPlatformName())
7178 .lower();
7179 std::string Introduced =
7180 OffendingDecl->getVersionIntroduced().getAsString();
7181 FixitNoteDiag << FixItHint::CreateInsertion(
7182 Insertion->Loc,
7183 (llvm::Twine(Insertion->Prefix) + "API_AVAILABLE(" + PlatformName +
7184 "(" + Introduced + "))" + Insertion->Suffix)
7185 .str());
7186 }
7187 return;
7188 }
Erik Pilkington796a3e22016-08-05 22:59:03 +00007189 case AR_Deprecated:
Aaron Ballmanfb237522014-10-15 15:37:51 +00007190 diag = !ObjCPropertyAccess ? diag::warn_deprecated
7191 : diag::warn_property_method_deprecated;
7192 diag_message = diag::warn_deprecated_message;
7193 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
7194 property_note_select = /* deprecated */ 0;
7195 available_here_select_kind = /* deprecated */ 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007196 if (const auto *AL = OffendingDecl->getAttr<DeprecatedAttr>())
7197 NoteLocation = AL->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00007198 break;
7199
Erik Pilkington796a3e22016-08-05 22:59:03 +00007200 case AR_Unavailable:
Aaron Ballmanfb237522014-10-15 15:37:51 +00007201 diag = !ObjCPropertyAccess ? diag::err_unavailable
7202 : diag::err_property_method_unavailable;
7203 diag_message = diag::err_unavailable_message;
7204 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
7205 property_note_select = /* unavailable */ 1;
7206 available_here_select_kind = /* unavailable */ 0;
John McCallb61e14e2015-10-27 04:54:50 +00007207
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007208 if (auto AL = OffendingDecl->getAttr<UnavailableAttr>()) {
7209 if (AL->isImplicit() && AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00007210 // Most of these failures are due to extra restrictions in ARC;
7211 // reflect that in the primary diagnostic when applicable.
7212 auto flagARCError = [&] {
7213 if (S.getLangOpts().ObjCAutoRefCount &&
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007214 S.getSourceManager().isInSystemHeader(
7215 OffendingDecl->getLocation()))
John McCallc6af8c62015-10-28 05:03:19 +00007216 diag = diag::err_unavailable_in_arc;
7217 };
7218
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007219 switch (AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00007220 case UnavailableAttr::IR_None: break;
7221
7222 case UnavailableAttr::IR_ARCForbiddenType:
7223 flagARCError();
7224 diag_available_here = diag::note_arc_forbidden_type;
7225 break;
7226
7227 case UnavailableAttr::IR_ForbiddenWeak:
7228 if (S.getLangOpts().ObjCWeakRuntime)
7229 diag_available_here = diag::note_arc_weak_disabled;
7230 else
7231 diag_available_here = diag::note_arc_weak_no_runtime;
7232 break;
7233
7234 case UnavailableAttr::IR_ARCForbiddenConversion:
7235 flagARCError();
7236 diag_available_here = diag::note_performs_forbidden_arc_conversion;
7237 break;
7238
7239 case UnavailableAttr::IR_ARCInitReturnsUnrelated:
7240 flagARCError();
7241 diag_available_here = diag::note_arc_init_returns_unrelated;
7242 break;
7243
7244 case UnavailableAttr::IR_ARCFieldWithOwnership:
7245 flagARCError();
7246 diag_available_here = diag::note_arc_field_with_ownership;
7247 break;
7248 }
7249 }
John McCallb61e14e2015-10-27 04:54:50 +00007250 }
Aaron Ballmanfb237522014-10-15 15:37:51 +00007251 break;
7252
Erik Pilkington796a3e22016-08-05 22:59:03 +00007253 case AR_Available:
7254 llvm_unreachable("Warning for availability of available declaration?");
Aaron Ballmanfb237522014-10-15 15:37:51 +00007255 }
7256
Manman Renc7890fe2016-03-16 18:50:49 +00007257 CharSourceRange UseRange;
7258 StringRef Replacement;
Erik Pilkington796a3e22016-08-05 22:59:03 +00007259 if (K == AR_Deprecated) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007260 if (auto AL = OffendingDecl->getAttr<DeprecatedAttr>())
7261 Replacement = AL->getReplacement();
7262 if (auto AL = getAttrForPlatform(S.Context, OffendingDecl))
7263 Replacement = AL->getReplacement();
Manman Renc7890fe2016-03-16 18:50:49 +00007264
7265 if (!Replacement.empty())
7266 UseRange =
7267 CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc));
7268 }
7269
Aaron Ballmanfb237522014-10-15 15:37:51 +00007270 if (!Message.empty()) {
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007271 S.Diag(Loc, diag_message) << ReferringDecl << Message
Manman Renc7890fe2016-03-16 18:50:49 +00007272 << (UseRange.isValid() ?
7273 FixItHint::CreateReplacement(UseRange, Replacement) : FixItHint());
Aaron Ballmanfb237522014-10-15 15:37:51 +00007274 if (ObjCProperty)
7275 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
7276 << ObjCProperty->getDeclName() << property_note_select;
7277 } else if (!UnknownObjCClass) {
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007278 S.Diag(Loc, diag) << ReferringDecl
Manman Renc7890fe2016-03-16 18:50:49 +00007279 << (UseRange.isValid() ?
7280 FixItHint::CreateReplacement(UseRange, Replacement) : FixItHint());
Aaron Ballmanfb237522014-10-15 15:37:51 +00007281 if (ObjCProperty)
7282 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
7283 << ObjCProperty->getDeclName() << property_note_select;
7284 } else {
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007285 S.Diag(Loc, diag_fwdclass_message) << ReferringDecl
Manman Renc7890fe2016-03-16 18:50:49 +00007286 << (UseRange.isValid() ?
7287 FixItHint::CreateReplacement(UseRange, Replacement) : FixItHint());
Aaron Ballmanfb237522014-10-15 15:37:51 +00007288 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
7289 }
7290
Erik Pilkington8b352c42017-08-14 19:49:12 +00007291 S.Diag(NoteLocation, diag_available_here)
7292 << OffendingDecl << available_here_select_kind;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007293}
7294
7295static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
7296 Decl *Ctx) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00007297 assert(DD.Kind == DelayedDiagnostic::Availability &&
7298 "Expected an availability diagnostic here");
7299
Aaron Ballmanfb237522014-10-15 15:37:51 +00007300 DD.Triggered = true;
Nico Weber0055a192015-03-19 19:18:22 +00007301 DoEmitAvailabilityWarning(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007302 S, DD.getAvailabilityResult(), Ctx, DD.getAvailabilityReferringDecl(),
7303 DD.getAvailabilityOffendingDecl(), DD.getAvailabilityMessage(), DD.Loc,
7304 DD.getUnknownObjCClass(), DD.getObjCProperty(), false);
Aaron Ballmanfb237522014-10-15 15:37:51 +00007305}
7306
John McCall2ec85372012-05-07 06:16:41 +00007307void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
7308 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00007309 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00007310 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00007311
John McCall2ec85372012-05-07 06:16:41 +00007312 // When delaying diagnostics to run in the context of a parsed
7313 // declaration, we only want to actually emit anything if parsing
7314 // succeeds.
7315 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00007316
John McCall2ec85372012-05-07 06:16:41 +00007317 // We emit all the active diagnostics in this pool or any of its
7318 // parents. In general, we'll get one pool for the decl spec
7319 // and a child pool for each declarator; in a decl group like:
7320 // deprecated_typedef foo, *bar, baz();
7321 // only the declarator pops will be passed decls. This is correct;
7322 // we really do need to consider delayed diagnostics from the decl spec
7323 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00007324 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00007325 do {
John McCall6347b682012-05-07 06:16:58 +00007326 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00007327 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
7328 // This const_cast is a bit lame. Really, Triggered should be mutable.
7329 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00007330 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00007331 continue;
7332
John McCallc1465822011-02-14 07:13:47 +00007333 switch (diag.Kind) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00007334 case DelayedDiagnostic::Availability:
Ted Kremenekb79ee572013-12-18 23:30:06 +00007335 // Don't bother giving deprecation/unavailable diagnostics if
7336 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00007337 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00007338 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00007339 break;
7340
7341 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00007342 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00007343 break;
John McCall31168b02011-06-15 23:02:42 +00007344
7345 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00007346 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00007347 break;
John McCall86121512010-01-27 03:50:35 +00007348 }
7349 }
John McCall2ec85372012-05-07 06:16:41 +00007350 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00007351}
7352
John McCall6347b682012-05-07 06:16:58 +00007353/// Given a set of delayed diagnostics, re-emit them as if they had
7354/// been delayed in the current context instead of in the given pool.
7355/// Essentially, this just moves them to the current pool.
7356void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
7357 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
7358 assert(curPool && "re-emitting in undelayed context not supported");
7359 curPool->steal(pool);
7360}
7361
Erik Pilkington9f866a72017-07-18 20:32:07 +00007362static void EmitAvailabilityWarning(Sema &S, AvailabilityResult AR,
7363 const NamedDecl *ReferringDecl,
7364 const NamedDecl *OffendingDecl,
7365 StringRef Message, SourceLocation Loc,
7366 const ObjCInterfaceDecl *UnknownObjCClass,
7367 const ObjCPropertyDecl *ObjCProperty,
7368 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00007369 // Delay if we're currently parsing a declaration.
Erik Pilkington9f866a72017-07-18 20:32:07 +00007370 if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
7371 S.DelayedDiagnostics.add(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007372 DelayedDiagnostic::makeAvailability(
7373 AR, Loc, ReferringDecl, OffendingDecl, UnknownObjCClass,
7374 ObjCProperty, Message, ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00007375 return;
7376 }
7377
Erik Pilkington9f866a72017-07-18 20:32:07 +00007378 Decl *Ctx = cast<Decl>(S.getCurLexicalContext());
7379 DoEmitAvailabilityWarning(S, AR, Ctx, ReferringDecl, OffendingDecl,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007380 Message, Loc, UnknownObjCClass, ObjCProperty,
7381 ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00007382}
Erik Pilkington48c7cc92016-07-29 17:37:38 +00007383
Erik Pilkington5cd57172016-08-16 17:44:11 +00007384namespace {
7385
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007386/// Returns true if the given statement can be a body-like child of \p Parent.
7387bool isBodyLikeChildStmt(const Stmt *S, const Stmt *Parent) {
7388 switch (Parent->getStmtClass()) {
7389 case Stmt::IfStmtClass:
7390 return cast<IfStmt>(Parent)->getThen() == S ||
7391 cast<IfStmt>(Parent)->getElse() == S;
7392 case Stmt::WhileStmtClass:
7393 return cast<WhileStmt>(Parent)->getBody() == S;
7394 case Stmt::DoStmtClass:
7395 return cast<DoStmt>(Parent)->getBody() == S;
7396 case Stmt::ForStmtClass:
7397 return cast<ForStmt>(Parent)->getBody() == S;
7398 case Stmt::CXXForRangeStmtClass:
7399 return cast<CXXForRangeStmt>(Parent)->getBody() == S;
7400 case Stmt::ObjCForCollectionStmtClass:
7401 return cast<ObjCForCollectionStmt>(Parent)->getBody() == S;
7402 case Stmt::CaseStmtClass:
7403 case Stmt::DefaultStmtClass:
7404 return cast<SwitchCase>(Parent)->getSubStmt() == S;
7405 default:
7406 return false;
7407 }
7408}
7409
7410class StmtUSEFinder : public RecursiveASTVisitor<StmtUSEFinder> {
7411 const Stmt *Target;
7412
7413public:
7414 bool VisitStmt(Stmt *S) { return S != Target; }
7415
7416 /// Returns true if the given statement is present in the given declaration.
7417 static bool isContained(const Stmt *Target, const Decl *D) {
7418 StmtUSEFinder Visitor;
7419 Visitor.Target = Target;
7420 return !Visitor.TraverseDecl(const_cast<Decl *>(D));
7421 }
7422};
7423
7424/// Traverses the AST and finds the last statement that used a given
7425/// declaration.
7426class LastDeclUSEFinder : public RecursiveASTVisitor<LastDeclUSEFinder> {
7427 const Decl *D;
7428
7429public:
7430 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
7431 if (DRE->getDecl() == D)
7432 return false;
7433 return true;
7434 }
7435
7436 static const Stmt *findLastStmtThatUsesDecl(const Decl *D,
7437 const CompoundStmt *Scope) {
7438 LastDeclUSEFinder Visitor;
7439 Visitor.D = D;
7440 for (auto I = Scope->body_rbegin(), E = Scope->body_rend(); I != E; ++I) {
7441 const Stmt *S = *I;
7442 if (!Visitor.TraverseStmt(const_cast<Stmt *>(S)))
7443 return S;
7444 }
7445 return nullptr;
7446 }
7447};
7448
Erik Pilkington5cd57172016-08-16 17:44:11 +00007449/// \brief This class implements -Wunguarded-availability.
7450///
7451/// This is done with a traversal of the AST of a function that makes reference
7452/// to a partially available declaration. Whenever we encounter an \c if of the
7453/// form: \c if(@available(...)), we use the version from the condition to visit
7454/// the then statement.
7455class DiagnoseUnguardedAvailability
7456 : public RecursiveASTVisitor<DiagnoseUnguardedAvailability> {
7457 typedef RecursiveASTVisitor<DiagnoseUnguardedAvailability> Base;
7458
7459 Sema &SemaRef;
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007460 Decl *Ctx;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007461
7462 /// Stack of potentially nested 'if (@available(...))'s.
7463 SmallVector<VersionTuple, 8> AvailabilityStack;
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007464 SmallVector<const Stmt *, 16> StmtStack;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007465
7466 void DiagnoseDeclAvailability(NamedDecl *D, SourceRange Range);
7467
7468public:
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007469 DiagnoseUnguardedAvailability(Sema &SemaRef, Decl *Ctx)
7470 : SemaRef(SemaRef), Ctx(Ctx) {
7471 AvailabilityStack.push_back(
7472 SemaRef.Context.getTargetInfo().getPlatformMinVersion());
Erik Pilkington5cd57172016-08-16 17:44:11 +00007473 }
7474
Alex Lorenz6f911122017-05-16 13:58:53 +00007475 bool TraverseDecl(Decl *D) {
7476 // Avoid visiting nested functions to prevent duplicate warnings.
7477 if (!D || isa<FunctionDecl>(D))
7478 return true;
7479 return Base::TraverseDecl(D);
7480 }
7481
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007482 bool TraverseStmt(Stmt *S) {
7483 if (!S)
7484 return true;
7485 StmtStack.push_back(S);
7486 bool Result = Base::TraverseStmt(S);
7487 StmtStack.pop_back();
7488 return Result;
7489 }
7490
Erik Pilkington5cd57172016-08-16 17:44:11 +00007491 void IssueDiagnostics(Stmt *S) { TraverseStmt(S); }
7492
7493 bool TraverseIfStmt(IfStmt *If);
7494
Alex Lorenz6f911122017-05-16 13:58:53 +00007495 bool TraverseLambdaExpr(LambdaExpr *E) { return true; }
7496
Erik Pilkingtonba87c622017-08-18 20:20:56 +00007497 // for 'case X:' statements, don't bother looking at the 'X'; it can't lead
7498 // to any useful diagnostics.
7499 bool TraverseCaseStmt(CaseStmt *CS) { return TraverseStmt(CS->getSubStmt()); }
7500
Erik Pilkington6ac77a62017-05-22 15:41:12 +00007501 bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *PRE) {
7502 if (PRE->isClassReceiver())
7503 DiagnoseDeclAvailability(PRE->getClassReceiver(), PRE->getReceiverLocation());
7504 return true;
7505 }
7506
Erik Pilkington5cd57172016-08-16 17:44:11 +00007507 bool VisitObjCMessageExpr(ObjCMessageExpr *Msg) {
7508 if (ObjCMethodDecl *D = Msg->getMethodDecl())
7509 DiagnoseDeclAvailability(
7510 D, SourceRange(Msg->getSelectorStartLoc(), Msg->getLocEnd()));
7511 return true;
7512 }
7513
7514 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
7515 DiagnoseDeclAvailability(DRE->getDecl(),
7516 SourceRange(DRE->getLocStart(), DRE->getLocEnd()));
7517 return true;
7518 }
7519
7520 bool VisitMemberExpr(MemberExpr *ME) {
7521 DiagnoseDeclAvailability(ME->getMemberDecl(),
7522 SourceRange(ME->getLocStart(), ME->getLocEnd()));
7523 return true;
7524 }
7525
Alex Lorenz0a484ba2017-05-24 15:15:29 +00007526 bool VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr *E) {
7527 SemaRef.Diag(E->getLocStart(), diag::warn_at_available_unchecked_use)
7528 << (!SemaRef.getLangOpts().ObjC1);
7529 return true;
7530 }
7531
Erik Pilkington5cd57172016-08-16 17:44:11 +00007532 bool VisitTypeLoc(TypeLoc Ty);
7533};
7534
7535void DiagnoseUnguardedAvailability::DiagnoseDeclAvailability(
7536 NamedDecl *D, SourceRange Range) {
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007537 AvailabilityResult Result;
7538 const NamedDecl *OffendingDecl;
7539 std::tie(Result, OffendingDecl) =
Erik Pilkington9f866a72017-07-18 20:32:07 +00007540 ShouldDiagnoseAvailabilityOfDecl(D, nullptr);
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007541 if (Result != AR_Available) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00007542 // All other diagnostic kinds have already been handled in
7543 // DiagnoseAvailabilityOfDecl.
7544 if (Result != AR_NotYetIntroduced)
7545 return;
7546
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007547 const AvailabilityAttr *AA =
7548 getAttrForPlatform(SemaRef.getASTContext(), OffendingDecl);
Erik Pilkington5cd57172016-08-16 17:44:11 +00007549 VersionTuple Introduced = AA->getIntroduced();
7550
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007551 if (AvailabilityStack.back() >= Introduced)
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007552 return;
7553
7554 // If the context of this function is less available than D, we should not
7555 // emit a diagnostic.
7556 if (!ShouldDiagnoseAvailabilityInContext(SemaRef, Result, Introduced, Ctx))
7557 return;
7558
Alex Lorenzc9a369f2017-06-22 17:02:24 +00007559 // We would like to emit the diagnostic even if -Wunguarded-availability is
7560 // not specified for deployment targets >= to iOS 11 or equivalent or
7561 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
7562 // later.
7563 unsigned DiagKind =
7564 shouldDiagnoseAvailabilityByDefault(
7565 SemaRef.Context,
7566 SemaRef.Context.getTargetInfo().getPlatformMinVersion(), Introduced)
7567 ? diag::warn_unguarded_availability_new
7568 : diag::warn_unguarded_availability;
7569
7570 SemaRef.Diag(Range.getBegin(), DiagKind)
Erik Pilkington5cd57172016-08-16 17:44:11 +00007571 << Range << D
7572 << AvailabilityAttr::getPrettyPlatformName(
7573 SemaRef.getASTContext().getTargetInfo().getPlatformName())
7574 << Introduced.getAsString();
7575
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007576 SemaRef.Diag(OffendingDecl->getLocation(),
7577 diag::note_availability_specified_here)
7578 << OffendingDecl << /* partial */ 3;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007579
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007580 auto FixitDiag =
7581 SemaRef.Diag(Range.getBegin(), diag::note_unguarded_available_silence)
7582 << Range << D
7583 << (SemaRef.getLangOpts().ObjC1 ? /*@available*/ 0
7584 : /*__builtin_available*/ 1);
7585
7586 // Find the statement which should be enclosed in the if @available check.
7587 if (StmtStack.empty())
7588 return;
7589 const Stmt *StmtOfUse = StmtStack.back();
7590 const CompoundStmt *Scope = nullptr;
7591 for (const Stmt *S : llvm::reverse(StmtStack)) {
7592 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
7593 Scope = CS;
7594 break;
7595 }
7596 if (isBodyLikeChildStmt(StmtOfUse, S)) {
7597 // The declaration won't be seen outside of the statement, so we don't
7598 // have to wrap the uses of any declared variables in if (@available).
7599 // Therefore we can avoid setting Scope here.
7600 break;
7601 }
7602 StmtOfUse = S;
7603 }
7604 const Stmt *LastStmtOfUse = nullptr;
7605 if (isa<DeclStmt>(StmtOfUse) && Scope) {
7606 for (const Decl *D : cast<DeclStmt>(StmtOfUse)->decls()) {
7607 if (StmtUSEFinder::isContained(StmtStack.back(), D)) {
7608 LastStmtOfUse = LastDeclUSEFinder::findLastStmtThatUsesDecl(D, Scope);
7609 break;
7610 }
7611 }
7612 }
7613
7614 const SourceManager &SM = SemaRef.getSourceManager();
7615 SourceLocation IfInsertionLoc =
7616 SM.getExpansionLoc(StmtOfUse->getLocStart());
7617 SourceLocation StmtEndLoc =
7618 SM.getExpansionRange(
7619 (LastStmtOfUse ? LastStmtOfUse : StmtOfUse)->getLocEnd())
7620 .second;
7621 if (SM.getFileID(IfInsertionLoc) != SM.getFileID(StmtEndLoc))
7622 return;
7623
7624 StringRef Indentation = Lexer::getIndentationForLine(IfInsertionLoc, SM);
7625 const char *ExtraIndentation = " ";
7626 std::string FixItString;
7627 llvm::raw_string_ostream FixItOS(FixItString);
7628 FixItOS << "if (" << (SemaRef.getLangOpts().ObjC1 ? "@available"
7629 : "__builtin_available")
Alex Lorenze1fb64e2017-05-09 15:34:46 +00007630 << "("
7631 << AvailabilityAttr::getPlatformNameSourceSpelling(
7632 SemaRef.getASTContext().getTargetInfo().getPlatformName())
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007633 << " " << Introduced.getAsString() << ", *)) {\n"
7634 << Indentation << ExtraIndentation;
7635 FixitDiag << FixItHint::CreateInsertion(IfInsertionLoc, FixItOS.str());
7636 SourceLocation ElseInsertionLoc = Lexer::findLocationAfterToken(
7637 StmtEndLoc, tok::semi, SM, SemaRef.getLangOpts(),
7638 /*SkipTrailingWhitespaceAndNewLine=*/false);
7639 if (ElseInsertionLoc.isInvalid())
7640 ElseInsertionLoc =
7641 Lexer::getLocForEndOfToken(StmtEndLoc, 0, SM, SemaRef.getLangOpts());
7642 FixItOS.str().clear();
7643 FixItOS << "\n"
7644 << Indentation << "} else {\n"
7645 << Indentation << ExtraIndentation
7646 << "// Fallback on earlier versions\n"
7647 << Indentation << "}";
7648 FixitDiag << FixItHint::CreateInsertion(ElseInsertionLoc, FixItOS.str());
Erik Pilkington5cd57172016-08-16 17:44:11 +00007649 }
7650}
7651
7652bool DiagnoseUnguardedAvailability::VisitTypeLoc(TypeLoc Ty) {
7653 const Type *TyPtr = Ty.getTypePtr();
7654 SourceRange Range{Ty.getBeginLoc(), Ty.getEndLoc()};
7655
Erik Pilkington6ac77a62017-05-22 15:41:12 +00007656 if (Range.isInvalid())
7657 return true;
7658
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007659 if (const auto *TT = dyn_cast<TagType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00007660 TagDecl *TD = TT->getDecl();
7661 DiagnoseDeclAvailability(TD, Range);
7662
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007663 } else if (const auto *TD = dyn_cast<TypedefType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00007664 TypedefNameDecl *D = TD->getDecl();
7665 DiagnoseDeclAvailability(D, Range);
7666
7667 } else if (const auto *ObjCO = dyn_cast<ObjCObjectType>(TyPtr)) {
7668 if (NamedDecl *D = ObjCO->getInterface())
7669 DiagnoseDeclAvailability(D, Range);
7670 }
7671
7672 return true;
7673}
7674
7675bool DiagnoseUnguardedAvailability::TraverseIfStmt(IfStmt *If) {
7676 VersionTuple CondVersion;
7677 if (auto *E = dyn_cast<ObjCAvailabilityCheckExpr>(If->getCond())) {
7678 CondVersion = E->getVersion();
7679
7680 // If we're using the '*' case here or if this check is redundant, then we
7681 // use the enclosing version to check both branches.
7682 if (CondVersion.empty() || CondVersion <= AvailabilityStack.back())
Alex Lorenz98f9fcd2017-08-17 14:22:27 +00007683 return TraverseStmt(If->getThen()) && TraverseStmt(If->getElse());
Erik Pilkington5cd57172016-08-16 17:44:11 +00007684 } else {
7685 // This isn't an availability checking 'if', we can just continue.
7686 return Base::TraverseIfStmt(If);
7687 }
7688
7689 AvailabilityStack.push_back(CondVersion);
7690 bool ShouldContinue = TraverseStmt(If->getThen());
7691 AvailabilityStack.pop_back();
7692
7693 return ShouldContinue && TraverseStmt(If->getElse());
7694}
7695
7696} // end anonymous namespace
7697
7698void Sema::DiagnoseUnguardedAvailabilityViolations(Decl *D) {
7699 Stmt *Body = nullptr;
7700
7701 if (auto *FD = D->getAsFunction()) {
7702 // FIXME: We only examine the pattern decl for availability violations now,
7703 // but we should also examine instantiated templates.
7704 if (FD->isTemplateInstantiation())
7705 return;
7706
7707 Body = FD->getBody();
7708 } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
7709 Body = MD->getBody();
Alex Lorenz28559ce2017-04-26 14:20:02 +00007710 else if (auto *BD = dyn_cast<BlockDecl>(D))
7711 Body = BD->getBody();
Erik Pilkington5cd57172016-08-16 17:44:11 +00007712
7713 assert(Body && "Need a body here!");
7714
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007715 DiagnoseUnguardedAvailability(*this, D).IssueDiagnostics(Body);
Erik Pilkington5cd57172016-08-16 17:44:11 +00007716}
Erik Pilkington9f866a72017-07-18 20:32:07 +00007717
7718void Sema::DiagnoseAvailabilityOfDecl(NamedDecl *D, SourceLocation Loc,
7719 const ObjCInterfaceDecl *UnknownObjCClass,
7720 bool ObjCPropertyAccess,
7721 bool AvoidPartialAvailabilityChecks) {
7722 std::string Message;
7723 AvailabilityResult Result;
7724 const NamedDecl* OffendingDecl;
7725 // See if this declaration is unavailable, deprecated, or partial.
7726 std::tie(Result, OffendingDecl) = ShouldDiagnoseAvailabilityOfDecl(D, &Message);
7727 if (Result == AR_Available)
7728 return;
7729
7730 if (Result == AR_NotYetIntroduced) {
7731 if (AvoidPartialAvailabilityChecks)
7732 return;
7733
7734 // We need to know the @available context in the current function to
7735 // diagnose this use, let DiagnoseUnguardedAvailabilityViolations do that
7736 // when we're done parsing the current function.
7737 if (getCurFunctionOrMethodDecl()) {
7738 getEnclosingFunction()->HasPotentialAvailabilityViolations = true;
7739 return;
7740 } else if (getCurBlock() || getCurLambda()) {
7741 getCurFunction()->HasPotentialAvailabilityViolations = true;
7742 return;
7743 }
7744 }
7745
7746 const ObjCPropertyDecl *ObjCPDecl = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007747 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007748 if (const ObjCPropertyDecl *PD = MD->findPropertyDecl()) {
7749 AvailabilityResult PDeclResult = PD->getAvailability(nullptr);
7750 if (PDeclResult == Result)
7751 ObjCPDecl = PD;
7752 }
7753 }
7754
7755 EmitAvailabilityWarning(*this, Result, D, OffendingDecl, Message, Loc,
7756 UnknownObjCClass, ObjCPDecl, ObjCPropertyAccess);
7757}