blob: 1d2c3b55342cdede32c50e9ea8a1a659a0fe68c0 [file] [log] [blame]
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
David Majnemer929025d2016-01-26 19:30:26 +000014#include "clang/AST/ASTConsumer.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000015#include "clang/AST/ASTContext.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000016#include "clang/AST/ASTMutationListener.h"
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +000017#include "clang/AST/CXXInheritance.h"
John McCall28a0cf72010-08-25 07:42:41 +000018#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000019#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000021#include "clang/AST/Expr.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000022#include "clang/AST/ExprCXX.h"
Rafael Espindoladb77c4a2013-02-26 19:13:56 +000023#include "clang/AST/Mangle.h"
Erik Pilkington5cd57172016-08-16 17:44:11 +000024#include "clang/AST/RecursiveASTVisitor.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000025#include "clang/Basic/CharInfo.h"
John McCall31168b02011-06-15 23:02:42 +000026#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000027#include "clang/Basic/TargetInfo.h"
Benjamin Kramer6ee15622013-09-13 15:35:43 +000028#include "clang/Lex/Preprocessor.h"
John McCall8b0666c2010-08-20 18:27:03 +000029#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000030#include "clang/Sema/DelayedDiagnostic.h"
Artem Belevichbcec9da2016-06-06 22:54:57 +000031#include "clang/Sema/Initialization.h"
John McCallf1e8b342011-09-29 07:17:38 +000032#include "clang/Sema/Lookup.h"
Richard Smithe233fbf2013-01-28 22:42:45 +000033#include "clang/Sema/Scope.h"
Reid Kleckner04f9bca2018-03-07 22:48:35 +000034#include "clang/Sema/ScopeInfo.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000035#include "clang/Sema/SemaInternal.h"
George Burgess IV177399e2017-01-09 04:12:14 +000036#include "llvm/ADT/STLExtras.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000037#include "llvm/ADT/StringExtras.h"
Hal Finkelee90a222014-09-26 05:04:30 +000038#include "llvm/Support/MathExtras.h"
Eugene Zelenko1ced5092016-02-12 22:53:10 +000039
Chris Lattner2c6fcf52008-06-26 18:38:35 +000040using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000041using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000042
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000043namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000044 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000045 C,
46 Cpp,
47 ObjC
48 };
Eugene Zelenko1ced5092016-02-12 22:53:10 +000049} // end namespace AttributeLangSupport
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000050
Chris Lattner58418ff2008-06-29 00:16:31 +000051//===----------------------------------------------------------------------===//
52// Helper functions
53//===----------------------------------------------------------------------===//
54
Ted Kremenek527042b2009-08-14 20:49:40 +000055/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000056/// type (function or function-typed variable) or an Objective-C
57/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000058static bool isFunctionOrMethod(const Decl *D) {
Craig Topperc3ec1492014-05-26 06:22:03 +000059 return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000060}
Eugene Zelenko1ced5092016-02-12 22:53:10 +000061
David Majnemer06864812015-04-07 06:01:53 +000062/// \brief Return true if the given decl has function type (function or
63/// function-typed variable) or an Objective-C method or a block.
64static bool isFunctionOrMethodOrBlock(const Decl *D) {
65 return isFunctionOrMethod(D) || isa<BlockDecl>(D);
66}
Fariborz Jahanian4447e172009-05-15 23:15:03 +000067
John McCall3882ace2011-01-05 12:14:39 +000068/// Return true if the given decl has a declarator that should have
69/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000070static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000071 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000072 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
73 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +000074}
75
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000076/// hasFunctionProto - Return true if the given decl has a argument
77/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000078/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000079static bool hasFunctionProto(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000080 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000081 return isa<FunctionProtoType>(FnTy);
Aaron Ballman12b9f652014-01-16 13:55:42 +000082 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000083}
84
Alp Toker601b22c2014-01-21 23:35:24 +000085/// getFunctionOrMethodNumParams - Return number of function or method
86/// parameters. It is an error to call this on a K&R function (use
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000087/// hasFunctionProto first).
Alp Toker601b22c2014-01-21 23:35:24 +000088static unsigned getFunctionOrMethodNumParams(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000089 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000090 return cast<FunctionProtoType>(FnTy)->getNumParams();
Aaron Ballmana70c6b52018-02-15 16:20:20 +000091 if (const auto *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000092 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000093 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000094}
95
Alp Toker601b22c2014-01-21 23:35:24 +000096static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000097 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000098 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
Aaron Ballmana70c6b52018-02-15 16:20:20 +000099 if (const auto *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000100 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000101
Alp Toker03376dc2014-07-07 09:02:20 +0000102 return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000103}
104
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000105static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) {
106 if (const auto *FD = dyn_cast<FunctionDecl>(D))
107 return FD->getParamDecl(Idx)->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000108 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000109 return MD->parameters()[Idx]->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000110 if (const auto *BD = dyn_cast<BlockDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000111 return BD->getParamDecl(Idx)->getSourceRange();
112 return SourceRange();
113}
114
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000115static QualType getFunctionOrMethodResultType(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000116 if (const FunctionType *FnTy = D->getFunctionType())
George Burgess IV00f70bd2018-03-01 05:43:23 +0000117 return FnTy->getReturnType();
Alp Toker314cc812014-01-25 16:55:45 +0000118 return cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000119}
120
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000121static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) {
122 if (const auto *FD = dyn_cast<FunctionDecl>(D))
123 return FD->getReturnTypeSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000124 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000125 return MD->getReturnTypeSourceRange();
126 return SourceRange();
127}
128
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000129static bool isFunctionOrMethodVariadic(const Decl *D) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000130 if (const FunctionType *FnTy = D->getFunctionType())
131 return cast<FunctionProtoType>(FnTy)->isVariadic();
132 if (const auto *BD = dyn_cast<BlockDecl>(D))
Aaron Ballmane13d0092014-08-01 17:02:34 +0000133 return BD->isVariadic();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000134 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000135}
136
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000137static bool isInstanceMethod(const Decl *D) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000138 if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000139 return MethodDecl->isInstance();
140 return false;
141}
142
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000143static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000144 const auto *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000145 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000146 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000147
John McCall96fa4842010-05-17 21:00:27 +0000148 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
149 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000150 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000151
John McCall96fa4842010-05-17 21:00:27 +0000152 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000153
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000154 // FIXME: Should we walk the chain of classes?
155 return ClsName == &Ctx.Idents.get("NSString") ||
156 ClsName == &Ctx.Idents.get("NSMutableString");
157}
158
Daniel Dunbar980c6692008-09-26 03:32:58 +0000159static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000160 const auto *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000161 if (!PT)
162 return false;
163
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000164 const auto *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000165 if (!RT)
166 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000167
Daniel Dunbar980c6692008-09-26 03:32:58 +0000168 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000169 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000170 return false;
171
172 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
173}
174
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000175static unsigned getNumAttributeArgs(const AttributeList &AL) {
Richard Smithb87c4652013-10-31 21:23:20 +0000176 // FIXME: Include the type in the argument list.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000177 return AL.getNumArgs() + AL.hasParsedType();
Richard Smithb87c4652013-10-31 21:23:20 +0000178}
179
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000180template <typename Compare>
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000181static bool checkAttributeNumArgsImpl(Sema &S, const AttributeList &AL,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000182 unsigned Num, unsigned Diag,
183 Compare Comp) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000184 if (Comp(getNumAttributeArgs(AL), Num)) {
185 S.Diag(AL.getLoc(), Diag) << AL.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000186 return false;
187 }
188
189 return true;
190}
191
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000192/// \brief Check if the attribute has exactly as many args as Num. May
193/// output an error.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000194static bool checkAttributeNumArgs(Sema &S, const AttributeList &AL,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000195 unsigned Num) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000196 return checkAttributeNumArgsImpl(S, AL, Num,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000197 diag::err_attribute_wrong_number_arguments,
198 std::not_equal_to<unsigned>());
199}
200
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000201/// \brief Check if the attribute has at least as many args as Num. May
202/// output an error.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000203static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &AL,
Richard Smithb87c4652013-10-31 21:23:20 +0000204 unsigned Num) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000205 return checkAttributeNumArgsImpl(S, AL, Num,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000206 diag::err_attribute_too_few_arguments,
207 std::less<unsigned>());
208}
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000209
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000210/// \brief Check if the attribute has at most as many args as Num. May
211/// output an error.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000212static bool checkAttributeAtMostNumArgs(Sema &S, const AttributeList &AL,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000213 unsigned Num) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000214 return checkAttributeNumArgsImpl(S, AL, Num,
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000215 diag::err_attribute_too_many_arguments,
216 std::greater<unsigned>());
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000217}
218
Erich Keane623efd82017-03-30 21:48:55 +0000219/// \brief A helper function to provide Attribute Location for the Attr types
220/// AND the AttributeList.
221template <typename AttrInfo>
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000222static typename std::enable_if<std::is_base_of<Attr, AttrInfo>::value,
Erich Keane623efd82017-03-30 21:48:55 +0000223 SourceLocation>::type
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000224getAttrLoc(const AttrInfo &AL) {
225 return AL.getLocation();
Erich Keane623efd82017-03-30 21:48:55 +0000226}
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000227static SourceLocation getAttrLoc(const AttributeList &AL) {
228 return AL.getLoc();
Erich Keane623efd82017-03-30 21:48:55 +0000229}
230
231/// \brief A helper function to provide Attribute Name for the Attr types
232/// AND the AttributeList.
233template <typename AttrInfo>
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000234static typename std::enable_if<std::is_base_of<Attr, AttrInfo>::value,
Erich Keane623efd82017-03-30 21:48:55 +0000235 const AttrInfo *>::type
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000236getAttrName(const AttrInfo &AL) {
237 return &AL;
Erich Keane623efd82017-03-30 21:48:55 +0000238}
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000239static const IdentifierInfo *getAttrName(const AttributeList &AL) {
240 return AL.getName();
Erich Keane623efd82017-03-30 21:48:55 +0000241}
242
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000243/// \brief If Expr is a valid integer constant, get the value of the integer
244/// expression and return success or failure. May output an error.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000245template <typename AttrInfo>
246static bool checkUInt32Argument(Sema &S, const AttrInfo &AI, const Expr *Expr,
Erich Keane623efd82017-03-30 21:48:55 +0000247 uint32_t &Val, unsigned Idx = UINT_MAX) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000248 llvm::APSInt I(32);
249 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
250 !Expr->isIntegerConstantExpr(I, S.Context)) {
251 if (Idx != UINT_MAX)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000252 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
253 << getAttrName(AI) << Idx << AANT_ArgumentIntegerConstant
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000254 << Expr->getSourceRange();
255 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000256 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_type)
257 << getAttrName(AI) << AANT_ArgumentIntegerConstant
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000258 << Expr->getSourceRange();
259 return false;
260 }
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000261
262 if (!I.isIntN(32)) {
Aaron Ballman31f42312014-07-24 14:51:23 +0000263 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
264 << I.toString(10, false) << 32 << /* Unsigned */ 1;
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000265 return false;
266 }
267
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000268 Val = (uint32_t)I.getZExtValue();
269 return true;
270}
271
George Burgess IVe3763372016-12-22 02:50:20 +0000272/// \brief Wrapper around checkUInt32Argument, with an extra check to be sure
273/// that the result will fit into a regular (signed) int. All args have the same
274/// purpose as they do in checkUInt32Argument.
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000275template <typename AttrInfo>
276static bool checkPositiveIntArgument(Sema &S, const AttrInfo &AI, const Expr *Expr,
Erich Keane623efd82017-03-30 21:48:55 +0000277 int &Val, unsigned Idx = UINT_MAX) {
George Burgess IVe3763372016-12-22 02:50:20 +0000278 uint32_t UVal;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000279 if (!checkUInt32Argument(S, AI, Expr, UVal, Idx))
George Burgess IVe3763372016-12-22 02:50:20 +0000280 return false;
281
George Burgess IVa8049572016-12-22 19:00:31 +0000282 if (UVal > (uint32_t)std::numeric_limits<int>::max()) {
George Burgess IVe3763372016-12-22 02:50:20 +0000283 llvm::APSInt I(32); // for toString
284 I = UVal;
285 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
286 << I.toString(10, false) << 32 << /* Unsigned */ 0;
287 return false;
288 }
289
290 Val = UVal;
291 return true;
292}
293
Aaron Ballmanfb763042013-12-02 18:05:46 +0000294/// \brief Diagnose mutually exclusive attributes when present on a given
295/// declaration. Returns true if diagnosed.
296template <typename AttrTy>
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +0000297static bool checkAttrMutualExclusion(Sema &S, Decl *D, SourceRange Range,
298 IdentifierInfo *Ident) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000299 if (const auto *A = D->getAttr<AttrTy>()) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +0000300 S.Diag(Range.getBegin(), diag::err_attributes_are_not_compatible) << Ident
301 << A;
302 S.Diag(A->getLocation(), diag::note_conflicting_attribute);
Aaron Ballmanfb763042013-12-02 18:05:46 +0000303 return true;
304 }
305 return false;
306}
307
Alp Toker601b22c2014-01-21 23:35:24 +0000308/// \brief Check if IdxExpr is a valid parameter index for a function or
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000309/// instance method D. May output an error.
310///
311/// \returns true if IdxExpr is a valid index.
Erich Keane623efd82017-03-30 21:48:55 +0000312template <typename AttrInfo>
313static bool checkFunctionOrMethodParameterIndex(
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000314 Sema &S, const Decl *D, const AttrInfo &AI, unsigned AttrArgNum,
Joel E. Denny81508102018-03-13 14:51:22 +0000315 const Expr *IdxExpr, ParamIdx &Idx, bool CanIndexImplicitThis = false) {
David Majnemer06864812015-04-07 06:01:53 +0000316 assert(isFunctionOrMethodOrBlock(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000317
318 // In C++ the implicit 'this' function parameter also counts.
319 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000320 bool HP = hasFunctionProto(D);
321 bool HasImplicitThisParam = isInstanceMethod(D);
322 bool IV = HP && isFunctionOrMethodVariadic(D);
Alp Toker601b22c2014-01-21 23:35:24 +0000323 unsigned NumParams =
324 (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000325
326 llvm::APSInt IdxInt;
327 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
328 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000329 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
330 << getAttrName(AI) << AttrArgNum << AANT_ArgumentIntegerConstant
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000331 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000332 return false;
333 }
334
Joel E. Denny81508102018-03-13 14:51:22 +0000335 unsigned IdxSource = IdxInt.getLimitedValue(UINT_MAX);
336 if (IdxSource < 1 || (!IV && IdxSource > NumParams)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000337 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_out_of_bounds)
Joel E. Denny81508102018-03-13 14:51:22 +0000338 << getAttrName(AI) << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000339 return false;
340 }
Joel E. Denny81508102018-03-13 14:51:22 +0000341 if (HasImplicitThisParam && !CanIndexImplicitThis) {
342 if (IdxSource == 1) {
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)
Joel E. Denny81508102018-03-13 14:51:22 +0000345 << getAttrName(AI) << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000346 return false;
347 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000348 }
349
Joel E. Denny81508102018-03-13 14:51:22 +0000350 Idx = ParamIdx(IdxSource, D);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000351 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
Aaron Ballman836684a2018-02-25 20:40:06 +0000768/// \brief Checks to be sure that the given parameter number is in bounds, and
769/// is an integral type. Will emit appropriate diagnostics if this returns
George Burgess IVe3763372016-12-22 02:50:20 +0000770/// false.
771///
Aaron Ballman836684a2018-02-25 20:40:06 +0000772/// AttrArgNo is used to actually retrieve the argument, so it's base-0.
Erich Keane623efd82017-03-30 21:48:55 +0000773template <typename AttrInfo>
774static bool checkParamIsIntegerType(Sema &S, const FunctionDecl *FD,
Joel E. Denny81508102018-03-13 14:51:22 +0000775 const AttrInfo &AI, unsigned AttrArgNo) {
Aaron Ballman836684a2018-02-25 20:40:06 +0000776 assert(AI.isArgExpr(AttrArgNo) && "Expected expression argument");
777 Expr *AttrArg = AI.getArgAsExpr(AttrArgNo);
Joel E. Denny81508102018-03-13 14:51:22 +0000778 ParamIdx Idx;
Aaron Ballman836684a2018-02-25 20:40:06 +0000779 if (!checkFunctionOrMethodParameterIndex(S, FD, AI, AttrArgNo + 1, AttrArg,
Erich Keane623efd82017-03-30 21:48:55 +0000780 Idx))
781 return false;
782
Joel E. Denny81508102018-03-13 14:51:22 +0000783 const ParmVarDecl *Param = FD->getParamDecl(Idx.getASTIndex());
Erich Keane623efd82017-03-30 21:48:55 +0000784 if (!Param->getType()->isIntegerType() && !Param->getType()->isCharType()) {
785 SourceLocation SrcLoc = AttrArg->getLocStart();
786 S.Diag(SrcLoc, diag::err_attribute_integers_only)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000787 << getAttrName(AI) << Param->getSourceRange();
Erich Keane623efd82017-03-30 21:48:55 +0000788 return false;
789 }
790 return true;
791}
792
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000793static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &AL) {
794 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
795 !checkAttributeAtMostNumArgs(S, AL, 2))
George Burgess IVe3763372016-12-22 02:50:20 +0000796 return;
797
798 const auto *FD = cast<FunctionDecl>(D);
799 if (!FD->getReturnType()->isPointerType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000800 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
801 << AL.getName();
George Burgess IVe3763372016-12-22 02:50:20 +0000802 return;
803 }
804
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000805 const Expr *SizeExpr = AL.getArgAsExpr(0);
Joel E. Denny81508102018-03-13 14:51:22 +0000806 int SizeArgNoVal;
Simon Pilgrim27cc0542017-02-15 15:12:06 +0000807 // Parameter indices are 1-indexed, hence Index=1
Joel E. Denny81508102018-03-13 14:51:22 +0000808 if (!checkPositiveIntArgument(S, AL, SizeExpr, SizeArgNoVal, /*Index=*/1))
George Burgess IVe3763372016-12-22 02:50:20 +0000809 return;
Aaron Ballman836684a2018-02-25 20:40:06 +0000810 if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/0))
George Burgess IVe3763372016-12-22 02:50:20 +0000811 return;
Joel E. Denny81508102018-03-13 14:51:22 +0000812 ParamIdx SizeArgNo(SizeArgNoVal, D);
George Burgess IVe3763372016-12-22 02:50:20 +0000813
Joel E. Denny81508102018-03-13 14:51:22 +0000814 ParamIdx NumberArgNo;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000815 if (AL.getNumArgs() == 2) {
816 const Expr *NumberExpr = AL.getArgAsExpr(1);
Joel E. Denny81508102018-03-13 14:51:22 +0000817 int Val;
Simon Pilgrim27cc0542017-02-15 15:12:06 +0000818 // Parameter indices are 1-based, hence Index=2
Joel E. Denny81508102018-03-13 14:51:22 +0000819 if (!checkPositiveIntArgument(S, AL, NumberExpr, Val, /*Index=*/2))
George Burgess IVe3763372016-12-22 02:50:20 +0000820 return;
Aaron Ballman836684a2018-02-25 20:40:06 +0000821 if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/1))
George Burgess IVe3763372016-12-22 02:50:20 +0000822 return;
Joel E. Denny81508102018-03-13 14:51:22 +0000823 NumberArgNo = ParamIdx(Val, D);
George Burgess IVe3763372016-12-22 02:50:20 +0000824 }
825
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000826 D->addAttr(::new (S.Context)
827 AllocSizeAttr(AL.getRange(), S.Context, SizeArgNo, NumberArgNo,
828 AL.getAttributeSpellingListIndex()));
George Burgess IVe3763372016-12-22 02:50:20 +0000829}
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000830
Michael Hana9171bc2012-08-03 17:40:43 +0000831static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000832 const AttributeList &AL,
Craig Topper5603df42013-07-05 19:34:19 +0000833 SmallVectorImpl<Expr *> &Args) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000834 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000835 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000836
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000837 if (!isIntOrBool(AL.getArgAsExpr(0))) {
838 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
839 << AL.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000840 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000841 }
842
843 // check that all arguments are lockable objects
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000844 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000845
Michael Han3be3b442012-07-23 18:48:41 +0000846 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000847}
848
Michael Hana9171bc2012-08-03 17:40:43 +0000849static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000850 const AttributeList &AL) {
Michael Han3be3b442012-07-23 18:48:41 +0000851 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000852 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000853 return;
854
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000855 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(
856 AL.getRange(), S.Context, AL.getArgAsExpr(0), Args.data(), Args.size(),
857 AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000858}
859
Michael Hana9171bc2012-08-03 17:40:43 +0000860static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000861 const AttributeList &AL) {
Michael Han3be3b442012-07-23 18:48:41 +0000862 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000863 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Michael Han3be3b442012-07-23 18:48:41 +0000864 return;
865
Nico Weber462fd1e2015-01-07 23:50:05 +0000866 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000867 AL.getRange(), S.Context, AL.getArgAsExpr(0), Args.data(),
868 Args.size(), AL.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000869}
870
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000871static void handleLockReturnedAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000872 const AttributeList &AL) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000873 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000874 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000875 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000876 unsigned Size = Args.size();
877 if (Size == 0)
878 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000879
Michael Han99315932013-01-24 16:46:58 +0000880 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000881 LockReturnedAttr(AL.getRange(), S.Context, Args[0],
882 AL.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000883}
884
885static void handleLocksExcludedAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000886 const AttributeList &AL) {
887 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000888 return;
889
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000890 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000891 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000892 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000893 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000894 if (Size == 0)
895 return;
896 Expr **StartArg = &Args[0];
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 LocksExcludedAttr(AL.getRange(), S.Context, StartArg, Size,
900 AL.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000901}
902
George Burgess IV177399e2017-01-09 04:12:14 +0000903static bool checkFunctionConditionAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000904 const AttributeList &AL,
George Burgess IV177399e2017-01-09 04:12:14 +0000905 Expr *&Cond, StringRef &Msg) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000906 Cond = AL.getArgAsExpr(0);
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000907 if (!Cond->isTypeDependent()) {
908 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
909 if (Converted.isInvalid())
George Burgess IV177399e2017-01-09 04:12:14 +0000910 return false;
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000911 Cond = Converted.get();
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000912 }
913
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000914 if (!S.checkStringLiteralArgumentAttr(AL, 1, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +0000915 return false;
916
917 if (Msg.empty())
918 Msg = "<no message provided>";
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000919
920 SmallVector<PartialDiagnosticAt, 8> Diags;
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +0000921 if (isa<FunctionDecl>(D) && !Cond->isValueDependent() &&
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000922 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
923 Diags)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000924 S.Diag(AL.getLoc(), diag::err_attr_cond_never_constant_expr)
925 << AL.getName();
George Burgess IV0d546532016-11-10 21:47:12 +0000926 for (const PartialDiagnosticAt &PDiag : Diags)
927 S.Diag(PDiag.first, PDiag.second);
George Burgess IV177399e2017-01-09 04:12:14 +0000928 return false;
929 }
930 return true;
931}
932
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000933static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &AL) {
934 S.Diag(AL.getLoc(), diag::ext_clang_enable_if);
George Burgess IV177399e2017-01-09 04:12:14 +0000935
936 Expr *Cond;
937 StringRef Msg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000938 if (checkFunctionConditionAttr(S, D, AL, Cond, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +0000939 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000940 EnableIfAttr(AL.getRange(), S.Context, Cond, Msg,
941 AL.getAttributeSpellingListIndex()));
George Burgess IV177399e2017-01-09 04:12:14 +0000942}
943
944namespace {
945/// Determines if a given Expr references any of the given function's
946/// ParmVarDecls, or the function's implicit `this` parameter (if applicable).
947class ArgumentDependenceChecker
948 : public RecursiveASTVisitor<ArgumentDependenceChecker> {
949#ifndef NDEBUG
950 const CXXRecordDecl *ClassType;
951#endif
952 llvm::SmallPtrSet<const ParmVarDecl *, 16> Parms;
953 bool Result;
954
955public:
956 ArgumentDependenceChecker(const FunctionDecl *FD) {
957#ifndef NDEBUG
958 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
959 ClassType = MD->getParent();
960 else
961 ClassType = nullptr;
962#endif
963 Parms.insert(FD->param_begin(), FD->param_end());
964 }
965
966 bool referencesArgs(Expr *E) {
967 Result = false;
968 TraverseStmt(E);
969 return Result;
970 }
971
972 bool VisitCXXThisExpr(CXXThisExpr *E) {
973 assert(E->getType()->getPointeeCXXRecordDecl() == ClassType &&
974 "`this` doesn't refer to the enclosing class?");
975 Result = true;
976 return false;
977 }
978
979 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
980 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
981 if (Parms.count(PVD)) {
982 Result = true;
983 return false;
984 }
985 return true;
986 }
987};
988}
989
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000990static void handleDiagnoseIfAttr(Sema &S, Decl *D, const AttributeList &AL) {
991 S.Diag(AL.getLoc(), diag::ext_clang_diagnose_if);
George Burgess IV177399e2017-01-09 04:12:14 +0000992
993 Expr *Cond;
994 StringRef Msg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000995 if (!checkFunctionConditionAttr(S, D, AL, Cond, Msg))
George Burgess IV177399e2017-01-09 04:12:14 +0000996 return;
997
998 StringRef DiagTypeStr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +0000999 if (!S.checkStringLiteralArgumentAttr(AL, 2, DiagTypeStr))
George Burgess IV177399e2017-01-09 04:12:14 +00001000 return;
1001
1002 DiagnoseIfAttr::DiagnosticType DiagType;
1003 if (!DiagnoseIfAttr::ConvertStrToDiagnosticType(DiagTypeStr, DiagType)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001004 S.Diag(AL.getArgAsExpr(2)->getLocStart(),
George Burgess IV177399e2017-01-09 04:12:14 +00001005 diag::err_diagnose_if_invalid_diagnostic_type);
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001006 return;
1007 }
1008
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00001009 bool ArgDependent = false;
Argyrios Kyrtzidis5f0c0aa2017-05-24 18:35:01 +00001010 if (const auto *FD = dyn_cast<FunctionDecl>(D))
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00001011 ArgDependent = ArgumentDependenceChecker(FD).referencesArgs(Cond);
George Burgess IV177399e2017-01-09 04:12:14 +00001012 D->addAttr(::new (S.Context) DiagnoseIfAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001013 AL.getRange(), S.Context, Cond, Msg, DiagType, ArgDependent,
1014 cast<NamedDecl>(D), AL.getAttributeSpellingListIndex()));
Nick Lewycky35a6ef42014-01-11 02:50:57 +00001015}
1016
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001017static void handlePassObjectSizeAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001018 const AttributeList &AL) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001019 if (D->hasAttr<PassObjectSizeAttr>()) {
1020 S.Diag(D->getLocStart(), diag::err_attribute_only_once_per_parameter)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001021 << AL.getName();
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001022 return;
1023 }
1024
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001025 Expr *E = AL.getArgAsExpr(0);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001026 uint32_t Type;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001027 if (!checkUInt32Argument(S, AL, E, Type, /*Idx=*/1))
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001028 return;
1029
1030 // pass_object_size's argument is passed in as the second argument of
1031 // __builtin_object_size. So, it has the same constraints as that second
1032 // argument; namely, it must be in the range [0, 3].
1033 if (Type > 3) {
1034 S.Diag(E->getLocStart(), diag::err_attribute_argument_outof_range)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001035 << AL.getName() << 0 << 3 << E->getSourceRange();
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001036 return;
1037 }
1038
1039 // pass_object_size is only supported on constant pointer parameters; as a
1040 // kindness to users, we allow the parameter to be non-const for declarations.
1041 // At this point, we have no clue if `D` belongs to a function declaration or
1042 // definition, so we defer the constness check until later.
1043 if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) {
1044 S.Diag(D->getLocStart(), diag::err_attribute_pointers_only)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001045 << AL.getName() << 1;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001046 return;
1047 }
1048
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001049 D->addAttr(::new (S.Context) PassObjectSizeAttr(
1050 AL.getRange(), S.Context, (int)Type, AL.getAttributeSpellingListIndex()));
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001051}
1052
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001053static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &AL) {
David Blaikie16f76d22013-09-06 01:28:43 +00001054 ConsumableAttr::ConsumedState DefaultState;
1055
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001056 if (AL.isArgIdent(0)) {
1057 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00001058 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1059 DefaultState)) {
1060 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001061 << AL.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +00001062 return;
1063 }
David Blaikie16f76d22013-09-06 01:28:43 +00001064 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001065 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1066 << AL.getName() << AANT_ArgumentIdentifier;
David Blaikie16f76d22013-09-06 01:28:43 +00001067 return;
1068 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001069
1070 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001071 ConsumableAttr(AL.getRange(), S.Context, DefaultState,
1072 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001073}
1074
1075static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001076 const AttributeList &AL) {
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001077 ASTContext &CurrContext = S.getASTContext();
1078 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
1079
1080 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1081 if (!RD->hasAttr<ConsumableAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001082 S.Diag(AL.getLoc(), diag::warn_attr_on_unconsumable_class) <<
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001083 RD->getNameAsString();
1084
1085 return false;
1086 }
1087 }
1088
1089 return true;
1090}
1091
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001092static void handleCallableWhenAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001093 const AttributeList &AL) {
1094 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001095 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001096
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001097 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001098 return;
1099
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001100 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001101 for (unsigned ArgIndex = 0; ArgIndex < AL.getNumArgs(); ++ArgIndex) {
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001102 CallableWhenAttr::ConsumedState CallableState;
1103
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001104 StringRef StateString;
1105 SourceLocation Loc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001106 if (AL.isArgIdent(ArgIndex)) {
1107 IdentifierLoc *Ident = AL.getArgAsIdent(ArgIndex);
Aaron Ballman55ef1512014-12-19 16:42:04 +00001108 StateString = Ident->Ident->getName();
1109 Loc = Ident->Loc;
1110 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001111 if (!S.checkStringLiteralArgumentAttr(AL, ArgIndex, StateString, &Loc))
Aaron Ballman55ef1512014-12-19 16:42:04 +00001112 return;
1113 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001114
1115 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +00001116 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001117 S.Diag(Loc, diag::warn_attribute_type_not_supported)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001118 << AL.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001119 return;
1120 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001121
1122 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001123 }
1124
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001125 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001126 CallableWhenAttr(AL.getRange(), S.Context, States.data(),
1127 States.size(), AL.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001128}
1129
DeLesley Hutchins69391772013-10-17 23:23:53 +00001130static void handleParamTypestateAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001131 const AttributeList &AL) {
DeLesley Hutchins69391772013-10-17 23:23:53 +00001132 ParamTypestateAttr::ConsumedState ParamState;
1133
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001134 if (AL.isArgIdent(0)) {
1135 IdentifierLoc *Ident = AL.getArgAsIdent(0);
DeLesley Hutchins69391772013-10-17 23:23:53 +00001136 StringRef StateString = Ident->Ident->getName();
1137
1138 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
1139 ParamState)) {
1140 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001141 << AL.getName() << StateString;
DeLesley Hutchins69391772013-10-17 23:23:53 +00001142 return;
1143 }
1144 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001145 S.Diag(AL.getLoc(), diag::err_attribute_argument_type) <<
1146 AL.getName() << AANT_ArgumentIdentifier;
DeLesley Hutchins69391772013-10-17 23:23:53 +00001147 return;
1148 }
1149
1150 // FIXME: This check is currently being done in the analysis. It can be
1151 // enabled here only after the parser propagates attributes at
1152 // template specialization definition, not declaration.
1153 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
1154 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1155 //
1156 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001157 // S.Diag(AL.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
DeLesley Hutchins69391772013-10-17 23:23:53 +00001158 // ReturnType.getAsString();
1159 // return;
1160 //}
1161
1162 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001163 ParamTypestateAttr(AL.getRange(), S.Context, ParamState,
1164 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins69391772013-10-17 23:23:53 +00001165}
1166
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001167static void handleReturnTypestateAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001168 const AttributeList &AL) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001169 ReturnTypestateAttr::ConsumedState ReturnState;
1170
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001171 if (AL.isArgIdent(0)) {
1172 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00001173 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1174 ReturnState)) {
1175 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001176 << AL.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001177 return;
1178 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001179 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001180 S.Diag(AL.getLoc(), diag::err_attribute_argument_type) <<
1181 AL.getName() << AANT_ArgumentIdentifier;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001182 return;
1183 }
1184
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001185 // FIXME: This check is currently being done in the analysis. It can be
1186 // enabled here only after the parser propagates attributes at
1187 // template specialization definition, not declaration.
1188 //QualType ReturnType;
1189 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001190 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1191 // ReturnType = Param->getType();
1192 //
1193 //} else if (const CXXConstructorDecl *Constructor =
1194 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001195 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1196 //
1197 //} else {
1198 //
1199 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1200 //}
1201 //
1202 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1203 //
1204 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1205 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1206 // ReturnType.getAsString();
1207 // return;
1208 //}
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001209
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001210 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001211 ReturnTypestateAttr(AL.getRange(), S.Context, ReturnState,
1212 AL.getAttributeSpellingListIndex()));
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001213}
1214
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001215static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &AL) {
1216 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001217 return;
1218
1219 SetTypestateAttr::ConsumedState NewState;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001220 if (AL.isArgIdent(0)) {
1221 IdentifierLoc *Ident = AL.getArgAsIdent(0);
Aaron Ballman91c98e12013-10-14 23:22:37 +00001222 StringRef Param = Ident->Ident->getName();
1223 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1224 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001225 << AL.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001226 return;
1227 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001228 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001229 S.Diag(AL.getLoc(), diag::err_attribute_argument_type) <<
1230 AL.getName() << AANT_ArgumentIdentifier;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001231 return;
1232 }
1233
1234 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001235 SetTypestateAttr(AL.getRange(), S.Context, NewState,
1236 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001237}
1238
Chris Wailes9385f9f2013-10-29 20:28:41 +00001239static void handleTestTypestateAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001240 const AttributeList &AL) {
1241 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001242 return;
1243
Chris Wailes9385f9f2013-10-29 20:28:41 +00001244 TestTypestateAttr::ConsumedState TestState;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001245 if (AL.isArgIdent(0)) {
1246 IdentifierLoc *Ident = AL.getArgAsIdent(0);
Aaron Ballman91c98e12013-10-14 23:22:37 +00001247 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001248 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001249 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001250 << AL.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001251 return;
1252 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001253 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001254 S.Diag(AL.getLoc(), diag::err_attribute_argument_type) <<
1255 AL.getName() << AANT_ArgumentIdentifier;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001256 return;
1257 }
1258
1259 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001260 TestTypestateAttr(AL.getRange(), S.Context, TestState,
1261 AL.getAttributeSpellingListIndex()));
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001262}
1263
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001264static void handleExtVectorTypeAttr(Sema &S, Decl *D, const AttributeList &AL) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001265 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001266 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001267}
1268
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001269static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &AL) {
1270 if (auto *TD = dyn_cast<TagDecl>(D))
1271 TD->addAttr(::new (S.Context) PackedAttr(AL.getRange(), S.Context,
1272 AL.getAttributeSpellingListIndex()));
1273 else if (auto *FD = dyn_cast<FieldDecl>(D)) {
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001274 bool BitfieldByteAligned = (!FD->getType()->isDependentType() &&
1275 !FD->getType()->isIncompleteType() &&
1276 FD->isBitField() &&
1277 S.Context.getTypeAlign(FD->getType()) <= 8);
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001278
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001279 if (S.getASTContext().getTargetInfo().getTriple().isPS4()) {
1280 if (BitfieldByteAligned)
1281 // The PS4 target needs to maintain ABI backwards compatibility.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001282 S.Diag(AL.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
1283 << AL.getName() << FD->getType();
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001284 else
1285 FD->addAttr(::new (S.Context) PackedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001286 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001287 } else {
1288 // Report warning about changed offset in the newer compiler versions.
1289 if (BitfieldByteAligned)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001290 S.Diag(AL.getLoc(), diag::warn_attribute_packed_for_bitfield);
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001291
1292 FD->addAttr(::new (S.Context) PackedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001293 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballmanb6fd7262017-08-08 18:07:17 +00001294 }
1295
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001296 } else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001297 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001298}
1299
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001300static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001301 // The IBOutlet/IBOutletCollection attributes only apply to instance
1302 // variables or properties of Objective-C classes. The outlet must also
1303 // have an object reference type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001304 if (const auto *VD = dyn_cast<ObjCIvarDecl>(D)) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001305 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001306 S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
1307 << AL.getName() << VD->getType() << 0;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001308 return false;
1309 }
1310 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001311 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001312 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001313 S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
1314 << AL.getName() << PD->getType() << 1;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001315 return false;
1316 }
1317 }
1318 else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001319 S.Diag(AL.getLoc(), diag::warn_attribute_iboutlet) << AL.getName();
Ted Kremenek7fd17232011-09-29 07:02:25 +00001320 return false;
1321 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001322
Ted Kremenek7fd17232011-09-29 07:02:25 +00001323 return true;
1324}
1325
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001326static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &AL) {
1327 if (!checkIBOutletCommon(S, D, AL))
Ted Kremenek1f672822010-02-18 03:08:58 +00001328 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001329
Michael Han99315932013-01-24 16:46:58 +00001330 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001331 IBOutletAttr(AL.getRange(), S.Context,
1332 AL.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001333}
1334
Chandler Carruthedc2c642011-07-02 00:01:44 +00001335static void handleIBOutletCollection(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001336 const AttributeList &AL) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001337
1338 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001339 if (AL.getNumArgs() > 1) {
1340 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1341 << AL.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001342 return;
1343 }
1344
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001345 if (!checkIBOutletCommon(S, D, AL))
Ted Kremenek26bde772010-05-19 17:38:06 +00001346 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001347
Richard Smithb1f9a282013-10-31 01:56:18 +00001348 ParsedType PT;
1349
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001350 if (AL.hasParsedType())
1351 PT = AL.getTypeArg();
Richard Smithb1f9a282013-10-31 01:56:18 +00001352 else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001353 PT = S.getTypeName(S.Context.Idents.get("NSObject"), AL.getLoc(),
Richard Smithb1f9a282013-10-31 01:56:18 +00001354 S.getScopeForContext(D->getDeclContext()->getParent()));
1355 if (!PT) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001356 S.Diag(AL.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
Richard Smithb1f9a282013-10-31 01:56:18 +00001357 return;
1358 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001359 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001360
Craig Topperc3ec1492014-05-26 06:22:03 +00001361 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001362 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1363 if (!QTLoc)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001364 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, AL.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001365
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001366 // Diagnose use of non-object type in iboutletcollection attribute.
1367 // FIXME. Gnu attribute extension ignores use of builtin types in
1368 // attributes. So, __attribute__((iboutletcollection(char))) will be
1369 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001370 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001371 S.Diag(AL.getLoc(),
Richard Smithb1f9a282013-10-31 01:56:18 +00001372 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1373 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001374 return;
1375 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001376
Michael Han99315932013-01-24 16:46:58 +00001377 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001378 IBOutletCollectionAttr(AL.getRange(), S.Context, QTLoc,
1379 AL.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001380}
1381
Hal Finkelee90a222014-09-26 05:04:30 +00001382bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1383 if (RefOkay) {
1384 if (T->isReferenceType())
1385 return true;
1386 } else {
1387 T = T.getNonReferenceType();
1388 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001389
Hal Finkelee90a222014-09-26 05:04:30 +00001390 // The nonnull attribute, and other similar attributes, can be applied to a
1391 // transparent union that contains a pointer type.
Richard Smith588bd9b2014-08-27 04:59:42 +00001392 if (const RecordType *UT = T->getAsUnionType()) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001393 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1394 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001395 for (const auto *I : UD->fields()) {
1396 QualType QT = I->getType();
Richard Smith588bd9b2014-08-27 04:59:42 +00001397 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1398 return true;
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001399 }
1400 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001401 }
1402
1403 return T->isAnyPointerType() || T->isBlockPointerType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001404}
1405
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001406static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &AL,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001407 SourceRange AttrParmRange,
Hal Finkelee90a222014-09-26 05:04:30 +00001408 SourceRange TypeRange,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001409 bool isReturnValue = false) {
Hal Finkelee90a222014-09-26 05:04:30 +00001410 if (!S.isValidPointerAttrType(T)) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001411 if (isReturnValue)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001412 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
1413 << AL.getName() << AttrParmRange << TypeRange;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001414 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001415 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
1416 << AL.getName() << AttrParmRange << TypeRange << 0;
Ted Kremenek9aedc152014-01-17 06:24:56 +00001417 return false;
1418 }
1419 return true;
1420}
1421
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001422static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &AL) {
Joel E. Denny81508102018-03-13 14:51:22 +00001423 SmallVector<ParamIdx, 8> NonNullArgs;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001424 for (unsigned I = 0; I < AL.getNumArgs(); ++I) {
1425 Expr *Ex = AL.getArgAsExpr(I);
Joel E. Denny81508102018-03-13 14:51:22 +00001426 ParamIdx Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001427 if (!checkFunctionOrMethodParameterIndex(S, D, AL, I + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001428 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001429
1430 // Is the function argument a pointer type?
Joel E. Denny81508102018-03-13 14:51:22 +00001431 if (Idx.getASTIndex() < getFunctionOrMethodNumParams(D) &&
1432 !attrNonNullArgCheck(
1433 S, getFunctionOrMethodParamType(D, Idx.getASTIndex()), AL,
1434 Ex->getSourceRange(),
1435 getFunctionOrMethodParamRange(D, Idx.getASTIndex())))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001436 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001437
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001438 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001439 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001440
1441 // If no arguments were specified to __attribute__((nonnull)) then all pointer
Richard Smith588bd9b2014-08-27 04:59:42 +00001442 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1443 // check if the attribute came from a macro expansion or a template
1444 // instantiation.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001445 if (NonNullArgs.empty() && AL.getLoc().isFileID() &&
Richard Smith51ec0cf2017-02-21 01:17:38 +00001446 !S.inTemplateInstantiation()) {
Richard Smith588bd9b2014-08-27 04:59:42 +00001447 bool AnyPointers = isFunctionOrMethodVariadic(D);
1448 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1449 I != E && !AnyPointers; ++I) {
1450 QualType T = getFunctionOrMethodParamType(D, I);
Hal Finkelee90a222014-09-26 05:04:30 +00001451 if (T->isDependentType() || S.isValidPointerAttrType(T))
Richard Smith588bd9b2014-08-27 04:59:42 +00001452 AnyPointers = true;
Ted Kremenek5fa50522008-11-18 06:52:58 +00001453 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001454
Richard Smith588bd9b2014-08-27 04:59:42 +00001455 if (!AnyPointers)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001456 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001457 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001458
Joel E. Denny81508102018-03-13 14:51:22 +00001459 ParamIdx *Start = NonNullArgs.data();
Richard Smith588bd9b2014-08-27 04:59:42 +00001460 unsigned Size = NonNullArgs.size();
1461 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001462 D->addAttr(::new (S.Context)
Joel E. Denny81508102018-03-13 14:51:22 +00001463 NonNullAttr(AL.getRange(), S.Context, Start, Size,
1464 AL.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001465}
1466
Jordan Rosec9399072014-02-11 17:27:59 +00001467static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001468 const AttributeList &AL) {
1469 if (AL.getNumArgs() > 0) {
Jordan Rosec9399072014-02-11 17:27:59 +00001470 if (D->getFunctionType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001471 handleNonNullAttr(S, D, AL);
Jordan Rosec9399072014-02-11 17:27:59 +00001472 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001473 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
Jordan Rosec9399072014-02-11 17:27:59 +00001474 << D->getSourceRange();
1475 }
1476 return;
1477 }
1478
1479 // Is the argument a pointer type?
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001480 if (!attrNonNullArgCheck(S, D->getType(), AL, SourceRange(),
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001481 D->getSourceRange()))
Jordan Rosec9399072014-02-11 17:27:59 +00001482 return;
1483
1484 D->addAttr(::new (S.Context)
Joel E. Denny81508102018-03-13 14:51:22 +00001485 NonNullAttr(AL.getRange(), S.Context, nullptr, 0,
1486 AL.getAttributeSpellingListIndex()));
Jordan Rosec9399072014-02-11 17:27:59 +00001487}
1488
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001489static void handleReturnsNonNullAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001490 const AttributeList &AL) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001491 QualType ResultType = getFunctionOrMethodResultType(D);
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001492 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001493 if (!attrNonNullArgCheck(S, ResultType, AL, SourceRange(), SR,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001494 /* isReturnValue */ true))
1495 return;
1496
1497 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001498 ReturnsNonNullAttr(AL.getRange(), S.Context,
1499 AL.getAttributeSpellingListIndex()));
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001500}
1501
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001502static void handleNoEscapeAttr(Sema &S, Decl *D, const AttributeList &AL) {
Akira Hatanaka98a49332017-09-22 00:41:05 +00001503 if (D->isInvalidDecl())
1504 return;
1505
1506 // noescape only applies to pointer types.
1507 QualType T = cast<ParmVarDecl>(D)->getType();
1508 if (!S.isValidPointerAttrType(T, /* RefOkay */ true)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001509 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
1510 << AL.getName() << AL.getRange() << 0;
Akira Hatanaka98a49332017-09-22 00:41:05 +00001511 return;
1512 }
1513
1514 D->addAttr(::new (S.Context) NoEscapeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001515 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Akira Hatanaka98a49332017-09-22 00:41:05 +00001516}
1517
Hal Finkelee90a222014-09-26 05:04:30 +00001518static void handleAssumeAlignedAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001519 const AttributeList &AL) {
1520 Expr *E = AL.getArgAsExpr(0),
1521 *OE = AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr;
1522 S.AddAssumeAlignedAttr(AL.getRange(), D, E, OE,
1523 AL.getAttributeSpellingListIndex());
Hal Finkelee90a222014-09-26 05:04:30 +00001524}
1525
Erich Keane623efd82017-03-30 21:48:55 +00001526static void handleAllocAlignAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001527 const AttributeList &AL) {
1528 S.AddAllocAlignAttr(AL.getRange(), D, AL.getArgAsExpr(0),
1529 AL.getAttributeSpellingListIndex());
Erich Keane623efd82017-03-30 21:48:55 +00001530}
1531
Hal Finkelee90a222014-09-26 05:04:30 +00001532void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
1533 Expr *OE, unsigned SpellingListIndex) {
1534 QualType ResultType = getFunctionOrMethodResultType(D);
1535 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1536
1537 AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex);
1538 SourceLocation AttrLoc = AttrRange.getBegin();
1539
1540 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1541 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1542 << &TmpAttr << AttrRange << SR;
1543 return;
1544 }
1545
1546 if (!E->isValueDependent()) {
1547 llvm::APSInt I(64);
1548 if (!E->isIntegerConstantExpr(I, Context)) {
1549 if (OE)
1550 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1551 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1552 << E->getSourceRange();
1553 else
1554 Diag(AttrLoc, diag::err_attribute_argument_type)
1555 << &TmpAttr << AANT_ArgumentIntegerConstant
1556 << E->getSourceRange();
1557 return;
1558 }
1559
1560 if (!I.isPowerOf2()) {
1561 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1562 << E->getSourceRange();
1563 return;
1564 }
1565 }
1566
1567 if (OE) {
1568 if (!OE->isValueDependent()) {
1569 llvm::APSInt I(64);
1570 if (!OE->isIntegerConstantExpr(I, Context)) {
1571 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1572 << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1573 << OE->getSourceRange();
1574 return;
1575 }
1576 }
1577 }
1578
1579 D->addAttr(::new (Context)
1580 AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex));
1581}
1582
Erich Keane623efd82017-03-30 21:48:55 +00001583void Sema::AddAllocAlignAttr(SourceRange AttrRange, Decl *D, Expr *ParamExpr,
1584 unsigned SpellingListIndex) {
1585 QualType ResultType = getFunctionOrMethodResultType(D);
1586
Joel E. Denny81508102018-03-13 14:51:22 +00001587 AllocAlignAttr TmpAttr(AttrRange, Context, ParamIdx(), SpellingListIndex);
Erich Keane623efd82017-03-30 21:48:55 +00001588 SourceLocation AttrLoc = AttrRange.getBegin();
1589
1590 if (!ResultType->isDependentType() &&
1591 !isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1592 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1593 << &TmpAttr << AttrRange << getFunctionOrMethodResultSourceRange(D);
1594 return;
1595 }
1596
Joel E. Denny81508102018-03-13 14:51:22 +00001597 ParamIdx Idx;
Erich Keane623efd82017-03-30 21:48:55 +00001598 const auto *FuncDecl = cast<FunctionDecl>(D);
1599 if (!checkFunctionOrMethodParameterIndex(*this, FuncDecl, TmpAttr,
Joel E. Denny81508102018-03-13 14:51:22 +00001600 /*AttrArgNo=*/1, ParamExpr, Idx))
Erich Keane623efd82017-03-30 21:48:55 +00001601 return;
1602
Joel E. Denny81508102018-03-13 14:51:22 +00001603 QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Erich Keane623efd82017-03-30 21:48:55 +00001604 if (!Ty->isDependentType() && !Ty->isIntegralType(Context)) {
1605 Diag(ParamExpr->getLocStart(), diag::err_attribute_integers_only)
Joel E. Denny81508102018-03-13 14:51:22 +00001606 << &TmpAttr
1607 << FuncDecl->getParamDecl(Idx.getASTIndex())->getSourceRange();
Erich Keane623efd82017-03-30 21:48:55 +00001608 return;
1609 }
1610
Joel E. Denny81508102018-03-13 14:51:22 +00001611 D->addAttr(::new (Context)
1612 AllocAlignAttr(AttrRange, Context, Idx, SpellingListIndex));
Erich Keane623efd82017-03-30 21:48:55 +00001613}
1614
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001615/// Normalize the attribute, __foo__ becomes foo.
1616/// Returns true if normalization was applied.
1617static bool normalizeName(StringRef &AttrName) {
Aaron Ballman62692362015-10-09 13:53:24 +00001618 if (AttrName.size() > 4 && AttrName.startswith("__") &&
1619 AttrName.endswith("__")) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001620 AttrName = AttrName.drop_front(2).drop_back(2);
1621 return true;
1622 }
1623 return false;
1624}
1625
Chandler Carruthedc2c642011-07-02 00:01:44 +00001626static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001627 // This attribute must be applied to a function declaration. The first
1628 // argument to the attribute must be an identifier, the name of the resource,
1629 // for example: malloc. The following arguments must be argument indexes, the
1630 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001631 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001632 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001633 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001634
Aaron Ballman00e99962013-08-31 01:11:41 +00001635 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001636 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001637 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001638 return;
1639 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001640
Richard Smith852e9ce2013-11-27 01:46:48 +00001641 // Figure out our Kind.
1642 OwnershipAttr::OwnershipKind K =
Craig Topperc3ec1492014-05-26 06:22:03 +00001643 OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0,
Richard Smith852e9ce2013-11-27 01:46:48 +00001644 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001645
Richard Smith852e9ce2013-11-27 01:46:48 +00001646 // Check arguments.
1647 switch (K) {
1648 case OwnershipAttr::Takes:
1649 case OwnershipAttr::Holds:
1650 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001651 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1652 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001653 return;
1654 }
1655 break;
1656 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001657 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001658 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1659 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001660 return;
1661 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001662 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001663 }
1664
Richard Smith852e9ce2013-11-27 01:46:48 +00001665 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001666
Richard Smith852e9ce2013-11-27 01:46:48 +00001667 StringRef ModuleName = Module->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001668 if (normalizeName(ModuleName)) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001669 Module = &S.PP.getIdentifierTable().get(ModuleName);
1670 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001671
Joel E. Denny81508102018-03-13 14:51:22 +00001672 SmallVector<ParamIdx, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001673 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1674 Expr *Ex = AL.getArgAsExpr(i);
Joel E. Denny81508102018-03-13 14:51:22 +00001675 ParamIdx Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001676 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001677 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001678
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001679 // Is the function argument a pointer type?
Joel E. Denny81508102018-03-13 14:51:22 +00001680 QualType T = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001681 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001682 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001683 case OwnershipAttr::Takes:
1684 case OwnershipAttr::Holds:
1685 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1686 Err = 0;
1687 break;
1688 case OwnershipAttr::Returns:
1689 if (!T->isIntegerType())
1690 Err = 1;
1691 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001692 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001693 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001694 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001695 << Ex->getSourceRange();
1696 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001697 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001698
1699 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001700 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001701 // Cannot have two ownership attributes of different kinds for the same
1702 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001703 if (I->getOwnKind() != K && I->args_end() !=
1704 std::find(I->args_begin(), I->args_end(), Idx)) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001705 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001706 << AL.getName() << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001707 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001708 } else if (K == OwnershipAttr::Returns &&
1709 I->getOwnKind() == OwnershipAttr::Returns) {
1710 // A returns attribute conflicts with any other returns attribute using
Joel E. Denny81508102018-03-13 14:51:22 +00001711 // a different index.
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001712 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1713 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
Joel E. Denny81508102018-03-13 14:51:22 +00001714 << I->args_begin()->getSourceIndex();
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001715 if (I->args_size())
1716 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
Joel E. Denny81508102018-03-13 14:51:22 +00001717 << Idx.getSourceIndex() << Ex->getSourceRange();
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001718 return;
1719 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001720 }
1721 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001722 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001723 }
1724
Joel E. Denny81508102018-03-13 14:51:22 +00001725 ParamIdx *Start = OwnershipArgs.data();
1726 unsigned Size = OwnershipArgs.size();
1727 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001728 D->addAttr(::new (S.Context)
Joel E. Denny81508102018-03-13 14:51:22 +00001729 OwnershipAttr(AL.getLoc(), S.Context, Module, Start, Size,
1730 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001731}
1732
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001733static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &AL) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001734 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001735 if (AL.getNumArgs() > 1) {
1736 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1737 << AL.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001738 return;
1739 }
1740
1741 // gcc rejects
1742 // class c {
1743 // static int a __attribute__((weakref ("v2")));
1744 // static int b() __attribute__((weakref ("f3")));
1745 // };
1746 // and ignores the attributes of
1747 // void f(void) {
1748 // static int a __attribute__((weakref ("v2")));
1749 // }
1750 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001751 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001752 if (!Ctx->isFileContext()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001753 S.Diag(AL.getLoc(), diag::err_attribute_weakref_not_global_context)
1754 << cast<NamedDecl>(D);
Sebastian Redl50c68252010-08-31 00:36:30 +00001755 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001756 }
1757
1758 // The GCC manual says
1759 //
1760 // At present, a declaration to which `weakref' is attached can only
1761 // be `static'.
1762 //
1763 // It also says
1764 //
1765 // Without a TARGET,
1766 // given as an argument to `weakref' or to `alias', `weakref' is
1767 // equivalent to `weak'.
1768 //
1769 // gcc 4.4.1 will accept
1770 // int a7 __attribute__((weakref));
1771 // as
1772 // int a7 __attribute__((weak));
1773 // This looks like a bug in gcc. We reject that for now. We should revisit
1774 // it if this behaviour is actually used.
1775
Rafael Espindolac18086a2010-02-23 22:00:30 +00001776 // GCC rejects
1777 // static ((alias ("y"), weakref)).
1778 // Should we? How to check that weakref is before or after alias?
1779
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001780 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1781 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1782 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001783 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001784 if (AL.getNumArgs() && S.checkStringLiteralArgumentAttr(AL, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001785 // GCC will accept anything as the argument of weakref. Should we
1786 // check for an existing decl?
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001787 D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str,
1788 AL.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001789
Michael Han99315932013-01-24 16:46:58 +00001790 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001791 WeakRefAttr(AL.getRange(), S.Context,
1792 AL.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001793}
1794
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001795static void handleIFuncAttr(Sema &S, Decl *D, const AttributeList &AL) {
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001796 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001797 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001798 return;
1799
1800 // Aliases should be on declarations, not definitions.
1801 const auto *FD = cast<FunctionDecl>(D);
1802 if (FD->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001803 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 1;
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001804 return;
1805 }
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001806
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001807 D->addAttr(::new (S.Context) IFuncAttr(AL.getRange(), S.Context, Str,
1808 AL.getAttributeSpellingListIndex()));
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001809}
1810
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001811static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &AL) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001812 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001813 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001814 return;
1815
Douglas Gregore8bbc122011-09-02 00:18:52 +00001816 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001817 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_darwin);
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001818 return;
1819 }
Justin Lebara8f0254b2016-01-23 21:28:10 +00001820 if (S.Context.getTargetInfo().getTriple().isNVPTX()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001821 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_nvptx);
Justin Lebara8f0254b2016-01-23 21:28:10 +00001822 }
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001823
David Majnemer2dc81462015-01-19 09:00:28 +00001824 // Aliases should be on declarations, not definitions.
1825 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1826 if (FD->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001827 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 0;
David Majnemer2dc81462015-01-19 09:00:28 +00001828 return;
1829 }
1830 } else {
1831 const auto *VD = cast<VarDecl>(D);
1832 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001833 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << VD << 0;
David Majnemer2dc81462015-01-19 09:00:28 +00001834 return;
1835 }
1836 }
1837
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001838 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001839
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001840 D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str,
1841 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001842}
1843
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001844static void handleTLSModelAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001845 const AttributeList &AL) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001846 StringRef Model;
1847 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001848 // Check that it is a string.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001849 if (!S.checkStringLiteralArgumentAttr(AL, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001850 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001851
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001852 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001853 if (Model != "global-dynamic" && Model != "local-dynamic"
1854 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001855 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001856 return;
1857 }
1858
Michael Han99315932013-01-24 16:46:58 +00001859 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001860 TLSModelAttr(AL.getRange(), S.Context, Model,
1861 AL.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001862}
1863
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001864static void handleRestrictAttr(Sema &S, Decl *D, const AttributeList &AL) {
David Majnemer631a90b2015-02-04 07:23:21 +00001865 QualType ResultType = getFunctionOrMethodResultType(D);
1866 if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
1867 D->addAttr(::new (S.Context) RestrictAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001868 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
David Majnemer631a90b2015-02-04 07:23:21 +00001869 return;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001870 }
1871
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001872 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
1873 << AL.getName() << getFunctionOrMethodResultSourceRange(D);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001874}
1875
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001876static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &AL) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001877 if (S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001878 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
1879 << AL.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001880 return;
1881 }
1882
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001883 if (CommonAttr *CA = S.mergeCommonAttr(D, AL.getRange(), AL.getName(),
1884 AL.getAttributeSpellingListIndex()))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001885 D->addAttr(CA);
Eric Christopher8a2ee392010-12-02 02:45:55 +00001886}
1887
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001888static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &AL) {
1889 if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, AL.getRange(),
1890 AL.getName()))
Charles Davis0e379112016-08-08 21:19:08 +00001891 return;
1892
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001893 if (AL.isDeclspecAttribute()) {
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00001894 const auto &Triple = S.getASTContext().getTargetInfo().getTriple();
1895 const auto &Arch = Triple.getArch();
1896 if (Arch != llvm::Triple::x86 &&
1897 (Arch != llvm::Triple::arm && Arch != llvm::Triple::thumb)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001898 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_on_arch)
1899 << AL.getName() << Triple.getArchName();
Saleem Abdulrasoolb51bcaf2017-04-07 15:13:47 +00001900 return;
1901 }
1902 }
1903
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001904 D->addAttr(::new (S.Context) NakedAttr(AL.getRange(), S.Context,
1905 AL.getAttributeSpellingListIndex()));
Charles Davis0e379112016-08-08 21:19:08 +00001906}
1907
Erich Keaneb11ebc52017-09-27 03:20:13 +00001908static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &Attrs) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001909 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001910
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001911 if (!isa<ObjCMethodDecl>(D)) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00001912 S.Diag(Attrs.getLoc(), diag::warn_attribute_wrong_decl_type)
1913 << Attrs.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001914 return;
1915 }
1916
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001917 D->addAttr(::new (S.Context) NoReturnAttr(
Erich Keaneb11ebc52017-09-27 03:20:13 +00001918 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001919}
1920
Oren Ben Simhon220671a2018-03-17 13:31:35 +00001921static void handleNoCfCheckAttr(Sema &S, Decl *D, const AttributeList &Attrs) {
1922 if (!S.getLangOpts().CFProtectionBranch)
1923 S.Diag(Attrs.getLoc(), diag::warn_nocf_check_attribute_ignored);
1924 else
1925 handleSimpleAttribute<AnyX86NoCfCheckAttr>(S, D, Attrs);
John McCall3882ace2011-01-05 12:14:39 +00001926}
1927
Oren Ben Simhon220671a2018-03-17 13:31:35 +00001928bool Sema::CheckAttrNoArgs(const AttributeList &Attrs) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00001929 if (!checkAttributeNumArgs(*this, Attrs, 0)) {
1930 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00001931 return true;
1932 }
1933
1934 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001935}
1936
Oren Ben Simhon220671a2018-03-17 13:31:35 +00001937bool Sema::CheckAttrTarget(const AttributeList &AL) {
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001938 // Check whether the attribute is valid on the current target.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001939 if (!AL.existsInTarget(Context.getTargetInfo())) {
1940 Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) << AL.getName();
1941 AL.setInvalid();
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001942 return true;
1943 }
1944
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00001945 return false;
1946}
1947
Chandler Carruthedc2c642011-07-02 00:01:44 +00001948static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001949 const AttributeList &AL) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001950
1951 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1952 // because 'analyzer_noreturn' does not impact the type.
David Majnemer06864812015-04-07 06:01:53 +00001953 if (!isFunctionOrMethodOrBlock(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001954 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00001955 if (!VD || (!VD->getType()->isBlockPointerType() &&
1956 !VD->getType()->isFunctionPointerType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001957 S.Diag(AL.getLoc(),
1958 AL.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Craig Topper8f7f3ea2015-11-17 05:40:05 +00001959 : diag::warn_attribute_wrong_decl_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001960 << AL.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001961 return;
1962 }
1963 }
1964
Michael Han99315932013-01-24 16:46:58 +00001965 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001966 AnalyzerNoReturnAttr(AL.getRange(), S.Context,
1967 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001968}
1969
John Thompsoncdb847ba2010-08-09 21:53:52 +00001970// PS3 PPU-specific.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001971static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &AL) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001972/*
1973 Returning a Vector Class in Registers
1974
Eric Christopherbc638a82010-12-01 22:13:54 +00001975 According to the PPU ABI specifications, a class with a single member of
1976 vector type is returned in memory when used as the return value of a function.
1977 This results in inefficient code when implementing vector classes. To return
1978 the value in a single vector register, add the vecreturn attribute to the
1979 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001980
1981 Example:
1982
1983 struct Vector
1984 {
1985 __vector float xyzw;
1986 } __attribute__((vecreturn));
1987
1988 Vector Add(Vector lhs, Vector rhs)
1989 {
1990 Vector result;
1991 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1992 return result; // This will be returned in a register
1993 }
1994*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001995 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00001996 S.Diag(AL.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001997 return;
1998 }
1999
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002000 const auto *R = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00002001 int count = 0;
2002
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002003 if (!isa<CXXRecordDecl>(R)) {
2004 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00002005 return;
2006 }
2007
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002008 if (!cast<CXXRecordDecl>(R)->isPOD()) {
2009 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
John Thompson9a587aaa2010-09-18 01:12:07 +00002010 return;
2011 }
2012
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002013 for (const auto *I : R->fields()) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002014 if ((count == 1) || !I->getType()->isVectorType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002015 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
John Thompson9a587aaa2010-09-18 01:12:07 +00002016 return;
2017 }
2018 count++;
2019 }
2020
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002021 D->addAttr(::new (S.Context) VecReturnAttr(
2022 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00002023}
2024
Richard Smithe233fbf2013-01-28 22:42:45 +00002025static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002026 const AttributeList &AL) {
Richard Smithe233fbf2013-01-28 22:42:45 +00002027 if (isa<ParmVarDecl>(D)) {
2028 // [[carries_dependency]] can only be applied to a parameter if it is a
2029 // parameter of a function declaration or lambda.
2030 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002031 S.Diag(AL.getLoc(),
Richard Smithe233fbf2013-01-28 22:42:45 +00002032 diag::err_carries_dependency_param_not_function_decl);
2033 return;
2034 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002035 }
Richard Smithe233fbf2013-01-28 22:42:45 +00002036
2037 D->addAttr(::new (S.Context) CarriesDependencyAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002038 AL.getRange(), S.Context,
2039 AL.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002040}
2041
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002042static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &AL) {
2043 bool IsCXX17Attr = AL.isCXX11Attribute() && !AL.getScopeName();
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002044
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002045 if (IsCXX17Attr && isa<VarDecl>(D)) {
2046 // The C++17 spelling of this attribute cannot be applied to a static data
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002047 // member per [dcl.attr.unused]p2.
2048 if (cast<VarDecl>(D)->isStaticDataMember()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002049 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
2050 << AL.getName() << ExpectedForMaybeUnused;
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002051 return;
2052 }
2053 }
2054
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002055 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002056 // about using it as an extension.
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002057 if (!S.getLangOpts().CPlusPlus17 && IsCXX17Attr)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002058 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL.getName();
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002059
2060 D->addAttr(::new (S.Context) UnusedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002061 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00002062}
2063
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002064static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002065 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002066 if (AL.getNumArgs() &&
2067 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002068 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002069
Michael Han99315932013-01-24 16:46:58 +00002070 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002071 ConstructorAttr(AL.getRange(), S.Context, priority,
2072 AL.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002073}
2074
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002075static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00002076 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002077 if (AL.getNumArgs() &&
2078 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002079 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002080
Michael Han99315932013-01-24 16:46:58 +00002081 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002082 DestructorAttr(AL.getRange(), S.Context, priority,
2083 AL.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002084}
2085
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002086template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00002087static void handleAttrWithMessage(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002088 const AttributeList &AL) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002089 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002090 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002091 if (AL.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002092 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002093
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002094 D->addAttr(::new (S.Context) AttrTy(AL.getRange(), S.Context, Str,
2095 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00002096}
2097
Ted Kremenek438f8db2014-02-22 01:06:05 +00002098static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002099 const AttributeList &AL) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00002100 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002101 S.Diag(AL.getLoc(), diag::err_objc_attr_protocol_requires_definition)
2102 << AL.getName() << AL.getRange();
Ted Kremenek27cfe102014-02-21 22:49:04 +00002103 return;
2104 }
2105
Ted Kremenek28eace62013-11-23 01:01:34 +00002106 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002107 ObjCExplicitProtocolImplAttr(AL.getRange(), S.Context,
2108 AL.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00002109}
2110
Jordy Rose740b0c22012-05-08 03:27:22 +00002111static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2112 IdentifierInfo *Platform,
2113 VersionTuple Introduced,
2114 VersionTuple Deprecated,
2115 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002116 StringRef PlatformName
2117 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2118 if (PlatformName.empty())
2119 PlatformName = Platform->getName();
2120
2121 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2122 // of these steps are needed).
2123 if (!Introduced.empty() && !Deprecated.empty() &&
2124 !(Introduced <= Deprecated)) {
2125 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2126 << 1 << PlatformName << Deprecated.getAsString()
2127 << 0 << Introduced.getAsString();
2128 return true;
2129 }
2130
2131 if (!Introduced.empty() && !Obsoleted.empty() &&
2132 !(Introduced <= Obsoleted)) {
2133 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2134 << 2 << PlatformName << Obsoleted.getAsString()
2135 << 0 << Introduced.getAsString();
2136 return true;
2137 }
2138
2139 if (!Deprecated.empty() && !Obsoleted.empty() &&
2140 !(Deprecated <= Obsoleted)) {
2141 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2142 << 2 << PlatformName << Obsoleted.getAsString()
2143 << 1 << Deprecated.getAsString();
2144 return true;
2145 }
2146
2147 return false;
2148}
2149
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002150/// \brief Check whether the two versions match.
2151///
2152/// If either version tuple is empty, then they are assumed to match. If
2153/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2154static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2155 bool BeforeIsOkay) {
2156 if (X.empty() || Y.empty())
2157 return true;
2158
2159 if (X == Y)
2160 return true;
2161
2162 if (BeforeIsOkay && X < Y)
2163 return true;
2164
2165 return false;
2166}
2167
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002168AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002169 IdentifierInfo *Platform,
Manman Ren719a8642016-05-06 21:04:01 +00002170 bool Implicit,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002171 VersionTuple Introduced,
2172 VersionTuple Deprecated,
2173 VersionTuple Obsoleted,
2174 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002175 StringRef Message,
Manman Rend8039df2016-02-22 04:47:24 +00002176 bool IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002177 StringRef Replacement,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002178 AvailabilityMergeKind AMK,
Michael Han99315932013-01-24 16:46:58 +00002179 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002180 VersionTuple MergedIntroduced = Introduced;
2181 VersionTuple MergedDeprecated = Deprecated;
2182 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002183 bool FoundAny = false;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002184 bool OverrideOrImpl = false;
2185 switch (AMK) {
2186 case AMK_None:
2187 case AMK_Redeclaration:
2188 OverrideOrImpl = false;
2189 break;
2190
2191 case AMK_Override:
2192 case AMK_ProtocolImplementation:
2193 OverrideOrImpl = true;
2194 break;
2195 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002196
Rafael Espindolac67f2232012-05-10 02:50:16 +00002197 if (D->hasAttrs()) {
2198 AttrVec &Attrs = D->getAttrs();
2199 for (unsigned i = 0, e = Attrs.size(); i != e;) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002200 const auto *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002201 if (!OldAA) {
2202 ++i;
2203 continue;
2204 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002205
Rafael Espindolac67f2232012-05-10 02:50:16 +00002206 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2207 if (OldPlatform != Platform) {
2208 ++i;
2209 continue;
2210 }
2211
Tim Northover7a73cc72015-10-30 16:30:49 +00002212 // If there is an existing availability attribute for this platform that
2213 // is explicit and the new one is implicit use the explicit one and
2214 // discard the new implicit attribute.
Manman Ren719a8642016-05-06 21:04:01 +00002215 if (!OldAA->isImplicit() && Implicit) {
Tim Northover7a73cc72015-10-30 16:30:49 +00002216 return nullptr;
2217 }
2218
2219 // If there is an existing attribute for this platform that is implicit
2220 // and the new attribute is explicit then erase the old one and
2221 // continue processing the attributes.
Manman Ren719a8642016-05-06 21:04:01 +00002222 if (!Implicit && OldAA->isImplicit()) {
Tim Northover7a73cc72015-10-30 16:30:49 +00002223 Attrs.erase(Attrs.begin() + i);
2224 --e;
2225 continue;
2226 }
2227
Rafael Espindolac67f2232012-05-10 02:50:16 +00002228 FoundAny = true;
2229 VersionTuple OldIntroduced = OldAA->getIntroduced();
2230 VersionTuple OldDeprecated = OldAA->getDeprecated();
2231 VersionTuple OldObsoleted = OldAA->getObsoleted();
2232 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002233
Douglas Gregord2a713e2015-09-30 21:27:42 +00002234 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
2235 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
2236 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002237 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregord2a713e2015-09-30 21:27:42 +00002238 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
2239 if (OverrideOrImpl) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002240 int Which = -1;
2241 VersionTuple FirstVersion;
2242 VersionTuple SecondVersion;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002243 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002244 Which = 0;
2245 FirstVersion = OldIntroduced;
2246 SecondVersion = Introduced;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002247 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002248 Which = 1;
2249 FirstVersion = Deprecated;
2250 SecondVersion = OldDeprecated;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002251 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002252 Which = 2;
2253 FirstVersion = Obsoleted;
2254 SecondVersion = OldObsoleted;
2255 }
2256
2257 if (Which == -1) {
2258 Diag(OldAA->getLocation(),
2259 diag::warn_mismatched_availability_override_unavail)
Douglas Gregord2a713e2015-09-30 21:27:42 +00002260 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2261 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002262 } else {
2263 Diag(OldAA->getLocation(),
2264 diag::warn_mismatched_availability_override)
2265 << Which
2266 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
Douglas Gregord2a713e2015-09-30 21:27:42 +00002267 << FirstVersion.getAsString() << SecondVersion.getAsString()
2268 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002269 }
Douglas Gregord2a713e2015-09-30 21:27:42 +00002270 if (AMK == AMK_Override)
2271 Diag(Range.getBegin(), diag::note_overridden_method);
2272 else
2273 Diag(Range.getBegin(), diag::note_protocol_method);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002274 } else {
2275 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2276 Diag(Range.getBegin(), diag::note_previous_attribute);
2277 }
2278
Rafael Espindolac67f2232012-05-10 02:50:16 +00002279 Attrs.erase(Attrs.begin() + i);
2280 --e;
2281 continue;
2282 }
2283
2284 VersionTuple MergedIntroduced2 = MergedIntroduced;
2285 VersionTuple MergedDeprecated2 = MergedDeprecated;
2286 VersionTuple MergedObsoleted2 = MergedObsoleted;
2287
2288 if (MergedIntroduced2.empty())
2289 MergedIntroduced2 = OldIntroduced;
2290 if (MergedDeprecated2.empty())
2291 MergedDeprecated2 = OldDeprecated;
2292 if (MergedObsoleted2.empty())
2293 MergedObsoleted2 = OldObsoleted;
2294
2295 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2296 MergedIntroduced2, MergedDeprecated2,
2297 MergedObsoleted2)) {
2298 Attrs.erase(Attrs.begin() + i);
2299 --e;
2300 continue;
2301 }
2302
2303 MergedIntroduced = MergedIntroduced2;
2304 MergedDeprecated = MergedDeprecated2;
2305 MergedObsoleted = MergedObsoleted2;
2306 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002307 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002308 }
2309
2310 if (FoundAny &&
2311 MergedIntroduced == Introduced &&
2312 MergedDeprecated == Deprecated &&
2313 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00002314 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002315
Douglas Gregord2a713e2015-09-30 21:27:42 +00002316 // Only create a new attribute if !OverrideOrImpl, but we want to do
Ted Kremenekb5445722013-04-06 00:34:27 +00002317 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002318 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002319 MergedDeprecated, MergedObsoleted) &&
Douglas Gregord2a713e2015-09-30 21:27:42 +00002320 !OverrideOrImpl) {
Manman Ren719a8642016-05-06 21:04:01 +00002321 auto *Avail = ::new (Context) AvailabilityAttr(Range, Context, Platform,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002322 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002323 Obsoleted, IsUnavailable, Message,
Manman Ren75bc6762016-03-21 17:30:55 +00002324 IsStrict, Replacement,
2325 AttrSpellingListIndex);
Manman Ren719a8642016-05-06 21:04:01 +00002326 Avail->setImplicit(Implicit);
2327 return Avail;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002328 }
Craig Topperc3ec1492014-05-26 06:22:03 +00002329 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002330}
2331
Chandler Carruthedc2c642011-07-02 00:01:44 +00002332static void handleAvailabilityAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002333 const AttributeList &AL) {
2334 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00002335 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002336 IdentifierLoc *Platform = AL.getArgAsIdent(0);
2337 unsigned Index = AL.getAttributeSpellingListIndex();
Michael Han99315932013-01-24 16:46:58 +00002338
Aaron Ballman00e99962013-08-31 01:11:41 +00002339 IdentifierInfo *II = Platform->Ident;
2340 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2341 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2342 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002343
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002344 auto *ND = dyn_cast<NamedDecl>(D);
Alex Lorenz472cc792017-04-20 09:35:02 +00002345 if (!ND) // We warned about this already, so just return.
Rafael Espindolac231fab2013-01-08 21:30:32 +00002346 return;
Rafael Espindolac231fab2013-01-08 21:30:32 +00002347
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002348 AvailabilityChange Introduced = AL.getAvailabilityIntroduced();
2349 AvailabilityChange Deprecated = AL.getAvailabilityDeprecated();
2350 AvailabilityChange Obsoleted = AL.getAvailabilityObsoleted();
2351 bool IsUnavailable = AL.getUnavailableLoc().isValid();
2352 bool IsStrict = AL.getStrictLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002353 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002354 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002355 Str = SE->getString();
Manman Ren75bc6762016-03-21 17:30:55 +00002356 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002357 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getReplacementExpr()))
Manman Ren75bc6762016-03-21 17:30:55 +00002358 Replacement = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002359
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002360 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, AL.getRange(), II,
Manman Ren719a8642016-05-06 21:04:01 +00002361 false/*Implicit*/,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002362 Introduced.Version,
2363 Deprecated.Version,
2364 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002365 IsUnavailable, Str,
Manman Ren75bc6762016-03-21 17:30:55 +00002366 IsStrict, Replacement,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002367 Sema::AMK_None,
Michael Han99315932013-01-24 16:46:58 +00002368 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002369 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002370 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002371
2372 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2373 // matches before the start of the watchOS platform.
2374 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2375 IdentifierInfo *NewII = nullptr;
2376 if (II->getName() == "ios")
2377 NewII = &S.Context.Idents.get("watchos");
2378 else if (II->getName() == "ios_app_extension")
2379 NewII = &S.Context.Idents.get("watchos_app_extension");
2380
2381 if (NewII) {
2382 auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple {
2383 if (Version.empty())
2384 return Version;
2385 auto Major = Version.getMajor();
2386 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2387 if (NewMajor >= 2) {
2388 if (Version.getMinor().hasValue()) {
2389 if (Version.getSubminor().hasValue())
2390 return VersionTuple(NewMajor, Version.getMinor().getValue(),
2391 Version.getSubminor().getValue());
2392 else
2393 return VersionTuple(NewMajor, Version.getMinor().getValue());
2394 }
2395 }
2396
2397 return VersionTuple(2, 0);
2398 };
2399
2400 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2401 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2402 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2403
2404 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002405 AL.getRange(),
Tim Northover7a73cc72015-10-30 16:30:49 +00002406 NewII,
Manman Ren719a8642016-05-06 21:04:01 +00002407 true/*Implicit*/,
Tim Northover7a73cc72015-10-30 16:30:49 +00002408 NewIntroduced,
2409 NewDeprecated,
2410 NewObsoleted,
2411 IsUnavailable, Str,
Manman Rend8039df2016-02-22 04:47:24 +00002412 IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002413 Replacement,
Tim Northover7a73cc72015-10-30 16:30:49 +00002414 Sema::AMK_None,
2415 Index);
2416 if (NewAttr)
2417 D->addAttr(NewAttr);
2418 }
2419 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2420 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2421 // matches before the start of the tvOS platform.
2422 IdentifierInfo *NewII = nullptr;
2423 if (II->getName() == "ios")
2424 NewII = &S.Context.Idents.get("tvos");
2425 else if (II->getName() == "ios_app_extension")
2426 NewII = &S.Context.Idents.get("tvos_app_extension");
2427
2428 if (NewII) {
2429 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002430 AL.getRange(),
Tim Northover7a73cc72015-10-30 16:30:49 +00002431 NewII,
Manman Ren719a8642016-05-06 21:04:01 +00002432 true/*Implicit*/,
Tim Northover7a73cc72015-10-30 16:30:49 +00002433 Introduced.Version,
2434 Deprecated.Version,
2435 Obsoleted.Version,
2436 IsUnavailable, Str,
Manman Rend8039df2016-02-22 04:47:24 +00002437 IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002438 Replacement,
Tim Northover7a73cc72015-10-30 16:30:49 +00002439 Sema::AMK_None,
2440 Index);
2441 if (NewAttr)
2442 D->addAttr(NewAttr);
2443 }
2444 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002445}
2446
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002447static void handleExternalSourceSymbolAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002448 const AttributeList &AL) {
2449 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002450 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002451 assert(checkAttributeAtMostNumArgs(S, AL, 3) &&
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002452 "Invalid number of arguments in an external_source_symbol attribute");
2453
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002454 StringRef Language;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002455 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(0)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002456 Language = SE->getString();
2457 StringRef DefinedIn;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002458 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(1)))
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002459 DefinedIn = SE->getString();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002460 bool IsGeneratedDeclaration = AL.getArgAsIdent(2) != nullptr;
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002461
2462 D->addAttr(::new (S.Context) ExternalSourceSymbolAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002463 AL.getRange(), S.Context, Language, DefinedIn, IsGeneratedDeclaration,
2464 AL.getAttributeSpellingListIndex()));
Alex Lorenzd5d27e12017-03-01 18:06:25 +00002465}
2466
John McCalld041a9b2013-02-20 01:54:26 +00002467template <class T>
2468static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2469 typename T::VisibilityType value,
2470 unsigned attrSpellingListIndex) {
2471 T *existingAttr = D->getAttr<T>();
2472 if (existingAttr) {
2473 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2474 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00002475 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00002476 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2477 S.Diag(range.getBegin(), diag::note_previous_attribute);
2478 D->dropAttr<T>();
2479 }
2480 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2481}
2482
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002483VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002484 VisibilityAttr::VisibilityType Vis,
2485 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002486 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2487 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002488}
2489
John McCalld041a9b2013-02-20 01:54:26 +00002490TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2491 TypeVisibilityAttr::VisibilityType Vis,
2492 unsigned AttrSpellingListIndex) {
2493 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2494 AttrSpellingListIndex);
2495}
2496
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002497static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &AL,
John McCalld041a9b2013-02-20 01:54:26 +00002498 bool isTypeVisibility) {
2499 // Visibility attributes don't mean anything on a typedef.
2500 if (isa<TypedefNameDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002501 S.Diag(AL.getRange().getBegin(), diag::warn_attribute_ignored)
2502 << AL.getName();
John McCalld041a9b2013-02-20 01:54:26 +00002503 return;
2504 }
2505
2506 // 'type_visibility' can only go on a type or namespace.
2507 if (isTypeVisibility &&
2508 !(isa<TagDecl>(D) ||
2509 isa<ObjCInterfaceDecl>(D) ||
2510 isa<NamespaceDecl>(D))) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002511 S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2512 << AL.getName() << ExpectedTypeOrNamespace;
John McCalld041a9b2013-02-20 01:54:26 +00002513 return;
2514 }
2515
Benjamin Kramer70370212013-09-09 15:08:57 +00002516 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002517 StringRef TypeStr;
2518 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002519 if (!S.checkStringLiteralArgumentAttr(AL, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002520 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002521
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002522 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002523 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002524 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002525 << AL.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002526 return;
2527 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002528
2529 // Complain about attempts to use protected visibility on targets
2530 // (like Darwin) that don't support it.
2531 if (type == VisibilityAttr::Protected &&
2532 !S.Context.getTargetInfo().hasProtectedVisibility()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002533 S.Diag(AL.getLoc(), diag::warn_attribute_protected_visibility);
Aaron Ballman682ee422013-09-11 19:47:58 +00002534 type = VisibilityAttr::Default;
2535 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002536
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002537 unsigned Index = AL.getAttributeSpellingListIndex();
2538 Attr *newAttr;
John McCalld041a9b2013-02-20 01:54:26 +00002539 if (isTypeVisibility) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002540 newAttr = S.mergeTypeVisibilityAttr(D, AL.getRange(),
John McCalld041a9b2013-02-20 01:54:26 +00002541 (TypeVisibilityAttr::VisibilityType) type,
2542 Index);
2543 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002544 newAttr = S.mergeVisibilityAttr(D, AL.getRange(), type, Index);
John McCalld041a9b2013-02-20 01:54:26 +00002545 }
2546 if (newAttr)
2547 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002548}
2549
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002550static void handleObjCMethodFamilyAttr(Sema &S, Decl *D,
2551 const AttributeList &AL) {
2552 const auto *M = cast<ObjCMethodDecl>(D);
2553 if (!AL.isArgIdent(0)) {
2554 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
2555 << AL.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002556 return;
2557 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002558
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002559 IdentifierLoc *IL = AL.getArgAsIdent(0);
Aaron Ballman682ee422013-09-11 19:47:58 +00002560 ObjCMethodFamilyAttr::FamilyKind F;
2561 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002562 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
2563 << AL.getName() << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002564 return;
2565 }
2566
Alp Toker314cc812014-01-25 16:55:45 +00002567 if (F == ObjCMethodFamilyAttr::OMF_init &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002568 !M->getReturnType()->isObjCObjectPointerType()) {
2569 S.Diag(M->getLocation(), diag::err_init_method_bad_return_type)
2570 << M->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002571 // Ignore the attribute.
2572 return;
2573 }
2574
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002575 D->addAttr(new (S.Context) ObjCMethodFamilyAttr(
2576 AL.getRange(), S.Context, F, AL.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002577}
2578
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002579static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &AL) {
2580 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002581 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002582 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002583 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2584 return;
2585 }
2586 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002587 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002588 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002589 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002590 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2591 return;
2592 }
2593 }
2594 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002595 // It is okay to include this attribute on properties, e.g.:
2596 //
2597 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2598 //
2599 // In this case it follows tradition and suppresses an error in the above
2600 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002601 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002602 }
Michael Han99315932013-01-24 16:46:58 +00002603 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002604 ObjCNSObjectAttr(AL.getRange(), S.Context,
2605 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002606}
2607
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002608static void handleObjCIndependentClass(Sema &S, Decl *D, const AttributeList &AL) {
2609 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002610 QualType T = TD->getUnderlyingType();
2611 if (!T->isObjCObjectPointerType()) {
2612 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2613 return;
2614 }
2615 } else {
2616 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2617 return;
2618 }
2619 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002620 ObjCIndependentClassAttr(AL.getRange(), S.Context,
2621 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002622}
2623
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002624static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &AL) {
2625 if (!AL.isArgIdent(0)) {
2626 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
2627 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002628 return;
2629 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002630
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002631 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002632 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002633 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002634 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
2635 << AL.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002636 return;
2637 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002638
Michael Han99315932013-01-24 16:46:58 +00002639 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002640 BlocksAttr(AL.getRange(), S.Context, type,
2641 AL.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002642}
2643
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002644static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002645 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002646 if (AL.getNumArgs() > 0) {
2647 Expr *E = AL.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002648 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002649 if (E->isTypeDependent() || E->isValueDependent() ||
2650 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002651 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
2652 << AL.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002653 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002654 return;
2655 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002656
John McCallb46f2872011-09-09 07:56:05 +00002657 if (Idx.isSigned() && Idx.isNegative()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002658 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_less_than_zero)
Chris Lattner3b054132008-11-19 05:08:23 +00002659 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002660 return;
2661 }
John McCallb46f2872011-09-09 07:56:05 +00002662
2663 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002664 }
2665
Aaron Ballman18a78382013-11-21 00:28:23 +00002666 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002667 if (AL.getNumArgs() > 1) {
2668 Expr *E = AL.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002669 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002670 if (E->isTypeDependent() || E->isValueDependent() ||
2671 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002672 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
2673 << AL.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002674 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002675 return;
2676 }
2677 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002678
John McCallb46f2872011-09-09 07:56:05 +00002679 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002680 // FIXME: This error message could be improved, it would be nice
2681 // to say what the bounds actually are.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002682 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
Chris Lattner3b054132008-11-19 05:08:23 +00002683 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002684 return;
2685 }
2686 }
2687
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002688 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002689 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002690 if (isa<FunctionNoProtoType>(FT)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002691 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_named_arguments);
Chris Lattner9363e312009-03-17 23:03:47 +00002692 return;
2693 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002694
Chris Lattner9363e312009-03-17 23:03:47 +00002695 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002696 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002697 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002698 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002699 } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002700 if (!MD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002701 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002702 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002703 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002704 } else if (const auto *BD = dyn_cast<BlockDecl>(D)) {
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002705 if (!BD->isVariadic()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002706 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002707 return;
2708 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002709 } else if (const auto *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002710 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002711 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002712 const FunctionType *FT = Ty->isFunctionPointerType()
2713 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002714 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002715 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002716 int m = Ty->isFunctionPointerType() ? 0 : 1;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002717 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002718 return;
2719 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002720 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002721 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
2722 << AL.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002723 return;
2724 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002725 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002726 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
2727 << AL.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002728 return;
2729 }
Michael Han99315932013-01-24 16:46:58 +00002730 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002731 SentinelAttr(AL.getRange(), S.Context, sentinel, nullPos,
2732 AL.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002733}
2734
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002735static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &AL) {
Alp Toker314cc812014-01-25 16:55:45 +00002736 if (D->getFunctionType() &&
2737 D->getFunctionType()->getReturnType()->isVoidType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002738 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method)
2739 << AL.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002740 return;
2741 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002742 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002743 if (MD->getReturnType()->isVoidType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002744 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method)
2745 << AL.getName() << 1;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002746 return;
2747 }
2748
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002749 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
Aaron Ballmane7964782016-03-07 22:44:55 +00002750 // about using it as an extension.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002751 if (!S.getLangOpts().CPlusPlus17 && AL.isCXX11Attribute() &&
2752 !AL.getScopeName())
2753 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL.getName();
Aaron Ballmane7964782016-03-07 22:44:55 +00002754
Michael Han99315932013-01-24 16:46:58 +00002755 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002756 WarnUnusedResultAttr(AL.getRange(), S.Context,
2757 AL.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002758}
2759
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002760static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &AL) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002761 // weak_import only applies to variable & function declarations.
2762 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002763 if (!D->canBeWeakImported(isDef)) {
2764 if (isDef)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002765 S.Diag(AL.getLoc(), diag::warn_attribute_invalid_on_definition)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002766 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002767 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002768 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002769 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002770 // Nothing to warn about here.
2771 } else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002772 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
2773 << AL.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002774
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002775 return;
2776 }
2777
Michael Han99315932013-01-24 16:46:58 +00002778 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002779 WeakImportAttr(AL.getRange(), S.Context,
2780 AL.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002781}
2782
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002783// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002784template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002785static void handleWorkGroupSize(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002786 const AttributeList &AL) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002787 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002788 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002789 const Expr *E = AL.getArgAsExpr(i);
2790 if (!checkUInt32Argument(S, AL, E, WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002791 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002792 if (WGSize[i] == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002793 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
2794 << AL.getName() << E->getSourceRange();
Joey Goulyb1d23a82014-05-19 14:41:38 +00002795 return;
2796 }
2797 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002798
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002799 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2800 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2801 Existing->getYDim() == WGSize[1] &&
2802 Existing->getZDim() == WGSize[2]))
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002803 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002804
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002805 D->addAttr(::new (S.Context) WorkGroupAttr(AL.getRange(), S.Context,
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002806 WGSize[0], WGSize[1], WGSize[2],
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002807 AL.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002808}
2809
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002810// Handles intel_reqd_sub_group_size.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002811static void handleSubGroupSize(Sema &S, Decl *D, const AttributeList &AL) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002812 uint32_t SGSize;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002813 const Expr *E = AL.getArgAsExpr(0);
2814 if (!checkUInt32Argument(S, AL, E, SGSize))
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002815 return;
2816 if (SGSize == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002817 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
2818 << AL.getName() << E->getSourceRange();
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002819 return;
2820 }
2821
2822 OpenCLIntelReqdSubGroupSizeAttr *Existing =
2823 D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>();
2824 if (Existing && Existing->getSubGroupSize() != SGSize)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002825 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL.getName();
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002826
2827 D->addAttr(::new (S.Context) OpenCLIntelReqdSubGroupSizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002828 AL.getRange(), S.Context, SGSize,
2829 AL.getAttributeSpellingListIndex()));
Xiuli Panbe6da4b2017-05-04 07:31:20 +00002830}
2831
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002832static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &AL) {
2833 if (!AL.hasParsedType()) {
2834 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
2835 << AL.getName() << 1;
Aaron Ballman00e99962013-08-31 01:11:41 +00002836 return;
2837 }
2838
Craig Topperc3ec1492014-05-26 06:22:03 +00002839 TypeSourceInfo *ParmTSI = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002840 QualType ParmType = S.GetTypeFromParser(AL.getTypeArg(), &ParmTSI);
Richard Smithb87c4652013-10-31 21:23:20 +00002841 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002842
2843 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2844 (ParmType->isBooleanType() ||
2845 !ParmType->isIntegralType(S.getASTContext()))) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002846 S.Diag(AL.getLoc(), diag::err_attribute_argument_vec_type_hint)
Joey Goulyaba589c2013-03-08 09:42:32 +00002847 << ParmType;
2848 return;
2849 }
2850
Aaron Ballmana9e05402013-12-02 22:16:55 +00002851 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002852 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002853 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL.getName();
Joey Goulyaba589c2013-03-08 09:42:32 +00002854 return;
2855 }
2856 }
2857
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002858 D->addAttr(::new (S.Context) VecTypeHintAttr(AL.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002859 ParmTSI,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002860 AL.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002861}
2862
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002863SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002864 StringRef Name,
2865 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002866 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2867 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002868 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002869 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2870 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002871 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002872 }
Michael Han99315932013-01-24 16:46:58 +00002873 return ::new (Context) SectionAttr(Range, Context, Name,
2874 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002875}
2876
Reid Kleckner2a133222015-03-04 23:39:17 +00002877bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2878 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2879 if (!Error.empty()) {
2880 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error;
2881 return false;
2882 }
2883 return true;
2884}
2885
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002886static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &AL) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002887 // Make sure that there is a string literal as the sections's single
2888 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002889 StringRef Str;
2890 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002891 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002892 return;
Mike Stump11289f42009-09-09 15:08:12 +00002893
Reid Kleckner2a133222015-03-04 23:39:17 +00002894 if (!S.checkSectionName(LiteralLoc, Str))
2895 return;
2896
Chris Lattner30ba6742009-08-10 19:03:04 +00002897 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002898 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002899 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002900 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002901 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002902 return;
2903 }
Mike Stump11289f42009-09-09 15:08:12 +00002904
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002905 unsigned Index = AL.getAttributeSpellingListIndex();
2906 SectionAttr *NewAttr = S.mergeSectionAttr(D, AL.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002907 if (NewAttr)
2908 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002909}
2910
Erich Keane57e15cd2017-07-19 22:06:33 +00002911// Check for things we'd like to warn about. Multiversioning issues are
2912// handled later in the process, once we know how many exist.
2913bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
2914 enum FirstParam { Unsupported, Duplicate };
2915 enum SecondParam { None, Architecture };
Eric Christopher789a7ad2015-06-12 01:36:05 +00002916 for (auto Str : {"tune=", "fpmath="})
2917 if (AttrStr.find(Str) != StringRef::npos)
Erich Keane57e15cd2017-07-19 22:06:33 +00002918 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
2919 << Unsupported << None << Str;
2920
2921 TargetAttr::ParsedTargetAttr ParsedAttrs = TargetAttr::parse(AttrStr);
2922
2923 if (!ParsedAttrs.Architecture.empty() &&
2924 !Context.getTargetInfo().isValidCPUName(ParsedAttrs.Architecture))
2925 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
2926 << Unsupported << Architecture << ParsedAttrs.Architecture;
2927
2928 if (ParsedAttrs.DuplicateArchitecture)
2929 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
2930 << Duplicate << None << "arch=";
2931
2932 for (const auto &Feature : ParsedAttrs.Features) {
2933 auto CurFeature = StringRef(Feature).drop_front(); // remove + or -.
2934 if (!Context.getTargetInfo().isValidFeatureName(CurFeature))
2935 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
2936 << Unsupported << None << CurFeature;
2937 }
2938
Erich Keane29636aa2018-02-16 17:31:59 +00002939 return false;
Eric Christopher789a7ad2015-06-12 01:36:05 +00002940}
2941
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002942static void handleTargetAttr(Sema &S, Decl *D, const AttributeList &AL) {
Eric Christopher11acf732015-06-12 01:35:52 +00002943 StringRef Str;
2944 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002945 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc) ||
Erich Keane29636aa2018-02-16 17:31:59 +00002946 S.checkTargetAttr(LiteralLoc, Str))
Eric Christopher11acf732015-06-12 01:35:52 +00002947 return;
Erich Keane29636aa2018-02-16 17:31:59 +00002948
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002949 unsigned Index = AL.getAttributeSpellingListIndex();
Eric Christopher11acf732015-06-12 01:35:52 +00002950 TargetAttr *NewAttr =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002951 ::new (S.Context) TargetAttr(AL.getRange(), S.Context, Str, Index);
Eric Christopher11acf732015-06-12 01:35:52 +00002952 D->addAttr(NewAttr);
2953}
2954
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002955static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &AL) {
2956 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002957 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00002958 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002959 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002960
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002961 // gcc only allows for simple identifiers. Since we support more than gcc, we
2962 // will warn the user.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002963 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002964 if (DRE->hasQualifier())
2965 S.Diag(Loc, diag::warn_cleanup_ext);
2966 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2967 NI = DRE->getNameInfo();
2968 if (!FD) {
2969 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2970 << NI.getName();
2971 return;
2972 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00002973 } else if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002974 if (ULE->hasExplicitTemplateArgs())
2975 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002976 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2977 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002978 if (!FD) {
2979 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2980 << NI.getName();
2981 if (ULE->getType() == S.Context.OverloadTy)
2982 S.NoteAllOverloadCandidates(ULE);
2983 return;
2984 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002985 } else {
2986 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002987 return;
2988 }
2989
Anders Carlssond277d792009-01-31 01:16:18 +00002990 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002991 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2992 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002993 return;
2994 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002995
Anders Carlsson723f55d2009-02-07 23:16:50 +00002996 // We're currently more strict than GCC about what function types we accept.
2997 // If this ever proves to be a problem it should be easy to fix.
Aaron Ballman3b70e752017-12-01 16:53:49 +00002998 QualType Ty = S.Context.getPointerType(cast<VarDecl>(D)->getType());
Anders Carlsson723f55d2009-02-07 23:16:50 +00002999 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00003000 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
3001 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00003002 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
3003 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00003004 return;
3005 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003006
Michael Han99315932013-01-24 16:46:58 +00003007 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003008 CleanupAttr(AL.getRange(), S.Context, FD,
3009 AL.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00003010}
3011
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003012static void handleEnumExtensibilityAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003013 const AttributeList &AL) {
3014 if (!AL.isArgIdent(0)) {
3015 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
3016 << AL.getName() << 0 << AANT_ArgumentIdentifier;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003017 return;
3018 }
3019
3020 EnumExtensibilityAttr::Kind ExtensibilityKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003021 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003022 if (!EnumExtensibilityAttr::ConvertStrToKind(II->getName(),
3023 ExtensibilityKind)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003024 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
3025 << AL.getName() << II;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003026 return;
3027 }
3028
3029 D->addAttr(::new (S.Context) EnumExtensibilityAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003030 AL.getRange(), S.Context, ExtensibilityKind,
3031 AL.getAttributeSpellingListIndex()));
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003032}
3033
Mike Stumpd3bb5572009-07-24 19:02:52 +00003034/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00003035/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003036static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &AL) {
3037 Expr *IdxExpr = AL.getArgAsExpr(0);
Joel E. Denny81508102018-03-13 14:51:22 +00003038 ParamIdx Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003039 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003040 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00003041
Eric Christopherb64963e2015-08-13 21:34:35 +00003042 // Make sure the format string is really a string.
Joel E. Denny81508102018-03-13 14:51:22 +00003043 QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
Mike Stumpd3bb5572009-07-24 19:02:52 +00003044
Eric Christopherb64963e2015-08-13 21:34:35 +00003045 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
3046 if (NotNSStringTy &&
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003047 !isCFStringType(Ty, S.Context) &&
3048 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003049 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003050 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003051 << "a string type" << IdxExpr->getSourceRange()
3052 << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003053 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003054 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003055 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003056 if (!isNSStringType(Ty, S.Context) &&
3057 !isCFStringType(Ty, S.Context) &&
3058 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003059 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003060 S.Diag(AL.getLoc(), diag::err_format_attribute_result_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00003061 << (NotNSStringTy ? "string type" : "NSString")
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00003062 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003063 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003064 }
3065
Joel E. Denny81508102018-03-13 14:51:22 +00003066 D->addAttr(::new (S.Context) FormatArgAttr(
3067 AL.getRange(), S.Context, Idx, AL.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003068}
3069
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003070enum FormatAttrKind {
3071 CFStringFormat,
3072 NSStringFormat,
3073 StrftimeFormat,
3074 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003075 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003076 InvalidFormat
3077};
3078
3079/// getFormatAttrKind - Map from format attribute names to supported format
3080/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003081static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003082 return llvm::StringSwitch<FormatAttrKind>(Format)
Mehdi Amini06d367c2016-10-24 20:39:34 +00003083 // Check for formats that get handled specially.
3084 .Case("NSString", NSStringFormat)
3085 .Case("CFString", CFStringFormat)
3086 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003087
Mehdi Amini06d367c2016-10-24 20:39:34 +00003088 // Otherwise, check for supported formats.
3089 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3090 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3091 .Case("kprintf", SupportedFormat) // OpenBSD.
3092 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
3093 .Case("os_trace", SupportedFormat)
3094 .Case("os_log", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003095
Mehdi Amini06d367c2016-10-24 20:39:34 +00003096 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3097 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003098}
3099
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003100/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003101/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003102static void handleInitPriorityAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003103 const AttributeList &AL) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003104 if (!S.getLangOpts().CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003105 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL.getName();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003106 return;
3107 }
3108
Aaron Ballman4a611152013-11-27 16:34:09 +00003109 if (S.getCurFunctionOrMethodDecl()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003110 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3111 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003112 return;
3113 }
Aaron Ballman4a611152013-11-27 16:34:09 +00003114 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003115 if (S.Context.getAsArrayType(T))
3116 T = S.Context.getBaseElementType(T);
3117 if (!T->getAs<RecordType>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003118 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3119 AL.setInvalid();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003120 return;
3121 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003122
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003123 Expr *E = AL.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003124 uint32_t prioritynum;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003125 if (!checkUInt32Argument(S, AL, E, prioritynum)) {
3126 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003127 return;
3128 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003129
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003130 if (prioritynum < 101 || prioritynum > 65535) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003131 S.Diag(AL.getLoc(), diag::err_attribute_argument_outof_range)
3132 << E->getSourceRange() << AL.getName() << 101 << 65535;
3133 AL.setInvalid();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003134 return;
3135 }
Michael Han99315932013-01-24 16:46:58 +00003136 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003137 InitPriorityAttr(AL.getRange(), S.Context, prioritynum,
3138 AL.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003139}
3140
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003141FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3142 IdentifierInfo *Format, int FormatIdx,
3143 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00003144 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003145 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003146 for (auto *F : D->specific_attrs<FormatAttr>()) {
3147 if (F->getType() == Format &&
3148 F->getFormatIdx() == FormatIdx &&
3149 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003150 // If we don't have a valid location for this attribute, adopt the
3151 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003152 if (F->getLocation().isInvalid())
3153 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00003154 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00003155 }
3156 }
3157
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003158 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3159 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003160}
3161
Mike Stumpd3bb5572009-07-24 19:02:52 +00003162/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003163/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003164static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &AL) {
3165 if (!AL.isArgIdent(0)) {
3166 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
3167 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003168 return;
3169 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003170
Chandler Carruth743682b2010-11-16 08:35:43 +00003171 // In C++ the implicit 'this' function parameter also counts, and they are
3172 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003173 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00003174 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003175
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003176 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
Aaron Ballman00e99962013-08-31 01:11:41 +00003177 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003178
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00003179 if (normalizeName(Format)) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003180 // If we've modified the string name, we need a new identifier for it.
3181 II = &S.Context.Idents.get(Format);
3182 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003183
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003184 // Check for supported formats.
3185 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00003186
3187 if (Kind == IgnoredFormat)
3188 return;
3189
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003190 if (Kind == InvalidFormat) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003191 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
3192 << AL.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003193 return;
3194 }
3195
3196 // checks for the 2nd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003197 Expr *IdxExpr = AL.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003198 uint32_t Idx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003199 if (!checkUInt32Argument(S, AL, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003200 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003201
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003202 if (Idx < 1 || Idx > NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003203 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3204 << AL.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003205 return;
3206 }
3207
3208 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003209 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003210
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003211 if (HasImplicitThisParam) {
3212 if (ArgIdx == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003213 S.Diag(AL.getLoc(),
Chandler Carruth743682b2010-11-16 08:35:43 +00003214 diag::err_format_attribute_implicit_this_format_string)
3215 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003216 return;
3217 }
3218 ArgIdx--;
3219 }
Mike Stump11289f42009-09-09 15:08:12 +00003220
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003221 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00003222 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003223
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003224 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003225 if (!isCFStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003226 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003227 << "a CFString" << IdxExpr->getSourceRange()
3228 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00003229 return;
3230 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003231 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003232 // FIXME: do we need to check if the type is NSString*? What are the
3233 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003234 if (!isNSStringType(Ty, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003235 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003236 << "an NSString" << IdxExpr->getSourceRange()
3237 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003238 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003239 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003240 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003241 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003242 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00003243 << "a string type" << IdxExpr->getSourceRange()
3244 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003245 return;
3246 }
3247
3248 // check the 3rd argument
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003249 Expr *FirstArgExpr = AL.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003250 uint32_t FirstArg;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003251 if (!checkUInt32Argument(S, AL, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003252 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003253
3254 // check if the function is variadic if the 3rd argument non-zero
3255 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003256 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003257 ++NumArgs; // +1 for ...
3258 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003259 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003260 return;
3261 }
3262 }
3263
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003264 // strftime requires FirstArg to be 0 because it doesn't read from any
3265 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003266 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003267 if (FirstArg != 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003268 S.Diag(AL.getLoc(), diag::err_format_strftime_third_parameter)
Chris Lattner3b054132008-11-19 05:08:23 +00003269 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003270 return;
3271 }
3272 // if 0 it disables parameter checking (to use with e.g. va_list)
3273 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003274 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3275 << AL.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003276 return;
3277 }
3278
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003279 FormatAttr *NewAttr = S.mergeFormatAttr(D, AL.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003280 Idx, FirstArg,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003281 AL.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003282 if (NewAttr)
3283 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003284}
3285
Chandler Carruthedc2c642011-07-02 00:01:44 +00003286static void handleTransparentUnionAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003287 const AttributeList &AL) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003288 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00003289 RecordDecl *RD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003290 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003291 if (TD && TD->getUnderlyingType()->isUnionType())
3292 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3293 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003294 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003295
3296 if (!RD || !RD->isUnion()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003297 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
3298 << AL.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003299 return;
3300 }
3301
John McCallf937c022011-10-07 06:10:15 +00003302 if (!RD->isCompleteDefinition()) {
Erich Keane2fe684b2017-02-28 20:44:39 +00003303 if (!RD->isBeingDefined())
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003304 S.Diag(AL.getLoc(),
Erich Keane2fe684b2017-02-28 20:44:39 +00003305 diag::warn_transparent_union_attribute_not_definition);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003306 return;
3307 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003308
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003309 RecordDecl::field_iterator Field = RD->field_begin(),
3310 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003311 if (Field == FieldEnd) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003312 S.Diag(AL.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003313 return;
3314 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003315
David Blaikie40ed2972012-06-06 20:45:41 +00003316 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003317 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003318 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003319 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003320 diag::warn_transparent_union_attribute_floating)
3321 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003322 return;
3323 }
3324
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003325 if (FirstType->isIncompleteType())
3326 return;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003327 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3328 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3329 for (; Field != FieldEnd; ++Field) {
3330 QualType FieldType = Field->getType();
Alex Lorenz6f4bc4f2016-10-06 09:47:29 +00003331 if (FieldType->isIncompleteType())
3332 return;
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003333 // FIXME: this isn't fully correct; we also need to test whether the
3334 // members of the union would all have the same calling convention as the
3335 // first member of the union. Checking just the size and alignment isn't
3336 // sufficient (consider structs passed on the stack instead of in registers
3337 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003338 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003339 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003340 // Warn if we drop the attribute.
3341 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003342 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003343 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003344 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003345 diag::warn_transparent_union_attribute_field_size_align)
3346 << isSize << Field->getDeclName() << FieldBits;
3347 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003348 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003349 diag::note_transparent_union_first_field_size_align)
3350 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003351 return;
3352 }
3353 }
3354
Michael Han99315932013-01-24 16:46:58 +00003355 RD->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003356 TransparentUnionAttr(AL.getRange(), S.Context,
3357 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003358}
3359
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003360static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003361 // Make sure that there is a string literal as the annotation's single
3362 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003363 StringRef Str;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003364 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003365 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003366
3367 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003368 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
3369 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003370 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003371 }
Michael Han99315932013-01-24 16:46:58 +00003372
3373 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003374 AnnotateAttr(AL.getRange(), S.Context, Str,
3375 AL.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003376}
3377
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003378static void handleAlignValueAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003379 const AttributeList &AL) {
3380 S.AddAlignValueAttr(AL.getRange(), D, AL.getArgAsExpr(0),
3381 AL.getAttributeSpellingListIndex());
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003382}
3383
3384void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
3385 unsigned SpellingListIndex) {
3386 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
3387 SourceLocation AttrLoc = AttrRange.getBegin();
3388
3389 QualType T;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003390 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003391 T = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003392 else if (const auto *VD = dyn_cast<ValueDecl>(D))
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003393 T = VD->getType();
3394 else
3395 llvm_unreachable("Unknown decl type for align_value");
3396
3397 if (!T->isDependentType() && !T->isAnyPointerType() &&
3398 !T->isReferenceType() && !T->isMemberPointerType()) {
3399 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
3400 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
3401 return;
3402 }
3403
3404 if (!E->isValueDependent()) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003405 llvm::APSInt Alignment;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003406 ExprResult ICE
3407 = VerifyIntegerConstantExpression(E, &Alignment,
3408 diag::err_align_value_attribute_argument_not_int,
3409 /*AllowFold*/ false);
3410 if (ICE.isInvalid())
3411 return;
3412
3413 if (!Alignment.isPowerOf2()) {
3414 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3415 << E->getSourceRange();
3416 return;
3417 }
3418
3419 D->addAttr(::new (Context)
3420 AlignValueAttr(AttrRange, Context, ICE.get(),
3421 SpellingListIndex));
3422 return;
3423 }
3424
3425 // Save dependent expressions in the AST to be instantiated.
3426 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003427}
3428
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003429static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &AL) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003430 // check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003431 if (AL.getNumArgs() > 1) {
3432 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
3433 << AL.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003434 return;
3435 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003436
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003437 if (AL.getNumArgs() == 0) {
3438 D->addAttr(::new (S.Context) AlignedAttr(AL.getRange(), S.Context,
3439 true, nullptr, AL.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00003440 return;
3441 }
3442
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003443 Expr *E = AL.getArgAsExpr(0);
3444 if (AL.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3445 S.Diag(AL.getEllipsisLoc(),
Richard Smith44c247f2013-02-22 08:32:16 +00003446 diag::err_pack_expansion_without_parameter_packs);
3447 return;
3448 }
3449
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003450 if (!AL.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
Richard Smith44c247f2013-02-22 08:32:16 +00003451 return;
3452
David Majnemer26a1e0e2015-04-07 02:37:09 +00003453 if (E->isValueDependent()) {
3454 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3455 if (!TND->getUnderlyingType()->isDependentType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003456 S.Diag(AL.getLoc(), diag::err_alignment_dependent_typedef_name)
David Majnemer26a1e0e2015-04-07 02:37:09 +00003457 << E->getSourceRange();
3458 return;
3459 }
3460 }
3461 }
3462
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003463 S.AddAlignedAttr(AL.getRange(), D, E, AL.getAttributeSpellingListIndex(),
3464 AL.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003465}
3466
3467void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003468 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003469 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3470 SourceLocation AttrLoc = AttrRange.getBegin();
3471
Richard Smith1dba27c2013-01-29 09:02:09 +00003472 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003473 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003474 // C++11 [dcl.align]p1:
3475 // An alignment-specifier may be applied to a variable or to a class
3476 // data member, but it shall not be applied to a bit-field, a function
3477 // parameter, the formal parameter of a catch clause, or a variable
3478 // declared with the register storage class specifier. An
3479 // alignment-specifier may also be applied to the declaration of a class
3480 // or enumeration type.
3481 // C11 6.7.5/2:
3482 // An alignment attribute shall not be specified in a declaration of
3483 // a typedef, or a bit-field, or a function, or a parameter, or an
3484 // object declared with the register storage-class specifier.
3485 int DiagKind = -1;
3486 if (isa<ParmVarDecl>(D)) {
3487 DiagKind = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003488 } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003489 if (VD->getStorageClass() == SC_Register)
3490 DiagKind = 1;
3491 if (VD->isExceptionVariable())
3492 DiagKind = 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003493 } else if (const auto *FD = dyn_cast<FieldDecl>(D)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003494 if (FD->isBitField())
3495 DiagKind = 3;
3496 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00003497 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00003498 << (TmpAttr.isC11() ? ExpectedVariableOrField
3499 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003500 return;
3501 }
3502 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003503 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00003504 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003505 return;
3506 }
3507 }
3508
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003509 if (E->isTypeDependent() || E->isValueDependent()) {
3510 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003511 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3512 AA->setPackExpansion(IsPackExpansion);
3513 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003514 return;
3515 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003516
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003517 // FIXME: Cache the number on the AL object?
David Majnemer0be6bd02015-07-26 09:02:21 +00003518 llvm::APSInt Alignment;
Douglas Gregore2b37442012-05-04 22:38:52 +00003519 ExprResult ICE
3520 = VerifyIntegerConstantExpression(E, &Alignment,
3521 diag::err_aligned_attribute_argument_not_int,
3522 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003523 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003524 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003525
David Majnemer0be6bd02015-07-26 09:02:21 +00003526 uint64_t AlignVal = Alignment.getZExtValue();
3527
Richard Smith848e1f12013-02-01 08:12:08 +00003528 // C++11 [dcl.align]p2:
3529 // -- if the constant expression evaluates to zero, the alignment
3530 // specifier shall have no effect
3531 // C11 6.7.5p6:
3532 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003533 if (!(TmpAttr.isAlignas() && !Alignment)) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003534 if (!llvm::isPowerOf2_64(AlignVal)) {
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003535 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3536 << E->getSourceRange();
3537 return;
3538 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003539 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003540
David Majnemerabecae72014-02-12 20:36:10 +00003541 // Alignment calculations can wrap around if it's greater than 2**28.
David Majnemer29c69db2015-07-26 01:48:59 +00003542 unsigned MaxValidAlignment =
3543 Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192
3544 : 268435456;
David Majnemer0be6bd02015-07-26 09:02:21 +00003545 if (AlignVal > MaxValidAlignment) {
David Majnemerabecae72014-02-12 20:36:10 +00003546 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
3547 << E->getSourceRange();
3548 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003549 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003550
David Majnemer0be6bd02015-07-26 09:02:21 +00003551 if (Context.getTargetInfo().isTLSSupported()) {
3552 unsigned MaxTLSAlign =
3553 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3554 .getQuantity();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003555 const auto *VD = dyn_cast<VarDecl>(D);
David Majnemer0be6bd02015-07-26 09:02:21 +00003556 if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD &&
3557 VD->getTLSKind() != VarDecl::TLS_None) {
3558 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3559 << (unsigned)AlignVal << VD << MaxTLSAlign;
3560 return;
3561 }
3562 }
3563
Richard Smith44c247f2013-02-22 08:32:16 +00003564 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003565 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00003566 AA->setPackExpansion(IsPackExpansion);
3567 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003568}
3569
Michael Hanaf02bbe2013-02-01 01:19:17 +00003570void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003571 unsigned SpellingListIndex, bool IsPackExpansion) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003572 // FIXME: Cache the number on the AL object if non-dependent?
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003573 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003574 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3575 SpellingListIndex);
3576 AA->setPackExpansion(IsPackExpansion);
3577 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003578}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003579
Richard Smith848e1f12013-02-01 08:12:08 +00003580void Sema::CheckAlignasUnderalignment(Decl *D) {
3581 assert(D->hasAttrs() && "no attributes on decl");
3582
David Majnemer475b25e2015-01-21 10:54:38 +00003583 QualType UnderlyingTy, DiagTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003584 if (const auto *VD = dyn_cast<ValueDecl>(D)) {
David Majnemer475b25e2015-01-21 10:54:38 +00003585 UnderlyingTy = DiagTy = VD->getType();
3586 } else {
3587 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003588 if (const auto *ED = dyn_cast<EnumDecl>(D))
David Majnemer475b25e2015-01-21 10:54:38 +00003589 UnderlyingTy = ED->getIntegerType();
3590 }
3591 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003592 return;
3593
3594 // C++11 [dcl.align]p5, C11 6.7.5/4:
3595 // The combined effect of all alignment attributes in a declaration shall
3596 // not specify an alignment that is less strict than the alignment that
3597 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003598 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003599 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003600 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003601 if (I->isAlignmentDependent())
3602 return;
3603 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003604 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003605 Align = std::max(Align, I->getAlignment(Context));
3606 }
3607
3608 if (AlignasAttr && Align) {
3609 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003610 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003611 if (NaturalAlign > RequestedAlign)
3612 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003613 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003614 }
3615}
3616
David Majnemer2c4e00a2014-01-29 22:07:36 +00003617bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003618 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003619 MSInheritanceAttr::Spelling SemanticSpelling) {
3620 assert(RD->hasDefinition() && "RD has no definition!");
3621
David Majnemer98c9ee22014-02-07 00:43:07 +00003622 // We may not have seen base specifiers or any virtual methods yet. We will
3623 // have to wait until the record is defined to catch any mismatches.
3624 if (!RD->getDefinition()->isCompleteDefinition())
3625 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003626
David Majnemer98c9ee22014-02-07 00:43:07 +00003627 // The unspecified model never matches what a definition could need.
3628 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
3629 return false;
3630
David Majnemer4bb09802014-02-10 19:50:15 +00003631 if (BestCase) {
3632 if (RD->calculateInheritanceModel() == SemanticSpelling)
3633 return false;
3634 } else {
3635 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3636 return false;
3637 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003638
3639 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3640 << 0 /*definition*/;
3641 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3642 << RD->getNameAsString();
3643 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003644}
3645
Alexey Bataevf278eb12015-11-19 10:13:11 +00003646/// parseModeAttrArg - Parses attribute mode string and returns parsed type
3647/// attribute.
3648static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
3649 bool &IntegerMode, bool &ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003650 IntegerMode = true;
3651 ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003652 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003653 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003654 switch (Str[0]) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003655 case 'Q':
3656 DestWidth = 8;
3657 break;
3658 case 'H':
3659 DestWidth = 16;
3660 break;
3661 case 'S':
3662 DestWidth = 32;
3663 break;
3664 case 'D':
3665 DestWidth = 64;
3666 break;
3667 case 'X':
3668 DestWidth = 96;
3669 break;
3670 case 'T':
3671 DestWidth = 128;
3672 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003673 }
3674 if (Str[1] == 'F') {
3675 IntegerMode = false;
3676 } else if (Str[1] == 'C') {
3677 IntegerMode = false;
3678 ComplexMode = true;
3679 } else if (Str[1] != 'I') {
3680 DestWidth = 0;
3681 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003682 break;
3683 case 4:
3684 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3685 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003686 if (Str == "word")
Reid Klecknerf27e7522016-02-01 18:58:24 +00003687 DestWidth = S.Context.getTargetInfo().getRegisterWidth();
Daniel Dunbarafff4342009-10-18 02:09:24 +00003688 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003689 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003690 break;
3691 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003692 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003693 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003694 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003695 case 11:
3696 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003697 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003698 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003699 }
Alexey Bataevf278eb12015-11-19 10:13:11 +00003700}
3701
3702/// handleModeAttr - This attribute modifies the width of a decl with primitive
3703/// type.
3704///
3705/// Despite what would be logical, the mode attribute is a decl attribute, not a
3706/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3707/// HImode, not an intermediate pointer.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003708static void handleModeAttr(Sema &S, Decl *D, const AttributeList &AL) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003709 // This attribute isn't documented, but glibc uses it. It changes
3710 // the width of an int or unsigned int to the specified size.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003711 if (!AL.isArgIdent(0)) {
3712 S.Diag(AL.getLoc(), diag::err_attribute_argument_type) << AL.getName()
Alexey Bataevf278eb12015-11-19 10:13:11 +00003713 << AANT_ArgumentIdentifier;
3714 return;
3715 }
3716
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003717 IdentifierInfo *Name = AL.getArgAsIdent(0)->Ident;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003718
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003719 S.AddModeAttr(AL.getRange(), D, Name, AL.getAttributeSpellingListIndex());
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003720}
3721
3722void Sema::AddModeAttr(SourceRange AttrRange, Decl *D, IdentifierInfo *Name,
3723 unsigned SpellingListIndex, bool InInstantiation) {
3724 StringRef Str = Name->getName();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003725 normalizeName(Str);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003726 SourceLocation AttrLoc = AttrRange.getBegin();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003727
3728 unsigned DestWidth = 0;
3729 bool IntegerMode = true;
3730 bool ComplexMode = false;
3731 llvm::APInt VectorSize(64, 0);
3732 if (Str.size() >= 4 && Str[0] == 'V') {
3733 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
3734 size_t StrSize = Str.size();
3735 size_t VectorStringLength = 0;
3736 while ((VectorStringLength + 1) < StrSize &&
3737 isdigit(Str[VectorStringLength + 1]))
3738 ++VectorStringLength;
3739 if (VectorStringLength &&
3740 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
3741 VectorSize.isPowerOf2()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003742 parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth,
Alexey Bataevf278eb12015-11-19 10:13:11 +00003743 IntegerMode, ComplexMode);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003744 // Avoid duplicate warning from template instantiation.
3745 if (!InInstantiation)
3746 Diag(AttrLoc, diag::warn_vector_mode_deprecated);
Alexey Bataevf278eb12015-11-19 10:13:11 +00003747 } else {
3748 VectorSize = 0;
3749 }
3750 }
3751
3752 if (!VectorSize)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003753 parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode);
3754
3755 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3756 // and friends, at least with glibc.
3757 // FIXME: Make sure floating-point mappings are accurate
3758 // FIXME: Support XF and TF types
3759 if (!DestWidth) {
3760 Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name;
3761 return;
3762 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003763
3764 QualType OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003765 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003766 OldTy = TD->getUnderlyingType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003767 else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003768 // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'.
3769 // Try to get type from enum declaration, default to int.
3770 OldTy = ED->getIntegerType();
3771 if (OldTy.isNull())
3772 OldTy = Context.IntTy;
3773 } else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003774 OldTy = cast<ValueDecl>(D)->getType();
Eli Friedman4735374e2009-03-03 06:41:03 +00003775
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003776 if (OldTy->isDependentType()) {
3777 D->addAttr(::new (Context)
3778 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
3779 return;
3780 }
3781
Alexey Bataev326057d2015-06-19 07:46:21 +00003782 // Base type can also be a vector type (see PR17453).
3783 // Distinguish between base type and base element type.
3784 QualType OldElemTy = OldTy;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003785 if (const auto *VT = OldTy->getAs<VectorType>())
Alexey Bataev326057d2015-06-19 07:46:21 +00003786 OldElemTy = VT->getElementType();
3787
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003788 // GCC allows 'mode' attribute on enumeration types (even incomplete), except
3789 // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete
3790 // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected.
3791 if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) &&
3792 VectorSize.getBoolValue()) {
3793 Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << AttrRange;
3794 return;
3795 }
3796 bool IntegralOrAnyEnumType =
3797 OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>();
3798
3799 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() &&
3800 !IntegralOrAnyEnumType)
3801 Diag(AttrLoc, diag::err_mode_not_primitive);
Eli Friedman4735374e2009-03-03 06:41:03 +00003802 else if (IntegerMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003803 if (!IntegralOrAnyEnumType)
3804 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00003805 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003806 if (!OldElemTy->isComplexType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003807 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00003808 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00003809 if (!OldElemTy->isFloatingType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003810 Diag(AttrLoc, diag::err_mode_wrong_type);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003811 }
3812
Alexey Bataev326057d2015-06-19 07:46:21 +00003813 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003814
3815 if (IntegerMode)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003816 NewElemTy = Context.getIntTypeForBitwidth(DestWidth,
3817 OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003818 else
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003819 NewElemTy = Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003820
Alexey Bataev326057d2015-06-19 07:46:21 +00003821 if (NewElemTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003822 Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003823 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003824 }
3825
Eli Friedman4735374e2009-03-03 06:41:03 +00003826 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003827 NewElemTy = Context.getComplexType(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00003828 }
3829
3830 QualType NewTy = NewElemTy;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003831 if (VectorSize.getBoolValue()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003832 NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(),
3833 VectorType::GenericVector);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003834 } else if (const auto *OldVT = OldTy->getAs<VectorType>()) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003835 // Complex machine mode does not support base vector types.
3836 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003837 Diag(AttrLoc, diag::err_complex_mode_vector_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00003838 return;
3839 }
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003840 unsigned NumElements = Context.getTypeSize(OldElemTy) *
Alexey Bataev326057d2015-06-19 07:46:21 +00003841 OldVT->getNumElements() /
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003842 Context.getTypeSize(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00003843 NewTy =
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003844 Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
Alexey Bataev326057d2015-06-19 07:46:21 +00003845 }
3846
3847 if (NewTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003848 Diag(AttrLoc, diag::err_mode_wrong_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00003849 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003850 }
3851
3852 // Install the new type.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003853 if (auto *TD = dyn_cast<TypedefNameDecl>(D))
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003854 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003855 else if (auto *ED = dyn_cast<EnumDecl>(D))
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003856 ED->setIntegerType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003857 else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003858 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003859
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003860 D->addAttr(::new (Context)
3861 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003862}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003863
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003864static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &AL) {
Michael Han99315932013-01-24 16:46:58 +00003865 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003866 NoDebugAttr(AL.getRange(), S.Context,
3867 AL.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003868}
3869
Paul Robinson30e41fb2014-12-15 18:57:28 +00003870AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range,
Paul Robinson080b1f32015-01-13 18:34:56 +00003871 IdentifierInfo *Ident,
Paul Robinson30e41fb2014-12-15 18:57:28 +00003872 unsigned AttrSpellingListIndex) {
3873 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003874 Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00003875 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3876 return nullptr;
3877 }
3878
3879 if (D->hasAttr<AlwaysInlineAttr>())
3880 return nullptr;
3881
3882 return ::new (Context) AlwaysInlineAttr(Range, Context,
3883 AttrSpellingListIndex);
3884}
3885
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003886CommonAttr *Sema::mergeCommonAttr(Decl *D, SourceRange Range,
3887 IdentifierInfo *Ident,
3888 unsigned AttrSpellingListIndex) {
3889 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, Range, Ident))
3890 return nullptr;
3891
3892 return ::new (Context) CommonAttr(Range, Context, AttrSpellingListIndex);
3893}
3894
3895InternalLinkageAttr *
3896Sema::mergeInternalLinkageAttr(Decl *D, SourceRange Range,
3897 IdentifierInfo *Ident,
3898 unsigned AttrSpellingListIndex) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003899 if (const auto *VD = dyn_cast<VarDecl>(D)) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003900 // Attribute applies to Var but not any subclass of it (like ParmVar,
3901 // ImplicitParm or VarTemplateSpecialization).
3902 if (VD->getKind() != Decl::Var) {
3903 Diag(Range.getBegin(), diag::warn_attribute_wrong_decl_type)
3904 << Ident << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
3905 : ExpectedVariableOrFunction);
3906 return nullptr;
3907 }
3908 // Attribute does not apply to non-static local variables.
3909 if (VD->hasLocalStorage()) {
3910 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
3911 return nullptr;
3912 }
3913 }
3914
3915 if (checkAttrMutualExclusion<CommonAttr>(*this, D, Range, Ident))
3916 return nullptr;
3917
3918 return ::new (Context)
3919 InternalLinkageAttr(Range, Context, AttrSpellingListIndex);
3920}
3921
Paul Robinson30e41fb2014-12-15 18:57:28 +00003922MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range,
3923 unsigned AttrSpellingListIndex) {
3924 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
3925 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'";
3926 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3927 return nullptr;
3928 }
3929
3930 if (D->hasAttr<MinSizeAttr>())
3931 return nullptr;
3932
3933 return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex);
3934}
3935
3936OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range,
3937 unsigned AttrSpellingListIndex) {
3938 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
3939 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
3940 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3941 D->dropAttr<AlwaysInlineAttr>();
3942 }
3943 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
3944 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
3945 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3946 D->dropAttr<MinSizeAttr>();
3947 }
3948
3949 if (D->hasAttr<OptimizeNoneAttr>())
3950 return nullptr;
3951
3952 return ::new (Context) OptimizeNoneAttr(Range, Context,
3953 AttrSpellingListIndex);
3954}
3955
Paul Robinsonf0674352014-03-31 22:29:15 +00003956static void handleAlwaysInlineAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003957 const AttributeList &AL) {
3958 if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, AL.getRange(),
3959 AL.getName()))
Akira Hatanakac8667622015-11-06 23:56:15 +00003960 return;
3961
Paul Robinson080b1f32015-01-13 18:34:56 +00003962 if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003963 D, AL.getRange(), AL.getName(),
3964 AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00003965 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00003966}
3967
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003968static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &AL) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003969 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003970 D, AL.getRange(), AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00003971 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00003972}
3973
Paul Robinsonf0674352014-03-31 22:29:15 +00003974static void handleOptimizeNoneAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003975 const AttributeList &AL) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003976 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003977 D, AL.getRange(), AL.getAttributeSpellingListIndex()))
Paul Robinson080b1f32015-01-13 18:34:56 +00003978 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00003979}
3980
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003981static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &AL) {
3982 if (checkAttrMutualExclusion<CUDASharedAttr>(S, D, AL.getRange(),
3983 AL.getName()))
Justin Lebare71b2fa2016-09-30 23:57:34 +00003984 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003985 const auto *VD = cast<VarDecl>(D);
Justin Lebare71b2fa2016-09-30 23:57:34 +00003986 if (!VD->hasGlobalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003987 S.Diag(AL.getLoc(), diag::err_cuda_nonglobal_constant);
Justin Lebare71b2fa2016-09-30 23:57:34 +00003988 return;
3989 }
3990 D->addAttr(::new (S.Context) CUDAConstantAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003991 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Justin Lebare71b2fa2016-09-30 23:57:34 +00003992}
3993
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003994static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &AL) {
3995 if (checkAttrMutualExclusion<CUDAConstantAttr>(S, D, AL.getRange(),
3996 AL.getName()))
Justin Lebar10411012016-09-30 23:57:30 +00003997 return;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00003998 const auto *VD = cast<VarDecl>(D);
Justin Lebar281ce2a2016-10-02 15:24:50 +00003999 // extern __shared__ is only allowed on arrays with no length (e.g.
4000 // "int x[]").
Jonas Hahnfeldee47d8c2018-02-14 16:04:03 +00004001 if (!S.getLangOpts().CUDARelocatableDeviceCode && VD->hasExternalStorage() &&
4002 !isa<IncompleteArrayType>(VD->getType())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004003 S.Diag(AL.getLoc(), diag::err_cuda_extern_shared) << VD;
Justin Lebar10411012016-09-30 23:57:30 +00004004 return;
4005 }
Justin Lebaraa370bd2016-10-13 18:45:13 +00004006 if (S.getLangOpts().CUDA && VD->hasLocalStorage() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004007 S.CUDADiagIfHostCode(AL.getLoc(), diag::err_cuda_host_shared)
Justin Lebaraa370bd2016-10-13 18:45:13 +00004008 << S.CurrentCUDATarget())
4009 return;
Justin Lebar10411012016-09-30 23:57:30 +00004010 D->addAttr(::new (S.Context) CUDASharedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004011 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Justin Lebar10411012016-09-30 23:57:30 +00004012}
4013
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004014static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &AL) {
4015 if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, AL.getRange(),
4016 AL.getName()) ||
4017 checkAttrMutualExclusion<CUDAHostAttr>(S, D, AL.getRange(),
4018 AL.getName())) {
Justin Lebar3eaaf862016-01-13 01:07:35 +00004019 return;
4020 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004021 const auto *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00004022 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00004023 SourceRange RTRange = FD->getReturnTypeSourceRange();
4024 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004025 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00004026 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
4027 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00004028 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004029 }
Justin Lebarc66a1062016-01-20 00:26:57 +00004030 if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) {
4031 if (Method->isInstance()) {
4032 S.Diag(Method->getLocStart(), diag::err_kern_is_nonstatic_method)
4033 << Method;
4034 return;
4035 }
4036 S.Diag(Method->getLocStart(), diag::warn_kern_is_method) << Method;
4037 }
4038 // Only warn for "inline" when compiling for host, to cut down on noise.
4039 if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice)
4040 S.Diag(FD->getLocStart(), diag::warn_kern_is_inline) << FD;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004041
Aaron Ballman3aff6332013-12-02 19:30:36 +00004042 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004043 CUDAGlobalAttr(AL.getRange(), S.Context,
4044 AL.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00004045}
4046
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004047static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &AL) {
4048 const auto *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00004049 if (!Fn->isInlineSpecified()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004050 S.Diag(AL.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00004051 return;
4052 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004053
Michael Han99315932013-01-24 16:46:58 +00004054 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004055 GNUInlineAttr(AL.getRange(), S.Context,
4056 AL.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00004057}
4058
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004059static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004060 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004061
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004062 // Diagnostic is emitted elsewhere: here we store the (valid) AL
John McCall3882ace2011-01-05 12:14:39 +00004063 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
4064 CallingConv CC;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004065 if (S.CheckCallingConvAttr(AL, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00004066 return;
4067
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004068 if (!isa<ObjCMethodDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004069 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
4070 << AL.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00004071 return;
4072 }
4073
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004074 switch (AL.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004075 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00004076 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004077 FastCallAttr(AL.getRange(), S.Context,
4078 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004079 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004080 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00004081 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004082 StdCallAttr(AL.getRange(), S.Context,
4083 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004084 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004085 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00004086 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004087 ThisCallAttr(AL.getRange(), S.Context,
4088 AL.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00004089 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004090 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00004091 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004092 CDeclAttr(AL.getRange(), S.Context,
4093 AL.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00004094 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004095 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00004096 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004097 PascalAttr(AL.getRange(), S.Context,
4098 AL.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00004099 return;
John McCall477f2bb2016-03-03 06:39:32 +00004100 case AttributeList::AT_SwiftCall:
4101 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004102 SwiftCallAttr(AL.getRange(), S.Context,
4103 AL.getAttributeSpellingListIndex()));
John McCall477f2bb2016-03-03 06:39:32 +00004104 return;
Reid Klecknerd7857f02014-10-24 17:42:17 +00004105 case AttributeList::AT_VectorCall:
4106 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004107 VectorCallAttr(AL.getRange(), S.Context,
4108 AL.getAttributeSpellingListIndex()));
Reid Klecknerd7857f02014-10-24 17:42:17 +00004109 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00004110 case AttributeList::AT_MSABI:
4111 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004112 MSABIAttr(AL.getRange(), S.Context,
4113 AL.getAttributeSpellingListIndex()));
Charles Davisb5a214e2013-08-30 04:39:01 +00004114 return;
4115 case AttributeList::AT_SysVABI:
4116 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004117 SysVABIAttr(AL.getRange(), S.Context,
4118 AL.getAttributeSpellingListIndex()));
Charles Davisb5a214e2013-08-30 04:39:01 +00004119 return;
Erich Keane757d3172016-11-02 18:29:35 +00004120 case AttributeList::AT_RegCall:
4121 D->addAttr(::new (S.Context) RegCallAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004122 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Erich Keane757d3172016-11-02 18:29:35 +00004123 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004124 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004125 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004126 switch (CC) {
4127 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004128 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004129 break;
4130 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004131 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00004132 break;
4133 default:
4134 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004135 }
4136
Michael Han99315932013-01-24 16:46:58 +00004137 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004138 PcsAttr(AL.getRange(), S.Context, PCS,
4139 AL.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00004140 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004141 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00004142 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00004143 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004144 IntelOclBiccAttr(AL.getRange(), S.Context,
4145 AL.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00004146 return;
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004147 case AttributeList::AT_PreserveMost:
4148 D->addAttr(::new (S.Context) PreserveMostAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004149 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004150 return;
4151 case AttributeList::AT_PreserveAll:
4152 D->addAttr(::new (S.Context) PreserveAllAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004153 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004154 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004155 default:
4156 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00004157 }
4158}
4159
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004160static void handleSuppressAttr(Sema &S, Decl *D, const AttributeList &AL) {
4161 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Matthias Gehre01a63382017-03-27 19:45:24 +00004162 return;
4163
4164 std::vector<StringRef> DiagnosticIdentifiers;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004165 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Matthias Gehre01a63382017-03-27 19:45:24 +00004166 StringRef RuleName;
4167
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004168 if (!S.checkStringLiteralArgumentAttr(AL, I, RuleName, nullptr))
Matthias Gehre01a63382017-03-27 19:45:24 +00004169 return;
4170
4171 // FIXME: Warn if the rule name is unknown. This is tricky because only
4172 // clang-tidy knows about available rules.
4173 DiagnosticIdentifiers.push_back(RuleName);
4174 }
4175 D->addAttr(::new (S.Context) SuppressAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004176 AL.getRange(), S.Context, DiagnosticIdentifiers.data(),
4177 DiagnosticIdentifiers.size(), AL.getAttributeSpellingListIndex()));
Matthias Gehre01a63382017-03-27 19:45:24 +00004178}
4179
Erich Keaneb11ebc52017-09-27 03:20:13 +00004180bool Sema::CheckCallingConvAttr(const AttributeList &Attrs, CallingConv &CC,
Aaron Ballman02df2e02012-12-09 17:45:41 +00004181 const FunctionDecl *FD) {
Erich Keaneb11ebc52017-09-27 03:20:13 +00004182 if (Attrs.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004183 return true;
4184
Erich Keaneb11ebc52017-09-27 03:20:13 +00004185 if (Attrs.hasProcessingCache()) {
4186 CC = (CallingConv) Attrs.getProcessingCache();
John McCall3b5a8f52016-03-03 00:10:03 +00004187 return false;
4188 }
4189
Erich Keaneb11ebc52017-09-27 03:20:13 +00004190 unsigned ReqArgs = Attrs.getKind() == AttributeList::AT_Pcs ? 1 : 0;
4191 if (!checkAttributeNumArgs(*this, Attrs, ReqArgs)) {
4192 Attrs.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004193 return true;
4194 }
4195
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004196 // TODO: diagnose uses of these conventions on the wrong target.
Erich Keaneb11ebc52017-09-27 03:20:13 +00004197 switch (Attrs.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004198 case AttributeList::AT_CDecl: CC = CC_C; break;
4199 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
4200 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
4201 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
4202 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
John McCall477f2bb2016-03-03 06:39:32 +00004203 case AttributeList::AT_SwiftCall: CC = CC_Swift; break;
Reid Klecknerd7857f02014-10-24 17:42:17 +00004204 case AttributeList::AT_VectorCall: CC = CC_X86VectorCall; break;
Erich Keane757d3172016-11-02 18:29:35 +00004205 case AttributeList::AT_RegCall: CC = CC_X86RegCall; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00004206 case AttributeList::AT_MSABI:
4207 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
Martin Storsjo022e7822017-07-17 20:49:45 +00004208 CC_Win64;
Charles Davisb5a214e2013-08-30 04:39:01 +00004209 break;
4210 case AttributeList::AT_SysVABI:
4211 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
4212 CC_C;
4213 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004214 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00004215 StringRef StrRef;
Erich Keaneb11ebc52017-09-27 03:20:13 +00004216 if (!checkStringLiteralArgumentAttr(Attrs, 0, StrRef)) {
4217 Attrs.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004218 return true;
4219 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004220 if (StrRef == "aapcs") {
4221 CC = CC_AAPCS;
4222 break;
4223 } else if (StrRef == "aapcs-vfp") {
4224 CC = CC_AAPCS_VFP;
4225 break;
4226 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004227
Erich Keaneb11ebc52017-09-27 03:20:13 +00004228 Attrs.setInvalid();
4229 Diag(Attrs.getLoc(), diag::err_invalid_pcs);
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00004230 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00004231 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00004232 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00004233 case AttributeList::AT_PreserveMost: CC = CC_PreserveMost; break;
4234 case AttributeList::AT_PreserveAll: CC = CC_PreserveAll; break;
David Blaikie8a40f702012-01-17 06:56:22 +00004235 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00004236 }
4237
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004238 const TargetInfo &TI = Context.getTargetInfo();
4239 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004240 if (A != TargetInfo::CCCR_OK) {
4241 if (A == TargetInfo::CCCR_Warning)
Erich Keaneb11ebc52017-09-27 03:20:13 +00004242 Diag(Attrs.getLoc(), diag::warn_cconv_ignored) << Attrs.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00004243
Reid Kleckner9fde2e02015-02-26 19:43:46 +00004244 // This convention is not valid for the target. Use the default function or
4245 // method calling convention.
Alexey Bataeva7547182016-05-18 09:06:38 +00004246 bool IsCXXMethod = false, IsVariadic = false;
4247 if (FD) {
4248 IsCXXMethod = FD->isCXXInstanceMember();
4249 IsVariadic = FD->isVariadic();
4250 }
4251 CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00004252 }
4253
Erich Keaneb11ebc52017-09-27 03:20:13 +00004254 Attrs.setProcessingCache((unsigned) CC);
John McCall3882ace2011-01-05 12:14:39 +00004255 return false;
4256}
4257
John McCall477f2bb2016-03-03 06:39:32 +00004258/// Pointer-like types in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004259static bool isValidSwiftContextType(QualType Ty) {
4260 if (!Ty->hasPointerRepresentation())
4261 return Ty->isDependentType();
4262 return Ty->getPointeeType().getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004263}
4264
4265/// Pointers and references in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004266static bool isValidSwiftIndirectResultType(QualType Ty) {
4267 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4268 Ty = PtrType->getPointeeType();
4269 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4270 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004271 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004272 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004273 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004274 return Ty.getAddressSpace() == LangAS::Default;
John McCall477f2bb2016-03-03 06:39:32 +00004275}
4276
4277/// Pointers and references to pointers in the default address space.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004278static bool isValidSwiftErrorResultType(QualType Ty) {
4279 if (const auto *PtrType = Ty->getAs<PointerType>()) {
4280 Ty = PtrType->getPointeeType();
4281 } else if (const auto *RefType = Ty->getAs<ReferenceType>()) {
4282 Ty = RefType->getPointeeType();
John McCall477f2bb2016-03-03 06:39:32 +00004283 } else {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004284 return Ty->isDependentType();
John McCall477f2bb2016-03-03 06:39:32 +00004285 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004286 if (!Ty.getQualifiers().empty())
John McCall477f2bb2016-03-03 06:39:32 +00004287 return false;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004288 return isValidSwiftContextType(Ty);
John McCall477f2bb2016-03-03 06:39:32 +00004289}
4290
Erich Keaneb11ebc52017-09-27 03:20:13 +00004291static void handleParameterABIAttr(Sema &S, Decl *D, const AttributeList &Attrs,
4292 ParameterABI Abi) {
4293 S.AddParameterABIAttr(Attrs.getRange(), D, Abi,
4294 Attrs.getAttributeSpellingListIndex());
John McCall477f2bb2016-03-03 06:39:32 +00004295}
4296
4297void Sema::AddParameterABIAttr(SourceRange range, Decl *D, ParameterABI abi,
4298 unsigned spellingIndex) {
4299
4300 QualType type = cast<ParmVarDecl>(D)->getType();
4301
4302 if (auto existingAttr = D->getAttr<ParameterABIAttr>()) {
4303 if (existingAttr->getABI() != abi) {
4304 Diag(range.getBegin(), diag::err_attributes_are_not_compatible)
4305 << getParameterABISpelling(abi) << existingAttr;
4306 Diag(existingAttr->getLocation(), diag::note_conflicting_attribute);
4307 return;
4308 }
4309 }
4310
4311 switch (abi) {
4312 case ParameterABI::Ordinary:
4313 llvm_unreachable("explicit attribute for ordinary parameter ABI?");
4314
4315 case ParameterABI::SwiftContext:
4316 if (!isValidSwiftContextType(type)) {
4317 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4318 << getParameterABISpelling(abi)
4319 << /*pointer to pointer */ 0 << type;
4320 }
4321 D->addAttr(::new (Context)
4322 SwiftContextAttr(range, Context, spellingIndex));
4323 return;
4324
4325 case ParameterABI::SwiftErrorResult:
4326 if (!isValidSwiftErrorResultType(type)) {
4327 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4328 << getParameterABISpelling(abi)
4329 << /*pointer to pointer */ 1 << type;
4330 }
4331 D->addAttr(::new (Context)
4332 SwiftErrorResultAttr(range, Context, spellingIndex));
4333 return;
4334
4335 case ParameterABI::SwiftIndirectResult:
4336 if (!isValidSwiftIndirectResultType(type)) {
4337 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
4338 << getParameterABISpelling(abi)
4339 << /*pointer*/ 0 << type;
4340 }
4341 D->addAttr(::new (Context)
4342 SwiftIndirectResultAttr(range, Context, spellingIndex));
4343 return;
4344 }
4345 llvm_unreachable("bad parameter ABI attribute");
4346}
4347
John McCall3882ace2011-01-05 12:14:39 +00004348/// Checks a regparm attribute, returning true if it is ill-formed and
4349/// otherwise setting numParams to the appropriate value.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004350bool Sema::CheckRegparmAttr(const AttributeList &AL, unsigned &numParams) {
4351 if (AL.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004352 return true;
4353
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004354 if (!checkAttributeNumArgs(*this, AL, 1)) {
4355 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004356 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004357 }
Eli Friedman7044b762009-03-27 21:06:47 +00004358
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004359 uint32_t NP;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004360 Expr *NumParamsExpr = AL.getArgAsExpr(0);
4361 if (!checkUInt32Argument(*this, AL, NumParamsExpr, NP)) {
4362 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004363 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004364 }
4365
Douglas Gregore8bbc122011-09-02 00:18:52 +00004366 if (Context.getTargetInfo().getRegParmMax() == 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004367 Diag(AL.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00004368 << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004369 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004370 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004371 }
4372
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004373 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00004374 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004375 Diag(AL.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00004376 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004377 AL.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004378 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004379 }
4380
John McCall3882ace2011-01-05 12:14:39 +00004381 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004382}
4383
Artem Belevichbcec9da2016-06-06 22:54:57 +00004384// Checks whether an argument of launch_bounds attribute is
4385// acceptable, performs implicit conversion to Rvalue, and returns
4386// non-nullptr Expr result on success. Otherwise, it returns nullptr
4387// and may output an error.
4388static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004389 const CUDALaunchBoundsAttr &AL,
Artem Belevichbcec9da2016-06-06 22:54:57 +00004390 const unsigned Idx) {
Artem Belevich7093e402015-04-21 22:55:54 +00004391 if (S.DiagnoseUnexpandedParameterPack(E))
Artem Belevichbcec9da2016-06-06 22:54:57 +00004392 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004393
4394 // Accept template arguments for now as they depend on something else.
4395 // We'll get to check them when they eventually get instantiated.
4396 if (E->isValueDependent())
Artem Belevichbcec9da2016-06-06 22:54:57 +00004397 return E;
Artem Belevich7093e402015-04-21 22:55:54 +00004398
4399 llvm::APSInt I(64);
4400 if (!E->isIntegerConstantExpr(I, S.Context)) {
4401 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004402 << &AL << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
Artem Belevichbcec9da2016-06-06 22:54:57 +00004403 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004404 }
4405 // Make sure we can fit it in 32 bits.
4406 if (!I.isIntN(32)) {
4407 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
4408 << 32 << /* Unsigned */ 1;
Artem Belevichbcec9da2016-06-06 22:54:57 +00004409 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004410 }
4411 if (I < 0)
4412 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004413 << &AL << Idx << E->getSourceRange();
Artem Belevich7093e402015-04-21 22:55:54 +00004414
Artem Belevichbcec9da2016-06-06 22:54:57 +00004415 // We may need to perform implicit conversion of the argument.
4416 InitializedEntity Entity = InitializedEntity::InitializeParameter(
4417 S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false);
4418 ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E);
4419 assert(!ValArg.isInvalid() &&
4420 "Unexpected PerformCopyInitialization() failure.");
4421
4422 return ValArg.getAs<Expr>();
Artem Belevich7093e402015-04-21 22:55:54 +00004423}
4424
4425void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads,
4426 Expr *MinBlocks, unsigned SpellingListIndex) {
4427 CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks,
4428 SpellingListIndex);
Artem Belevichbcec9da2016-06-06 22:54:57 +00004429 MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0);
4430 if (MaxThreads == nullptr)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004431 return;
4432
Artem Belevichbcec9da2016-06-06 22:54:57 +00004433 if (MinBlocks) {
4434 MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1);
4435 if (MinBlocks == nullptr)
4436 return;
4437 }
Artem Belevich7093e402015-04-21 22:55:54 +00004438
4439 D->addAttr(::new (Context) CUDALaunchBoundsAttr(
4440 AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex));
4441}
4442
4443static void handleLaunchBoundsAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004444 const AttributeList &AL) {
4445 if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
4446 !checkAttributeAtMostNumArgs(S, AL, 2))
Artem Belevich7093e402015-04-21 22:55:54 +00004447 return;
4448
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004449 S.AddLaunchBoundsAttr(AL.getRange(), D, AL.getArgAsExpr(0),
4450 AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr,
4451 AL.getAttributeSpellingListIndex());
Peter Collingbourne827301e2010-12-12 23:03:07 +00004452}
4453
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004454static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004455 const AttributeList &AL) {
4456 if (!AL.isArgIdent(0)) {
4457 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
4458 << AL.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004459 return;
4460 }
Joel E. Denny81508102018-03-13 14:51:22 +00004461
4462 ParamIdx ArgumentIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004463 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 2, AL.getArgAsExpr(1),
Alp Toker601b22c2014-01-21 23:35:24 +00004464 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004465 return;
4466
Joel E. Denny81508102018-03-13 14:51:22 +00004467 ParamIdx TypeTagIdx;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004468 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 3, AL.getArgAsExpr(2),
Alp Toker601b22c2014-01-21 23:35:24 +00004469 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004470 return;
4471
Aaron Ballmana26d8ee2018-02-25 14:01:04 +00004472 bool IsPointer = AL.getName()->getName() == "pointer_with_type_tag";
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004473 if (IsPointer) {
4474 // Ensure that buffer has a pointer type.
Joel E. Denny81508102018-03-13 14:51:22 +00004475 unsigned ArgumentIdxAST = ArgumentIdx.getASTIndex();
4476 if (ArgumentIdxAST >= getFunctionOrMethodNumParams(D) ||
4477 !getFunctionOrMethodParamType(D, ArgumentIdxAST)->isPointerType())
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004478 S.Diag(AL.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballmanbd0f6562018-02-25 20:28:10 +00004479 << AL.getName() << 0;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004480 }
4481
Aaron Ballmana26d8ee2018-02-25 14:01:04 +00004482 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(
4483 AL.getRange(), S.Context, AL.getArgAsIdent(0)->Ident, ArgumentIdx,
4484 TypeTagIdx, IsPointer, AL.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004485}
4486
4487static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004488 const AttributeList &AL) {
4489 if (!AL.isArgIdent(0)) {
4490 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
4491 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004492 return;
4493 }
Aaron Ballman00e99962013-08-31 01:11:41 +00004494
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004495 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballman00e99962013-08-31 01:11:41 +00004496 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004497
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004498 if (!isa<VarDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004499 S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type)
4500 << AL.getName() << ExpectedVariable;
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004501 return;
4502 }
4503
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004504 IdentifierInfo *PointerKind = AL.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00004505 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004506 S.GetTypeFromParser(AL.getMatchingCType(), &MatchingCTypeLoc);
Richard Smithb87c4652013-10-31 21:23:20 +00004507 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004508
Michael Han99315932013-01-24 16:46:58 +00004509 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004510 TypeTagForDatatypeAttr(AL.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00004511 MatchingCTypeLoc,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004512 AL.getLayoutCompatible(),
4513 AL.getMustBeNull(),
4514 AL.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004515}
4516
Joel E. Denny81508102018-03-13 14:51:22 +00004517static void handleXRayLogArgsAttr(Sema &S, Decl *D, const AttributeList &AL) {
4518 ParamIdx ArgCount;
Dean Michael Berris7456a282017-06-16 03:22:09 +00004519
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004520 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, AL.getArgAsExpr(0),
Dean Michael Berris7456a282017-06-16 03:22:09 +00004521 ArgCount,
Joel E. Denny81508102018-03-13 14:51:22 +00004522 true /* CanIndexImplicitThis */))
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004523 return;
4524
Joel E. Denny81508102018-03-13 14:51:22 +00004525 // ArgCount isn't a parameter index [0;n), it's a count [1;n]
4526 D->addAttr(::new (S.Context) XRayLogArgsAttr(
4527 AL.getRange(), S.Context, ArgCount.getSourceIndex(),
4528 AL.getAttributeSpellingListIndex()));
Dean Michael Berris418da3f2017-03-06 07:08:21 +00004529}
4530
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004531//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004532// Checker-specific attribute handlers.
4533//===----------------------------------------------------------------------===//
4534
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004535static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType QT) {
4536 return QT->isDependentType() || QT->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004537}
4538
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004539static bool isValidSubjectOfNSAttribute(Sema &S, QualType QT) {
4540 return QT->isDependentType() || QT->isObjCObjectPointerType() ||
4541 S.Context.isObjCNSObjectType(QT);
John McCalled433932011-01-25 03:31:58 +00004542}
Eugene Zelenko1ced5092016-02-12 22:53:10 +00004543
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004544static bool isValidSubjectOfCFAttribute(Sema &S, QualType QT) {
4545 return QT->isDependentType() || QT->isPointerType() ||
4546 isValidSubjectOfNSAttribute(S, QT);
John McCalled433932011-01-25 03:31:58 +00004547}
4548
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004549static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &AL) {
4550 S.AddNSConsumedAttr(AL.getRange(), D, AL.getAttributeSpellingListIndex(),
4551 AL.getKind() == AttributeList::AT_NSConsumed,
John McCall3b5a8f52016-03-03 00:10:03 +00004552 /*template instantiation*/ false);
4553}
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004554
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004555void Sema::AddNSConsumedAttr(SourceRange AttrRange, Decl *D,
4556 unsigned SpellingIndex, bool IsNSConsumed,
4557 bool IsTemplateInstantiation) {
4558 const auto *Param = cast<ParmVarDecl>(D);
4559 bool TypeOK;
John McCall3b5a8f52016-03-03 00:10:03 +00004560
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004561 if (IsNSConsumed)
4562 TypeOK = isValidSubjectOfNSAttribute(*this, Param->getType());
4563 else
4564 TypeOK = isValidSubjectOfCFAttribute(*this, Param->getType());
John McCalled433932011-01-25 03:31:58 +00004565
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004566 if (!TypeOK) {
John McCall3b5a8f52016-03-03 00:10:03 +00004567 // These attributes are normally just advisory, but in ARC, ns_consumed
4568 // is significant. Allow non-dependent code to contain inappropriate
4569 // attributes even in ARC, but require template instantiations to be
4570 // set up correctly.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004571 Diag(D->getLocStart(), (IsTemplateInstantiation && IsNSConsumed &&
4572 getLangOpts().ObjCAutoRefCount
4573 ? diag::err_ns_attribute_wrong_parameter_type
4574 : diag::warn_ns_attribute_wrong_parameter_type))
4575 << AttrRange << (IsNSConsumed ? "ns_consumed" : "cf_consumed")
4576 << (IsNSConsumed ? /*objc pointers*/ 0 : /*cf pointers*/ 1);
John McCalled433932011-01-25 03:31:58 +00004577 return;
4578 }
4579
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004580 if (IsNSConsumed)
4581 D->addAttr(::new (Context)
4582 NSConsumedAttr(AttrRange, Context, SpellingIndex));
John McCalled433932011-01-25 03:31:58 +00004583 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004584 D->addAttr(::new (Context)
4585 CFConsumedAttr(AttrRange, Context, SpellingIndex));
John McCalled433932011-01-25 03:31:58 +00004586}
4587
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004588bool Sema::checkNSReturnsRetainedReturnType(SourceLocation Loc, QualType QT) {
4589 if (isValidSubjectOfNSReturnsRetainedAttribute(QT))
John McCall12251882017-07-15 11:06:46 +00004590 return false;
4591
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004592 Diag(Loc, diag::warn_ns_attribute_wrong_return_type)
4593 << "'ns_returns_retained'" << 0 << 0;
John McCall12251882017-07-15 11:06:46 +00004594 return true;
4595}
4596
Chandler Carruthedc2c642011-07-02 00:01:44 +00004597static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004598 const AttributeList &AL) {
4599 QualType ReturnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004600
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004601 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
4602 ReturnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004603 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004604 (AL.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004605 return; // ignore: was handled as a type attribute
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004606 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D))
4607 ReturnType = PD->getType();
4608 else if (const auto *FD = dyn_cast<FunctionDecl>(D))
4609 ReturnType = FD->getReturnType();
4610 else if (const auto *Param = dyn_cast<ParmVarDecl>(D)) {
4611 ReturnType = Param->getType()->getPointeeType();
4612 if (ReturnType.isNull()) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004613 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004614 << AL.getName() << /*pointer-to-CF*/2
4615 << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004616 return;
4617 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004618 } else if (AL.isUsedAsTypeAttr()) {
John McCall12251882017-07-15 11:06:46 +00004619 return;
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004620 } else {
4621 AttributeDeclKind ExpectedDeclKind;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004622 switch (AL.getKind()) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004623 default: llvm_unreachable("invalid ownership attribute");
4624 case AttributeList::AT_NSReturnsRetained:
4625 case AttributeList::AT_NSReturnsAutoreleased:
4626 case AttributeList::AT_NSReturnsNotRetained:
4627 ExpectedDeclKind = ExpectedFunctionOrMethod;
4628 break;
4629
4630 case AttributeList::AT_CFReturnsRetained:
4631 case AttributeList::AT_CFReturnsNotRetained:
4632 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
4633 break;
4634 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004635 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004636 << AL.getRange() << AL.getName() << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004637 return;
4638 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004639
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004640 bool TypeOK;
4641 bool Cf;
4642 switch (AL.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004643 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004644 case AttributeList::AT_NSReturnsRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004645 TypeOK = isValidSubjectOfNSReturnsRetainedAttribute(ReturnType);
4646 Cf = false;
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004647 break;
4648
4649 case AttributeList::AT_NSReturnsAutoreleased:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004650 case AttributeList::AT_NSReturnsNotRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004651 TypeOK = isValidSubjectOfNSAttribute(S, ReturnType);
4652 Cf = false;
John McCalled433932011-01-25 03:31:58 +00004653 break;
4654
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004655 case AttributeList::AT_CFReturnsRetained:
4656 case AttributeList::AT_CFReturnsNotRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004657 TypeOK = isValidSubjectOfCFAttribute(S, ReturnType);
4658 Cf = true;
John McCalled433932011-01-25 03:31:58 +00004659 break;
4660 }
4661
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004662 if (!TypeOK) {
4663 if (AL.isUsedAsTypeAttr())
John McCall12251882017-07-15 11:06:46 +00004664 return;
4665
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004666 if (isa<ParmVarDecl>(D)) {
4667 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004668 << AL.getName() << /*pointer-to-CF*/2
4669 << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004670 } else {
4671 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
4672 enum : unsigned {
4673 Function,
4674 Method,
4675 Property
4676 } SubjectKind = Function;
4677 if (isa<ObjCMethodDecl>(D))
4678 SubjectKind = Method;
4679 else if (isa<ObjCPropertyDecl>(D))
4680 SubjectKind = Property;
4681 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004682 << AL.getName() << SubjectKind << Cf
4683 << AL.getRange();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004684 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004685 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004686 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004687
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004688 switch (AL.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004689 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004690 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004691 case AttributeList::AT_NSReturnsAutoreleased:
Nico Weber462fd1e2015-01-07 23:50:05 +00004692 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004693 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004694 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004695 case AttributeList::AT_CFReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004696 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004697 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004698 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004699 case AttributeList::AT_NSReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004700 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004701 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004702 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004703 case AttributeList::AT_CFReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004704 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004705 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004706 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004707 case AttributeList::AT_NSReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004708 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004709 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004710 return;
4711 };
4712}
4713
John McCallcf166702011-07-22 08:53:00 +00004714static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
Erich Keaneb11ebc52017-09-27 03:20:13 +00004715 const AttributeList &Attrs) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00004716 const int EP_ObjCMethod = 1;
4717 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004718
Erich Keaneb11ebc52017-09-27 03:20:13 +00004719 SourceLocation loc = Attrs.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004720 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004721 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004722 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004723 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004724 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00004725
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004726 if (!resultType->isReferenceType() &&
4727 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004728 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00004729 << SourceRange(loc)
Erich Keaneb11ebc52017-09-27 03:20:13 +00004730 << Attrs.getName()
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004731 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004732 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00004733
4734 // Drop the attribute.
4735 return;
4736 }
4737
Nico Weber462fd1e2015-01-07 23:50:05 +00004738 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(
Erich Keaneb11ebc52017-09-27 03:20:13 +00004739 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004740}
4741
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004742static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
Erich Keaneb11ebc52017-09-27 03:20:13 +00004743 const AttributeList &Attrs) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004744 const auto *Method = cast<ObjCMethodDecl>(D);
4745
4746 const DeclContext *DC = Method->getDeclContext();
4747 if (const auto *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004748 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004749 << Attrs.getName() << 0;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004750 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4751 return;
4752 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004753 if (Method->getMethodFamily() == OMF_dealloc) {
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004754 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004755 << Attrs.getName() << 1;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004756 return;
4757 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004758
4759 D->addAttr(::new (S.Context) ObjCRequiresSuperAttr(
4760 Attrs.getRange(), S.Context, Attrs.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004761}
4762
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004763static void handleObjCBridgeAttr(Sema &S, Decl *D, const AttributeList &AL) {
4764 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004765
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004766 if (!Parm) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004767 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << AL.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004768 return;
4769 }
John McCall28592582015-02-01 22:34:06 +00004770
4771 // Typedefs only allow objc_bridge(id) and have some additional checking.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004772 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall28592582015-02-01 22:34:06 +00004773 if (!Parm->Ident->isStr("id")) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004774 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_id)
4775 << AL.getName();
John McCall28592582015-02-01 22:34:06 +00004776 return;
4777 }
4778
4779 // Only allow 'cv void *'.
4780 QualType T = TD->getUnderlyingType();
4781 if (!T->isVoidPointerType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004782 S.Diag(AL.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
John McCall28592582015-02-01 22:34:06 +00004783 return;
4784 }
4785 }
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004786
4787 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004788 ObjCBridgeAttr(AL.getRange(), S.Context, Parm->Ident,
4789 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004790}
4791
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004792static void handleObjCBridgeMutableAttr(Sema &S, Decl *D,
4793 const AttributeList &AL) {
4794 IdentifierLoc *Parm = AL.isArgIdent(0) ? AL.getArgAsIdent(0) : nullptr;
Craig Topperc3ec1492014-05-26 06:22:03 +00004795
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004796 if (!Parm) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004797 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << AL.getName() << 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004798 return;
4799 }
4800
4801 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004802 ObjCBridgeMutableAttr(AL.getRange(), S.Context, Parm->Ident,
4803 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004804}
4805
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004806static void handleObjCBridgeRelatedAttr(Sema &S, Decl *D,
4807 const AttributeList &AL) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004808 IdentifierInfo *RelatedClass =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004809 AL.isArgIdent(0) ? AL.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004810 if (!RelatedClass) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004811 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << AL.getName() << 0;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004812 return;
4813 }
4814 IdentifierInfo *ClassMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004815 AL.getArgAsIdent(1) ? AL.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004816 IdentifierInfo *InstanceMethod =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004817 AL.getArgAsIdent(2) ? AL.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004818 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004819 ObjCBridgeRelatedAttr(AL.getRange(), S.Context, RelatedClass,
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004820 ClassMethod, InstanceMethod,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004821 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004822}
4823
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004824static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004825 const AttributeList &AL) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004826 ObjCInterfaceDecl *IFace;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004827 if (auto *CatDecl = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004828 IFace = CatDecl->getClassInterface();
4829 else
4830 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00004831
4832 if (!IFace)
4833 return;
4834
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00004835 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00004836 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004837 ObjCDesignatedInitializerAttr(AL.getRange(), S.Context,
4838 AL.getAttributeSpellingListIndex()));
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004839}
4840
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004841static void handleObjCRuntimeName(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004842 const AttributeList &AL) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004843 StringRef MetaDataName;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004844 if (!S.checkStringLiteralArgumentAttr(AL, 0, MetaDataName))
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004845 return;
4846 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004847 ObjCRuntimeNameAttr(AL.getRange(), S.Context,
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004848 MetaDataName,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004849 AL.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004850}
4851
Nico Webera6916892016-06-10 18:53:04 +00004852// When a user wants to use objc_boxable with a union or struct
4853// but they don't have access to the declaration (legacy/third-party code)
4854// then they can 'enable' this feature with a typedef:
Alex Denisovfde64952015-06-26 05:28:36 +00004855// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004856static void handleObjCBoxable(Sema &S, Decl *D, const AttributeList &AL) {
Alex Denisovfde64952015-06-26 05:28:36 +00004857 bool notify = false;
4858
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004859 auto *RD = dyn_cast<RecordDecl>(D);
Alex Denisovfde64952015-06-26 05:28:36 +00004860 if (RD && RD->getDefinition()) {
4861 RD = RD->getDefinition();
4862 notify = true;
4863 }
4864
4865 if (RD) {
4866 ObjCBoxableAttr *BoxableAttr = ::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004867 ObjCBoxableAttr(AL.getRange(), S.Context,
4868 AL.getAttributeSpellingListIndex());
Alex Denisovfde64952015-06-26 05:28:36 +00004869 RD->addAttr(BoxableAttr);
4870 if (notify) {
4871 // we need to notify ASTReader/ASTWriter about
4872 // modification of existing declaration
4873 if (ASTMutationListener *L = S.getASTMutationListener())
4874 L->AddedAttributeToRecord(BoxableAttr, RD);
4875 }
4876 }
4877}
4878
Chandler Carruthedc2c642011-07-02 00:01:44 +00004879static void handleObjCOwnershipAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004880 const AttributeList &AL) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004881 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004882
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004883 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004884 << AL.getRange() << AL.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004885}
4886
Chandler Carruthedc2c642011-07-02 00:01:44 +00004887static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004888 const AttributeList &AL) {
4889 const auto *VD = cast<ValueDecl>(D);
4890 QualType QT = VD->getType();
John McCall31168b02011-06-15 23:02:42 +00004891
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004892 if (!QT->isDependentType() &&
4893 !QT->isObjCLifetimeType()) {
4894 S.Diag(AL.getLoc(), diag::err_objc_precise_lifetime_bad_type)
4895 << QT;
John McCall31168b02011-06-15 23:02:42 +00004896 return;
4897 }
4898
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004899 Qualifiers::ObjCLifetime Lifetime = QT.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +00004900
4901 // If we have no lifetime yet, check the lifetime we're presumably
4902 // going to infer.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004903 if (Lifetime == Qualifiers::OCL_None && !QT->isDependentType())
4904 Lifetime = QT->getObjCARCImplicitLifetime();
John McCall31168b02011-06-15 23:02:42 +00004905
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004906 switch (Lifetime) {
John McCall31168b02011-06-15 23:02:42 +00004907 case Qualifiers::OCL_None:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004908 assert(QT->isDependentType() &&
John McCall31168b02011-06-15 23:02:42 +00004909 "didn't infer lifetime for non-dependent type?");
4910 break;
4911
4912 case Qualifiers::OCL_Weak: // meaningful
4913 case Qualifiers::OCL_Strong: // meaningful
4914 break;
4915
4916 case Qualifiers::OCL_ExplicitNone:
4917 case Qualifiers::OCL_Autoreleasing:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004918 S.Diag(AL.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
4919 << (Lifetime == Qualifiers::OCL_Autoreleasing);
John McCall31168b02011-06-15 23:02:42 +00004920 break;
4921 }
4922
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004923 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004924 ObjCPreciseLifetimeAttr(AL.getRange(), S.Context,
4925 AL.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004926}
4927
Francois Picheta83957a2010-12-19 06:50:37 +00004928//===----------------------------------------------------------------------===//
4929// Microsoft specific attribute handlers.
4930//===----------------------------------------------------------------------===//
4931
Nico Weber88f5ed92016-09-13 18:55:26 +00004932UuidAttr *Sema::mergeUuidAttr(Decl *D, SourceRange Range,
4933 unsigned AttrSpellingListIndex, StringRef Uuid) {
4934 if (const auto *UA = D->getAttr<UuidAttr>()) {
Nico Weberd58c2602016-09-14 01:16:54 +00004935 if (UA->getGuid().equals_lower(Uuid))
Nico Weber88f5ed92016-09-13 18:55:26 +00004936 return nullptr;
4937 Diag(UA->getLocation(), diag::err_mismatched_uuid);
4938 Diag(Range.getBegin(), diag::note_previous_uuid);
4939 D->dropAttr<UuidAttr>();
4940 }
4941
4942 return ::new (Context) UuidAttr(Range, Context, Uuid, AttrSpellingListIndex);
4943}
4944
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004945static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00004946 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004947 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
4948 << AL.getName() << AttributeLangSupport::C;
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00004949 return;
4950 }
4951
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004952 StringRef StrRef;
4953 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004954 if (!S.checkStringLiteralArgumentAttr(AL, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00004955 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004956
David Majnemer89085342013-08-09 08:56:20 +00004957 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4958 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00004959 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4960 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00004961
Reid Kleckner140c4a72013-05-17 14:04:52 +00004962 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00004963 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004964 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004965 return;
4966 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004967
David Majnemer89085342013-08-09 08:56:20 +00004968 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004969 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00004970 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004971 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00004972 return;
4973 }
David Majnemer89085342013-08-09 08:56:20 +00004974 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004975 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004976 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004977 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004978 }
Francois Picheta83957a2010-12-19 06:50:37 +00004979
Nico Weber469891e2017-05-05 17:05:56 +00004980 // FIXME: It'd be nice to also emit a fixit removing uuid(...) (and, if it's
4981 // the only thing in the [] list, the [] too), and add an insertion of
4982 // __declspec(uuid(...)). But sadly, neither the SourceLocs of the commas
4983 // separating attributes nor of the [ and the ] are in the AST.
Nico Weber0a234042017-05-05 17:15:08 +00004984 // Cf "SourceLocations of attribute list delimiters - [[ ... , ... ]] etc"
Nico Weber469891e2017-05-05 17:05:56 +00004985 // on cfe-dev.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004986 if (AL.isMicrosoftAttribute()) // Check for [uuid(...)] spelling.
4987 S.Diag(AL.getLoc(), diag::warn_atl_uuid_deprecated);
Nico Weber469891e2017-05-05 17:05:56 +00004988
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004989 UuidAttr *UA = S.mergeUuidAttr(D, AL.getRange(),
4990 AL.getAttributeSpellingListIndex(), StrRef);
Nico Weber88f5ed92016-09-13 18:55:26 +00004991 if (UA)
4992 D->addAttr(UA);
Charles Davis163855f2010-02-16 18:27:26 +00004993}
4994
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004995static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &AL) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00004996 if (!S.LangOpts.CPlusPlus) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00004997 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
4998 << AL.getName() << AttributeLangSupport::C;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004999 return;
5000 }
5001 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005002 D, AL.getRange(), /*BestCase=*/true,
5003 AL.getAttributeSpellingListIndex(),
5004 (MSInheritanceAttr::Spelling)AL.getSemanticSpelling());
David Majnemer929025d2016-01-26 19:30:26 +00005005 if (IA) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00005006 D->addAttr(IA);
David Majnemer929025d2016-01-26 19:30:26 +00005007 S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D));
5008 }
David Majnemer2c4e00a2014-01-29 22:07:36 +00005009}
5010
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005011static void handleDeclspecThreadAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005012 const AttributeList &AL) {
5013 const auto *VD = cast<VarDecl>(D);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005014 if (!S.Context.getTargetInfo().isTLSSupported()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005015 S.Diag(AL.getLoc(), diag::err_thread_unsupported);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005016 return;
5017 }
5018 if (VD->getTSCSpec() != TSCS_unspecified) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005019 S.Diag(AL.getLoc(), diag::err_declspec_thread_on_thread_variable);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005020 return;
5021 }
5022 if (VD->hasLocalStorage()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005023 S.Diag(AL.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005024 return;
5025 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005026 D->addAttr(::new (S.Context) ThreadAttr(AL.getRange(), S.Context,
5027 AL.getAttributeSpellingListIndex()));
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005028}
5029
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005030static void handleAbiTagAttr(Sema &S, Decl *D, const AttributeList &AL) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005031 SmallVector<StringRef, 4> Tags;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005032 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005033 StringRef Tag;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005034 if (!S.checkStringLiteralArgumentAttr(AL, I, Tag))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005035 return;
5036 Tags.push_back(Tag);
5037 }
5038
5039 if (const auto *NS = dyn_cast<NamespaceDecl>(D)) {
5040 if (!NS->isInline()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005041 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 0;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005042 return;
5043 }
5044 if (NS->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005045 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 1;
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005046 return;
5047 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005048 if (AL.getNumArgs() == 0)
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005049 Tags.push_back(NS->getName());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005050 } else if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005051 return;
5052
5053 // Store tags sorted and without duplicates.
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +00005054 llvm::sort(Tags.begin(), Tags.end());
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005055 Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end());
5056
5057 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005058 AbiTagAttr(AL.getRange(), S.Context, Tags.data(), Tags.size(),
5059 AL.getAttributeSpellingListIndex()));
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005060}
5061
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005062static void handleARMInterruptAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005063 const AttributeList &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005064 // Check the attribute arguments.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005065 if (AL.getNumArgs() > 1) {
5066 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
5067 << AL.getName() << 1;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005068 return;
5069 }
5070
5071 StringRef Str;
5072 SourceLocation ArgLoc;
5073
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005074 if (AL.getNumArgs() == 0)
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005075 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005076 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005077 return;
5078
5079 ARMInterruptAttr::InterruptType Kind;
5080 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005081 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
5082 << AL.getName() << Str << ArgLoc;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005083 return;
5084 }
5085
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005086 unsigned Index = AL.getAttributeSpellingListIndex();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005087 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005088 ARMInterruptAttr(AL.getLoc(), S.Context, Kind, Index));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005089}
5090
5091static void handleMSP430InterruptAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005092 const AttributeList &AL) {
5093 if (!checkAttributeNumArgs(S, AL, 1))
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005094 return;
5095
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005096 if (!AL.isArgExpr(0)) {
5097 S.Diag(AL.getLoc(), diag::err_attribute_argument_type) << AL.getName()
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005098 << AANT_ArgumentIntegerConstant;
5099 return;
5100 }
5101
5102 // FIXME: Check for decl - it should be void ()(void).
5103
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005104 Expr *NumParamsExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005105 llvm::APSInt NumParams(32);
5106 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005107 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
5108 << AL.getName() << AANT_ArgumentIntegerConstant
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005109 << NumParamsExpr->getSourceRange();
5110 return;
5111 }
5112
5113 unsigned Num = NumParams.getLimitedValue(255);
5114 if ((Num & 1) || Num > 30) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005115 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
5116 << AL.getName() << (int)NumParams.getSExtValue()
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005117 << NumParamsExpr->getSourceRange();
5118 return;
5119 }
5120
Aaron Ballman36a53502014-01-16 13:03:14 +00005121 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005122 MSP430InterruptAttr(AL.getLoc(), S.Context, Num,
5123 AL.getAttributeSpellingListIndex()));
Aaron Ballman36a53502014-01-16 13:03:14 +00005124 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005125}
5126
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005127static void handleMipsInterruptAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005128 const AttributeList &AL) {
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005129 // Only one optional argument permitted.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005130 if (AL.getNumArgs() > 1) {
5131 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
5132 << AL.getName() << 1;
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005133 return;
5134 }
5135
5136 StringRef Str;
5137 SourceLocation ArgLoc;
5138
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005139 if (AL.getNumArgs() == 0)
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005140 Str = "";
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005141 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005142 return;
5143
5144 // Semantic checks for a function with the 'interrupt' attribute for MIPS:
5145 // a) Must be a function.
5146 // b) Must have no parameters.
5147 // c) Must have the 'void' return type.
5148 // d) Cannot have the 'mips16' attribute, as that instruction set
5149 // lacks the 'eret' instruction.
5150 // e) The attribute itself must either have no argument or one of the
5151 // valid interrupt types, see [MipsInterruptDocs].
5152
5153 if (!isFunctionOrMethod(D)) {
5154 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5155 << "'interrupt'" << ExpectedFunctionOrMethod;
5156 return;
5157 }
5158
5159 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
5160 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
5161 << 0;
5162 return;
5163 }
5164
5165 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5166 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
5167 << 1;
5168 return;
5169 }
5170
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005171 if (checkAttrMutualExclusion<Mips16Attr>(S, D, AL.getRange(),
5172 AL.getName()))
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005173 return;
5174
5175 MipsInterruptAttr::InterruptType Kind;
5176 if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005177 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
5178 << AL.getName() << "'" + std::string(Str) + "'";
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005179 return;
5180 }
5181
5182 D->addAttr(::new (S.Context) MipsInterruptAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005183 AL.getLoc(), S.Context, Kind, AL.getAttributeSpellingListIndex()));
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00005184}
5185
Alexey Bataevd51e9932016-01-15 04:06:31 +00005186static void handleAnyX86InterruptAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005187 const AttributeList &AL) {
Alexey Bataevd51e9932016-01-15 04:06:31 +00005188 // Semantic checks for a function with the 'interrupt' attribute.
5189 // a) Must be a function.
5190 // b) Must have the 'void' return type.
5191 // c) Must take 1 or 2 arguments.
5192 // d) The 1st argument must be a pointer.
5193 // e) The 2nd argument (if any) must be an unsigned integer.
5194 if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) ||
5195 CXXMethodDecl::isStaticOverloadedOperator(
5196 cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005197 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
5198 << AL.getName() << ExpectedFunctionWithProtoType;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005199 return;
5200 }
5201 // Interrupt handler must have void return type.
5202 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
5203 S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(),
5204 diag::err_anyx86_interrupt_attribute)
5205 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5206 ? 0
5207 : 1)
5208 << 0;
5209 return;
5210 }
5211 // Interrupt handler must have 1 or 2 parameters.
5212 unsigned NumParams = getFunctionOrMethodNumParams(D);
5213 if (NumParams < 1 || NumParams > 2) {
5214 S.Diag(D->getLocStart(), diag::err_anyx86_interrupt_attribute)
5215 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5216 ? 0
5217 : 1)
5218 << 1;
5219 return;
5220 }
5221 // The first argument must be a pointer.
5222 if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) {
5223 S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(),
5224 diag::err_anyx86_interrupt_attribute)
5225 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5226 ? 0
5227 : 1)
5228 << 2;
5229 return;
5230 }
5231 // The second argument, if present, must be an unsigned integer.
5232 unsigned TypeSize =
5233 S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64
5234 ? 64
5235 : 32;
5236 if (NumParams == 2 &&
5237 (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() ||
5238 S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) {
5239 S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(),
5240 diag::err_anyx86_interrupt_attribute)
5241 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
5242 ? 0
5243 : 1)
5244 << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false);
5245 return;
5246 }
5247 D->addAttr(::new (S.Context) AnyX86InterruptAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005248 AL.getLoc(), S.Context, AL.getAttributeSpellingListIndex()));
Alexey Bataevd51e9932016-01-15 04:06:31 +00005249 D->addAttr(UsedAttr::CreateImplicit(S.Context));
5250}
5251
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005252static void handleAVRInterruptAttr(Sema &S, Decl *D, const AttributeList &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005253 if (!isFunctionOrMethod(D)) {
5254 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5255 << "'interrupt'" << ExpectedFunction;
5256 return;
5257 }
5258
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005259 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005260 return;
5261
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005262 handleSimpleAttribute<AVRInterruptAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005263}
5264
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005265static void handleAVRSignalAttr(Sema &S, Decl *D, const AttributeList &AL) {
Dylan McKaye8232d72017-02-08 05:09:26 +00005266 if (!isFunctionOrMethod(D)) {
5267 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5268 << "'signal'" << ExpectedFunction;
5269 return;
5270 }
5271
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005272 if (!checkAttributeNumArgs(S, AL, 0))
Dylan McKaye8232d72017-02-08 05:09:26 +00005273 return;
5274
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005275 handleSimpleAttribute<AVRSignalAttr>(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005276}
5277
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005278static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005279 // Dispatch the interrupt attribute based on the current target.
Alexey Bataevd51e9932016-01-15 04:06:31 +00005280 switch (S.Context.getTargetInfo().getTriple().getArch()) {
5281 case llvm::Triple::msp430:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005282 handleMSP430InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005283 break;
5284 case llvm::Triple::mipsel:
5285 case llvm::Triple::mips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005286 handleMipsInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005287 break;
5288 case llvm::Triple::x86:
5289 case llvm::Triple::x86_64:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005290 handleAnyX86InterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005291 break;
Dylan McKaye8232d72017-02-08 05:09:26 +00005292 case llvm::Triple::avr:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005293 handleAVRInterruptAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005294 break;
Alexey Bataevd51e9932016-01-15 04:06:31 +00005295 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005296 handleARMInterruptAttr(S, D, AL);
Alexey Bataevd51e9932016-01-15 04:06:31 +00005297 break;
5298 }
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005299}
5300
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005301static void handleAMDGPUFlatWorkGroupSizeAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005302 const AttributeList &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005303 uint32_t Min = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005304 Expr *MinExpr = AL.getArgAsExpr(0);
5305 if (!checkUInt32Argument(S, AL, MinExpr, Min))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005306 return;
5307
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005308 uint32_t Max = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005309 Expr *MaxExpr = AL.getArgAsExpr(1);
5310 if (!checkUInt32Argument(S, AL, MaxExpr, Max))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005311 return;
5312
5313 if (Min == 0 && Max != 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005314 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid)
5315 << AL.getName() << 0;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005316 return;
5317 }
5318 if (Min > Max) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005319 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid)
5320 << AL.getName() << 1;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005321 return;
5322 }
5323
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005324 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005325 AMDGPUFlatWorkGroupSizeAttr(AL.getLoc(), S.Context, Min, Max,
5326 AL.getAttributeSpellingListIndex()));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005327}
5328
5329static void handleAMDGPUWavesPerEUAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005330 const AttributeList &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005331 uint32_t Min = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005332 Expr *MinExpr = AL.getArgAsExpr(0);
5333 if (!checkUInt32Argument(S, AL, MinExpr, Min))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005334 return;
5335
5336 uint32_t Max = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005337 if (AL.getNumArgs() == 2) {
5338 Expr *MaxExpr = AL.getArgAsExpr(1);
5339 if (!checkUInt32Argument(S, AL, MaxExpr, Max))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005340 return;
5341 }
5342
5343 if (Min == 0 && Max != 0) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005344 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid)
5345 << AL.getName() << 0;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005346 return;
5347 }
5348 if (Max != 0 && Min > Max) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005349 S.Diag(AL.getLoc(), diag::err_attribute_argument_invalid)
5350 << AL.getName() << 1;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005351 return;
5352 }
5353
5354 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005355 AMDGPUWavesPerEUAttr(AL.getLoc(), S.Context, Min, Max,
5356 AL.getAttributeSpellingListIndex()));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005357}
5358
5359static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005360 const AttributeList &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005361 uint32_t NumSGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005362 Expr *NumSGPRExpr = AL.getArgAsExpr(0);
5363 if (!checkUInt32Argument(S, AL, NumSGPRExpr, NumSGPR))
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005364 return;
5365
5366 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005367 AMDGPUNumSGPRAttr(AL.getLoc(), S.Context, NumSGPR,
5368 AL.getAttributeSpellingListIndex()));
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005369}
5370
5371static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005372 const AttributeList &AL) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005373 uint32_t NumVGPR = 0;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005374 Expr *NumVGPRExpr = AL.getArgAsExpr(0);
5375 if (!checkUInt32Argument(S, AL, NumVGPRExpr, NumVGPR))
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005376 return;
5377
5378 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005379 AMDGPUNumVGPRAttr(AL.getLoc(), S.Context, NumVGPR,
5380 AL.getAttributeSpellingListIndex()));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005381}
5382
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005383static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005384 const AttributeList& AL) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005385 // If we try to apply it to a function pointer, don't warn, but don't
5386 // do anything, either. It doesn't matter anyway, because there's nothing
5387 // special about calling a force_align_arg_pointer function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005388 const auto *VD = dyn_cast<ValueDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005389 if (VD && VD->getType()->isFunctionPointerType())
5390 return;
5391 // Also don't warn on function pointer typedefs.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005392 const auto *TD = dyn_cast<TypedefNameDecl>(D);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005393 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
5394 TD->getUnderlyingType()->isFunctionType()))
5395 return;
5396 // Attribute can only be applied to function types.
5397 if (!isa<FunctionDecl>(D)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005398 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
5399 << AL.getName() << ExpectedFunction;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005400 return;
5401 }
5402
Aaron Ballman36a53502014-01-16 13:03:14 +00005403 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005404 X86ForceAlignArgPointerAttr(AL.getRange(), S.Context,
5405 AL.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005406}
5407
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005408static void handleLayoutVersion(Sema &S, Decl *D, const AttributeList &AL) {
David Majnemercd3ebfe2016-05-23 17:16:12 +00005409 uint32_t Version;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005410 Expr *VersionExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
5411 if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), Version))
David Majnemercd3ebfe2016-05-23 17:16:12 +00005412 return;
5413
5414 // TODO: Investigate what happens with the next major version of MSVC.
5415 if (Version != LangOptions::MSVC2015) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005416 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
5417 << AL.getName() << Version << VersionExpr->getSourceRange();
David Majnemercd3ebfe2016-05-23 17:16:12 +00005418 return;
5419 }
5420
5421 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005422 LayoutVersionAttr(AL.getRange(), S.Context, Version,
5423 AL.getAttributeSpellingListIndex()));
David Majnemercd3ebfe2016-05-23 17:16:12 +00005424}
5425
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005426DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
5427 unsigned AttrSpellingListIndex) {
5428 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00005429 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00005430 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005431 }
5432
5433 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00005434 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005435
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00005436 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005437}
5438
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005439DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
5440 unsigned AttrSpellingListIndex) {
5441 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00005442 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005443 D->dropAttr<DLLImportAttr>();
5444 }
5445
5446 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00005447 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005448
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00005449 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005450}
5451
Hans Wennborge82f19c2014-06-24 23:57:05 +00005452static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00005453 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
5454 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
5455 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored)
5456 << A.getName();
5457 return;
5458 }
5459
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005460 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Hans Wennborg606bd6d2014-11-03 14:24:45 +00005461 if (FD->isInlined() && A.getKind() == AttributeList::AT_DLLImport &&
5462 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
5463 // MinGW doesn't allow dllimport on inline functions.
5464 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
5465 << A.getName();
5466 return;
5467 }
5468 }
5469
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005470 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
Hans Wennborg5869ec42015-09-15 21:05:30 +00005471 if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
5472 MD->getParent()->isLambda()) {
5473 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A.getName();
5474 return;
5475 }
5476 }
5477
Hans Wennborge82f19c2014-06-24 23:57:05 +00005478 unsigned Index = A.getAttributeSpellingListIndex();
5479 Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport
5480 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
5481 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005482 if (NewAttr)
5483 D->addAttr(NewAttr);
5484}
5485
David Majnemer2c4e00a2014-01-29 22:07:36 +00005486MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00005487Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00005488 unsigned AttrSpellingListIndex,
5489 MSInheritanceAttr::Spelling SemanticSpelling) {
5490 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
5491 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00005492 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005493 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
5494 << 1 /*previous declaration*/;
5495 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
5496 D->dropAttr<MSInheritanceAttr>();
5497 }
5498
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005499 auto *RD = cast<CXXRecordDecl>(D);
David Majnemer2c4e00a2014-01-29 22:07:36 +00005500 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00005501 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
5502 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00005503 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005504 }
5505 } else {
5506 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
5507 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
5508 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00005509 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005510 }
5511 if (RD->getDescribedClassTemplate()) {
5512 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
5513 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00005514 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005515 }
5516 }
5517
5518 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00005519 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00005520}
5521
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005522static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005523 // The capability attributes take a single string parameter for the name of
5524 // the capability they represent. The lockable attribute does not take any
5525 // parameters. However, semantically, both attributes represent the same
5526 // concept, and so they use the same semantic attribute. Eventually, the
5527 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00005528 //
Alp Toker958027b2014-07-14 19:42:55 +00005529 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00005530 // literal will be considered a "mutex."
5531 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005532 SourceLocation LiteralLoc;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005533 if (AL.getKind() == AttributeList::AT_Capability &&
5534 !S.checkStringLiteralArgumentAttr(AL, 0, N, &LiteralLoc))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005535 return;
5536
Aaron Ballman6c810072014-03-05 21:47:13 +00005537 // Currently, there are only two names allowed for a capability: role and
5538 // mutex (case insensitive). Diagnose other capability names.
5539 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
5540 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
5541
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005542 D->addAttr(::new (S.Context) CapabilityAttr(AL.getRange(), S.Context, N,
5543 AL.getAttributeSpellingListIndex()));
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005544}
5545
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005546static void handleAssertCapabilityAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005547 const AttributeList &AL) {
Josh Gaoec1369e2017-08-08 19:44:34 +00005548 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005549 if (!checkLockFunAttrCommon(S, D, AL, Args))
Josh Gaoec1369e2017-08-08 19:44:34 +00005550 return;
5551
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005552 D->addAttr(::new (S.Context) AssertCapabilityAttr(AL.getRange(), S.Context,
Josh Gaoec1369e2017-08-08 19:44:34 +00005553 Args.data(), Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005554 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005555}
5556
5557static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005558 const AttributeList &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005559 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005560 if (!checkLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005561 return;
5562
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005563 D->addAttr(::new (S.Context) AcquireCapabilityAttr(AL.getRange(),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005564 S.Context,
5565 Args.data(), Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005566 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005567}
5568
5569static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005570 const AttributeList &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005571 SmallVector<Expr*, 2> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005572 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005573 return;
5574
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005575 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(AL.getRange(),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005576 S.Context,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005577 AL.getArgAsExpr(0),
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005578 Args.data(),
5579 Args.size(),
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005580 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005581}
5582
5583static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005584 const AttributeList &AL) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005585 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00005586 SmallVector<Expr *, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005587 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005588
Aaron Ballman18d85ae2014-03-20 16:02:49 +00005589 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005590 AL.getRange(), S.Context, Args.data(), Args.size(),
5591 AL.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005592}
5593
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005594static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005595 const AttributeList &AL) {
5596 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005597 return;
5598
5599 // check that all arguments are lockable objects
5600 SmallVector<Expr*, 1> Args;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005601 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005602 if (Args.empty())
5603 return;
5604
5605 RequiresCapabilityAttr *RCA = ::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005606 RequiresCapabilityAttr(AL.getRange(), S.Context, Args.data(),
5607 Args.size(), AL.getAttributeSpellingListIndex());
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005608
5609 D->addAttr(RCA);
5610}
5611
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005612static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &AL) {
5613 if (const auto *NSD = dyn_cast<NamespaceDecl>(D)) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005614 if (NSD->isAnonymousNamespace()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005615 S.Diag(AL.getLoc(), diag::warn_deprecated_anonymous_namespace);
Aaron Ballman43f40102014-11-14 22:34:56 +00005616 // Do not want to attach the attribute to the namespace because that will
5617 // cause confusing diagnostic reports for uses of declarations within the
5618 // namespace.
5619 return;
5620 }
5621 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00005622
Manman Renc7890fe2016-03-16 18:50:49 +00005623 // Handle the cases where the attribute has a text message.
5624 StringRef Str, Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005625 if (AL.isArgExpr(0) && AL.getArgAsExpr(0) &&
5626 !S.checkStringLiteralArgumentAttr(AL, 0, Str))
Manman Renc7890fe2016-03-16 18:50:49 +00005627 return;
5628
5629 // Only support a single optional message for Declspec and CXX11.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005630 if (AL.isDeclspecAttribute() || AL.isCXX11Attribute())
5631 checkAttributeAtMostNumArgs(S, AL, 1);
5632 else if (AL.isArgExpr(1) && AL.getArgAsExpr(1) &&
5633 !S.checkStringLiteralArgumentAttr(AL, 1, Replacement))
Manman Renc7890fe2016-03-16 18:50:49 +00005634 return;
5635
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00005636 if (!S.getLangOpts().CPlusPlus14)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005637 if (AL.isCXX11Attribute() &&
5638 !(AL.hasScope() && AL.getScopeName()->isStr("gnu")))
5639 S.Diag(AL.getLoc(), diag::ext_cxx14_attr) << AL.getName();
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00005640
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005641 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005642 DeprecatedAttr(AL.getRange(), S.Context, Str, Replacement,
5643 AL.getAttributeSpellingListIndex()));
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005644}
5645
5646static bool isGlobalVar(const Decl *D) {
5647 if (const auto *S = dyn_cast<VarDecl>(D))
5648 return S->hasGlobalStorage();
5649 return false;
Aaron Ballman43f40102014-11-14 22:34:56 +00005650}
5651
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005652static void handleNoSanitizeAttr(Sema &S, Decl *D, const AttributeList &AL) {
5653 if (!checkAttributeAtLeastNumArgs(S, AL, 1))
Peter Collingbourne915df992015-05-15 18:33:32 +00005654 return;
5655
Benjamin Kramer1b582012016-02-13 18:11:49 +00005656 std::vector<StringRef> Sanitizers;
Peter Collingbourne915df992015-05-15 18:33:32 +00005657
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005658 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
Peter Collingbourne915df992015-05-15 18:33:32 +00005659 StringRef SanitizerName;
5660 SourceLocation LiteralLoc;
5661
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005662 if (!S.checkStringLiteralArgumentAttr(AL, I, SanitizerName, &LiteralLoc))
Peter Collingbourne915df992015-05-15 18:33:32 +00005663 return;
5664
5665 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0)
5666 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005667 else if (isGlobalVar(D) && SanitizerName != "address")
5668 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005669 << AL.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne915df992015-05-15 18:33:32 +00005670 Sanitizers.push_back(SanitizerName);
5671 }
5672
5673 D->addAttr(::new (S.Context) NoSanitizeAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005674 AL.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(),
5675 AL.getAttributeSpellingListIndex()));
Peter Collingbourne915df992015-05-15 18:33:32 +00005676}
5677
5678static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005679 const AttributeList &AL) {
5680 StringRef AttrName = AL.getName()->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00005681 normalizeName(AttrName);
Douglas Katzman3ed0f642016-10-14 19:55:09 +00005682 StringRef SanitizerName = llvm::StringSwitch<StringRef>(AttrName)
5683 .Case("no_address_safety_analysis", "address")
5684 .Case("no_sanitize_address", "address")
5685 .Case("no_sanitize_thread", "thread")
5686 .Case("no_sanitize_memory", "memory");
5687 if (isGlobalVar(D) && SanitizerName != "address")
5688 S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005689 << AL.getName() << ExpectedFunction;
Peter Collingbourne915df992015-05-15 18:33:32 +00005690 D->addAttr(::new (S.Context)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005691 NoSanitizeAttr(AL.getRange(), S.Context, &SanitizerName, 1,
5692 AL.getAttributeSpellingListIndex()));
Peter Collingbourne915df992015-05-15 18:33:32 +00005693}
5694
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005695static void handleInternalLinkageAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005696 const AttributeList &AL) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005697 if (InternalLinkageAttr *Internal =
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005698 S.mergeInternalLinkageAttr(D, AL.getRange(), AL.getName(),
5699 AL.getAttributeSpellingListIndex()))
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005700 D->addAttr(Internal);
5701}
5702
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005703static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const AttributeList &AL) {
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005704 if (S.LangOpts.OpenCLVersion != 200)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005705 S.Diag(AL.getLoc(), diag::err_attribute_requires_opencl_version)
5706 << AL.getName() << "2.0" << 0;
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005707 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005708 S.Diag(AL.getLoc(), diag::warn_opencl_attr_deprecated_ignored)
5709 << AL.getName() << "2.0";
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005710}
5711
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005712/// Handles semantic checking for features that are common to all attributes,
5713/// such as checking whether a parameter was properly specified, or the correct
5714/// number of arguments were passed, etc.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005715static bool handleCommonAttributeFeatures(Sema &S, Decl *D,
5716 const AttributeList &AL) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005717 // Several attributes carry different semantics than the parsing requires, so
Alex Lorenz24952fb2017-04-19 15:52:11 +00005718 // those are opted out of the common argument checks.
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005719 //
5720 // We also bail on unknown and ignored attributes because those are handled
5721 // as part of the target-specific handling logic.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005722 if (AL.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005723 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005724 // Check whether the attribute requires specific language extensions to be
5725 // enabled.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005726 if (!AL.diagnoseLangOpts(S))
Aaron Ballman3aff6332013-12-02 19:30:36 +00005727 return true;
Alex Lorenz24952fb2017-04-19 15:52:11 +00005728 // Check whether the attribute appertains to the given subject.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005729 if (!AL.diagnoseAppertainsTo(S, D))
Alex Lorenz24952fb2017-04-19 15:52:11 +00005730 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005731 if (AL.hasCustomParsing())
Alex Lorenz24952fb2017-04-19 15:52:11 +00005732 return false;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005733
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005734 if (AL.getMinArgs() == AL.getMaxArgs()) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005735 // If there are no optional arguments, then checking for the argument count
5736 // is trivial.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005737 if (!checkAttributeNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005738 return true;
5739 } else {
5740 // There are optional arguments, so checking is slightly more involved.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005741 if (AL.getMinArgs() &&
5742 !checkAttributeAtLeastNumArgs(S, AL, AL.getMinArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005743 return true;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005744 else if (!AL.hasVariadicArg() && AL.getMaxArgs() &&
5745 !checkAttributeAtMostNumArgs(S, AL, AL.getMaxArgs()))
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005746 return true;
5747 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005748
Oren Ben Simhon220671a2018-03-17 13:31:35 +00005749 if (S.CheckAttrTarget(AL))
5750 return true;
5751
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005752 return false;
5753}
5754
Xiuli Pan11e13f62016-02-26 03:13:03 +00005755static void handleOpenCLAccessAttr(Sema &S, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005756 const AttributeList &AL) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00005757 if (D->isInvalidDecl())
5758 return;
5759
5760 // Check if there is only one access qualifier.
5761 if (D->hasAttr<OpenCLAccessAttr>()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005762 S.Diag(AL.getLoc(), diag::err_opencl_multiple_access_qualifiers)
Xiuli Pan11e13f62016-02-26 03:13:03 +00005763 << D->getSourceRange();
5764 D->setInvalidDecl(true);
5765 return;
5766 }
5767
5768 // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that an
5769 // image object can be read and written.
5770 // OpenCL v2.0 s6.13.6 - A kernel cannot read from and write to the same pipe
5771 // object. Using the read_write (or __read_write) qualifier with the pipe
5772 // qualifier is a compilation error.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005773 if (const auto *PDecl = dyn_cast<ParmVarDecl>(D)) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00005774 const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005775 if (AL.getName()->getName().find("read_write") != StringRef::npos) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00005776 if (S.getLangOpts().OpenCLVersion < 200 || DeclTy->isPipeType()) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005777 S.Diag(AL.getLoc(), diag::err_opencl_invalid_read_write)
5778 << AL.getName() << PDecl->getType() << DeclTy->isImageType();
Xiuli Pan11e13f62016-02-26 03:13:03 +00005779 D->setInvalidDecl(true);
5780 return;
5781 }
5782 }
5783 }
5784
5785 D->addAttr(::new (S.Context) OpenCLAccessAttr(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005786 AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
Xiuli Pan11e13f62016-02-26 03:13:03 +00005787}
5788
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005789//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005790// Top Level Sema Entry Points
5791//===----------------------------------------------------------------------===//
5792
Richard Smithf8a75c32013-08-29 00:47:48 +00005793/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
5794/// the attribute applies to decls. If the attribute is a type attribute, just
5795/// silently ignore it if a GNU attribute.
5796static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005797 const AttributeList &AL,
Richard Smithf8a75c32013-08-29 00:47:48 +00005798 bool IncludeCXX11Attributes) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005799 if (AL.isInvalid() || AL.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00005800 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00005801
Richard Smithf8a75c32013-08-29 00:47:48 +00005802 // Ignore C++11 attributes on declarator chunks: they appertain to the type
5803 // instead.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005804 if (AL.isCXX11Attribute() && !IncludeCXX11Attributes)
Richard Smithf8a75c32013-08-29 00:47:48 +00005805 return;
5806
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005807 // Unknown attributes are automatically warned on. Target-specific attributes
5808 // which do not apply to the current target architecture are treated as
5809 // though they were unknown attributes.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005810 if (AL.getKind() == AttributeList::UnknownAttribute ||
5811 !AL.existsInTarget(S.Context.getTargetInfo())) {
5812 S.Diag(AL.getLoc(), AL.isDeclspecAttribute()
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005813 ? diag::warn_unhandled_ms_attribute_ignored
5814 : diag::warn_unknown_attribute_ignored)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005815 << AL.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005816 return;
5817 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005818
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005819 if (handleCommonAttributeFeatures(S, D, AL))
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005820 return;
5821
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005822 switch (AL.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005823 default:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005824 if (!AL.isStmtAttr()) {
Richard Smith4f902c72016-03-08 00:32:55 +00005825 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005826 assert(AL.isTypeAttr() && "Non-type attribute not handled");
Richard Smith4f902c72016-03-08 00:32:55 +00005827 break;
5828 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005829 S.Diag(AL.getLoc(), diag::err_stmt_attribute_invalid_on_decl)
5830 << AL.getName() << D->getLocation();
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005831 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005832 case AttributeList::AT_Interrupt:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005833 handleInterruptAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005834 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005835 case AttributeList::AT_X86ForceAlignArgPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005836 handleX86ForceAlignArgPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005837 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005838 case AttributeList::AT_DLLExport:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005839 case AttributeList::AT_DLLImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005840 handleDLLAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005841 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005842 case AttributeList::AT_Mips16:
Simon Atanasyan2c87f532017-05-22 12:47:43 +00005843 handleSimpleAttributeWithExclusions<Mips16Attr, MicroMipsAttr,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005844 MipsInterruptAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005845 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005846 case AttributeList::AT_NoMips16:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005847 handleSimpleAttribute<NoMips16Attr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005848 break;
Simon Atanasyan2c87f532017-05-22 12:47:43 +00005849 case AttributeList::AT_MicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005850 handleSimpleAttributeWithExclusions<MicroMipsAttr, Mips16Attr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00005851 break;
5852 case AttributeList::AT_NoMicroMips:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005853 handleSimpleAttribute<NoMicroMipsAttr>(S, D, AL);
Simon Atanasyan2c87f532017-05-22 12:47:43 +00005854 break;
Simon Atanasyan1a116db2017-07-20 20:34:18 +00005855 case AttributeList::AT_MipsLongCall:
5856 handleSimpleAttributeWithExclusions<MipsLongCallAttr, MipsShortCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005857 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00005858 break;
5859 case AttributeList::AT_MipsShortCall:
5860 handleSimpleAttributeWithExclusions<MipsShortCallAttr, MipsLongCallAttr>(
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005861 S, D, AL);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00005862 break;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005863 case AttributeList::AT_AMDGPUFlatWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005864 handleAMDGPUFlatWorkGroupSizeAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005865 break;
5866 case AttributeList::AT_AMDGPUWavesPerEU:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005867 handleAMDGPUWavesPerEUAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005868 break;
5869 case AttributeList::AT_AMDGPUNumSGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005870 handleAMDGPUNumSGPRAttr(S, D, AL);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005871 break;
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005872 case AttributeList::AT_AMDGPUNumVGPR:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005873 handleAMDGPUNumVGPRAttr(S, D, AL);
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00005874 break;
Dylan McKaye8232d72017-02-08 05:09:26 +00005875 case AttributeList::AT_AVRSignal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005876 handleAVRSignalAttr(S, D, AL);
Dylan McKaye8232d72017-02-08 05:09:26 +00005877 break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00005878 case AttributeList::AT_IBAction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005879 handleSimpleAttribute<IBActionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005880 break;
5881 case AttributeList::AT_IBOutlet:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005882 handleIBOutlet(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005883 break;
Richard Smith10876ef2013-01-17 01:30:42 +00005884 case AttributeList::AT_IBOutletCollection:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005885 handleIBOutletCollection(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005886 break;
Dmitry Polukhin85eda122016-04-11 07:48:59 +00005887 case AttributeList::AT_IFunc:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005888 handleIFuncAttr(S, D, AL);
Dmitry Polukhin85eda122016-04-11 07:48:59 +00005889 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005890 case AttributeList::AT_Alias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005891 handleAliasAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005892 break;
5893 case AttributeList::AT_Aligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005894 handleAlignedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005895 break;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00005896 case AttributeList::AT_AlignValue:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005897 handleAlignValueAttr(S, D, AL);
Hal Finkel1b0d24e2014-10-02 21:21:25 +00005898 break;
George Burgess IVe3763372016-12-22 02:50:20 +00005899 case AttributeList::AT_AllocSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005900 handleAllocSizeAttr(S, D, AL);
George Burgess IVe3763372016-12-22 02:50:20 +00005901 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005902 case AttributeList::AT_AlwaysInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005903 handleAlwaysInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005904 break;
Erich Keane293a0552018-02-14 00:14:07 +00005905 case AttributeList::AT_Artificial:
Aaron Ballman736c09b2018-02-15 16:28:10 +00005906 handleSimpleAttribute<ArtificialAttr>(S, D, AL);
Erich Keane293a0552018-02-14 00:14:07 +00005907 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005908 case AttributeList::AT_AnalyzerNoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005909 handleAnalyzerNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005910 break;
5911 case AttributeList::AT_TLSModel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005912 handleTLSModelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005913 break;
5914 case AttributeList::AT_Annotate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005915 handleAnnotateAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005916 break;
5917 case AttributeList::AT_Availability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005918 handleAvailabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005919 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005920 case AttributeList::AT_CarriesDependency:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005921 handleDependencyAttr(S, scope, D, AL);
Richard Smithe233fbf2013-01-28 22:42:45 +00005922 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005923 case AttributeList::AT_Common:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005924 handleCommonAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005925 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005926 case AttributeList::AT_CUDAConstant:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005927 handleConstantAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005928 break;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00005929 case AttributeList::AT_PassObjectSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005930 handlePassObjectSizeAttr(S, D, AL);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00005931 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005932 case AttributeList::AT_Constructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005933 handleConstructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005934 break;
Richard Smith10876ef2013-01-17 01:30:42 +00005935 case AttributeList::AT_CXX11NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005936 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005937 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005938 case AttributeList::AT_Deprecated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005939 handleDeprecatedAttr(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00005940 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005941 case AttributeList::AT_Destructor:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005942 handleDestructorAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005943 break;
5944 case AttributeList::AT_EnableIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005945 handleEnableIfAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005946 break;
George Burgess IV177399e2017-01-09 04:12:14 +00005947 case AttributeList::AT_DiagnoseIf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005948 handleDiagnoseIfAttr(S, D, AL);
George Burgess IV177399e2017-01-09 04:12:14 +00005949 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005950 case AttributeList::AT_ExtVectorType:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005951 handleExtVectorTypeAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005952 break;
Alex Lorenzd5d27e12017-03-01 18:06:25 +00005953 case AttributeList::AT_ExternalSourceSymbol:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005954 handleExternalSourceSymbolAttr(S, D, AL);
Alex Lorenzd5d27e12017-03-01 18:06:25 +00005955 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00005956 case AttributeList::AT_MinSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005957 handleMinSizeAttr(S, D, AL);
Quentin Colombet4e172062012-11-01 23:55:47 +00005958 break;
Paul Robinsonf0674352014-03-31 22:29:15 +00005959 case AttributeList::AT_OptimizeNone:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005960 handleOptimizeNoneAttr(S, D, AL);
Paul Robinsonf0674352014-03-31 22:29:15 +00005961 break;
Alexis Hunt724f14e2014-11-28 00:53:20 +00005962 case AttributeList::AT_FlagEnum:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005963 handleSimpleAttribute<FlagEnumAttr>(S, D, AL);
Alexis Hunt724f14e2014-11-28 00:53:20 +00005964 break;
Akira Hatanaka3c268af2017-03-21 02:23:00 +00005965 case AttributeList::AT_EnumExtensibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005966 handleEnumExtensibilityAttr(S, D, AL);
Akira Hatanaka3c268af2017-03-21 02:23:00 +00005967 break;
Peter Collingbourne41af7c22014-05-20 17:12:51 +00005968 case AttributeList::AT_Flatten:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005969 handleSimpleAttribute<FlattenAttr>(S, D, AL);
Peter Collingbourne41af7c22014-05-20 17:12:51 +00005970 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005971 case AttributeList::AT_Format:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005972 handleFormatAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005973 break;
5974 case AttributeList::AT_FormatArg:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005975 handleFormatArgAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005976 break;
5977 case AttributeList::AT_CUDAGlobal:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005978 handleGlobalAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005979 break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00005980 case AttributeList::AT_CUDADevice:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005981 handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005982 AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005983 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005984 case AttributeList::AT_CUDAHost:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005985 handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D,
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005986 AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005987 break;
5988 case AttributeList::AT_GNUInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005989 handleGNUInlineAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005990 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005991 case AttributeList::AT_CUDALaunchBounds:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005992 handleLaunchBoundsAttr(S, D, AL);
Peter Collingbourne827301e2010-12-12 23:03:07 +00005993 break;
David Majnemer631a90b2015-02-04 07:23:21 +00005994 case AttributeList::AT_Restrict:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005995 handleRestrictAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005996 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005997 case AttributeList::AT_MayAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00005998 handleSimpleAttribute<MayAliasAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005999 break;
6000 case AttributeList::AT_Mode:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006001 handleModeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006002 break;
David Majnemer1bf0f8e2015-07-20 22:51:52 +00006003 case AttributeList::AT_NoAlias:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006004 handleSimpleAttribute<NoAliasAttr>(S, D, AL);
David Majnemer1bf0f8e2015-07-20 22:51:52 +00006005 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00006006 case AttributeList::AT_NoCommon:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006007 handleSimpleAttribute<NoCommonAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006008 break;
Peter Collingbourneb4728c12014-05-19 22:14:34 +00006009 case AttributeList::AT_NoSplitStack:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006010 handleSimpleAttribute<NoSplitStackAttr>(S, D, AL);
Peter Collingbourneb4728c12014-05-19 22:14:34 +00006011 break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00006012 case AttributeList::AT_NonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006013 if (auto *PVD = dyn_cast<ParmVarDecl>(D))
6014 handleNonNullAttrParameter(S, PVD, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006015 else
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006016 handleNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006017 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00006018 case AttributeList::AT_ReturnsNonNull:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006019 handleReturnsNonNullAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006020 break;
Akira Hatanaka98a49332017-09-22 00:41:05 +00006021 case AttributeList::AT_NoEscape:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006022 handleNoEscapeAttr(S, D, AL);
Akira Hatanaka98a49332017-09-22 00:41:05 +00006023 break;
Hal Finkelee90a222014-09-26 05:04:30 +00006024 case AttributeList::AT_AssumeAligned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006025 handleAssumeAlignedAttr(S, D, AL);
Hal Finkelee90a222014-09-26 05:04:30 +00006026 break;
Erich Keane623efd82017-03-30 21:48:55 +00006027 case AttributeList::AT_AllocAlign:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006028 handleAllocAlignAttr(S, D, AL);
Erich Keane623efd82017-03-30 21:48:55 +00006029 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00006030 case AttributeList::AT_Overloadable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006031 handleSimpleAttribute<OverloadableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006032 break;
6033 case AttributeList::AT_Ownership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006034 handleOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006035 break;
6036 case AttributeList::AT_Cold:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006037 handleSimpleAttributeWithExclusions<ColdAttr, HotAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006038 break;
6039 case AttributeList::AT_Hot:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006040 handleSimpleAttributeWithExclusions<HotAttr, ColdAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006041 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00006042 case AttributeList::AT_Naked:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006043 handleNakedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006044 break;
6045 case AttributeList::AT_NoReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006046 handleNoReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006047 break;
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006048 case AttributeList::AT_AnyX86NoCfCheck:
6049 handleNoCfCheckAttr(S, D, AL);
6050 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00006051 case AttributeList::AT_NoThrow:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006052 handleSimpleAttribute<NoThrowAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006053 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00006054 case AttributeList::AT_CUDAShared:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006055 handleSharedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006056 break;
6057 case AttributeList::AT_VecReturn:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006058 handleVecReturnAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006059 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006060 case AttributeList::AT_ObjCOwnership:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006061 handleObjCOwnershipAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006062 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006063 case AttributeList::AT_ObjCPreciseLifetime:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006064 handleObjCPreciseLifetimeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006065 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006066 case AttributeList::AT_ObjCReturnsInnerPointer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006067 handleObjCReturnsInnerPointerAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006068 break;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00006069 case AttributeList::AT_ObjCRequiresSuper:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006070 handleObjCRequiresSuperAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006071 break;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00006072 case AttributeList::AT_ObjCBridge:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006073 handleObjCBridgeAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006074 break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00006075 case AttributeList::AT_ObjCBridgeMutable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006076 handleObjCBridgeMutableAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006077 break;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00006078 case AttributeList::AT_ObjCBridgeRelated:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006079 handleObjCBridgeRelatedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006080 break;
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00006081 case AttributeList::AT_ObjCDesignatedInitializer:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006082 handleObjCDesignatedInitializer(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006083 break;
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00006084 case AttributeList::AT_ObjCRuntimeName:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006085 handleObjCRuntimeName(S, D, AL);
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00006086 break;
Douglas Gregor24ae22c2016-04-01 23:23:52 +00006087 case AttributeList::AT_ObjCRuntimeVisible:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006088 handleSimpleAttribute<ObjCRuntimeVisibleAttr>(S, D, AL);
Douglas Gregor24ae22c2016-04-01 23:23:52 +00006089 break;
Alex Denisovfde64952015-06-26 05:28:36 +00006090 case AttributeList::AT_ObjCBoxable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006091 handleObjCBoxable(S, D, AL);
Alex Denisovfde64952015-06-26 05:28:36 +00006092 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006093 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006094 handleSimpleAttributeWithExclusions<CFAuditedTransferAttr,
6095 CFUnknownTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006096 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006097 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006098 handleSimpleAttributeWithExclusions<CFUnknownTransferAttr,
6099 CFAuditedTransferAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006100 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006101 case AttributeList::AT_CFConsumed:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006102 case AttributeList::AT_NSConsumed:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006103 handleNSConsumedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006104 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006105 case AttributeList::AT_NSConsumesSelf:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006106 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006107 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006108 case AttributeList::AT_NSReturnsAutoreleased:
6109 case AttributeList::AT_NSReturnsNotRetained:
6110 case AttributeList::AT_CFReturnsNotRetained:
6111 case AttributeList::AT_NSReturnsRetained:
6112 case AttributeList::AT_CFReturnsRetained:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006113 handleNSReturnsRetainedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006114 break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00006115 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006116 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006117 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006118 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006119 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006120 break;
Xiuli Panbe6da4b2017-05-04 07:31:20 +00006121 case AttributeList::AT_OpenCLIntelReqdSubGroupSize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006122 handleSubGroupSize(S, D, AL);
Xiuli Panbe6da4b2017-05-04 07:31:20 +00006123 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00006124 case AttributeList::AT_VecTypeHint:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006125 handleVecTypeHint(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006126 break;
Eric Fiselier341e8252016-09-02 18:53:31 +00006127 case AttributeList::AT_RequireConstantInit:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006128 handleSimpleAttribute<RequireConstantInitAttr>(S, D, AL);
Eric Fiselier341e8252016-09-02 18:53:31 +00006129 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006130 case AttributeList::AT_InitPriority:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006131 handleInitPriorityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006132 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006133 case AttributeList::AT_Packed:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006134 handlePackedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006135 break;
6136 case AttributeList::AT_Section:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006137 handleSectionAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006138 break;
Eric Christopher11acf732015-06-12 01:35:52 +00006139 case AttributeList::AT_Target:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006140 handleTargetAttr(S, D, AL);
Eric Christopher11acf732015-06-12 01:35:52 +00006141 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006142 case AttributeList::AT_Unavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006143 handleAttrWithMessage<UnavailableAttr>(S, D, AL);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00006144 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006145 case AttributeList::AT_ArcWeakrefUnavailable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006146 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006147 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006148 case AttributeList::AT_ObjCRootClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006149 handleSimpleAttribute<ObjCRootClassAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006150 break;
Alex Lorenza8c44ba2016-10-28 10:25:10 +00006151 case AttributeList::AT_ObjCSubclassingRestricted:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006152 handleSimpleAttribute<ObjCSubclassingRestrictedAttr>(S, D, AL);
Alex Lorenza8c44ba2016-10-28 10:25:10 +00006153 break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00006154 case AttributeList::AT_ObjCExplicitProtocolImpl:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006155 handleObjCSuppresProtocolAttr(S, D, AL);
Ted Kremenek28eace62013-11-23 01:01:34 +00006156 break;
6157 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006158 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006159 break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00006160 case AttributeList::AT_Unused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006161 handleUnusedAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006162 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006163 case AttributeList::AT_ReturnsTwice:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006164 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006165 break;
Akira Hatanakac8667622015-11-06 23:56:15 +00006166 case AttributeList::AT_NotTailCalled:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006167 handleSimpleAttributeWithExclusions<NotTailCalledAttr,
6168 AlwaysInlineAttr>(S, D, AL);
Akira Hatanakac8667622015-11-06 23:56:15 +00006169 break;
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00006170 case AttributeList::AT_DisableTailCalls:
Aaron Ballman3cfa9d12018-03-04 15:32:01 +00006171 handleSimpleAttributeWithExclusions<DisableTailCallsAttr,
6172 NakedAttr>(S, D, AL);
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00006173 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006174 case AttributeList::AT_Used:
Aaron Ballman1a3901c2018-03-03 21:02:09 +00006175 handleSimpleAttribute<UsedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006176 break;
John McCalld041a9b2013-02-20 01:54:26 +00006177 case AttributeList::AT_Visibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006178 handleVisibilityAttr(S, D, AL, false);
John McCalld041a9b2013-02-20 01:54:26 +00006179 break;
6180 case AttributeList::AT_TypeVisibility:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006181 handleVisibilityAttr(S, D, AL, true);
John McCalld041a9b2013-02-20 01:54:26 +00006182 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00006183 case AttributeList::AT_WarnUnused:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006184 handleSimpleAttribute<WarnUnusedAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006185 break;
6186 case AttributeList::AT_WarnUnusedResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006187 handleWarnUnusedResult(S, D, AL);
Chris Lattner237f2752009-02-14 07:37:35 +00006188 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00006189 case AttributeList::AT_Weak:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006190 handleSimpleAttribute<WeakAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006191 break;
6192 case AttributeList::AT_WeakRef:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006193 handleWeakRefAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006194 break;
6195 case AttributeList::AT_WeakImport:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006196 handleWeakImportAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006197 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006198 case AttributeList::AT_TransparentUnion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006199 handleTransparentUnionAttr(S, D, AL);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006200 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006201 case AttributeList::AT_ObjCException:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006202 handleSimpleAttribute<ObjCExceptionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006203 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006204 case AttributeList::AT_ObjCMethodFamily:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006205 handleObjCMethodFamilyAttr(S, D, AL);
John McCall86bc21f2011-03-02 11:33:24 +00006206 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006207 case AttributeList::AT_ObjCNSObject:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006208 handleObjCNSObject(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006209 break;
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00006210 case AttributeList::AT_ObjCIndependentClass:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006211 handleObjCIndependentClass(S, D, AL);
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00006212 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006213 case AttributeList::AT_Blocks:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006214 handleBlocksAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006215 break;
6216 case AttributeList::AT_Sentinel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006217 handleSentinelAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006218 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00006219 case AttributeList::AT_Const:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006220 handleSimpleAttribute<ConstAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006221 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00006222 case AttributeList::AT_Pure:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006223 handleSimpleAttribute<PureAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006224 break;
6225 case AttributeList::AT_Cleanup:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006226 handleCleanupAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006227 break;
6228 case AttributeList::AT_NoDebug:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006229 handleNoDebugAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006230 break;
Aaron Ballman7c19ab12014-02-22 16:59:24 +00006231 case AttributeList::AT_NoDuplicate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006232 handleSimpleAttribute<NoDuplicateAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006233 break;
Yaxun Liu7d07ae72016-11-01 18:45:32 +00006234 case AttributeList::AT_Convergent:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006235 handleSimpleAttribute<ConvergentAttr>(S, D, AL);
Yaxun Liu7d07ae72016-11-01 18:45:32 +00006236 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00006237 case AttributeList::AT_NoInline:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006238 handleSimpleAttribute<NoInlineAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006239 break;
6240 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006241 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006242 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006243 case AttributeList::AT_StdCall:
6244 case AttributeList::AT_CDecl:
6245 case AttributeList::AT_FastCall:
6246 case AttributeList::AT_ThisCall:
6247 case AttributeList::AT_Pascal:
Erich Keane757d3172016-11-02 18:29:35 +00006248 case AttributeList::AT_RegCall:
John McCall477f2bb2016-03-03 06:39:32 +00006249 case AttributeList::AT_SwiftCall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00006250 case AttributeList::AT_VectorCall:
Charles Davisb5a214e2013-08-30 04:39:01 +00006251 case AttributeList::AT_MSABI:
6252 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006253 case AttributeList::AT_Pcs:
Guy Benyeif0a014b2012-12-25 08:53:55 +00006254 case AttributeList::AT_IntelOclBicc:
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00006255 case AttributeList::AT_PreserveMost:
6256 case AttributeList::AT_PreserveAll:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006257 handleCallConvAttr(S, D, AL);
John McCallab26cfa2010-02-05 21:31:56 +00006258 break;
Matthias Gehre01a63382017-03-27 19:45:24 +00006259 case AttributeList::AT_Suppress:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006260 handleSuppressAttr(S, D, AL);
Matthias Gehre01a63382017-03-27 19:45:24 +00006261 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006262 case AttributeList::AT_OpenCLKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006263 handleSimpleAttribute<OpenCLKernelAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006264 break;
Xiuli Pan11e13f62016-02-26 03:13:03 +00006265 case AttributeList::AT_OpenCLAccess:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006266 handleOpenCLAccessAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006267 break;
Anastasia Stulovafde76222016-04-01 16:05:09 +00006268 case AttributeList::AT_OpenCLNoSVM:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006269 handleOpenCLNoSVMAttr(S, D, AL);
Anastasia Stulovafde76222016-04-01 16:05:09 +00006270 break;
John McCall477f2bb2016-03-03 06:39:32 +00006271 case AttributeList::AT_SwiftContext:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006272 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftContext);
John McCall477f2bb2016-03-03 06:39:32 +00006273 break;
6274 case AttributeList::AT_SwiftErrorResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006275 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftErrorResult);
John McCall477f2bb2016-03-03 06:39:32 +00006276 break;
6277 case AttributeList::AT_SwiftIndirectResult:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006278 handleParameterABIAttr(S, D, AL, ParameterABI::SwiftIndirectResult);
John McCall477f2bb2016-03-03 06:39:32 +00006279 break;
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00006280 case AttributeList::AT_InternalLinkage:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006281 handleInternalLinkageAttr(S, D, AL);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00006282 break;
Peter Collingbourne3afb2662016-04-28 17:09:37 +00006283 case AttributeList::AT_LTOVisibilityPublic:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006284 handleSimpleAttribute<LTOVisibilityPublicAttr>(S, D, AL);
Peter Collingbourne3afb2662016-04-28 17:09:37 +00006285 break;
John McCall8d32c052012-05-22 21:28:12 +00006286
6287 // Microsoft attributes:
David Majnemercd3ebfe2016-05-23 17:16:12 +00006288 case AttributeList::AT_EmptyBases:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006289 handleSimpleAttribute<EmptyBasesAttr>(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00006290 break;
6291 case AttributeList::AT_LayoutVersion:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006292 handleLayoutVersion(S, D, AL);
David Majnemercd3ebfe2016-05-23 17:16:12 +00006293 break;
Akira Hatanaka02914dc2018-02-05 20:23:22 +00006294 case AttributeList::AT_TrivialABI:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006295 handleSimpleAttribute<TrivialABIAttr>(S, D, AL);
Akira Hatanaka02914dc2018-02-05 20:23:22 +00006296 break;
David Majnemer8ab003a2015-02-02 19:30:52 +00006297 case AttributeList::AT_MSNoVTable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006298 handleSimpleAttribute<MSNoVTableAttr>(S, D, AL);
David Majnemer129f4172015-02-02 10:22:20 +00006299 break;
David Majnemer8ab003a2015-02-02 19:30:52 +00006300 case AttributeList::AT_MSStruct:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006301 handleSimpleAttribute<MSStructAttr>(S, D, AL);
John McCall8d32c052012-05-22 21:28:12 +00006302 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006303 case AttributeList::AT_Uuid:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006304 handleUuidAttr(S, D, AL);
Francois Picheta83957a2010-12-19 06:50:37 +00006305 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00006306 case AttributeList::AT_MSInheritance:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006307 handleMSInheritanceAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006308 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00006309 case AttributeList::AT_SelectAny:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006310 handleSimpleAttribute<SelectAnyAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006311 break;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00006312 case AttributeList::AT_Thread:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006313 handleDeclspecThreadAttr(S, D, AL);
Reid Kleckner7d6d2702014-05-01 03:16:47 +00006314 break;
David Majnemercd3ebfe2016-05-23 17:16:12 +00006315
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00006316 case AttributeList::AT_AbiTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006317 handleAbiTagAttr(S, D, AL);
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00006318 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006319
6320 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00006321 case AttributeList::AT_AssertExclusiveLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006322 handleAssertExclusiveLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00006323 break;
6324 case AttributeList::AT_AssertSharedLock:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006325 handleAssertSharedLockAttr(S, D, AL);
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00006326 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006327 case AttributeList::AT_GuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006328 handleSimpleAttribute<GuardedVarAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006329 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006330 case AttributeList::AT_PtGuardedVar:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006331 handlePtGuardedVarAttr(S, D, AL);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006332 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006333 case AttributeList::AT_ScopedLockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006334 handleSimpleAttribute<ScopedLockableAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006335 break;
Peter Collingbourne915df992015-05-15 18:33:32 +00006336 case AttributeList::AT_NoSanitize:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006337 handleNoSanitizeAttr(S, D, AL);
Peter Collingbourne915df992015-05-15 18:33:32 +00006338 break;
6339 case AttributeList::AT_NoSanitizeSpecific:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006340 handleNoSanitizeSpecificAttr(S, D, AL);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00006341 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006342 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006343 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, AL);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00006344 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006345 case AttributeList::AT_GuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006346 handleGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006347 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006348 case AttributeList::AT_PtGuardedBy:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006349 handlePtGuardedByAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006350 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006351 case AttributeList::AT_ExclusiveTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006352 handleExclusiveTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006353 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006354 case AttributeList::AT_LockReturned:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006355 handleLockReturnedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006356 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006357 case AttributeList::AT_LocksExcluded:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006358 handleLocksExcludedAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006359 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006360 case AttributeList::AT_SharedTrylockFunction:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006361 handleSharedTrylockFunctionAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006362 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006363 case AttributeList::AT_AcquiredBefore:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006364 handleAcquiredBeforeAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006365 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006366 case AttributeList::AT_AcquiredAfter:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006367 handleAcquiredAfterAttr(S, D, AL);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00006368 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00006369
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006370 // Capability analysis attributes.
6371 case AttributeList::AT_Capability:
6372 case AttributeList::AT_Lockable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006373 handleCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006374 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006375 case AttributeList::AT_RequiresCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006376 handleRequiresCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006377 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006378
6379 case AttributeList::AT_AssertCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006380 handleAssertCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006381 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006382 case AttributeList::AT_AcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006383 handleAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006384 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006385 case AttributeList::AT_ReleaseCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006386 handleReleaseCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006387 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00006388 case AttributeList::AT_TryAcquireCapability:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006389 handleTryAcquireCapabilityAttr(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006390 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00006391
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00006392 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00006393 case AttributeList::AT_Consumable:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006394 handleConsumableAttr(S, D, AL);
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00006395 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00006396 case AttributeList::AT_ConsumableAutoCast:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006397 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006398 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00006399 case AttributeList::AT_ConsumableSetOnRead:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006400 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, AL);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00006401 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00006402 case AttributeList::AT_CallableWhen:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006403 handleCallableWhenAttr(S, D, AL);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00006404 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00006405 case AttributeList::AT_ParamTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006406 handleParamTypestateAttr(S, D, AL);
DeLesley Hutchins69391772013-10-17 23:23:53 +00006407 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00006408 case AttributeList::AT_ReturnTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006409 handleReturnTypestateAttr(S, D, AL);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00006410 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00006411 case AttributeList::AT_SetTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006412 handleSetTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00006413 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00006414 case AttributeList::AT_TestTypestate:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006415 handleTestTypestateAttr(S, D, AL);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00006416 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00006417
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006418 // Type safety attributes.
6419 case AttributeList::AT_ArgumentWithTypeTag:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006420 handleArgumentWithTypeTagAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006421 break;
6422 case AttributeList::AT_TypeTagForDatatype:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006423 handleTypeTagForDatatypeAttr(S, D, AL);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00006424 break;
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00006425 case AttributeList::AT_AnyX86NoCallerSavedRegisters:
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006426 handleSimpleAttribute<AnyX86NoCallerSavedRegistersAttr>(S, D, AL);
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00006427 break;
Pirama Arumuga Nainare5d2d712016-06-10 21:51:18 +00006428 case AttributeList::AT_RenderScriptKernel:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006429 handleSimpleAttribute<RenderScriptKernelAttr>(S, D, AL);
Pirama Arumuga Nainar8b788d02016-06-09 23:34:20 +00006430 break;
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00006431 // XRay attributes.
6432 case AttributeList::AT_XRayInstrument:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006433 handleSimpleAttribute<XRayInstrumentAttr>(S, D, AL);
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00006434 break;
Dean Michael Berris418da3f2017-03-06 07:08:21 +00006435 case AttributeList::AT_XRayLogArgs:
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006436 handleXRayLogArgsAttr(S, D, AL);
Dean Michael Berris418da3f2017-03-06 07:08:21 +00006437 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006438 }
6439}
6440
6441/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
6442/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00006443void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00006444 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00006445 bool IncludeCXX11Attributes) {
6446 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00006447 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00006448
Joey Gouly2cd9db12013-12-13 16:15:28 +00006449 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00006450 // GCC accepts
6451 // static int a9 __attribute__((weakref));
6452 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00006453 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00006454 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
6455 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00006456 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00006457 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006458 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00006459
Aaron Ballmanbe243a72014-12-04 22:45:31 +00006460 // FIXME: We should be able to handle this in TableGen as well. It would be
6461 // good to have a way to specify "these attributes must appear as a group",
6462 // for these. Additionally, it would be good to have a way to specify "these
6463 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00006464 if (!D->hasAttr<OpenCLKernelAttr>()) {
6465 // These attributes cannot be applied to a non-kernel function.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006466 if (const auto *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00006467 // FIXME: This emits a different error message than
6468 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00006469 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006470 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006471 } else if (const auto *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00006472 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006473 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006474 } else if (const auto *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00006475 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00006476 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006477 } else if (const auto *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006478 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6479 << A << ExpectedKernelFunction;
6480 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006481 } else if (const auto *A = D->getAttr<AMDGPUWavesPerEUAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00006482 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6483 << A << ExpectedKernelFunction;
6484 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006485 } else if (const auto *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00006486 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6487 << A << ExpectedKernelFunction;
6488 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006489 } else if (const auto *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00006490 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6491 << A << ExpectedKernelFunction;
6492 D->setInvalidDecl();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006493 } else if (const auto *A = D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) {
Xiuli Panbe6da4b2017-05-04 07:31:20 +00006494 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
6495 D->setInvalidDecl();
Joey Gouly2cd9db12013-12-13 16:15:28 +00006496 }
6497 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00006498}
6499
Hiroshi Inoue939d9322017-06-30 05:40:31 +00006500// Helper for delayed processing TransparentUnion attribute.
Erich Keane2fe684b2017-02-28 20:44:39 +00006501void Sema::ProcessDeclAttributeDelayed(Decl *D, const AttributeList *AttrList) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006502 for (const AttributeList *AL = AttrList; AL; AL = AL->getNext())
6503 if (AL->getKind() == AttributeList::AT_TransparentUnion) {
6504 handleTransparentUnionAttr(*this, D, *AL);
Erich Keane2fe684b2017-02-28 20:44:39 +00006505 break;
6506 }
6507}
6508
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006509// Annotation attributes are the only attributes allowed after an access
6510// specifier.
6511bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
6512 const AttributeList *AttrList) {
6513 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00006514 if (l->getKind() == AttributeList::AT_Annotate) {
David Majnemer706f3152014-12-14 01:05:01 +00006515 ProcessDeclAttribute(*this, nullptr, ASDecl, *l, l->isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00006516 } else {
6517 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
6518 return true;
6519 }
6520 }
6521
6522 return false;
6523}
6524
John McCall42856de2011-10-01 05:17:03 +00006525/// checkUnusedDeclAttributes - Check a list of attributes to see if it
6526/// contains any decl attributes that we should warn about.
6527static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
6528 for ( ; A; A = A->getNext()) {
6529 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00006530 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00006531 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
6532
6533 if (A->getKind() == AttributeList::UnknownAttribute) {
6534 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
6535 << A->getName() << A->getRange();
6536 } else {
6537 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
6538 << A->getName() << A->getRange();
6539 }
6540 }
6541}
6542
6543/// checkUnusedDeclAttributes - Given a declarator which is not being
6544/// used to build a declaration, complain about any decl attributes
6545/// which might be lying around on it.
6546void Sema::checkUnusedDeclAttributes(Declarator &D) {
6547 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
6548 ::checkUnusedDeclAttributes(*this, D.getAttributes());
6549 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
6550 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
6551}
6552
Ryan Flynn7d470f32009-07-30 03:15:39 +00006553/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00006554/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00006555NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
6556 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00006557 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00006558 NamedDecl *NewD = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006559 if (auto *FD = dyn_cast<FunctionDecl>(ND)) {
Alexander Kornienko061900f2015-12-03 11:37:28 +00006560 FunctionDecl *NewFD;
6561 // FIXME: Missing call to CheckFunctionDeclaration().
Eli Friedmance3e2c82011-09-07 04:05:06 +00006562 // FIXME: Mangling?
6563 // FIXME: Is the qualifier info correct?
6564 // FIXME: Is the DeclContext correct?
Alexander Kornienko061900f2015-12-03 11:37:28 +00006565 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
6566 Loc, Loc, DeclarationName(II),
6567 FD->getType(), FD->getTypeSourceInfo(),
6568 SC_None, false/*isInlineSpecified*/,
6569 FD->hasPrototype(),
6570 false/*isConstexprSpecified*/);
Eli Friedmance3e2c82011-09-07 04:05:06 +00006571 NewD = NewFD;
6572
6573 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00006574 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00006575
6576 // Fake up parameter variables; they are declared as if this were
6577 // a typedef.
6578 QualType FDTy = FD->getType();
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006579 if (const auto *FT = FDTy->getAs<FunctionProtoType>()) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00006580 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00006581 for (const auto &AI : FT->param_types()) {
6582 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00006583 Param->setScopeInfo(0, Params.size());
6584 Params.push_back(Param);
6585 }
David Blaikie9c70e042011-09-21 18:16:56 +00006586 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00006587 }
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006588 } else if (auto *VD = dyn_cast<VarDecl>(ND)) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00006589 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00006590 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00006591 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00006592 VD->getStorageClass());
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006593 if (VD->getQualifier())
6594 cast<VarDecl>(NewD)->setQualifierInfo(VD->getQualifierLoc());
Ryan Flynn7d470f32009-07-30 03:15:39 +00006595 }
6596 return NewD;
6597}
6598
James Dennett634962f2012-06-14 21:40:34 +00006599/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00006600/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00006601void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00006602 if (W.getUsed()) return; // only do this once
6603 W.setUsed(true);
6604 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
6605 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00006606 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00006607 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
6608 W.getLocation()));
6609 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00006610 WeakTopLevelDecl.push_back(NewD);
6611 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
6612 // to insert Decl at TU scope, sorry.
6613 DeclContext *SavedContext = CurContext;
6614 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00006615 NewD->setDeclContext(CurContext);
6616 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00006617 PushOnScopeChains(NewD, S);
6618 CurContext = SavedContext;
6619 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00006620 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00006621 }
6622}
6623
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006624void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
6625 // It's valid to "forward-declare" #pragma weak, in which case we
6626 // have to do this.
6627 LoadExternalWeakUndeclaredIdentifiers();
6628 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00006629 NamedDecl *ND = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006630 if (auto *VD = dyn_cast<VarDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006631 if (VD->isExternC())
6632 ND = VD;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006633 if (auto *FD = dyn_cast<FunctionDecl>(D))
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006634 if (FD->isExternC())
6635 ND = FD;
6636 if (ND) {
6637 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00006638 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006639 if (I != WeakUndeclaredIdentifiers.end()) {
6640 WeakInfo W = I->second;
6641 DeclApplyPragmaWeak(S, ND, W);
6642 WeakUndeclaredIdentifiers[Id] = W;
6643 }
6644 }
6645 }
6646 }
6647}
6648
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006649/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
6650/// it, apply them to D. This is a bit tricky because PD can have attributes
6651/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00006652void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006653 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00006654 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00006655 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00006656
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006657 // Walk the declarator structure, applying decl attributes that were in a type
6658 // position to the decl itself. This handles cases like:
6659 // int *__attr__(x)** D;
6660 // when X is a decl attribute.
6661 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
6662 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00006663 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00006664
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006665 // Finally, apply any attributes on the decl itself.
6666 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00006667 ProcessDeclAttributeList(S, D, Attrs);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00006668
6669 // Apply additional attributes specified by '#pragma clang attribute'.
6670 AddPragmaAttributes(S, D);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006671}
John McCall28a6aea2009-11-04 02:18:39 +00006672
John McCall31168b02011-06-15 23:02:42 +00006673/// Is the given declaration allowed to use a forbidden type?
John McCallb61e14e2015-10-27 04:54:50 +00006674/// If so, it'll still be annotated with an attribute that makes it
6675/// illegal to actually use.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006676static bool isForbiddenTypeAllowed(Sema &S, Decl *D,
John McCallb61e14e2015-10-27 04:54:50 +00006677 const DelayedDiagnostic &diag,
John McCallc6af8c62015-10-28 05:03:19 +00006678 UnavailableAttr::ImplicitReason &reason) {
John McCall31168b02011-06-15 23:02:42 +00006679 // Private ivars are always okay. Unfortunately, people don't
6680 // always properly make their ivars private, even in system headers.
6681 // Plus we need to make fields okay, too.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006682 if (!isa<FieldDecl>(D) && !isa<ObjCPropertyDecl>(D) &&
6683 !isa<FunctionDecl>(D))
John McCall31168b02011-06-15 23:02:42 +00006684 return false;
6685
John McCallc6af8c62015-10-28 05:03:19 +00006686 // Silently accept unsupported uses of __weak in both user and system
6687 // declarations when it's been disabled, for ease of integration with
6688 // -fno-objc-arc files. We do have to take some care against attempts
6689 // to define such things; for now, we've only done that for ivars
6690 // and properties.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006691 if ((isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) {
John McCallc6af8c62015-10-28 05:03:19 +00006692 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
6693 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
6694 reason = UnavailableAttr::IR_ForbiddenWeak;
6695 return true;
6696 }
John McCallb61e14e2015-10-27 04:54:50 +00006697 }
6698
John McCallc6af8c62015-10-28 05:03:19 +00006699 // Allow all sorts of things in system headers.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006700 if (S.Context.getSourceManager().isInSystemHeader(D->getLocation())) {
John McCallc6af8c62015-10-28 05:03:19 +00006701 // Currently, all the failures dealt with this way are due to ARC
6702 // restrictions.
6703 reason = UnavailableAttr::IR_ARCForbiddenType;
6704 return true;
John McCallb61e14e2015-10-27 04:54:50 +00006705 }
6706
6707 return false;
John McCall31168b02011-06-15 23:02:42 +00006708}
6709
6710/// Handle a delayed forbidden-type diagnostic.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006711static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &DD,
6712 Decl *D) {
6713 auto Reason = UnavailableAttr::IR_None;
6714 if (D && isForbiddenTypeAllowed(S, D, DD, Reason)) {
6715 assert(Reason && "didn't set reason?");
6716 D->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", Reason, DD.Loc));
John McCall31168b02011-06-15 23:02:42 +00006717 return;
6718 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00006719 if (S.getLangOpts().ObjCAutoRefCount)
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006720 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00006721 // FIXME: we may want to suppress diagnostics for all
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006722 // kind of forbidden type messages on unavailable functions.
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00006723 if (FD->hasAttr<UnavailableAttr>() &&
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006724 DD.getForbiddenTypeDiagnostic() ==
6725 diag::err_arc_array_param_no_ownership) {
6726 DD.Triggered = true;
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00006727 return;
6728 }
6729 }
John McCall31168b02011-06-15 23:02:42 +00006730
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006731 S.Diag(DD.Loc, DD.getForbiddenTypeDiagnostic())
6732 << DD.getForbiddenTypeOperand() << DD.getForbiddenTypeArgument();
6733 DD.Triggered = true;
John McCall31168b02011-06-15 23:02:42 +00006734}
6735
Manman Ren45b1ab12016-05-06 19:57:16 +00006736static const AvailabilityAttr *getAttrForPlatform(ASTContext &Context,
6737 const Decl *D) {
6738 // Check each AvailabilityAttr to find the one for this platform.
6739 for (const auto *A : D->attrs()) {
6740 if (const auto *Avail = dyn_cast<AvailabilityAttr>(A)) {
6741 // FIXME: this is copied from CheckAvailability. We should try to
6742 // de-duplicate.
6743
6744 // Check if this is an App Extension "platform", and if so chop off
6745 // the suffix for matching with the actual platform.
6746 StringRef ActualPlatform = Avail->getPlatform()->getName();
6747 StringRef RealizedPlatform = ActualPlatform;
6748 if (Context.getLangOpts().AppExt) {
6749 size_t suffix = RealizedPlatform.rfind("_app_extension");
6750 if (suffix != StringRef::npos)
6751 RealizedPlatform = RealizedPlatform.slice(0, suffix);
6752 }
6753
6754 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
6755
6756 // Match the platform name.
6757 if (RealizedPlatform == TargetPlatform)
6758 return Avail;
6759 }
6760 }
6761 return nullptr;
6762}
6763
Erik Pilkington9f866a72017-07-18 20:32:07 +00006764/// The diagnostic we should emit for \c D, and the declaration that
6765/// originated it, or \c AR_Available.
6766///
6767/// \param D The declaration to check.
6768/// \param Message If non-null, this will be populated with the message from
6769/// the availability attribute that is selected.
6770static std::pair<AvailabilityResult, const NamedDecl *>
6771ShouldDiagnoseAvailabilityOfDecl(const NamedDecl *D, std::string *Message) {
6772 AvailabilityResult Result = D->getAvailability(Message);
6773
6774 // For typedefs, if the typedef declaration appears available look
6775 // to the underlying type to see if it is more restrictive.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006776 while (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00006777 if (Result == AR_Available) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006778 if (const auto *TT = TD->getUnderlyingType()->getAs<TagType>()) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00006779 D = TT->getDecl();
6780 Result = D->getAvailability(Message);
6781 continue;
6782 }
6783 }
6784 break;
6785 }
6786
6787 // Forward class declarations get their attributes from their definition.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006788 if (const auto *IDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00006789 if (IDecl->getDefinition()) {
6790 D = IDecl->getDefinition();
6791 Result = D->getAvailability(Message);
6792 }
6793 }
6794
6795 if (const auto *ECD = dyn_cast<EnumConstantDecl>(D))
6796 if (Result == AR_Available) {
6797 const DeclContext *DC = ECD->getDeclContext();
6798 if (const auto *TheEnumDecl = dyn_cast<EnumDecl>(DC)) {
6799 Result = TheEnumDecl->getAvailability(Message);
6800 D = TheEnumDecl;
6801 }
6802 }
6803
6804 return {Result, D};
6805}
6806
6807
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00006808/// \brief whether we should emit a diagnostic for \c K and \c DeclVersion in
6809/// the context of \c Ctx. For example, we should emit an unavailable diagnostic
6810/// in a deprecated context, but not the other way around.
6811static bool ShouldDiagnoseAvailabilityInContext(Sema &S, AvailabilityResult K,
6812 VersionTuple DeclVersion,
6813 Decl *Ctx) {
6814 assert(K != AR_Available && "Expected an unavailable declaration here!");
6815
6816 // Checks if we should emit the availability diagnostic in the context of C.
6817 auto CheckContext = [&](const Decl *C) {
6818 if (K == AR_NotYetIntroduced) {
6819 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, C))
6820 if (AA->getIntroduced() >= DeclVersion)
6821 return true;
6822 } else if (K == AR_Deprecated)
6823 if (C->isDeprecated())
6824 return true;
6825
6826 if (C->isUnavailable())
6827 return true;
6828 return false;
6829 };
6830
6831 // FIXME: This is a temporary workaround! Some existing Apple headers depends
6832 // on nested declarations in an @interface having the availability of the
6833 // interface when they really shouldn't: they are members of the enclosing
6834 // context, and can referenced from there.
6835 if (S.OriginalLexicalContext && cast<Decl>(S.OriginalLexicalContext) != Ctx) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006836 const auto *OrigCtx = cast<Decl>(S.OriginalLexicalContext);
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00006837 if (CheckContext(OrigCtx))
6838 return false;
6839
6840 // An implementation implicitly has the availability of the interface.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006841 if (const auto *CatOrImpl = dyn_cast<ObjCImplDecl>(OrigCtx)) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00006842 if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface())
6843 if (CheckContext(Interface))
6844 return false;
6845 }
6846 // A category implicitly has the availability of the interface.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006847 else if (const auto *CatD = dyn_cast<ObjCCategoryDecl>(OrigCtx))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00006848 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
6849 if (CheckContext(Interface))
6850 return false;
6851 }
6852
6853 do {
6854 if (CheckContext(Ctx))
6855 return false;
6856
6857 // An implementation implicitly has the availability of the interface.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006858 if (const auto *CatOrImpl = dyn_cast<ObjCImplDecl>(Ctx)) {
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00006859 if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface())
6860 if (CheckContext(Interface))
6861 return false;
6862 }
6863 // A category implicitly has the availability of the interface.
Aaron Ballmana70c6b52018-02-15 16:20:20 +00006864 else if (const auto *CatD = dyn_cast<ObjCCategoryDecl>(Ctx))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00006865 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
6866 if (CheckContext(Interface))
6867 return false;
6868 } while ((Ctx = cast_or_null<Decl>(Ctx->getDeclContext())));
6869
6870 return true;
6871}
6872
Alex Lorenzc9a369f2017-06-22 17:02:24 +00006873static bool
6874shouldDiagnoseAvailabilityByDefault(const ASTContext &Context,
6875 const VersionTuple &DeploymentVersion,
6876 const VersionTuple &DeclVersion) {
6877 const auto &Triple = Context.getTargetInfo().getTriple();
6878 VersionTuple ForceAvailabilityFromVersion;
6879 switch (Triple.getOS()) {
6880 case llvm::Triple::IOS:
6881 case llvm::Triple::TvOS:
6882 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/11);
6883 break;
6884 case llvm::Triple::WatchOS:
6885 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/4);
6886 break;
6887 case llvm::Triple::Darwin:
6888 case llvm::Triple::MacOSX:
6889 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/10, /*Minor=*/13);
6890 break;
6891 default:
6892 // New targets should always warn about availability.
6893 return Triple.getVendor() == llvm::Triple::Apple;
6894 }
6895 return DeploymentVersion >= ForceAvailabilityFromVersion ||
6896 DeclVersion >= ForceAvailabilityFromVersion;
6897}
6898
Erik Pilkington4042f3c2017-07-05 17:08:56 +00006899static NamedDecl *findEnclosingDeclToAnnotate(Decl *OrigCtx) {
6900 for (Decl *Ctx = OrigCtx; Ctx;
6901 Ctx = cast_or_null<Decl>(Ctx->getDeclContext())) {
6902 if (isa<TagDecl>(Ctx) || isa<FunctionDecl>(Ctx) || isa<ObjCMethodDecl>(Ctx))
6903 return cast<NamedDecl>(Ctx);
6904 if (auto *CD = dyn_cast<ObjCContainerDecl>(Ctx)) {
6905 if (auto *Imp = dyn_cast<ObjCImplDecl>(Ctx))
6906 return Imp->getClassInterface();
6907 return CD;
6908 }
6909 }
6910
6911 return dyn_cast<NamedDecl>(OrigCtx);
6912}
6913
Alex Lorenz727c21e2017-07-26 13:58:02 +00006914namespace {
6915
6916struct AttributeInsertion {
6917 StringRef Prefix;
6918 SourceLocation Loc;
6919 StringRef Suffix;
6920
6921 static AttributeInsertion createInsertionAfter(const NamedDecl *D) {
6922 return {" ", D->getLocEnd(), ""};
6923 }
6924 static AttributeInsertion createInsertionAfter(SourceLocation Loc) {
6925 return {" ", Loc, ""};
6926 }
6927 static AttributeInsertion createInsertionBefore(const NamedDecl *D) {
6928 return {"", D->getLocStart(), "\n"};
6929 }
6930};
6931
6932} // end anonymous namespace
6933
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00006934/// Tries to parse a string as ObjC method name.
6935///
6936/// \param Name The string to parse. Expected to originate from availability
6937/// attribute argument.
6938/// \param SlotNames The vector that will be populated with slot names. In case
6939/// of unsuccessful parsing can contain invalid data.
6940/// \returns A number of method parameters if parsing was successful, None
6941/// otherwise.
6942static Optional<unsigned>
6943tryParseObjCMethodName(StringRef Name, SmallVectorImpl<StringRef> &SlotNames,
6944 const LangOptions &LangOpts) {
6945 // Accept replacements starting with - or + as valid ObjC method names.
6946 if (!Name.empty() && (Name.front() == '-' || Name.front() == '+'))
6947 Name = Name.drop_front(1);
6948 if (Name.empty())
6949 return None;
6950 Name.split(SlotNames, ':');
6951 unsigned NumParams;
6952 if (Name.back() == ':') {
6953 // Remove an empty string at the end that doesn't represent any slot.
6954 SlotNames.pop_back();
6955 NumParams = SlotNames.size();
6956 } else {
6957 if (SlotNames.size() != 1)
6958 // Not a valid method name, just a colon-separated string.
6959 return None;
6960 NumParams = 0;
6961 }
6962 // Verify all slot names are valid.
6963 bool AllowDollar = LangOpts.DollarIdents;
6964 for (StringRef S : SlotNames) {
6965 if (S.empty())
6966 continue;
6967 if (!isValidIdentifier(S, AllowDollar))
6968 return None;
6969 }
6970 return NumParams;
6971}
6972
Alex Lorenz727c21e2017-07-26 13:58:02 +00006973/// Returns a source location in which it's appropriate to insert a new
6974/// attribute for the given declaration \D.
6975static Optional<AttributeInsertion>
6976createAttributeInsertion(const NamedDecl *D, const SourceManager &SM,
6977 const LangOptions &LangOpts) {
6978 if (isa<ObjCPropertyDecl>(D))
6979 return AttributeInsertion::createInsertionAfter(D);
6980 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
6981 if (MD->hasBody())
6982 return None;
6983 return AttributeInsertion::createInsertionAfter(D);
6984 }
6985 if (const auto *TD = dyn_cast<TagDecl>(D)) {
6986 SourceLocation Loc =
6987 Lexer::getLocForEndOfToken(TD->getInnerLocStart(), 0, SM, LangOpts);
6988 if (Loc.isInvalid())
6989 return None;
6990 // Insert after the 'struct'/whatever keyword.
6991 return AttributeInsertion::createInsertionAfter(Loc);
6992 }
6993 return AttributeInsertion::createInsertionBefore(D);
6994}
6995
Erik Pilkington4042f3c2017-07-05 17:08:56 +00006996/// Actually emit an availability diagnostic for a reference to an unavailable
6997/// decl.
6998///
6999/// \param Ctx The context that the reference occurred in
7000/// \param ReferringDecl The exact declaration that was referenced.
7001/// \param OffendingDecl A related decl to \c ReferringDecl that has an
7002/// availability attribute corrisponding to \c K attached to it. Note that this
7003/// may not be the same as ReferringDecl, i.e. if an EnumDecl is annotated and
7004/// we refer to a member EnumConstantDecl, ReferringDecl is the EnumConstantDecl
7005/// and OffendingDecl is the EnumDecl.
Erik Pilkington796a3e22016-08-05 22:59:03 +00007006static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007007 Decl *Ctx, const NamedDecl *ReferringDecl,
7008 const NamedDecl *OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007009 StringRef Message,
7010 ArrayRef<SourceLocation> Locs,
Aaron Ballmanfb237522014-10-15 15:37:51 +00007011 const ObjCInterfaceDecl *UnknownObjCClass,
7012 const ObjCPropertyDecl *ObjCProperty,
7013 bool ObjCPropertyAccess) {
7014 // Diagnostics for deprecated or unavailable.
7015 unsigned diag, diag_message, diag_fwdclass_message;
John McCallb61e14e2015-10-27 04:54:50 +00007016 unsigned diag_available_here = diag::note_availability_specified_here;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007017 SourceLocation NoteLocation = OffendingDecl->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00007018
7019 // Matches 'diag::note_property_attribute' options.
7020 unsigned property_note_select;
7021
7022 // Matches diag::note_availability_specified_here.
7023 unsigned available_here_select_kind;
7024
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007025 VersionTuple DeclVersion;
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007026 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, OffendingDecl))
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007027 DeclVersion = AA->getIntroduced();
7028
7029 if (!ShouldDiagnoseAvailabilityInContext(S, K, DeclVersion, Ctx))
7030 return;
7031
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007032 SourceLocation Loc = Locs.front();
7033
Erik Pilkington8b352c42017-08-14 19:49:12 +00007034 // The declaration can have multiple availability attributes, we are looking
7035 // at one of them.
7036 const AvailabilityAttr *A = getAttrForPlatform(S.Context, OffendingDecl);
7037 if (A && A->isInherited()) {
7038 for (const Decl *Redecl = OffendingDecl->getMostRecentDecl(); Redecl;
7039 Redecl = Redecl->getPreviousDecl()) {
7040 const AvailabilityAttr *AForRedecl =
7041 getAttrForPlatform(S.Context, Redecl);
7042 if (AForRedecl && !AForRedecl->isInherited()) {
7043 // If D is a declaration with inherited attributes, the note should
7044 // point to the declaration with actual attributes.
7045 NoteLocation = Redecl->getLocation();
7046 break;
7047 }
7048 }
7049 }
7050
Aaron Ballmanfb237522014-10-15 15:37:51 +00007051 switch (K) {
Erik Pilkington8b352c42017-08-14 19:49:12 +00007052 case AR_NotYetIntroduced: {
7053 // We would like to emit the diagnostic even if -Wunguarded-availability is
7054 // not specified for deployment targets >= to iOS 11 or equivalent or
7055 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
7056 // later.
7057 const AvailabilityAttr *AA =
7058 getAttrForPlatform(S.getASTContext(), OffendingDecl);
7059 VersionTuple Introduced = AA->getIntroduced();
7060
7061 bool UseNewWarning = shouldDiagnoseAvailabilityByDefault(
7062 S.Context, S.Context.getTargetInfo().getPlatformMinVersion(),
7063 Introduced);
7064 unsigned Warning = UseNewWarning ? diag::warn_unguarded_availability_new
7065 : diag::warn_unguarded_availability;
7066
7067 S.Diag(Loc, Warning)
7068 << OffendingDecl
7069 << AvailabilityAttr::getPrettyPlatformName(
7070 S.getASTContext().getTargetInfo().getPlatformName())
7071 << Introduced.getAsString();
7072
7073 S.Diag(OffendingDecl->getLocation(), diag::note_availability_specified_here)
7074 << OffendingDecl << /* partial */ 3;
7075
7076 if (const auto *Enclosing = findEnclosingDeclToAnnotate(Ctx)) {
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007077 if (const auto *TD = dyn_cast<TagDecl>(Enclosing))
Erik Pilkington8b352c42017-08-14 19:49:12 +00007078 if (TD->getDeclName().isEmpty()) {
7079 S.Diag(TD->getLocation(),
7080 diag::note_decl_unguarded_availability_silence)
7081 << /*Anonymous*/ 1 << TD->getKindName();
7082 return;
7083 }
7084 auto FixitNoteDiag =
7085 S.Diag(Enclosing->getLocation(),
7086 diag::note_decl_unguarded_availability_silence)
7087 << /*Named*/ 0 << Enclosing;
7088 // Don't offer a fixit for declarations with availability attributes.
7089 if (Enclosing->hasAttr<AvailabilityAttr>())
7090 return;
7091 if (!S.getPreprocessor().isMacroDefined("API_AVAILABLE"))
7092 return;
7093 Optional<AttributeInsertion> Insertion = createAttributeInsertion(
7094 Enclosing, S.getSourceManager(), S.getLangOpts());
7095 if (!Insertion)
7096 return;
7097 std::string PlatformName =
7098 AvailabilityAttr::getPlatformNameSourceSpelling(
7099 S.getASTContext().getTargetInfo().getPlatformName())
7100 .lower();
7101 std::string Introduced =
7102 OffendingDecl->getVersionIntroduced().getAsString();
7103 FixitNoteDiag << FixItHint::CreateInsertion(
7104 Insertion->Loc,
7105 (llvm::Twine(Insertion->Prefix) + "API_AVAILABLE(" + PlatformName +
7106 "(" + Introduced + "))" + Insertion->Suffix)
7107 .str());
7108 }
7109 return;
7110 }
Erik Pilkington796a3e22016-08-05 22:59:03 +00007111 case AR_Deprecated:
Aaron Ballmanfb237522014-10-15 15:37:51 +00007112 diag = !ObjCPropertyAccess ? diag::warn_deprecated
7113 : diag::warn_property_method_deprecated;
7114 diag_message = diag::warn_deprecated_message;
7115 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
7116 property_note_select = /* deprecated */ 0;
7117 available_here_select_kind = /* deprecated */ 2;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007118 if (const auto *AL = OffendingDecl->getAttr<DeprecatedAttr>())
7119 NoteLocation = AL->getLocation();
Aaron Ballmanfb237522014-10-15 15:37:51 +00007120 break;
7121
Erik Pilkington796a3e22016-08-05 22:59:03 +00007122 case AR_Unavailable:
Aaron Ballmanfb237522014-10-15 15:37:51 +00007123 diag = !ObjCPropertyAccess ? diag::err_unavailable
7124 : diag::err_property_method_unavailable;
7125 diag_message = diag::err_unavailable_message;
7126 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
7127 property_note_select = /* unavailable */ 1;
7128 available_here_select_kind = /* unavailable */ 0;
John McCallb61e14e2015-10-27 04:54:50 +00007129
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007130 if (auto AL = OffendingDecl->getAttr<UnavailableAttr>()) {
7131 if (AL->isImplicit() && AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00007132 // Most of these failures are due to extra restrictions in ARC;
7133 // reflect that in the primary diagnostic when applicable.
7134 auto flagARCError = [&] {
7135 if (S.getLangOpts().ObjCAutoRefCount &&
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007136 S.getSourceManager().isInSystemHeader(
7137 OffendingDecl->getLocation()))
John McCallc6af8c62015-10-28 05:03:19 +00007138 diag = diag::err_unavailable_in_arc;
7139 };
7140
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007141 switch (AL->getImplicitReason()) {
John McCallc6af8c62015-10-28 05:03:19 +00007142 case UnavailableAttr::IR_None: break;
7143
7144 case UnavailableAttr::IR_ARCForbiddenType:
7145 flagARCError();
7146 diag_available_here = diag::note_arc_forbidden_type;
7147 break;
7148
7149 case UnavailableAttr::IR_ForbiddenWeak:
7150 if (S.getLangOpts().ObjCWeakRuntime)
7151 diag_available_here = diag::note_arc_weak_disabled;
7152 else
7153 diag_available_here = diag::note_arc_weak_no_runtime;
7154 break;
7155
7156 case UnavailableAttr::IR_ARCForbiddenConversion:
7157 flagARCError();
7158 diag_available_here = diag::note_performs_forbidden_arc_conversion;
7159 break;
7160
7161 case UnavailableAttr::IR_ARCInitReturnsUnrelated:
7162 flagARCError();
7163 diag_available_here = diag::note_arc_init_returns_unrelated;
7164 break;
7165
7166 case UnavailableAttr::IR_ARCFieldWithOwnership:
7167 flagARCError();
7168 diag_available_here = diag::note_arc_field_with_ownership;
7169 break;
7170 }
7171 }
John McCallb61e14e2015-10-27 04:54:50 +00007172 }
Aaron Ballmanfb237522014-10-15 15:37:51 +00007173 break;
7174
Erik Pilkington796a3e22016-08-05 22:59:03 +00007175 case AR_Available:
7176 llvm_unreachable("Warning for availability of available declaration?");
Aaron Ballmanfb237522014-10-15 15:37:51 +00007177 }
7178
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007179 SmallVector<FixItHint, 12> FixIts;
Erik Pilkington796a3e22016-08-05 22:59:03 +00007180 if (K == AR_Deprecated) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007181 StringRef Replacement;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007182 if (auto AL = OffendingDecl->getAttr<DeprecatedAttr>())
7183 Replacement = AL->getReplacement();
7184 if (auto AL = getAttrForPlatform(S.Context, OffendingDecl))
7185 Replacement = AL->getReplacement();
Manman Renc7890fe2016-03-16 18:50:49 +00007186
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007187 CharSourceRange UseRange;
Manman Renc7890fe2016-03-16 18:50:49 +00007188 if (!Replacement.empty())
7189 UseRange =
7190 CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc));
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007191 if (UseRange.isValid()) {
7192 if (const auto *MethodDecl = dyn_cast<ObjCMethodDecl>(ReferringDecl)) {
7193 Selector Sel = MethodDecl->getSelector();
7194 SmallVector<StringRef, 12> SelectorSlotNames;
7195 Optional<unsigned> NumParams = tryParseObjCMethodName(
7196 Replacement, SelectorSlotNames, S.getLangOpts());
7197 if (NumParams && NumParams.getValue() == Sel.getNumArgs()) {
7198 assert(SelectorSlotNames.size() == Locs.size());
7199 for (unsigned I = 0; I < Locs.size(); ++I) {
7200 if (!Sel.getNameForSlot(I).empty()) {
7201 CharSourceRange NameRange = CharSourceRange::getCharRange(
7202 Locs[I], S.getLocForEndOfToken(Locs[I]));
7203 FixIts.push_back(FixItHint::CreateReplacement(
7204 NameRange, SelectorSlotNames[I]));
7205 } else
7206 FixIts.push_back(
7207 FixItHint::CreateInsertion(Locs[I], SelectorSlotNames[I]));
7208 }
7209 } else
7210 FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement));
7211 } else
7212 FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement));
7213 }
Manman Renc7890fe2016-03-16 18:50:49 +00007214 }
7215
Aaron Ballmanfb237522014-10-15 15:37:51 +00007216 if (!Message.empty()) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007217 S.Diag(Loc, diag_message) << ReferringDecl << Message << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007218 if (ObjCProperty)
7219 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
7220 << ObjCProperty->getDeclName() << property_note_select;
7221 } else if (!UnknownObjCClass) {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007222 S.Diag(Loc, diag) << ReferringDecl << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007223 if (ObjCProperty)
7224 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
7225 << ObjCProperty->getDeclName() << property_note_select;
7226 } else {
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007227 S.Diag(Loc, diag_fwdclass_message) << ReferringDecl << FixIts;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007228 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
7229 }
7230
Erik Pilkington8b352c42017-08-14 19:49:12 +00007231 S.Diag(NoteLocation, diag_available_here)
7232 << OffendingDecl << available_here_select_kind;
Aaron Ballmanfb237522014-10-15 15:37:51 +00007233}
7234
7235static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
7236 Decl *Ctx) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00007237 assert(DD.Kind == DelayedDiagnostic::Availability &&
7238 "Expected an availability diagnostic here");
7239
Aaron Ballmanfb237522014-10-15 15:37:51 +00007240 DD.Triggered = true;
Nico Weber0055a192015-03-19 19:18:22 +00007241 DoEmitAvailabilityWarning(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007242 S, DD.getAvailabilityResult(), Ctx, DD.getAvailabilityReferringDecl(),
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007243 DD.getAvailabilityOffendingDecl(), DD.getAvailabilityMessage(),
7244 DD.getAvailabilitySelectorLocs(), DD.getUnknownObjCClass(),
7245 DD.getObjCProperty(), false);
Aaron Ballmanfb237522014-10-15 15:37:51 +00007246}
7247
John McCall2ec85372012-05-07 06:16:41 +00007248void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
7249 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00007250 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00007251 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00007252
John McCall2ec85372012-05-07 06:16:41 +00007253 // When delaying diagnostics to run in the context of a parsed
7254 // declaration, we only want to actually emit anything if parsing
7255 // succeeds.
7256 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00007257
John McCall2ec85372012-05-07 06:16:41 +00007258 // We emit all the active diagnostics in this pool or any of its
7259 // parents. In general, we'll get one pool for the decl spec
7260 // and a child pool for each declarator; in a decl group like:
7261 // deprecated_typedef foo, *bar, baz();
7262 // only the declarator pops will be passed decls. This is correct;
7263 // we really do need to consider delayed diagnostics from the decl spec
7264 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00007265 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00007266 do {
John McCall6347b682012-05-07 06:16:58 +00007267 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00007268 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
7269 // This const_cast is a bit lame. Really, Triggered should be mutable.
7270 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00007271 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00007272 continue;
7273
John McCallc1465822011-02-14 07:13:47 +00007274 switch (diag.Kind) {
Erik Pilkingtona8003972016-10-28 21:39:27 +00007275 case DelayedDiagnostic::Availability:
Ted Kremenekb79ee572013-12-18 23:30:06 +00007276 // Don't bother giving deprecation/unavailable diagnostics if
7277 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00007278 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00007279 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00007280 break;
7281
7282 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00007283 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00007284 break;
John McCall31168b02011-06-15 23:02:42 +00007285
7286 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00007287 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00007288 break;
John McCall86121512010-01-27 03:50:35 +00007289 }
7290 }
John McCall2ec85372012-05-07 06:16:41 +00007291 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00007292}
7293
John McCall6347b682012-05-07 06:16:58 +00007294/// Given a set of delayed diagnostics, re-emit them as if they had
7295/// been delayed in the current context instead of in the given pool.
7296/// Essentially, this just moves them to the current pool.
7297void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
7298 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
7299 assert(curPool && "re-emitting in undelayed context not supported");
7300 curPool->steal(pool);
7301}
7302
Erik Pilkington9f866a72017-07-18 20:32:07 +00007303static void EmitAvailabilityWarning(Sema &S, AvailabilityResult AR,
7304 const NamedDecl *ReferringDecl,
7305 const NamedDecl *OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007306 StringRef Message,
7307 ArrayRef<SourceLocation> Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00007308 const ObjCInterfaceDecl *UnknownObjCClass,
7309 const ObjCPropertyDecl *ObjCProperty,
7310 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00007311 // Delay if we're currently parsing a declaration.
Erik Pilkington9f866a72017-07-18 20:32:07 +00007312 if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
7313 S.DelayedDiagnostics.add(
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007314 DelayedDiagnostic::makeAvailability(
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007315 AR, Locs, ReferringDecl, OffendingDecl, UnknownObjCClass,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007316 ObjCProperty, Message, ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00007317 return;
7318 }
7319
Erik Pilkington9f866a72017-07-18 20:32:07 +00007320 Decl *Ctx = cast<Decl>(S.getCurLexicalContext());
7321 DoEmitAvailabilityWarning(S, AR, Ctx, ReferringDecl, OffendingDecl,
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007322 Message, Locs, UnknownObjCClass, ObjCProperty,
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007323 ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00007324}
Erik Pilkington48c7cc92016-07-29 17:37:38 +00007325
Erik Pilkington5cd57172016-08-16 17:44:11 +00007326namespace {
7327
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007328/// Returns true if the given statement can be a body-like child of \p Parent.
7329bool isBodyLikeChildStmt(const Stmt *S, const Stmt *Parent) {
7330 switch (Parent->getStmtClass()) {
7331 case Stmt::IfStmtClass:
7332 return cast<IfStmt>(Parent)->getThen() == S ||
7333 cast<IfStmt>(Parent)->getElse() == S;
7334 case Stmt::WhileStmtClass:
7335 return cast<WhileStmt>(Parent)->getBody() == S;
7336 case Stmt::DoStmtClass:
7337 return cast<DoStmt>(Parent)->getBody() == S;
7338 case Stmt::ForStmtClass:
7339 return cast<ForStmt>(Parent)->getBody() == S;
7340 case Stmt::CXXForRangeStmtClass:
7341 return cast<CXXForRangeStmt>(Parent)->getBody() == S;
7342 case Stmt::ObjCForCollectionStmtClass:
7343 return cast<ObjCForCollectionStmt>(Parent)->getBody() == S;
7344 case Stmt::CaseStmtClass:
7345 case Stmt::DefaultStmtClass:
7346 return cast<SwitchCase>(Parent)->getSubStmt() == S;
7347 default:
7348 return false;
7349 }
7350}
7351
7352class StmtUSEFinder : public RecursiveASTVisitor<StmtUSEFinder> {
7353 const Stmt *Target;
7354
7355public:
7356 bool VisitStmt(Stmt *S) { return S != Target; }
7357
7358 /// Returns true if the given statement is present in the given declaration.
7359 static bool isContained(const Stmt *Target, const Decl *D) {
7360 StmtUSEFinder Visitor;
7361 Visitor.Target = Target;
7362 return !Visitor.TraverseDecl(const_cast<Decl *>(D));
7363 }
7364};
7365
7366/// Traverses the AST and finds the last statement that used a given
7367/// declaration.
7368class LastDeclUSEFinder : public RecursiveASTVisitor<LastDeclUSEFinder> {
7369 const Decl *D;
7370
7371public:
7372 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
7373 if (DRE->getDecl() == D)
7374 return false;
7375 return true;
7376 }
7377
7378 static const Stmt *findLastStmtThatUsesDecl(const Decl *D,
7379 const CompoundStmt *Scope) {
7380 LastDeclUSEFinder Visitor;
7381 Visitor.D = D;
7382 for (auto I = Scope->body_rbegin(), E = Scope->body_rend(); I != E; ++I) {
7383 const Stmt *S = *I;
7384 if (!Visitor.TraverseStmt(const_cast<Stmt *>(S)))
7385 return S;
7386 }
7387 return nullptr;
7388 }
7389};
7390
Erik Pilkington5cd57172016-08-16 17:44:11 +00007391/// \brief This class implements -Wunguarded-availability.
7392///
7393/// This is done with a traversal of the AST of a function that makes reference
7394/// to a partially available declaration. Whenever we encounter an \c if of the
7395/// form: \c if(@available(...)), we use the version from the condition to visit
7396/// the then statement.
7397class DiagnoseUnguardedAvailability
7398 : public RecursiveASTVisitor<DiagnoseUnguardedAvailability> {
7399 typedef RecursiveASTVisitor<DiagnoseUnguardedAvailability> Base;
7400
7401 Sema &SemaRef;
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007402 Decl *Ctx;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007403
7404 /// Stack of potentially nested 'if (@available(...))'s.
7405 SmallVector<VersionTuple, 8> AvailabilityStack;
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007406 SmallVector<const Stmt *, 16> StmtStack;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007407
7408 void DiagnoseDeclAvailability(NamedDecl *D, SourceRange Range);
7409
7410public:
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007411 DiagnoseUnguardedAvailability(Sema &SemaRef, Decl *Ctx)
7412 : SemaRef(SemaRef), Ctx(Ctx) {
7413 AvailabilityStack.push_back(
7414 SemaRef.Context.getTargetInfo().getPlatformMinVersion());
Erik Pilkington5cd57172016-08-16 17:44:11 +00007415 }
7416
Alex Lorenz6f911122017-05-16 13:58:53 +00007417 bool TraverseDecl(Decl *D) {
7418 // Avoid visiting nested functions to prevent duplicate warnings.
7419 if (!D || isa<FunctionDecl>(D))
7420 return true;
7421 return Base::TraverseDecl(D);
7422 }
7423
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007424 bool TraverseStmt(Stmt *S) {
7425 if (!S)
7426 return true;
7427 StmtStack.push_back(S);
7428 bool Result = Base::TraverseStmt(S);
7429 StmtStack.pop_back();
7430 return Result;
7431 }
7432
Erik Pilkington5cd57172016-08-16 17:44:11 +00007433 void IssueDiagnostics(Stmt *S) { TraverseStmt(S); }
7434
7435 bool TraverseIfStmt(IfStmt *If);
7436
Alex Lorenz6f911122017-05-16 13:58:53 +00007437 bool TraverseLambdaExpr(LambdaExpr *E) { return true; }
7438
Erik Pilkingtonba87c622017-08-18 20:20:56 +00007439 // for 'case X:' statements, don't bother looking at the 'X'; it can't lead
7440 // to any useful diagnostics.
7441 bool TraverseCaseStmt(CaseStmt *CS) { return TraverseStmt(CS->getSubStmt()); }
7442
Erik Pilkington6ac77a62017-05-22 15:41:12 +00007443 bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *PRE) {
7444 if (PRE->isClassReceiver())
7445 DiagnoseDeclAvailability(PRE->getClassReceiver(), PRE->getReceiverLocation());
7446 return true;
7447 }
7448
Erik Pilkington5cd57172016-08-16 17:44:11 +00007449 bool VisitObjCMessageExpr(ObjCMessageExpr *Msg) {
7450 if (ObjCMethodDecl *D = Msg->getMethodDecl())
7451 DiagnoseDeclAvailability(
7452 D, SourceRange(Msg->getSelectorStartLoc(), Msg->getLocEnd()));
7453 return true;
7454 }
7455
7456 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
7457 DiagnoseDeclAvailability(DRE->getDecl(),
7458 SourceRange(DRE->getLocStart(), DRE->getLocEnd()));
7459 return true;
7460 }
7461
7462 bool VisitMemberExpr(MemberExpr *ME) {
7463 DiagnoseDeclAvailability(ME->getMemberDecl(),
7464 SourceRange(ME->getLocStart(), ME->getLocEnd()));
7465 return true;
7466 }
7467
Alex Lorenz0a484ba2017-05-24 15:15:29 +00007468 bool VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr *E) {
7469 SemaRef.Diag(E->getLocStart(), diag::warn_at_available_unchecked_use)
7470 << (!SemaRef.getLangOpts().ObjC1);
7471 return true;
7472 }
7473
Erik Pilkington5cd57172016-08-16 17:44:11 +00007474 bool VisitTypeLoc(TypeLoc Ty);
7475};
7476
7477void DiagnoseUnguardedAvailability::DiagnoseDeclAvailability(
7478 NamedDecl *D, SourceRange Range) {
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007479 AvailabilityResult Result;
7480 const NamedDecl *OffendingDecl;
7481 std::tie(Result, OffendingDecl) =
Erik Pilkington9f866a72017-07-18 20:32:07 +00007482 ShouldDiagnoseAvailabilityOfDecl(D, nullptr);
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007483 if (Result != AR_Available) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00007484 // All other diagnostic kinds have already been handled in
7485 // DiagnoseAvailabilityOfDecl.
7486 if (Result != AR_NotYetIntroduced)
7487 return;
7488
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007489 const AvailabilityAttr *AA =
7490 getAttrForPlatform(SemaRef.getASTContext(), OffendingDecl);
Erik Pilkington5cd57172016-08-16 17:44:11 +00007491 VersionTuple Introduced = AA->getIntroduced();
7492
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007493 if (AvailabilityStack.back() >= Introduced)
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007494 return;
7495
7496 // If the context of this function is less available than D, we should not
7497 // emit a diagnostic.
7498 if (!ShouldDiagnoseAvailabilityInContext(SemaRef, Result, Introduced, Ctx))
7499 return;
7500
Alex Lorenzc9a369f2017-06-22 17:02:24 +00007501 // We would like to emit the diagnostic even if -Wunguarded-availability is
7502 // not specified for deployment targets >= to iOS 11 or equivalent or
7503 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
7504 // later.
7505 unsigned DiagKind =
7506 shouldDiagnoseAvailabilityByDefault(
7507 SemaRef.Context,
7508 SemaRef.Context.getTargetInfo().getPlatformMinVersion(), Introduced)
7509 ? diag::warn_unguarded_availability_new
7510 : diag::warn_unguarded_availability;
7511
7512 SemaRef.Diag(Range.getBegin(), DiagKind)
Erik Pilkington5cd57172016-08-16 17:44:11 +00007513 << Range << D
7514 << AvailabilityAttr::getPrettyPlatformName(
7515 SemaRef.getASTContext().getTargetInfo().getPlatformName())
7516 << Introduced.getAsString();
7517
Erik Pilkington4042f3c2017-07-05 17:08:56 +00007518 SemaRef.Diag(OffendingDecl->getLocation(),
7519 diag::note_availability_specified_here)
7520 << OffendingDecl << /* partial */ 3;
Erik Pilkington5cd57172016-08-16 17:44:11 +00007521
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007522 auto FixitDiag =
7523 SemaRef.Diag(Range.getBegin(), diag::note_unguarded_available_silence)
7524 << Range << D
7525 << (SemaRef.getLangOpts().ObjC1 ? /*@available*/ 0
7526 : /*__builtin_available*/ 1);
7527
7528 // Find the statement which should be enclosed in the if @available check.
7529 if (StmtStack.empty())
7530 return;
7531 const Stmt *StmtOfUse = StmtStack.back();
7532 const CompoundStmt *Scope = nullptr;
7533 for (const Stmt *S : llvm::reverse(StmtStack)) {
7534 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
7535 Scope = CS;
7536 break;
7537 }
7538 if (isBodyLikeChildStmt(StmtOfUse, S)) {
7539 // The declaration won't be seen outside of the statement, so we don't
7540 // have to wrap the uses of any declared variables in if (@available).
7541 // Therefore we can avoid setting Scope here.
7542 break;
7543 }
7544 StmtOfUse = S;
7545 }
7546 const Stmt *LastStmtOfUse = nullptr;
7547 if (isa<DeclStmt>(StmtOfUse) && Scope) {
7548 for (const Decl *D : cast<DeclStmt>(StmtOfUse)->decls()) {
7549 if (StmtUSEFinder::isContained(StmtStack.back(), D)) {
7550 LastStmtOfUse = LastDeclUSEFinder::findLastStmtThatUsesDecl(D, Scope);
7551 break;
7552 }
7553 }
7554 }
7555
7556 const SourceManager &SM = SemaRef.getSourceManager();
7557 SourceLocation IfInsertionLoc =
7558 SM.getExpansionLoc(StmtOfUse->getLocStart());
7559 SourceLocation StmtEndLoc =
7560 SM.getExpansionRange(
7561 (LastStmtOfUse ? LastStmtOfUse : StmtOfUse)->getLocEnd())
7562 .second;
7563 if (SM.getFileID(IfInsertionLoc) != SM.getFileID(StmtEndLoc))
7564 return;
7565
7566 StringRef Indentation = Lexer::getIndentationForLine(IfInsertionLoc, SM);
7567 const char *ExtraIndentation = " ";
7568 std::string FixItString;
7569 llvm::raw_string_ostream FixItOS(FixItString);
7570 FixItOS << "if (" << (SemaRef.getLangOpts().ObjC1 ? "@available"
7571 : "__builtin_available")
Alex Lorenze1fb64e2017-05-09 15:34:46 +00007572 << "("
7573 << AvailabilityAttr::getPlatformNameSourceSpelling(
7574 SemaRef.getASTContext().getTargetInfo().getPlatformName())
Alex Lorenz9c5c2bf2017-05-05 16:42:44 +00007575 << " " << Introduced.getAsString() << ", *)) {\n"
7576 << Indentation << ExtraIndentation;
7577 FixitDiag << FixItHint::CreateInsertion(IfInsertionLoc, FixItOS.str());
7578 SourceLocation ElseInsertionLoc = Lexer::findLocationAfterToken(
7579 StmtEndLoc, tok::semi, SM, SemaRef.getLangOpts(),
7580 /*SkipTrailingWhitespaceAndNewLine=*/false);
7581 if (ElseInsertionLoc.isInvalid())
7582 ElseInsertionLoc =
7583 Lexer::getLocForEndOfToken(StmtEndLoc, 0, SM, SemaRef.getLangOpts());
7584 FixItOS.str().clear();
7585 FixItOS << "\n"
7586 << Indentation << "} else {\n"
7587 << Indentation << ExtraIndentation
7588 << "// Fallback on earlier versions\n"
7589 << Indentation << "}";
7590 FixitDiag << FixItHint::CreateInsertion(ElseInsertionLoc, FixItOS.str());
Erik Pilkington5cd57172016-08-16 17:44:11 +00007591 }
7592}
7593
7594bool DiagnoseUnguardedAvailability::VisitTypeLoc(TypeLoc Ty) {
7595 const Type *TyPtr = Ty.getTypePtr();
7596 SourceRange Range{Ty.getBeginLoc(), Ty.getEndLoc()};
7597
Erik Pilkington6ac77a62017-05-22 15:41:12 +00007598 if (Range.isInvalid())
7599 return true;
7600
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007601 if (const auto *TT = dyn_cast<TagType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00007602 TagDecl *TD = TT->getDecl();
7603 DiagnoseDeclAvailability(TD, Range);
7604
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007605 } else if (const auto *TD = dyn_cast<TypedefType>(TyPtr)) {
Erik Pilkington5cd57172016-08-16 17:44:11 +00007606 TypedefNameDecl *D = TD->getDecl();
7607 DiagnoseDeclAvailability(D, Range);
7608
7609 } else if (const auto *ObjCO = dyn_cast<ObjCObjectType>(TyPtr)) {
7610 if (NamedDecl *D = ObjCO->getInterface())
7611 DiagnoseDeclAvailability(D, Range);
7612 }
7613
7614 return true;
7615}
7616
7617bool DiagnoseUnguardedAvailability::TraverseIfStmt(IfStmt *If) {
7618 VersionTuple CondVersion;
7619 if (auto *E = dyn_cast<ObjCAvailabilityCheckExpr>(If->getCond())) {
7620 CondVersion = E->getVersion();
7621
7622 // If we're using the '*' case here or if this check is redundant, then we
7623 // use the enclosing version to check both branches.
7624 if (CondVersion.empty() || CondVersion <= AvailabilityStack.back())
Alex Lorenz98f9fcd2017-08-17 14:22:27 +00007625 return TraverseStmt(If->getThen()) && TraverseStmt(If->getElse());
Erik Pilkington5cd57172016-08-16 17:44:11 +00007626 } else {
7627 // This isn't an availability checking 'if', we can just continue.
7628 return Base::TraverseIfStmt(If);
7629 }
7630
7631 AvailabilityStack.push_back(CondVersion);
7632 bool ShouldContinue = TraverseStmt(If->getThen());
7633 AvailabilityStack.pop_back();
7634
7635 return ShouldContinue && TraverseStmt(If->getElse());
7636}
7637
7638} // end anonymous namespace
7639
7640void Sema::DiagnoseUnguardedAvailabilityViolations(Decl *D) {
7641 Stmt *Body = nullptr;
7642
7643 if (auto *FD = D->getAsFunction()) {
7644 // FIXME: We only examine the pattern decl for availability violations now,
7645 // but we should also examine instantiated templates.
7646 if (FD->isTemplateInstantiation())
7647 return;
7648
7649 Body = FD->getBody();
7650 } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
7651 Body = MD->getBody();
Alex Lorenz28559ce2017-04-26 14:20:02 +00007652 else if (auto *BD = dyn_cast<BlockDecl>(D))
7653 Body = BD->getBody();
Erik Pilkington5cd57172016-08-16 17:44:11 +00007654
7655 assert(Body && "Need a body here!");
7656
Erik Pilkingtonf35114c2016-10-25 19:05:50 +00007657 DiagnoseUnguardedAvailability(*this, D).IssueDiagnostics(Body);
Erik Pilkington5cd57172016-08-16 17:44:11 +00007658}
Erik Pilkington9f866a72017-07-18 20:32:07 +00007659
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007660void Sema::DiagnoseAvailabilityOfDecl(NamedDecl *D,
7661 ArrayRef<SourceLocation> Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00007662 const ObjCInterfaceDecl *UnknownObjCClass,
7663 bool ObjCPropertyAccess,
7664 bool AvoidPartialAvailabilityChecks) {
7665 std::string Message;
7666 AvailabilityResult Result;
7667 const NamedDecl* OffendingDecl;
7668 // See if this declaration is unavailable, deprecated, or partial.
7669 std::tie(Result, OffendingDecl) = ShouldDiagnoseAvailabilityOfDecl(D, &Message);
7670 if (Result == AR_Available)
7671 return;
7672
7673 if (Result == AR_NotYetIntroduced) {
7674 if (AvoidPartialAvailabilityChecks)
7675 return;
7676
7677 // We need to know the @available context in the current function to
7678 // diagnose this use, let DiagnoseUnguardedAvailabilityViolations do that
7679 // when we're done parsing the current function.
7680 if (getCurFunctionOrMethodDecl()) {
7681 getEnclosingFunction()->HasPotentialAvailabilityViolations = true;
7682 return;
7683 } else if (getCurBlock() || getCurLambda()) {
7684 getCurFunction()->HasPotentialAvailabilityViolations = true;
7685 return;
7686 }
7687 }
7688
7689 const ObjCPropertyDecl *ObjCPDecl = nullptr;
Aaron Ballmana70c6b52018-02-15 16:20:20 +00007690 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
Erik Pilkington9f866a72017-07-18 20:32:07 +00007691 if (const ObjCPropertyDecl *PD = MD->findPropertyDecl()) {
7692 AvailabilityResult PDeclResult = PD->getAvailability(nullptr);
7693 if (PDeclResult == Result)
7694 ObjCPDecl = PD;
7695 }
7696 }
7697
Volodymyr Sapsai7d89ce92018-03-29 17:34:09 +00007698 EmitAvailabilityWarning(*this, Result, D, OffendingDecl, Message, Locs,
Erik Pilkington9f866a72017-07-18 20:32:07 +00007699 UnknownObjCClass, ObjCPDecl, ObjCPropertyAccess);
7700}