blob: a9473d2e11956368fa5abb3a3e9d282705eb51a5 [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"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000015#include "clang/AST/ASTContext.h"
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +000016#include "clang/AST/CXXInheritance.h"
John McCall28a0cf72010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000018#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000020#include "clang/AST/Expr.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000021#include "clang/AST/ExprCXX.h"
Rafael Espindoladb77c4a2013-02-26 19:13:56 +000022#include "clang/AST/Mangle.h"
Alex Denisovfde64952015-06-26 05:28:36 +000023#include "clang/AST/ASTMutationListener.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000024#include "clang/Basic/CharInfo.h"
John McCall31168b02011-06-15 23:02:42 +000025#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000026#include "clang/Basic/TargetInfo.h"
Benjamin Kramer6ee15622013-09-13 15:35:43 +000027#include "clang/Lex/Preprocessor.h"
John McCall8b0666c2010-08-20 18:27:03 +000028#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000029#include "clang/Sema/DelayedDiagnostic.h"
John McCallf1e8b342011-09-29 07:17:38 +000030#include "clang/Sema/Lookup.h"
Richard Smithe233fbf2013-01-28 22:42:45 +000031#include "clang/Sema/Scope.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000032#include "llvm/ADT/StringExtras.h"
Hal Finkelee90a222014-09-26 05:04:30 +000033#include "llvm/Support/MathExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000034using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000035using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000036
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000037namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000038 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000039 C,
40 Cpp,
41 ObjC
42 };
43}
44
Chris Lattner58418ff2008-06-29 00:16:31 +000045//===----------------------------------------------------------------------===//
46// Helper functions
47//===----------------------------------------------------------------------===//
48
Ted Kremenek527042b2009-08-14 20:49:40 +000049/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000050/// type (function or function-typed variable) or an Objective-C
51/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000052static bool isFunctionOrMethod(const Decl *D) {
Craig Topperc3ec1492014-05-26 06:22:03 +000053 return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000054}
David Majnemer06864812015-04-07 06:01:53 +000055/// \brief Return true if the given decl has function type (function or
56/// function-typed variable) or an Objective-C method or a block.
57static bool isFunctionOrMethodOrBlock(const Decl *D) {
58 return isFunctionOrMethod(D) || isa<BlockDecl>(D);
59}
Fariborz Jahanian4447e172009-05-15 23:15:03 +000060
John McCall3882ace2011-01-05 12:14:39 +000061/// Return true if the given decl has a declarator that should have
62/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000063static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000064 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000065 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
66 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +000067}
68
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000069/// hasFunctionProto - Return true if the given decl has a argument
70/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000071/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000072static bool hasFunctionProto(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000073 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000074 return isa<FunctionProtoType>(FnTy);
Aaron Ballman12b9f652014-01-16 13:55:42 +000075 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000076}
77
Alp Toker601b22c2014-01-21 23:35:24 +000078/// getFunctionOrMethodNumParams - Return number of function or method
79/// parameters. It is an error to call this on a K&R function (use
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000080/// hasFunctionProto first).
Alp Toker601b22c2014-01-21 23:35:24 +000081static unsigned getFunctionOrMethodNumParams(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000082 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000083 return cast<FunctionProtoType>(FnTy)->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000084 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000085 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000086 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000087}
88
Alp Toker601b22c2014-01-21 23:35:24 +000089static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000090 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000091 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +000092 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000093 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +000094
Alp Toker03376dc2014-07-07 09:02:20 +000095 return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000096}
97
Aaron Ballman4bfa0de2014-08-01 12:58:11 +000098static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) {
99 if (const auto *FD = dyn_cast<FunctionDecl>(D))
100 return FD->getParamDecl(Idx)->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000101 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000102 return MD->parameters()[Idx]->getSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000103 if (const auto *BD = dyn_cast<BlockDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000104 return BD->getParamDecl(Idx)->getSourceRange();
105 return SourceRange();
106}
107
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000108static QualType getFunctionOrMethodResultType(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000109 if (const FunctionType *FnTy = D->getFunctionType())
Aaron Ballman6288d062014-07-11 16:31:29 +0000110 return cast<FunctionType>(FnTy)->getReturnType();
Alp Toker314cc812014-01-25 16:55:45 +0000111 return cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000112}
113
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000114static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) {
115 if (const auto *FD = dyn_cast<FunctionDecl>(D))
116 return FD->getReturnTypeSourceRange();
Aaron Ballmane13d0092014-08-01 17:02:34 +0000117 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000118 return MD->getReturnTypeSourceRange();
119 return SourceRange();
120}
121
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000122static bool isFunctionOrMethodVariadic(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +0000123 if (const FunctionType *FnTy = D->getFunctionType()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000124 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000125 return proto->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000126 }
Aaron Ballmane13d0092014-08-01 17:02:34 +0000127 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
128 return BD->isVariadic();
129
130 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000131}
132
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000133static bool isInstanceMethod(const Decl *D) {
134 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000135 return MethodDecl->isInstance();
136 return false;
137}
138
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000139static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000140 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000141 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000142 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000143
John McCall96fa4842010-05-17 21:00:27 +0000144 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
145 if (!Cls)
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 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000149
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000150 // FIXME: Should we walk the chain of classes?
151 return ClsName == &Ctx.Idents.get("NSString") ||
152 ClsName == &Ctx.Idents.get("NSMutableString");
153}
154
Daniel Dunbar980c6692008-09-26 03:32:58 +0000155static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000156 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000157 if (!PT)
158 return false;
159
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000160 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000161 if (!RT)
162 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000163
Daniel Dunbar980c6692008-09-26 03:32:58 +0000164 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000165 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000166 return false;
167
168 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
169}
170
Richard Smithb87c4652013-10-31 21:23:20 +0000171static unsigned getNumAttributeArgs(const AttributeList &Attr) {
172 // FIXME: Include the type in the argument list.
173 return Attr.getNumArgs() + Attr.hasParsedType();
174}
175
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000176template <typename Compare>
177static bool checkAttributeNumArgsImpl(Sema &S, const AttributeList &Attr,
178 unsigned Num, unsigned Diag,
179 Compare Comp) {
180 if (Comp(getNumAttributeArgs(Attr), Num)) {
181 S.Diag(Attr.getLoc(), Diag) << Attr.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000182 return false;
183 }
184
185 return true;
186}
187
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000188/// \brief Check if the attribute has exactly as many args as Num. May
189/// output an error.
190static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
191 unsigned Num) {
192 return checkAttributeNumArgsImpl(S, Attr, Num,
193 diag::err_attribute_wrong_number_arguments,
194 std::not_equal_to<unsigned>());
195}
196
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000197/// \brief Check if the attribute has at least as many args as Num. May
198/// output an error.
199static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000200 unsigned Num) {
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000201 return checkAttributeNumArgsImpl(S, Attr, Num,
202 diag::err_attribute_too_few_arguments,
203 std::less<unsigned>());
204}
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000205
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000206/// \brief Check if the attribute has at most as many args as Num. May
207/// output an error.
208static bool checkAttributeAtMostNumArgs(Sema &S, const AttributeList &Attr,
209 unsigned Num) {
210 return checkAttributeNumArgsImpl(S, Attr, Num,
211 diag::err_attribute_too_many_arguments,
212 std::greater<unsigned>());
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000213}
214
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000215/// \brief If Expr is a valid integer constant, get the value of the integer
216/// expression and return success or failure. May output an error.
217static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
218 const Expr *Expr, uint32_t &Val,
219 unsigned Idx = UINT_MAX) {
220 llvm::APSInt I(32);
221 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
222 !Expr->isIntegerConstantExpr(I, S.Context)) {
223 if (Idx != UINT_MAX)
224 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
225 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
226 << Expr->getSourceRange();
227 else
228 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
229 << Attr.getName() << AANT_ArgumentIntegerConstant
230 << Expr->getSourceRange();
231 return false;
232 }
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000233
234 if (!I.isIntN(32)) {
Aaron Ballman31f42312014-07-24 14:51:23 +0000235 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
236 << I.toString(10, false) << 32 << /* Unsigned */ 1;
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000237 return false;
238 }
239
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000240 Val = (uint32_t)I.getZExtValue();
241 return true;
242}
243
Aaron Ballmanfb763042013-12-02 18:05:46 +0000244/// \brief Diagnose mutually exclusive attributes when present on a given
245/// declaration. Returns true if diagnosed.
246template <typename AttrTy>
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +0000247static bool checkAttrMutualExclusion(Sema &S, Decl *D, SourceRange Range,
248 IdentifierInfo *Ident) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000249 if (AttrTy *A = D->getAttr<AttrTy>()) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +0000250 S.Diag(Range.getBegin(), diag::err_attributes_are_not_compatible) << Ident
251 << A;
252 S.Diag(A->getLocation(), diag::note_conflicting_attribute);
Aaron Ballmanfb763042013-12-02 18:05:46 +0000253 return true;
254 }
255 return false;
256}
257
Alp Toker601b22c2014-01-21 23:35:24 +0000258/// \brief Check if IdxExpr is a valid parameter index for a function or
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000259/// instance method D. May output an error.
260///
261/// \returns true if IdxExpr is a valid index.
Alp Toker601b22c2014-01-21 23:35:24 +0000262static bool checkFunctionOrMethodParameterIndex(Sema &S, const Decl *D,
263 const AttributeList &Attr,
264 unsigned AttrArgNum,
265 const Expr *IdxExpr,
266 uint64_t &Idx) {
David Majnemer06864812015-04-07 06:01:53 +0000267 assert(isFunctionOrMethodOrBlock(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000268
269 // In C++ the implicit 'this' function parameter also counts.
270 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000271 bool HP = hasFunctionProto(D);
272 bool HasImplicitThisParam = isInstanceMethod(D);
273 bool IV = HP && isFunctionOrMethodVariadic(D);
Alp Toker601b22c2014-01-21 23:35:24 +0000274 unsigned NumParams =
275 (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000276
277 llvm::APSInt IdxInt;
278 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
279 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000280 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
281 << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant
282 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000283 return false;
284 }
285
286 Idx = IdxInt.getLimitedValue();
Alp Toker601b22c2014-01-21 23:35:24 +0000287 if (Idx < 1 || (!IV && Idx > NumParams)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000288 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
289 << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000290 return false;
291 }
292 Idx--; // Convert to zero-based.
293 if (HasImplicitThisParam) {
294 if (Idx == 0) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000295 S.Diag(Attr.getLoc(),
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000296 diag::err_attribute_invalid_implicit_this_argument)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000297 << Attr.getName() << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000298 return false;
299 }
300 --Idx;
301 }
302
303 return true;
304}
305
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000306/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
307/// If not emit an error and return false. If the argument is an identifier it
308/// will emit an error with a fixit hint and treat it as if it was a string
309/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000310bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
311 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000312 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000313 // Look for identifiers. If we have one emit a hint to fix it to a literal.
314 if (Attr.isArgIdent(ArgNum)) {
315 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000316 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000317 << Attr.getName() << AANT_ArgumentString
318 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000319 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000320 Str = Loc->Ident->getName();
321 if (ArgLocation)
322 *ArgLocation = Loc->Loc;
323 return true;
324 }
325
326 // Now check for an actual string literal.
327 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
328 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
329 if (ArgLocation)
330 *ArgLocation = ArgExpr->getLocStart();
331
332 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000333 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000334 << Attr.getName() << AANT_ArgumentString;
335 return false;
336 }
337
338 Str = Literal->getString();
339 return true;
340}
341
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000342/// \brief Applies the given attribute to the Decl without performing any
343/// additional semantic checking.
344template <typename AttrType>
345static void handleSimpleAttribute(Sema &S, Decl *D,
346 const AttributeList &Attr) {
347 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
348 Attr.getAttributeSpellingListIndex()));
349}
350
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000351/// \brief Check if the passed-in expression is of type int or bool.
352static bool isIntOrBool(Expr *Exp) {
353 QualType QT = Exp->getType();
354 return QT->isBooleanType() || QT->isIntegerType();
355}
356
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000357
358// Check to see if the type is a smart pointer of some kind. We assume
359// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000360static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
Richard Smithcf4bdde2015-02-21 02:45:19 +0000361 DeclContextLookupResult Res1 = RT->getDecl()->lookup(
362 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000363 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000364 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000365
Richard Smithcf4bdde2015-02-21 02:45:19 +0000366 DeclContextLookupResult Res2 = RT->getDecl()->lookup(
367 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000368 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000369 return false;
370
371 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000372}
373
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000374/// \brief Check if passed in Decl is a pointer type.
375/// Note that this function may produce an error message.
376/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000377static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
378 const AttributeList &Attr) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000379 const ValueDecl *vd = cast<ValueDecl>(D);
380 QualType QT = vd->getType();
381 if (QT->isAnyPointerType())
382 return true;
383
384 if (const RecordType *RT = QT->getAs<RecordType>()) {
385 // If it's an incomplete type, it could be a smart pointer; skip it.
386 // (We don't want to force template instantiation if we can avoid it,
387 // since that would alter the order in which templates are instantiated.)
388 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000389 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000390
Aaron Ballman553e6812013-12-26 14:54:11 +0000391 if (threadSafetyCheckIsSmartPointer(S, RT))
392 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000393 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000394
395 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Aaron Ballman68289452013-12-26 15:06:01 +0000396 << Attr.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000397 return false;
398}
399
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000400/// \brief Checks that the passed in QualType either is of RecordType or points
401/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000402static const RecordType *getRecordType(QualType QT) {
403 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000404 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000405
406 // Now check if we point to record type.
407 if (const PointerType *PT = QT->getAs<PointerType>())
408 return PT->getPointeeType()->getAs<RecordType>();
409
Craig Topperc3ec1492014-05-26 06:22:03 +0000410 return nullptr;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000411}
412
Aaron Ballman76050722014-04-04 15:13:57 +0000413static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000414 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000415
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000416 if (!RT)
417 return false;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000418
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000419 // Don't check for the capability if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000420 if (RT->isIncompleteType())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000421 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000422
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000423 // Allow smart pointers to be used as capability objects.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000424 // FIXME -- Check the type that the smart pointer points to.
425 if (threadSafetyCheckIsSmartPointer(S, RT))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000426 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000427
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000428 // Check if the record itself has a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000429 RecordDecl *RD = RT->getDecl();
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000430 if (RD->hasAttr<CapabilityAttr>())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000431 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000432
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000433 // Else check if any base classes have a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000434 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
435 CXXBasePaths BPaths(false, false);
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000436 if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &) {
437 const auto *Type = BS->getType()->getAs<RecordType>();
438 return Type->getDecl()->hasAttr<CapabilityAttr>();
439 }, BPaths))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000440 return true;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000441 }
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000442 return false;
443}
444
Aaron Ballman76050722014-04-04 15:13:57 +0000445static bool checkTypedefTypeForCapability(QualType Ty) {
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000446 const auto *TD = Ty->getAs<TypedefType>();
447 if (!TD)
448 return false;
449
450 TypedefNameDecl *TN = TD->getDecl();
451 if (!TN)
452 return false;
453
454 return TN->hasAttr<CapabilityAttr>();
455}
456
Aaron Ballman76050722014-04-04 15:13:57 +0000457static bool typeHasCapability(Sema &S, QualType Ty) {
458 if (checkTypedefTypeForCapability(Ty))
459 return true;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000460
Aaron Ballman76050722014-04-04 15:13:57 +0000461 if (checkRecordTypeForCapability(S, Ty))
462 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000463
Aaron Ballman76050722014-04-04 15:13:57 +0000464 return false;
465}
466
467static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
468 // Capability expressions are simple expressions involving the boolean logic
469 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
470 // a DeclRefExpr is found, its type should be checked to determine whether it
471 // is a capability or not.
472
473 if (const auto *E = dyn_cast<DeclRefExpr>(Ex))
474 return typeHasCapability(S, E->getType());
475 else if (const auto *E = dyn_cast<CastExpr>(Ex))
476 return isCapabilityExpr(S, E->getSubExpr());
477 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
478 return isCapabilityExpr(S, E->getSubExpr());
479 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
480 if (E->getOpcode() == UO_LNot)
481 return isCapabilityExpr(S, E->getSubExpr());
482 return false;
483 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
484 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
485 return isCapabilityExpr(S, E->getLHS()) &&
486 isCapabilityExpr(S, E->getRHS());
487 return false;
488 }
489
490 return false;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000491}
492
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000493/// \brief Checks that all attribute arguments, starting from Sidx, resolve to
494/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000495/// \param Sidx The attribute argument index to start checking with.
496/// \param ParamIdxOk Whether an argument can be indexing into a function
497/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000498static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
499 const AttributeList &Attr,
500 SmallVectorImpl<Expr *> &Args,
501 int Sidx = 0,
502 bool ParamIdxOk = false) {
503 for (unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000504 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000505
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000506 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000507 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000508 Args.push_back(ArgExp);
509 continue;
510 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000511
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000512 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000513 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000514 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000515 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000516 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000517 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000518 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000519 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000520
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000521 // We allow constant strings to be used as a placeholder for expressions
522 // that are not valid C++ syntax, but warn that they are ignored.
523 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
524 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000525 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000526 continue;
527 }
528
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000529 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000530
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000531 // A pointer to member expression of the form &MyClass::mu is treated
532 // specially -- we need to look at the type of the member.
533 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
534 if (UOp->getOpcode() == UO_AddrOf)
535 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
536 if (DRE->getDecl()->isCXXInstanceMember())
537 ArgTy = DRE->getDecl()->getType();
538
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000539 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000540 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000541
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000542 // Now check if we index into a record type function param.
543 if(!RT && ParamIdxOk) {
544 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000545 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
546 if(FD && IL) {
547 unsigned int NumParams = FD->getNumParams();
548 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000549 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
550 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
551 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000552 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
553 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000554 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000555 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000556 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000557 }
558 }
559
Aaron Ballman76050722014-04-04 15:13:57 +0000560 // If the type does not have a capability, see if the components of the
561 // expression have capabilities. This allows for writing C code where the
562 // capability may be on the type, and the expression is a capability
563 // boolean logic expression. Eg) requires_capability(A || B && !C)
564 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
565 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
566 << Attr.getName() << ArgTy;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000567
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000568 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000569 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000570}
571
Chris Lattner58418ff2008-06-29 00:16:31 +0000572//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000573// Attribute Implementations
574//===----------------------------------------------------------------------===//
575
Michael Hana9171bc2012-08-03 17:40:43 +0000576static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000577 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000578 if (!threadSafetyCheckIsPointer(S, D, Attr))
579 return;
580
Michael Han99315932013-01-24 16:46:58 +0000581 D->addAttr(::new (S.Context)
582 PtGuardedVarAttr(Attr.getRange(), S.Context,
583 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000584}
585
Michael Hana9171bc2012-08-03 17:40:43 +0000586static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
587 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000588 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000589 SmallVector<Expr*, 1> Args;
590 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000591 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000592 unsigned Size = Args.size();
593 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000594 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000595
Michael Han3be3b442012-07-23 18:48:41 +0000596 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000597
Michael Han3be3b442012-07-23 18:48:41 +0000598 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000599}
600
Michael Han3be3b442012-07-23 18:48:41 +0000601static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000602 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000603 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
604 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000605
Aaron Ballman36a53502014-01-16 13:03:14 +0000606 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg,
607 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000608}
609
Michael Hana9171bc2012-08-03 17:40:43 +0000610static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000611 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000612 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000613 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
614 return;
615
616 if (!threadSafetyCheckIsPointer(S, D, Attr))
617 return;
618
619 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +0000620 S.Context, Arg,
621 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000622}
623
Michael Hana9171bc2012-08-03 17:40:43 +0000624static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
625 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000626 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000627 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000628 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000629
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000630 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000631 QualType QT = cast<ValueDecl>(D)->getType();
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000632 if (!QT->isDependentType() && !typeHasCapability(S, QT)) {
633 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
634 << Attr.getName();
635 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000636 }
637
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000638 // Check that all arguments are lockable objects.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000639 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000640 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000641 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000642
Michael Han3be3b442012-07-23 18:48:41 +0000643 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000644}
645
Michael Hana9171bc2012-08-03 17:40:43 +0000646static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000647 const AttributeList &Attr) {
648 SmallVector<Expr*, 1> Args;
649 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
650 return;
651
652 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000653 D->addAttr(::new (S.Context)
654 AcquiredAfterAttr(Attr.getRange(), S.Context,
655 StartArg, Args.size(),
656 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000657}
658
Michael Hana9171bc2012-08-03 17:40:43 +0000659static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000660 const AttributeList &Attr) {
661 SmallVector<Expr*, 1> Args;
662 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
663 return;
664
665 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000666 D->addAttr(::new (S.Context)
667 AcquiredBeforeAttr(Attr.getRange(), S.Context,
668 StartArg, Args.size(),
669 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000670}
671
Michael Hana9171bc2012-08-03 17:40:43 +0000672static bool checkLockFunAttrCommon(Sema &S, Decl *D,
673 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000674 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000675 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000676 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000677 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000678
Michael Han3be3b442012-07-23 18:48:41 +0000679 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000680}
681
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000682static void handleAssertSharedLockAttr(Sema &S, Decl *D,
683 const AttributeList &Attr) {
684 SmallVector<Expr*, 1> Args;
685 if (!checkLockFunAttrCommon(S, D, Attr, Args))
686 return;
687
688 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000689 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000690 D->addAttr(::new (S.Context)
691 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
692 Attr.getAttributeSpellingListIndex()));
693}
694
695static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
696 const AttributeList &Attr) {
697 SmallVector<Expr*, 1> Args;
698 if (!checkLockFunAttrCommon(S, D, Attr, Args))
699 return;
700
701 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000702 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000703 D->addAttr(::new (S.Context)
704 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
705 StartArg, Size,
706 Attr.getAttributeSpellingListIndex()));
707}
708
709
Michael Hana9171bc2012-08-03 17:40:43 +0000710static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
711 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000712 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000713 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000714 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000715
Aaron Ballman00e99962013-08-31 01:11:41 +0000716 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000717 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000718 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000719 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000720 }
721
722 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000723 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000724
Michael Han3be3b442012-07-23 18:48:41 +0000725 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000726}
727
Michael Hana9171bc2012-08-03 17:40:43 +0000728static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000729 const AttributeList &Attr) {
730 SmallVector<Expr*, 2> Args;
731 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
732 return;
733
Michael Han99315932013-01-24 16:46:58 +0000734 D->addAttr(::new (S.Context)
735 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000736 Attr.getArgAsExpr(0),
737 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000738 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000739}
740
Michael Hana9171bc2012-08-03 17:40:43 +0000741static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000742 const AttributeList &Attr) {
743 SmallVector<Expr*, 2> Args;
744 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
745 return;
746
Nico Weber462fd1e2015-01-07 23:50:05 +0000747 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(
748 Attr.getRange(), S.Context, Attr.getArgAsExpr(0), Args.data(),
749 Args.size(), Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000750}
751
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000752static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000753 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000754 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000755 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000756 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000757 unsigned Size = Args.size();
758 if (Size == 0)
759 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000760
Michael Han99315932013-01-24 16:46:58 +0000761 D->addAttr(::new (S.Context)
762 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
763 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000764}
765
766static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000767 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000768 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000769 return;
770
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000771 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000772 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000773 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000774 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000775 if (Size == 0)
776 return;
777 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000778
Michael Han99315932013-01-24 16:46:58 +0000779 D->addAttr(::new (S.Context)
780 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
781 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000782}
783
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000784static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) {
785 Expr *Cond = Attr.getArgAsExpr(0);
786 if (!Cond->isTypeDependent()) {
787 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
788 if (Converted.isInvalid())
789 return;
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000790 Cond = Converted.get();
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000791 }
792
793 StringRef Msg;
794 if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg))
795 return;
796
797 SmallVector<PartialDiagnosticAt, 8> Diags;
798 if (!Cond->isValueDependent() &&
799 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
800 Diags)) {
801 S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr);
802 for (int I = 0, N = Diags.size(); I != N; ++I)
803 S.Diag(Diags[I].first, Diags[I].second);
804 return;
805 }
806
807 D->addAttr(::new (S.Context)
808 EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg,
809 Attr.getAttributeSpellingListIndex()));
810}
811
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000812static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000813 ConsumableAttr::ConsumedState DefaultState;
814
815 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000816 IdentifierLoc *IL = Attr.getArgAsIdent(0);
817 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
818 DefaultState)) {
819 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
820 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000821 return;
822 }
David Blaikie16f76d22013-09-06 01:28:43 +0000823 } else {
824 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
825 << Attr.getName() << AANT_ArgumentIdentifier;
826 return;
827 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000828
829 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000830 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000831 Attr.getAttributeSpellingListIndex()));
832}
833
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000834
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000835static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
836 const AttributeList &Attr) {
837 ASTContext &CurrContext = S.getASTContext();
838 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
839
840 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
841 if (!RD->hasAttr<ConsumableAttr>()) {
842 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
843 RD->getNameAsString();
844
845 return false;
846 }
847 }
848
849 return true;
850}
851
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000852
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000853static void handleCallableWhenAttr(Sema &S, Decl *D,
854 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000855 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
856 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000857
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000858 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
859 return;
860
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000861 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
862 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
863 CallableWhenAttr::ConsumedState CallableState;
864
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000865 StringRef StateString;
866 SourceLocation Loc;
Aaron Ballman55ef1512014-12-19 16:42:04 +0000867 if (Attr.isArgIdent(ArgIndex)) {
868 IdentifierLoc *Ident = Attr.getArgAsIdent(ArgIndex);
869 StateString = Ident->Ident->getName();
870 Loc = Ident->Loc;
871 } else {
872 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
873 return;
874 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000875
876 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000877 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000878 S.Diag(Loc, diag::warn_attribute_type_not_supported)
879 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000880 return;
881 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000882
883 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000884 }
885
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000886 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000887 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
888 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000889}
890
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000891
DeLesley Hutchins69391772013-10-17 23:23:53 +0000892static void handleParamTypestateAttr(Sema &S, Decl *D,
893 const AttributeList &Attr) {
DeLesley Hutchins69391772013-10-17 23:23:53 +0000894 ParamTypestateAttr::ConsumedState ParamState;
895
896 if (Attr.isArgIdent(0)) {
897 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
898 StringRef StateString = Ident->Ident->getName();
899
900 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
901 ParamState)) {
902 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
903 << Attr.getName() << StateString;
904 return;
905 }
906 } else {
907 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
908 Attr.getName() << AANT_ArgumentIdentifier;
909 return;
910 }
911
912 // FIXME: This check is currently being done in the analysis. It can be
913 // enabled here only after the parser propagates attributes at
914 // template specialization definition, not declaration.
915 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
916 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
917 //
918 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
919 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
920 // ReturnType.getAsString();
921 // return;
922 //}
923
924 D->addAttr(::new (S.Context)
925 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
926 Attr.getAttributeSpellingListIndex()));
927}
928
929
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000930static void handleReturnTypestateAttr(Sema &S, Decl *D,
931 const AttributeList &Attr) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000932 ReturnTypestateAttr::ConsumedState ReturnState;
933
934 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000935 IdentifierLoc *IL = Attr.getArgAsIdent(0);
936 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
937 ReturnState)) {
938 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
939 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000940 return;
941 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000942 } else {
943 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
944 Attr.getName() << AANT_ArgumentIdentifier;
945 return;
946 }
947
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000948 // FIXME: This check is currently being done in the analysis. It can be
949 // enabled here only after the parser propagates attributes at
950 // template specialization definition, not declaration.
951 //QualType ReturnType;
952 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000953 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
954 // ReturnType = Param->getType();
955 //
956 //} else if (const CXXConstructorDecl *Constructor =
957 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000958 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
959 //
960 //} else {
961 //
962 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
963 //}
964 //
965 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
966 //
967 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
968 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
969 // ReturnType.getAsString();
970 // return;
971 //}
972
973 D->addAttr(::new (S.Context)
974 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
975 Attr.getAttributeSpellingListIndex()));
976}
977
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000978
979static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000980 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
981 return;
982
983 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000984 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000985 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
986 StringRef Param = Ident->Ident->getName();
987 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
988 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
989 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000990 return;
991 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000992 } else {
993 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
994 Attr.getName() << AANT_ArgumentIdentifier;
995 return;
996 }
997
998 D->addAttr(::new (S.Context)
999 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1000 Attr.getAttributeSpellingListIndex()));
1001}
1002
Chris Wailes9385f9f2013-10-29 20:28:41 +00001003static void handleTestTypestateAttr(Sema &S, Decl *D,
1004 const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001005 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1006 return;
1007
Chris Wailes9385f9f2013-10-29 20:28:41 +00001008 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001009 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001010 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1011 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001012 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001013 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1014 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001015 return;
1016 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001017 } else {
1018 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1019 Attr.getName() << AANT_ArgumentIdentifier;
1020 return;
1021 }
1022
1023 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001024 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001025 Attr.getAttributeSpellingListIndex()));
1026}
1027
Chandler Carruthedc2c642011-07-02 00:01:44 +00001028static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1029 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001030 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001031 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001032}
1033
Chandler Carruthedc2c642011-07-02 00:01:44 +00001034static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001035 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Aaron Ballman36a53502014-01-16 13:03:14 +00001036 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context,
1037 Attr.getAttributeSpellingListIndex()));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001038 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001039 // If the alignment is less than or equal to 8 bits, the packed attribute
1040 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001041 if (!FD->getType()->isDependentType() &&
1042 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001043 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001044 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001045 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001046 else
Michael Han99315932013-01-24 16:46:58 +00001047 FD->addAttr(::new (S.Context)
1048 PackedAttr(Attr.getRange(), S.Context,
1049 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001050 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001051 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001052}
1053
Ted Kremenek7fd17232011-09-29 07:02:25 +00001054static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1055 // The IBOutlet/IBOutletCollection attributes only apply to instance
1056 // variables or properties of Objective-C classes. The outlet must also
1057 // have an object reference type.
1058 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1059 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001060 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001061 << Attr.getName() << VD->getType() << 0;
1062 return false;
1063 }
1064 }
1065 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1066 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001067 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001068 << Attr.getName() << PD->getType() << 1;
1069 return false;
1070 }
1071 }
1072 else {
1073 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1074 return false;
1075 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001076
Ted Kremenek7fd17232011-09-29 07:02:25 +00001077 return true;
1078}
1079
Chandler Carruthedc2c642011-07-02 00:01:44 +00001080static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001081 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001082 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001083
Michael Han99315932013-01-24 16:46:58 +00001084 D->addAttr(::new (S.Context)
1085 IBOutletAttr(Attr.getRange(), S.Context,
1086 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001087}
1088
Chandler Carruthedc2c642011-07-02 00:01:44 +00001089static void handleIBOutletCollection(Sema &S, Decl *D,
1090 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001091
1092 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001093 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001094 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1095 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001096 return;
1097 }
1098
Ted Kremenek7fd17232011-09-29 07:02:25 +00001099 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001100 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001101
Richard Smithb1f9a282013-10-31 01:56:18 +00001102 ParsedType PT;
1103
1104 if (Attr.hasParsedType())
1105 PT = Attr.getTypeArg();
1106 else {
1107 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1108 S.getScopeForContext(D->getDeclContext()->getParent()));
1109 if (!PT) {
1110 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1111 return;
1112 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001113 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001114
Craig Topperc3ec1492014-05-26 06:22:03 +00001115 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001116 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1117 if (!QTLoc)
1118 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001119
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001120 // Diagnose use of non-object type in iboutletcollection attribute.
1121 // FIXME. Gnu attribute extension ignores use of builtin types in
1122 // attributes. So, __attribute__((iboutletcollection(char))) will be
1123 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001124 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001125 S.Diag(Attr.getLoc(),
1126 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1127 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001128 return;
1129 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001130
Michael Han99315932013-01-24 16:46:58 +00001131 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001132 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001133 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001134}
1135
Hal Finkelee90a222014-09-26 05:04:30 +00001136bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1137 if (RefOkay) {
1138 if (T->isReferenceType())
1139 return true;
1140 } else {
1141 T = T.getNonReferenceType();
1142 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001143
Hal Finkelee90a222014-09-26 05:04:30 +00001144 // The nonnull attribute, and other similar attributes, can be applied to a
1145 // transparent union that contains a pointer type.
Richard Smith588bd9b2014-08-27 04:59:42 +00001146 if (const RecordType *UT = T->getAsUnionType()) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001147 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1148 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001149 for (const auto *I : UD->fields()) {
1150 QualType QT = I->getType();
Richard Smith588bd9b2014-08-27 04:59:42 +00001151 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1152 return true;
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001153 }
1154 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001155 }
1156
1157 return T->isAnyPointerType() || T->isBlockPointerType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001158}
1159
Ted Kremenek9aedc152014-01-17 06:24:56 +00001160static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001161 SourceRange AttrParmRange,
Hal Finkelee90a222014-09-26 05:04:30 +00001162 SourceRange TypeRange,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001163 bool isReturnValue = false) {
Hal Finkelee90a222014-09-26 05:04:30 +00001164 if (!S.isValidPointerAttrType(T)) {
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001165 S.Diag(Attr.getLoc(), isReturnValue
1166 ? diag::warn_attribute_return_pointers_only
1167 : diag::warn_attribute_pointers_only)
Hal Finkelee90a222014-09-26 05:04:30 +00001168 << Attr.getName() << AttrParmRange << TypeRange;
Ted Kremenek9aedc152014-01-17 06:24:56 +00001169 return false;
1170 }
1171 return true;
1172}
1173
Chandler Carruthedc2c642011-07-02 00:01:44 +00001174static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001175 SmallVector<unsigned, 8> NonNullArgs;
Richard Smith588bd9b2014-08-27 04:59:42 +00001176 for (unsigned I = 0; I < Attr.getNumArgs(); ++I) {
1177 Expr *Ex = Attr.getArgAsExpr(I);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001178 uint64_t Idx;
Richard Smith588bd9b2014-08-27 04:59:42 +00001179 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, I + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001180 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001181
1182 // Is the function argument a pointer type?
Richard Smith588bd9b2014-08-27 04:59:42 +00001183 if (Idx < getFunctionOrMethodNumParams(D) &&
1184 !attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001185 Ex->getSourceRange(),
1186 getFunctionOrMethodParamRange(D, Idx)))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001187 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001188
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001189 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001190 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001191
1192 // If no arguments were specified to __attribute__((nonnull)) then all pointer
Richard Smith588bd9b2014-08-27 04:59:42 +00001193 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1194 // check if the attribute came from a macro expansion or a template
1195 // instantiation.
1196 if (NonNullArgs.empty() && Attr.getLoc().isFileID() &&
1197 S.ActiveTemplateInstantiations.empty()) {
1198 bool AnyPointers = isFunctionOrMethodVariadic(D);
1199 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1200 I != E && !AnyPointers; ++I) {
1201 QualType T = getFunctionOrMethodParamType(D, I);
Hal Finkelee90a222014-09-26 05:04:30 +00001202 if (T->isDependentType() || S.isValidPointerAttrType(T))
Richard Smith588bd9b2014-08-27 04:59:42 +00001203 AnyPointers = true;
Ted Kremenek5fa50522008-11-18 06:52:58 +00001204 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001205
Richard Smith588bd9b2014-08-27 04:59:42 +00001206 if (!AnyPointers)
1207 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001208 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001209
Richard Smith588bd9b2014-08-27 04:59:42 +00001210 unsigned *Start = NonNullArgs.data();
1211 unsigned Size = NonNullArgs.size();
1212 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001213 D->addAttr(::new (S.Context)
Richard Smith588bd9b2014-08-27 04:59:42 +00001214 NonNullAttr(Attr.getRange(), S.Context, Start, Size,
Michael Han99315932013-01-24 16:46:58 +00001215 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001216}
1217
Jordan Rosec9399072014-02-11 17:27:59 +00001218static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
1219 const AttributeList &Attr) {
1220 if (Attr.getNumArgs() > 0) {
1221 if (D->getFunctionType()) {
1222 handleNonNullAttr(S, D, Attr);
1223 } else {
1224 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1225 << D->getSourceRange();
1226 }
1227 return;
1228 }
1229
1230 // Is the argument a pointer type?
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001231 if (!attrNonNullArgCheck(S, D->getType(), Attr, SourceRange(),
1232 D->getSourceRange()))
Jordan Rosec9399072014-02-11 17:27:59 +00001233 return;
1234
1235 D->addAttr(::new (S.Context)
Craig Topperc3ec1492014-05-26 06:22:03 +00001236 NonNullAttr(Attr.getRange(), S.Context, nullptr, 0,
Jordan Rosec9399072014-02-11 17:27:59 +00001237 Attr.getAttributeSpellingListIndex()));
1238}
1239
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001240static void handleReturnsNonNullAttr(Sema &S, Decl *D,
1241 const AttributeList &Attr) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001242 QualType ResultType = getFunctionOrMethodResultType(D);
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001243 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1244 if (!attrNonNullArgCheck(S, ResultType, Attr, SourceRange(), SR,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001245 /* isReturnValue */ true))
1246 return;
1247
1248 D->addAttr(::new (S.Context)
1249 ReturnsNonNullAttr(Attr.getRange(), S.Context,
1250 Attr.getAttributeSpellingListIndex()));
1251}
1252
Hal Finkelee90a222014-09-26 05:04:30 +00001253static void handleAssumeAlignedAttr(Sema &S, Decl *D,
1254 const AttributeList &Attr) {
1255 Expr *E = Attr.getArgAsExpr(0),
1256 *OE = Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr;
1257 S.AddAssumeAlignedAttr(Attr.getRange(), D, E, OE,
1258 Attr.getAttributeSpellingListIndex());
1259}
1260
1261void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
1262 Expr *OE, unsigned SpellingListIndex) {
1263 QualType ResultType = getFunctionOrMethodResultType(D);
1264 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1265
1266 AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex);
1267 SourceLocation AttrLoc = AttrRange.getBegin();
1268
1269 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1270 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1271 << &TmpAttr << AttrRange << SR;
1272 return;
1273 }
1274
1275 if (!E->isValueDependent()) {
1276 llvm::APSInt I(64);
1277 if (!E->isIntegerConstantExpr(I, Context)) {
1278 if (OE)
1279 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1280 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1281 << E->getSourceRange();
1282 else
1283 Diag(AttrLoc, diag::err_attribute_argument_type)
1284 << &TmpAttr << AANT_ArgumentIntegerConstant
1285 << E->getSourceRange();
1286 return;
1287 }
1288
1289 if (!I.isPowerOf2()) {
1290 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1291 << E->getSourceRange();
1292 return;
1293 }
1294 }
1295
1296 if (OE) {
1297 if (!OE->isValueDependent()) {
1298 llvm::APSInt I(64);
1299 if (!OE->isIntegerConstantExpr(I, Context)) {
1300 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1301 << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1302 << OE->getSourceRange();
1303 return;
1304 }
1305 }
1306 }
1307
1308 D->addAttr(::new (Context)
1309 AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex));
1310}
1311
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001312/// Normalize the attribute, __foo__ becomes foo.
1313/// Returns true if normalization was applied.
1314static bool normalizeName(StringRef &AttrName) {
Aaron Ballman62692362015-10-09 13:53:24 +00001315 if (AttrName.size() > 4 && AttrName.startswith("__") &&
1316 AttrName.endswith("__")) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001317 AttrName = AttrName.drop_front(2).drop_back(2);
1318 return true;
1319 }
1320 return false;
1321}
1322
Chandler Carruthedc2c642011-07-02 00:01:44 +00001323static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001324 // This attribute must be applied to a function declaration. The first
1325 // argument to the attribute must be an identifier, the name of the resource,
1326 // for example: malloc. The following arguments must be argument indexes, the
1327 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001328 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001329 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001330 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001331
Aaron Ballman00e99962013-08-31 01:11:41 +00001332 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001333 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001334 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001335 return;
1336 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001337
Richard Smith852e9ce2013-11-27 01:46:48 +00001338 // Figure out our Kind.
1339 OwnershipAttr::OwnershipKind K =
Craig Topperc3ec1492014-05-26 06:22:03 +00001340 OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0,
Richard Smith852e9ce2013-11-27 01:46:48 +00001341 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001342
Richard Smith852e9ce2013-11-27 01:46:48 +00001343 // Check arguments.
1344 switch (K) {
1345 case OwnershipAttr::Takes:
1346 case OwnershipAttr::Holds:
1347 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001348 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1349 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001350 return;
1351 }
1352 break;
1353 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001354 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001355 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1356 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001357 return;
1358 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001359 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001360 }
1361
Richard Smith852e9ce2013-11-27 01:46:48 +00001362 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001363
Richard Smith852e9ce2013-11-27 01:46:48 +00001364 StringRef ModuleName = Module->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001365 if (normalizeName(ModuleName)) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001366 Module = &S.PP.getIdentifierTable().get(ModuleName);
1367 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001368
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001369 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001370 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1371 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001372 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001373 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001374 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001375
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001376 // Is the function argument a pointer type?
Alp Toker601b22c2014-01-21 23:35:24 +00001377 QualType T = getFunctionOrMethodParamType(D, Idx);
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001378 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001379 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001380 case OwnershipAttr::Takes:
1381 case OwnershipAttr::Holds:
1382 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1383 Err = 0;
1384 break;
1385 case OwnershipAttr::Returns:
1386 if (!T->isIntegerType())
1387 Err = 1;
1388 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001389 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001390 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001391 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001392 << Ex->getSourceRange();
1393 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001394 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001395
1396 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001397 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001398 // Cannot have two ownership attributes of different kinds for the same
1399 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001400 if (I->getOwnKind() != K && I->args_end() !=
1401 std::find(I->args_begin(), I->args_end(), Idx)) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001402 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001403 << AL.getName() << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001404 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001405 } else if (K == OwnershipAttr::Returns &&
1406 I->getOwnKind() == OwnershipAttr::Returns) {
1407 // A returns attribute conflicts with any other returns attribute using
1408 // a different index. Note, diagnostic reporting is 1-based, but stored
1409 // argument indexes are 0-based.
1410 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1411 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
1412 << *(I->args_begin()) + 1;
1413 if (I->args_size())
1414 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
1415 << (unsigned)Idx + 1 << Ex->getSourceRange();
1416 return;
1417 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001418 }
1419 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001420 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001421 }
1422
1423 unsigned* start = OwnershipArgs.data();
1424 unsigned size = OwnershipArgs.size();
1425 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001426
Michael Han99315932013-01-24 16:46:58 +00001427 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001428 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001429 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001430}
1431
Chandler Carruthedc2c642011-07-02 00:01:44 +00001432static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001433 // Check the attribute arguments.
1434 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001435 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1436 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001437 return;
1438 }
1439
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001440 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001441
Rafael Espindolac18086a2010-02-23 22:00:30 +00001442 // gcc rejects
1443 // class c {
1444 // static int a __attribute__((weakref ("v2")));
1445 // static int b() __attribute__((weakref ("f3")));
1446 // };
1447 // and ignores the attributes of
1448 // void f(void) {
1449 // static int a __attribute__((weakref ("v2")));
1450 // }
1451 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001452 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001453 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001454 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1455 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001456 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001457 }
1458
1459 // The GCC manual says
1460 //
1461 // At present, a declaration to which `weakref' is attached can only
1462 // be `static'.
1463 //
1464 // It also says
1465 //
1466 // Without a TARGET,
1467 // given as an argument to `weakref' or to `alias', `weakref' is
1468 // equivalent to `weak'.
1469 //
1470 // gcc 4.4.1 will accept
1471 // int a7 __attribute__((weakref));
1472 // as
1473 // int a7 __attribute__((weak));
1474 // This looks like a bug in gcc. We reject that for now. We should revisit
1475 // it if this behaviour is actually used.
1476
Rafael Espindolac18086a2010-02-23 22:00:30 +00001477 // GCC rejects
1478 // static ((alias ("y"), weakref)).
1479 // Should we? How to check that weakref is before or after alias?
1480
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001481 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1482 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1483 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001484 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001485 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001486 // GCC will accept anything as the argument of weakref. Should we
1487 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001488 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1489 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001490
Michael Han99315932013-01-24 16:46:58 +00001491 D->addAttr(::new (S.Context)
1492 WeakRefAttr(Attr.getRange(), S.Context,
1493 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001494}
1495
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001496static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1497 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001498 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001499 return;
1500
Douglas Gregore8bbc122011-09-02 00:18:52 +00001501 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001502 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1503 return;
1504 }
1505
David Majnemer2dc81462015-01-19 09:00:28 +00001506 // Aliases should be on declarations, not definitions.
1507 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1508 if (FD->isThisDeclarationADefinition()) {
1509 S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << FD;
1510 return;
1511 }
1512 } else {
1513 const auto *VD = cast<VarDecl>(D);
1514 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
1515 S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << VD;
1516 return;
1517 }
1518 }
1519
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001520 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001521
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001522 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001523 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001524}
1525
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001526static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001527 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr.getRange(), Attr.getName()))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001528 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001529
Michael Han99315932013-01-24 16:46:58 +00001530 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1531 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001532}
1533
1534static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001535 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr.getRange(), Attr.getName()))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001536 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001537
Michael Han99315932013-01-24 16:46:58 +00001538 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1539 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001540}
1541
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001542static void handleTLSModelAttr(Sema &S, Decl *D,
1543 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001544 StringRef Model;
1545 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001546 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001547 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001548 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001549
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001550 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001551 if (Model != "global-dynamic" && Model != "local-dynamic"
1552 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001553 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001554 return;
1555 }
1556
Michael Han99315932013-01-24 16:46:58 +00001557 D->addAttr(::new (S.Context)
1558 TLSModelAttr(Attr.getRange(), S.Context, Model,
1559 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001560}
1561
David Majnemer631a90b2015-02-04 07:23:21 +00001562static void handleRestrictAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1563 QualType ResultType = getFunctionOrMethodResultType(D);
1564 if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
1565 D->addAttr(::new (S.Context) RestrictAttr(
1566 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1567 return;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001568 }
1569
David Majnemer631a90b2015-02-04 07:23:21 +00001570 S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only)
1571 << Attr.getName() << getFunctionOrMethodResultSourceRange(D);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001572}
1573
Chandler Carruthedc2c642011-07-02 00:01:44 +00001574static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001575 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001576 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001577 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001578 return;
1579 }
1580
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001581 if (CommonAttr *CA = S.mergeCommonAttr(D, Attr.getRange(), Attr.getName(),
1582 Attr.getAttributeSpellingListIndex()))
1583 D->addAttr(CA);
Eric Christopher8a2ee392010-12-02 02:45:55 +00001584}
1585
Chandler Carruthedc2c642011-07-02 00:01:44 +00001586static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001587 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001588
1589 if (S.CheckNoReturnAttr(attr)) return;
1590
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001591 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001592 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001593 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001594 return;
1595 }
1596
Michael Han99315932013-01-24 16:46:58 +00001597 D->addAttr(::new (S.Context)
1598 NoReturnAttr(attr.getRange(), S.Context,
1599 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001600}
1601
1602bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001603 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001604 attr.setInvalid();
1605 return true;
1606 }
1607
1608 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001609}
1610
Chandler Carruthedc2c642011-07-02 00:01:44 +00001611static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1612 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001613
1614 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1615 // because 'analyzer_noreturn' does not impact the type.
David Majnemer06864812015-04-07 06:01:53 +00001616 if (!isFunctionOrMethodOrBlock(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001617 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00001618 if (!VD || (!VD->getType()->isBlockPointerType() &&
1619 !VD->getType()->isFunctionPointerType())) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001620 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001621 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001622 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001623 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001624 return;
1625 }
1626 }
1627
Michael Han99315932013-01-24 16:46:58 +00001628 D->addAttr(::new (S.Context)
1629 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1630 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001631}
1632
John Thompsoncdb847ba2010-08-09 21:53:52 +00001633// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001634static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001635/*
1636 Returning a Vector Class in Registers
1637
Eric Christopherbc638a82010-12-01 22:13:54 +00001638 According to the PPU ABI specifications, a class with a single member of
1639 vector type is returned in memory when used as the return value of a function.
1640 This results in inefficient code when implementing vector classes. To return
1641 the value in a single vector register, add the vecreturn attribute to the
1642 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001643
1644 Example:
1645
1646 struct Vector
1647 {
1648 __vector float xyzw;
1649 } __attribute__((vecreturn));
1650
1651 Vector Add(Vector lhs, Vector rhs)
1652 {
1653 Vector result;
1654 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1655 return result; // This will be returned in a register
1656 }
1657*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001658 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1659 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001660 return;
1661 }
1662
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001663 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001664 int count = 0;
1665
1666 if (!isa<CXXRecordDecl>(record)) {
1667 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1668 return;
1669 }
1670
1671 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1672 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1673 return;
1674 }
1675
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001676 for (const auto *I : record->fields()) {
1677 if ((count == 1) || !I->getType()->isVectorType()) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001678 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1679 return;
1680 }
1681 count++;
1682 }
1683
Michael Han99315932013-01-24 16:46:58 +00001684 D->addAttr(::new (S.Context)
1685 VecReturnAttr(Attr.getRange(), S.Context,
1686 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001687}
1688
Richard Smithe233fbf2013-01-28 22:42:45 +00001689static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1690 const AttributeList &Attr) {
1691 if (isa<ParmVarDecl>(D)) {
1692 // [[carries_dependency]] can only be applied to a parameter if it is a
1693 // parameter of a function declaration or lambda.
1694 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1695 S.Diag(Attr.getLoc(),
1696 diag::err_carries_dependency_param_not_function_decl);
1697 return;
1698 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001699 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001700
1701 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1702 Attr.getRange(), S.Context,
1703 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001704}
1705
Akira Hatanakac8667622015-11-06 23:56:15 +00001706static void handleNotTailCalledAttr(Sema &S, Decl *D,
1707 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001708 if (checkAttrMutualExclusion<AlwaysInlineAttr>(S, D, Attr.getRange(),
1709 Attr.getName()))
Akira Hatanakac8667622015-11-06 23:56:15 +00001710 return;
1711
1712 D->addAttr(::new (S.Context) NotTailCalledAttr(
1713 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1714}
1715
Chandler Carruthedc2c642011-07-02 00:01:44 +00001716static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001717 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001718 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001719 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001720 return;
1721 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001722 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001723 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001724 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001725 return;
1726 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001727
Michael Han99315932013-01-24 16:46:58 +00001728 D->addAttr(::new (S.Context)
1729 UsedAttr(Attr.getRange(), S.Context,
1730 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001731}
1732
Chandler Carruthedc2c642011-07-02 00:01:44 +00001733static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001734 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001735 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001736 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1737 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001738
Michael Han99315932013-01-24 16:46:58 +00001739 D->addAttr(::new (S.Context)
1740 ConstructorAttr(Attr.getRange(), S.Context, priority,
1741 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001742}
1743
Chandler Carruthedc2c642011-07-02 00:01:44 +00001744static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00001745 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001746 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001747 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1748 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001749
Michael Han99315932013-01-24 16:46:58 +00001750 D->addAttr(::new (S.Context)
1751 DestructorAttr(Attr.getRange(), S.Context, priority,
1752 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001753}
1754
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001755template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001756static void handleAttrWithMessage(Sema &S, Decl *D,
1757 const AttributeList &Attr) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001758 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001759 StringRef Str;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001760 if (Attr.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001761 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001762
Michael Han99315932013-01-24 16:46:58 +00001763 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1764 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001765}
1766
Ted Kremenek438f8db2014-02-22 01:06:05 +00001767static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Ted Kremenek28eace62013-11-23 01:01:34 +00001768 const AttributeList &Attr) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00001769 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Ted Kremenek27cfe102014-02-21 22:49:04 +00001770 S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition)
1771 << Attr.getName() << Attr.getRange();
1772 return;
1773 }
1774
Ted Kremenek28eace62013-11-23 01:01:34 +00001775 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001776 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1777 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001778}
1779
Jordy Rose740b0c22012-05-08 03:27:22 +00001780static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1781 IdentifierInfo *Platform,
1782 VersionTuple Introduced,
1783 VersionTuple Deprecated,
1784 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001785 StringRef PlatformName
1786 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1787 if (PlatformName.empty())
1788 PlatformName = Platform->getName();
1789
1790 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1791 // of these steps are needed).
1792 if (!Introduced.empty() && !Deprecated.empty() &&
1793 !(Introduced <= Deprecated)) {
1794 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1795 << 1 << PlatformName << Deprecated.getAsString()
1796 << 0 << Introduced.getAsString();
1797 return true;
1798 }
1799
1800 if (!Introduced.empty() && !Obsoleted.empty() &&
1801 !(Introduced <= Obsoleted)) {
1802 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1803 << 2 << PlatformName << Obsoleted.getAsString()
1804 << 0 << Introduced.getAsString();
1805 return true;
1806 }
1807
1808 if (!Deprecated.empty() && !Obsoleted.empty() &&
1809 !(Deprecated <= Obsoleted)) {
1810 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1811 << 2 << PlatformName << Obsoleted.getAsString()
1812 << 1 << Deprecated.getAsString();
1813 return true;
1814 }
1815
1816 return false;
1817}
1818
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001819/// \brief Check whether the two versions match.
1820///
1821/// If either version tuple is empty, then they are assumed to match. If
1822/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1823static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1824 bool BeforeIsOkay) {
1825 if (X.empty() || Y.empty())
1826 return true;
1827
1828 if (X == Y)
1829 return true;
1830
1831 if (BeforeIsOkay && X < Y)
1832 return true;
1833
1834 return false;
1835}
1836
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001837AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001838 IdentifierInfo *Platform,
1839 VersionTuple Introduced,
1840 VersionTuple Deprecated,
1841 VersionTuple Obsoleted,
1842 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001843 StringRef Message,
Douglas Gregord2a713e2015-09-30 21:27:42 +00001844 AvailabilityMergeKind AMK,
Michael Han99315932013-01-24 16:46:58 +00001845 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001846 VersionTuple MergedIntroduced = Introduced;
1847 VersionTuple MergedDeprecated = Deprecated;
1848 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001849 bool FoundAny = false;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001850 bool OverrideOrImpl = false;
1851 switch (AMK) {
1852 case AMK_None:
1853 case AMK_Redeclaration:
1854 OverrideOrImpl = false;
1855 break;
1856
1857 case AMK_Override:
1858 case AMK_ProtocolImplementation:
1859 OverrideOrImpl = true;
1860 break;
1861 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001862
Rafael Espindolac67f2232012-05-10 02:50:16 +00001863 if (D->hasAttrs()) {
1864 AttrVec &Attrs = D->getAttrs();
1865 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1866 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1867 if (!OldAA) {
1868 ++i;
1869 continue;
1870 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001871
Rafael Espindolac67f2232012-05-10 02:50:16 +00001872 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1873 if (OldPlatform != Platform) {
1874 ++i;
1875 continue;
1876 }
1877
Tim Northover7a73cc72015-10-30 16:30:49 +00001878 // If there is an existing availability attribute for this platform that
1879 // is explicit and the new one is implicit use the explicit one and
1880 // discard the new implicit attribute.
1881 if (OldAA->getRange().isValid() && Range.isInvalid()) {
1882 return nullptr;
1883 }
1884
1885 // If there is an existing attribute for this platform that is implicit
1886 // and the new attribute is explicit then erase the old one and
1887 // continue processing the attributes.
1888 if (Range.isValid() && OldAA->getRange().isInvalid()) {
1889 Attrs.erase(Attrs.begin() + i);
1890 --e;
1891 continue;
1892 }
1893
Rafael Espindolac67f2232012-05-10 02:50:16 +00001894 FoundAny = true;
1895 VersionTuple OldIntroduced = OldAA->getIntroduced();
1896 VersionTuple OldDeprecated = OldAA->getDeprecated();
1897 VersionTuple OldObsoleted = OldAA->getObsoleted();
1898 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001899
Douglas Gregord2a713e2015-09-30 21:27:42 +00001900 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
1901 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
1902 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001903 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregord2a713e2015-09-30 21:27:42 +00001904 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
1905 if (OverrideOrImpl) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001906 int Which = -1;
1907 VersionTuple FirstVersion;
1908 VersionTuple SecondVersion;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001909 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001910 Which = 0;
1911 FirstVersion = OldIntroduced;
1912 SecondVersion = Introduced;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001913 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001914 Which = 1;
1915 FirstVersion = Deprecated;
1916 SecondVersion = OldDeprecated;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001917 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001918 Which = 2;
1919 FirstVersion = Obsoleted;
1920 SecondVersion = OldObsoleted;
1921 }
1922
1923 if (Which == -1) {
1924 Diag(OldAA->getLocation(),
1925 diag::warn_mismatched_availability_override_unavail)
Douglas Gregord2a713e2015-09-30 21:27:42 +00001926 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1927 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001928 } else {
1929 Diag(OldAA->getLocation(),
1930 diag::warn_mismatched_availability_override)
1931 << Which
1932 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
Douglas Gregord2a713e2015-09-30 21:27:42 +00001933 << FirstVersion.getAsString() << SecondVersion.getAsString()
1934 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001935 }
Douglas Gregord2a713e2015-09-30 21:27:42 +00001936 if (AMK == AMK_Override)
1937 Diag(Range.getBegin(), diag::note_overridden_method);
1938 else
1939 Diag(Range.getBegin(), diag::note_protocol_method);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001940 } else {
1941 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1942 Diag(Range.getBegin(), diag::note_previous_attribute);
1943 }
1944
Rafael Espindolac67f2232012-05-10 02:50:16 +00001945 Attrs.erase(Attrs.begin() + i);
1946 --e;
1947 continue;
1948 }
1949
1950 VersionTuple MergedIntroduced2 = MergedIntroduced;
1951 VersionTuple MergedDeprecated2 = MergedDeprecated;
1952 VersionTuple MergedObsoleted2 = MergedObsoleted;
1953
1954 if (MergedIntroduced2.empty())
1955 MergedIntroduced2 = OldIntroduced;
1956 if (MergedDeprecated2.empty())
1957 MergedDeprecated2 = OldDeprecated;
1958 if (MergedObsoleted2.empty())
1959 MergedObsoleted2 = OldObsoleted;
1960
1961 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1962 MergedIntroduced2, MergedDeprecated2,
1963 MergedObsoleted2)) {
1964 Attrs.erase(Attrs.begin() + i);
1965 --e;
1966 continue;
1967 }
1968
1969 MergedIntroduced = MergedIntroduced2;
1970 MergedDeprecated = MergedDeprecated2;
1971 MergedObsoleted = MergedObsoleted2;
1972 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001973 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001974 }
1975
1976 if (FoundAny &&
1977 MergedIntroduced == Introduced &&
1978 MergedDeprecated == Deprecated &&
1979 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00001980 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001981
Douglas Gregord2a713e2015-09-30 21:27:42 +00001982 // Only create a new attribute if !OverrideOrImpl, but we want to do
Ted Kremenekb5445722013-04-06 00:34:27 +00001983 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001984 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001985 MergedDeprecated, MergedObsoleted) &&
Douglas Gregord2a713e2015-09-30 21:27:42 +00001986 !OverrideOrImpl) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001987 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1988 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001989 Obsoleted, IsUnavailable, Message,
1990 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001991 }
Craig Topperc3ec1492014-05-26 06:22:03 +00001992 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001993}
1994
Chandler Carruthedc2c642011-07-02 00:01:44 +00001995static void handleAvailabilityAttr(Sema &S, Decl *D,
1996 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001997 if (!checkAttributeNumArgs(S, Attr, 1))
1998 return;
1999 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00002000 unsigned Index = Attr.getAttributeSpellingListIndex();
2001
Aaron Ballman00e99962013-08-31 01:11:41 +00002002 IdentifierInfo *II = Platform->Ident;
2003 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2004 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2005 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002006
Rafael Espindolac231fab2013-01-08 21:30:32 +00002007 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2008 if (!ND) {
2009 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2010 return;
2011 }
2012
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002013 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2014 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2015 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002016 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002017 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00002018 if (const StringLiteral *SE =
2019 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002020 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002021
Aaron Ballman00e99962013-08-31 01:11:41 +00002022 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002023 Introduced.Version,
2024 Deprecated.Version,
2025 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002026 IsUnavailable, Str,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002027 Sema::AMK_None,
Michael Han99315932013-01-24 16:46:58 +00002028 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002029 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002030 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002031
2032 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2033 // matches before the start of the watchOS platform.
2034 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2035 IdentifierInfo *NewII = nullptr;
2036 if (II->getName() == "ios")
2037 NewII = &S.Context.Idents.get("watchos");
2038 else if (II->getName() == "ios_app_extension")
2039 NewII = &S.Context.Idents.get("watchos_app_extension");
2040
2041 if (NewII) {
2042 auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple {
2043 if (Version.empty())
2044 return Version;
2045 auto Major = Version.getMajor();
2046 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2047 if (NewMajor >= 2) {
2048 if (Version.getMinor().hasValue()) {
2049 if (Version.getSubminor().hasValue())
2050 return VersionTuple(NewMajor, Version.getMinor().getValue(),
2051 Version.getSubminor().getValue());
2052 else
2053 return VersionTuple(NewMajor, Version.getMinor().getValue());
2054 }
2055 }
2056
2057 return VersionTuple(2, 0);
2058 };
2059
2060 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2061 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2062 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2063
2064 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
2065 SourceRange(),
2066 NewII,
2067 NewIntroduced,
2068 NewDeprecated,
2069 NewObsoleted,
2070 IsUnavailable, Str,
2071 Sema::AMK_None,
2072 Index);
2073 if (NewAttr)
2074 D->addAttr(NewAttr);
2075 }
2076 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2077 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2078 // matches before the start of the tvOS platform.
2079 IdentifierInfo *NewII = nullptr;
2080 if (II->getName() == "ios")
2081 NewII = &S.Context.Idents.get("tvos");
2082 else if (II->getName() == "ios_app_extension")
2083 NewII = &S.Context.Idents.get("tvos_app_extension");
2084
2085 if (NewII) {
2086 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
2087 SourceRange(),
2088 NewII,
2089 Introduced.Version,
2090 Deprecated.Version,
2091 Obsoleted.Version,
2092 IsUnavailable, Str,
2093 Sema::AMK_None,
2094 Index);
2095 if (NewAttr)
2096 D->addAttr(NewAttr);
2097 }
2098 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002099}
2100
John McCalld041a9b2013-02-20 01:54:26 +00002101template <class T>
2102static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2103 typename T::VisibilityType value,
2104 unsigned attrSpellingListIndex) {
2105 T *existingAttr = D->getAttr<T>();
2106 if (existingAttr) {
2107 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2108 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00002109 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00002110 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2111 S.Diag(range.getBegin(), diag::note_previous_attribute);
2112 D->dropAttr<T>();
2113 }
2114 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2115}
2116
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002117VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002118 VisibilityAttr::VisibilityType Vis,
2119 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002120 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2121 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002122}
2123
John McCalld041a9b2013-02-20 01:54:26 +00002124TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2125 TypeVisibilityAttr::VisibilityType Vis,
2126 unsigned AttrSpellingListIndex) {
2127 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2128 AttrSpellingListIndex);
2129}
2130
2131static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2132 bool isTypeVisibility) {
2133 // Visibility attributes don't mean anything on a typedef.
2134 if (isa<TypedefNameDecl>(D)) {
2135 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2136 << Attr.getName();
2137 return;
2138 }
2139
2140 // 'type_visibility' can only go on a type or namespace.
2141 if (isTypeVisibility &&
2142 !(isa<TagDecl>(D) ||
2143 isa<ObjCInterfaceDecl>(D) ||
2144 isa<NamespaceDecl>(D))) {
2145 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2146 << Attr.getName() << ExpectedTypeOrNamespace;
2147 return;
2148 }
2149
Benjamin Kramer70370212013-09-09 15:08:57 +00002150 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002151 StringRef TypeStr;
2152 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002153 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002154 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002155
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002156 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002157 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002158 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002159 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002160 return;
2161 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002162
2163 // Complain about attempts to use protected visibility on targets
2164 // (like Darwin) that don't support it.
2165 if (type == VisibilityAttr::Protected &&
2166 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2167 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2168 type = VisibilityAttr::Default;
2169 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002170
Michael Han99315932013-01-24 16:46:58 +00002171 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002172 clang::Attr *newAttr;
2173 if (isTypeVisibility) {
2174 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2175 (TypeVisibilityAttr::VisibilityType) type,
2176 Index);
2177 } else {
2178 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2179 }
2180 if (newAttr)
2181 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002182}
2183
Chandler Carruthedc2c642011-07-02 00:01:44 +00002184static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2185 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002186 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002187 if (!Attr.isArgIdent(0)) {
2188 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2189 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002190 return;
2191 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002192
Aaron Ballman682ee422013-09-11 19:47:58 +00002193 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2194 ObjCMethodFamilyAttr::FamilyKind F;
2195 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2196 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2197 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002198 return;
2199 }
2200
Alp Toker314cc812014-01-25 16:55:45 +00002201 if (F == ObjCMethodFamilyAttr::OMF_init &&
2202 !method->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00002203 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
Alp Toker314cc812014-01-25 16:55:45 +00002204 << method->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002205 // Ignore the attribute.
2206 return;
2207 }
2208
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002209 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00002210 S.Context, F,
2211 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002212}
2213
Chandler Carruthedc2c642011-07-02 00:01:44 +00002214static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002215 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002216 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002217 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002218 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2219 return;
2220 }
2221 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002222 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2223 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002224 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002225 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2226 return;
2227 }
2228 }
2229 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002230 // It is okay to include this attribute on properties, e.g.:
2231 //
2232 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2233 //
2234 // In this case it follows tradition and suppresses an error in the above
2235 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002236 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002237 }
Michael Han99315932013-01-24 16:46:58 +00002238 D->addAttr(::new (S.Context)
2239 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2240 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002241}
2242
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002243static void handleObjCIndependentClass(Sema &S, Decl *D, const AttributeList &Attr) {
2244 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
2245 QualType T = TD->getUnderlyingType();
2246 if (!T->isObjCObjectPointerType()) {
2247 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2248 return;
2249 }
2250 } else {
2251 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2252 return;
2253 }
2254 D->addAttr(::new (S.Context)
2255 ObjCIndependentClassAttr(Attr.getRange(), S.Context,
2256 Attr.getAttributeSpellingListIndex()));
2257}
2258
Chandler Carruthedc2c642011-07-02 00:01:44 +00002259static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002260 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002261 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002262 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002263 return;
2264 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002265
Aaron Ballman00e99962013-08-31 01:11:41 +00002266 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002267 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002268 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2269 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2270 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002271 return;
2272 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002273
Michael Han99315932013-01-24 16:46:58 +00002274 D->addAttr(::new (S.Context)
2275 BlocksAttr(Attr.getRange(), S.Context, type,
2276 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002277}
2278
Chandler Carruthedc2c642011-07-02 00:01:44 +00002279static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002280 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002281 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002282 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002283 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002284 if (E->isTypeDependent() || E->isValueDependent() ||
2285 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002286 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002287 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002288 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002289 return;
2290 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002291
John McCallb46f2872011-09-09 07:56:05 +00002292 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002293 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2294 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002295 return;
2296 }
John McCallb46f2872011-09-09 07:56:05 +00002297
2298 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002299 }
2300
Aaron Ballman18a78382013-11-21 00:28:23 +00002301 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002302 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002303 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002304 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002305 if (E->isTypeDependent() || E->isValueDependent() ||
2306 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002307 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002308 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002309 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002310 return;
2311 }
2312 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002313
John McCallb46f2872011-09-09 07:56:05 +00002314 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002315 // FIXME: This error message could be improved, it would be nice
2316 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002317 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2318 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002319 return;
2320 }
2321 }
2322
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002323 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002324 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002325 if (isa<FunctionNoProtoType>(FT)) {
2326 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2327 return;
2328 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002329
Chris Lattner9363e312009-03-17 23:03:47 +00002330 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002331 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002332 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002333 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002334 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002335 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002336 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002337 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002338 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002339 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2340 if (!BD->isVariadic()) {
2341 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2342 return;
2343 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002344 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002345 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002346 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002347 const FunctionType *FT = Ty->isFunctionPointerType()
2348 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002349 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002350 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002351 int m = Ty->isFunctionPointerType() ? 0 : 1;
2352 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002353 return;
2354 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002355 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002356 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002357 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002358 return;
2359 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002360 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002361 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002362 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002363 return;
2364 }
Michael Han99315932013-01-24 16:46:58 +00002365 D->addAttr(::new (S.Context)
2366 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2367 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002368}
2369
Chandler Carruthedc2c642011-07-02 00:01:44 +00002370static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Alp Toker314cc812014-01-25 16:55:45 +00002371 if (D->getFunctionType() &&
2372 D->getFunctionType()->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002373 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2374 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002375 return;
2376 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002377 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002378 if (MD->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002379 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2380 << Attr.getName() << 1;
2381 return;
2382 }
2383
Michael Han99315932013-01-24 16:46:58 +00002384 D->addAttr(::new (S.Context)
2385 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2386 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002387}
2388
Chandler Carruthedc2c642011-07-02 00:01:44 +00002389static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002390 // weak_import only applies to variable & function declarations.
2391 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002392 if (!D->canBeWeakImported(isDef)) {
2393 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002394 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2395 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002396 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002397 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002398 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002399 // Nothing to warn about here.
2400 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002401 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002402 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002403
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002404 return;
2405 }
2406
Michael Han99315932013-01-24 16:46:58 +00002407 D->addAttr(::new (S.Context)
2408 WeakImportAttr(Attr.getRange(), S.Context,
2409 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002410}
2411
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002412// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002413template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002414static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002415 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002416 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002417 for (unsigned i = 0; i < 3; ++i) {
2418 const Expr *E = Attr.getArgAsExpr(i);
2419 if (!checkUInt32Argument(S, Attr, E, WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002420 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002421 if (WGSize[i] == 0) {
2422 S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero)
2423 << Attr.getName() << E->getSourceRange();
2424 return;
2425 }
2426 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002427
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002428 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2429 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2430 Existing->getYDim() == WGSize[1] &&
2431 Existing->getZDim() == WGSize[2]))
2432 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002433
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002434 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2435 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002436 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002437}
2438
Joey Goulyaba589c2013-03-08 09:42:32 +00002439static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002440 if (!Attr.hasParsedType()) {
2441 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2442 << Attr.getName() << 1;
2443 return;
2444 }
2445
Craig Topperc3ec1492014-05-26 06:22:03 +00002446 TypeSourceInfo *ParmTSI = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00002447 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2448 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002449
2450 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2451 (ParmType->isBooleanType() ||
2452 !ParmType->isIntegralType(S.getASTContext()))) {
2453 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2454 << ParmType;
2455 return;
2456 }
2457
Aaron Ballmana9e05402013-12-02 22:16:55 +00002458 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002459 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002460 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2461 return;
2462 }
2463 }
2464
2465 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002466 ParmTSI,
2467 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002468}
2469
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002470SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002471 StringRef Name,
2472 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002473 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2474 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002475 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002476 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2477 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002478 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002479 }
Michael Han99315932013-01-24 16:46:58 +00002480 return ::new (Context) SectionAttr(Range, Context, Name,
2481 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002482}
2483
Reid Kleckner2a133222015-03-04 23:39:17 +00002484bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2485 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2486 if (!Error.empty()) {
2487 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error;
2488 return false;
2489 }
2490 return true;
2491}
2492
Chandler Carruthedc2c642011-07-02 00:01:44 +00002493static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002494 // Make sure that there is a string literal as the sections's single
2495 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002496 StringRef Str;
2497 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002498 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002499 return;
Mike Stump11289f42009-09-09 15:08:12 +00002500
Reid Kleckner2a133222015-03-04 23:39:17 +00002501 if (!S.checkSectionName(LiteralLoc, Str))
2502 return;
2503
Chris Lattner30ba6742009-08-10 19:03:04 +00002504 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002505 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002506 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002507 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002508 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002509 return;
2510 }
Mike Stump11289f42009-09-09 15:08:12 +00002511
Michael Han99315932013-01-24 16:46:58 +00002512 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002513 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002514 if (NewAttr)
2515 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002516}
2517
Eric Christopher789a7ad2015-06-12 01:36:05 +00002518// Check for things we'd like to warn about, no errors or validation for now.
2519// TODO: Validation should use a backend target library that specifies
2520// the allowable subtarget features and cpus. We could use something like a
2521// TargetCodeGenInfo hook here to do validation.
2522void Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
2523 for (auto Str : {"tune=", "fpmath="})
2524 if (AttrStr.find(Str) != StringRef::npos)
2525 Diag(LiteralLoc, diag::warn_unsupported_target_attribute) << Str;
2526}
2527
Eric Christopher11acf732015-06-12 01:35:52 +00002528static void handleTargetAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eric Christopher11acf732015-06-12 01:35:52 +00002529 StringRef Str;
2530 SourceLocation LiteralLoc;
2531 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
2532 return;
Eric Christopher789a7ad2015-06-12 01:36:05 +00002533 S.checkTargetAttr(LiteralLoc, Str);
Eric Christopher11acf732015-06-12 01:35:52 +00002534 unsigned Index = Attr.getAttributeSpellingListIndex();
2535 TargetAttr *NewAttr =
2536 ::new (S.Context) TargetAttr(Attr.getRange(), S.Context, Str, Index);
2537 D->addAttr(NewAttr);
2538}
2539
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002540
Chandler Carruthedc2c642011-07-02 00:01:44 +00002541static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002542 VarDecl *VD = cast<VarDecl>(D);
2543 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002544 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002545 return;
2546 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002547
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002548 Expr *E = Attr.getArgAsExpr(0);
2549 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00002550 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002551 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002552
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002553 // gcc only allows for simple identifiers. Since we support more than gcc, we
2554 // will warn the user.
2555 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2556 if (DRE->hasQualifier())
2557 S.Diag(Loc, diag::warn_cleanup_ext);
2558 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2559 NI = DRE->getNameInfo();
2560 if (!FD) {
2561 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2562 << NI.getName();
2563 return;
2564 }
2565 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2566 if (ULE->hasExplicitTemplateArgs())
2567 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002568 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2569 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002570 if (!FD) {
2571 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2572 << NI.getName();
2573 if (ULE->getType() == S.Context.OverloadTy)
2574 S.NoteAllOverloadCandidates(ULE);
2575 return;
2576 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002577 } else {
2578 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002579 return;
2580 }
2581
Anders Carlssond277d792009-01-31 01:16:18 +00002582 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002583 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2584 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002585 return;
2586 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002587
Anders Carlsson723f55d2009-02-07 23:16:50 +00002588 // We're currently more strict than GCC about what function types we accept.
2589 // If this ever proves to be a problem it should be easy to fix.
2590 QualType Ty = S.Context.getPointerType(VD->getType());
2591 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002592 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2593 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002594 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2595 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002596 return;
2597 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002598
Michael Han99315932013-01-24 16:46:58 +00002599 D->addAttr(::new (S.Context)
2600 CleanupAttr(Attr.getRange(), S.Context, FD,
2601 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002602}
2603
Mike Stumpd3bb5572009-07-24 19:02:52 +00002604/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002605/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002606static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002607 Expr *IdxExpr = Attr.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00002608 uint64_t Idx;
2609 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002610 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002611
Eric Christopherb64963e2015-08-13 21:34:35 +00002612 // Make sure the format string is really a string.
Alp Toker601b22c2014-01-21 23:35:24 +00002613 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002614
Eric Christopherb64963e2015-08-13 21:34:35 +00002615 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
2616 if (NotNSStringTy &&
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002617 !isCFStringType(Ty, S.Context) &&
2618 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002619 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002620 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00002621 << "a string type" << IdxExpr->getSourceRange()
2622 << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002623 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002624 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002625 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002626 if (!isNSStringType(Ty, S.Context) &&
2627 !isCFStringType(Ty, S.Context) &&
2628 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002629 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002630 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00002631 << (NotNSStringTy ? "string type" : "NSString")
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00002632 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002633 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002634 }
2635
Alp Toker601b22c2014-01-21 23:35:24 +00002636 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002637 // because that has corrected for the implicit this parameter, and is zero-
2638 // based. The attribute expects what the user wrote explicitly.
2639 llvm::APSInt Val;
2640 IdxExpr->EvaluateAsInt(Val, S.Context);
2641
Michael Han99315932013-01-24 16:46:58 +00002642 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002643 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002644 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002645}
2646
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002647enum FormatAttrKind {
2648 CFStringFormat,
2649 NSStringFormat,
2650 StrftimeFormat,
2651 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002652 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002653 InvalidFormat
2654};
2655
2656/// getFormatAttrKind - Map from format attribute names to supported format
2657/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002658static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002659 return llvm::StringSwitch<FormatAttrKind>(Format)
2660 // Check for formats that get handled specially.
2661 .Case("NSString", NSStringFormat)
2662 .Case("CFString", CFStringFormat)
2663 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002664
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002665 // Otherwise, check for supported formats.
2666 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2667 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2668 .Case("kprintf", SupportedFormat) // OpenBSD.
Dimitry Andric6b5ed342015-02-19 22:32:33 +00002669 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
Fariborz Jahanianf8dce0f2015-02-21 00:45:58 +00002670 .Case("os_trace", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002671
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002672 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2673 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002674}
2675
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002676/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002677/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002678static void handleInitPriorityAttr(Sema &S, Decl *D,
2679 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002680 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002681 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2682 return;
2683 }
2684
Aaron Ballman4a611152013-11-27 16:34:09 +00002685 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002686 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2687 Attr.setInvalid();
2688 return;
2689 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002690 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002691 if (S.Context.getAsArrayType(T))
2692 T = S.Context.getBaseElementType(T);
2693 if (!T->getAs<RecordType>()) {
2694 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2695 Attr.setInvalid();
2696 return;
2697 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002698
2699 Expr *E = Attr.getArgAsExpr(0);
2700 uint32_t prioritynum;
2701 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002702 Attr.setInvalid();
2703 return;
2704 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002705
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002706 if (prioritynum < 101 || prioritynum > 65535) {
2707 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002708 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002709 Attr.setInvalid();
2710 return;
2711 }
Michael Han99315932013-01-24 16:46:58 +00002712 D->addAttr(::new (S.Context)
2713 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2714 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002715}
2716
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002717FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2718 IdentifierInfo *Format, int FormatIdx,
2719 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002720 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002721 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002722 for (auto *F : D->specific_attrs<FormatAttr>()) {
2723 if (F->getType() == Format &&
2724 F->getFormatIdx() == FormatIdx &&
2725 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002726 // If we don't have a valid location for this attribute, adopt the
2727 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002728 if (F->getLocation().isInvalid())
2729 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00002730 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00002731 }
2732 }
2733
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002734 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2735 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002736}
2737
Mike Stumpd3bb5572009-07-24 19:02:52 +00002738/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002739/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002740static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002741 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002742 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002743 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002744 return;
2745 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002746
Chandler Carruth743682b2010-11-16 08:35:43 +00002747 // In C++ the implicit 'this' function parameter also counts, and they are
2748 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002749 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00002750 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002751
Aaron Ballman00e99962013-08-31 01:11:41 +00002752 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2753 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002754
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00002755 if (normalizeName(Format)) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002756 // If we've modified the string name, we need a new identifier for it.
2757 II = &S.Context.Idents.get(Format);
2758 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002759
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002760 // Check for supported formats.
2761 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002762
2763 if (Kind == IgnoredFormat)
2764 return;
2765
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002766 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002767 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002768 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002769 return;
2770 }
2771
2772 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002773 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002774 uint32_t Idx;
2775 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002776 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002777
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002778 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002779 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002780 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002781 return;
2782 }
2783
2784 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002785 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002786
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002787 if (HasImplicitThisParam) {
2788 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002789 S.Diag(Attr.getLoc(),
2790 diag::err_format_attribute_implicit_this_format_string)
2791 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002792 return;
2793 }
2794 ArgIdx--;
2795 }
Mike Stump11289f42009-09-09 15:08:12 +00002796
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002797 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002798 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002799
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002800 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002801 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002802 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002803 << "a CFString" << IdxExpr->getSourceRange()
2804 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00002805 return;
2806 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002807 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002808 // FIXME: do we need to check if the type is NSString*? What are the
2809 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002810 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002811 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002812 << "an NSString" << IdxExpr->getSourceRange()
2813 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002814 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002815 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002816 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002817 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002818 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002819 << "a string type" << IdxExpr->getSourceRange()
2820 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002821 return;
2822 }
2823
2824 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002825 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002826 uint32_t FirstArg;
2827 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002828 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002829
2830 // check if the function is variadic if the 3rd argument non-zero
2831 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002832 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002833 ++NumArgs; // +1 for ...
2834 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002835 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002836 return;
2837 }
2838 }
2839
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002840 // strftime requires FirstArg to be 0 because it doesn't read from any
2841 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002842 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002843 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002844 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2845 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002846 return;
2847 }
2848 // if 0 it disables parameter checking (to use with e.g. va_list)
2849 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002850 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002851 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002852 return;
2853 }
2854
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002855 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002856 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002857 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002858 if (NewAttr)
2859 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002860}
2861
Chandler Carruthedc2c642011-07-02 00:01:44 +00002862static void handleTransparentUnionAttr(Sema &S, Decl *D,
2863 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002864 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00002865 RecordDecl *RD = nullptr;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002866 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002867 if (TD && TD->getUnderlyingType()->isUnionType())
2868 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2869 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002870 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002871
2872 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002873 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002874 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002875 return;
2876 }
2877
John McCallf937c022011-10-07 06:10:15 +00002878 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002879 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002880 diag::warn_transparent_union_attribute_not_definition);
2881 return;
2882 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002883
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002884 RecordDecl::field_iterator Field = RD->field_begin(),
2885 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002886 if (Field == FieldEnd) {
2887 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2888 return;
2889 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002890
David Blaikie40ed2972012-06-06 20:45:41 +00002891 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002892 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002893 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002894 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002895 diag::warn_transparent_union_attribute_floating)
2896 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002897 return;
2898 }
2899
2900 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2901 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2902 for (; Field != FieldEnd; ++Field) {
2903 QualType FieldType = Field->getType();
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002904 // FIXME: this isn't fully correct; we also need to test whether the
2905 // members of the union would all have the same calling convention as the
2906 // first member of the union. Checking just the size and alignment isn't
2907 // sufficient (consider structs passed on the stack instead of in registers
2908 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002909 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002910 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002911 // Warn if we drop the attribute.
2912 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002913 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002914 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002915 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002916 diag::warn_transparent_union_attribute_field_size_align)
2917 << isSize << Field->getDeclName() << FieldBits;
2918 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002919 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002920 diag::note_transparent_union_first_field_size_align)
2921 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002922 return;
2923 }
2924 }
2925
Michael Han99315932013-01-24 16:46:58 +00002926 RD->addAttr(::new (S.Context)
2927 TransparentUnionAttr(Attr.getRange(), S.Context,
2928 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002929}
2930
Chandler Carruthedc2c642011-07-02 00:01:44 +00002931static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002932 // Make sure that there is a string literal as the annotation's single
2933 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002934 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002935 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002936 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002937
2938 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002939 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
2940 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002941 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002942 }
Michael Han99315932013-01-24 16:46:58 +00002943
2944 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002945 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002946 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002947}
2948
Hal Finkel1b0d24e2014-10-02 21:21:25 +00002949static void handleAlignValueAttr(Sema &S, Decl *D,
2950 const AttributeList &Attr) {
2951 S.AddAlignValueAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
2952 Attr.getAttributeSpellingListIndex());
2953}
2954
2955void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
2956 unsigned SpellingListIndex) {
2957 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
2958 SourceLocation AttrLoc = AttrRange.getBegin();
2959
2960 QualType T;
2961 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
2962 T = TD->getUnderlyingType();
2963 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2964 T = VD->getType();
2965 else
2966 llvm_unreachable("Unknown decl type for align_value");
2967
2968 if (!T->isDependentType() && !T->isAnyPointerType() &&
2969 !T->isReferenceType() && !T->isMemberPointerType()) {
2970 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
2971 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
2972 return;
2973 }
2974
2975 if (!E->isValueDependent()) {
David Majnemer0be6bd02015-07-26 09:02:21 +00002976 llvm::APSInt Alignment;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00002977 ExprResult ICE
2978 = VerifyIntegerConstantExpression(E, &Alignment,
2979 diag::err_align_value_attribute_argument_not_int,
2980 /*AllowFold*/ false);
2981 if (ICE.isInvalid())
2982 return;
2983
2984 if (!Alignment.isPowerOf2()) {
2985 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
2986 << E->getSourceRange();
2987 return;
2988 }
2989
2990 D->addAttr(::new (Context)
2991 AlignValueAttr(AttrRange, Context, ICE.get(),
2992 SpellingListIndex));
2993 return;
2994 }
2995
2996 // Save dependent expressions in the AST to be instantiated.
2997 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
2998 return;
2999}
3000
Chandler Carruthedc2c642011-07-02 00:01:44 +00003001static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003002 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003003 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003004 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3005 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003006 return;
3007 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003008
Richard Smith848e1f12013-02-01 08:12:08 +00003009 if (Attr.getNumArgs() == 0) {
3010 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
Craig Topperc3ec1492014-05-26 06:22:03 +00003011 true, nullptr, Attr.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00003012 return;
3013 }
3014
Aaron Ballman00e99962013-08-31 01:11:41 +00003015 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00003016 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3017 S.Diag(Attr.getEllipsisLoc(),
3018 diag::err_pack_expansion_without_parameter_packs);
3019 return;
3020 }
3021
3022 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3023 return;
3024
David Majnemer26a1e0e2015-04-07 02:37:09 +00003025 if (E->isValueDependent()) {
3026 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3027 if (!TND->getUnderlyingType()->isDependentType()) {
3028 S.Diag(Attr.getLoc(), diag::err_alignment_dependent_typedef_name)
3029 << E->getSourceRange();
3030 return;
3031 }
3032 }
3033 }
3034
Richard Smith44c247f2013-02-22 08:32:16 +00003035 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3036 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003037}
3038
3039void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003040 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003041 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3042 SourceLocation AttrLoc = AttrRange.getBegin();
3043
Richard Smith1dba27c2013-01-29 09:02:09 +00003044 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003045 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003046 // C++11 [dcl.align]p1:
3047 // An alignment-specifier may be applied to a variable or to a class
3048 // data member, but it shall not be applied to a bit-field, a function
3049 // parameter, the formal parameter of a catch clause, or a variable
3050 // declared with the register storage class specifier. An
3051 // alignment-specifier may also be applied to the declaration of a class
3052 // or enumeration type.
3053 // C11 6.7.5/2:
3054 // An alignment attribute shall not be specified in a declaration of
3055 // a typedef, or a bit-field, or a function, or a parameter, or an
3056 // object declared with the register storage-class specifier.
3057 int DiagKind = -1;
3058 if (isa<ParmVarDecl>(D)) {
3059 DiagKind = 0;
3060 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3061 if (VD->getStorageClass() == SC_Register)
3062 DiagKind = 1;
3063 if (VD->isExceptionVariable())
3064 DiagKind = 2;
3065 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3066 if (FD->isBitField())
3067 DiagKind = 3;
3068 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00003069 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00003070 << (TmpAttr.isC11() ? ExpectedVariableOrField
3071 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003072 return;
3073 }
3074 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003075 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00003076 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003077 return;
3078 }
3079 }
3080
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003081 if (E->isTypeDependent() || E->isValueDependent()) {
3082 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003083 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3084 AA->setPackExpansion(IsPackExpansion);
3085 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003086 return;
3087 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003088
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003089 // FIXME: Cache the number on the Attr object?
David Majnemer0be6bd02015-07-26 09:02:21 +00003090 llvm::APSInt Alignment;
Douglas Gregore2b37442012-05-04 22:38:52 +00003091 ExprResult ICE
3092 = VerifyIntegerConstantExpression(E, &Alignment,
3093 diag::err_aligned_attribute_argument_not_int,
3094 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003095 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003096 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003097
David Majnemer0be6bd02015-07-26 09:02:21 +00003098 uint64_t AlignVal = Alignment.getZExtValue();
3099
Richard Smith848e1f12013-02-01 08:12:08 +00003100 // C++11 [dcl.align]p2:
3101 // -- if the constant expression evaluates to zero, the alignment
3102 // specifier shall have no effect
3103 // C11 6.7.5p6:
3104 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003105 if (!(TmpAttr.isAlignas() && !Alignment)) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003106 if (!llvm::isPowerOf2_64(AlignVal)) {
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003107 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3108 << E->getSourceRange();
3109 return;
3110 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003111 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003112
David Majnemerabecae72014-02-12 20:36:10 +00003113 // Alignment calculations can wrap around if it's greater than 2**28.
David Majnemer29c69db2015-07-26 01:48:59 +00003114 unsigned MaxValidAlignment =
3115 Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192
3116 : 268435456;
David Majnemer0be6bd02015-07-26 09:02:21 +00003117 if (AlignVal > MaxValidAlignment) {
David Majnemerabecae72014-02-12 20:36:10 +00003118 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
3119 << E->getSourceRange();
3120 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003121 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003122
David Majnemer0be6bd02015-07-26 09:02:21 +00003123 if (Context.getTargetInfo().isTLSSupported()) {
3124 unsigned MaxTLSAlign =
3125 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3126 .getQuantity();
3127 auto *VD = dyn_cast<VarDecl>(D);
3128 if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD &&
3129 VD->getTLSKind() != VarDecl::TLS_None) {
3130 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3131 << (unsigned)AlignVal << VD << MaxTLSAlign;
3132 return;
3133 }
3134 }
3135
Richard Smith44c247f2013-02-22 08:32:16 +00003136 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003137 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00003138 AA->setPackExpansion(IsPackExpansion);
3139 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003140}
3141
Michael Hanaf02bbe2013-02-01 01:19:17 +00003142void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003143 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003144 // FIXME: Cache the number on the Attr object if non-dependent?
3145 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003146 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3147 SpellingListIndex);
3148 AA->setPackExpansion(IsPackExpansion);
3149 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003150}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003151
Richard Smith848e1f12013-02-01 08:12:08 +00003152void Sema::CheckAlignasUnderalignment(Decl *D) {
3153 assert(D->hasAttrs() && "no attributes on decl");
3154
David Majnemer475b25e2015-01-21 10:54:38 +00003155 QualType UnderlyingTy, DiagTy;
3156 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
3157 UnderlyingTy = DiagTy = VD->getType();
3158 } else {
3159 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
3160 if (EnumDecl *ED = dyn_cast<EnumDecl>(D))
3161 UnderlyingTy = ED->getIntegerType();
3162 }
3163 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003164 return;
3165
3166 // C++11 [dcl.align]p5, C11 6.7.5/4:
3167 // The combined effect of all alignment attributes in a declaration shall
3168 // not specify an alignment that is less strict than the alignment that
3169 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003170 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003171 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003172 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003173 if (I->isAlignmentDependent())
3174 return;
3175 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003176 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003177 Align = std::max(Align, I->getAlignment(Context));
3178 }
3179
3180 if (AlignasAttr && Align) {
3181 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003182 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003183 if (NaturalAlign > RequestedAlign)
3184 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003185 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003186 }
3187}
3188
David Majnemer2c4e00a2014-01-29 22:07:36 +00003189bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003190 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003191 MSInheritanceAttr::Spelling SemanticSpelling) {
3192 assert(RD->hasDefinition() && "RD has no definition!");
3193
David Majnemer98c9ee22014-02-07 00:43:07 +00003194 // We may not have seen base specifiers or any virtual methods yet. We will
3195 // have to wait until the record is defined to catch any mismatches.
3196 if (!RD->getDefinition()->isCompleteDefinition())
3197 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003198
David Majnemer98c9ee22014-02-07 00:43:07 +00003199 // The unspecified model never matches what a definition could need.
3200 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
3201 return false;
3202
David Majnemer4bb09802014-02-10 19:50:15 +00003203 if (BestCase) {
3204 if (RD->calculateInheritanceModel() == SemanticSpelling)
3205 return false;
3206 } else {
3207 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3208 return false;
3209 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003210
3211 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3212 << 0 /*definition*/;
3213 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3214 << RD->getNameAsString();
3215 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003216}
3217
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003218/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003219/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003220///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003221/// Despite what would be logical, the mode attribute is a decl attribute, not a
3222/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3223/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003224static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003225 // This attribute isn't documented, but glibc uses it. It changes
3226 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00003227 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00003228 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3229 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003230 return;
3231 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003232
Aaron Ballman00e99962013-08-31 01:11:41 +00003233 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3234 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003235
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00003236 normalizeName(Str);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003237
3238 unsigned DestWidth = 0;
3239 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003240 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003241 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003242 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003243 switch (Str[0]) {
3244 case 'Q': DestWidth = 8; break;
3245 case 'H': DestWidth = 16; break;
3246 case 'S': DestWidth = 32; break;
3247 case 'D': DestWidth = 64; break;
3248 case 'X': DestWidth = 96; break;
3249 case 'T': DestWidth = 128; break;
3250 }
3251 if (Str[1] == 'F') {
3252 IntegerMode = false;
3253 } else if (Str[1] == 'C') {
3254 IntegerMode = false;
3255 ComplexMode = true;
3256 } else if (Str[1] != 'I') {
3257 DestWidth = 0;
3258 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003259 break;
3260 case 4:
3261 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3262 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003263 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003264 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003265 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003266 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003267 break;
3268 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003269 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003270 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003271 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003272 case 11:
3273 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003274 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003275 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003276 }
3277
3278 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003279 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003280 OldTy = TD->getUnderlyingType();
3281 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3282 OldTy = VD->getType();
3283 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003284 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00003285 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003286 return;
3287 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003288
Alexey Bataev326057d2015-06-19 07:46:21 +00003289 // Base type can also be a vector type (see PR17453).
3290 // Distinguish between base type and base element type.
3291 QualType OldElemTy = OldTy;
3292 if (const VectorType *VT = OldTy->getAs<VectorType>())
3293 OldElemTy = VT->getElementType();
3294
3295 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003296 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3297 else if (IntegerMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003298 if (!OldElemTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003299 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3300 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003301 if (!OldElemTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003302 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3303 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00003304 if (!OldElemTy->isFloatingType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003305 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3306 }
3307
Mike Stump87c57ac2009-05-16 07:39:55 +00003308 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3309 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003310 // FIXME: Make sure floating-point mappings are accurate
3311 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003312 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00003313 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003314 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003315 }
3316
Alexey Bataev326057d2015-06-19 07:46:21 +00003317 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003318
3319 if (IntegerMode)
Alexey Bataev326057d2015-06-19 07:46:21 +00003320 NewElemTy = S.Context.getIntTypeForBitwidth(
3321 DestWidth, OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003322 else
Alexey Bataev326057d2015-06-19 07:46:21 +00003323 NewElemTy = S.Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003324
Alexey Bataev326057d2015-06-19 07:46:21 +00003325 if (NewElemTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00003326 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003327 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003328 }
3329
Eli Friedman4735374e2009-03-03 06:41:03 +00003330 if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003331 NewElemTy = S.Context.getComplexType(NewElemTy);
3332 }
3333
3334 QualType NewTy = NewElemTy;
3335 if (const VectorType *OldVT = OldTy->getAs<VectorType>()) {
3336 // Complex machine mode does not support base vector types.
3337 if (ComplexMode) {
3338 S.Diag(Attr.getLoc(), diag::err_complex_mode_vector_type);
3339 return;
3340 }
3341 unsigned NumElements = S.Context.getTypeSize(OldElemTy) *
3342 OldVT->getNumElements() /
3343 S.Context.getTypeSize(NewElemTy);
3344 NewTy =
3345 S.Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
3346 }
3347
3348 if (NewTy.isNull()) {
3349 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3350 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003351 }
3352
3353 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003354 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3355 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3356 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003357 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003358
3359 D->addAttr(::new (S.Context)
3360 ModeAttr(Attr.getRange(), S.Context, Name,
3361 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003362}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003363
Chandler Carruthedc2c642011-07-02 00:01:44 +00003364static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003365 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3366 if (!VD->hasGlobalStorage())
3367 S.Diag(Attr.getLoc(),
3368 diag::warn_attribute_requires_functions_or_static_globals)
3369 << Attr.getName();
3370 } else if (!isFunctionOrMethod(D)) {
3371 S.Diag(Attr.getLoc(),
3372 diag::warn_attribute_requires_functions_or_static_globals)
3373 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003374 return;
3375 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003376
Michael Han99315932013-01-24 16:46:58 +00003377 D->addAttr(::new (S.Context)
3378 NoDebugAttr(Attr.getRange(), S.Context,
3379 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003380}
3381
Paul Robinson30e41fb2014-12-15 18:57:28 +00003382AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range,
Paul Robinson080b1f32015-01-13 18:34:56 +00003383 IdentifierInfo *Ident,
Paul Robinson30e41fb2014-12-15 18:57:28 +00003384 unsigned AttrSpellingListIndex) {
3385 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003386 Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00003387 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3388 return nullptr;
3389 }
3390
3391 if (D->hasAttr<AlwaysInlineAttr>())
3392 return nullptr;
3393
3394 return ::new (Context) AlwaysInlineAttr(Range, Context,
3395 AttrSpellingListIndex);
3396}
3397
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003398CommonAttr *Sema::mergeCommonAttr(Decl *D, SourceRange Range,
3399 IdentifierInfo *Ident,
3400 unsigned AttrSpellingListIndex) {
3401 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, Range, Ident))
3402 return nullptr;
3403
3404 return ::new (Context) CommonAttr(Range, Context, AttrSpellingListIndex);
3405}
3406
3407InternalLinkageAttr *
3408Sema::mergeInternalLinkageAttr(Decl *D, SourceRange Range,
3409 IdentifierInfo *Ident,
3410 unsigned AttrSpellingListIndex) {
3411 if (auto VD = dyn_cast<VarDecl>(D)) {
3412 // Attribute applies to Var but not any subclass of it (like ParmVar,
3413 // ImplicitParm or VarTemplateSpecialization).
3414 if (VD->getKind() != Decl::Var) {
3415 Diag(Range.getBegin(), diag::warn_attribute_wrong_decl_type)
3416 << Ident << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
3417 : ExpectedVariableOrFunction);
3418 return nullptr;
3419 }
3420 // Attribute does not apply to non-static local variables.
3421 if (VD->hasLocalStorage()) {
3422 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
3423 return nullptr;
3424 }
3425 }
3426
3427 if (checkAttrMutualExclusion<CommonAttr>(*this, D, Range, Ident))
3428 return nullptr;
3429
3430 return ::new (Context)
3431 InternalLinkageAttr(Range, Context, AttrSpellingListIndex);
3432}
3433
Paul Robinson30e41fb2014-12-15 18:57:28 +00003434MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range,
3435 unsigned AttrSpellingListIndex) {
3436 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
3437 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'";
3438 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3439 return nullptr;
3440 }
3441
3442 if (D->hasAttr<MinSizeAttr>())
3443 return nullptr;
3444
3445 return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex);
3446}
3447
3448OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range,
3449 unsigned AttrSpellingListIndex) {
3450 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
3451 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
3452 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3453 D->dropAttr<AlwaysInlineAttr>();
3454 }
3455 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
3456 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
3457 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3458 D->dropAttr<MinSizeAttr>();
3459 }
3460
3461 if (D->hasAttr<OptimizeNoneAttr>())
3462 return nullptr;
3463
3464 return ::new (Context) OptimizeNoneAttr(Range, Context,
3465 AttrSpellingListIndex);
3466}
3467
Paul Robinsonf0674352014-03-31 22:29:15 +00003468static void handleAlwaysInlineAttr(Sema &S, Decl *D,
3469 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003470 if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, Attr.getRange(),
3471 Attr.getName()))
Akira Hatanakac8667622015-11-06 23:56:15 +00003472 return;
3473
Paul Robinson080b1f32015-01-13 18:34:56 +00003474 if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr(
3475 D, Attr.getRange(), Attr.getName(),
3476 Attr.getAttributeSpellingListIndex()))
3477 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00003478}
3479
Paul Robinson080b1f32015-01-13 18:34:56 +00003480static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3481 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(
3482 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3483 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00003484}
3485
Paul Robinsonf0674352014-03-31 22:29:15 +00003486static void handleOptimizeNoneAttr(Sema &S, Decl *D,
3487 const AttributeList &Attr) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003488 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(
3489 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3490 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00003491}
3492
Chandler Carruthedc2c642011-07-02 00:01:44 +00003493static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003494 FunctionDecl *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00003495 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00003496 SourceRange RTRange = FD->getReturnTypeSourceRange();
3497 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00003498 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00003499 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
3500 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00003501 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003502 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003503
Aaron Ballman3aff6332013-12-02 19:30:36 +00003504 D->addAttr(::new (S.Context)
3505 CUDAGlobalAttr(Attr.getRange(), S.Context,
Eli Bendersky83860042014-09-02 22:00:06 +00003506 Attr.getAttributeSpellingListIndex()));
Artem Belevichc3fa25d2015-09-22 17:22:51 +00003507
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003508}
3509
Chandler Carruthedc2c642011-07-02 00:01:44 +00003510static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003511 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003512 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003513 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003514 return;
3515 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003516
Michael Han99315932013-01-24 16:46:58 +00003517 D->addAttr(::new (S.Context)
3518 GNUInlineAttr(Attr.getRange(), S.Context,
3519 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003520}
3521
Chandler Carruthedc2c642011-07-02 00:01:44 +00003522static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003523 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003524
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003525 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003526 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3527 CallingConv CC;
Richard Smitheec7cb12015-05-28 23:38:53 +00003528 if (S.CheckCallingConvAttr(Attr, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00003529 return;
3530
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003531 if (!isa<ObjCMethodDecl>(D)) {
3532 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3533 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003534 return;
3535 }
3536
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003537 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003538 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003539 D->addAttr(::new (S.Context)
3540 FastCallAttr(Attr.getRange(), S.Context,
3541 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003542 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003543 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003544 D->addAttr(::new (S.Context)
3545 StdCallAttr(Attr.getRange(), S.Context,
3546 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003547 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003548 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003549 D->addAttr(::new (S.Context)
3550 ThisCallAttr(Attr.getRange(), S.Context,
3551 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003552 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003553 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003554 D->addAttr(::new (S.Context)
3555 CDeclAttr(Attr.getRange(), S.Context,
3556 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003557 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003558 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003559 D->addAttr(::new (S.Context)
3560 PascalAttr(Attr.getRange(), S.Context,
3561 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003562 return;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003563 case AttributeList::AT_VectorCall:
3564 D->addAttr(::new (S.Context)
3565 VectorCallAttr(Attr.getRange(), S.Context,
3566 Attr.getAttributeSpellingListIndex()));
3567 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003568 case AttributeList::AT_MSABI:
3569 D->addAttr(::new (S.Context)
3570 MSABIAttr(Attr.getRange(), S.Context,
3571 Attr.getAttributeSpellingListIndex()));
3572 return;
3573 case AttributeList::AT_SysVABI:
3574 D->addAttr(::new (S.Context)
3575 SysVABIAttr(Attr.getRange(), S.Context,
3576 Attr.getAttributeSpellingListIndex()));
3577 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003578 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003579 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003580 switch (CC) {
3581 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003582 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003583 break;
3584 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003585 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003586 break;
3587 default:
3588 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003589 }
3590
Michael Han99315932013-01-24 16:46:58 +00003591 D->addAttr(::new (S.Context)
3592 PcsAttr(Attr.getRange(), S.Context, PCS,
3593 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003594 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003595 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00003596 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003597 D->addAttr(::new (S.Context)
3598 IntelOclBiccAttr(Attr.getRange(), S.Context,
3599 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003600 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003601
Abramo Bagnara50099372010-04-30 13:10:51 +00003602 default:
3603 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003604 }
3605}
3606
Aaron Ballman02df2e02012-12-09 17:45:41 +00003607bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3608 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003609 if (attr.isInvalid())
3610 return true;
3611
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003612 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003613 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003614 attr.setInvalid();
3615 return true;
3616 }
3617
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003618 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003619 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003620 case AttributeList::AT_CDecl: CC = CC_C; break;
3621 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3622 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3623 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3624 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003625 case AttributeList::AT_VectorCall: CC = CC_X86VectorCall; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003626 case AttributeList::AT_MSABI:
3627 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3628 CC_X86_64Win64;
3629 break;
3630 case AttributeList::AT_SysVABI:
3631 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3632 CC_C;
3633 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003634 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003635 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003636 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003637 attr.setInvalid();
3638 return true;
3639 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003640 if (StrRef == "aapcs") {
3641 CC = CC_AAPCS;
3642 break;
3643 } else if (StrRef == "aapcs-vfp") {
3644 CC = CC_AAPCS_VFP;
3645 break;
3646 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003647
3648 attr.setInvalid();
3649 Diag(attr.getLoc(), diag::err_invalid_pcs);
3650 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003651 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00003652 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003653 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003654 }
3655
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003656 const TargetInfo &TI = Context.getTargetInfo();
3657 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
Reid Kleckner9fde2e02015-02-26 19:43:46 +00003658 if (A != TargetInfo::CCCR_OK) {
3659 if (A == TargetInfo::CCCR_Warning)
3660 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003661
Reid Kleckner9fde2e02015-02-26 19:43:46 +00003662 // This convention is not valid for the target. Use the default function or
3663 // method calling convention.
Aaron Ballman02df2e02012-12-09 17:45:41 +00003664 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3665 if (FD)
3666 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3667 TargetInfo::CCMT_NonMember;
3668 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003669 }
3670
John McCall3882ace2011-01-05 12:14:39 +00003671 return false;
3672}
3673
John McCall3882ace2011-01-05 12:14:39 +00003674/// Checks a regparm attribute, returning true if it is ill-formed and
3675/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003676bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3677 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003678 return true;
3679
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003680 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003681 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003682 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003683 }
Eli Friedman7044b762009-03-27 21:06:47 +00003684
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003685 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003686 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003687 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003688 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003689 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003690 }
3691
Douglas Gregore8bbc122011-09-02 00:18:52 +00003692 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003693 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003694 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003695 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003696 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003697 }
3698
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003699 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003700 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003701 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003702 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003703 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003704 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003705 }
3706
John McCall3882ace2011-01-05 12:14:39 +00003707 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003708}
3709
Artem Belevich7093e402015-04-21 22:55:54 +00003710// Checks whether an argument of launch_bounds attribute is acceptable
3711// May output an error.
3712static bool checkLaunchBoundsArgument(Sema &S, Expr *E,
3713 const CUDALaunchBoundsAttr &Attr,
3714 const unsigned Idx) {
3715
3716 if (S.DiagnoseUnexpandedParameterPack(E))
3717 return false;
3718
3719 // Accept template arguments for now as they depend on something else.
3720 // We'll get to check them when they eventually get instantiated.
3721 if (E->isValueDependent())
3722 return true;
3723
3724 llvm::APSInt I(64);
3725 if (!E->isIntegerConstantExpr(I, S.Context)) {
3726 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
3727 << &Attr << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
3728 return false;
3729 }
3730 // Make sure we can fit it in 32 bits.
3731 if (!I.isIntN(32)) {
3732 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
3733 << 32 << /* Unsigned */ 1;
3734 return false;
3735 }
3736 if (I < 0)
3737 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
3738 << &Attr << Idx << E->getSourceRange();
3739
3740 return true;
3741}
3742
3743void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads,
3744 Expr *MinBlocks, unsigned SpellingListIndex) {
3745 CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks,
3746 SpellingListIndex);
3747
3748 if (!checkLaunchBoundsArgument(*this, MaxThreads, TmpAttr, 0))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003749 return;
3750
Artem Belevich7093e402015-04-21 22:55:54 +00003751 if (MinBlocks && !checkLaunchBoundsArgument(*this, MinBlocks, TmpAttr, 1))
3752 return;
3753
3754 D->addAttr(::new (Context) CUDALaunchBoundsAttr(
3755 AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex));
3756}
3757
3758static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3759 const AttributeList &Attr) {
3760 if (!checkAttributeAtLeastNumArgs(S, Attr, 1) ||
3761 !checkAttributeAtMostNumArgs(S, Attr, 2))
3762 return;
3763
3764 S.AddLaunchBoundsAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
3765 Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr,
3766 Attr.getAttributeSpellingListIndex());
Peter Collingbourne827301e2010-12-12 23:03:07 +00003767}
3768
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003769static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3770 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003771 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003772 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003773 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003774 return;
3775 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003776
3777 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003778 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003779
Aaron Ballman00e99962013-08-31 01:11:41 +00003780 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003781
3782 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3783 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3784 << Attr.getName() << ExpectedFunctionOrMethod;
3785 return;
3786 }
3787
3788 uint64_t ArgumentIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003789 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3790 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003791 return;
3792
3793 uint64_t TypeTagIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003794 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3795 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003796 return;
3797
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003798 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003799 if (IsPointer) {
3800 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00003801 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003802 if (!BufferTy->isPointerType()) {
3803 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003804 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003805 }
3806 }
3807
Michael Han99315932013-01-24 16:46:58 +00003808 D->addAttr(::new (S.Context)
3809 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3810 ArgumentIdx, TypeTagIdx, IsPointer,
3811 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003812}
3813
3814static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3815 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003816 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003817 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003818 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003819 return;
3820 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003821
3822 if (!checkAttributeNumArgs(S, Attr, 1))
3823 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003824
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003825 if (!isa<VarDecl>(D)) {
3826 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3827 << Attr.getName() << ExpectedVariable;
3828 return;
3829 }
3830
Aaron Ballman00e99962013-08-31 01:11:41 +00003831 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00003832 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00003833 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3834 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003835
Michael Han99315932013-01-24 16:46:58 +00003836 D->addAttr(::new (S.Context)
3837 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003838 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003839 Attr.getLayoutCompatible(),
3840 Attr.getMustBeNull(),
3841 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003842}
3843
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003844//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003845// Checker-specific attribute handlers.
3846//===----------------------------------------------------------------------===//
3847
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003848static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) {
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003849 return type->isDependentType() ||
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003850 type->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003851}
3852
John McCalled433932011-01-25 03:31:58 +00003853static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003854 return type->isDependentType() ||
3855 type->isObjCObjectPointerType() ||
3856 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003857}
3858static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003859 return type->isDependentType() ||
3860 type->isPointerType() ||
3861 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003862}
3863
Chandler Carruthedc2c642011-07-02 00:01:44 +00003864static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003865 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003866 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003867
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003868 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003869 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3870 cf = false;
3871 } else {
3872 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3873 cf = true;
3874 }
3875
3876 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003877 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003878 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003879 return;
3880 }
3881
3882 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003883 param->addAttr(::new (S.Context)
3884 CFConsumedAttr(Attr.getRange(), S.Context,
3885 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003886 else
Michael Han99315932013-01-24 16:46:58 +00003887 param->addAttr(::new (S.Context)
3888 NSConsumedAttr(Attr.getRange(), S.Context,
3889 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003890}
3891
Chandler Carruthedc2c642011-07-02 00:01:44 +00003892static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3893 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003894
John McCalled433932011-01-25 03:31:58 +00003895 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003896
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003897 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003898 returnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003899 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003900 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003901 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003902 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3903 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003904 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003905 returnType = FD->getReturnType();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00003906 else if (auto *Param = dyn_cast<ParmVarDecl>(D)) {
3907 returnType = Param->getType()->getPointeeType();
3908 if (returnType.isNull()) {
3909 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
3910 << Attr.getName() << /*pointer-to-CF*/2
3911 << Attr.getRange();
3912 return;
3913 }
3914 } else {
3915 AttributeDeclKind ExpectedDeclKind;
3916 switch (Attr.getKind()) {
3917 default: llvm_unreachable("invalid ownership attribute");
3918 case AttributeList::AT_NSReturnsRetained:
3919 case AttributeList::AT_NSReturnsAutoreleased:
3920 case AttributeList::AT_NSReturnsNotRetained:
3921 ExpectedDeclKind = ExpectedFunctionOrMethod;
3922 break;
3923
3924 case AttributeList::AT_CFReturnsRetained:
3925 case AttributeList::AT_CFReturnsNotRetained:
3926 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
3927 break;
3928 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003929 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00003930 << Attr.getRange() << Attr.getName() << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003931 return;
3932 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003933
John McCalled433932011-01-25 03:31:58 +00003934 bool typeOK;
3935 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003936 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003937 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003938 case AttributeList::AT_NSReturnsRetained:
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003939 typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType);
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003940 cf = false;
3941 break;
3942
3943 case AttributeList::AT_NSReturnsAutoreleased:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003944 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003945 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3946 cf = false;
3947 break;
3948
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003949 case AttributeList::AT_CFReturnsRetained:
3950 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003951 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3952 cf = true;
3953 break;
3954 }
3955
3956 if (!typeOK) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00003957 if (isa<ParmVarDecl>(D)) {
3958 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
3959 << Attr.getName() << /*pointer-to-CF*/2
3960 << Attr.getRange();
3961 } else {
3962 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
3963 enum : unsigned {
3964 Function,
3965 Method,
3966 Property
3967 } SubjectKind = Function;
3968 if (isa<ObjCMethodDecl>(D))
3969 SubjectKind = Method;
3970 else if (isa<ObjCPropertyDecl>(D))
3971 SubjectKind = Property;
3972 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3973 << Attr.getName() << SubjectKind << cf
3974 << Attr.getRange();
3975 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003976 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003977 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003978
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003979 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003980 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003981 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003982 case AttributeList::AT_NSReturnsAutoreleased:
Nico Weber462fd1e2015-01-07 23:50:05 +00003983 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(
3984 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003985 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003986 case AttributeList::AT_CFReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00003987 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(
3988 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003989 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003990 case AttributeList::AT_NSReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00003991 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(
3992 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003993 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003994 case AttributeList::AT_CFReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00003995 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(
3996 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003997 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003998 case AttributeList::AT_NSReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00003999 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(
4000 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004001 return;
4002 };
4003}
4004
John McCallcf166702011-07-22 08:53:00 +00004005static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4006 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00004007 const int EP_ObjCMethod = 1;
4008 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004009
John McCallcf166702011-07-22 08:53:00 +00004010 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004011 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004012 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004013 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004014 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004015 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00004016
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004017 if (!resultType->isReferenceType() &&
4018 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004019 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00004020 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004021 << attr.getName()
4022 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004023 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00004024
4025 // Drop the attribute.
4026 return;
4027 }
4028
Nico Weber462fd1e2015-01-07 23:50:05 +00004029 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(
4030 attr.getRange(), S.Context, attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004031}
4032
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004033static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4034 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004035 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004036
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004037 DeclContext *DC = method->getDeclContext();
4038 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4039 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4040 << attr.getName() << 0;
4041 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4042 return;
4043 }
4044 if (method->getMethodFamily() == OMF_dealloc) {
4045 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4046 << attr.getName() << 1;
4047 return;
4048 }
4049
Michael Han99315932013-01-24 16:46:58 +00004050 method->addAttr(::new (S.Context)
4051 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4052 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004053}
4054
Aaron Ballmanfb763042013-12-02 18:05:46 +00004055static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
4056 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004057 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr.getRange(),
4058 Attr.getName()))
John McCall32f5fe12011-09-30 05:12:12 +00004059 return;
John McCall32f5fe12011-09-30 05:12:12 +00004060
Aaron Ballmanfb763042013-12-02 18:05:46 +00004061 D->addAttr(::new (S.Context)
4062 CFAuditedTransferAttr(Attr.getRange(), S.Context,
4063 Attr.getAttributeSpellingListIndex()));
4064}
4065
4066static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
4067 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004068 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr.getRange(),
4069 Attr.getName()))
Aaron Ballmanfb763042013-12-02 18:05:46 +00004070 return;
4071
4072 D->addAttr(::new (S.Context)
4073 CFUnknownTransferAttr(Attr.getRange(), S.Context,
4074 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004075}
4076
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004077static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
4078 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004079 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004080
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004081 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004082 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004083 return;
4084 }
John McCall28592582015-02-01 22:34:06 +00004085
4086 // Typedefs only allow objc_bridge(id) and have some additional checking.
4087 if (auto TD = dyn_cast<TypedefNameDecl>(D)) {
4088 if (!Parm->Ident->isStr("id")) {
4089 S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_id)
4090 << Attr.getName();
4091 return;
4092 }
4093
4094 // Only allow 'cv void *'.
4095 QualType T = TD->getUnderlyingType();
4096 if (!T->isVoidPointerType()) {
4097 S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
4098 return;
4099 }
4100 }
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004101
4102 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004103 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004104 Attr.getAttributeSpellingListIndex()));
4105}
4106
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004107static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
4108 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004109 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
4110
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004111 if (!Parm) {
4112 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
4113 return;
4114 }
4115
4116 D->addAttr(::new (S.Context)
4117 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
4118 Attr.getAttributeSpellingListIndex()));
4119}
4120
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004121static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
4122 const AttributeList &Attr) {
4123 IdentifierInfo *RelatedClass =
Craig Topperc3ec1492014-05-26 06:22:03 +00004124 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004125 if (!RelatedClass) {
4126 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
4127 return;
4128 }
4129 IdentifierInfo *ClassMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00004130 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004131 IdentifierInfo *InstanceMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00004132 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004133 D->addAttr(::new (S.Context)
4134 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
4135 ClassMethod, InstanceMethod,
4136 Attr.getAttributeSpellingListIndex()));
4137}
4138
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004139static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
4140 const AttributeList &Attr) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004141 ObjCInterfaceDecl *IFace;
Nico Weber462fd1e2015-01-07 23:50:05 +00004142 if (ObjCCategoryDecl *CatDecl =
4143 dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004144 IFace = CatDecl->getClassInterface();
4145 else
4146 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00004147
4148 if (!IFace)
4149 return;
4150
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00004151 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00004152 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004153 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
4154 Attr.getAttributeSpellingListIndex()));
4155}
4156
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004157static void handleObjCRuntimeName(Sema &S, Decl *D,
4158 const AttributeList &Attr) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004159 StringRef MetaDataName;
4160 if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName))
4161 return;
4162 D->addAttr(::new (S.Context)
4163 ObjCRuntimeNameAttr(Attr.getRange(), S.Context,
4164 MetaDataName,
4165 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004166}
4167
Alex Denisovfde64952015-06-26 05:28:36 +00004168// when a user wants to use objc_boxable with a union or struct
4169// but she doesn't have access to the declaration (legacy/third-party code)
4170// then she can 'enable' this feature via trick with a typedef
4171// e.g.:
4172// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
4173static void handleObjCBoxable(Sema &S, Decl *D, const AttributeList &Attr) {
4174 bool notify = false;
4175
4176 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4177 if (RD && RD->getDefinition()) {
4178 RD = RD->getDefinition();
4179 notify = true;
4180 }
4181
4182 if (RD) {
4183 ObjCBoxableAttr *BoxableAttr = ::new (S.Context)
4184 ObjCBoxableAttr(Attr.getRange(), S.Context,
4185 Attr.getAttributeSpellingListIndex());
4186 RD->addAttr(BoxableAttr);
4187 if (notify) {
4188 // we need to notify ASTReader/ASTWriter about
4189 // modification of existing declaration
4190 if (ASTMutationListener *L = S.getASTMutationListener())
4191 L->AddedAttributeToRecord(BoxableAttr, RD);
4192 }
4193 }
4194}
4195
Chandler Carruthedc2c642011-07-02 00:01:44 +00004196static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4197 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004198 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004199
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004200 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004201 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004202}
4203
Chandler Carruthedc2c642011-07-02 00:01:44 +00004204static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4205 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004206 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004207 QualType type = vd->getType();
4208
4209 if (!type->isDependentType() &&
4210 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004211 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004212 << type;
4213 return;
4214 }
4215
4216 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4217
4218 // If we have no lifetime yet, check the lifetime we're presumably
4219 // going to infer.
4220 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4221 lifetime = type->getObjCARCImplicitLifetime();
4222
4223 switch (lifetime) {
4224 case Qualifiers::OCL_None:
4225 assert(type->isDependentType() &&
4226 "didn't infer lifetime for non-dependent type?");
4227 break;
4228
4229 case Qualifiers::OCL_Weak: // meaningful
4230 case Qualifiers::OCL_Strong: // meaningful
4231 break;
4232
4233 case Qualifiers::OCL_ExplicitNone:
4234 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004235 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004236 << (lifetime == Qualifiers::OCL_Autoreleasing);
4237 break;
4238 }
4239
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004240 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004241 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4242 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004243}
4244
Francois Picheta83957a2010-12-19 06:50:37 +00004245//===----------------------------------------------------------------------===//
4246// Microsoft specific attribute handlers.
4247//===----------------------------------------------------------------------===//
4248
Chandler Carruthedc2c642011-07-02 00:01:44 +00004249static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00004250 if (!S.LangOpts.CPlusPlus) {
4251 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4252 << Attr.getName() << AttributeLangSupport::C;
4253 return;
4254 }
4255
Aaron Ballman60e705e2013-11-24 20:58:02 +00004256 if (!isa<CXXRecordDecl>(D)) {
4257 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4258 << Attr.getName() << ExpectedClass;
4259 return;
4260 }
4261
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004262 StringRef StrRef;
4263 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00004264 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00004265 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004266
David Majnemer89085342013-08-09 08:56:20 +00004267 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4268 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00004269 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4270 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00004271
Reid Kleckner140c4a72013-05-17 14:04:52 +00004272 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00004273 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004274 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004275 return;
4276 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004277
David Majnemer89085342013-08-09 08:56:20 +00004278 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004279 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00004280 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004281 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00004282 return;
4283 }
David Majnemer89085342013-08-09 08:56:20 +00004284 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004285 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004286 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004287 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004288 }
Francois Picheta83957a2010-12-19 06:50:37 +00004289
David Majnemer89085342013-08-09 08:56:20 +00004290 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4291 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004292}
4293
David Majnemer2c4e00a2014-01-29 22:07:36 +00004294static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4295 if (!S.LangOpts.CPlusPlus) {
4296 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4297 << Attr.getName() << AttributeLangSupport::C;
4298 return;
4299 }
4300 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
David Majnemer4bb09802014-02-10 19:50:15 +00004301 D, Attr.getRange(), /*BestCase=*/true,
4302 Attr.getAttributeSpellingListIndex(),
David Majnemer2c4e00a2014-01-29 22:07:36 +00004303 (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling());
4304 if (IA)
4305 D->addAttr(IA);
4306}
4307
Reid Kleckner7d6d2702014-05-01 03:16:47 +00004308static void handleDeclspecThreadAttr(Sema &S, Decl *D,
4309 const AttributeList &Attr) {
4310 VarDecl *VD = cast<VarDecl>(D);
4311 if (!S.Context.getTargetInfo().isTLSSupported()) {
4312 S.Diag(Attr.getLoc(), diag::err_thread_unsupported);
4313 return;
4314 }
4315 if (VD->getTSCSpec() != TSCS_unspecified) {
4316 S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable);
4317 return;
4318 }
4319 if (VD->hasLocalStorage()) {
4320 S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
4321 return;
4322 }
4323 VD->addAttr(::new (S.Context) ThreadAttr(
4324 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
4325}
4326
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004327static void handleARMInterruptAttr(Sema &S, Decl *D,
4328 const AttributeList &Attr) {
4329 // Check the attribute arguments.
4330 if (Attr.getNumArgs() > 1) {
4331 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
4332 << Attr.getName() << 1;
4333 return;
4334 }
4335
4336 StringRef Str;
4337 SourceLocation ArgLoc;
4338
4339 if (Attr.getNumArgs() == 0)
4340 Str = "";
4341 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
4342 return;
4343
4344 ARMInterruptAttr::InterruptType Kind;
4345 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
4346 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
4347 << Attr.getName() << Str << ArgLoc;
4348 return;
4349 }
4350
4351 unsigned Index = Attr.getAttributeSpellingListIndex();
4352 D->addAttr(::new (S.Context)
4353 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
4354}
4355
4356static void handleMSP430InterruptAttr(Sema &S, Decl *D,
4357 const AttributeList &Attr) {
4358 if (!checkAttributeNumArgs(S, Attr, 1))
4359 return;
4360
4361 if (!Attr.isArgExpr(0)) {
4362 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
4363 << AANT_ArgumentIntegerConstant;
4364 return;
4365 }
4366
4367 // FIXME: Check for decl - it should be void ()(void).
4368
4369 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4370 llvm::APSInt NumParams(32);
4371 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
4372 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4373 << Attr.getName() << AANT_ArgumentIntegerConstant
4374 << NumParamsExpr->getSourceRange();
4375 return;
4376 }
4377
4378 unsigned Num = NumParams.getLimitedValue(255);
4379 if ((Num & 1) || Num > 30) {
4380 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
4381 << Attr.getName() << (int)NumParams.getSExtValue()
4382 << NumParamsExpr->getSourceRange();
4383 return;
4384 }
4385
Aaron Ballman36a53502014-01-16 13:03:14 +00004386 D->addAttr(::new (S.Context)
4387 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
4388 Attr.getAttributeSpellingListIndex()));
4389 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004390}
4391
4392static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4393 // Dispatch the interrupt attribute based on the current target.
4394 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
4395 handleMSP430InterruptAttr(S, D, Attr);
4396 else
4397 handleARMInterruptAttr(S, D, Attr);
4398}
4399
Matt Arsenault43fae6c2014-12-04 20:38:18 +00004400static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D,
4401 const AttributeList &Attr) {
4402 uint32_t NumRegs;
4403 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4404 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4405 return;
4406
4407 D->addAttr(::new (S.Context)
4408 AMDGPUNumVGPRAttr(Attr.getLoc(), S.Context,
4409 NumRegs,
4410 Attr.getAttributeSpellingListIndex()));
4411}
4412
4413static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D,
4414 const AttributeList &Attr) {
4415 uint32_t NumRegs;
4416 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4417 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4418 return;
4419
4420 D->addAttr(::new (S.Context)
4421 AMDGPUNumSGPRAttr(Attr.getLoc(), S.Context,
4422 NumRegs,
4423 Attr.getAttributeSpellingListIndex()));
4424}
4425
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004426static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
4427 const AttributeList& Attr) {
4428 // If we try to apply it to a function pointer, don't warn, but don't
4429 // do anything, either. It doesn't matter anyway, because there's nothing
4430 // special about calling a force_align_arg_pointer function.
4431 ValueDecl *VD = dyn_cast<ValueDecl>(D);
4432 if (VD && VD->getType()->isFunctionPointerType())
4433 return;
4434 // Also don't warn on function pointer typedefs.
4435 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
4436 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
4437 TD->getUnderlyingType()->isFunctionType()))
4438 return;
4439 // Attribute can only be applied to function types.
4440 if (!isa<FunctionDecl>(D)) {
4441 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4442 << Attr.getName() << /* function */0;
4443 return;
4444 }
4445
Aaron Ballman36a53502014-01-16 13:03:14 +00004446 D->addAttr(::new (S.Context)
4447 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
4448 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004449}
4450
4451DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
4452 unsigned AttrSpellingListIndex) {
4453 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004454 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00004455 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004456 }
4457
4458 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004459 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004460
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004461 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004462}
4463
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004464DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
4465 unsigned AttrSpellingListIndex) {
4466 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004467 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004468 D->dropAttr<DLLImportAttr>();
4469 }
4470
4471 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004472 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004473
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004474 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004475}
4476
Hans Wennborge82f19c2014-06-24 23:57:05 +00004477static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00004478 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
4479 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4480 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored)
4481 << A.getName();
4482 return;
4483 }
4484
Hans Wennborg606bd6d2014-11-03 14:24:45 +00004485 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4486 if (FD->isInlined() && A.getKind() == AttributeList::AT_DLLImport &&
4487 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4488 // MinGW doesn't allow dllimport on inline functions.
4489 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
4490 << A.getName();
4491 return;
4492 }
4493 }
4494
Hans Wennborg5869ec42015-09-15 21:05:30 +00004495 if (auto *MD = dyn_cast<CXXMethodDecl>(D)) {
4496 if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
4497 MD->getParent()->isLambda()) {
4498 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A.getName();
4499 return;
4500 }
4501 }
4502
Hans Wennborge82f19c2014-06-24 23:57:05 +00004503 unsigned Index = A.getAttributeSpellingListIndex();
4504 Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport
4505 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
4506 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004507 if (NewAttr)
4508 D->addAttr(NewAttr);
4509}
4510
David Majnemer2c4e00a2014-01-29 22:07:36 +00004511MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00004512Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00004513 unsigned AttrSpellingListIndex,
4514 MSInheritanceAttr::Spelling SemanticSpelling) {
4515 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
4516 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00004517 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004518 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
4519 << 1 /*previous declaration*/;
4520 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
4521 D->dropAttr<MSInheritanceAttr>();
4522 }
4523
4524 CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
4525 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00004526 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
4527 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004528 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004529 }
4530 } else {
4531 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
4532 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4533 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004534 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004535 }
4536 if (RD->getDescribedClassTemplate()) {
4537 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4538 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004539 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004540 }
4541 }
4542
4543 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00004544 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00004545}
4546
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004547static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4548 // The capability attributes take a single string parameter for the name of
4549 // the capability they represent. The lockable attribute does not take any
4550 // parameters. However, semantically, both attributes represent the same
4551 // concept, and so they use the same semantic attribute. Eventually, the
4552 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00004553 //
Alp Toker958027b2014-07-14 19:42:55 +00004554 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00004555 // literal will be considered a "mutex."
4556 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004557 SourceLocation LiteralLoc;
4558 if (Attr.getKind() == AttributeList::AT_Capability &&
4559 !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc))
4560 return;
4561
Aaron Ballman6c810072014-03-05 21:47:13 +00004562 // Currently, there are only two names allowed for a capability: role and
4563 // mutex (case insensitive). Diagnose other capability names.
4564 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
4565 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
4566
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004567 D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N,
4568 Attr.getAttributeSpellingListIndex()));
4569}
4570
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004571static void handleAssertCapabilityAttr(Sema &S, Decl *D,
4572 const AttributeList &Attr) {
4573 D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context,
4574 Attr.getArgAsExpr(0),
4575 Attr.getAttributeSpellingListIndex()));
4576}
4577
4578static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
4579 const AttributeList &Attr) {
4580 SmallVector<Expr*, 1> Args;
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004581 if (!checkLockFunAttrCommon(S, D, Attr, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004582 return;
4583
4584 D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(),
4585 S.Context,
4586 Args.data(), Args.size(),
4587 Attr.getAttributeSpellingListIndex()));
4588}
4589
4590static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
4591 const AttributeList &Attr) {
4592 SmallVector<Expr*, 2> Args;
4593 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
4594 return;
4595
4596 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(),
4597 S.Context,
4598 Attr.getArgAsExpr(0),
4599 Args.data(),
4600 Args.size(),
4601 Attr.getAttributeSpellingListIndex()));
4602}
4603
4604static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
4605 const AttributeList &Attr) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004606 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004607 SmallVector<Expr *, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004608 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004609
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004610 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
4611 Attr.getRange(), S.Context, Args.data(), Args.size(),
4612 Attr.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004613}
4614
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004615static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
4616 const AttributeList &Attr) {
4617 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
4618 return;
4619
4620 // check that all arguments are lockable objects
4621 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004622 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004623 if (Args.empty())
4624 return;
4625
4626 RequiresCapabilityAttr *RCA = ::new (S.Context)
4627 RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(),
4628 Args.size(), Attr.getAttributeSpellingListIndex());
4629
4630 D->addAttr(RCA);
4631}
4632
Aaron Ballman43f40102014-11-14 22:34:56 +00004633static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4634 if (auto *NSD = dyn_cast<NamespaceDecl>(D)) {
4635 if (NSD->isAnonymousNamespace()) {
4636 S.Diag(Attr.getLoc(), diag::warn_deprecated_anonymous_namespace);
4637 // Do not want to attach the attribute to the namespace because that will
4638 // cause confusing diagnostic reports for uses of declarations within the
4639 // namespace.
4640 return;
4641 }
4642 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00004643
4644 if (!S.getLangOpts().CPlusPlus14)
4645 if (Attr.isCXX11Attribute() &&
4646 !(Attr.hasScope() && Attr.getScopeName()->isStr("gnu")))
Saleem Abdulrasoolb47d6062015-02-18 04:33:26 +00004647 S.Diag(Attr.getLoc(), diag::ext_deprecated_attr_is_a_cxx14_extension);
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00004648
Aaron Ballman43f40102014-11-14 22:34:56 +00004649 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
4650}
4651
Peter Collingbourne915df992015-05-15 18:33:32 +00004652static void handleNoSanitizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4653 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
4654 return;
4655
4656 std::vector<std::string> Sanitizers;
4657
4658 for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) {
4659 StringRef SanitizerName;
4660 SourceLocation LiteralLoc;
4661
4662 if (!S.checkStringLiteralArgumentAttr(Attr, I, SanitizerName, &LiteralLoc))
4663 return;
4664
4665 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0)
4666 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
4667
4668 Sanitizers.push_back(SanitizerName);
4669 }
4670
4671 D->addAttr(::new (S.Context) NoSanitizeAttr(
4672 Attr.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(),
4673 Attr.getAttributeSpellingListIndex()));
4674}
4675
4676static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
4677 const AttributeList &Attr) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00004678 StringRef AttrName = Attr.getName()->getName();
4679 normalizeName(AttrName);
Peter Collingbourne915df992015-05-15 18:33:32 +00004680 std::string SanitizerName =
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00004681 llvm::StringSwitch<std::string>(AttrName)
Peter Collingbourne915df992015-05-15 18:33:32 +00004682 .Case("no_address_safety_analysis", "address")
4683 .Case("no_sanitize_address", "address")
4684 .Case("no_sanitize_thread", "thread")
Peter Collingbourne94410942015-05-15 20:11:18 +00004685 .Case("no_sanitize_memory", "memory");
Peter Collingbourne915df992015-05-15 18:33:32 +00004686 D->addAttr(::new (S.Context)
4687 NoSanitizeAttr(Attr.getRange(), S.Context, &SanitizerName, 1,
4688 Attr.getAttributeSpellingListIndex()));
4689}
4690
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004691static void handleInternalLinkageAttr(Sema &S, Decl *D,
4692 const AttributeList &Attr) {
4693 if (InternalLinkageAttr *Internal =
4694 S.mergeInternalLinkageAttr(D, Attr.getRange(), Attr.getName(),
4695 Attr.getAttributeSpellingListIndex()))
4696 D->addAttr(Internal);
4697}
4698
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004699/// Handles semantic checking for features that are common to all attributes,
4700/// such as checking whether a parameter was properly specified, or the correct
4701/// number of arguments were passed, etc.
4702static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4703 const AttributeList &Attr) {
4704 // Several attributes carry different semantics than the parsing requires, so
4705 // those are opted out of the common handling.
4706 //
4707 // We also bail on unknown and ignored attributes because those are handled
4708 // as part of the target-specific handling logic.
4709 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004710 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004711 return false;
4712
Aaron Ballman3aff6332013-12-02 19:30:36 +00004713 // Check whether the attribute requires specific language extensions to be
4714 // enabled.
4715 if (!Attr.diagnoseLangOpts(S))
4716 return true;
4717
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00004718 if (Attr.getMinArgs() == Attr.getMaxArgs()) {
4719 // If there are no optional arguments, then checking for the argument count
4720 // is trivial.
4721 if (!checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4722 return true;
4723 } else {
4724 // There are optional arguments, so checking is slightly more involved.
4725 if (Attr.getMinArgs() &&
4726 !checkAttributeAtLeastNumArgs(S, Attr, Attr.getMinArgs()))
4727 return true;
4728 else if (!Attr.hasVariadicArg() && Attr.getMaxArgs() &&
4729 !checkAttributeAtMostNumArgs(S, Attr, Attr.getMaxArgs()))
4730 return true;
4731 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004732
4733 // Check whether the attribute appertains to the given subject.
4734 if (!Attr.diagnoseAppertainsTo(S, D))
4735 return true;
4736
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004737 return false;
4738}
4739
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004740//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004741// Top Level Sema Entry Points
4742//===----------------------------------------------------------------------===//
4743
Richard Smithf8a75c32013-08-29 00:47:48 +00004744/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4745/// the attribute applies to decls. If the attribute is a type attribute, just
4746/// silently ignore it if a GNU attribute.
4747static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4748 const AttributeList &Attr,
4749 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004750 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00004751 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004752
Richard Smithf8a75c32013-08-29 00:47:48 +00004753 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4754 // instead.
4755 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4756 return;
4757
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004758 // Unknown attributes are automatically warned on. Target-specific attributes
4759 // which do not apply to the current target architecture are treated as
4760 // though they were unknown attributes.
4761 if (Attr.getKind() == AttributeList::UnknownAttribute ||
Bob Wilson7c730832015-07-20 22:57:31 +00004762 !Attr.existsInTarget(S.Context.getTargetInfo())) {
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004763 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute()
4764 ? diag::warn_unhandled_ms_attribute_ignored
4765 : diag::warn_unknown_attribute_ignored)
4766 << Attr.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004767 return;
4768 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004769
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004770 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4771 return;
4772
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004773 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004774 default:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004775 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004776 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004777 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004778 case AttributeList::AT_Interrupt:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004779 handleInterruptAttr(S, D, Attr);
4780 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004781 case AttributeList::AT_X86ForceAlignArgPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004782 handleX86ForceAlignArgPointerAttr(S, D, Attr);
4783 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004784 case AttributeList::AT_DLLExport:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004785 case AttributeList::AT_DLLImport:
Hans Wennborge82f19c2014-06-24 23:57:05 +00004786 handleDLLAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004787 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004788 case AttributeList::AT_Mips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004789 handleSimpleAttribute<Mips16Attr>(S, D, Attr);
4790 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004791 case AttributeList::AT_NoMips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004792 handleSimpleAttribute<NoMips16Attr>(S, D, Attr);
4793 break;
Matt Arsenault43fae6c2014-12-04 20:38:18 +00004794 case AttributeList::AT_AMDGPUNumVGPR:
4795 handleAMDGPUNumVGPRAttr(S, D, Attr);
4796 break;
4797 case AttributeList::AT_AMDGPUNumSGPR:
4798 handleAMDGPUNumSGPRAttr(S, D, Attr);
4799 break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00004800 case AttributeList::AT_IBAction:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004801 handleSimpleAttribute<IBActionAttr>(S, D, Attr);
4802 break;
4803 case AttributeList::AT_IBOutlet:
4804 handleIBOutlet(S, D, Attr);
4805 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004806 case AttributeList::AT_IBOutletCollection:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004807 handleIBOutletCollection(S, D, Attr);
4808 break;
4809 case AttributeList::AT_Alias:
4810 handleAliasAttr(S, D, Attr);
4811 break;
4812 case AttributeList::AT_Aligned:
4813 handleAlignedAttr(S, D, Attr);
4814 break;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00004815 case AttributeList::AT_AlignValue:
4816 handleAlignValueAttr(S, D, Attr);
4817 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004818 case AttributeList::AT_AlwaysInline:
Paul Robinsonf0674352014-03-31 22:29:15 +00004819 handleAlwaysInlineAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004820 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004821 case AttributeList::AT_AnalyzerNoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004822 handleAnalyzerNoReturnAttr(S, D, Attr);
4823 break;
4824 case AttributeList::AT_TLSModel:
4825 handleTLSModelAttr(S, D, Attr);
4826 break;
4827 case AttributeList::AT_Annotate:
4828 handleAnnotateAttr(S, D, Attr);
4829 break;
4830 case AttributeList::AT_Availability:
4831 handleAvailabilityAttr(S, D, Attr);
4832 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004833 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004834 handleDependencyAttr(S, scope, D, Attr);
4835 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004836 case AttributeList::AT_Common:
4837 handleCommonAttr(S, D, Attr);
4838 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004839 case AttributeList::AT_CUDAConstant:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004840 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr);
4841 break;
4842 case AttributeList::AT_Constructor:
4843 handleConstructorAttr(S, D, Attr);
4844 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004845 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004846 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr);
4847 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004848 case AttributeList::AT_Deprecated:
Aaron Ballman43f40102014-11-14 22:34:56 +00004849 handleDeprecatedAttr(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004850 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004851 case AttributeList::AT_Destructor:
4852 handleDestructorAttr(S, D, Attr);
4853 break;
4854 case AttributeList::AT_EnableIf:
4855 handleEnableIfAttr(S, D, Attr);
4856 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004857 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004858 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004859 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004860 case AttributeList::AT_MinSize:
Paul Robinsonaae2fba2014-12-10 23:34:36 +00004861 handleMinSizeAttr(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00004862 break;
Paul Robinsonf0674352014-03-31 22:29:15 +00004863 case AttributeList::AT_OptimizeNone:
4864 handleOptimizeNoneAttr(S, D, Attr);
4865 break;
Alexis Hunt724f14e2014-11-28 00:53:20 +00004866 case AttributeList::AT_FlagEnum:
4867 handleSimpleAttribute<FlagEnumAttr>(S, D, Attr);
4868 break;
Peter Collingbourne41af7c22014-05-20 17:12:51 +00004869 case AttributeList::AT_Flatten:
4870 handleSimpleAttribute<FlattenAttr>(S, D, Attr);
4871 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004872 case AttributeList::AT_Format:
4873 handleFormatAttr(S, D, Attr);
4874 break;
4875 case AttributeList::AT_FormatArg:
4876 handleFormatArgAttr(S, D, Attr);
4877 break;
4878 case AttributeList::AT_CUDAGlobal:
4879 handleGlobalAttr(S, D, Attr);
4880 break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00004881 case AttributeList::AT_CUDADevice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004882 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr);
4883 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004884 case AttributeList::AT_CUDAHost:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004885 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr);
4886 break;
4887 case AttributeList::AT_GNUInline:
4888 handleGNUInlineAttr(S, D, Attr);
4889 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004890 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004891 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004892 break;
David Majnemer631a90b2015-02-04 07:23:21 +00004893 case AttributeList::AT_Restrict:
4894 handleRestrictAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004895 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004896 case AttributeList::AT_MayAlias:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004897 handleSimpleAttribute<MayAliasAttr>(S, D, Attr);
4898 break;
4899 case AttributeList::AT_Mode:
4900 handleModeAttr(S, D, Attr);
4901 break;
David Majnemer1bf0f8e2015-07-20 22:51:52 +00004902 case AttributeList::AT_NoAlias:
4903 handleSimpleAttribute<NoAliasAttr>(S, D, Attr);
4904 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004905 case AttributeList::AT_NoCommon:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004906 handleSimpleAttribute<NoCommonAttr>(S, D, Attr);
4907 break;
Peter Collingbourneb4728c12014-05-19 22:14:34 +00004908 case AttributeList::AT_NoSplitStack:
4909 handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr);
4910 break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00004911 case AttributeList::AT_NonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004912 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
4913 handleNonNullAttrParameter(S, PVD, Attr);
4914 else
4915 handleNonNullAttr(S, D, Attr);
4916 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00004917 case AttributeList::AT_ReturnsNonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004918 handleReturnsNonNullAttr(S, D, Attr);
4919 break;
Hal Finkelee90a222014-09-26 05:04:30 +00004920 case AttributeList::AT_AssumeAligned:
4921 handleAssumeAlignedAttr(S, D, Attr);
4922 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004923 case AttributeList::AT_Overloadable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004924 handleSimpleAttribute<OverloadableAttr>(S, D, Attr);
4925 break;
4926 case AttributeList::AT_Ownership:
4927 handleOwnershipAttr(S, D, Attr);
4928 break;
4929 case AttributeList::AT_Cold:
4930 handleColdAttr(S, D, Attr);
4931 break;
4932 case AttributeList::AT_Hot:
4933 handleHotAttr(S, D, Attr);
4934 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004935 case AttributeList::AT_Naked:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004936 handleSimpleAttribute<NakedAttr>(S, D, Attr);
4937 break;
4938 case AttributeList::AT_NoReturn:
4939 handleNoReturnAttr(S, D, Attr);
4940 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004941 case AttributeList::AT_NoThrow:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004942 handleSimpleAttribute<NoThrowAttr>(S, D, Attr);
4943 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004944 case AttributeList::AT_CUDAShared:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004945 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr);
4946 break;
4947 case AttributeList::AT_VecReturn:
4948 handleVecReturnAttr(S, D, Attr);
4949 break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004950
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004951 case AttributeList::AT_ObjCOwnership:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004952 handleObjCOwnershipAttr(S, D, Attr);
4953 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004954 case AttributeList::AT_ObjCPreciseLifetime:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004955 handleObjCPreciseLifetimeAttr(S, D, Attr);
4956 break;
John McCall31168b02011-06-15 23:02:42 +00004957
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004958 case AttributeList::AT_ObjCReturnsInnerPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004959 handleObjCReturnsInnerPointerAttr(S, D, Attr);
4960 break;
John McCallcf166702011-07-22 08:53:00 +00004961
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004962 case AttributeList::AT_ObjCRequiresSuper:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004963 handleObjCRequiresSuperAttr(S, D, Attr);
4964 break;
4965
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004966 case AttributeList::AT_ObjCBridge:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004967 handleObjCBridgeAttr(S, scope, D, Attr);
4968 break;
4969
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004970 case AttributeList::AT_ObjCBridgeMutable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004971 handleObjCBridgeMutableAttr(S, scope, D, Attr);
4972 break;
4973
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004974 case AttributeList::AT_ObjCBridgeRelated:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004975 handleObjCBridgeRelatedAttr(S, scope, D, Attr);
4976 break;
John McCallf1e8b342011-09-29 07:17:38 +00004977
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004978 case AttributeList::AT_ObjCDesignatedInitializer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004979 handleObjCDesignatedInitializer(S, D, Attr);
4980 break;
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004981
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004982 case AttributeList::AT_ObjCRuntimeName:
4983 handleObjCRuntimeName(S, D, Attr);
4984 break;
Alex Denisovfde64952015-06-26 05:28:36 +00004985
4986 case AttributeList::AT_ObjCBoxable:
4987 handleObjCBoxable(S, D, Attr);
4988 break;
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004989
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004990 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004991 handleCFAuditedTransferAttr(S, D, Attr);
4992 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004993 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004994 handleCFUnknownTransferAttr(S, D, Attr);
4995 break;
John McCall32f5fe12011-09-30 05:12:12 +00004996
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004997 case AttributeList::AT_CFConsumed:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004998 case AttributeList::AT_NSConsumed:
4999 handleNSConsumedAttr(S, D, Attr);
5000 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005001 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005002 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr);
5003 break;
John McCalled433932011-01-25 03:31:58 +00005004
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005005 case AttributeList::AT_NSReturnsAutoreleased:
5006 case AttributeList::AT_NSReturnsNotRetained:
5007 case AttributeList::AT_CFReturnsNotRetained:
5008 case AttributeList::AT_NSReturnsRetained:
5009 case AttributeList::AT_CFReturnsRetained:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005010 handleNSReturnsRetainedAttr(S, D, Attr);
5011 break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00005012 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005013 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr);
5014 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005015 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005016 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr);
5017 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00005018 case AttributeList::AT_VecTypeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005019 handleVecTypeHint(S, D, Attr);
5020 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00005021
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005022 case AttributeList::AT_InitPriority:
5023 handleInitPriorityAttr(S, D, Attr);
5024 break;
5025
5026 case AttributeList::AT_Packed:
5027 handlePackedAttr(S, D, Attr);
5028 break;
5029 case AttributeList::AT_Section:
5030 handleSectionAttr(S, D, Attr);
5031 break;
Eric Christopher11acf732015-06-12 01:35:52 +00005032 case AttributeList::AT_Target:
5033 handleTargetAttr(S, D, Attr);
5034 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005035 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00005036 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00005037 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005038 case AttributeList::AT_ArcWeakrefUnavailable:
5039 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr);
5040 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005041 case AttributeList::AT_ObjCRootClass:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005042 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr);
5043 break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00005044 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek438f8db2014-02-22 01:06:05 +00005045 handleObjCSuppresProtocolAttr(S, D, Attr);
Ted Kremenek28eace62013-11-23 01:01:34 +00005046 break;
5047 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005048 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr);
5049 break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00005050 case AttributeList::AT_Unused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005051 handleSimpleAttribute<UnusedAttr>(S, D, Attr);
5052 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005053 case AttributeList::AT_ReturnsTwice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005054 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr);
5055 break;
Akira Hatanakac8667622015-11-06 23:56:15 +00005056 case AttributeList::AT_NotTailCalled:
5057 handleNotTailCalledAttr(S, D, Attr);
5058 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005059 case AttributeList::AT_Used:
5060 handleUsedAttr(S, D, Attr);
5061 break;
John McCalld041a9b2013-02-20 01:54:26 +00005062 case AttributeList::AT_Visibility:
5063 handleVisibilityAttr(S, D, Attr, false);
5064 break;
5065 case AttributeList::AT_TypeVisibility:
5066 handleVisibilityAttr(S, D, Attr, true);
5067 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00005068 case AttributeList::AT_WarnUnused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005069 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr);
5070 break;
5071 case AttributeList::AT_WarnUnusedResult:
5072 handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00005073 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00005074 case AttributeList::AT_Weak:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005075 handleSimpleAttribute<WeakAttr>(S, D, Attr);
5076 break;
5077 case AttributeList::AT_WeakRef:
5078 handleWeakRefAttr(S, D, Attr);
5079 break;
5080 case AttributeList::AT_WeakImport:
5081 handleWeakImportAttr(S, D, Attr);
5082 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005083 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005084 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005085 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005086 case AttributeList::AT_ObjCException:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005087 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr);
5088 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005089 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005090 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00005091 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005092 case AttributeList::AT_ObjCNSObject:
5093 handleObjCNSObject(S, D, Attr);
5094 break;
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00005095 case AttributeList::AT_ObjCIndependentClass:
5096 handleObjCIndependentClass(S, D, Attr);
5097 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005098 case AttributeList::AT_Blocks:
5099 handleBlocksAttr(S, D, Attr);
5100 break;
5101 case AttributeList::AT_Sentinel:
5102 handleSentinelAttr(S, D, Attr);
5103 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00005104 case AttributeList::AT_Const:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005105 handleSimpleAttribute<ConstAttr>(S, D, Attr);
5106 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005107 case AttributeList::AT_Pure:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005108 handleSimpleAttribute<PureAttr>(S, D, Attr);
5109 break;
5110 case AttributeList::AT_Cleanup:
5111 handleCleanupAttr(S, D, Attr);
5112 break;
5113 case AttributeList::AT_NoDebug:
5114 handleNoDebugAttr(S, D, Attr);
5115 break;
Aaron Ballman7c19ab12014-02-22 16:59:24 +00005116 case AttributeList::AT_NoDuplicate:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005117 handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr);
5118 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005119 case AttributeList::AT_NoInline:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005120 handleSimpleAttribute<NoInlineAttr>(S, D, Attr);
5121 break;
5122 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
5123 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr);
5124 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005125 case AttributeList::AT_StdCall:
5126 case AttributeList::AT_CDecl:
5127 case AttributeList::AT_FastCall:
5128 case AttributeList::AT_ThisCall:
5129 case AttributeList::AT_Pascal:
Reid Klecknerd7857f02014-10-24 17:42:17 +00005130 case AttributeList::AT_VectorCall:
Charles Davisb5a214e2013-08-30 04:39:01 +00005131 case AttributeList::AT_MSABI:
5132 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005133 case AttributeList::AT_Pcs:
Guy Benyeif0a014b2012-12-25 08:53:55 +00005134 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005135 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00005136 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005137 case AttributeList::AT_OpenCLKernel:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005138 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr);
5139 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00005140 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005141 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr);
5142 break;
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005143 case AttributeList::AT_InternalLinkage:
5144 handleInternalLinkageAttr(S, D, Attr);
5145 break;
John McCall8d32c052012-05-22 21:28:12 +00005146
5147 // Microsoft attributes:
David Majnemer8ab003a2015-02-02 19:30:52 +00005148 case AttributeList::AT_MSNoVTable:
5149 handleSimpleAttribute<MSNoVTableAttr>(S, D, Attr);
David Majnemer129f4172015-02-02 10:22:20 +00005150 break;
David Majnemer8ab003a2015-02-02 19:30:52 +00005151 case AttributeList::AT_MSStruct:
5152 handleSimpleAttribute<MSStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00005153 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005154 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005155 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00005156 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00005157 case AttributeList::AT_MSInheritance:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005158 handleMSInheritanceAttr(S, D, Attr);
5159 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00005160 case AttributeList::AT_SelectAny:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005161 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr);
5162 break;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005163 case AttributeList::AT_Thread:
5164 handleDeclspecThreadAttr(S, D, Attr);
5165 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005166
5167 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00005168 case AttributeList::AT_AssertExclusiveLock:
5169 handleAssertExclusiveLockAttr(S, D, Attr);
5170 break;
5171 case AttributeList::AT_AssertSharedLock:
5172 handleAssertSharedLockAttr(S, D, Attr);
5173 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005174 case AttributeList::AT_GuardedVar:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005175 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr);
5176 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005177 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00005178 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005179 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005180 case AttributeList::AT_ScopedLockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005181 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr);
5182 break;
Peter Collingbourne915df992015-05-15 18:33:32 +00005183 case AttributeList::AT_NoSanitize:
5184 handleNoSanitizeAttr(S, D, Attr);
5185 break;
5186 case AttributeList::AT_NoSanitizeSpecific:
5187 handleNoSanitizeSpecificAttr(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00005188 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005189 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005190 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00005191 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005192 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005193 handleGuardedByAttr(S, D, Attr);
5194 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005195 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00005196 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005197 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005198 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005199 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005200 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005201 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005202 handleLockReturnedAttr(S, D, Attr);
5203 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005204 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005205 handleLocksExcludedAttr(S, D, Attr);
5206 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005207 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005208 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005209 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005210 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00005211 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005212 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005213 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00005214 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005215 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005216
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005217 // Capability analysis attributes.
5218 case AttributeList::AT_Capability:
5219 case AttributeList::AT_Lockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005220 handleCapabilityAttr(S, D, Attr);
5221 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005222 case AttributeList::AT_RequiresCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005223 handleRequiresCapabilityAttr(S, D, Attr);
5224 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005225
5226 case AttributeList::AT_AssertCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005227 handleAssertCapabilityAttr(S, D, Attr);
5228 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005229 case AttributeList::AT_AcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005230 handleAcquireCapabilityAttr(S, D, Attr);
5231 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005232 case AttributeList::AT_ReleaseCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005233 handleReleaseCapabilityAttr(S, D, Attr);
5234 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005235 case AttributeList::AT_TryAcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005236 handleTryAcquireCapabilityAttr(S, D, Attr);
5237 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005238
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00005239 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00005240 case AttributeList::AT_Consumable:
5241 handleConsumableAttr(S, D, Attr);
5242 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00005243 case AttributeList::AT_ConsumableAutoCast:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005244 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr);
5245 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00005246 case AttributeList::AT_ConsumableSetOnRead:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005247 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr);
5248 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00005249 case AttributeList::AT_CallableWhen:
5250 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00005251 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00005252 case AttributeList::AT_ParamTypestate:
5253 handleParamTypestateAttr(S, D, Attr);
5254 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00005255 case AttributeList::AT_ReturnTypestate:
5256 handleReturnTypestateAttr(S, D, Attr);
5257 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00005258 case AttributeList::AT_SetTypestate:
5259 handleSetTypestateAttr(S, D, Attr);
5260 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00005261 case AttributeList::AT_TestTypestate:
5262 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00005263 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00005264
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00005265 // Type safety attributes.
5266 case AttributeList::AT_ArgumentWithTypeTag:
5267 handleArgumentWithTypeTagAttr(S, D, Attr);
5268 break;
5269 case AttributeList::AT_TypeTagForDatatype:
5270 handleTypeTagForDatatypeAttr(S, D, Attr);
5271 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005272 }
5273}
5274
5275/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5276/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00005277void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00005278 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00005279 bool IncludeCXX11Attributes) {
5280 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00005281 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00005282
Joey Gouly2cd9db12013-12-13 16:15:28 +00005283 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00005284 // GCC accepts
5285 // static int a9 __attribute__((weakref));
5286 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00005287 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00005288 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
5289 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00005290 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00005291 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005292 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00005293
Aaron Ballmanbe243a72014-12-04 22:45:31 +00005294 // FIXME: We should be able to handle this in TableGen as well. It would be
5295 // good to have a way to specify "these attributes must appear as a group",
5296 // for these. Additionally, it would be good to have a way to specify "these
5297 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00005298 if (!D->hasAttr<OpenCLKernelAttr>()) {
5299 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00005300 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00005301 // FIXME: This emits a different error message than
5302 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00005303 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005304 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00005305 } else if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00005306 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005307 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00005308 } else if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00005309 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005310 D->setInvalidDecl();
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00005311 } else if (Attr *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
5312 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
5313 << A << ExpectedKernelFunction;
5314 D->setInvalidDecl();
5315 } else if (Attr *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
5316 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
5317 << A << ExpectedKernelFunction;
5318 D->setInvalidDecl();
Joey Gouly2cd9db12013-12-13 16:15:28 +00005319 }
5320 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005321}
5322
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005323// Annotation attributes are the only attributes allowed after an access
5324// specifier.
5325bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5326 const AttributeList *AttrList) {
5327 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005328 if (l->getKind() == AttributeList::AT_Annotate) {
David Majnemer706f3152014-12-14 01:05:01 +00005329 ProcessDeclAttribute(*this, nullptr, ASDecl, *l, l->isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005330 } else {
5331 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5332 return true;
5333 }
5334 }
5335
5336 return false;
5337}
5338
John McCall42856de2011-10-01 05:17:03 +00005339/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5340/// contains any decl attributes that we should warn about.
5341static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5342 for ( ; A; A = A->getNext()) {
5343 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00005344 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00005345 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5346
5347 if (A->getKind() == AttributeList::UnknownAttribute) {
5348 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5349 << A->getName() << A->getRange();
5350 } else {
5351 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5352 << A->getName() << A->getRange();
5353 }
5354 }
5355}
5356
5357/// checkUnusedDeclAttributes - Given a declarator which is not being
5358/// used to build a declaration, complain about any decl attributes
5359/// which might be lying around on it.
5360void Sema::checkUnusedDeclAttributes(Declarator &D) {
5361 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5362 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5363 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5364 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5365}
5366
Ryan Flynn7d470f32009-07-30 03:15:39 +00005367/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00005368/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00005369NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5370 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00005371 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00005372 NamedDecl *NewD = nullptr;
Ryan Flynn7d470f32009-07-30 03:15:39 +00005373 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00005374 FunctionDecl *NewFD;
5375 // FIXME: Missing call to CheckFunctionDeclaration().
5376 // FIXME: Mangling?
5377 // FIXME: Is the qualifier info correct?
5378 // FIXME: Is the DeclContext correct?
5379 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5380 Loc, Loc, DeclarationName(II),
5381 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005382 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00005383 FD->hasPrototype(),
5384 false/*isConstexprSpecified*/);
5385 NewD = NewFD;
5386
5387 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00005388 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00005389
5390 // Fake up parameter variables; they are declared as if this were
5391 // a typedef.
5392 QualType FDTy = FD->getType();
5393 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5394 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00005395 for (const auto &AI : FT->param_types()) {
5396 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00005397 Param->setScopeInfo(0, Params.size());
5398 Params.push_back(Param);
5399 }
David Blaikie9c70e042011-09-21 18:16:56 +00005400 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00005401 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005402 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5403 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00005404 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00005405 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005406 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00005407 if (VD->getQualifier()) {
5408 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00005409 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00005410 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005411 }
5412 return NewD;
5413}
5414
James Dennett634962f2012-06-14 21:40:34 +00005415/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00005416/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00005417void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00005418 if (W.getUsed()) return; // only do this once
5419 W.setUsed(true);
5420 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5421 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00005422 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00005423 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
5424 W.getLocation()));
5425 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00005426 WeakTopLevelDecl.push_back(NewD);
5427 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5428 // to insert Decl at TU scope, sorry.
5429 DeclContext *SavedContext = CurContext;
5430 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00005431 NewD->setDeclContext(CurContext);
5432 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00005433 PushOnScopeChains(NewD, S);
5434 CurContext = SavedContext;
5435 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00005436 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005437 }
5438}
5439
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005440void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5441 // It's valid to "forward-declare" #pragma weak, in which case we
5442 // have to do this.
5443 LoadExternalWeakUndeclaredIdentifiers();
5444 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00005445 NamedDecl *ND = nullptr;
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005446 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5447 if (VD->isExternC())
5448 ND = VD;
5449 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5450 if (FD->isExternC())
5451 ND = FD;
5452 if (ND) {
5453 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00005454 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005455 if (I != WeakUndeclaredIdentifiers.end()) {
5456 WeakInfo W = I->second;
5457 DeclApplyPragmaWeak(S, ND, W);
5458 WeakUndeclaredIdentifiers[Id] = W;
5459 }
5460 }
5461 }
5462 }
5463}
5464
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005465/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5466/// it, apply them to D. This is a bit tricky because PD can have attributes
5467/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00005468void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005469 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00005470 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00005471 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005472
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005473 // Walk the declarator structure, applying decl attributes that were in a type
5474 // position to the decl itself. This handles cases like:
5475 // int *__attr__(x)** D;
5476 // when X is a decl attribute.
5477 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5478 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00005479 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005480
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005481 // Finally, apply any attributes on the decl itself.
5482 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00005483 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005484}
John McCall28a6aea2009-11-04 02:18:39 +00005485
John McCall31168b02011-06-15 23:02:42 +00005486/// Is the given declaration allowed to use a forbidden type?
John McCallb61e14e2015-10-27 04:54:50 +00005487/// If so, it'll still be annotated with an attribute that makes it
5488/// illegal to actually use.
5489static bool isForbiddenTypeAllowed(Sema &S, Decl *decl,
5490 const DelayedDiagnostic &diag,
John McCallc6af8c62015-10-28 05:03:19 +00005491 UnavailableAttr::ImplicitReason &reason) {
John McCall31168b02011-06-15 23:02:42 +00005492 // Private ivars are always okay. Unfortunately, people don't
5493 // always properly make their ivars private, even in system headers.
5494 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005495 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5496 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005497 return false;
5498
John McCallc6af8c62015-10-28 05:03:19 +00005499 // Silently accept unsupported uses of __weak in both user and system
5500 // declarations when it's been disabled, for ease of integration with
5501 // -fno-objc-arc files. We do have to take some care against attempts
5502 // to define such things; for now, we've only done that for ivars
5503 // and properties.
5504 if ((isa<ObjCIvarDecl>(decl) || isa<ObjCPropertyDecl>(decl))) {
5505 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
5506 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
5507 reason = UnavailableAttr::IR_ForbiddenWeak;
5508 return true;
5509 }
John McCallb61e14e2015-10-27 04:54:50 +00005510 }
5511
John McCallc6af8c62015-10-28 05:03:19 +00005512 // Allow all sorts of things in system headers.
5513 if (S.Context.getSourceManager().isInSystemHeader(decl->getLocation())) {
5514 // Currently, all the failures dealt with this way are due to ARC
5515 // restrictions.
5516 reason = UnavailableAttr::IR_ARCForbiddenType;
5517 return true;
John McCallb61e14e2015-10-27 04:54:50 +00005518 }
5519
5520 return false;
John McCall31168b02011-06-15 23:02:42 +00005521}
5522
5523/// Handle a delayed forbidden-type diagnostic.
5524static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5525 Decl *decl) {
John McCallc6af8c62015-10-28 05:03:19 +00005526 auto reason = UnavailableAttr::IR_None;
5527 if (decl && isForbiddenTypeAllowed(S, decl, diag, reason)) {
5528 assert(reason && "didn't set reason?");
5529 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", reason,
5530 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00005531 return;
5532 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005533 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005534 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005535 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005536 // kind of forbidden type messages on unavailable functions.
5537 if (FD->hasAttr<UnavailableAttr>() &&
5538 diag.getForbiddenTypeDiagnostic() ==
5539 diag::err_arc_array_param_no_ownership) {
5540 diag.Triggered = true;
5541 return;
5542 }
5543 }
John McCall31168b02011-06-15 23:02:42 +00005544
5545 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5546 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5547 diag.Triggered = true;
5548}
5549
Aaron Ballmanfb237522014-10-15 15:37:51 +00005550
5551static bool isDeclDeprecated(Decl *D) {
5552 do {
5553 if (D->isDeprecated())
5554 return true;
5555 // A category implicitly has the availability of the interface.
5556 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005557 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
5558 return Interface->isDeprecated();
Aaron Ballmanfb237522014-10-15 15:37:51 +00005559 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5560 return false;
5561}
5562
5563static bool isDeclUnavailable(Decl *D) {
5564 do {
5565 if (D->isUnavailable())
5566 return true;
5567 // A category implicitly has the availability of the interface.
5568 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005569 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
5570 return Interface->isUnavailable();
Aaron Ballmanfb237522014-10-15 15:37:51 +00005571 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5572 return false;
5573}
5574
Nico Weber0055a192015-03-19 19:18:22 +00005575static void DoEmitAvailabilityWarning(Sema &S, Sema::AvailabilityDiagnostic K,
Aaron Ballmanfb237522014-10-15 15:37:51 +00005576 Decl *Ctx, const NamedDecl *D,
5577 StringRef Message, SourceLocation Loc,
5578 const ObjCInterfaceDecl *UnknownObjCClass,
5579 const ObjCPropertyDecl *ObjCProperty,
5580 bool ObjCPropertyAccess) {
5581 // Diagnostics for deprecated or unavailable.
5582 unsigned diag, diag_message, diag_fwdclass_message;
John McCallb61e14e2015-10-27 04:54:50 +00005583 unsigned diag_available_here = diag::note_availability_specified_here;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005584
5585 // Matches 'diag::note_property_attribute' options.
5586 unsigned property_note_select;
5587
5588 // Matches diag::note_availability_specified_here.
5589 unsigned available_here_select_kind;
5590
5591 // Don't warn if our current context is deprecated or unavailable.
5592 switch (K) {
Nico Weber0055a192015-03-19 19:18:22 +00005593 case Sema::AD_Deprecation:
Jordan Rosed17c03e2015-04-30 17:20:35 +00005594 if (isDeclDeprecated(Ctx) || isDeclUnavailable(Ctx))
Aaron Ballmanfb237522014-10-15 15:37:51 +00005595 return;
5596 diag = !ObjCPropertyAccess ? diag::warn_deprecated
5597 : diag::warn_property_method_deprecated;
5598 diag_message = diag::warn_deprecated_message;
5599 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
5600 property_note_select = /* deprecated */ 0;
5601 available_here_select_kind = /* deprecated */ 2;
5602 break;
5603
Nico Weber0055a192015-03-19 19:18:22 +00005604 case Sema::AD_Unavailable:
Aaron Ballmanfb237522014-10-15 15:37:51 +00005605 if (isDeclUnavailable(Ctx))
5606 return;
5607 diag = !ObjCPropertyAccess ? diag::err_unavailable
5608 : diag::err_property_method_unavailable;
5609 diag_message = diag::err_unavailable_message;
5610 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
5611 property_note_select = /* unavailable */ 1;
5612 available_here_select_kind = /* unavailable */ 0;
John McCallb61e14e2015-10-27 04:54:50 +00005613
John McCallc6af8c62015-10-28 05:03:19 +00005614 if (auto attr = D->getAttr<UnavailableAttr>()) {
5615 if (attr->isImplicit() && attr->getImplicitReason()) {
5616 // Most of these failures are due to extra restrictions in ARC;
5617 // reflect that in the primary diagnostic when applicable.
5618 auto flagARCError = [&] {
5619 if (S.getLangOpts().ObjCAutoRefCount &&
5620 S.getSourceManager().isInSystemHeader(D->getLocation()))
5621 diag = diag::err_unavailable_in_arc;
5622 };
5623
5624 switch (attr->getImplicitReason()) {
5625 case UnavailableAttr::IR_None: break;
5626
5627 case UnavailableAttr::IR_ARCForbiddenType:
5628 flagARCError();
5629 diag_available_here = diag::note_arc_forbidden_type;
5630 break;
5631
5632 case UnavailableAttr::IR_ForbiddenWeak:
5633 if (S.getLangOpts().ObjCWeakRuntime)
5634 diag_available_here = diag::note_arc_weak_disabled;
5635 else
5636 diag_available_here = diag::note_arc_weak_no_runtime;
5637 break;
5638
5639 case UnavailableAttr::IR_ARCForbiddenConversion:
5640 flagARCError();
5641 diag_available_here = diag::note_performs_forbidden_arc_conversion;
5642 break;
5643
5644 case UnavailableAttr::IR_ARCInitReturnsUnrelated:
5645 flagARCError();
5646 diag_available_here = diag::note_arc_init_returns_unrelated;
5647 break;
5648
5649 case UnavailableAttr::IR_ARCFieldWithOwnership:
5650 flagARCError();
5651 diag_available_here = diag::note_arc_field_with_ownership;
5652 break;
5653 }
5654 }
John McCallb61e14e2015-10-27 04:54:50 +00005655 }
5656
Aaron Ballmanfb237522014-10-15 15:37:51 +00005657 break;
5658
Nico Weber0055a192015-03-19 19:18:22 +00005659 case Sema::AD_Partial:
5660 diag = diag::warn_partial_availability;
5661 diag_message = diag::warn_partial_message;
5662 diag_fwdclass_message = diag::warn_partial_fwdclass_message;
5663 property_note_select = /* partial */ 2;
5664 available_here_select_kind = /* partial */ 3;
5665 break;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005666 }
5667
Aaron Ballmanfb237522014-10-15 15:37:51 +00005668 if (!Message.empty()) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005669 S.Diag(Loc, diag_message) << D << Message;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005670 if (ObjCProperty)
5671 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5672 << ObjCProperty->getDeclName() << property_note_select;
5673 } else if (!UnknownObjCClass) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005674 S.Diag(Loc, diag) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005675 if (ObjCProperty)
5676 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5677 << ObjCProperty->getDeclName() << property_note_select;
5678 } else {
Aaron Ballman43f40102014-11-14 22:34:56 +00005679 S.Diag(Loc, diag_fwdclass_message) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005680 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5681 }
5682
John McCallb61e14e2015-10-27 04:54:50 +00005683 S.Diag(D->getLocation(), diag_available_here)
Aaron Ballmanfb237522014-10-15 15:37:51 +00005684 << D << available_here_select_kind;
Nico Weber0055a192015-03-19 19:18:22 +00005685 if (K == Sema::AD_Partial)
5686 S.Diag(Loc, diag::note_partial_availability_silence) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005687}
5688
5689static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
5690 Decl *Ctx) {
Nico Weber0055a192015-03-19 19:18:22 +00005691 assert(DD.Kind == DelayedDiagnostic::Deprecation ||
5692 DD.Kind == DelayedDiagnostic::Unavailable);
5693 Sema::AvailabilityDiagnostic AD = DD.Kind == DelayedDiagnostic::Deprecation
5694 ? Sema::AD_Deprecation
5695 : Sema::AD_Unavailable;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005696 DD.Triggered = true;
Nico Weber0055a192015-03-19 19:18:22 +00005697 DoEmitAvailabilityWarning(
5698 S, AD, Ctx, DD.getDeprecationDecl(), DD.getDeprecationMessage(), DD.Loc,
5699 DD.getUnknownObjCClass(), DD.getObjCProperty(), false);
Aaron Ballmanfb237522014-10-15 15:37:51 +00005700}
5701
John McCall2ec85372012-05-07 06:16:41 +00005702void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5703 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005704 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005705 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005706
John McCall2ec85372012-05-07 06:16:41 +00005707 // When delaying diagnostics to run in the context of a parsed
5708 // declaration, we only want to actually emit anything if parsing
5709 // succeeds.
5710 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005711
John McCall2ec85372012-05-07 06:16:41 +00005712 // We emit all the active diagnostics in this pool or any of its
5713 // parents. In general, we'll get one pool for the decl spec
5714 // and a child pool for each declarator; in a decl group like:
5715 // deprecated_typedef foo, *bar, baz();
5716 // only the declarator pops will be passed decls. This is correct;
5717 // we really do need to consider delayed diagnostics from the decl spec
5718 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00005719 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00005720 do {
John McCall6347b682012-05-07 06:16:58 +00005721 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00005722 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5723 // This const_cast is a bit lame. Really, Triggered should be mutable.
5724 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00005725 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00005726 continue;
5727
John McCallc1465822011-02-14 07:13:47 +00005728 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00005729 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00005730 case DelayedDiagnostic::Unavailable:
5731 // Don't bother giving deprecation/unavailable diagnostics if
5732 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00005733 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00005734 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00005735 break;
5736
5737 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00005738 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005739 break;
John McCall31168b02011-06-15 23:02:42 +00005740
5741 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00005742 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00005743 break;
John McCall86121512010-01-27 03:50:35 +00005744 }
5745 }
John McCall2ec85372012-05-07 06:16:41 +00005746 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00005747}
5748
John McCall6347b682012-05-07 06:16:58 +00005749/// Given a set of delayed diagnostics, re-emit them as if they had
5750/// been delayed in the current context instead of in the given pool.
5751/// Essentially, this just moves them to the current pool.
5752void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5753 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5754 assert(curPool && "re-emitting in undelayed context not supported");
5755 curPool->steal(pool);
5756}
5757
Ted Kremenekb79ee572013-12-18 23:30:06 +00005758void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
5759 NamedDecl *D, StringRef Message,
5760 SourceLocation Loc,
5761 const ObjCInterfaceDecl *UnknownObjCClass,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00005762 const ObjCPropertyDecl *ObjCProperty,
5763 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00005764 // Delay if we're currently parsing a declaration.
Nico Weber0055a192015-03-19 19:18:22 +00005765 if (DelayedDiagnostics.shouldDelayDiagnostics() && AD != AD_Partial) {
Nico Weber462fd1e2015-01-07 23:50:05 +00005766 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(
5767 AD, Loc, D, UnknownObjCClass, ObjCProperty, Message,
5768 ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00005769 return;
5770 }
5771
Ted Kremenekb79ee572013-12-18 23:30:06 +00005772 Decl *Ctx = cast<Decl>(getCurLexicalContext());
Nico Weber0055a192015-03-19 19:18:22 +00005773 DoEmitAvailabilityWarning(*this, AD, Ctx, D, Message, Loc, UnknownObjCClass,
5774 ObjCProperty, ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00005775}