blob: a5780a7d71fb0ad0ccb5253b9c8a4add5ad3c289 [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
John McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
David Majnemer929025d2016-01-26 19:30:26 +000015#include "clang/AST/ASTConsumer.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.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"
Alex Denisovfde64952015-06-26 05:28:36 +000024#include "clang/AST/ASTMutationListener.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"
Chris Lattner30ba6742009-08-10 19:03:04 +000034#include "llvm/ADT/StringExtras.h"
Hal Finkelee90a222014-09-26 05:04:30 +000035#include "llvm/Support/MathExtras.h"
Eugene Zelenko1ced5092016-02-12 22:53:10 +000036
Chris Lattner2c6fcf52008-06-26 18:38:35 +000037using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000038using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000039
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000040namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000041 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000042 C,
43 Cpp,
44 ObjC
45 };
Eugene Zelenko1ced5092016-02-12 22:53:10 +000046} // end namespace AttributeLangSupport
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000047
Chris Lattner58418ff2008-06-29 00:16:31 +000048//===----------------------------------------------------------------------===//
49// Helper functions
50//===----------------------------------------------------------------------===//
51
Ted Kremenek527042b2009-08-14 20:49:40 +000052/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000053/// type (function or function-typed variable) or an Objective-C
54/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000055static bool isFunctionOrMethod(const Decl *D) {
Craig Topperc3ec1492014-05-26 06:22:03 +000056 return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000057}
Eugene Zelenko1ced5092016-02-12 22:53:10 +000058
David Majnemer06864812015-04-07 06:01:53 +000059/// \brief Return true if the given decl has function type (function or
60/// function-typed variable) or an Objective-C method or a block.
61static bool isFunctionOrMethodOrBlock(const Decl *D) {
62 return isFunctionOrMethod(D) || isa<BlockDecl>(D);
63}
Fariborz Jahanian4447e172009-05-15 23:15:03 +000064
John McCall3882ace2011-01-05 12:14:39 +000065/// Return true if the given decl has a declarator that should have
66/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000067static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000068 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000069 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
70 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +000071}
72
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000073/// hasFunctionProto - Return true if the given decl has a argument
74/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000075/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000076static bool hasFunctionProto(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000077 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000078 return isa<FunctionProtoType>(FnTy);
Aaron Ballman12b9f652014-01-16 13:55:42 +000079 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000080}
81
Alp Toker601b22c2014-01-21 23:35:24 +000082/// getFunctionOrMethodNumParams - Return number of function or method
83/// parameters. It is an error to call this on a K&R function (use
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000084/// hasFunctionProto first).
Alp Toker601b22c2014-01-21 23:35:24 +000085static unsigned getFunctionOrMethodNumParams(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000086 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000087 return cast<FunctionProtoType>(FnTy)->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000088 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000089 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000090 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000091}
92
Alp Toker601b22c2014-01-21 23:35:24 +000093static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000094 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000095 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +000096 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000097 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +000098
Alp Toker03376dc2014-07-07 09:02:20 +000099 return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000100}
101
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000102static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) {
103 if (const auto *FD = dyn_cast<FunctionDecl>(D))
104 return FD->getParamDecl(Idx)->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000105 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000106 return MD->parameters()[Idx]->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000107 if (const auto *BD = dyn_cast<BlockDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000108 return BD->getParamDecl(Idx)->getSourceRange();
109 return SourceRange();
110}
111
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000112static QualType getFunctionOrMethodResultType(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000113 if (const FunctionType *FnTy = D->getFunctionType())
Aaron Ballman6288d062014-07-11 16:31:29 +0000114 return cast<FunctionType>(FnTy)->getReturnType();
Alp Toker314cc812014-01-25 16:55:45 +0000115 return cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000116}
117
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000118static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) {
119 if (const auto *FD = dyn_cast<FunctionDecl>(D))
120 return FD->getReturnTypeSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000121 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000122 return MD->getReturnTypeSourceRange();
123 return SourceRange();
124}
125
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000126static bool isFunctionOrMethodVariadic(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000127 if (const FunctionType *FnTy = D->getFunctionType()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000128 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000129 return proto->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000130 }
Aaron Ballmane13d0092014-08-01 17:02:34 +0000131 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
132 return BD->isVariadic();
133
134 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) {
138 if (const CXXMethodDecl *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) {
John McCall9dd450b2009-09-21 23:43:11 +0000144 const ObjCObjectPointerType *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) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000160 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000161 if (!PT)
162 return false;
163
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000164 const RecordType *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
Richard Smithb87c4652013-10-31 21:23:20 +0000175static unsigned getNumAttributeArgs(const AttributeList &Attr) {
176 // FIXME: Include the type in the argument list.
177 return Attr.getNumArgs() + Attr.hasParsedType();
178}
179
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000180template <typename Compare>
181static bool checkAttributeNumArgsImpl(Sema &S, const AttributeList &Attr,
182 unsigned Num, unsigned Diag,
183 Compare Comp) {
184 if (Comp(getNumAttributeArgs(Attr), Num)) {
185 S.Diag(Attr.getLoc(), Diag) << Attr.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.
194static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
195 unsigned Num) {
196 return checkAttributeNumArgsImpl(S, Attr, Num,
197 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.
203static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000204 unsigned Num) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000205 return checkAttributeNumArgsImpl(S, Attr, Num,
206 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.
212static bool checkAttributeAtMostNumArgs(Sema &S, const AttributeList &Attr,
213 unsigned Num) {
214 return checkAttributeNumArgsImpl(S, Attr, Num,
215 diag::err_attribute_too_many_arguments,
216 std::greater<unsigned>());
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000217}
218
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000219/// \brief If Expr is a valid integer constant, get the value of the integer
220/// expression and return success or failure. May output an error.
221static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
222 const Expr *Expr, uint32_t &Val,
223 unsigned Idx = UINT_MAX) {
224 llvm::APSInt I(32);
225 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
226 !Expr->isIntegerConstantExpr(I, S.Context)) {
227 if (Idx != UINT_MAX)
228 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
229 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
230 << Expr->getSourceRange();
231 else
232 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
233 << Attr.getName() << AANT_ArgumentIntegerConstant
234 << Expr->getSourceRange();
235 return false;
236 }
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000237
238 if (!I.isIntN(32)) {
Aaron Ballman31f42312014-07-24 14:51:23 +0000239 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
240 << I.toString(10, false) << 32 << /* Unsigned */ 1;
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000241 return false;
242 }
243
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000244 Val = (uint32_t)I.getZExtValue();
245 return true;
246}
247
Aaron Ballmanfb763042013-12-02 18:05:46 +0000248/// \brief Diagnose mutually exclusive attributes when present on a given
249/// declaration. Returns true if diagnosed.
250template <typename AttrTy>
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +0000251static bool checkAttrMutualExclusion(Sema &S, Decl *D, SourceRange Range,
252 IdentifierInfo *Ident) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000253 if (AttrTy *A = D->getAttr<AttrTy>()) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +0000254 S.Diag(Range.getBegin(), diag::err_attributes_are_not_compatible) << Ident
255 << A;
256 S.Diag(A->getLocation(), diag::note_conflicting_attribute);
Aaron Ballmanfb763042013-12-02 18:05:46 +0000257 return true;
258 }
259 return false;
260}
261
Alp Toker601b22c2014-01-21 23:35:24 +0000262/// \brief Check if IdxExpr is a valid parameter index for a function or
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000263/// instance method D. May output an error.
264///
265/// \returns true if IdxExpr is a valid index.
Alp Toker601b22c2014-01-21 23:35:24 +0000266static bool checkFunctionOrMethodParameterIndex(Sema &S, const Decl *D,
267 const AttributeList &Attr,
268 unsigned AttrArgNum,
269 const Expr *IdxExpr,
270 uint64_t &Idx) {
David Majnemer06864812015-04-07 06:01:53 +0000271 assert(isFunctionOrMethodOrBlock(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000272
273 // In C++ the implicit 'this' function parameter also counts.
274 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000275 bool HP = hasFunctionProto(D);
276 bool HasImplicitThisParam = isInstanceMethod(D);
277 bool IV = HP && isFunctionOrMethodVariadic(D);
Alp Toker601b22c2014-01-21 23:35:24 +0000278 unsigned NumParams =
279 (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000280
281 llvm::APSInt IdxInt;
282 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
283 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000284 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
285 << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant
286 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000287 return false;
288 }
289
290 Idx = IdxInt.getLimitedValue();
Alp Toker601b22c2014-01-21 23:35:24 +0000291 if (Idx < 1 || (!IV && Idx > NumParams)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000292 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
293 << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000294 return false;
295 }
296 Idx--; // Convert to zero-based.
297 if (HasImplicitThisParam) {
298 if (Idx == 0) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000299 S.Diag(Attr.getLoc(),
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000300 diag::err_attribute_invalid_implicit_this_argument)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000301 << Attr.getName() << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000302 return false;
303 }
304 --Idx;
305 }
306
307 return true;
308}
309
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000310/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
311/// If not emit an error and return false. If the argument is an identifier it
312/// will emit an error with a fixit hint and treat it as if it was a string
313/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000314bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
315 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000316 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000317 // Look for identifiers. If we have one emit a hint to fix it to a literal.
318 if (Attr.isArgIdent(ArgNum)) {
319 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000320 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000321 << Attr.getName() << AANT_ArgumentString
322 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Craig Topper07fa1762015-11-15 02:31:46 +0000323 << FixItHint::CreateInsertion(getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000324 Str = Loc->Ident->getName();
325 if (ArgLocation)
326 *ArgLocation = Loc->Loc;
327 return true;
328 }
329
330 // Now check for an actual string literal.
331 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
332 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
333 if (ArgLocation)
334 *ArgLocation = ArgExpr->getLocStart();
335
336 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000337 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000338 << Attr.getName() << AANT_ArgumentString;
339 return false;
340 }
341
342 Str = Literal->getString();
343 return true;
344}
345
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000346/// \brief Applies the given attribute to the Decl without performing any
347/// additional semantic checking.
348template <typename AttrType>
349static void handleSimpleAttribute(Sema &S, Decl *D,
350 const AttributeList &Attr) {
351 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
352 Attr.getAttributeSpellingListIndex()));
353}
354
Justin Lebar3eaaf862016-01-13 01:07:35 +0000355template <typename AttrType>
356static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
357 const AttributeList &Attr) {
358 handleSimpleAttribute<AttrType>(S, D, Attr);
359}
360
361/// \brief Applies the given attribute to the Decl so long as the Decl doesn't
362/// already have one of the given incompatible attributes.
363template <typename AttrType, typename IncompatibleAttrType,
364 typename... IncompatibleAttrTypes>
365static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
366 const AttributeList &Attr) {
367 if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, Attr.getRange(),
368 Attr.getName()))
369 return;
370 handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
371 Attr);
372}
373
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000374/// \brief Check if the passed-in expression is of type int or bool.
375static bool isIntOrBool(Expr *Exp) {
376 QualType QT = Exp->getType();
377 return QT->isBooleanType() || QT->isIntegerType();
378}
379
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000380
381// Check to see if the type is a smart pointer of some kind. We assume
382// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000383static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
Richard Smithcf4bdde2015-02-21 02:45:19 +0000384 DeclContextLookupResult Res1 = RT->getDecl()->lookup(
385 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000386 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000387 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000388
Richard Smithcf4bdde2015-02-21 02:45:19 +0000389 DeclContextLookupResult Res2 = RT->getDecl()->lookup(
390 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000391 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000392 return false;
393
394 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000395}
396
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000397/// \brief Check if passed in Decl is a pointer type.
398/// Note that this function may produce an error message.
399/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000400static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
401 const AttributeList &Attr) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000402 const ValueDecl *vd = cast<ValueDecl>(D);
403 QualType QT = vd->getType();
404 if (QT->isAnyPointerType())
405 return true;
406
407 if (const RecordType *RT = QT->getAs<RecordType>()) {
408 // If it's an incomplete type, it could be a smart pointer; skip it.
409 // (We don't want to force template instantiation if we can avoid it,
410 // since that would alter the order in which templates are instantiated.)
411 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000412 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000413
Aaron Ballman553e6812013-12-26 14:54:11 +0000414 if (threadSafetyCheckIsSmartPointer(S, RT))
415 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000416 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000417
418 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Aaron Ballman68289452013-12-26 15:06:01 +0000419 << Attr.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000420 return false;
421}
422
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000423/// \brief Checks that the passed in QualType either is of RecordType or points
424/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000425static const RecordType *getRecordType(QualType QT) {
426 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000427 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000428
429 // Now check if we point to record type.
430 if (const PointerType *PT = QT->getAs<PointerType>())
431 return PT->getPointeeType()->getAs<RecordType>();
432
Craig Topperc3ec1492014-05-26 06:22:03 +0000433 return nullptr;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000434}
435
Aaron Ballman76050722014-04-04 15:13:57 +0000436static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000437 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000438
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000439 if (!RT)
440 return false;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000441
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000442 // Don't check for the capability if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000443 if (RT->isIncompleteType())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000444 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000445
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000446 // Allow smart pointers to be used as capability objects.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000447 // FIXME -- Check the type that the smart pointer points to.
448 if (threadSafetyCheckIsSmartPointer(S, RT))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000449 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000450
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000451 // Check if the record itself has a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000452 RecordDecl *RD = RT->getDecl();
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000453 if (RD->hasAttr<CapabilityAttr>())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000454 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000455
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000456 // Else check if any base classes have a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000457 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
458 CXXBasePaths BPaths(false, false);
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000459 if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &) {
460 const auto *Type = BS->getType()->getAs<RecordType>();
461 return Type->getDecl()->hasAttr<CapabilityAttr>();
462 }, BPaths))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000463 return true;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000464 }
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000465 return false;
466}
467
Aaron Ballman76050722014-04-04 15:13:57 +0000468static bool checkTypedefTypeForCapability(QualType Ty) {
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000469 const auto *TD = Ty->getAs<TypedefType>();
470 if (!TD)
471 return false;
472
473 TypedefNameDecl *TN = TD->getDecl();
474 if (!TN)
475 return false;
476
477 return TN->hasAttr<CapabilityAttr>();
478}
479
Aaron Ballman76050722014-04-04 15:13:57 +0000480static bool typeHasCapability(Sema &S, QualType Ty) {
481 if (checkTypedefTypeForCapability(Ty))
482 return true;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000483
Aaron Ballman76050722014-04-04 15:13:57 +0000484 if (checkRecordTypeForCapability(S, Ty))
485 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000486
Aaron Ballman76050722014-04-04 15:13:57 +0000487 return false;
488}
489
490static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
491 // Capability expressions are simple expressions involving the boolean logic
492 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
493 // a DeclRefExpr is found, its type should be checked to determine whether it
494 // is a capability or not.
495
496 if (const auto *E = dyn_cast<DeclRefExpr>(Ex))
497 return typeHasCapability(S, E->getType());
498 else if (const auto *E = dyn_cast<CastExpr>(Ex))
499 return isCapabilityExpr(S, E->getSubExpr());
500 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
501 return isCapabilityExpr(S, E->getSubExpr());
502 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
503 if (E->getOpcode() == UO_LNot)
504 return isCapabilityExpr(S, E->getSubExpr());
505 return false;
506 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
507 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
508 return isCapabilityExpr(S, E->getLHS()) &&
509 isCapabilityExpr(S, E->getRHS());
510 return false;
511 }
512
513 return false;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000514}
515
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000516/// \brief Checks that all attribute arguments, starting from Sidx, resolve to
517/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000518/// \param Sidx The attribute argument index to start checking with.
519/// \param ParamIdxOk Whether an argument can be indexing into a function
520/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000521static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
522 const AttributeList &Attr,
523 SmallVectorImpl<Expr *> &Args,
524 int Sidx = 0,
525 bool ParamIdxOk = false) {
526 for (unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000527 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000528
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000529 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000530 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000531 Args.push_back(ArgExp);
532 continue;
533 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000534
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000535 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000536 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000537 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000538 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000539 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000540 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000541 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000542 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000543
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000544 // We allow constant strings to be used as a placeholder for expressions
545 // that are not valid C++ syntax, but warn that they are ignored.
546 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
547 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000548 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000549 continue;
550 }
551
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000552 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000553
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000554 // A pointer to member expression of the form &MyClass::mu is treated
555 // specially -- we need to look at the type of the member.
556 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
557 if (UOp->getOpcode() == UO_AddrOf)
558 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
559 if (DRE->getDecl()->isCXXInstanceMember())
560 ArgTy = DRE->getDecl()->getType();
561
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000562 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000563 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000564
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000565 // Now check if we index into a record type function param.
566 if(!RT && ParamIdxOk) {
567 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000568 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
569 if(FD && IL) {
570 unsigned int NumParams = FD->getNumParams();
571 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000572 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
573 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
574 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000575 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
576 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000577 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000578 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000579 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000580 }
581 }
582
Aaron Ballman76050722014-04-04 15:13:57 +0000583 // If the type does not have a capability, see if the components of the
584 // expression have capabilities. This allows for writing C code where the
585 // capability may be on the type, and the expression is a capability
586 // boolean logic expression. Eg) requires_capability(A || B && !C)
587 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
588 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
589 << Attr.getName() << ArgTy;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000590
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000591 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000592 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000593}
594
Chris Lattner58418ff2008-06-29 00:16:31 +0000595//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000596// Attribute Implementations
597//===----------------------------------------------------------------------===//
598
Michael Hana9171bc2012-08-03 17:40:43 +0000599static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000600 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000601 if (!threadSafetyCheckIsPointer(S, D, Attr))
602 return;
603
Michael Han99315932013-01-24 16:46:58 +0000604 D->addAttr(::new (S.Context)
605 PtGuardedVarAttr(Attr.getRange(), S.Context,
606 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000607}
608
Michael Hana9171bc2012-08-03 17:40:43 +0000609static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
610 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000611 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000612 SmallVector<Expr*, 1> Args;
613 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000614 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000615 unsigned Size = Args.size();
616 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000617 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000618
Michael Han3be3b442012-07-23 18:48:41 +0000619 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000620
Michael Han3be3b442012-07-23 18:48:41 +0000621 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000622}
623
Michael Han3be3b442012-07-23 18:48:41 +0000624static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000625 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000626 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
627 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000628
Aaron Ballman36a53502014-01-16 13:03:14 +0000629 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg,
630 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000631}
632
Michael Hana9171bc2012-08-03 17:40:43 +0000633static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000634 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000635 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000636 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
637 return;
638
639 if (!threadSafetyCheckIsPointer(S, D, Attr))
640 return;
641
642 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +0000643 S.Context, Arg,
644 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000645}
646
Michael Hana9171bc2012-08-03 17:40:43 +0000647static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
648 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000649 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000650 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000651 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000652
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000653 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000654 QualType QT = cast<ValueDecl>(D)->getType();
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000655 if (!QT->isDependentType() && !typeHasCapability(S, QT)) {
656 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
657 << Attr.getName();
658 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000659 }
660
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000661 // Check that all arguments are lockable objects.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000662 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000663 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000664 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000665
Michael Han3be3b442012-07-23 18:48:41 +0000666 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000667}
668
Michael Hana9171bc2012-08-03 17:40:43 +0000669static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000670 const AttributeList &Attr) {
671 SmallVector<Expr*, 1> Args;
672 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
673 return;
674
675 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000676 D->addAttr(::new (S.Context)
677 AcquiredAfterAttr(Attr.getRange(), S.Context,
678 StartArg, Args.size(),
679 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000680}
681
Michael Hana9171bc2012-08-03 17:40:43 +0000682static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000683 const AttributeList &Attr) {
684 SmallVector<Expr*, 1> Args;
685 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
686 return;
687
688 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000689 D->addAttr(::new (S.Context)
690 AcquiredBeforeAttr(Attr.getRange(), S.Context,
691 StartArg, Args.size(),
692 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000693}
694
Michael Hana9171bc2012-08-03 17:40:43 +0000695static bool checkLockFunAttrCommon(Sema &S, Decl *D,
696 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000697 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000698 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000699 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000700 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000701
Michael Han3be3b442012-07-23 18:48:41 +0000702 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000703}
704
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000705static void handleAssertSharedLockAttr(Sema &S, Decl *D,
706 const AttributeList &Attr) {
707 SmallVector<Expr*, 1> Args;
708 if (!checkLockFunAttrCommon(S, D, Attr, Args))
709 return;
710
711 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000712 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000713 D->addAttr(::new (S.Context)
714 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
715 Attr.getAttributeSpellingListIndex()));
716}
717
718static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
719 const AttributeList &Attr) {
720 SmallVector<Expr*, 1> Args;
721 if (!checkLockFunAttrCommon(S, D, Attr, Args))
722 return;
723
724 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000725 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000726 D->addAttr(::new (S.Context)
727 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
728 StartArg, Size,
729 Attr.getAttributeSpellingListIndex()));
730}
731
732
Michael Hana9171bc2012-08-03 17:40:43 +0000733static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
734 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000735 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000736 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000737 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000738
Aaron Ballman00e99962013-08-31 01:11:41 +0000739 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000740 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000741 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000742 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000743 }
744
745 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000746 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000747
Michael Han3be3b442012-07-23 18:48:41 +0000748 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000749}
750
Michael Hana9171bc2012-08-03 17:40:43 +0000751static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000752 const AttributeList &Attr) {
753 SmallVector<Expr*, 2> Args;
754 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
755 return;
756
Michael Han99315932013-01-24 16:46:58 +0000757 D->addAttr(::new (S.Context)
758 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000759 Attr.getArgAsExpr(0),
760 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000761 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000762}
763
Michael Hana9171bc2012-08-03 17:40:43 +0000764static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000765 const AttributeList &Attr) {
766 SmallVector<Expr*, 2> Args;
767 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
768 return;
769
Nico Weber462fd1e2015-01-07 23:50:05 +0000770 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(
771 Attr.getRange(), S.Context, Attr.getArgAsExpr(0), Args.data(),
772 Args.size(), Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000773}
774
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000775static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000776 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000777 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000778 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000779 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000780 unsigned Size = Args.size();
781 if (Size == 0)
782 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000783
Michael Han99315932013-01-24 16:46:58 +0000784 D->addAttr(::new (S.Context)
785 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
786 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000787}
788
789static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000790 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000791 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000792 return;
793
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000794 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000795 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000796 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000797 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000798 if (Size == 0)
799 return;
800 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000801
Michael Han99315932013-01-24 16:46:58 +0000802 D->addAttr(::new (S.Context)
803 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
804 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000805}
806
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000807static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Saleem Abdulrasool046ba5b2016-02-18 06:49:31 +0000808 S.Diag(Attr.getLoc(), diag::ext_clang_enable_if);
809
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000810 Expr *Cond = Attr.getArgAsExpr(0);
811 if (!Cond->isTypeDependent()) {
812 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
813 if (Converted.isInvalid())
814 return;
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000815 Cond = Converted.get();
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000816 }
817
818 StringRef Msg;
819 if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg))
820 return;
821
822 SmallVector<PartialDiagnosticAt, 8> Diags;
823 if (!Cond->isValueDependent() &&
824 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
825 Diags)) {
826 S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr);
827 for (int I = 0, N = Diags.size(); I != N; ++I)
828 S.Diag(Diags[I].first, Diags[I].second);
829 return;
830 }
831
832 D->addAttr(::new (S.Context)
833 EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg,
834 Attr.getAttributeSpellingListIndex()));
835}
836
George Burgess IV3e3bb95b2015-12-02 21:58:08 +0000837static void handlePassObjectSizeAttr(Sema &S, Decl *D,
838 const AttributeList &Attr) {
839 if (D->hasAttr<PassObjectSizeAttr>()) {
840 S.Diag(D->getLocStart(), diag::err_attribute_only_once_per_parameter)
841 << Attr.getName();
842 return;
843 }
844
845 Expr *E = Attr.getArgAsExpr(0);
846 uint32_t Type;
847 if (!checkUInt32Argument(S, Attr, E, Type, /*Idx=*/1))
848 return;
849
850 // pass_object_size's argument is passed in as the second argument of
851 // __builtin_object_size. So, it has the same constraints as that second
852 // argument; namely, it must be in the range [0, 3].
853 if (Type > 3) {
854 S.Diag(E->getLocStart(), diag::err_attribute_argument_outof_range)
855 << Attr.getName() << 0 << 3 << E->getSourceRange();
856 return;
857 }
858
859 // pass_object_size is only supported on constant pointer parameters; as a
860 // kindness to users, we allow the parameter to be non-const for declarations.
861 // At this point, we have no clue if `D` belongs to a function declaration or
862 // definition, so we defer the constness check until later.
863 if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) {
864 S.Diag(D->getLocStart(), diag::err_attribute_pointers_only)
865 << Attr.getName() << 1;
866 return;
867 }
868
869 D->addAttr(::new (S.Context)
870 PassObjectSizeAttr(Attr.getRange(), S.Context, (int)Type,
871 Attr.getAttributeSpellingListIndex()));
872}
873
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000874static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000875 ConsumableAttr::ConsumedState DefaultState;
876
877 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000878 IdentifierLoc *IL = Attr.getArgAsIdent(0);
879 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
880 DefaultState)) {
881 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
882 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000883 return;
884 }
David Blaikie16f76d22013-09-06 01:28:43 +0000885 } else {
886 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
887 << Attr.getName() << AANT_ArgumentIdentifier;
888 return;
889 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000890
891 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000892 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000893 Attr.getAttributeSpellingListIndex()));
894}
895
896static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
897 const AttributeList &Attr) {
898 ASTContext &CurrContext = S.getASTContext();
899 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
900
901 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
902 if (!RD->hasAttr<ConsumableAttr>()) {
903 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
904 RD->getNameAsString();
905
906 return false;
907 }
908 }
909
910 return true;
911}
912
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000913static void handleCallableWhenAttr(Sema &S, Decl *D,
914 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000915 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
916 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000917
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000918 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
919 return;
920
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000921 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
922 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
923 CallableWhenAttr::ConsumedState CallableState;
924
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000925 StringRef StateString;
926 SourceLocation Loc;
Aaron Ballman55ef1512014-12-19 16:42:04 +0000927 if (Attr.isArgIdent(ArgIndex)) {
928 IdentifierLoc *Ident = Attr.getArgAsIdent(ArgIndex);
929 StateString = Ident->Ident->getName();
930 Loc = Ident->Loc;
931 } else {
932 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
933 return;
934 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000935
936 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000937 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000938 S.Diag(Loc, diag::warn_attribute_type_not_supported)
939 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000940 return;
941 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000942
943 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000944 }
945
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000946 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000947 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
948 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000949}
950
DeLesley Hutchins69391772013-10-17 23:23:53 +0000951static void handleParamTypestateAttr(Sema &S, Decl *D,
952 const AttributeList &Attr) {
DeLesley Hutchins69391772013-10-17 23:23:53 +0000953 ParamTypestateAttr::ConsumedState ParamState;
954
955 if (Attr.isArgIdent(0)) {
956 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
957 StringRef StateString = Ident->Ident->getName();
958
959 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
960 ParamState)) {
961 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
962 << Attr.getName() << StateString;
963 return;
964 }
965 } else {
966 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
967 Attr.getName() << AANT_ArgumentIdentifier;
968 return;
969 }
970
971 // FIXME: This check is currently being done in the analysis. It can be
972 // enabled here only after the parser propagates attributes at
973 // template specialization definition, not declaration.
974 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
975 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
976 //
977 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
978 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
979 // ReturnType.getAsString();
980 // return;
981 //}
982
983 D->addAttr(::new (S.Context)
984 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
985 Attr.getAttributeSpellingListIndex()));
986}
987
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000988static void handleReturnTypestateAttr(Sema &S, Decl *D,
989 const AttributeList &Attr) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000990 ReturnTypestateAttr::ConsumedState ReturnState;
991
992 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000993 IdentifierLoc *IL = Attr.getArgAsIdent(0);
994 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
995 ReturnState)) {
996 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
997 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000998 return;
999 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001000 } else {
1001 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1002 Attr.getName() << AANT_ArgumentIdentifier;
1003 return;
1004 }
1005
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001006 // FIXME: This check is currently being done in the analysis. It can be
1007 // enabled here only after the parser propagates attributes at
1008 // template specialization definition, not declaration.
1009 //QualType ReturnType;
1010 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001011 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1012 // ReturnType = Param->getType();
1013 //
1014 //} else if (const CXXConstructorDecl *Constructor =
1015 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001016 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1017 //
1018 //} else {
1019 //
1020 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1021 //}
1022 //
1023 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1024 //
1025 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1026 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1027 // ReturnType.getAsString();
1028 // return;
1029 //}
1030
1031 D->addAttr(::new (S.Context)
1032 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1033 Attr.getAttributeSpellingListIndex()));
1034}
1035
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001036static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001037 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1038 return;
1039
1040 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001041 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001042 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1043 StringRef Param = Ident->Ident->getName();
1044 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1045 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1046 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001047 return;
1048 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001049 } else {
1050 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1051 Attr.getName() << AANT_ArgumentIdentifier;
1052 return;
1053 }
1054
1055 D->addAttr(::new (S.Context)
1056 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1057 Attr.getAttributeSpellingListIndex()));
1058}
1059
Chris Wailes9385f9f2013-10-29 20:28:41 +00001060static void handleTestTypestateAttr(Sema &S, Decl *D,
1061 const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001062 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1063 return;
1064
Chris Wailes9385f9f2013-10-29 20:28:41 +00001065 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001066 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001067 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1068 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001069 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001070 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1071 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001072 return;
1073 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001074 } else {
1075 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1076 Attr.getName() << AANT_ArgumentIdentifier;
1077 return;
1078 }
1079
1080 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001081 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001082 Attr.getAttributeSpellingListIndex()));
1083}
1084
Chandler Carruthedc2c642011-07-02 00:01:44 +00001085static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1086 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001087 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001088 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001089}
1090
Chandler Carruthedc2c642011-07-02 00:01:44 +00001091static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001092 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Aaron Ballman36a53502014-01-16 13:03:14 +00001093 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context,
1094 Attr.getAttributeSpellingListIndex()));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001095 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001096 // Report warning about changed offset in the newer compiler versions.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001097 if (!FD->getType()->isDependentType() &&
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001098 !FD->getType()->isIncompleteType() && FD->isBitField() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001099 S.Context.getTypeAlign(FD->getType()) <= 8)
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001100 S.Diag(Attr.getLoc(), diag::warn_attribute_packed_for_bitfield);
1101
1102 FD->addAttr(::new (S.Context) PackedAttr(
1103 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001104 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001105 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001106}
1107
Ted Kremenek7fd17232011-09-29 07:02:25 +00001108static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1109 // The IBOutlet/IBOutletCollection attributes only apply to instance
1110 // variables or properties of Objective-C classes. The outlet must also
1111 // have an object reference type.
1112 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1113 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001114 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001115 << Attr.getName() << VD->getType() << 0;
1116 return false;
1117 }
1118 }
1119 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1120 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001121 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001122 << Attr.getName() << PD->getType() << 1;
1123 return false;
1124 }
1125 }
1126 else {
1127 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1128 return false;
1129 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001130
Ted Kremenek7fd17232011-09-29 07:02:25 +00001131 return true;
1132}
1133
Chandler Carruthedc2c642011-07-02 00:01:44 +00001134static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001135 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001136 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001137
Michael Han99315932013-01-24 16:46:58 +00001138 D->addAttr(::new (S.Context)
1139 IBOutletAttr(Attr.getRange(), S.Context,
1140 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001141}
1142
Chandler Carruthedc2c642011-07-02 00:01:44 +00001143static void handleIBOutletCollection(Sema &S, Decl *D,
1144 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001145
1146 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001147 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001148 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1149 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001150 return;
1151 }
1152
Ted Kremenek7fd17232011-09-29 07:02:25 +00001153 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001154 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001155
Richard Smithb1f9a282013-10-31 01:56:18 +00001156 ParsedType PT;
1157
1158 if (Attr.hasParsedType())
1159 PT = Attr.getTypeArg();
1160 else {
1161 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1162 S.getScopeForContext(D->getDeclContext()->getParent()));
1163 if (!PT) {
1164 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1165 return;
1166 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001167 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001168
Craig Topperc3ec1492014-05-26 06:22:03 +00001169 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001170 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1171 if (!QTLoc)
1172 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001173
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001174 // Diagnose use of non-object type in iboutletcollection attribute.
1175 // FIXME. Gnu attribute extension ignores use of builtin types in
1176 // attributes. So, __attribute__((iboutletcollection(char))) will be
1177 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001178 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001179 S.Diag(Attr.getLoc(),
1180 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1181 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001182 return;
1183 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001184
Michael Han99315932013-01-24 16:46:58 +00001185 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001186 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001187 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001188}
1189
Hal Finkelee90a222014-09-26 05:04:30 +00001190bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1191 if (RefOkay) {
1192 if (T->isReferenceType())
1193 return true;
1194 } else {
1195 T = T.getNonReferenceType();
1196 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001197
Hal Finkelee90a222014-09-26 05:04:30 +00001198 // The nonnull attribute, and other similar attributes, can be applied to a
1199 // transparent union that contains a pointer type.
Richard Smith588bd9b2014-08-27 04:59:42 +00001200 if (const RecordType *UT = T->getAsUnionType()) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001201 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1202 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001203 for (const auto *I : UD->fields()) {
1204 QualType QT = I->getType();
Richard Smith588bd9b2014-08-27 04:59:42 +00001205 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1206 return true;
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001207 }
1208 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001209 }
1210
1211 return T->isAnyPointerType() || T->isBlockPointerType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001212}
1213
Ted Kremenek9aedc152014-01-17 06:24:56 +00001214static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001215 SourceRange AttrParmRange,
Hal Finkelee90a222014-09-26 05:04:30 +00001216 SourceRange TypeRange,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001217 bool isReturnValue = false) {
Hal Finkelee90a222014-09-26 05:04:30 +00001218 if (!S.isValidPointerAttrType(T)) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001219 if (isReturnValue)
1220 S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only)
1221 << Attr.getName() << AttrParmRange << TypeRange;
1222 else
1223 S.Diag(Attr.getLoc(), diag::warn_attribute_pointers_only)
1224 << Attr.getName() << AttrParmRange << TypeRange << 0;
Ted Kremenek9aedc152014-01-17 06:24:56 +00001225 return false;
1226 }
1227 return true;
1228}
1229
Chandler Carruthedc2c642011-07-02 00:01:44 +00001230static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001231 SmallVector<unsigned, 8> NonNullArgs;
Richard Smith588bd9b2014-08-27 04:59:42 +00001232 for (unsigned I = 0; I < Attr.getNumArgs(); ++I) {
1233 Expr *Ex = Attr.getArgAsExpr(I);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001234 uint64_t Idx;
Richard Smith588bd9b2014-08-27 04:59:42 +00001235 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, I + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001236 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001237
1238 // Is the function argument a pointer type?
Richard Smith588bd9b2014-08-27 04:59:42 +00001239 if (Idx < getFunctionOrMethodNumParams(D) &&
1240 !attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001241 Ex->getSourceRange(),
1242 getFunctionOrMethodParamRange(D, Idx)))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001243 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001244
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001245 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001246 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001247
1248 // If no arguments were specified to __attribute__((nonnull)) then all pointer
Richard Smith588bd9b2014-08-27 04:59:42 +00001249 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1250 // check if the attribute came from a macro expansion or a template
1251 // instantiation.
1252 if (NonNullArgs.empty() && Attr.getLoc().isFileID() &&
1253 S.ActiveTemplateInstantiations.empty()) {
1254 bool AnyPointers = isFunctionOrMethodVariadic(D);
1255 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1256 I != E && !AnyPointers; ++I) {
1257 QualType T = getFunctionOrMethodParamType(D, I);
Hal Finkelee90a222014-09-26 05:04:30 +00001258 if (T->isDependentType() || S.isValidPointerAttrType(T))
Richard Smith588bd9b2014-08-27 04:59:42 +00001259 AnyPointers = true;
Ted Kremenek5fa50522008-11-18 06:52:58 +00001260 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001261
Richard Smith588bd9b2014-08-27 04:59:42 +00001262 if (!AnyPointers)
1263 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001264 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001265
Richard Smith588bd9b2014-08-27 04:59:42 +00001266 unsigned *Start = NonNullArgs.data();
1267 unsigned Size = NonNullArgs.size();
1268 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001269 D->addAttr(::new (S.Context)
Richard Smith588bd9b2014-08-27 04:59:42 +00001270 NonNullAttr(Attr.getRange(), S.Context, Start, Size,
Michael Han99315932013-01-24 16:46:58 +00001271 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001272}
1273
Jordan Rosec9399072014-02-11 17:27:59 +00001274static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
1275 const AttributeList &Attr) {
1276 if (Attr.getNumArgs() > 0) {
1277 if (D->getFunctionType()) {
1278 handleNonNullAttr(S, D, Attr);
1279 } else {
1280 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1281 << D->getSourceRange();
1282 }
1283 return;
1284 }
1285
1286 // Is the argument a pointer type?
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001287 if (!attrNonNullArgCheck(S, D->getType(), Attr, SourceRange(),
1288 D->getSourceRange()))
Jordan Rosec9399072014-02-11 17:27:59 +00001289 return;
1290
1291 D->addAttr(::new (S.Context)
Craig Topperc3ec1492014-05-26 06:22:03 +00001292 NonNullAttr(Attr.getRange(), S.Context, nullptr, 0,
Jordan Rosec9399072014-02-11 17:27:59 +00001293 Attr.getAttributeSpellingListIndex()));
1294}
1295
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001296static void handleReturnsNonNullAttr(Sema &S, Decl *D,
1297 const AttributeList &Attr) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001298 QualType ResultType = getFunctionOrMethodResultType(D);
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001299 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1300 if (!attrNonNullArgCheck(S, ResultType, Attr, SourceRange(), SR,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001301 /* isReturnValue */ true))
1302 return;
1303
1304 D->addAttr(::new (S.Context)
1305 ReturnsNonNullAttr(Attr.getRange(), S.Context,
1306 Attr.getAttributeSpellingListIndex()));
1307}
1308
Hal Finkelee90a222014-09-26 05:04:30 +00001309static void handleAssumeAlignedAttr(Sema &S, Decl *D,
1310 const AttributeList &Attr) {
1311 Expr *E = Attr.getArgAsExpr(0),
1312 *OE = Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr;
1313 S.AddAssumeAlignedAttr(Attr.getRange(), D, E, OE,
1314 Attr.getAttributeSpellingListIndex());
1315}
1316
1317void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
1318 Expr *OE, unsigned SpellingListIndex) {
1319 QualType ResultType = getFunctionOrMethodResultType(D);
1320 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1321
1322 AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex);
1323 SourceLocation AttrLoc = AttrRange.getBegin();
1324
1325 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1326 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1327 << &TmpAttr << AttrRange << SR;
1328 return;
1329 }
1330
1331 if (!E->isValueDependent()) {
1332 llvm::APSInt I(64);
1333 if (!E->isIntegerConstantExpr(I, Context)) {
1334 if (OE)
1335 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1336 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1337 << E->getSourceRange();
1338 else
1339 Diag(AttrLoc, diag::err_attribute_argument_type)
1340 << &TmpAttr << AANT_ArgumentIntegerConstant
1341 << E->getSourceRange();
1342 return;
1343 }
1344
1345 if (!I.isPowerOf2()) {
1346 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1347 << E->getSourceRange();
1348 return;
1349 }
1350 }
1351
1352 if (OE) {
1353 if (!OE->isValueDependent()) {
1354 llvm::APSInt I(64);
1355 if (!OE->isIntegerConstantExpr(I, Context)) {
1356 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1357 << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1358 << OE->getSourceRange();
1359 return;
1360 }
1361 }
1362 }
1363
1364 D->addAttr(::new (Context)
1365 AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex));
1366}
1367
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001368/// Normalize the attribute, __foo__ becomes foo.
1369/// Returns true if normalization was applied.
1370static bool normalizeName(StringRef &AttrName) {
Aaron Ballman62692362015-10-09 13:53:24 +00001371 if (AttrName.size() > 4 && AttrName.startswith("__") &&
1372 AttrName.endswith("__")) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001373 AttrName = AttrName.drop_front(2).drop_back(2);
1374 return true;
1375 }
1376 return false;
1377}
1378
Chandler Carruthedc2c642011-07-02 00:01:44 +00001379static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001380 // This attribute must be applied to a function declaration. The first
1381 // argument to the attribute must be an identifier, the name of the resource,
1382 // for example: malloc. The following arguments must be argument indexes, the
1383 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001384 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001385 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001386 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001387
Aaron Ballman00e99962013-08-31 01:11:41 +00001388 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001389 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001390 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001391 return;
1392 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001393
Richard Smith852e9ce2013-11-27 01:46:48 +00001394 // Figure out our Kind.
1395 OwnershipAttr::OwnershipKind K =
Craig Topperc3ec1492014-05-26 06:22:03 +00001396 OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0,
Richard Smith852e9ce2013-11-27 01:46:48 +00001397 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001398
Richard Smith852e9ce2013-11-27 01:46:48 +00001399 // Check arguments.
1400 switch (K) {
1401 case OwnershipAttr::Takes:
1402 case OwnershipAttr::Holds:
1403 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001404 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1405 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001406 return;
1407 }
1408 break;
1409 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001410 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001411 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1412 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001413 return;
1414 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001415 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001416 }
1417
Richard Smith852e9ce2013-11-27 01:46:48 +00001418 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001419
Richard Smith852e9ce2013-11-27 01:46:48 +00001420 StringRef ModuleName = Module->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001421 if (normalizeName(ModuleName)) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001422 Module = &S.PP.getIdentifierTable().get(ModuleName);
1423 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001424
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001425 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001426 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1427 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001428 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001429 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001430 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001431
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001432 // Is the function argument a pointer type?
Alp Toker601b22c2014-01-21 23:35:24 +00001433 QualType T = getFunctionOrMethodParamType(D, Idx);
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001434 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001435 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001436 case OwnershipAttr::Takes:
1437 case OwnershipAttr::Holds:
1438 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1439 Err = 0;
1440 break;
1441 case OwnershipAttr::Returns:
1442 if (!T->isIntegerType())
1443 Err = 1;
1444 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001445 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001446 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001447 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001448 << Ex->getSourceRange();
1449 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001450 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001451
1452 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001453 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001454 // Cannot have two ownership attributes of different kinds for the same
1455 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001456 if (I->getOwnKind() != K && I->args_end() !=
1457 std::find(I->args_begin(), I->args_end(), Idx)) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001458 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001459 << AL.getName() << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001460 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001461 } else if (K == OwnershipAttr::Returns &&
1462 I->getOwnKind() == OwnershipAttr::Returns) {
1463 // A returns attribute conflicts with any other returns attribute using
1464 // a different index. Note, diagnostic reporting is 1-based, but stored
1465 // argument indexes are 0-based.
1466 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1467 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
1468 << *(I->args_begin()) + 1;
1469 if (I->args_size())
1470 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
1471 << (unsigned)Idx + 1 << Ex->getSourceRange();
1472 return;
1473 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001474 }
1475 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001476 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001477 }
1478
1479 unsigned* start = OwnershipArgs.data();
1480 unsigned size = OwnershipArgs.size();
1481 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001482
Michael Han99315932013-01-24 16:46:58 +00001483 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001484 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001485 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001486}
1487
Chandler Carruthedc2c642011-07-02 00:01:44 +00001488static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001489 // Check the attribute arguments.
1490 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001491 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1492 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001493 return;
1494 }
1495
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001496 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001497
Rafael Espindolac18086a2010-02-23 22:00:30 +00001498 // gcc rejects
1499 // class c {
1500 // static int a __attribute__((weakref ("v2")));
1501 // static int b() __attribute__((weakref ("f3")));
1502 // };
1503 // and ignores the attributes of
1504 // void f(void) {
1505 // static int a __attribute__((weakref ("v2")));
1506 // }
1507 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001508 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001509 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001510 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1511 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001512 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001513 }
1514
1515 // The GCC manual says
1516 //
1517 // At present, a declaration to which `weakref' is attached can only
1518 // be `static'.
1519 //
1520 // It also says
1521 //
1522 // Without a TARGET,
1523 // given as an argument to `weakref' or to `alias', `weakref' is
1524 // equivalent to `weak'.
1525 //
1526 // gcc 4.4.1 will accept
1527 // int a7 __attribute__((weakref));
1528 // as
1529 // int a7 __attribute__((weak));
1530 // This looks like a bug in gcc. We reject that for now. We should revisit
1531 // it if this behaviour is actually used.
1532
Rafael Espindolac18086a2010-02-23 22:00:30 +00001533 // GCC rejects
1534 // static ((alias ("y"), weakref)).
1535 // Should we? How to check that weakref is before or after alias?
1536
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001537 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1538 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1539 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001540 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001541 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001542 // GCC will accept anything as the argument of weakref. Should we
1543 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001544 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1545 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001546
Michael Han99315932013-01-24 16:46:58 +00001547 D->addAttr(::new (S.Context)
1548 WeakRefAttr(Attr.getRange(), S.Context,
1549 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001550}
1551
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001552static void handleIFuncAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1553 StringRef Str;
1554 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
1555 return;
1556
1557 // Aliases should be on declarations, not definitions.
1558 const auto *FD = cast<FunctionDecl>(D);
1559 if (FD->isThisDeclarationADefinition()) {
1560 S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << FD << 1;
1561 return;
1562 }
1563 // FIXME: it should be handled as a target specific attribute.
1564 if (S.Context.getTargetInfo().getTriple().getObjectFormat() !=
1565 llvm::Triple::ELF) {
1566 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1567 return;
1568 }
1569
1570 D->addAttr(::new (S.Context) IFuncAttr(Attr.getRange(), S.Context, Str,
1571 Attr.getAttributeSpellingListIndex()));
1572}
1573
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001574static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1575 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001576 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001577 return;
1578
Douglas Gregore8bbc122011-09-02 00:18:52 +00001579 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001580 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1581 return;
1582 }
Justin Lebara8f0254b2016-01-23 21:28:10 +00001583 if (S.Context.getTargetInfo().getTriple().isNVPTX()) {
1584 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_nvptx);
1585 }
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001586
David Majnemer2dc81462015-01-19 09:00:28 +00001587 // Aliases should be on declarations, not definitions.
1588 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1589 if (FD->isThisDeclarationADefinition()) {
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001590 S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << FD << 0;
David Majnemer2dc81462015-01-19 09:00:28 +00001591 return;
1592 }
1593 } else {
1594 const auto *VD = cast<VarDecl>(D);
1595 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001596 S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << VD << 0;
David Majnemer2dc81462015-01-19 09:00:28 +00001597 return;
1598 }
1599 }
1600
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001601 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001602
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001603 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001604 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001605}
1606
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001607static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001608 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr.getRange(), Attr.getName()))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001609 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001610
Michael Han99315932013-01-24 16:46:58 +00001611 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1612 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001613}
1614
1615static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001616 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr.getRange(), Attr.getName()))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001617 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001618
Michael Han99315932013-01-24 16:46:58 +00001619 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1620 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001621}
1622
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001623static void handleTLSModelAttr(Sema &S, Decl *D,
1624 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001625 StringRef Model;
1626 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001627 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001628 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001629 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001630
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001631 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001632 if (Model != "global-dynamic" && Model != "local-dynamic"
1633 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001634 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001635 return;
1636 }
1637
Michael Han99315932013-01-24 16:46:58 +00001638 D->addAttr(::new (S.Context)
1639 TLSModelAttr(Attr.getRange(), S.Context, Model,
1640 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001641}
1642
David Majnemer631a90b2015-02-04 07:23:21 +00001643static void handleRestrictAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1644 QualType ResultType = getFunctionOrMethodResultType(D);
1645 if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
1646 D->addAttr(::new (S.Context) RestrictAttr(
1647 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1648 return;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001649 }
1650
David Majnemer631a90b2015-02-04 07:23:21 +00001651 S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only)
1652 << Attr.getName() << getFunctionOrMethodResultSourceRange(D);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001653}
1654
Chandler Carruthedc2c642011-07-02 00:01:44 +00001655static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001656 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001657 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001658 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001659 return;
1660 }
1661
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001662 if (CommonAttr *CA = S.mergeCommonAttr(D, Attr.getRange(), Attr.getName(),
1663 Attr.getAttributeSpellingListIndex()))
1664 D->addAttr(CA);
Eric Christopher8a2ee392010-12-02 02:45:55 +00001665}
1666
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00001667static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1668 if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, Attr.getRange(),
1669 Attr.getName()))
1670 return;
1671
1672 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context,
1673 Attr.getAttributeSpellingListIndex()));
1674}
1675
Chandler Carruthedc2c642011-07-02 00:01:44 +00001676static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001677 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001678
1679 if (S.CheckNoReturnAttr(attr)) return;
1680
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001681 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001682 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001683 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001684 return;
1685 }
1686
Michael Han99315932013-01-24 16:46:58 +00001687 D->addAttr(::new (S.Context)
1688 NoReturnAttr(attr.getRange(), S.Context,
1689 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001690}
1691
1692bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001693 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001694 attr.setInvalid();
1695 return true;
1696 }
1697
1698 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001699}
1700
Chandler Carruthedc2c642011-07-02 00:01:44 +00001701static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1702 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001703
1704 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1705 // because 'analyzer_noreturn' does not impact the type.
David Majnemer06864812015-04-07 06:01:53 +00001706 if (!isFunctionOrMethodOrBlock(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001707 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00001708 if (!VD || (!VD->getType()->isBlockPointerType() &&
1709 !VD->getType()->isFunctionPointerType())) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001710 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001711 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Craig Topper8f7f3ea2015-11-17 05:40:05 +00001712 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001713 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001714 return;
1715 }
1716 }
1717
Michael Han99315932013-01-24 16:46:58 +00001718 D->addAttr(::new (S.Context)
1719 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1720 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001721}
1722
John Thompsoncdb847ba2010-08-09 21:53:52 +00001723// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001724static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001725/*
1726 Returning a Vector Class in Registers
1727
Eric Christopherbc638a82010-12-01 22:13:54 +00001728 According to the PPU ABI specifications, a class with a single member of
1729 vector type is returned in memory when used as the return value of a function.
1730 This results in inefficient code when implementing vector classes. To return
1731 the value in a single vector register, add the vecreturn attribute to the
1732 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001733
1734 Example:
1735
1736 struct Vector
1737 {
1738 __vector float xyzw;
1739 } __attribute__((vecreturn));
1740
1741 Vector Add(Vector lhs, Vector rhs)
1742 {
1743 Vector result;
1744 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1745 return result; // This will be returned in a register
1746 }
1747*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001748 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1749 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001750 return;
1751 }
1752
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001753 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001754 int count = 0;
1755
1756 if (!isa<CXXRecordDecl>(record)) {
1757 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1758 return;
1759 }
1760
1761 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1762 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1763 return;
1764 }
1765
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001766 for (const auto *I : record->fields()) {
1767 if ((count == 1) || !I->getType()->isVectorType()) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001768 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1769 return;
1770 }
1771 count++;
1772 }
1773
Michael Han99315932013-01-24 16:46:58 +00001774 D->addAttr(::new (S.Context)
1775 VecReturnAttr(Attr.getRange(), S.Context,
1776 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001777}
1778
Richard Smithe233fbf2013-01-28 22:42:45 +00001779static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1780 const AttributeList &Attr) {
1781 if (isa<ParmVarDecl>(D)) {
1782 // [[carries_dependency]] can only be applied to a parameter if it is a
1783 // parameter of a function declaration or lambda.
1784 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1785 S.Diag(Attr.getLoc(),
1786 diag::err_carries_dependency_param_not_function_decl);
1787 return;
1788 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001789 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001790
1791 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1792 Attr.getRange(), S.Context,
1793 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001794}
1795
Akira Hatanakac8667622015-11-06 23:56:15 +00001796static void handleNotTailCalledAttr(Sema &S, Decl *D,
1797 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001798 if (checkAttrMutualExclusion<AlwaysInlineAttr>(S, D, Attr.getRange(),
1799 Attr.getName()))
Akira Hatanakac8667622015-11-06 23:56:15 +00001800 return;
1801
1802 D->addAttr(::new (S.Context) NotTailCalledAttr(
1803 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1804}
1805
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00001806static void handleDisableTailCallsAttr(Sema &S, Decl *D,
1807 const AttributeList &Attr) {
1808 if (checkAttrMutualExclusion<NakedAttr>(S, D, Attr.getRange(),
1809 Attr.getName()))
1810 return;
1811
1812 D->addAttr(::new (S.Context) DisableTailCallsAttr(
1813 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1814}
1815
Chandler Carruthedc2c642011-07-02 00:01:44 +00001816static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001817 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001818 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001819 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001820 return;
1821 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001822 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001823 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001824 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001825 return;
1826 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001827
Michael Han99315932013-01-24 16:46:58 +00001828 D->addAttr(::new (S.Context)
1829 UsedAttr(Attr.getRange(), S.Context,
1830 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001831}
1832
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00001833static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1834 bool IsCXX1zAttr = Attr.isCXX11Attribute() && !Attr.getScopeName();
1835
1836 if (IsCXX1zAttr && isa<VarDecl>(D)) {
1837 // The C++1z spelling of this attribute cannot be applied to a static data
1838 // member per [dcl.attr.unused]p2.
1839 if (cast<VarDecl>(D)->isStaticDataMember()) {
1840 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1841 << Attr.getName() << ExpectedForMaybeUnused;
1842 return;
1843 }
1844 }
1845
1846 // If this is spelled as the standard C++1z attribute, but not in C++1z, warn
1847 // about using it as an extension.
1848 if (!S.getLangOpts().CPlusPlus1z && IsCXX1zAttr)
1849 S.Diag(Attr.getLoc(), diag::ext_cxx1z_attr) << Attr.getName();
1850
1851 D->addAttr(::new (S.Context) UnusedAttr(
1852 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1853}
1854
Chandler Carruthedc2c642011-07-02 00:01:44 +00001855static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001856 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001857 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001858 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1859 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001860
Michael Han99315932013-01-24 16:46:58 +00001861 D->addAttr(::new (S.Context)
1862 ConstructorAttr(Attr.getRange(), S.Context, priority,
1863 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001864}
1865
Chandler Carruthedc2c642011-07-02 00:01:44 +00001866static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00001867 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001868 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001869 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1870 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001871
Michael Han99315932013-01-24 16:46:58 +00001872 D->addAttr(::new (S.Context)
1873 DestructorAttr(Attr.getRange(), S.Context, priority,
1874 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001875}
1876
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001877template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001878static void handleAttrWithMessage(Sema &S, Decl *D,
1879 const AttributeList &Attr) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001880 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001881 StringRef Str;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001882 if (Attr.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001883 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001884
Michael Han99315932013-01-24 16:46:58 +00001885 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1886 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001887}
1888
Ted Kremenek438f8db2014-02-22 01:06:05 +00001889static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Ted Kremenek28eace62013-11-23 01:01:34 +00001890 const AttributeList &Attr) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00001891 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Ted Kremenek27cfe102014-02-21 22:49:04 +00001892 S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition)
1893 << Attr.getName() << Attr.getRange();
1894 return;
1895 }
1896
Ted Kremenek28eace62013-11-23 01:01:34 +00001897 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001898 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1899 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001900}
1901
Jordy Rose740b0c22012-05-08 03:27:22 +00001902static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1903 IdentifierInfo *Platform,
1904 VersionTuple Introduced,
1905 VersionTuple Deprecated,
1906 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001907 StringRef PlatformName
1908 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1909 if (PlatformName.empty())
1910 PlatformName = Platform->getName();
1911
1912 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1913 // of these steps are needed).
1914 if (!Introduced.empty() && !Deprecated.empty() &&
1915 !(Introduced <= Deprecated)) {
1916 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1917 << 1 << PlatformName << Deprecated.getAsString()
1918 << 0 << Introduced.getAsString();
1919 return true;
1920 }
1921
1922 if (!Introduced.empty() && !Obsoleted.empty() &&
1923 !(Introduced <= Obsoleted)) {
1924 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1925 << 2 << PlatformName << Obsoleted.getAsString()
1926 << 0 << Introduced.getAsString();
1927 return true;
1928 }
1929
1930 if (!Deprecated.empty() && !Obsoleted.empty() &&
1931 !(Deprecated <= Obsoleted)) {
1932 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1933 << 2 << PlatformName << Obsoleted.getAsString()
1934 << 1 << Deprecated.getAsString();
1935 return true;
1936 }
1937
1938 return false;
1939}
1940
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001941/// \brief Check whether the two versions match.
1942///
1943/// If either version tuple is empty, then they are assumed to match. If
1944/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1945static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1946 bool BeforeIsOkay) {
1947 if (X.empty() || Y.empty())
1948 return true;
1949
1950 if (X == Y)
1951 return true;
1952
1953 if (BeforeIsOkay && X < Y)
1954 return true;
1955
1956 return false;
1957}
1958
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001959AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001960 IdentifierInfo *Platform,
Manman Ren719a8642016-05-06 21:04:01 +00001961 bool Implicit,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001962 VersionTuple Introduced,
1963 VersionTuple Deprecated,
1964 VersionTuple Obsoleted,
1965 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001966 StringRef Message,
Manman Rend8039df2016-02-22 04:47:24 +00001967 bool IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00001968 StringRef Replacement,
Douglas Gregord2a713e2015-09-30 21:27:42 +00001969 AvailabilityMergeKind AMK,
Michael Han99315932013-01-24 16:46:58 +00001970 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001971 VersionTuple MergedIntroduced = Introduced;
1972 VersionTuple MergedDeprecated = Deprecated;
1973 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001974 bool FoundAny = false;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001975 bool OverrideOrImpl = false;
1976 switch (AMK) {
1977 case AMK_None:
1978 case AMK_Redeclaration:
1979 OverrideOrImpl = false;
1980 break;
1981
1982 case AMK_Override:
1983 case AMK_ProtocolImplementation:
1984 OverrideOrImpl = true;
1985 break;
1986 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001987
Rafael Espindolac67f2232012-05-10 02:50:16 +00001988 if (D->hasAttrs()) {
1989 AttrVec &Attrs = D->getAttrs();
1990 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1991 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1992 if (!OldAA) {
1993 ++i;
1994 continue;
1995 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001996
Rafael Espindolac67f2232012-05-10 02:50:16 +00001997 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1998 if (OldPlatform != Platform) {
1999 ++i;
2000 continue;
2001 }
2002
Tim Northover7a73cc72015-10-30 16:30:49 +00002003 // If there is an existing availability attribute for this platform that
2004 // is explicit and the new one is implicit use the explicit one and
2005 // discard the new implicit attribute.
Manman Ren719a8642016-05-06 21:04:01 +00002006 if (!OldAA->isImplicit() && Implicit) {
Tim Northover7a73cc72015-10-30 16:30:49 +00002007 return nullptr;
2008 }
2009
2010 // If there is an existing attribute for this platform that is implicit
2011 // and the new attribute is explicit then erase the old one and
2012 // continue processing the attributes.
Manman Ren719a8642016-05-06 21:04:01 +00002013 if (!Implicit && OldAA->isImplicit()) {
Tim Northover7a73cc72015-10-30 16:30:49 +00002014 Attrs.erase(Attrs.begin() + i);
2015 --e;
2016 continue;
2017 }
2018
Rafael Espindolac67f2232012-05-10 02:50:16 +00002019 FoundAny = true;
2020 VersionTuple OldIntroduced = OldAA->getIntroduced();
2021 VersionTuple OldDeprecated = OldAA->getDeprecated();
2022 VersionTuple OldObsoleted = OldAA->getObsoleted();
2023 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002024
Douglas Gregord2a713e2015-09-30 21:27:42 +00002025 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
2026 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
2027 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002028 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregord2a713e2015-09-30 21:27:42 +00002029 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
2030 if (OverrideOrImpl) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002031 int Which = -1;
2032 VersionTuple FirstVersion;
2033 VersionTuple SecondVersion;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002034 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002035 Which = 0;
2036 FirstVersion = OldIntroduced;
2037 SecondVersion = Introduced;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002038 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002039 Which = 1;
2040 FirstVersion = Deprecated;
2041 SecondVersion = OldDeprecated;
Douglas Gregord2a713e2015-09-30 21:27:42 +00002042 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002043 Which = 2;
2044 FirstVersion = Obsoleted;
2045 SecondVersion = OldObsoleted;
2046 }
2047
2048 if (Which == -1) {
2049 Diag(OldAA->getLocation(),
2050 diag::warn_mismatched_availability_override_unavail)
Douglas Gregord2a713e2015-09-30 21:27:42 +00002051 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2052 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002053 } else {
2054 Diag(OldAA->getLocation(),
2055 diag::warn_mismatched_availability_override)
2056 << Which
2057 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
Douglas Gregord2a713e2015-09-30 21:27:42 +00002058 << FirstVersion.getAsString() << SecondVersion.getAsString()
2059 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002060 }
Douglas Gregord2a713e2015-09-30 21:27:42 +00002061 if (AMK == AMK_Override)
2062 Diag(Range.getBegin(), diag::note_overridden_method);
2063 else
2064 Diag(Range.getBegin(), diag::note_protocol_method);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002065 } else {
2066 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2067 Diag(Range.getBegin(), diag::note_previous_attribute);
2068 }
2069
Rafael Espindolac67f2232012-05-10 02:50:16 +00002070 Attrs.erase(Attrs.begin() + i);
2071 --e;
2072 continue;
2073 }
2074
2075 VersionTuple MergedIntroduced2 = MergedIntroduced;
2076 VersionTuple MergedDeprecated2 = MergedDeprecated;
2077 VersionTuple MergedObsoleted2 = MergedObsoleted;
2078
2079 if (MergedIntroduced2.empty())
2080 MergedIntroduced2 = OldIntroduced;
2081 if (MergedDeprecated2.empty())
2082 MergedDeprecated2 = OldDeprecated;
2083 if (MergedObsoleted2.empty())
2084 MergedObsoleted2 = OldObsoleted;
2085
2086 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2087 MergedIntroduced2, MergedDeprecated2,
2088 MergedObsoleted2)) {
2089 Attrs.erase(Attrs.begin() + i);
2090 --e;
2091 continue;
2092 }
2093
2094 MergedIntroduced = MergedIntroduced2;
2095 MergedDeprecated = MergedDeprecated2;
2096 MergedObsoleted = MergedObsoleted2;
2097 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002098 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002099 }
2100
2101 if (FoundAny &&
2102 MergedIntroduced == Introduced &&
2103 MergedDeprecated == Deprecated &&
2104 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00002105 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002106
Douglas Gregord2a713e2015-09-30 21:27:42 +00002107 // Only create a new attribute if !OverrideOrImpl, but we want to do
Ted Kremenekb5445722013-04-06 00:34:27 +00002108 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002109 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002110 MergedDeprecated, MergedObsoleted) &&
Douglas Gregord2a713e2015-09-30 21:27:42 +00002111 !OverrideOrImpl) {
Manman Ren719a8642016-05-06 21:04:01 +00002112 auto *Avail = ::new (Context) AvailabilityAttr(Range, Context, Platform,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002113 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002114 Obsoleted, IsUnavailable, Message,
Manman Ren75bc6762016-03-21 17:30:55 +00002115 IsStrict, Replacement,
2116 AttrSpellingListIndex);
Manman Ren719a8642016-05-06 21:04:01 +00002117 Avail->setImplicit(Implicit);
2118 return Avail;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002119 }
Craig Topperc3ec1492014-05-26 06:22:03 +00002120 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002121}
2122
Chandler Carruthedc2c642011-07-02 00:01:44 +00002123static void handleAvailabilityAttr(Sema &S, Decl *D,
2124 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002125 if (!checkAttributeNumArgs(S, Attr, 1))
2126 return;
2127 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00002128 unsigned Index = Attr.getAttributeSpellingListIndex();
2129
Aaron Ballman00e99962013-08-31 01:11:41 +00002130 IdentifierInfo *II = Platform->Ident;
2131 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2132 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2133 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002134
Rafael Espindolac231fab2013-01-08 21:30:32 +00002135 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2136 if (!ND) {
2137 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2138 return;
2139 }
2140
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002141 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2142 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2143 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002144 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Manman Rend8039df2016-02-22 04:47:24 +00002145 bool IsStrict = Attr.getStrictLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002146 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00002147 if (const StringLiteral *SE =
2148 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002149 Str = SE->getString();
Manman Ren75bc6762016-03-21 17:30:55 +00002150 StringRef Replacement;
2151 if (const StringLiteral *SE =
2152 dyn_cast_or_null<StringLiteral>(Attr.getReplacementExpr()))
2153 Replacement = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002154
Aaron Ballman00e99962013-08-31 01:11:41 +00002155 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Manman Ren719a8642016-05-06 21:04:01 +00002156 false/*Implicit*/,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002157 Introduced.Version,
2158 Deprecated.Version,
2159 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002160 IsUnavailable, Str,
Manman Ren75bc6762016-03-21 17:30:55 +00002161 IsStrict, Replacement,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002162 Sema::AMK_None,
Michael Han99315932013-01-24 16:46:58 +00002163 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002164 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002165 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002166
2167 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2168 // matches before the start of the watchOS platform.
2169 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2170 IdentifierInfo *NewII = nullptr;
2171 if (II->getName() == "ios")
2172 NewII = &S.Context.Idents.get("watchos");
2173 else if (II->getName() == "ios_app_extension")
2174 NewII = &S.Context.Idents.get("watchos_app_extension");
2175
2176 if (NewII) {
2177 auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple {
2178 if (Version.empty())
2179 return Version;
2180 auto Major = Version.getMajor();
2181 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2182 if (NewMajor >= 2) {
2183 if (Version.getMinor().hasValue()) {
2184 if (Version.getSubminor().hasValue())
2185 return VersionTuple(NewMajor, Version.getMinor().getValue(),
2186 Version.getSubminor().getValue());
2187 else
2188 return VersionTuple(NewMajor, Version.getMinor().getValue());
2189 }
2190 }
2191
2192 return VersionTuple(2, 0);
2193 };
2194
2195 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2196 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2197 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2198
2199 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
Manman Ren719a8642016-05-06 21:04:01 +00002200 Attr.getRange(),
Tim Northover7a73cc72015-10-30 16:30:49 +00002201 NewII,
Manman Ren719a8642016-05-06 21:04:01 +00002202 true/*Implicit*/,
Tim Northover7a73cc72015-10-30 16:30:49 +00002203 NewIntroduced,
2204 NewDeprecated,
2205 NewObsoleted,
2206 IsUnavailable, Str,
Manman Rend8039df2016-02-22 04:47:24 +00002207 IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002208 Replacement,
Tim Northover7a73cc72015-10-30 16:30:49 +00002209 Sema::AMK_None,
2210 Index);
2211 if (NewAttr)
2212 D->addAttr(NewAttr);
2213 }
2214 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2215 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2216 // matches before the start of the tvOS platform.
2217 IdentifierInfo *NewII = nullptr;
2218 if (II->getName() == "ios")
2219 NewII = &S.Context.Idents.get("tvos");
2220 else if (II->getName() == "ios_app_extension")
2221 NewII = &S.Context.Idents.get("tvos_app_extension");
2222
2223 if (NewII) {
2224 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
Manman Ren719a8642016-05-06 21:04:01 +00002225 Attr.getRange(),
Tim Northover7a73cc72015-10-30 16:30:49 +00002226 NewII,
Manman Ren719a8642016-05-06 21:04:01 +00002227 true/*Implicit*/,
Tim Northover7a73cc72015-10-30 16:30:49 +00002228 Introduced.Version,
2229 Deprecated.Version,
2230 Obsoleted.Version,
2231 IsUnavailable, Str,
Manman Rend8039df2016-02-22 04:47:24 +00002232 IsStrict,
Manman Ren75bc6762016-03-21 17:30:55 +00002233 Replacement,
Tim Northover7a73cc72015-10-30 16:30:49 +00002234 Sema::AMK_None,
2235 Index);
2236 if (NewAttr)
2237 D->addAttr(NewAttr);
2238 }
2239 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002240}
2241
John McCalld041a9b2013-02-20 01:54:26 +00002242template <class T>
2243static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2244 typename T::VisibilityType value,
2245 unsigned attrSpellingListIndex) {
2246 T *existingAttr = D->getAttr<T>();
2247 if (existingAttr) {
2248 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2249 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00002250 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00002251 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2252 S.Diag(range.getBegin(), diag::note_previous_attribute);
2253 D->dropAttr<T>();
2254 }
2255 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2256}
2257
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002258VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002259 VisibilityAttr::VisibilityType Vis,
2260 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002261 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2262 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002263}
2264
John McCalld041a9b2013-02-20 01:54:26 +00002265TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2266 TypeVisibilityAttr::VisibilityType Vis,
2267 unsigned AttrSpellingListIndex) {
2268 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2269 AttrSpellingListIndex);
2270}
2271
2272static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2273 bool isTypeVisibility) {
2274 // Visibility attributes don't mean anything on a typedef.
2275 if (isa<TypedefNameDecl>(D)) {
2276 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2277 << Attr.getName();
2278 return;
2279 }
2280
2281 // 'type_visibility' can only go on a type or namespace.
2282 if (isTypeVisibility &&
2283 !(isa<TagDecl>(D) ||
2284 isa<ObjCInterfaceDecl>(D) ||
2285 isa<NamespaceDecl>(D))) {
2286 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2287 << Attr.getName() << ExpectedTypeOrNamespace;
2288 return;
2289 }
2290
Benjamin Kramer70370212013-09-09 15:08:57 +00002291 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002292 StringRef TypeStr;
2293 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002294 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002295 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002296
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002297 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002298 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002299 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002300 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002301 return;
2302 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002303
2304 // Complain about attempts to use protected visibility on targets
2305 // (like Darwin) that don't support it.
2306 if (type == VisibilityAttr::Protected &&
2307 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2308 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2309 type = VisibilityAttr::Default;
2310 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002311
Michael Han99315932013-01-24 16:46:58 +00002312 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002313 clang::Attr *newAttr;
2314 if (isTypeVisibility) {
2315 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2316 (TypeVisibilityAttr::VisibilityType) type,
2317 Index);
2318 } else {
2319 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2320 }
2321 if (newAttr)
2322 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002323}
2324
Chandler Carruthedc2c642011-07-02 00:01:44 +00002325static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2326 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002327 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002328 if (!Attr.isArgIdent(0)) {
2329 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2330 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002331 return;
2332 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002333
Aaron Ballman682ee422013-09-11 19:47:58 +00002334 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2335 ObjCMethodFamilyAttr::FamilyKind F;
2336 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2337 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2338 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002339 return;
2340 }
2341
Alp Toker314cc812014-01-25 16:55:45 +00002342 if (F == ObjCMethodFamilyAttr::OMF_init &&
2343 !method->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00002344 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
Alp Toker314cc812014-01-25 16:55:45 +00002345 << method->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002346 // Ignore the attribute.
2347 return;
2348 }
2349
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002350 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00002351 S.Context, F,
2352 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002353}
2354
Chandler Carruthedc2c642011-07-02 00:01:44 +00002355static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002356 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002357 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002358 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002359 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2360 return;
2361 }
2362 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002363 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2364 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002365 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002366 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2367 return;
2368 }
2369 }
2370 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002371 // It is okay to include this attribute on properties, e.g.:
2372 //
2373 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2374 //
2375 // In this case it follows tradition and suppresses an error in the above
2376 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002377 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002378 }
Michael Han99315932013-01-24 16:46:58 +00002379 D->addAttr(::new (S.Context)
2380 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2381 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002382}
2383
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002384static void handleObjCIndependentClass(Sema &S, Decl *D, const AttributeList &Attr) {
2385 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
2386 QualType T = TD->getUnderlyingType();
2387 if (!T->isObjCObjectPointerType()) {
2388 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2389 return;
2390 }
2391 } else {
2392 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2393 return;
2394 }
2395 D->addAttr(::new (S.Context)
2396 ObjCIndependentClassAttr(Attr.getRange(), S.Context,
2397 Attr.getAttributeSpellingListIndex()));
2398}
2399
Chandler Carruthedc2c642011-07-02 00:01:44 +00002400static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002401 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002402 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002403 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002404 return;
2405 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002406
Aaron Ballman00e99962013-08-31 01:11:41 +00002407 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002408 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002409 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2410 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2411 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002412 return;
2413 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002414
Michael Han99315932013-01-24 16:46:58 +00002415 D->addAttr(::new (S.Context)
2416 BlocksAttr(Attr.getRange(), S.Context, type,
2417 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002418}
2419
Chandler Carruthedc2c642011-07-02 00:01:44 +00002420static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002421 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002422 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002423 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002424 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002425 if (E->isTypeDependent() || E->isValueDependent() ||
2426 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002427 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002428 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002429 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002430 return;
2431 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002432
John McCallb46f2872011-09-09 07:56:05 +00002433 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002434 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2435 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002436 return;
2437 }
John McCallb46f2872011-09-09 07:56:05 +00002438
2439 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002440 }
2441
Aaron Ballman18a78382013-11-21 00:28:23 +00002442 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002443 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002444 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002445 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002446 if (E->isTypeDependent() || E->isValueDependent() ||
2447 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002448 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002449 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002450 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002451 return;
2452 }
2453 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002454
John McCallb46f2872011-09-09 07:56:05 +00002455 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002456 // FIXME: This error message could be improved, it would be nice
2457 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002458 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2459 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002460 return;
2461 }
2462 }
2463
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002464 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002465 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002466 if (isa<FunctionNoProtoType>(FT)) {
2467 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2468 return;
2469 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002470
Chris Lattner9363e312009-03-17 23:03:47 +00002471 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002472 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002473 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002474 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002475 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002476 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002477 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002478 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002479 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002480 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2481 if (!BD->isVariadic()) {
2482 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2483 return;
2484 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002485 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002486 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002487 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002488 const FunctionType *FT = Ty->isFunctionPointerType()
2489 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002490 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002491 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002492 int m = Ty->isFunctionPointerType() ? 0 : 1;
2493 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002494 return;
2495 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002496 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002497 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002498 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002499 return;
2500 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002501 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002502 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002503 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002504 return;
2505 }
Michael Han99315932013-01-24 16:46:58 +00002506 D->addAttr(::new (S.Context)
2507 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2508 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002509}
2510
Chandler Carruthedc2c642011-07-02 00:01:44 +00002511static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Alp Toker314cc812014-01-25 16:55:45 +00002512 if (D->getFunctionType() &&
2513 D->getFunctionType()->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002514 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2515 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002516 return;
2517 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002518 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002519 if (MD->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002520 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2521 << Attr.getName() << 1;
2522 return;
2523 }
2524
Aaron Ballmane7964782016-03-07 22:44:55 +00002525 // If this is spelled as the standard C++1z attribute, but not in C++1z, warn
2526 // about using it as an extension.
2527 if (!S.getLangOpts().CPlusPlus1z && Attr.isCXX11Attribute() &&
2528 !Attr.getScopeName())
Richard Smith4f902c72016-03-08 00:32:55 +00002529 S.Diag(Attr.getLoc(), diag::ext_cxx1z_attr) << Attr.getName();
Aaron Ballmane7964782016-03-07 22:44:55 +00002530
Michael Han99315932013-01-24 16:46:58 +00002531 D->addAttr(::new (S.Context)
2532 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2533 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002534}
2535
Chandler Carruthedc2c642011-07-02 00:01:44 +00002536static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002537 // weak_import only applies to variable & function declarations.
2538 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002539 if (!D->canBeWeakImported(isDef)) {
2540 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002541 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2542 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002543 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002544 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002545 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002546 // Nothing to warn about here.
2547 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002548 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002549 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002550
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002551 return;
2552 }
2553
Michael Han99315932013-01-24 16:46:58 +00002554 D->addAttr(::new (S.Context)
2555 WeakImportAttr(Attr.getRange(), S.Context,
2556 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002557}
2558
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002559// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002560template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002561static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002562 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002563 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002564 for (unsigned i = 0; i < 3; ++i) {
2565 const Expr *E = Attr.getArgAsExpr(i);
2566 if (!checkUInt32Argument(S, Attr, E, WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002567 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002568 if (WGSize[i] == 0) {
2569 S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero)
2570 << Attr.getName() << E->getSourceRange();
2571 return;
2572 }
2573 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002574
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002575 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2576 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2577 Existing->getYDim() == WGSize[1] &&
2578 Existing->getZDim() == WGSize[2]))
2579 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002580
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002581 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2582 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002583 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002584}
2585
Joey Goulyaba589c2013-03-08 09:42:32 +00002586static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002587 if (!Attr.hasParsedType()) {
2588 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2589 << Attr.getName() << 1;
2590 return;
2591 }
2592
Craig Topperc3ec1492014-05-26 06:22:03 +00002593 TypeSourceInfo *ParmTSI = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00002594 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2595 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002596
2597 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2598 (ParmType->isBooleanType() ||
2599 !ParmType->isIntegralType(S.getASTContext()))) {
2600 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2601 << ParmType;
2602 return;
2603 }
2604
Aaron Ballmana9e05402013-12-02 22:16:55 +00002605 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002606 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002607 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2608 return;
2609 }
2610 }
2611
2612 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002613 ParmTSI,
2614 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002615}
2616
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002617SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002618 StringRef Name,
2619 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002620 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2621 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002622 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002623 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2624 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002625 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002626 }
Michael Han99315932013-01-24 16:46:58 +00002627 return ::new (Context) SectionAttr(Range, Context, Name,
2628 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002629}
2630
Reid Kleckner2a133222015-03-04 23:39:17 +00002631bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2632 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2633 if (!Error.empty()) {
2634 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error;
2635 return false;
2636 }
2637 return true;
2638}
2639
Chandler Carruthedc2c642011-07-02 00:01:44 +00002640static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002641 // Make sure that there is a string literal as the sections's single
2642 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002643 StringRef Str;
2644 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002645 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002646 return;
Mike Stump11289f42009-09-09 15:08:12 +00002647
Reid Kleckner2a133222015-03-04 23:39:17 +00002648 if (!S.checkSectionName(LiteralLoc, Str))
2649 return;
2650
Chris Lattner30ba6742009-08-10 19:03:04 +00002651 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002652 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002653 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002654 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002655 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002656 return;
2657 }
Mike Stump11289f42009-09-09 15:08:12 +00002658
Michael Han99315932013-01-24 16:46:58 +00002659 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002660 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002661 if (NewAttr)
2662 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002663}
2664
Eric Christopher789a7ad2015-06-12 01:36:05 +00002665// Check for things we'd like to warn about, no errors or validation for now.
2666// TODO: Validation should use a backend target library that specifies
2667// the allowable subtarget features and cpus. We could use something like a
2668// TargetCodeGenInfo hook here to do validation.
2669void Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
2670 for (auto Str : {"tune=", "fpmath="})
2671 if (AttrStr.find(Str) != StringRef::npos)
2672 Diag(LiteralLoc, diag::warn_unsupported_target_attribute) << Str;
2673}
2674
Eric Christopher11acf732015-06-12 01:35:52 +00002675static void handleTargetAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eric Christopher11acf732015-06-12 01:35:52 +00002676 StringRef Str;
2677 SourceLocation LiteralLoc;
2678 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
2679 return;
Eric Christopher789a7ad2015-06-12 01:36:05 +00002680 S.checkTargetAttr(LiteralLoc, Str);
Eric Christopher11acf732015-06-12 01:35:52 +00002681 unsigned Index = Attr.getAttributeSpellingListIndex();
2682 TargetAttr *NewAttr =
2683 ::new (S.Context) TargetAttr(Attr.getRange(), S.Context, Str, Index);
2684 D->addAttr(NewAttr);
2685}
2686
Chandler Carruthedc2c642011-07-02 00:01:44 +00002687static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002688 VarDecl *VD = cast<VarDecl>(D);
2689 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002690 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002691 return;
2692 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002693
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002694 Expr *E = Attr.getArgAsExpr(0);
2695 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00002696 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002697 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002698
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002699 // gcc only allows for simple identifiers. Since we support more than gcc, we
2700 // will warn the user.
2701 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2702 if (DRE->hasQualifier())
2703 S.Diag(Loc, diag::warn_cleanup_ext);
2704 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2705 NI = DRE->getNameInfo();
2706 if (!FD) {
2707 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2708 << NI.getName();
2709 return;
2710 }
2711 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2712 if (ULE->hasExplicitTemplateArgs())
2713 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002714 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2715 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002716 if (!FD) {
2717 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2718 << NI.getName();
2719 if (ULE->getType() == S.Context.OverloadTy)
2720 S.NoteAllOverloadCandidates(ULE);
2721 return;
2722 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002723 } else {
2724 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002725 return;
2726 }
2727
Anders Carlssond277d792009-01-31 01:16:18 +00002728 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002729 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2730 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002731 return;
2732 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002733
Anders Carlsson723f55d2009-02-07 23:16:50 +00002734 // We're currently more strict than GCC about what function types we accept.
2735 // If this ever proves to be a problem it should be easy to fix.
2736 QualType Ty = S.Context.getPointerType(VD->getType());
2737 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002738 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2739 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002740 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2741 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002742 return;
2743 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002744
Michael Han99315932013-01-24 16:46:58 +00002745 D->addAttr(::new (S.Context)
2746 CleanupAttr(Attr.getRange(), S.Context, FD,
2747 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002748}
2749
Mike Stumpd3bb5572009-07-24 19:02:52 +00002750/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002751/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002752static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002753 Expr *IdxExpr = Attr.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00002754 uint64_t Idx;
2755 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002756 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002757
Eric Christopherb64963e2015-08-13 21:34:35 +00002758 // Make sure the format string is really a string.
Alp Toker601b22c2014-01-21 23:35:24 +00002759 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002760
Eric Christopherb64963e2015-08-13 21:34:35 +00002761 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
2762 if (NotNSStringTy &&
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002763 !isCFStringType(Ty, S.Context) &&
2764 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002765 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002766 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00002767 << "a string type" << IdxExpr->getSourceRange()
2768 << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002769 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002770 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002771 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002772 if (!isNSStringType(Ty, S.Context) &&
2773 !isCFStringType(Ty, S.Context) &&
2774 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002775 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002776 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00002777 << (NotNSStringTy ? "string type" : "NSString")
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00002778 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002779 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002780 }
2781
Alp Toker601b22c2014-01-21 23:35:24 +00002782 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002783 // because that has corrected for the implicit this parameter, and is zero-
2784 // based. The attribute expects what the user wrote explicitly.
2785 llvm::APSInt Val;
2786 IdxExpr->EvaluateAsInt(Val, S.Context);
2787
Michael Han99315932013-01-24 16:46:58 +00002788 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002789 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002790 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002791}
2792
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002793enum FormatAttrKind {
2794 CFStringFormat,
2795 NSStringFormat,
2796 StrftimeFormat,
2797 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002798 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002799 InvalidFormat
2800};
2801
2802/// getFormatAttrKind - Map from format attribute names to supported format
2803/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002804static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002805 return llvm::StringSwitch<FormatAttrKind>(Format)
2806 // Check for formats that get handled specially.
2807 .Case("NSString", NSStringFormat)
2808 .Case("CFString", CFStringFormat)
2809 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002810
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002811 // Otherwise, check for supported formats.
2812 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2813 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2814 .Case("kprintf", SupportedFormat) // OpenBSD.
Dimitry Andric6b5ed342015-02-19 22:32:33 +00002815 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
Fariborz Jahanianf8dce0f2015-02-21 00:45:58 +00002816 .Case("os_trace", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002817
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002818 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2819 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002820}
2821
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002822/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002823/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002824static void handleInitPriorityAttr(Sema &S, Decl *D,
2825 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002826 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002827 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2828 return;
2829 }
2830
Aaron Ballman4a611152013-11-27 16:34:09 +00002831 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002832 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2833 Attr.setInvalid();
2834 return;
2835 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002836 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002837 if (S.Context.getAsArrayType(T))
2838 T = S.Context.getBaseElementType(T);
2839 if (!T->getAs<RecordType>()) {
2840 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2841 Attr.setInvalid();
2842 return;
2843 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002844
2845 Expr *E = Attr.getArgAsExpr(0);
2846 uint32_t prioritynum;
2847 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002848 Attr.setInvalid();
2849 return;
2850 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002851
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002852 if (prioritynum < 101 || prioritynum > 65535) {
2853 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00002854 << E->getSourceRange() << Attr.getName() << 101 << 65535;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002855 Attr.setInvalid();
2856 return;
2857 }
Michael Han99315932013-01-24 16:46:58 +00002858 D->addAttr(::new (S.Context)
2859 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2860 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002861}
2862
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002863FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2864 IdentifierInfo *Format, int FormatIdx,
2865 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002866 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002867 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002868 for (auto *F : D->specific_attrs<FormatAttr>()) {
2869 if (F->getType() == Format &&
2870 F->getFormatIdx() == FormatIdx &&
2871 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002872 // If we don't have a valid location for this attribute, adopt the
2873 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002874 if (F->getLocation().isInvalid())
2875 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00002876 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00002877 }
2878 }
2879
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002880 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2881 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002882}
2883
Mike Stumpd3bb5572009-07-24 19:02:52 +00002884/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002885/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002886static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002887 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002888 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002889 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002890 return;
2891 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002892
Chandler Carruth743682b2010-11-16 08:35:43 +00002893 // In C++ the implicit 'this' function parameter also counts, and they are
2894 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002895 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00002896 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002897
Aaron Ballman00e99962013-08-31 01:11:41 +00002898 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2899 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002900
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00002901 if (normalizeName(Format)) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002902 // If we've modified the string name, we need a new identifier for it.
2903 II = &S.Context.Idents.get(Format);
2904 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002905
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002906 // Check for supported formats.
2907 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002908
2909 if (Kind == IgnoredFormat)
2910 return;
2911
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002912 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002913 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002914 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002915 return;
2916 }
2917
2918 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002919 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002920 uint32_t Idx;
2921 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002922 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002923
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002924 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002925 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002926 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002927 return;
2928 }
2929
2930 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002931 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002932
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002933 if (HasImplicitThisParam) {
2934 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002935 S.Diag(Attr.getLoc(),
2936 diag::err_format_attribute_implicit_this_format_string)
2937 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002938 return;
2939 }
2940 ArgIdx--;
2941 }
Mike Stump11289f42009-09-09 15:08:12 +00002942
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002943 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002944 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002945
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002946 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002947 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002948 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002949 << "a CFString" << IdxExpr->getSourceRange()
2950 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00002951 return;
2952 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002953 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002954 // FIXME: do we need to check if the type is NSString*? What are the
2955 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002956 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002957 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002958 << "an NSString" << IdxExpr->getSourceRange()
2959 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002960 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002961 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002962 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002963 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002964 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002965 << "a string type" << IdxExpr->getSourceRange()
2966 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002967 return;
2968 }
2969
2970 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002971 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002972 uint32_t FirstArg;
2973 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002974 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002975
2976 // check if the function is variadic if the 3rd argument non-zero
2977 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002978 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002979 ++NumArgs; // +1 for ...
2980 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002981 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002982 return;
2983 }
2984 }
2985
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002986 // strftime requires FirstArg to be 0 because it doesn't read from any
2987 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002988 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002989 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002990 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2991 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002992 return;
2993 }
2994 // if 0 it disables parameter checking (to use with e.g. va_list)
2995 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002996 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002997 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002998 return;
2999 }
3000
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003001 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003002 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00003003 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003004 if (NewAttr)
3005 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003006}
3007
Chandler Carruthedc2c642011-07-02 00:01:44 +00003008static void handleTransparentUnionAttr(Sema &S, Decl *D,
3009 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003010 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00003011 RecordDecl *RD = nullptr;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003012 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003013 if (TD && TD->getUnderlyingType()->isUnionType())
3014 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3015 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003016 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003017
3018 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00003019 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003020 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003021 return;
3022 }
3023
John McCallf937c022011-10-07 06:10:15 +00003024 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003025 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003026 diag::warn_transparent_union_attribute_not_definition);
3027 return;
3028 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003029
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003030 RecordDecl::field_iterator Field = RD->field_begin(),
3031 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003032 if (Field == FieldEnd) {
3033 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3034 return;
3035 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003036
David Blaikie40ed2972012-06-06 20:45:41 +00003037 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003038 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003039 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003040 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003041 diag::warn_transparent_union_attribute_floating)
3042 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003043 return;
3044 }
3045
3046 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3047 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3048 for (; Field != FieldEnd; ++Field) {
3049 QualType FieldType = Field->getType();
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003050 // FIXME: this isn't fully correct; we also need to test whether the
3051 // members of the union would all have the same calling convention as the
3052 // first member of the union. Checking just the size and alignment isn't
3053 // sufficient (consider structs passed on the stack instead of in registers
3054 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003055 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00003056 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003057 // Warn if we drop the attribute.
3058 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003059 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003060 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003061 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003062 diag::warn_transparent_union_attribute_field_size_align)
3063 << isSize << Field->getDeclName() << FieldBits;
3064 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003065 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003066 diag::note_transparent_union_first_field_size_align)
3067 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003068 return;
3069 }
3070 }
3071
Michael Han99315932013-01-24 16:46:58 +00003072 RD->addAttr(::new (S.Context)
3073 TransparentUnionAttr(Attr.getRange(), S.Context,
3074 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003075}
3076
Chandler Carruthedc2c642011-07-02 00:01:44 +00003077static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003078 // Make sure that there is a string literal as the annotation's single
3079 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003080 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003081 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003082 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003083
3084 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003085 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
3086 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003087 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003088 }
Michael Han99315932013-01-24 16:46:58 +00003089
3090 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003091 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00003092 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003093}
3094
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003095static void handleAlignValueAttr(Sema &S, Decl *D,
3096 const AttributeList &Attr) {
3097 S.AddAlignValueAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
3098 Attr.getAttributeSpellingListIndex());
3099}
3100
3101void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
3102 unsigned SpellingListIndex) {
3103 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
3104 SourceLocation AttrLoc = AttrRange.getBegin();
3105
3106 QualType T;
3107 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3108 T = TD->getUnderlyingType();
3109 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3110 T = VD->getType();
3111 else
3112 llvm_unreachable("Unknown decl type for align_value");
3113
3114 if (!T->isDependentType() && !T->isAnyPointerType() &&
3115 !T->isReferenceType() && !T->isMemberPointerType()) {
3116 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
3117 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
3118 return;
3119 }
3120
3121 if (!E->isValueDependent()) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003122 llvm::APSInt Alignment;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003123 ExprResult ICE
3124 = VerifyIntegerConstantExpression(E, &Alignment,
3125 diag::err_align_value_attribute_argument_not_int,
3126 /*AllowFold*/ false);
3127 if (ICE.isInvalid())
3128 return;
3129
3130 if (!Alignment.isPowerOf2()) {
3131 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3132 << E->getSourceRange();
3133 return;
3134 }
3135
3136 D->addAttr(::new (Context)
3137 AlignValueAttr(AttrRange, Context, ICE.get(),
3138 SpellingListIndex));
3139 return;
3140 }
3141
3142 // Save dependent expressions in the AST to be instantiated.
3143 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003144}
3145
Chandler Carruthedc2c642011-07-02 00:01:44 +00003146static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003147 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003148 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003149 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3150 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003151 return;
3152 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003153
Richard Smith848e1f12013-02-01 08:12:08 +00003154 if (Attr.getNumArgs() == 0) {
3155 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
Craig Topperc3ec1492014-05-26 06:22:03 +00003156 true, nullptr, Attr.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00003157 return;
3158 }
3159
Aaron Ballman00e99962013-08-31 01:11:41 +00003160 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00003161 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3162 S.Diag(Attr.getEllipsisLoc(),
3163 diag::err_pack_expansion_without_parameter_packs);
3164 return;
3165 }
3166
3167 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3168 return;
3169
David Majnemer26a1e0e2015-04-07 02:37:09 +00003170 if (E->isValueDependent()) {
3171 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3172 if (!TND->getUnderlyingType()->isDependentType()) {
3173 S.Diag(Attr.getLoc(), diag::err_alignment_dependent_typedef_name)
3174 << E->getSourceRange();
3175 return;
3176 }
3177 }
3178 }
3179
Richard Smith44c247f2013-02-22 08:32:16 +00003180 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3181 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003182}
3183
3184void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003185 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003186 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3187 SourceLocation AttrLoc = AttrRange.getBegin();
3188
Richard Smith1dba27c2013-01-29 09:02:09 +00003189 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003190 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003191 // C++11 [dcl.align]p1:
3192 // An alignment-specifier may be applied to a variable or to a class
3193 // data member, but it shall not be applied to a bit-field, a function
3194 // parameter, the formal parameter of a catch clause, or a variable
3195 // declared with the register storage class specifier. An
3196 // alignment-specifier may also be applied to the declaration of a class
3197 // or enumeration type.
3198 // C11 6.7.5/2:
3199 // An alignment attribute shall not be specified in a declaration of
3200 // a typedef, or a bit-field, or a function, or a parameter, or an
3201 // object declared with the register storage-class specifier.
3202 int DiagKind = -1;
3203 if (isa<ParmVarDecl>(D)) {
3204 DiagKind = 0;
3205 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3206 if (VD->getStorageClass() == SC_Register)
3207 DiagKind = 1;
3208 if (VD->isExceptionVariable())
3209 DiagKind = 2;
3210 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3211 if (FD->isBitField())
3212 DiagKind = 3;
3213 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00003214 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00003215 << (TmpAttr.isC11() ? ExpectedVariableOrField
3216 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003217 return;
3218 }
3219 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003220 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00003221 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003222 return;
3223 }
3224 }
3225
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003226 if (E->isTypeDependent() || E->isValueDependent()) {
3227 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003228 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3229 AA->setPackExpansion(IsPackExpansion);
3230 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003231 return;
3232 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003233
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003234 // FIXME: Cache the number on the Attr object?
David Majnemer0be6bd02015-07-26 09:02:21 +00003235 llvm::APSInt Alignment;
Douglas Gregore2b37442012-05-04 22:38:52 +00003236 ExprResult ICE
3237 = VerifyIntegerConstantExpression(E, &Alignment,
3238 diag::err_aligned_attribute_argument_not_int,
3239 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003240 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003241 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003242
David Majnemer0be6bd02015-07-26 09:02:21 +00003243 uint64_t AlignVal = Alignment.getZExtValue();
3244
Richard Smith848e1f12013-02-01 08:12:08 +00003245 // C++11 [dcl.align]p2:
3246 // -- if the constant expression evaluates to zero, the alignment
3247 // specifier shall have no effect
3248 // C11 6.7.5p6:
3249 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003250 if (!(TmpAttr.isAlignas() && !Alignment)) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003251 if (!llvm::isPowerOf2_64(AlignVal)) {
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003252 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3253 << E->getSourceRange();
3254 return;
3255 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003256 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003257
David Majnemerabecae72014-02-12 20:36:10 +00003258 // Alignment calculations can wrap around if it's greater than 2**28.
David Majnemer29c69db2015-07-26 01:48:59 +00003259 unsigned MaxValidAlignment =
3260 Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192
3261 : 268435456;
David Majnemer0be6bd02015-07-26 09:02:21 +00003262 if (AlignVal > MaxValidAlignment) {
David Majnemerabecae72014-02-12 20:36:10 +00003263 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
3264 << E->getSourceRange();
3265 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003266 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003267
David Majnemer0be6bd02015-07-26 09:02:21 +00003268 if (Context.getTargetInfo().isTLSSupported()) {
3269 unsigned MaxTLSAlign =
3270 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3271 .getQuantity();
3272 auto *VD = dyn_cast<VarDecl>(D);
3273 if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD &&
3274 VD->getTLSKind() != VarDecl::TLS_None) {
3275 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3276 << (unsigned)AlignVal << VD << MaxTLSAlign;
3277 return;
3278 }
3279 }
3280
Richard Smith44c247f2013-02-22 08:32:16 +00003281 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003282 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00003283 AA->setPackExpansion(IsPackExpansion);
3284 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003285}
3286
Michael Hanaf02bbe2013-02-01 01:19:17 +00003287void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003288 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003289 // FIXME: Cache the number on the Attr object if non-dependent?
3290 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003291 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3292 SpellingListIndex);
3293 AA->setPackExpansion(IsPackExpansion);
3294 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003295}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003296
Richard Smith848e1f12013-02-01 08:12:08 +00003297void Sema::CheckAlignasUnderalignment(Decl *D) {
3298 assert(D->hasAttrs() && "no attributes on decl");
3299
David Majnemer475b25e2015-01-21 10:54:38 +00003300 QualType UnderlyingTy, DiagTy;
3301 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
3302 UnderlyingTy = DiagTy = VD->getType();
3303 } else {
3304 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
3305 if (EnumDecl *ED = dyn_cast<EnumDecl>(D))
3306 UnderlyingTy = ED->getIntegerType();
3307 }
3308 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003309 return;
3310
3311 // C++11 [dcl.align]p5, C11 6.7.5/4:
3312 // The combined effect of all alignment attributes in a declaration shall
3313 // not specify an alignment that is less strict than the alignment that
3314 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003315 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003316 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003317 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003318 if (I->isAlignmentDependent())
3319 return;
3320 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003321 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003322 Align = std::max(Align, I->getAlignment(Context));
3323 }
3324
3325 if (AlignasAttr && Align) {
3326 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003327 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003328 if (NaturalAlign > RequestedAlign)
3329 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003330 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003331 }
3332}
3333
David Majnemer2c4e00a2014-01-29 22:07:36 +00003334bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003335 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003336 MSInheritanceAttr::Spelling SemanticSpelling) {
3337 assert(RD->hasDefinition() && "RD has no definition!");
3338
David Majnemer98c9ee22014-02-07 00:43:07 +00003339 // We may not have seen base specifiers or any virtual methods yet. We will
3340 // have to wait until the record is defined to catch any mismatches.
3341 if (!RD->getDefinition()->isCompleteDefinition())
3342 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003343
David Majnemer98c9ee22014-02-07 00:43:07 +00003344 // The unspecified model never matches what a definition could need.
3345 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
3346 return false;
3347
David Majnemer4bb09802014-02-10 19:50:15 +00003348 if (BestCase) {
3349 if (RD->calculateInheritanceModel() == SemanticSpelling)
3350 return false;
3351 } else {
3352 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3353 return false;
3354 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003355
3356 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3357 << 0 /*definition*/;
3358 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3359 << RD->getNameAsString();
3360 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003361}
3362
Alexey Bataevf278eb12015-11-19 10:13:11 +00003363/// parseModeAttrArg - Parses attribute mode string and returns parsed type
3364/// attribute.
3365static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
3366 bool &IntegerMode, bool &ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003367 IntegerMode = true;
3368 ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003369 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003370 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003371 switch (Str[0]) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003372 case 'Q':
3373 DestWidth = 8;
3374 break;
3375 case 'H':
3376 DestWidth = 16;
3377 break;
3378 case 'S':
3379 DestWidth = 32;
3380 break;
3381 case 'D':
3382 DestWidth = 64;
3383 break;
3384 case 'X':
3385 DestWidth = 96;
3386 break;
3387 case 'T':
3388 DestWidth = 128;
3389 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003390 }
3391 if (Str[1] == 'F') {
3392 IntegerMode = false;
3393 } else if (Str[1] == 'C') {
3394 IntegerMode = false;
3395 ComplexMode = true;
3396 } else if (Str[1] != 'I') {
3397 DestWidth = 0;
3398 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003399 break;
3400 case 4:
3401 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3402 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003403 if (Str == "word")
Reid Klecknerf27e7522016-02-01 18:58:24 +00003404 DestWidth = S.Context.getTargetInfo().getRegisterWidth();
Daniel Dunbarafff4342009-10-18 02:09:24 +00003405 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003406 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003407 break;
3408 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003409 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003410 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003411 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003412 case 11:
3413 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003414 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003415 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003416 }
Alexey Bataevf278eb12015-11-19 10:13:11 +00003417}
3418
3419/// handleModeAttr - This attribute modifies the width of a decl with primitive
3420/// type.
3421///
3422/// Despite what would be logical, the mode attribute is a decl attribute, not a
3423/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3424/// HImode, not an intermediate pointer.
3425static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3426 // This attribute isn't documented, but glibc uses it. It changes
3427 // the width of an int or unsigned int to the specified size.
3428 if (!Attr.isArgIdent(0)) {
3429 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3430 << AANT_ArgumentIdentifier;
3431 return;
3432 }
3433
3434 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003435
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003436 S.AddModeAttr(Attr.getRange(), D, Name, Attr.getAttributeSpellingListIndex());
3437}
3438
3439void Sema::AddModeAttr(SourceRange AttrRange, Decl *D, IdentifierInfo *Name,
3440 unsigned SpellingListIndex, bool InInstantiation) {
3441 StringRef Str = Name->getName();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003442 normalizeName(Str);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003443 SourceLocation AttrLoc = AttrRange.getBegin();
Alexey Bataevf278eb12015-11-19 10:13:11 +00003444
3445 unsigned DestWidth = 0;
3446 bool IntegerMode = true;
3447 bool ComplexMode = false;
3448 llvm::APInt VectorSize(64, 0);
3449 if (Str.size() >= 4 && Str[0] == 'V') {
3450 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
3451 size_t StrSize = Str.size();
3452 size_t VectorStringLength = 0;
3453 while ((VectorStringLength + 1) < StrSize &&
3454 isdigit(Str[VectorStringLength + 1]))
3455 ++VectorStringLength;
3456 if (VectorStringLength &&
3457 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
3458 VectorSize.isPowerOf2()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003459 parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth,
Alexey Bataevf278eb12015-11-19 10:13:11 +00003460 IntegerMode, ComplexMode);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003461 // Avoid duplicate warning from template instantiation.
3462 if (!InInstantiation)
3463 Diag(AttrLoc, diag::warn_vector_mode_deprecated);
Alexey Bataevf278eb12015-11-19 10:13:11 +00003464 } else {
3465 VectorSize = 0;
3466 }
3467 }
3468
3469 if (!VectorSize)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003470 parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode);
3471
3472 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3473 // and friends, at least with glibc.
3474 // FIXME: Make sure floating-point mappings are accurate
3475 // FIXME: Support XF and TF types
3476 if (!DestWidth) {
3477 Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name;
3478 return;
3479 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003480
3481 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003482 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003483 OldTy = TD->getUnderlyingType();
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003484 else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) {
3485 // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'.
3486 // Try to get type from enum declaration, default to int.
3487 OldTy = ED->getIntegerType();
3488 if (OldTy.isNull())
3489 OldTy = Context.IntTy;
3490 } else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003491 OldTy = cast<ValueDecl>(D)->getType();
Eli Friedman4735374e2009-03-03 06:41:03 +00003492
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003493 if (OldTy->isDependentType()) {
3494 D->addAttr(::new (Context)
3495 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
3496 return;
3497 }
3498
Alexey Bataev326057d2015-06-19 07:46:21 +00003499 // Base type can also be a vector type (see PR17453).
3500 // Distinguish between base type and base element type.
3501 QualType OldElemTy = OldTy;
3502 if (const VectorType *VT = OldTy->getAs<VectorType>())
3503 OldElemTy = VT->getElementType();
3504
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003505 // GCC allows 'mode' attribute on enumeration types (even incomplete), except
3506 // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete
3507 // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected.
3508 if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) &&
3509 VectorSize.getBoolValue()) {
3510 Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << AttrRange;
3511 return;
3512 }
3513 bool IntegralOrAnyEnumType =
3514 OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>();
3515
3516 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() &&
3517 !IntegralOrAnyEnumType)
3518 Diag(AttrLoc, diag::err_mode_not_primitive);
Eli Friedman4735374e2009-03-03 06:41:03 +00003519 else if (IntegerMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003520 if (!IntegralOrAnyEnumType)
3521 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00003522 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003523 if (!OldElemTy->isComplexType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003524 Diag(AttrLoc, diag::err_mode_wrong_type);
Eli Friedman4735374e2009-03-03 06:41:03 +00003525 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00003526 if (!OldElemTy->isFloatingType())
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003527 Diag(AttrLoc, diag::err_mode_wrong_type);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003528 }
3529
Alexey Bataev326057d2015-06-19 07:46:21 +00003530 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003531
3532 if (IntegerMode)
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003533 NewElemTy = Context.getIntTypeForBitwidth(DestWidth,
3534 OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003535 else
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003536 NewElemTy = Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003537
Alexey Bataev326057d2015-06-19 07:46:21 +00003538 if (NewElemTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003539 Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003540 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003541 }
3542
Eli Friedman4735374e2009-03-03 06:41:03 +00003543 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003544 NewElemTy = Context.getComplexType(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00003545 }
3546
3547 QualType NewTy = NewElemTy;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003548 if (VectorSize.getBoolValue()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003549 NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(),
3550 VectorType::GenericVector);
Alexey Bataevf278eb12015-11-19 10:13:11 +00003551 } else if (const VectorType *OldVT = OldTy->getAs<VectorType>()) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003552 // Complex machine mode does not support base vector types.
3553 if (ComplexMode) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003554 Diag(AttrLoc, diag::err_complex_mode_vector_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00003555 return;
3556 }
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003557 unsigned NumElements = Context.getTypeSize(OldElemTy) *
Alexey Bataev326057d2015-06-19 07:46:21 +00003558 OldVT->getNumElements() /
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003559 Context.getTypeSize(NewElemTy);
Alexey Bataev326057d2015-06-19 07:46:21 +00003560 NewTy =
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003561 Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
Alexey Bataev326057d2015-06-19 07:46:21 +00003562 }
3563
3564 if (NewTy.isNull()) {
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003565 Diag(AttrLoc, diag::err_mode_wrong_type);
Alexey Bataev326057d2015-06-19 07:46:21 +00003566 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003567 }
3568
3569 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003570 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3571 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003572 else if (EnumDecl *ED = dyn_cast<EnumDecl>(D))
3573 ED->setIntegerType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003574 else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003575 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003576
Denis Zobnind9e2dcd2016-02-02 13:50:39 +00003577 D->addAttr(::new (Context)
3578 ModeAttr(AttrRange, Context, Name, SpellingListIndex));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003579}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003580
Chandler Carruthedc2c642011-07-02 00:01:44 +00003581static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han99315932013-01-24 16:46:58 +00003582 D->addAttr(::new (S.Context)
3583 NoDebugAttr(Attr.getRange(), S.Context,
3584 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003585}
3586
Paul Robinson30e41fb2014-12-15 18:57:28 +00003587AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range,
Paul Robinson080b1f32015-01-13 18:34:56 +00003588 IdentifierInfo *Ident,
Paul Robinson30e41fb2014-12-15 18:57:28 +00003589 unsigned AttrSpellingListIndex) {
3590 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003591 Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00003592 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3593 return nullptr;
3594 }
3595
3596 if (D->hasAttr<AlwaysInlineAttr>())
3597 return nullptr;
3598
3599 return ::new (Context) AlwaysInlineAttr(Range, Context,
3600 AttrSpellingListIndex);
3601}
3602
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003603CommonAttr *Sema::mergeCommonAttr(Decl *D, SourceRange Range,
3604 IdentifierInfo *Ident,
3605 unsigned AttrSpellingListIndex) {
3606 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, Range, Ident))
3607 return nullptr;
3608
3609 return ::new (Context) CommonAttr(Range, Context, AttrSpellingListIndex);
3610}
3611
3612InternalLinkageAttr *
3613Sema::mergeInternalLinkageAttr(Decl *D, SourceRange Range,
3614 IdentifierInfo *Ident,
3615 unsigned AttrSpellingListIndex) {
3616 if (auto VD = dyn_cast<VarDecl>(D)) {
3617 // Attribute applies to Var but not any subclass of it (like ParmVar,
3618 // ImplicitParm or VarTemplateSpecialization).
3619 if (VD->getKind() != Decl::Var) {
3620 Diag(Range.getBegin(), diag::warn_attribute_wrong_decl_type)
3621 << Ident << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
3622 : ExpectedVariableOrFunction);
3623 return nullptr;
3624 }
3625 // Attribute does not apply to non-static local variables.
3626 if (VD->hasLocalStorage()) {
3627 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
3628 return nullptr;
3629 }
3630 }
3631
3632 if (checkAttrMutualExclusion<CommonAttr>(*this, D, Range, Ident))
3633 return nullptr;
3634
3635 return ::new (Context)
3636 InternalLinkageAttr(Range, Context, AttrSpellingListIndex);
3637}
3638
Paul Robinson30e41fb2014-12-15 18:57:28 +00003639MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range,
3640 unsigned AttrSpellingListIndex) {
3641 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
3642 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'";
3643 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3644 return nullptr;
3645 }
3646
3647 if (D->hasAttr<MinSizeAttr>())
3648 return nullptr;
3649
3650 return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex);
3651}
3652
3653OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range,
3654 unsigned AttrSpellingListIndex) {
3655 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
3656 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
3657 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3658 D->dropAttr<AlwaysInlineAttr>();
3659 }
3660 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
3661 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
3662 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3663 D->dropAttr<MinSizeAttr>();
3664 }
3665
3666 if (D->hasAttr<OptimizeNoneAttr>())
3667 return nullptr;
3668
3669 return ::new (Context) OptimizeNoneAttr(Range, Context,
3670 AttrSpellingListIndex);
3671}
3672
Paul Robinsonf0674352014-03-31 22:29:15 +00003673static void handleAlwaysInlineAttr(Sema &S, Decl *D,
3674 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003675 if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, Attr.getRange(),
3676 Attr.getName()))
Akira Hatanakac8667622015-11-06 23:56:15 +00003677 return;
3678
Paul Robinson080b1f32015-01-13 18:34:56 +00003679 if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr(
3680 D, Attr.getRange(), Attr.getName(),
3681 Attr.getAttributeSpellingListIndex()))
3682 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00003683}
3684
Paul Robinson080b1f32015-01-13 18:34:56 +00003685static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3686 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(
3687 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3688 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00003689}
3690
Paul Robinsonf0674352014-03-31 22:29:15 +00003691static void handleOptimizeNoneAttr(Sema &S, Decl *D,
3692 const AttributeList &Attr) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003693 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(
3694 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3695 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00003696}
3697
Chandler Carruthedc2c642011-07-02 00:01:44 +00003698static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Justin Lebar3eaaf862016-01-13 01:07:35 +00003699 if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, Attr.getRange(),
3700 Attr.getName()) ||
3701 checkAttrMutualExclusion<CUDAHostAttr>(S, D, Attr.getRange(),
3702 Attr.getName())) {
3703 return;
3704 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003705 FunctionDecl *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00003706 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00003707 SourceRange RTRange = FD->getReturnTypeSourceRange();
3708 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00003709 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00003710 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
3711 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00003712 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003713 }
Justin Lebarc66a1062016-01-20 00:26:57 +00003714 if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) {
3715 if (Method->isInstance()) {
3716 S.Diag(Method->getLocStart(), diag::err_kern_is_nonstatic_method)
3717 << Method;
3718 return;
3719 }
3720 S.Diag(Method->getLocStart(), diag::warn_kern_is_method) << Method;
3721 }
3722 // Only warn for "inline" when compiling for host, to cut down on noise.
3723 if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice)
3724 S.Diag(FD->getLocStart(), diag::warn_kern_is_inline) << FD;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003725
Aaron Ballman3aff6332013-12-02 19:30:36 +00003726 D->addAttr(::new (S.Context)
3727 CUDAGlobalAttr(Attr.getRange(), S.Context,
Eli Bendersky83860042014-09-02 22:00:06 +00003728 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003729}
3730
Chandler Carruthedc2c642011-07-02 00:01:44 +00003731static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003732 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003733 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003734 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003735 return;
3736 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003737
Michael Han99315932013-01-24 16:46:58 +00003738 D->addAttr(::new (S.Context)
3739 GNUInlineAttr(Attr.getRange(), S.Context,
3740 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003741}
3742
Chandler Carruthedc2c642011-07-02 00:01:44 +00003743static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003744 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003745
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003746 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003747 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3748 CallingConv CC;
Richard Smitheec7cb12015-05-28 23:38:53 +00003749 if (S.CheckCallingConvAttr(Attr, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00003750 return;
3751
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003752 if (!isa<ObjCMethodDecl>(D)) {
3753 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3754 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003755 return;
3756 }
3757
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003758 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003759 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003760 D->addAttr(::new (S.Context)
3761 FastCallAttr(Attr.getRange(), S.Context,
3762 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003763 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003764 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003765 D->addAttr(::new (S.Context)
3766 StdCallAttr(Attr.getRange(), S.Context,
3767 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003768 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003769 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003770 D->addAttr(::new (S.Context)
3771 ThisCallAttr(Attr.getRange(), S.Context,
3772 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003773 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003774 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003775 D->addAttr(::new (S.Context)
3776 CDeclAttr(Attr.getRange(), S.Context,
3777 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003778 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003779 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003780 D->addAttr(::new (S.Context)
3781 PascalAttr(Attr.getRange(), S.Context,
3782 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003783 return;
John McCall477f2bb2016-03-03 06:39:32 +00003784 case AttributeList::AT_SwiftCall:
3785 D->addAttr(::new (S.Context)
3786 SwiftCallAttr(Attr.getRange(), S.Context,
3787 Attr.getAttributeSpellingListIndex()));
3788 return;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003789 case AttributeList::AT_VectorCall:
3790 D->addAttr(::new (S.Context)
3791 VectorCallAttr(Attr.getRange(), S.Context,
3792 Attr.getAttributeSpellingListIndex()));
3793 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003794 case AttributeList::AT_MSABI:
3795 D->addAttr(::new (S.Context)
3796 MSABIAttr(Attr.getRange(), S.Context,
3797 Attr.getAttributeSpellingListIndex()));
3798 return;
3799 case AttributeList::AT_SysVABI:
3800 D->addAttr(::new (S.Context)
3801 SysVABIAttr(Attr.getRange(), S.Context,
3802 Attr.getAttributeSpellingListIndex()));
3803 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003804 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003805 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003806 switch (CC) {
3807 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003808 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003809 break;
3810 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003811 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003812 break;
3813 default:
3814 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003815 }
3816
Michael Han99315932013-01-24 16:46:58 +00003817 D->addAttr(::new (S.Context)
3818 PcsAttr(Attr.getRange(), S.Context, PCS,
3819 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003820 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003821 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00003822 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003823 D->addAttr(::new (S.Context)
3824 IntelOclBiccAttr(Attr.getRange(), S.Context,
3825 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003826 return;
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00003827 case AttributeList::AT_PreserveMost:
3828 D->addAttr(::new (S.Context) PreserveMostAttr(
3829 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
3830 return;
3831 case AttributeList::AT_PreserveAll:
3832 D->addAttr(::new (S.Context) PreserveAllAttr(
3833 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
3834 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003835 default:
3836 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003837 }
3838}
3839
Aaron Ballman02df2e02012-12-09 17:45:41 +00003840bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3841 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003842 if (attr.isInvalid())
3843 return true;
3844
John McCall3b5a8f52016-03-03 00:10:03 +00003845 if (attr.hasProcessingCache()) {
3846 CC = (CallingConv) attr.getProcessingCache();
3847 return false;
3848 }
3849
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003850 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003851 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003852 attr.setInvalid();
3853 return true;
3854 }
3855
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003856 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003857 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003858 case AttributeList::AT_CDecl: CC = CC_C; break;
3859 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3860 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3861 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3862 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
John McCall477f2bb2016-03-03 06:39:32 +00003863 case AttributeList::AT_SwiftCall: CC = CC_Swift; break;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003864 case AttributeList::AT_VectorCall: CC = CC_X86VectorCall; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003865 case AttributeList::AT_MSABI:
3866 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3867 CC_X86_64Win64;
3868 break;
3869 case AttributeList::AT_SysVABI:
3870 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3871 CC_C;
3872 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003873 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003874 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003875 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003876 attr.setInvalid();
3877 return true;
3878 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003879 if (StrRef == "aapcs") {
3880 CC = CC_AAPCS;
3881 break;
3882 } else if (StrRef == "aapcs-vfp") {
3883 CC = CC_AAPCS_VFP;
3884 break;
3885 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003886
3887 attr.setInvalid();
3888 Diag(attr.getLoc(), diag::err_invalid_pcs);
3889 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003890 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00003891 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00003892 case AttributeList::AT_PreserveMost: CC = CC_PreserveMost; break;
3893 case AttributeList::AT_PreserveAll: CC = CC_PreserveAll; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003894 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003895 }
3896
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003897 const TargetInfo &TI = Context.getTargetInfo();
3898 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
Reid Kleckner9fde2e02015-02-26 19:43:46 +00003899 if (A != TargetInfo::CCCR_OK) {
3900 if (A == TargetInfo::CCCR_Warning)
3901 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003902
Reid Kleckner9fde2e02015-02-26 19:43:46 +00003903 // This convention is not valid for the target. Use the default function or
3904 // method calling convention.
Alexey Bataeva7547182016-05-18 09:06:38 +00003905 bool IsCXXMethod = false, IsVariadic = false;
3906 if (FD) {
3907 IsCXXMethod = FD->isCXXInstanceMember();
3908 IsVariadic = FD->isVariadic();
3909 }
3910 CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003911 }
3912
John McCall3b5a8f52016-03-03 00:10:03 +00003913 attr.setProcessingCache((unsigned) CC);
John McCall3882ace2011-01-05 12:14:39 +00003914 return false;
3915}
3916
John McCall477f2bb2016-03-03 06:39:32 +00003917/// Pointer-like types in the default address space.
3918static bool isValidSwiftContextType(QualType type) {
3919 if (!type->hasPointerRepresentation())
3920 return type->isDependentType();
3921 return type->getPointeeType().getAddressSpace() == 0;
3922}
3923
3924/// Pointers and references in the default address space.
3925static bool isValidSwiftIndirectResultType(QualType type) {
3926 if (auto ptrType = type->getAs<PointerType>()) {
3927 type = ptrType->getPointeeType();
3928 } else if (auto refType = type->getAs<ReferenceType>()) {
3929 type = refType->getPointeeType();
3930 } else {
3931 return type->isDependentType();
3932 }
3933 return type.getAddressSpace() == 0;
3934}
3935
3936/// Pointers and references to pointers in the default address space.
3937static bool isValidSwiftErrorResultType(QualType type) {
3938 if (auto ptrType = type->getAs<PointerType>()) {
3939 type = ptrType->getPointeeType();
3940 } else if (auto refType = type->getAs<ReferenceType>()) {
3941 type = refType->getPointeeType();
3942 } else {
3943 return type->isDependentType();
3944 }
3945 if (!type.getQualifiers().empty())
3946 return false;
3947 return isValidSwiftContextType(type);
3948}
3949
3950static void handleParameterABIAttr(Sema &S, Decl *D, const AttributeList &attr,
3951 ParameterABI abi) {
3952 S.AddParameterABIAttr(attr.getRange(), D, abi,
3953 attr.getAttributeSpellingListIndex());
3954}
3955
3956void Sema::AddParameterABIAttr(SourceRange range, Decl *D, ParameterABI abi,
3957 unsigned spellingIndex) {
3958
3959 QualType type = cast<ParmVarDecl>(D)->getType();
3960
3961 if (auto existingAttr = D->getAttr<ParameterABIAttr>()) {
3962 if (existingAttr->getABI() != abi) {
3963 Diag(range.getBegin(), diag::err_attributes_are_not_compatible)
3964 << getParameterABISpelling(abi) << existingAttr;
3965 Diag(existingAttr->getLocation(), diag::note_conflicting_attribute);
3966 return;
3967 }
3968 }
3969
3970 switch (abi) {
3971 case ParameterABI::Ordinary:
3972 llvm_unreachable("explicit attribute for ordinary parameter ABI?");
3973
3974 case ParameterABI::SwiftContext:
3975 if (!isValidSwiftContextType(type)) {
3976 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
3977 << getParameterABISpelling(abi)
3978 << /*pointer to pointer */ 0 << type;
3979 }
3980 D->addAttr(::new (Context)
3981 SwiftContextAttr(range, Context, spellingIndex));
3982 return;
3983
3984 case ParameterABI::SwiftErrorResult:
3985 if (!isValidSwiftErrorResultType(type)) {
3986 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
3987 << getParameterABISpelling(abi)
3988 << /*pointer to pointer */ 1 << type;
3989 }
3990 D->addAttr(::new (Context)
3991 SwiftErrorResultAttr(range, Context, spellingIndex));
3992 return;
3993
3994 case ParameterABI::SwiftIndirectResult:
3995 if (!isValidSwiftIndirectResultType(type)) {
3996 Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type)
3997 << getParameterABISpelling(abi)
3998 << /*pointer*/ 0 << type;
3999 }
4000 D->addAttr(::new (Context)
4001 SwiftIndirectResultAttr(range, Context, spellingIndex));
4002 return;
4003 }
4004 llvm_unreachable("bad parameter ABI attribute");
4005}
4006
John McCall3882ace2011-01-05 12:14:39 +00004007/// Checks a regparm attribute, returning true if it is ill-formed and
4008/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004009bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
4010 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00004011 return true;
4012
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00004013 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004014 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004015 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004016 }
Eli Friedman7044b762009-03-27 21:06:47 +00004017
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004018 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00004019 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004020 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004021 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004022 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004023 }
4024
Douglas Gregore8bbc122011-09-02 00:18:52 +00004025 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004026 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00004027 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004028 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004029 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004030 }
4031
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004032 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00004033 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004034 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00004035 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004036 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00004037 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004038 }
4039
John McCall3882ace2011-01-05 12:14:39 +00004040 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004041}
4042
Artem Belevichbcec9da2016-06-06 22:54:57 +00004043// Checks whether an argument of launch_bounds attribute is
4044// acceptable, performs implicit conversion to Rvalue, and returns
4045// non-nullptr Expr result on success. Otherwise, it returns nullptr
4046// and may output an error.
4047static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E,
4048 const CUDALaunchBoundsAttr &Attr,
4049 const unsigned Idx) {
Artem Belevich7093e402015-04-21 22:55:54 +00004050 if (S.DiagnoseUnexpandedParameterPack(E))
Artem Belevichbcec9da2016-06-06 22:54:57 +00004051 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004052
4053 // Accept template arguments for now as they depend on something else.
4054 // We'll get to check them when they eventually get instantiated.
4055 if (E->isValueDependent())
Artem Belevichbcec9da2016-06-06 22:54:57 +00004056 return E;
Artem Belevich7093e402015-04-21 22:55:54 +00004057
4058 llvm::APSInt I(64);
4059 if (!E->isIntegerConstantExpr(I, S.Context)) {
4060 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
4061 << &Attr << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
Artem Belevichbcec9da2016-06-06 22:54:57 +00004062 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004063 }
4064 // Make sure we can fit it in 32 bits.
4065 if (!I.isIntN(32)) {
4066 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
4067 << 32 << /* Unsigned */ 1;
Artem Belevichbcec9da2016-06-06 22:54:57 +00004068 return nullptr;
Artem Belevich7093e402015-04-21 22:55:54 +00004069 }
4070 if (I < 0)
4071 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
4072 << &Attr << Idx << E->getSourceRange();
4073
Artem Belevichbcec9da2016-06-06 22:54:57 +00004074 // We may need to perform implicit conversion of the argument.
4075 InitializedEntity Entity = InitializedEntity::InitializeParameter(
4076 S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false);
4077 ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E);
4078 assert(!ValArg.isInvalid() &&
4079 "Unexpected PerformCopyInitialization() failure.");
4080
4081 return ValArg.getAs<Expr>();
Artem Belevich7093e402015-04-21 22:55:54 +00004082}
4083
4084void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads,
4085 Expr *MinBlocks, unsigned SpellingListIndex) {
4086 CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks,
4087 SpellingListIndex);
Artem Belevichbcec9da2016-06-06 22:54:57 +00004088 MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0);
4089 if (MaxThreads == nullptr)
Aaron Ballman3aff6332013-12-02 19:30:36 +00004090 return;
4091
Artem Belevichbcec9da2016-06-06 22:54:57 +00004092 if (MinBlocks) {
4093 MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1);
4094 if (MinBlocks == nullptr)
4095 return;
4096 }
Artem Belevich7093e402015-04-21 22:55:54 +00004097
4098 D->addAttr(::new (Context) CUDALaunchBoundsAttr(
4099 AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex));
4100}
4101
4102static void handleLaunchBoundsAttr(Sema &S, Decl *D,
4103 const AttributeList &Attr) {
4104 if (!checkAttributeAtLeastNumArgs(S, Attr, 1) ||
4105 !checkAttributeAtMostNumArgs(S, Attr, 2))
4106 return;
4107
4108 S.AddLaunchBoundsAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
4109 Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr,
4110 Attr.getAttributeSpellingListIndex());
Peter Collingbourne827301e2010-12-12 23:03:07 +00004111}
4112
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004113static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4114 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00004115 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00004116 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00004117 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004118 return;
4119 }
Aaron Ballman00e99962013-08-31 01:11:41 +00004120
4121 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004122 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004123
Aaron Ballman00e99962013-08-31 01:11:41 +00004124 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004125
4126 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4127 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4128 << Attr.getName() << ExpectedFunctionOrMethod;
4129 return;
4130 }
4131
4132 uint64_t ArgumentIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00004133 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
4134 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004135 return;
4136
4137 uint64_t TypeTagIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00004138 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
4139 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004140 return;
4141
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00004142 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004143 if (IsPointer) {
4144 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00004145 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004146 if (!BufferTy->isPointerType()) {
4147 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00004148 << Attr.getName() << 0;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004149 }
4150 }
4151
Michael Han99315932013-01-24 16:46:58 +00004152 D->addAttr(::new (S.Context)
4153 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4154 ArgumentIdx, TypeTagIdx, IsPointer,
4155 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004156}
4157
4158static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4159 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00004160 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00004161 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00004162 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004163 return;
4164 }
Aaron Ballman00e99962013-08-31 01:11:41 +00004165
4166 if (!checkAttributeNumArgs(S, Attr, 1))
4167 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004168
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004169 if (!isa<VarDecl>(D)) {
4170 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4171 << Attr.getName() << ExpectedVariable;
4172 return;
4173 }
4174
Aaron Ballman00e99962013-08-31 01:11:41 +00004175 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00004176 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00004177 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
4178 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004179
Michael Han99315932013-01-24 16:46:58 +00004180 D->addAttr(::new (S.Context)
4181 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00004182 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00004183 Attr.getLayoutCompatible(),
4184 Attr.getMustBeNull(),
4185 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004186}
4187
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004188//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004189// Checker-specific attribute handlers.
4190//===----------------------------------------------------------------------===//
4191
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00004192static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) {
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004193 return type->isDependentType() ||
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00004194 type->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004195}
4196
John McCalled433932011-01-25 03:31:58 +00004197static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004198 return type->isDependentType() ||
4199 type->isObjCObjectPointerType() ||
4200 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00004201}
Eugene Zelenko1ced5092016-02-12 22:53:10 +00004202
John McCalled433932011-01-25 03:31:58 +00004203static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004204 return type->isDependentType() ||
4205 type->isPointerType() ||
4206 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00004207}
4208
Chandler Carruthedc2c642011-07-02 00:01:44 +00004209static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John McCall3b5a8f52016-03-03 00:10:03 +00004210 S.AddNSConsumedAttr(Attr.getRange(), D, Attr.getAttributeSpellingListIndex(),
4211 Attr.getKind() == AttributeList::AT_NSConsumed,
4212 /*template instantiation*/ false);
4213}
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004214
John McCall3b5a8f52016-03-03 00:10:03 +00004215void Sema::AddNSConsumedAttr(SourceRange attrRange, Decl *D,
4216 unsigned spellingIndex, bool isNSConsumed,
4217 bool isTemplateInstantiation) {
4218 ParmVarDecl *param = cast<ParmVarDecl>(D);
4219 bool typeOK;
4220
4221 if (isNSConsumed) {
4222 typeOK = isValidSubjectOfNSAttribute(*this, param->getType());
John McCalled433932011-01-25 03:31:58 +00004223 } else {
John McCall3b5a8f52016-03-03 00:10:03 +00004224 typeOK = isValidSubjectOfCFAttribute(*this, param->getType());
John McCalled433932011-01-25 03:31:58 +00004225 }
4226
4227 if (!typeOK) {
John McCall3b5a8f52016-03-03 00:10:03 +00004228 // These attributes are normally just advisory, but in ARC, ns_consumed
4229 // is significant. Allow non-dependent code to contain inappropriate
4230 // attributes even in ARC, but require template instantiations to be
4231 // set up correctly.
4232 Diag(D->getLocStart(),
4233 (isTemplateInstantiation && isNSConsumed &&
4234 getLangOpts().ObjCAutoRefCount
4235 ? diag::err_ns_attribute_wrong_parameter_type
4236 : diag::warn_ns_attribute_wrong_parameter_type))
4237 << attrRange
4238 << (isNSConsumed ? "ns_consumed" : "cf_consumed")
4239 << (isNSConsumed ? /*objc pointers*/ 0 : /*cf pointers*/ 1);
John McCalled433932011-01-25 03:31:58 +00004240 return;
4241 }
4242
John McCall3b5a8f52016-03-03 00:10:03 +00004243 if (isNSConsumed)
4244 param->addAttr(::new (Context)
4245 NSConsumedAttr(attrRange, Context, spellingIndex));
John McCalled433932011-01-25 03:31:58 +00004246 else
John McCall3b5a8f52016-03-03 00:10:03 +00004247 param->addAttr(::new (Context)
4248 CFConsumedAttr(attrRange, Context, spellingIndex));
John McCalled433932011-01-25 03:31:58 +00004249}
4250
Chandler Carruthedc2c642011-07-02 00:01:44 +00004251static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4252 const AttributeList &Attr) {
John McCalled433932011-01-25 03:31:58 +00004253 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004254
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004255 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004256 returnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004257 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004258 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004259 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00004260 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4261 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004262 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004263 returnType = FD->getReturnType();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004264 else if (auto *Param = dyn_cast<ParmVarDecl>(D)) {
4265 returnType = Param->getType()->getPointeeType();
4266 if (returnType.isNull()) {
4267 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
4268 << Attr.getName() << /*pointer-to-CF*/2
4269 << Attr.getRange();
4270 return;
4271 }
4272 } else {
4273 AttributeDeclKind ExpectedDeclKind;
4274 switch (Attr.getKind()) {
4275 default: llvm_unreachable("invalid ownership attribute");
4276 case AttributeList::AT_NSReturnsRetained:
4277 case AttributeList::AT_NSReturnsAutoreleased:
4278 case AttributeList::AT_NSReturnsNotRetained:
4279 ExpectedDeclKind = ExpectedFunctionOrMethod;
4280 break;
4281
4282 case AttributeList::AT_CFReturnsRetained:
4283 case AttributeList::AT_CFReturnsNotRetained:
4284 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
4285 break;
4286 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004287 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004288 << Attr.getRange() << Attr.getName() << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004289 return;
4290 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004291
John McCalled433932011-01-25 03:31:58 +00004292 bool typeOK;
4293 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004294 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004295 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004296 case AttributeList::AT_NSReturnsRetained:
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00004297 typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType);
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004298 cf = false;
4299 break;
4300
4301 case AttributeList::AT_NSReturnsAutoreleased:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004302 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004303 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4304 cf = false;
4305 break;
4306
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004307 case AttributeList::AT_CFReturnsRetained:
4308 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004309 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4310 cf = true;
4311 break;
4312 }
4313
4314 if (!typeOK) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004315 if (isa<ParmVarDecl>(D)) {
4316 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
4317 << Attr.getName() << /*pointer-to-CF*/2
4318 << Attr.getRange();
4319 } else {
4320 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
4321 enum : unsigned {
4322 Function,
4323 Method,
4324 Property
4325 } SubjectKind = Function;
4326 if (isa<ObjCMethodDecl>(D))
4327 SubjectKind = Method;
4328 else if (isa<ObjCPropertyDecl>(D))
4329 SubjectKind = Property;
4330 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4331 << Attr.getName() << SubjectKind << cf
4332 << Attr.getRange();
4333 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004334 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004335 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004336
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004337 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004338 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004339 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004340 case AttributeList::AT_NSReturnsAutoreleased:
Nico Weber462fd1e2015-01-07 23:50:05 +00004341 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(
4342 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004343 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004344 case AttributeList::AT_CFReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004345 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(
4346 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004347 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004348 case AttributeList::AT_NSReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004349 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(
4350 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004351 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004352 case AttributeList::AT_CFReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004353 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(
4354 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004355 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004356 case AttributeList::AT_NSReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004357 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(
4358 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004359 return;
4360 };
4361}
4362
John McCallcf166702011-07-22 08:53:00 +00004363static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4364 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00004365 const int EP_ObjCMethod = 1;
4366 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004367
John McCallcf166702011-07-22 08:53:00 +00004368 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004369 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004370 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004371 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004372 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004373 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00004374
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004375 if (!resultType->isReferenceType() &&
4376 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004377 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00004378 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004379 << attr.getName()
4380 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004381 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00004382
4383 // Drop the attribute.
4384 return;
4385 }
4386
Nico Weber462fd1e2015-01-07 23:50:05 +00004387 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(
4388 attr.getRange(), S.Context, attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004389}
4390
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004391static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4392 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004393 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004394
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004395 DeclContext *DC = method->getDeclContext();
4396 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4397 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4398 << attr.getName() << 0;
4399 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4400 return;
4401 }
4402 if (method->getMethodFamily() == OMF_dealloc) {
4403 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4404 << attr.getName() << 1;
4405 return;
4406 }
4407
Michael Han99315932013-01-24 16:46:58 +00004408 method->addAttr(::new (S.Context)
4409 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4410 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004411}
4412
Aaron Ballmanfb763042013-12-02 18:05:46 +00004413static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
4414 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004415 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr.getRange(),
4416 Attr.getName()))
John McCall32f5fe12011-09-30 05:12:12 +00004417 return;
John McCall32f5fe12011-09-30 05:12:12 +00004418
Aaron Ballmanfb763042013-12-02 18:05:46 +00004419 D->addAttr(::new (S.Context)
4420 CFAuditedTransferAttr(Attr.getRange(), S.Context,
4421 Attr.getAttributeSpellingListIndex()));
4422}
4423
4424static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
4425 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004426 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr.getRange(),
4427 Attr.getName()))
Aaron Ballmanfb763042013-12-02 18:05:46 +00004428 return;
4429
4430 D->addAttr(::new (S.Context)
4431 CFUnknownTransferAttr(Attr.getRange(), S.Context,
4432 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004433}
4434
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004435static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
4436 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004437 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004438
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004439 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004440 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004441 return;
4442 }
John McCall28592582015-02-01 22:34:06 +00004443
4444 // Typedefs only allow objc_bridge(id) and have some additional checking.
4445 if (auto TD = dyn_cast<TypedefNameDecl>(D)) {
4446 if (!Parm->Ident->isStr("id")) {
4447 S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_id)
4448 << Attr.getName();
4449 return;
4450 }
4451
4452 // Only allow 'cv void *'.
4453 QualType T = TD->getUnderlyingType();
4454 if (!T->isVoidPointerType()) {
4455 S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
4456 return;
4457 }
4458 }
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004459
4460 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004461 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004462 Attr.getAttributeSpellingListIndex()));
4463}
4464
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004465static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
4466 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004467 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
4468
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004469 if (!Parm) {
4470 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
4471 return;
4472 }
4473
4474 D->addAttr(::new (S.Context)
4475 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
4476 Attr.getAttributeSpellingListIndex()));
4477}
4478
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004479static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
4480 const AttributeList &Attr) {
4481 IdentifierInfo *RelatedClass =
Craig Topperc3ec1492014-05-26 06:22:03 +00004482 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004483 if (!RelatedClass) {
4484 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
4485 return;
4486 }
4487 IdentifierInfo *ClassMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00004488 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004489 IdentifierInfo *InstanceMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00004490 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004491 D->addAttr(::new (S.Context)
4492 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
4493 ClassMethod, InstanceMethod,
4494 Attr.getAttributeSpellingListIndex()));
4495}
4496
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004497static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
4498 const AttributeList &Attr) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004499 ObjCInterfaceDecl *IFace;
Nico Weber462fd1e2015-01-07 23:50:05 +00004500 if (ObjCCategoryDecl *CatDecl =
4501 dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004502 IFace = CatDecl->getClassInterface();
4503 else
4504 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00004505
4506 if (!IFace)
4507 return;
4508
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00004509 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00004510 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004511 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
4512 Attr.getAttributeSpellingListIndex()));
4513}
4514
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004515static void handleObjCRuntimeName(Sema &S, Decl *D,
4516 const AttributeList &Attr) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004517 StringRef MetaDataName;
4518 if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName))
4519 return;
4520 D->addAttr(::new (S.Context)
4521 ObjCRuntimeNameAttr(Attr.getRange(), S.Context,
4522 MetaDataName,
4523 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004524}
4525
Nico Webera6916892016-06-10 18:53:04 +00004526// When a user wants to use objc_boxable with a union or struct
4527// but they don't have access to the declaration (legacy/third-party code)
4528// then they can 'enable' this feature with a typedef:
Alex Denisovfde64952015-06-26 05:28:36 +00004529// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
4530static void handleObjCBoxable(Sema &S, Decl *D, const AttributeList &Attr) {
4531 bool notify = false;
4532
4533 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4534 if (RD && RD->getDefinition()) {
4535 RD = RD->getDefinition();
4536 notify = true;
4537 }
4538
4539 if (RD) {
4540 ObjCBoxableAttr *BoxableAttr = ::new (S.Context)
4541 ObjCBoxableAttr(Attr.getRange(), S.Context,
4542 Attr.getAttributeSpellingListIndex());
4543 RD->addAttr(BoxableAttr);
4544 if (notify) {
4545 // we need to notify ASTReader/ASTWriter about
4546 // modification of existing declaration
4547 if (ASTMutationListener *L = S.getASTMutationListener())
4548 L->AddedAttributeToRecord(BoxableAttr, RD);
4549 }
4550 }
4551}
4552
Chandler Carruthedc2c642011-07-02 00:01:44 +00004553static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4554 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004555 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004556
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004557 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004558 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004559}
4560
Chandler Carruthedc2c642011-07-02 00:01:44 +00004561static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4562 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004563 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004564 QualType type = vd->getType();
4565
4566 if (!type->isDependentType() &&
4567 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004568 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004569 << type;
4570 return;
4571 }
4572
4573 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4574
4575 // If we have no lifetime yet, check the lifetime we're presumably
4576 // going to infer.
4577 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4578 lifetime = type->getObjCARCImplicitLifetime();
4579
4580 switch (lifetime) {
4581 case Qualifiers::OCL_None:
4582 assert(type->isDependentType() &&
4583 "didn't infer lifetime for non-dependent type?");
4584 break;
4585
4586 case Qualifiers::OCL_Weak: // meaningful
4587 case Qualifiers::OCL_Strong: // meaningful
4588 break;
4589
4590 case Qualifiers::OCL_ExplicitNone:
4591 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004592 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004593 << (lifetime == Qualifiers::OCL_Autoreleasing);
4594 break;
4595 }
4596
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004597 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004598 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4599 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004600}
4601
Francois Picheta83957a2010-12-19 06:50:37 +00004602//===----------------------------------------------------------------------===//
4603// Microsoft specific attribute handlers.
4604//===----------------------------------------------------------------------===//
4605
Chandler Carruthedc2c642011-07-02 00:01:44 +00004606static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00004607 if (!S.LangOpts.CPlusPlus) {
4608 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4609 << Attr.getName() << AttributeLangSupport::C;
4610 return;
4611 }
4612
Aaron Ballman60e705e2013-11-24 20:58:02 +00004613 if (!isa<CXXRecordDecl>(D)) {
4614 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4615 << Attr.getName() << ExpectedClass;
4616 return;
4617 }
4618
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004619 StringRef StrRef;
4620 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00004621 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00004622 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004623
David Majnemer89085342013-08-09 08:56:20 +00004624 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4625 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00004626 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4627 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00004628
Reid Kleckner140c4a72013-05-17 14:04:52 +00004629 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00004630 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004631 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004632 return;
4633 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004634
David Majnemer89085342013-08-09 08:56:20 +00004635 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004636 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00004637 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004638 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00004639 return;
4640 }
David Majnemer89085342013-08-09 08:56:20 +00004641 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004642 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004643 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004644 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004645 }
Francois Picheta83957a2010-12-19 06:50:37 +00004646
David Majnemer89085342013-08-09 08:56:20 +00004647 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4648 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004649}
4650
David Majnemer2c4e00a2014-01-29 22:07:36 +00004651static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4652 if (!S.LangOpts.CPlusPlus) {
4653 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4654 << Attr.getName() << AttributeLangSupport::C;
4655 return;
4656 }
4657 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
David Majnemer4bb09802014-02-10 19:50:15 +00004658 D, Attr.getRange(), /*BestCase=*/true,
4659 Attr.getAttributeSpellingListIndex(),
David Majnemer2c4e00a2014-01-29 22:07:36 +00004660 (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling());
David Majnemer929025d2016-01-26 19:30:26 +00004661 if (IA) {
David Majnemer2c4e00a2014-01-29 22:07:36 +00004662 D->addAttr(IA);
David Majnemer929025d2016-01-26 19:30:26 +00004663 S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D));
4664 }
David Majnemer2c4e00a2014-01-29 22:07:36 +00004665}
4666
Reid Kleckner7d6d2702014-05-01 03:16:47 +00004667static void handleDeclspecThreadAttr(Sema &S, Decl *D,
4668 const AttributeList &Attr) {
4669 VarDecl *VD = cast<VarDecl>(D);
4670 if (!S.Context.getTargetInfo().isTLSSupported()) {
4671 S.Diag(Attr.getLoc(), diag::err_thread_unsupported);
4672 return;
4673 }
4674 if (VD->getTSCSpec() != TSCS_unspecified) {
4675 S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable);
4676 return;
4677 }
4678 if (VD->hasLocalStorage()) {
4679 S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
4680 return;
4681 }
4682 VD->addAttr(::new (S.Context) ThreadAttr(
4683 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
4684}
4685
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00004686static void handleAbiTagAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4687 SmallVector<StringRef, 4> Tags;
4688 for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) {
4689 StringRef Tag;
4690 if (!S.checkStringLiteralArgumentAttr(Attr, I, Tag))
4691 return;
4692 Tags.push_back(Tag);
4693 }
4694
4695 if (const auto *NS = dyn_cast<NamespaceDecl>(D)) {
4696 if (!NS->isInline()) {
4697 S.Diag(Attr.getLoc(), diag::warn_attr_abi_tag_namespace) << 0;
4698 return;
4699 }
4700 if (NS->isAnonymousNamespace()) {
4701 S.Diag(Attr.getLoc(), diag::warn_attr_abi_tag_namespace) << 1;
4702 return;
4703 }
4704 if (Attr.getNumArgs() == 0)
4705 Tags.push_back(NS->getName());
4706 } else if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
4707 return;
4708
4709 // Store tags sorted and without duplicates.
4710 std::sort(Tags.begin(), Tags.end());
4711 Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end());
4712
4713 D->addAttr(::new (S.Context)
4714 AbiTagAttr(Attr.getRange(), S.Context, Tags.data(), Tags.size(),
4715 Attr.getAttributeSpellingListIndex()));
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00004716}
4717
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004718static void handleARMInterruptAttr(Sema &S, Decl *D,
4719 const AttributeList &Attr) {
4720 // Check the attribute arguments.
4721 if (Attr.getNumArgs() > 1) {
4722 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
4723 << Attr.getName() << 1;
4724 return;
4725 }
4726
4727 StringRef Str;
4728 SourceLocation ArgLoc;
4729
4730 if (Attr.getNumArgs() == 0)
4731 Str = "";
4732 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
4733 return;
4734
4735 ARMInterruptAttr::InterruptType Kind;
4736 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
4737 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
4738 << Attr.getName() << Str << ArgLoc;
4739 return;
4740 }
4741
4742 unsigned Index = Attr.getAttributeSpellingListIndex();
4743 D->addAttr(::new (S.Context)
4744 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
4745}
4746
4747static void handleMSP430InterruptAttr(Sema &S, Decl *D,
4748 const AttributeList &Attr) {
4749 if (!checkAttributeNumArgs(S, Attr, 1))
4750 return;
4751
4752 if (!Attr.isArgExpr(0)) {
4753 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
4754 << AANT_ArgumentIntegerConstant;
4755 return;
4756 }
4757
4758 // FIXME: Check for decl - it should be void ()(void).
4759
4760 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4761 llvm::APSInt NumParams(32);
4762 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
4763 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4764 << Attr.getName() << AANT_ArgumentIntegerConstant
4765 << NumParamsExpr->getSourceRange();
4766 return;
4767 }
4768
4769 unsigned Num = NumParams.getLimitedValue(255);
4770 if ((Num & 1) || Num > 30) {
4771 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
4772 << Attr.getName() << (int)NumParams.getSExtValue()
4773 << NumParamsExpr->getSourceRange();
4774 return;
4775 }
4776
Aaron Ballman36a53502014-01-16 13:03:14 +00004777 D->addAttr(::new (S.Context)
4778 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
4779 Attr.getAttributeSpellingListIndex()));
4780 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004781}
4782
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00004783static void handleMipsInterruptAttr(Sema &S, Decl *D,
4784 const AttributeList &Attr) {
4785 // Only one optional argument permitted.
4786 if (Attr.getNumArgs() > 1) {
4787 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
4788 << Attr.getName() << 1;
4789 return;
4790 }
4791
4792 StringRef Str;
4793 SourceLocation ArgLoc;
4794
4795 if (Attr.getNumArgs() == 0)
4796 Str = "";
4797 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
4798 return;
4799
4800 // Semantic checks for a function with the 'interrupt' attribute for MIPS:
4801 // a) Must be a function.
4802 // b) Must have no parameters.
4803 // c) Must have the 'void' return type.
4804 // d) Cannot have the 'mips16' attribute, as that instruction set
4805 // lacks the 'eret' instruction.
4806 // e) The attribute itself must either have no argument or one of the
4807 // valid interrupt types, see [MipsInterruptDocs].
4808
4809 if (!isFunctionOrMethod(D)) {
4810 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
4811 << "'interrupt'" << ExpectedFunctionOrMethod;
4812 return;
4813 }
4814
4815 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
4816 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
4817 << 0;
4818 return;
4819 }
4820
4821 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
4822 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
4823 << 1;
4824 return;
4825 }
4826
4827 if (checkAttrMutualExclusion<Mips16Attr>(S, D, Attr.getRange(),
4828 Attr.getName()))
4829 return;
4830
4831 MipsInterruptAttr::InterruptType Kind;
4832 if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
4833 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
4834 << Attr.getName() << "'" + std::string(Str) + "'";
4835 return;
4836 }
4837
4838 D->addAttr(::new (S.Context) MipsInterruptAttr(
4839 Attr.getLoc(), S.Context, Kind, Attr.getAttributeSpellingListIndex()));
4840}
4841
Alexey Bataevd51e9932016-01-15 04:06:31 +00004842static void handleAnyX86InterruptAttr(Sema &S, Decl *D,
4843 const AttributeList &Attr) {
4844 // Semantic checks for a function with the 'interrupt' attribute.
4845 // a) Must be a function.
4846 // b) Must have the 'void' return type.
4847 // c) Must take 1 or 2 arguments.
4848 // d) The 1st argument must be a pointer.
4849 // e) The 2nd argument (if any) must be an unsigned integer.
4850 if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) ||
4851 CXXMethodDecl::isStaticOverloadedOperator(
4852 cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) {
4853 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4854 << Attr.getName() << ExpectedFunctionWithProtoType;
4855 return;
4856 }
4857 // Interrupt handler must have void return type.
4858 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
4859 S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(),
4860 diag::err_anyx86_interrupt_attribute)
4861 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
4862 ? 0
4863 : 1)
4864 << 0;
4865 return;
4866 }
4867 // Interrupt handler must have 1 or 2 parameters.
4868 unsigned NumParams = getFunctionOrMethodNumParams(D);
4869 if (NumParams < 1 || NumParams > 2) {
4870 S.Diag(D->getLocStart(), diag::err_anyx86_interrupt_attribute)
4871 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
4872 ? 0
4873 : 1)
4874 << 1;
4875 return;
4876 }
4877 // The first argument must be a pointer.
4878 if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) {
4879 S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(),
4880 diag::err_anyx86_interrupt_attribute)
4881 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
4882 ? 0
4883 : 1)
4884 << 2;
4885 return;
4886 }
4887 // The second argument, if present, must be an unsigned integer.
4888 unsigned TypeSize =
4889 S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64
4890 ? 64
4891 : 32;
4892 if (NumParams == 2 &&
4893 (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() ||
4894 S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) {
4895 S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(),
4896 diag::err_anyx86_interrupt_attribute)
4897 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
4898 ? 0
4899 : 1)
4900 << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false);
4901 return;
4902 }
4903 D->addAttr(::new (S.Context) AnyX86InterruptAttr(
4904 Attr.getLoc(), S.Context, Attr.getAttributeSpellingListIndex()));
4905 D->addAttr(UsedAttr::CreateImplicit(S.Context));
4906}
4907
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004908static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4909 // Dispatch the interrupt attribute based on the current target.
Alexey Bataevd51e9932016-01-15 04:06:31 +00004910 switch (S.Context.getTargetInfo().getTriple().getArch()) {
4911 case llvm::Triple::msp430:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004912 handleMSP430InterruptAttr(S, D, Attr);
Alexey Bataevd51e9932016-01-15 04:06:31 +00004913 break;
4914 case llvm::Triple::mipsel:
4915 case llvm::Triple::mips:
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00004916 handleMipsInterruptAttr(S, D, Attr);
Alexey Bataevd51e9932016-01-15 04:06:31 +00004917 break;
4918 case llvm::Triple::x86:
4919 case llvm::Triple::x86_64:
4920 handleAnyX86InterruptAttr(S, D, Attr);
4921 break;
4922 default:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004923 handleARMInterruptAttr(S, D, Attr);
Alexey Bataevd51e9932016-01-15 04:06:31 +00004924 break;
4925 }
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004926}
4927
Matt Arsenault43fae6c2014-12-04 20:38:18 +00004928static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D,
4929 const AttributeList &Attr) {
4930 uint32_t NumRegs;
4931 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4932 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4933 return;
4934
4935 D->addAttr(::new (S.Context)
4936 AMDGPUNumVGPRAttr(Attr.getLoc(), S.Context,
4937 NumRegs,
4938 Attr.getAttributeSpellingListIndex()));
4939}
4940
4941static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D,
4942 const AttributeList &Attr) {
4943 uint32_t NumRegs;
4944 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4945 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4946 return;
4947
4948 D->addAttr(::new (S.Context)
4949 AMDGPUNumSGPRAttr(Attr.getLoc(), S.Context,
4950 NumRegs,
4951 Attr.getAttributeSpellingListIndex()));
4952}
4953
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004954static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
4955 const AttributeList& Attr) {
4956 // If we try to apply it to a function pointer, don't warn, but don't
4957 // do anything, either. It doesn't matter anyway, because there's nothing
4958 // special about calling a force_align_arg_pointer function.
4959 ValueDecl *VD = dyn_cast<ValueDecl>(D);
4960 if (VD && VD->getType()->isFunctionPointerType())
4961 return;
4962 // Also don't warn on function pointer typedefs.
4963 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
4964 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
4965 TD->getUnderlyingType()->isFunctionType()))
4966 return;
4967 // Attribute can only be applied to function types.
4968 if (!isa<FunctionDecl>(D)) {
4969 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4970 << Attr.getName() << /* function */0;
4971 return;
4972 }
4973
Aaron Ballman36a53502014-01-16 13:03:14 +00004974 D->addAttr(::new (S.Context)
4975 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
4976 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004977}
4978
David Majnemercd3ebfe2016-05-23 17:16:12 +00004979static void handleLayoutVersion(Sema &S, Decl *D, const AttributeList &Attr) {
4980 uint32_t Version;
4981 Expr *VersionExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4982 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), Version))
4983 return;
4984
4985 // TODO: Investigate what happens with the next major version of MSVC.
4986 if (Version != LangOptions::MSVC2015) {
4987 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
4988 << Attr.getName() << Version << VersionExpr->getSourceRange();
4989 return;
4990 }
4991
4992 D->addAttr(::new (S.Context)
4993 LayoutVersionAttr(Attr.getRange(), S.Context, Version,
4994 Attr.getAttributeSpellingListIndex()));
4995}
4996
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004997DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
4998 unsigned AttrSpellingListIndex) {
4999 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00005000 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00005001 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005002 }
5003
5004 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00005005 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005006
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00005007 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005008}
5009
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005010DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
5011 unsigned AttrSpellingListIndex) {
5012 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00005013 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005014 D->dropAttr<DLLImportAttr>();
5015 }
5016
5017 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00005018 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005019
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00005020 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005021}
5022
Hans Wennborge82f19c2014-06-24 23:57:05 +00005023static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00005024 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
5025 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
5026 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored)
5027 << A.getName();
5028 return;
5029 }
5030
Hans Wennborg606bd6d2014-11-03 14:24:45 +00005031 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5032 if (FD->isInlined() && A.getKind() == AttributeList::AT_DLLImport &&
5033 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
5034 // MinGW doesn't allow dllimport on inline functions.
5035 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
5036 << A.getName();
5037 return;
5038 }
5039 }
5040
Hans Wennborg5869ec42015-09-15 21:05:30 +00005041 if (auto *MD = dyn_cast<CXXMethodDecl>(D)) {
5042 if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
5043 MD->getParent()->isLambda()) {
5044 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A.getName();
5045 return;
5046 }
5047 }
5048
Hans Wennborge82f19c2014-06-24 23:57:05 +00005049 unsigned Index = A.getAttributeSpellingListIndex();
5050 Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport
5051 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
5052 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005053 if (NewAttr)
5054 D->addAttr(NewAttr);
5055}
5056
David Majnemer2c4e00a2014-01-29 22:07:36 +00005057MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00005058Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00005059 unsigned AttrSpellingListIndex,
5060 MSInheritanceAttr::Spelling SemanticSpelling) {
5061 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
5062 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00005063 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005064 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
5065 << 1 /*previous declaration*/;
5066 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
5067 D->dropAttr<MSInheritanceAttr>();
5068 }
5069
5070 CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
5071 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00005072 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
5073 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00005074 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005075 }
5076 } else {
5077 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
5078 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
5079 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00005080 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005081 }
5082 if (RD->getDescribedClassTemplate()) {
5083 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
5084 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00005085 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00005086 }
5087 }
5088
5089 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00005090 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00005091}
5092
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005093static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
5094 // The capability attributes take a single string parameter for the name of
5095 // the capability they represent. The lockable attribute does not take any
5096 // parameters. However, semantically, both attributes represent the same
5097 // concept, and so they use the same semantic attribute. Eventually, the
5098 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00005099 //
Alp Toker958027b2014-07-14 19:42:55 +00005100 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00005101 // literal will be considered a "mutex."
5102 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005103 SourceLocation LiteralLoc;
5104 if (Attr.getKind() == AttributeList::AT_Capability &&
5105 !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc))
5106 return;
5107
Aaron Ballman6c810072014-03-05 21:47:13 +00005108 // Currently, there are only two names allowed for a capability: role and
5109 // mutex (case insensitive). Diagnose other capability names.
5110 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
5111 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
5112
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005113 D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N,
5114 Attr.getAttributeSpellingListIndex()));
5115}
5116
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005117static void handleAssertCapabilityAttr(Sema &S, Decl *D,
5118 const AttributeList &Attr) {
5119 D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context,
5120 Attr.getArgAsExpr(0),
5121 Attr.getAttributeSpellingListIndex()));
5122}
5123
5124static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
5125 const AttributeList &Attr) {
5126 SmallVector<Expr*, 1> Args;
Aaron Ballman18d85ae2014-03-20 16:02:49 +00005127 if (!checkLockFunAttrCommon(S, D, Attr, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005128 return;
5129
5130 D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(),
5131 S.Context,
5132 Args.data(), Args.size(),
5133 Attr.getAttributeSpellingListIndex()));
5134}
5135
5136static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
5137 const AttributeList &Attr) {
5138 SmallVector<Expr*, 2> Args;
5139 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
5140 return;
5141
5142 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(),
5143 S.Context,
5144 Attr.getArgAsExpr(0),
5145 Args.data(),
5146 Args.size(),
5147 Attr.getAttributeSpellingListIndex()));
5148}
5149
5150static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
5151 const AttributeList &Attr) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005152 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00005153 SmallVector<Expr *, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00005154 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005155
Aaron Ballman18d85ae2014-03-20 16:02:49 +00005156 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
5157 Attr.getRange(), S.Context, Args.data(), Args.size(),
5158 Attr.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005159}
5160
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005161static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
5162 const AttributeList &Attr) {
5163 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
5164 return;
5165
5166 // check that all arguments are lockable objects
5167 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00005168 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005169 if (Args.empty())
5170 return;
5171
5172 RequiresCapabilityAttr *RCA = ::new (S.Context)
5173 RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(),
5174 Args.size(), Attr.getAttributeSpellingListIndex());
5175
5176 D->addAttr(RCA);
5177}
5178
Aaron Ballman43f40102014-11-14 22:34:56 +00005179static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
5180 if (auto *NSD = dyn_cast<NamespaceDecl>(D)) {
5181 if (NSD->isAnonymousNamespace()) {
5182 S.Diag(Attr.getLoc(), diag::warn_deprecated_anonymous_namespace);
5183 // Do not want to attach the attribute to the namespace because that will
5184 // cause confusing diagnostic reports for uses of declarations within the
5185 // namespace.
5186 return;
5187 }
5188 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00005189
Manman Renc7890fe2016-03-16 18:50:49 +00005190 // Handle the cases where the attribute has a text message.
5191 StringRef Str, Replacement;
5192 if (Attr.isArgExpr(0) && Attr.getArgAsExpr(0) &&
5193 !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
5194 return;
5195
5196 // Only support a single optional message for Declspec and CXX11.
5197 if (Attr.isDeclspecAttribute() || Attr.isCXX11Attribute())
5198 checkAttributeAtMostNumArgs(S, Attr, 1);
5199 else if (Attr.isArgExpr(1) && Attr.getArgAsExpr(1) &&
5200 !S.checkStringLiteralArgumentAttr(Attr, 1, Replacement))
5201 return;
5202
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00005203 if (!S.getLangOpts().CPlusPlus14)
5204 if (Attr.isCXX11Attribute() &&
5205 !(Attr.hasScope() && Attr.getScopeName()->isStr("gnu")))
Richard Smith4f902c72016-03-08 00:32:55 +00005206 S.Diag(Attr.getLoc(), diag::ext_cxx14_attr) << Attr.getName();
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00005207
Manman Renc7890fe2016-03-16 18:50:49 +00005208 D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getRange(), S.Context, Str,
5209 Replacement,
5210 Attr.getAttributeSpellingListIndex()));
Aaron Ballman43f40102014-11-14 22:34:56 +00005211}
5212
Peter Collingbourne915df992015-05-15 18:33:32 +00005213static void handleNoSanitizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
5214 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
5215 return;
5216
Benjamin Kramer1b582012016-02-13 18:11:49 +00005217 std::vector<StringRef> Sanitizers;
Peter Collingbourne915df992015-05-15 18:33:32 +00005218
5219 for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) {
5220 StringRef SanitizerName;
5221 SourceLocation LiteralLoc;
5222
5223 if (!S.checkStringLiteralArgumentAttr(Attr, I, SanitizerName, &LiteralLoc))
5224 return;
5225
5226 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0)
5227 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
5228
5229 Sanitizers.push_back(SanitizerName);
5230 }
5231
5232 D->addAttr(::new (S.Context) NoSanitizeAttr(
5233 Attr.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(),
5234 Attr.getAttributeSpellingListIndex()));
5235}
5236
5237static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
5238 const AttributeList &Attr) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00005239 StringRef AttrName = Attr.getName()->getName();
5240 normalizeName(AttrName);
Benjamin Kramer1b582012016-02-13 18:11:49 +00005241 StringRef SanitizerName =
5242 llvm::StringSwitch<StringRef>(AttrName)
Peter Collingbourne915df992015-05-15 18:33:32 +00005243 .Case("no_address_safety_analysis", "address")
5244 .Case("no_sanitize_address", "address")
5245 .Case("no_sanitize_thread", "thread")
Peter Collingbourne94410942015-05-15 20:11:18 +00005246 .Case("no_sanitize_memory", "memory");
Peter Collingbourne915df992015-05-15 18:33:32 +00005247 D->addAttr(::new (S.Context)
5248 NoSanitizeAttr(Attr.getRange(), S.Context, &SanitizerName, 1,
5249 Attr.getAttributeSpellingListIndex()));
5250}
5251
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005252static void handleInternalLinkageAttr(Sema &S, Decl *D,
5253 const AttributeList &Attr) {
5254 if (InternalLinkageAttr *Internal =
5255 S.mergeInternalLinkageAttr(D, Attr.getRange(), Attr.getName(),
5256 Attr.getAttributeSpellingListIndex()))
5257 D->addAttr(Internal);
5258}
5259
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +00005260static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const AttributeList &Attr) {
5261 if (S.LangOpts.OpenCLVersion != 200)
5262 S.Diag(Attr.getLoc(), diag::err_attribute_requires_opencl_version)
5263 << Attr.getName() << "2.0" << 0;
5264 else
5265 S.Diag(Attr.getLoc(), diag::warn_opencl_attr_deprecated_ignored)
5266 << Attr.getName() << "2.0";
5267}
5268
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005269/// Handles semantic checking for features that are common to all attributes,
5270/// such as checking whether a parameter was properly specified, or the correct
5271/// number of arguments were passed, etc.
5272static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
5273 const AttributeList &Attr) {
5274 // Several attributes carry different semantics than the parsing requires, so
5275 // those are opted out of the common handling.
5276 //
5277 // We also bail on unknown and ignored attributes because those are handled
5278 // as part of the target-specific handling logic.
5279 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005280 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005281 return false;
5282
Aaron Ballman3aff6332013-12-02 19:30:36 +00005283 // Check whether the attribute requires specific language extensions to be
5284 // enabled.
5285 if (!Attr.diagnoseLangOpts(S))
5286 return true;
5287
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00005288 if (Attr.getMinArgs() == Attr.getMaxArgs()) {
5289 // If there are no optional arguments, then checking for the argument count
5290 // is trivial.
5291 if (!checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
5292 return true;
5293 } else {
5294 // There are optional arguments, so checking is slightly more involved.
5295 if (Attr.getMinArgs() &&
5296 !checkAttributeAtLeastNumArgs(S, Attr, Attr.getMinArgs()))
5297 return true;
5298 else if (!Attr.hasVariadicArg() && Attr.getMaxArgs() &&
5299 !checkAttributeAtMostNumArgs(S, Attr, Attr.getMaxArgs()))
5300 return true;
5301 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00005302
5303 // Check whether the attribute appertains to the given subject.
5304 if (!Attr.diagnoseAppertainsTo(S, D))
5305 return true;
5306
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005307 return false;
5308}
5309
Xiuli Pan11e13f62016-02-26 03:13:03 +00005310static void handleOpenCLAccessAttr(Sema &S, Decl *D,
5311 const AttributeList &Attr) {
5312 if (D->isInvalidDecl())
5313 return;
5314
5315 // Check if there is only one access qualifier.
5316 if (D->hasAttr<OpenCLAccessAttr>()) {
5317 S.Diag(Attr.getLoc(), diag::err_opencl_multiple_access_qualifiers)
5318 << D->getSourceRange();
5319 D->setInvalidDecl(true);
5320 return;
5321 }
5322
5323 // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that an
5324 // image object can be read and written.
5325 // OpenCL v2.0 s6.13.6 - A kernel cannot read from and write to the same pipe
5326 // object. Using the read_write (or __read_write) qualifier with the pipe
5327 // qualifier is a compilation error.
5328 if (const ParmVarDecl *PDecl = dyn_cast<ParmVarDecl>(D)) {
5329 const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr();
5330 if (Attr.getName()->getName().find("read_write") != StringRef::npos) {
5331 if (S.getLangOpts().OpenCLVersion < 200 || DeclTy->isPipeType()) {
5332 S.Diag(Attr.getLoc(), diag::err_opencl_invalid_read_write)
5333 << Attr.getName() << PDecl->getType() << DeclTy->isImageType();
5334 D->setInvalidDecl(true);
5335 return;
5336 }
5337 }
5338 }
5339
5340 D->addAttr(::new (S.Context) OpenCLAccessAttr(
5341 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
5342}
5343
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005344//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005345// Top Level Sema Entry Points
5346//===----------------------------------------------------------------------===//
5347
Richard Smithf8a75c32013-08-29 00:47:48 +00005348/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
5349/// the attribute applies to decls. If the attribute is a type attribute, just
5350/// silently ignore it if a GNU attribute.
5351static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
5352 const AttributeList &Attr,
5353 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005354 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00005355 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00005356
Richard Smithf8a75c32013-08-29 00:47:48 +00005357 // Ignore C++11 attributes on declarator chunks: they appertain to the type
5358 // instead.
5359 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
5360 return;
5361
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005362 // Unknown attributes are automatically warned on. Target-specific attributes
5363 // which do not apply to the current target architecture are treated as
5364 // though they were unknown attributes.
5365 if (Attr.getKind() == AttributeList::UnknownAttribute ||
Bob Wilson7c730832015-07-20 22:57:31 +00005366 !Attr.existsInTarget(S.Context.getTargetInfo())) {
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005367 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute()
5368 ? diag::warn_unhandled_ms_attribute_ignored
5369 : diag::warn_unknown_attribute_ignored)
5370 << Attr.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005371 return;
5372 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005373
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005374 if (handleCommonAttributeFeatures(S, scope, D, Attr))
5375 return;
5376
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005377 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005378 default:
Richard Smith4f902c72016-03-08 00:32:55 +00005379 if (!Attr.isStmtAttr()) {
5380 // Type attributes are handled elsewhere; silently move on.
5381 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
5382 break;
5383 }
5384 S.Diag(Attr.getLoc(), diag::err_stmt_attribute_invalid_on_decl)
5385 << Attr.getName() << D->getLocation();
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005386 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005387 case AttributeList::AT_Interrupt:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005388 handleInterruptAttr(S, D, Attr);
5389 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005390 case AttributeList::AT_X86ForceAlignArgPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005391 handleX86ForceAlignArgPointerAttr(S, D, Attr);
5392 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005393 case AttributeList::AT_DLLExport:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005394 case AttributeList::AT_DLLImport:
Hans Wennborge82f19c2014-06-24 23:57:05 +00005395 handleDLLAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005396 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005397 case AttributeList::AT_Mips16:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005398 handleSimpleAttributeWithExclusions<Mips16Attr, MipsInterruptAttr>(S, D,
5399 Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005400 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005401 case AttributeList::AT_NoMips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005402 handleSimpleAttribute<NoMips16Attr>(S, D, Attr);
5403 break;
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005404 case AttributeList::AT_AMDGPUNumVGPR:
5405 handleAMDGPUNumVGPRAttr(S, D, Attr);
5406 break;
5407 case AttributeList::AT_AMDGPUNumSGPR:
5408 handleAMDGPUNumSGPRAttr(S, D, Attr);
5409 break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00005410 case AttributeList::AT_IBAction:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005411 handleSimpleAttribute<IBActionAttr>(S, D, Attr);
5412 break;
5413 case AttributeList::AT_IBOutlet:
5414 handleIBOutlet(S, D, Attr);
5415 break;
Richard Smith10876ef2013-01-17 01:30:42 +00005416 case AttributeList::AT_IBOutletCollection:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005417 handleIBOutletCollection(S, D, Attr);
5418 break;
Dmitry Polukhin85eda122016-04-11 07:48:59 +00005419 case AttributeList::AT_IFunc:
5420 handleIFuncAttr(S, D, Attr);
5421 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005422 case AttributeList::AT_Alias:
5423 handleAliasAttr(S, D, Attr);
5424 break;
5425 case AttributeList::AT_Aligned:
5426 handleAlignedAttr(S, D, Attr);
5427 break;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00005428 case AttributeList::AT_AlignValue:
5429 handleAlignValueAttr(S, D, Attr);
5430 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005431 case AttributeList::AT_AlwaysInline:
Paul Robinsonf0674352014-03-31 22:29:15 +00005432 handleAlwaysInlineAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005433 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005434 case AttributeList::AT_AnalyzerNoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005435 handleAnalyzerNoReturnAttr(S, D, Attr);
5436 break;
5437 case AttributeList::AT_TLSModel:
5438 handleTLSModelAttr(S, D, Attr);
5439 break;
5440 case AttributeList::AT_Annotate:
5441 handleAnnotateAttr(S, D, Attr);
5442 break;
5443 case AttributeList::AT_Availability:
5444 handleAvailabilityAttr(S, D, Attr);
5445 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005446 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00005447 handleDependencyAttr(S, scope, D, Attr);
5448 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005449 case AttributeList::AT_Common:
5450 handleCommonAttr(S, D, Attr);
5451 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005452 case AttributeList::AT_CUDAConstant:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005453 handleSimpleAttributeWithExclusions<CUDAConstantAttr, CUDASharedAttr>(S, D,
5454 Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005455 break;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00005456 case AttributeList::AT_PassObjectSize:
5457 handlePassObjectSizeAttr(S, D, Attr);
5458 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005459 case AttributeList::AT_Constructor:
5460 handleConstructorAttr(S, D, Attr);
5461 break;
Richard Smith10876ef2013-01-17 01:30:42 +00005462 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005463 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr);
5464 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005465 case AttributeList::AT_Deprecated:
Aaron Ballman43f40102014-11-14 22:34:56 +00005466 handleDeprecatedAttr(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00005467 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005468 case AttributeList::AT_Destructor:
5469 handleDestructorAttr(S, D, Attr);
5470 break;
5471 case AttributeList::AT_EnableIf:
5472 handleEnableIfAttr(S, D, Attr);
5473 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005474 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005475 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005476 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00005477 case AttributeList::AT_MinSize:
Paul Robinsonaae2fba2014-12-10 23:34:36 +00005478 handleMinSizeAttr(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00005479 break;
Paul Robinsonf0674352014-03-31 22:29:15 +00005480 case AttributeList::AT_OptimizeNone:
5481 handleOptimizeNoneAttr(S, D, Attr);
5482 break;
Alexis Hunt724f14e2014-11-28 00:53:20 +00005483 case AttributeList::AT_FlagEnum:
5484 handleSimpleAttribute<FlagEnumAttr>(S, D, Attr);
5485 break;
Peter Collingbourne41af7c22014-05-20 17:12:51 +00005486 case AttributeList::AT_Flatten:
5487 handleSimpleAttribute<FlattenAttr>(S, D, Attr);
5488 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005489 case AttributeList::AT_Format:
5490 handleFormatAttr(S, D, Attr);
5491 break;
5492 case AttributeList::AT_FormatArg:
5493 handleFormatArgAttr(S, D, Attr);
5494 break;
5495 case AttributeList::AT_CUDAGlobal:
5496 handleGlobalAttr(S, D, Attr);
5497 break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00005498 case AttributeList::AT_CUDADevice:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005499 handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D,
5500 Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005501 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005502 case AttributeList::AT_CUDAHost:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005503 handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D,
5504 Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005505 break;
5506 case AttributeList::AT_GNUInline:
5507 handleGNUInlineAttr(S, D, Attr);
5508 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005509 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005510 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00005511 break;
David Majnemer631a90b2015-02-04 07:23:21 +00005512 case AttributeList::AT_Restrict:
5513 handleRestrictAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005514 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005515 case AttributeList::AT_MayAlias:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005516 handleSimpleAttribute<MayAliasAttr>(S, D, Attr);
5517 break;
5518 case AttributeList::AT_Mode:
5519 handleModeAttr(S, D, Attr);
5520 break;
David Majnemer1bf0f8e2015-07-20 22:51:52 +00005521 case AttributeList::AT_NoAlias:
5522 handleSimpleAttribute<NoAliasAttr>(S, D, Attr);
5523 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005524 case AttributeList::AT_NoCommon:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005525 handleSimpleAttribute<NoCommonAttr>(S, D, Attr);
5526 break;
Peter Collingbourneb4728c12014-05-19 22:14:34 +00005527 case AttributeList::AT_NoSplitStack:
5528 handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr);
5529 break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00005530 case AttributeList::AT_NonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005531 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
5532 handleNonNullAttrParameter(S, PVD, Attr);
5533 else
5534 handleNonNullAttr(S, D, Attr);
5535 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00005536 case AttributeList::AT_ReturnsNonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005537 handleReturnsNonNullAttr(S, D, Attr);
5538 break;
Hal Finkelee90a222014-09-26 05:04:30 +00005539 case AttributeList::AT_AssumeAligned:
5540 handleAssumeAlignedAttr(S, D, Attr);
5541 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005542 case AttributeList::AT_Overloadable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005543 handleSimpleAttribute<OverloadableAttr>(S, D, Attr);
5544 break;
5545 case AttributeList::AT_Ownership:
5546 handleOwnershipAttr(S, D, Attr);
5547 break;
5548 case AttributeList::AT_Cold:
5549 handleColdAttr(S, D, Attr);
5550 break;
5551 case AttributeList::AT_Hot:
5552 handleHotAttr(S, D, Attr);
5553 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005554 case AttributeList::AT_Naked:
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00005555 handleNakedAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005556 break;
5557 case AttributeList::AT_NoReturn:
5558 handleNoReturnAttr(S, D, Attr);
5559 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00005560 case AttributeList::AT_NoThrow:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005561 handleSimpleAttribute<NoThrowAttr>(S, D, Attr);
5562 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005563 case AttributeList::AT_CUDAShared:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005564 handleSimpleAttributeWithExclusions<CUDASharedAttr, CUDAConstantAttr>(S, D,
5565 Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005566 break;
5567 case AttributeList::AT_VecReturn:
5568 handleVecReturnAttr(S, D, Attr);
5569 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005570 case AttributeList::AT_ObjCOwnership:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005571 handleObjCOwnershipAttr(S, D, Attr);
5572 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005573 case AttributeList::AT_ObjCPreciseLifetime:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005574 handleObjCPreciseLifetimeAttr(S, D, Attr);
5575 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005576 case AttributeList::AT_ObjCReturnsInnerPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005577 handleObjCReturnsInnerPointerAttr(S, D, Attr);
5578 break;
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005579 case AttributeList::AT_ObjCRequiresSuper:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005580 handleObjCRequiresSuperAttr(S, D, Attr);
5581 break;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005582 case AttributeList::AT_ObjCBridge:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005583 handleObjCBridgeAttr(S, scope, D, Attr);
5584 break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005585 case AttributeList::AT_ObjCBridgeMutable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005586 handleObjCBridgeMutableAttr(S, scope, D, Attr);
5587 break;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005588 case AttributeList::AT_ObjCBridgeRelated:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005589 handleObjCBridgeRelatedAttr(S, scope, D, Attr);
5590 break;
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005591 case AttributeList::AT_ObjCDesignatedInitializer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005592 handleObjCDesignatedInitializer(S, D, Attr);
5593 break;
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00005594 case AttributeList::AT_ObjCRuntimeName:
5595 handleObjCRuntimeName(S, D, Attr);
5596 break;
Douglas Gregor24ae22c2016-04-01 23:23:52 +00005597 case AttributeList::AT_ObjCRuntimeVisible:
5598 handleSimpleAttribute<ObjCRuntimeVisibleAttr>(S, D, Attr);
5599 break;
Alex Denisovfde64952015-06-26 05:28:36 +00005600 case AttributeList::AT_ObjCBoxable:
5601 handleObjCBoxable(S, D, Attr);
5602 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005603 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005604 handleCFAuditedTransferAttr(S, D, Attr);
5605 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005606 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005607 handleCFUnknownTransferAttr(S, D, Attr);
5608 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005609 case AttributeList::AT_CFConsumed:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005610 case AttributeList::AT_NSConsumed:
5611 handleNSConsumedAttr(S, D, Attr);
5612 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005613 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005614 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr);
5615 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005616 case AttributeList::AT_NSReturnsAutoreleased:
5617 case AttributeList::AT_NSReturnsNotRetained:
5618 case AttributeList::AT_CFReturnsNotRetained:
5619 case AttributeList::AT_NSReturnsRetained:
5620 case AttributeList::AT_CFReturnsRetained:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005621 handleNSReturnsRetainedAttr(S, D, Attr);
5622 break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00005623 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005624 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr);
5625 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005626 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005627 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr);
5628 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00005629 case AttributeList::AT_VecTypeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005630 handleVecTypeHint(S, D, Attr);
5631 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005632 case AttributeList::AT_InitPriority:
5633 handleInitPriorityAttr(S, D, Attr);
5634 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005635 case AttributeList::AT_Packed:
5636 handlePackedAttr(S, D, Attr);
5637 break;
5638 case AttributeList::AT_Section:
5639 handleSectionAttr(S, D, Attr);
5640 break;
Eric Christopher11acf732015-06-12 01:35:52 +00005641 case AttributeList::AT_Target:
5642 handleTargetAttr(S, D, Attr);
5643 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005644 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00005645 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00005646 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005647 case AttributeList::AT_ArcWeakrefUnavailable:
5648 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr);
5649 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005650 case AttributeList::AT_ObjCRootClass:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005651 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr);
5652 break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00005653 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek438f8db2014-02-22 01:06:05 +00005654 handleObjCSuppresProtocolAttr(S, D, Attr);
Ted Kremenek28eace62013-11-23 01:01:34 +00005655 break;
5656 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005657 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr);
5658 break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00005659 case AttributeList::AT_Unused:
Aaron Ballman0bcd6c12016-03-09 16:48:08 +00005660 handleUnusedAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005661 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005662 case AttributeList::AT_ReturnsTwice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005663 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr);
5664 break;
Akira Hatanakac8667622015-11-06 23:56:15 +00005665 case AttributeList::AT_NotTailCalled:
5666 handleNotTailCalledAttr(S, D, Attr);
5667 break;
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00005668 case AttributeList::AT_DisableTailCalls:
5669 handleDisableTailCallsAttr(S, D, Attr);
5670 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005671 case AttributeList::AT_Used:
5672 handleUsedAttr(S, D, Attr);
5673 break;
John McCalld041a9b2013-02-20 01:54:26 +00005674 case AttributeList::AT_Visibility:
5675 handleVisibilityAttr(S, D, Attr, false);
5676 break;
5677 case AttributeList::AT_TypeVisibility:
5678 handleVisibilityAttr(S, D, Attr, true);
5679 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00005680 case AttributeList::AT_WarnUnused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005681 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr);
5682 break;
5683 case AttributeList::AT_WarnUnusedResult:
5684 handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00005685 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00005686 case AttributeList::AT_Weak:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005687 handleSimpleAttribute<WeakAttr>(S, D, Attr);
5688 break;
5689 case AttributeList::AT_WeakRef:
5690 handleWeakRefAttr(S, D, Attr);
5691 break;
5692 case AttributeList::AT_WeakImport:
5693 handleWeakImportAttr(S, D, Attr);
5694 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005695 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005696 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005697 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005698 case AttributeList::AT_ObjCException:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005699 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr);
5700 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005701 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005702 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00005703 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005704 case AttributeList::AT_ObjCNSObject:
5705 handleObjCNSObject(S, D, Attr);
5706 break;
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00005707 case AttributeList::AT_ObjCIndependentClass:
5708 handleObjCIndependentClass(S, D, Attr);
5709 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005710 case AttributeList::AT_Blocks:
5711 handleBlocksAttr(S, D, Attr);
5712 break;
5713 case AttributeList::AT_Sentinel:
5714 handleSentinelAttr(S, D, Attr);
5715 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00005716 case AttributeList::AT_Const:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005717 handleSimpleAttribute<ConstAttr>(S, D, Attr);
5718 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005719 case AttributeList::AT_Pure:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005720 handleSimpleAttribute<PureAttr>(S, D, Attr);
5721 break;
5722 case AttributeList::AT_Cleanup:
5723 handleCleanupAttr(S, D, Attr);
5724 break;
5725 case AttributeList::AT_NoDebug:
5726 handleNoDebugAttr(S, D, Attr);
5727 break;
Aaron Ballman7c19ab12014-02-22 16:59:24 +00005728 case AttributeList::AT_NoDuplicate:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005729 handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr);
5730 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005731 case AttributeList::AT_NoInline:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005732 handleSimpleAttribute<NoInlineAttr>(S, D, Attr);
5733 break;
5734 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
5735 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr);
5736 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005737 case AttributeList::AT_StdCall:
5738 case AttributeList::AT_CDecl:
5739 case AttributeList::AT_FastCall:
5740 case AttributeList::AT_ThisCall:
5741 case AttributeList::AT_Pascal:
John McCall477f2bb2016-03-03 06:39:32 +00005742 case AttributeList::AT_SwiftCall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00005743 case AttributeList::AT_VectorCall:
Charles Davisb5a214e2013-08-30 04:39:01 +00005744 case AttributeList::AT_MSABI:
5745 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005746 case AttributeList::AT_Pcs:
Guy Benyeif0a014b2012-12-25 08:53:55 +00005747 case AttributeList::AT_IntelOclBicc:
Roman Levenstein35aa5ce2016-03-16 18:00:46 +00005748 case AttributeList::AT_PreserveMost:
5749 case AttributeList::AT_PreserveAll:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005750 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00005751 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005752 case AttributeList::AT_OpenCLKernel:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005753 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr);
5754 break;
Xiuli Pan11e13f62016-02-26 03:13:03 +00005755 case AttributeList::AT_OpenCLAccess:
5756 handleOpenCLAccessAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005757 break;
Anastasia Stulovafde76222016-04-01 16:05:09 +00005758 case AttributeList::AT_OpenCLNoSVM:
5759 handleOpenCLNoSVMAttr(S, D, Attr);
5760 break;
John McCall477f2bb2016-03-03 06:39:32 +00005761 case AttributeList::AT_SwiftContext:
5762 handleParameterABIAttr(S, D, Attr, ParameterABI::SwiftContext);
5763 break;
5764 case AttributeList::AT_SwiftErrorResult:
5765 handleParameterABIAttr(S, D, Attr, ParameterABI::SwiftErrorResult);
5766 break;
5767 case AttributeList::AT_SwiftIndirectResult:
5768 handleParameterABIAttr(S, D, Attr, ParameterABI::SwiftIndirectResult);
5769 break;
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005770 case AttributeList::AT_InternalLinkage:
5771 handleInternalLinkageAttr(S, D, Attr);
5772 break;
Peter Collingbourne3afb2662016-04-28 17:09:37 +00005773 case AttributeList::AT_LTOVisibilityPublic:
5774 handleSimpleAttribute<LTOVisibilityPublicAttr>(S, D, Attr);
5775 break;
John McCall8d32c052012-05-22 21:28:12 +00005776
5777 // Microsoft attributes:
David Majnemercd3ebfe2016-05-23 17:16:12 +00005778 case AttributeList::AT_EmptyBases:
5779 handleSimpleAttribute<EmptyBasesAttr>(S, D, Attr);
5780 break;
5781 case AttributeList::AT_LayoutVersion:
5782 handleLayoutVersion(S, D, Attr);
5783 break;
David Majnemer8ab003a2015-02-02 19:30:52 +00005784 case AttributeList::AT_MSNoVTable:
5785 handleSimpleAttribute<MSNoVTableAttr>(S, D, Attr);
David Majnemer129f4172015-02-02 10:22:20 +00005786 break;
David Majnemer8ab003a2015-02-02 19:30:52 +00005787 case AttributeList::AT_MSStruct:
5788 handleSimpleAttribute<MSStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00005789 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005790 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005791 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00005792 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00005793 case AttributeList::AT_MSInheritance:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005794 handleMSInheritanceAttr(S, D, Attr);
5795 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00005796 case AttributeList::AT_SelectAny:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005797 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr);
5798 break;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005799 case AttributeList::AT_Thread:
5800 handleDeclspecThreadAttr(S, D, Attr);
5801 break;
David Majnemercd3ebfe2016-05-23 17:16:12 +00005802
Dmitry Polukhinbf17ecf2016-03-09 15:30:53 +00005803 case AttributeList::AT_AbiTag:
5804 handleAbiTagAttr(S, D, Attr);
5805 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005806
5807 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00005808 case AttributeList::AT_AssertExclusiveLock:
5809 handleAssertExclusiveLockAttr(S, D, Attr);
5810 break;
5811 case AttributeList::AT_AssertSharedLock:
5812 handleAssertSharedLockAttr(S, D, Attr);
5813 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005814 case AttributeList::AT_GuardedVar:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005815 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr);
5816 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005817 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00005818 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005819 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005820 case AttributeList::AT_ScopedLockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005821 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr);
5822 break;
Peter Collingbourne915df992015-05-15 18:33:32 +00005823 case AttributeList::AT_NoSanitize:
5824 handleNoSanitizeAttr(S, D, Attr);
5825 break;
5826 case AttributeList::AT_NoSanitizeSpecific:
5827 handleNoSanitizeSpecificAttr(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00005828 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005829 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005830 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00005831 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005832 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005833 handleGuardedByAttr(S, D, Attr);
5834 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005835 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00005836 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005837 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005838 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005839 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005840 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005841 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005842 handleLockReturnedAttr(S, D, Attr);
5843 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005844 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005845 handleLocksExcludedAttr(S, D, Attr);
5846 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005847 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005848 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005849 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005850 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00005851 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005852 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005853 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00005854 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005855 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005856
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005857 // Capability analysis attributes.
5858 case AttributeList::AT_Capability:
5859 case AttributeList::AT_Lockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005860 handleCapabilityAttr(S, D, Attr);
5861 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005862 case AttributeList::AT_RequiresCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005863 handleRequiresCapabilityAttr(S, D, Attr);
5864 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005865
5866 case AttributeList::AT_AssertCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005867 handleAssertCapabilityAttr(S, D, Attr);
5868 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005869 case AttributeList::AT_AcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005870 handleAcquireCapabilityAttr(S, D, Attr);
5871 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005872 case AttributeList::AT_ReleaseCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005873 handleReleaseCapabilityAttr(S, D, Attr);
5874 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005875 case AttributeList::AT_TryAcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005876 handleTryAcquireCapabilityAttr(S, D, Attr);
5877 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005878
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00005879 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00005880 case AttributeList::AT_Consumable:
5881 handleConsumableAttr(S, D, Attr);
5882 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00005883 case AttributeList::AT_ConsumableAutoCast:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005884 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr);
5885 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00005886 case AttributeList::AT_ConsumableSetOnRead:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005887 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr);
5888 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00005889 case AttributeList::AT_CallableWhen:
5890 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00005891 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00005892 case AttributeList::AT_ParamTypestate:
5893 handleParamTypestateAttr(S, D, Attr);
5894 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00005895 case AttributeList::AT_ReturnTypestate:
5896 handleReturnTypestateAttr(S, D, Attr);
5897 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00005898 case AttributeList::AT_SetTypestate:
5899 handleSetTypestateAttr(S, D, Attr);
5900 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00005901 case AttributeList::AT_TestTypestate:
5902 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00005903 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00005904
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00005905 // Type safety attributes.
5906 case AttributeList::AT_ArgumentWithTypeTag:
5907 handleArgumentWithTypeTagAttr(S, D, Attr);
5908 break;
5909 case AttributeList::AT_TypeTagForDatatype:
5910 handleTypeTagForDatatypeAttr(S, D, Attr);
5911 break;
Pirama Arumuga Nainare5d2d712016-06-10 21:51:18 +00005912 case AttributeList::AT_RenderScriptKernel:
5913 handleSimpleAttribute<RenderScriptKernelAttr>(S, D, Attr);
Pirama Arumuga Nainar8b788d02016-06-09 23:34:20 +00005914 break;
Aaron Ballman7d2aecb2016-07-13 22:32:15 +00005915 // XRay attributes.
5916 case AttributeList::AT_XRayInstrument:
5917 handleSimpleAttribute<XRayInstrumentAttr>(S, D, Attr);
5918 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005919 }
5920}
5921
5922/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5923/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00005924void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00005925 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00005926 bool IncludeCXX11Attributes) {
5927 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00005928 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00005929
Joey Gouly2cd9db12013-12-13 16:15:28 +00005930 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00005931 // GCC accepts
5932 // static int a9 __attribute__((weakref));
5933 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00005934 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00005935 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
5936 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00005937 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00005938 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005939 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00005940
Aaron Ballmanbe243a72014-12-04 22:45:31 +00005941 // FIXME: We should be able to handle this in TableGen as well. It would be
5942 // good to have a way to specify "these attributes must appear as a group",
5943 // for these. Additionally, it would be good to have a way to specify "these
5944 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00005945 if (!D->hasAttr<OpenCLKernelAttr>()) {
5946 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00005947 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00005948 // FIXME: This emits a different error message than
5949 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00005950 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005951 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00005952 } else if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00005953 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005954 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00005955 } else if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00005956 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005957 D->setInvalidDecl();
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00005958 } else if (Attr *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
5959 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
5960 << A << ExpectedKernelFunction;
5961 D->setInvalidDecl();
5962 } else if (Attr *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
5963 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
5964 << A << ExpectedKernelFunction;
5965 D->setInvalidDecl();
Joey Gouly2cd9db12013-12-13 16:15:28 +00005966 }
5967 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005968}
5969
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005970// Annotation attributes are the only attributes allowed after an access
5971// specifier.
5972bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5973 const AttributeList *AttrList) {
5974 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005975 if (l->getKind() == AttributeList::AT_Annotate) {
David Majnemer706f3152014-12-14 01:05:01 +00005976 ProcessDeclAttribute(*this, nullptr, ASDecl, *l, l->isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005977 } else {
5978 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5979 return true;
5980 }
5981 }
5982
5983 return false;
5984}
5985
John McCall42856de2011-10-01 05:17:03 +00005986/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5987/// contains any decl attributes that we should warn about.
5988static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5989 for ( ; A; A = A->getNext()) {
5990 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00005991 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00005992 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5993
5994 if (A->getKind() == AttributeList::UnknownAttribute) {
5995 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5996 << A->getName() << A->getRange();
5997 } else {
5998 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5999 << A->getName() << A->getRange();
6000 }
6001 }
6002}
6003
6004/// checkUnusedDeclAttributes - Given a declarator which is not being
6005/// used to build a declaration, complain about any decl attributes
6006/// which might be lying around on it.
6007void Sema::checkUnusedDeclAttributes(Declarator &D) {
6008 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
6009 ::checkUnusedDeclAttributes(*this, D.getAttributes());
6010 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
6011 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
6012}
6013
Ryan Flynn7d470f32009-07-30 03:15:39 +00006014/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00006015/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00006016NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
6017 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00006018 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00006019 NamedDecl *NewD = nullptr;
Ryan Flynn7d470f32009-07-30 03:15:39 +00006020 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Alexander Kornienko061900f2015-12-03 11:37:28 +00006021 FunctionDecl *NewFD;
6022 // FIXME: Missing call to CheckFunctionDeclaration().
Eli Friedmance3e2c82011-09-07 04:05:06 +00006023 // FIXME: Mangling?
6024 // FIXME: Is the qualifier info correct?
6025 // FIXME: Is the DeclContext correct?
Alexander Kornienko061900f2015-12-03 11:37:28 +00006026 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
6027 Loc, Loc, DeclarationName(II),
6028 FD->getType(), FD->getTypeSourceInfo(),
6029 SC_None, false/*isInlineSpecified*/,
6030 FD->hasPrototype(),
6031 false/*isConstexprSpecified*/);
Eli Friedmance3e2c82011-09-07 04:05:06 +00006032 NewD = NewFD;
6033
6034 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00006035 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00006036
6037 // Fake up parameter variables; they are declared as if this were
6038 // a typedef.
6039 QualType FDTy = FD->getType();
6040 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
6041 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00006042 for (const auto &AI : FT->param_types()) {
6043 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00006044 Param->setScopeInfo(0, Params.size());
6045 Params.push_back(Param);
6046 }
David Blaikie9c70e042011-09-21 18:16:56 +00006047 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00006048 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00006049 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
6050 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00006051 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00006052 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00006053 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00006054 if (VD->getQualifier()) {
6055 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00006056 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00006057 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00006058 }
6059 return NewD;
6060}
6061
James Dennett634962f2012-06-14 21:40:34 +00006062/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00006063/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00006064void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00006065 if (W.getUsed()) return; // only do this once
6066 W.setUsed(true);
6067 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
6068 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00006069 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00006070 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
6071 W.getLocation()));
6072 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00006073 WeakTopLevelDecl.push_back(NewD);
6074 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
6075 // to insert Decl at TU scope, sorry.
6076 DeclContext *SavedContext = CurContext;
6077 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00006078 NewD->setDeclContext(CurContext);
6079 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00006080 PushOnScopeChains(NewD, S);
6081 CurContext = SavedContext;
6082 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00006083 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00006084 }
6085}
6086
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006087void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
6088 // It's valid to "forward-declare" #pragma weak, in which case we
6089 // have to do this.
6090 LoadExternalWeakUndeclaredIdentifiers();
6091 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00006092 NamedDecl *ND = nullptr;
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006093 if (VarDecl *VD = dyn_cast<VarDecl>(D))
6094 if (VD->isExternC())
6095 ND = VD;
6096 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
6097 if (FD->isExternC())
6098 ND = FD;
6099 if (ND) {
6100 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00006101 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00006102 if (I != WeakUndeclaredIdentifiers.end()) {
6103 WeakInfo W = I->second;
6104 DeclApplyPragmaWeak(S, ND, W);
6105 WeakUndeclaredIdentifiers[Id] = W;
6106 }
6107 }
6108 }
6109 }
6110}
6111
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006112/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
6113/// it, apply them to D. This is a bit tricky because PD can have attributes
6114/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00006115void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006116 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00006117 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00006118 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00006119
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006120 // Walk the declarator structure, applying decl attributes that were in a type
6121 // position to the decl itself. This handles cases like:
6122 // int *__attr__(x)** D;
6123 // when X is a decl attribute.
6124 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
6125 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00006126 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00006127
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006128 // Finally, apply any attributes on the decl itself.
6129 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00006130 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00006131}
John McCall28a6aea2009-11-04 02:18:39 +00006132
John McCall31168b02011-06-15 23:02:42 +00006133/// Is the given declaration allowed to use a forbidden type?
John McCallb61e14e2015-10-27 04:54:50 +00006134/// If so, it'll still be annotated with an attribute that makes it
6135/// illegal to actually use.
6136static bool isForbiddenTypeAllowed(Sema &S, Decl *decl,
6137 const DelayedDiagnostic &diag,
John McCallc6af8c62015-10-28 05:03:19 +00006138 UnavailableAttr::ImplicitReason &reason) {
John McCall31168b02011-06-15 23:02:42 +00006139 // Private ivars are always okay. Unfortunately, people don't
6140 // always properly make their ivars private, even in system headers.
6141 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00006142 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
6143 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00006144 return false;
6145
John McCallc6af8c62015-10-28 05:03:19 +00006146 // Silently accept unsupported uses of __weak in both user and system
6147 // declarations when it's been disabled, for ease of integration with
6148 // -fno-objc-arc files. We do have to take some care against attempts
6149 // to define such things; for now, we've only done that for ivars
6150 // and properties.
6151 if ((isa<ObjCIvarDecl>(decl) || isa<ObjCPropertyDecl>(decl))) {
6152 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
6153 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
6154 reason = UnavailableAttr::IR_ForbiddenWeak;
6155 return true;
6156 }
John McCallb61e14e2015-10-27 04:54:50 +00006157 }
6158
John McCallc6af8c62015-10-28 05:03:19 +00006159 // Allow all sorts of things in system headers.
6160 if (S.Context.getSourceManager().isInSystemHeader(decl->getLocation())) {
6161 // Currently, all the failures dealt with this way are due to ARC
6162 // restrictions.
6163 reason = UnavailableAttr::IR_ARCForbiddenType;
6164 return true;
John McCallb61e14e2015-10-27 04:54:50 +00006165 }
6166
6167 return false;
John McCall31168b02011-06-15 23:02:42 +00006168}
6169
6170/// Handle a delayed forbidden-type diagnostic.
6171static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
6172 Decl *decl) {
John McCallc6af8c62015-10-28 05:03:19 +00006173 auto reason = UnavailableAttr::IR_None;
6174 if (decl && isForbiddenTypeAllowed(S, decl, diag, reason)) {
6175 assert(reason && "didn't set reason?");
6176 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", reason,
6177 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00006178 return;
6179 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00006180 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00006181 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00006182 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00006183 // kind of forbidden type messages on unavailable functions.
6184 if (FD->hasAttr<UnavailableAttr>() &&
6185 diag.getForbiddenTypeDiagnostic() ==
6186 diag::err_arc_array_param_no_ownership) {
6187 diag.Triggered = true;
6188 return;
6189 }
6190 }
John McCall31168b02011-06-15 23:02:42 +00006191
6192 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
6193 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
6194 diag.Triggered = true;
6195}
6196
Aaron Ballmanfb237522014-10-15 15:37:51 +00006197static bool isDeclDeprecated(Decl *D) {
6198 do {
6199 if (D->isDeprecated())
6200 return true;
6201 // A category implicitly has the availability of the interface.
6202 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00006203 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
6204 return Interface->isDeprecated();
Aaron Ballmanfb237522014-10-15 15:37:51 +00006205 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
6206 return false;
6207}
6208
6209static bool isDeclUnavailable(Decl *D) {
6210 do {
6211 if (D->isUnavailable())
6212 return true;
6213 // A category implicitly has the availability of the interface.
6214 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00006215 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
6216 return Interface->isUnavailable();
Aaron Ballmanfb237522014-10-15 15:37:51 +00006217 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
6218 return false;
6219}
6220
Manman Ren45b1ab12016-05-06 19:57:16 +00006221static const AvailabilityAttr *getAttrForPlatform(ASTContext &Context,
6222 const Decl *D) {
6223 // Check each AvailabilityAttr to find the one for this platform.
6224 for (const auto *A : D->attrs()) {
6225 if (const auto *Avail = dyn_cast<AvailabilityAttr>(A)) {
6226 // FIXME: this is copied from CheckAvailability. We should try to
6227 // de-duplicate.
6228
6229 // Check if this is an App Extension "platform", and if so chop off
6230 // the suffix for matching with the actual platform.
6231 StringRef ActualPlatform = Avail->getPlatform()->getName();
6232 StringRef RealizedPlatform = ActualPlatform;
6233 if (Context.getLangOpts().AppExt) {
6234 size_t suffix = RealizedPlatform.rfind("_app_extension");
6235 if (suffix != StringRef::npos)
6236 RealizedPlatform = RealizedPlatform.slice(0, suffix);
6237 }
6238
6239 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
6240
6241 // Match the platform name.
6242 if (RealizedPlatform == TargetPlatform)
6243 return Avail;
6244 }
6245 }
6246 return nullptr;
6247}
6248
Nico Weber0055a192015-03-19 19:18:22 +00006249static void DoEmitAvailabilityWarning(Sema &S, Sema::AvailabilityDiagnostic K,
Aaron Ballmanfb237522014-10-15 15:37:51 +00006250 Decl *Ctx, const NamedDecl *D,
6251 StringRef Message, SourceLocation Loc,
6252 const ObjCInterfaceDecl *UnknownObjCClass,
6253 const ObjCPropertyDecl *ObjCProperty,
6254 bool ObjCPropertyAccess) {
6255 // Diagnostics for deprecated or unavailable.
6256 unsigned diag, diag_message, diag_fwdclass_message;
John McCallb61e14e2015-10-27 04:54:50 +00006257 unsigned diag_available_here = diag::note_availability_specified_here;
Aaron Ballmanfb237522014-10-15 15:37:51 +00006258
6259 // Matches 'diag::note_property_attribute' options.
6260 unsigned property_note_select;
6261
6262 // Matches diag::note_availability_specified_here.
6263 unsigned available_here_select_kind;
6264
6265 // Don't warn if our current context is deprecated or unavailable.
6266 switch (K) {
Nico Weber0055a192015-03-19 19:18:22 +00006267 case Sema::AD_Deprecation:
Jordan Rosed17c03e2015-04-30 17:20:35 +00006268 if (isDeclDeprecated(Ctx) || isDeclUnavailable(Ctx))
Aaron Ballmanfb237522014-10-15 15:37:51 +00006269 return;
6270 diag = !ObjCPropertyAccess ? diag::warn_deprecated
6271 : diag::warn_property_method_deprecated;
6272 diag_message = diag::warn_deprecated_message;
6273 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
6274 property_note_select = /* deprecated */ 0;
6275 available_here_select_kind = /* deprecated */ 2;
6276 break;
6277
Nico Weber0055a192015-03-19 19:18:22 +00006278 case Sema::AD_Unavailable:
Aaron Ballmanfb237522014-10-15 15:37:51 +00006279 if (isDeclUnavailable(Ctx))
6280 return;
6281 diag = !ObjCPropertyAccess ? diag::err_unavailable
6282 : diag::err_property_method_unavailable;
6283 diag_message = diag::err_unavailable_message;
6284 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
6285 property_note_select = /* unavailable */ 1;
6286 available_here_select_kind = /* unavailable */ 0;
John McCallb61e14e2015-10-27 04:54:50 +00006287
John McCallc6af8c62015-10-28 05:03:19 +00006288 if (auto attr = D->getAttr<UnavailableAttr>()) {
6289 if (attr->isImplicit() && attr->getImplicitReason()) {
6290 // Most of these failures are due to extra restrictions in ARC;
6291 // reflect that in the primary diagnostic when applicable.
6292 auto flagARCError = [&] {
6293 if (S.getLangOpts().ObjCAutoRefCount &&
6294 S.getSourceManager().isInSystemHeader(D->getLocation()))
6295 diag = diag::err_unavailable_in_arc;
6296 };
6297
6298 switch (attr->getImplicitReason()) {
6299 case UnavailableAttr::IR_None: break;
6300
6301 case UnavailableAttr::IR_ARCForbiddenType:
6302 flagARCError();
6303 diag_available_here = diag::note_arc_forbidden_type;
6304 break;
6305
6306 case UnavailableAttr::IR_ForbiddenWeak:
6307 if (S.getLangOpts().ObjCWeakRuntime)
6308 diag_available_here = diag::note_arc_weak_disabled;
6309 else
6310 diag_available_here = diag::note_arc_weak_no_runtime;
6311 break;
6312
6313 case UnavailableAttr::IR_ARCForbiddenConversion:
6314 flagARCError();
6315 diag_available_here = diag::note_performs_forbidden_arc_conversion;
6316 break;
6317
6318 case UnavailableAttr::IR_ARCInitReturnsUnrelated:
6319 flagARCError();
6320 diag_available_here = diag::note_arc_init_returns_unrelated;
6321 break;
6322
6323 case UnavailableAttr::IR_ARCFieldWithOwnership:
6324 flagARCError();
6325 diag_available_here = diag::note_arc_field_with_ownership;
6326 break;
6327 }
6328 }
John McCallb61e14e2015-10-27 04:54:50 +00006329 }
Aaron Ballmanfb237522014-10-15 15:37:51 +00006330 break;
6331
Nico Weber0055a192015-03-19 19:18:22 +00006332 case Sema::AD_Partial:
6333 diag = diag::warn_partial_availability;
6334 diag_message = diag::warn_partial_message;
6335 diag_fwdclass_message = diag::warn_partial_fwdclass_message;
6336 property_note_select = /* partial */ 2;
6337 available_here_select_kind = /* partial */ 3;
6338 break;
Aaron Ballmanfb237522014-10-15 15:37:51 +00006339 }
6340
Manman Renc7890fe2016-03-16 18:50:49 +00006341 CharSourceRange UseRange;
6342 StringRef Replacement;
6343 if (K == Sema::AD_Deprecation) {
6344 if (auto attr = D->getAttr<DeprecatedAttr>())
6345 Replacement = attr->getReplacement();
Manman Ren45b1ab12016-05-06 19:57:16 +00006346 if (auto attr = getAttrForPlatform(S.Context, D))
Manman Ren75bc6762016-03-21 17:30:55 +00006347 Replacement = attr->getReplacement();
Manman Renc7890fe2016-03-16 18:50:49 +00006348
6349 if (!Replacement.empty())
6350 UseRange =
6351 CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc));
6352 }
6353
Aaron Ballmanfb237522014-10-15 15:37:51 +00006354 if (!Message.empty()) {
Manman Renc7890fe2016-03-16 18:50:49 +00006355 S.Diag(Loc, diag_message) << D << Message
6356 << (UseRange.isValid() ?
6357 FixItHint::CreateReplacement(UseRange, Replacement) : FixItHint());
Aaron Ballmanfb237522014-10-15 15:37:51 +00006358 if (ObjCProperty)
6359 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
6360 << ObjCProperty->getDeclName() << property_note_select;
6361 } else if (!UnknownObjCClass) {
Manman Renc7890fe2016-03-16 18:50:49 +00006362 S.Diag(Loc, diag) << D
6363 << (UseRange.isValid() ?
6364 FixItHint::CreateReplacement(UseRange, Replacement) : FixItHint());
Aaron Ballmanfb237522014-10-15 15:37:51 +00006365 if (ObjCProperty)
6366 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
6367 << ObjCProperty->getDeclName() << property_note_select;
6368 } else {
Manman Renc7890fe2016-03-16 18:50:49 +00006369 S.Diag(Loc, diag_fwdclass_message) << D
6370 << (UseRange.isValid() ?
6371 FixItHint::CreateReplacement(UseRange, Replacement) : FixItHint());
Aaron Ballmanfb237522014-10-15 15:37:51 +00006372 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
6373 }
6374
Manman Ren45b1ab12016-05-06 19:57:16 +00006375 // The declaration can have multiple availability attributes, we are looking
6376 // at one of them.
6377 const AvailabilityAttr *A = getAttrForPlatform(S.Context, D);
6378 if (A && A->isInherited()) {
6379 for (const Decl *Redecl = D->getMostRecentDecl(); Redecl;
6380 Redecl = Redecl->getPreviousDecl()) {
6381 const AvailabilityAttr *AForRedecl = getAttrForPlatform(S.Context,
6382 Redecl);
6383 if (AForRedecl && !AForRedecl->isInherited()) {
6384 // If D is a declaration with inherited attributes, the note should
6385 // point to the declaration with actual attributes.
6386 S.Diag(Redecl->getLocation(), diag_available_here) << D
6387 << available_here_select_kind;
6388 break;
6389 }
6390 }
6391 }
6392 else
6393 S.Diag(D->getLocation(), diag_available_here)
6394 << D << available_here_select_kind;
6395
Nico Weber0055a192015-03-19 19:18:22 +00006396 if (K == Sema::AD_Partial)
6397 S.Diag(Loc, diag::note_partial_availability_silence) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00006398}
6399
6400static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
6401 Decl *Ctx) {
Nico Weber0055a192015-03-19 19:18:22 +00006402 assert(DD.Kind == DelayedDiagnostic::Deprecation ||
Manman Rend8039df2016-02-22 04:47:24 +00006403 DD.Kind == DelayedDiagnostic::Unavailable);
6404 Sema::AvailabilityDiagnostic AD = DD.Kind == DelayedDiagnostic::Deprecation
6405 ? Sema::AD_Deprecation
6406 : Sema::AD_Unavailable;
Aaron Ballmanfb237522014-10-15 15:37:51 +00006407 DD.Triggered = true;
Nico Weber0055a192015-03-19 19:18:22 +00006408 DoEmitAvailabilityWarning(
6409 S, AD, Ctx, DD.getDeprecationDecl(), DD.getDeprecationMessage(), DD.Loc,
6410 DD.getUnknownObjCClass(), DD.getObjCProperty(), false);
Aaron Ballmanfb237522014-10-15 15:37:51 +00006411}
6412
John McCall2ec85372012-05-07 06:16:41 +00006413void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
6414 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00006415 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00006416 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00006417
John McCall2ec85372012-05-07 06:16:41 +00006418 // When delaying diagnostics to run in the context of a parsed
6419 // declaration, we only want to actually emit anything if parsing
6420 // succeeds.
6421 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00006422
John McCall2ec85372012-05-07 06:16:41 +00006423 // We emit all the active diagnostics in this pool or any of its
6424 // parents. In general, we'll get one pool for the decl spec
6425 // and a child pool for each declarator; in a decl group like:
6426 // deprecated_typedef foo, *bar, baz();
6427 // only the declarator pops will be passed decls. This is correct;
6428 // we really do need to consider delayed diagnostics from the decl spec
6429 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00006430 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00006431 do {
John McCall6347b682012-05-07 06:16:58 +00006432 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00006433 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
6434 // This const_cast is a bit lame. Really, Triggered should be mutable.
6435 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00006436 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00006437 continue;
6438
John McCallc1465822011-02-14 07:13:47 +00006439 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00006440 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00006441 case DelayedDiagnostic::Unavailable:
6442 // Don't bother giving deprecation/unavailable diagnostics if
6443 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00006444 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00006445 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00006446 break;
6447
6448 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00006449 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00006450 break;
John McCall31168b02011-06-15 23:02:42 +00006451
6452 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00006453 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00006454 break;
John McCall86121512010-01-27 03:50:35 +00006455 }
6456 }
John McCall2ec85372012-05-07 06:16:41 +00006457 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00006458}
6459
John McCall6347b682012-05-07 06:16:58 +00006460/// Given a set of delayed diagnostics, re-emit them as if they had
6461/// been delayed in the current context instead of in the given pool.
6462/// Essentially, this just moves them to the current pool.
6463void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
6464 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
6465 assert(curPool && "re-emitting in undelayed context not supported");
6466 curPool->steal(pool);
6467}
6468
Ted Kremenekb79ee572013-12-18 23:30:06 +00006469void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
6470 NamedDecl *D, StringRef Message,
6471 SourceLocation Loc,
6472 const ObjCInterfaceDecl *UnknownObjCClass,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00006473 const ObjCPropertyDecl *ObjCProperty,
6474 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00006475 // Delay if we're currently parsing a declaration.
Nico Weber0055a192015-03-19 19:18:22 +00006476 if (DelayedDiagnostics.shouldDelayDiagnostics() && AD != AD_Partial) {
Nico Weber462fd1e2015-01-07 23:50:05 +00006477 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(
6478 AD, Loc, D, UnknownObjCClass, ObjCProperty, Message,
6479 ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00006480 return;
6481 }
6482
Ted Kremenekb79ee572013-12-18 23:30:06 +00006483 Decl *Ctx = cast<Decl>(getCurLexicalContext());
Nico Weber0055a192015-03-19 19:18:22 +00006484 DoEmitAvailabilityWarning(*this, AD, Ctx, D, Message, Loc, UnknownObjCClass,
6485 ObjCProperty, ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00006486}