blob: 13a11bcac9ebe77d0fedfe4b6d4a28b28f29a9c8 [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, "\"")
Craig Topper07fa1762015-11-15 02:31:46 +0000319 << FixItHint::CreateInsertion(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
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00001586static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1587 if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, Attr.getRange(),
1588 Attr.getName()))
1589 return;
1590
1591 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context,
1592 Attr.getAttributeSpellingListIndex()));
1593}
1594
Chandler Carruthedc2c642011-07-02 00:01:44 +00001595static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001596 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001597
1598 if (S.CheckNoReturnAttr(attr)) return;
1599
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001600 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001601 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001602 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001603 return;
1604 }
1605
Michael Han99315932013-01-24 16:46:58 +00001606 D->addAttr(::new (S.Context)
1607 NoReturnAttr(attr.getRange(), S.Context,
1608 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001609}
1610
1611bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001612 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001613 attr.setInvalid();
1614 return true;
1615 }
1616
1617 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001618}
1619
Chandler Carruthedc2c642011-07-02 00:01:44 +00001620static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1621 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001622
1623 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1624 // because 'analyzer_noreturn' does not impact the type.
David Majnemer06864812015-04-07 06:01:53 +00001625 if (!isFunctionOrMethodOrBlock(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001626 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00001627 if (!VD || (!VD->getType()->isBlockPointerType() &&
1628 !VD->getType()->isFunctionPointerType())) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001629 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001630 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Craig Topper8f7f3ea2015-11-17 05:40:05 +00001631 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001632 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001633 return;
1634 }
1635 }
1636
Michael Han99315932013-01-24 16:46:58 +00001637 D->addAttr(::new (S.Context)
1638 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1639 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001640}
1641
John Thompsoncdb847ba2010-08-09 21:53:52 +00001642// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001643static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001644/*
1645 Returning a Vector Class in Registers
1646
Eric Christopherbc638a82010-12-01 22:13:54 +00001647 According to the PPU ABI specifications, a class with a single member of
1648 vector type is returned in memory when used as the return value of a function.
1649 This results in inefficient code when implementing vector classes. To return
1650 the value in a single vector register, add the vecreturn attribute to the
1651 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001652
1653 Example:
1654
1655 struct Vector
1656 {
1657 __vector float xyzw;
1658 } __attribute__((vecreturn));
1659
1660 Vector Add(Vector lhs, Vector rhs)
1661 {
1662 Vector result;
1663 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1664 return result; // This will be returned in a register
1665 }
1666*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001667 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1668 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001669 return;
1670 }
1671
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001672 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001673 int count = 0;
1674
1675 if (!isa<CXXRecordDecl>(record)) {
1676 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1677 return;
1678 }
1679
1680 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1681 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1682 return;
1683 }
1684
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001685 for (const auto *I : record->fields()) {
1686 if ((count == 1) || !I->getType()->isVectorType()) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001687 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1688 return;
1689 }
1690 count++;
1691 }
1692
Michael Han99315932013-01-24 16:46:58 +00001693 D->addAttr(::new (S.Context)
1694 VecReturnAttr(Attr.getRange(), S.Context,
1695 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001696}
1697
Richard Smithe233fbf2013-01-28 22:42:45 +00001698static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1699 const AttributeList &Attr) {
1700 if (isa<ParmVarDecl>(D)) {
1701 // [[carries_dependency]] can only be applied to a parameter if it is a
1702 // parameter of a function declaration or lambda.
1703 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1704 S.Diag(Attr.getLoc(),
1705 diag::err_carries_dependency_param_not_function_decl);
1706 return;
1707 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001708 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001709
1710 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1711 Attr.getRange(), S.Context,
1712 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001713}
1714
Akira Hatanakac8667622015-11-06 23:56:15 +00001715static void handleNotTailCalledAttr(Sema &S, Decl *D,
1716 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001717 if (checkAttrMutualExclusion<AlwaysInlineAttr>(S, D, Attr.getRange(),
1718 Attr.getName()))
Akira Hatanakac8667622015-11-06 23:56:15 +00001719 return;
1720
1721 D->addAttr(::new (S.Context) NotTailCalledAttr(
1722 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1723}
1724
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00001725static void handleDisableTailCallsAttr(Sema &S, Decl *D,
1726 const AttributeList &Attr) {
1727 if (checkAttrMutualExclusion<NakedAttr>(S, D, Attr.getRange(),
1728 Attr.getName()))
1729 return;
1730
1731 D->addAttr(::new (S.Context) DisableTailCallsAttr(
1732 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1733}
1734
Chandler Carruthedc2c642011-07-02 00:01:44 +00001735static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001736 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001737 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001738 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001739 return;
1740 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001741 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001742 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001743 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001744 return;
1745 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001746
Michael Han99315932013-01-24 16:46:58 +00001747 D->addAttr(::new (S.Context)
1748 UsedAttr(Attr.getRange(), S.Context,
1749 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001750}
1751
Chandler Carruthedc2c642011-07-02 00:01:44 +00001752static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001753 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001754 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001755 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1756 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001757
Michael Han99315932013-01-24 16:46:58 +00001758 D->addAttr(::new (S.Context)
1759 ConstructorAttr(Attr.getRange(), S.Context, priority,
1760 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001761}
1762
Chandler Carruthedc2c642011-07-02 00:01:44 +00001763static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00001764 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001765 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001766 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1767 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001768
Michael Han99315932013-01-24 16:46:58 +00001769 D->addAttr(::new (S.Context)
1770 DestructorAttr(Attr.getRange(), S.Context, priority,
1771 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001772}
1773
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001774template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001775static void handleAttrWithMessage(Sema &S, Decl *D,
1776 const AttributeList &Attr) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001777 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001778 StringRef Str;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001779 if (Attr.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001780 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001781
Michael Han99315932013-01-24 16:46:58 +00001782 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1783 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001784}
1785
Ted Kremenek438f8db2014-02-22 01:06:05 +00001786static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Ted Kremenek28eace62013-11-23 01:01:34 +00001787 const AttributeList &Attr) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00001788 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Ted Kremenek27cfe102014-02-21 22:49:04 +00001789 S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition)
1790 << Attr.getName() << Attr.getRange();
1791 return;
1792 }
1793
Ted Kremenek28eace62013-11-23 01:01:34 +00001794 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001795 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1796 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001797}
1798
Jordy Rose740b0c22012-05-08 03:27:22 +00001799static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1800 IdentifierInfo *Platform,
1801 VersionTuple Introduced,
1802 VersionTuple Deprecated,
1803 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001804 StringRef PlatformName
1805 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1806 if (PlatformName.empty())
1807 PlatformName = Platform->getName();
1808
1809 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1810 // of these steps are needed).
1811 if (!Introduced.empty() && !Deprecated.empty() &&
1812 !(Introduced <= Deprecated)) {
1813 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1814 << 1 << PlatformName << Deprecated.getAsString()
1815 << 0 << Introduced.getAsString();
1816 return true;
1817 }
1818
1819 if (!Introduced.empty() && !Obsoleted.empty() &&
1820 !(Introduced <= Obsoleted)) {
1821 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1822 << 2 << PlatformName << Obsoleted.getAsString()
1823 << 0 << Introduced.getAsString();
1824 return true;
1825 }
1826
1827 if (!Deprecated.empty() && !Obsoleted.empty() &&
1828 !(Deprecated <= Obsoleted)) {
1829 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1830 << 2 << PlatformName << Obsoleted.getAsString()
1831 << 1 << Deprecated.getAsString();
1832 return true;
1833 }
1834
1835 return false;
1836}
1837
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001838/// \brief Check whether the two versions match.
1839///
1840/// If either version tuple is empty, then they are assumed to match. If
1841/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1842static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1843 bool BeforeIsOkay) {
1844 if (X.empty() || Y.empty())
1845 return true;
1846
1847 if (X == Y)
1848 return true;
1849
1850 if (BeforeIsOkay && X < Y)
1851 return true;
1852
1853 return false;
1854}
1855
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001856AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001857 IdentifierInfo *Platform,
1858 VersionTuple Introduced,
1859 VersionTuple Deprecated,
1860 VersionTuple Obsoleted,
1861 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001862 StringRef Message,
Douglas Gregord2a713e2015-09-30 21:27:42 +00001863 AvailabilityMergeKind AMK,
Michael Han99315932013-01-24 16:46:58 +00001864 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001865 VersionTuple MergedIntroduced = Introduced;
1866 VersionTuple MergedDeprecated = Deprecated;
1867 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001868 bool FoundAny = false;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001869 bool OverrideOrImpl = false;
1870 switch (AMK) {
1871 case AMK_None:
1872 case AMK_Redeclaration:
1873 OverrideOrImpl = false;
1874 break;
1875
1876 case AMK_Override:
1877 case AMK_ProtocolImplementation:
1878 OverrideOrImpl = true;
1879 break;
1880 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001881
Rafael Espindolac67f2232012-05-10 02:50:16 +00001882 if (D->hasAttrs()) {
1883 AttrVec &Attrs = D->getAttrs();
1884 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1885 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1886 if (!OldAA) {
1887 ++i;
1888 continue;
1889 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001890
Rafael Espindolac67f2232012-05-10 02:50:16 +00001891 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1892 if (OldPlatform != Platform) {
1893 ++i;
1894 continue;
1895 }
1896
Tim Northover7a73cc72015-10-30 16:30:49 +00001897 // If there is an existing availability attribute for this platform that
1898 // is explicit and the new one is implicit use the explicit one and
1899 // discard the new implicit attribute.
1900 if (OldAA->getRange().isValid() && Range.isInvalid()) {
1901 return nullptr;
1902 }
1903
1904 // If there is an existing attribute for this platform that is implicit
1905 // and the new attribute is explicit then erase the old one and
1906 // continue processing the attributes.
1907 if (Range.isValid() && OldAA->getRange().isInvalid()) {
1908 Attrs.erase(Attrs.begin() + i);
1909 --e;
1910 continue;
1911 }
1912
Rafael Espindolac67f2232012-05-10 02:50:16 +00001913 FoundAny = true;
1914 VersionTuple OldIntroduced = OldAA->getIntroduced();
1915 VersionTuple OldDeprecated = OldAA->getDeprecated();
1916 VersionTuple OldObsoleted = OldAA->getObsoleted();
1917 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001918
Douglas Gregord2a713e2015-09-30 21:27:42 +00001919 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
1920 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
1921 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001922 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregord2a713e2015-09-30 21:27:42 +00001923 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
1924 if (OverrideOrImpl) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001925 int Which = -1;
1926 VersionTuple FirstVersion;
1927 VersionTuple SecondVersion;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001928 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001929 Which = 0;
1930 FirstVersion = OldIntroduced;
1931 SecondVersion = Introduced;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001932 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001933 Which = 1;
1934 FirstVersion = Deprecated;
1935 SecondVersion = OldDeprecated;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001936 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001937 Which = 2;
1938 FirstVersion = Obsoleted;
1939 SecondVersion = OldObsoleted;
1940 }
1941
1942 if (Which == -1) {
1943 Diag(OldAA->getLocation(),
1944 diag::warn_mismatched_availability_override_unavail)
Douglas Gregord2a713e2015-09-30 21:27:42 +00001945 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1946 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001947 } else {
1948 Diag(OldAA->getLocation(),
1949 diag::warn_mismatched_availability_override)
1950 << Which
1951 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
Douglas Gregord2a713e2015-09-30 21:27:42 +00001952 << FirstVersion.getAsString() << SecondVersion.getAsString()
1953 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001954 }
Douglas Gregord2a713e2015-09-30 21:27:42 +00001955 if (AMK == AMK_Override)
1956 Diag(Range.getBegin(), diag::note_overridden_method);
1957 else
1958 Diag(Range.getBegin(), diag::note_protocol_method);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001959 } else {
1960 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1961 Diag(Range.getBegin(), diag::note_previous_attribute);
1962 }
1963
Rafael Espindolac67f2232012-05-10 02:50:16 +00001964 Attrs.erase(Attrs.begin() + i);
1965 --e;
1966 continue;
1967 }
1968
1969 VersionTuple MergedIntroduced2 = MergedIntroduced;
1970 VersionTuple MergedDeprecated2 = MergedDeprecated;
1971 VersionTuple MergedObsoleted2 = MergedObsoleted;
1972
1973 if (MergedIntroduced2.empty())
1974 MergedIntroduced2 = OldIntroduced;
1975 if (MergedDeprecated2.empty())
1976 MergedDeprecated2 = OldDeprecated;
1977 if (MergedObsoleted2.empty())
1978 MergedObsoleted2 = OldObsoleted;
1979
1980 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1981 MergedIntroduced2, MergedDeprecated2,
1982 MergedObsoleted2)) {
1983 Attrs.erase(Attrs.begin() + i);
1984 --e;
1985 continue;
1986 }
1987
1988 MergedIntroduced = MergedIntroduced2;
1989 MergedDeprecated = MergedDeprecated2;
1990 MergedObsoleted = MergedObsoleted2;
1991 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001992 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001993 }
1994
1995 if (FoundAny &&
1996 MergedIntroduced == Introduced &&
1997 MergedDeprecated == Deprecated &&
1998 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00001999 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002000
Douglas Gregord2a713e2015-09-30 21:27:42 +00002001 // Only create a new attribute if !OverrideOrImpl, but we want to do
Ted Kremenekb5445722013-04-06 00:34:27 +00002002 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002003 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002004 MergedDeprecated, MergedObsoleted) &&
Douglas Gregord2a713e2015-09-30 21:27:42 +00002005 !OverrideOrImpl) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002006 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2007 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002008 Obsoleted, IsUnavailable, Message,
2009 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002010 }
Craig Topperc3ec1492014-05-26 06:22:03 +00002011 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002012}
2013
Chandler Carruthedc2c642011-07-02 00:01:44 +00002014static void handleAvailabilityAttr(Sema &S, Decl *D,
2015 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002016 if (!checkAttributeNumArgs(S, Attr, 1))
2017 return;
2018 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00002019 unsigned Index = Attr.getAttributeSpellingListIndex();
2020
Aaron Ballman00e99962013-08-31 01:11:41 +00002021 IdentifierInfo *II = Platform->Ident;
2022 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2023 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2024 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002025
Rafael Espindolac231fab2013-01-08 21:30:32 +00002026 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2027 if (!ND) {
2028 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2029 return;
2030 }
2031
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002032 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2033 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2034 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002035 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002036 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00002037 if (const StringLiteral *SE =
2038 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002039 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002040
Aaron Ballman00e99962013-08-31 01:11:41 +00002041 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002042 Introduced.Version,
2043 Deprecated.Version,
2044 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002045 IsUnavailable, Str,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002046 Sema::AMK_None,
Michael Han99315932013-01-24 16:46:58 +00002047 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002048 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002049 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002050
2051 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2052 // matches before the start of the watchOS platform.
2053 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2054 IdentifierInfo *NewII = nullptr;
2055 if (II->getName() == "ios")
2056 NewII = &S.Context.Idents.get("watchos");
2057 else if (II->getName() == "ios_app_extension")
2058 NewII = &S.Context.Idents.get("watchos_app_extension");
2059
2060 if (NewII) {
2061 auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple {
2062 if (Version.empty())
2063 return Version;
2064 auto Major = Version.getMajor();
2065 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2066 if (NewMajor >= 2) {
2067 if (Version.getMinor().hasValue()) {
2068 if (Version.getSubminor().hasValue())
2069 return VersionTuple(NewMajor, Version.getMinor().getValue(),
2070 Version.getSubminor().getValue());
2071 else
2072 return VersionTuple(NewMajor, Version.getMinor().getValue());
2073 }
2074 }
2075
2076 return VersionTuple(2, 0);
2077 };
2078
2079 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2080 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2081 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2082
2083 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
2084 SourceRange(),
2085 NewII,
2086 NewIntroduced,
2087 NewDeprecated,
2088 NewObsoleted,
2089 IsUnavailable, Str,
2090 Sema::AMK_None,
2091 Index);
2092 if (NewAttr)
2093 D->addAttr(NewAttr);
2094 }
2095 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2096 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2097 // matches before the start of the tvOS platform.
2098 IdentifierInfo *NewII = nullptr;
2099 if (II->getName() == "ios")
2100 NewII = &S.Context.Idents.get("tvos");
2101 else if (II->getName() == "ios_app_extension")
2102 NewII = &S.Context.Idents.get("tvos_app_extension");
2103
2104 if (NewII) {
2105 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
2106 SourceRange(),
2107 NewII,
2108 Introduced.Version,
2109 Deprecated.Version,
2110 Obsoleted.Version,
2111 IsUnavailable, Str,
2112 Sema::AMK_None,
2113 Index);
2114 if (NewAttr)
2115 D->addAttr(NewAttr);
2116 }
2117 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002118}
2119
John McCalld041a9b2013-02-20 01:54:26 +00002120template <class T>
2121static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2122 typename T::VisibilityType value,
2123 unsigned attrSpellingListIndex) {
2124 T *existingAttr = D->getAttr<T>();
2125 if (existingAttr) {
2126 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2127 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00002128 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00002129 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2130 S.Diag(range.getBegin(), diag::note_previous_attribute);
2131 D->dropAttr<T>();
2132 }
2133 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2134}
2135
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002136VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002137 VisibilityAttr::VisibilityType Vis,
2138 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002139 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2140 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002141}
2142
John McCalld041a9b2013-02-20 01:54:26 +00002143TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2144 TypeVisibilityAttr::VisibilityType Vis,
2145 unsigned AttrSpellingListIndex) {
2146 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2147 AttrSpellingListIndex);
2148}
2149
2150static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2151 bool isTypeVisibility) {
2152 // Visibility attributes don't mean anything on a typedef.
2153 if (isa<TypedefNameDecl>(D)) {
2154 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2155 << Attr.getName();
2156 return;
2157 }
2158
2159 // 'type_visibility' can only go on a type or namespace.
2160 if (isTypeVisibility &&
2161 !(isa<TagDecl>(D) ||
2162 isa<ObjCInterfaceDecl>(D) ||
2163 isa<NamespaceDecl>(D))) {
2164 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2165 << Attr.getName() << ExpectedTypeOrNamespace;
2166 return;
2167 }
2168
Benjamin Kramer70370212013-09-09 15:08:57 +00002169 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002170 StringRef TypeStr;
2171 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002172 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002173 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002174
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002175 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002176 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002177 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002178 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002179 return;
2180 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002181
2182 // Complain about attempts to use protected visibility on targets
2183 // (like Darwin) that don't support it.
2184 if (type == VisibilityAttr::Protected &&
2185 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2186 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2187 type = VisibilityAttr::Default;
2188 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002189
Michael Han99315932013-01-24 16:46:58 +00002190 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002191 clang::Attr *newAttr;
2192 if (isTypeVisibility) {
2193 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2194 (TypeVisibilityAttr::VisibilityType) type,
2195 Index);
2196 } else {
2197 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2198 }
2199 if (newAttr)
2200 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002201}
2202
Chandler Carruthedc2c642011-07-02 00:01:44 +00002203static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2204 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002205 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002206 if (!Attr.isArgIdent(0)) {
2207 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2208 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002209 return;
2210 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002211
Aaron Ballman682ee422013-09-11 19:47:58 +00002212 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2213 ObjCMethodFamilyAttr::FamilyKind F;
2214 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2215 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2216 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002217 return;
2218 }
2219
Alp Toker314cc812014-01-25 16:55:45 +00002220 if (F == ObjCMethodFamilyAttr::OMF_init &&
2221 !method->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00002222 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
Alp Toker314cc812014-01-25 16:55:45 +00002223 << method->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002224 // Ignore the attribute.
2225 return;
2226 }
2227
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002228 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00002229 S.Context, F,
2230 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002231}
2232
Chandler Carruthedc2c642011-07-02 00:01:44 +00002233static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002234 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002235 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002236 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002237 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2238 return;
2239 }
2240 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002241 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2242 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002243 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002244 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2245 return;
2246 }
2247 }
2248 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002249 // It is okay to include this attribute on properties, e.g.:
2250 //
2251 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2252 //
2253 // In this case it follows tradition and suppresses an error in the above
2254 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002255 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002256 }
Michael Han99315932013-01-24 16:46:58 +00002257 D->addAttr(::new (S.Context)
2258 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2259 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002260}
2261
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002262static void handleObjCIndependentClass(Sema &S, Decl *D, const AttributeList &Attr) {
2263 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
2264 QualType T = TD->getUnderlyingType();
2265 if (!T->isObjCObjectPointerType()) {
2266 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2267 return;
2268 }
2269 } else {
2270 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2271 return;
2272 }
2273 D->addAttr(::new (S.Context)
2274 ObjCIndependentClassAttr(Attr.getRange(), S.Context,
2275 Attr.getAttributeSpellingListIndex()));
2276}
2277
Chandler Carruthedc2c642011-07-02 00:01:44 +00002278static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002279 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002280 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002281 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002282 return;
2283 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002284
Aaron Ballman00e99962013-08-31 01:11:41 +00002285 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002286 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002287 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2288 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2289 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002290 return;
2291 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002292
Michael Han99315932013-01-24 16:46:58 +00002293 D->addAttr(::new (S.Context)
2294 BlocksAttr(Attr.getRange(), S.Context, type,
2295 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002296}
2297
Chandler Carruthedc2c642011-07-02 00:01:44 +00002298static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002299 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002300 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002301 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002302 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002303 if (E->isTypeDependent() || E->isValueDependent() ||
2304 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002305 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002306 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002307 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002308 return;
2309 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002310
John McCallb46f2872011-09-09 07:56:05 +00002311 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002312 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2313 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002314 return;
2315 }
John McCallb46f2872011-09-09 07:56:05 +00002316
2317 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002318 }
2319
Aaron Ballman18a78382013-11-21 00:28:23 +00002320 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002321 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002322 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002323 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002324 if (E->isTypeDependent() || E->isValueDependent() ||
2325 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002326 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002327 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002328 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002329 return;
2330 }
2331 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002332
John McCallb46f2872011-09-09 07:56:05 +00002333 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002334 // FIXME: This error message could be improved, it would be nice
2335 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002336 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2337 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002338 return;
2339 }
2340 }
2341
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002342 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002343 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002344 if (isa<FunctionNoProtoType>(FT)) {
2345 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2346 return;
2347 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002348
Chris Lattner9363e312009-03-17 23:03:47 +00002349 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002350 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002351 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002352 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002353 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002354 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002355 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002356 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002357 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002358 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2359 if (!BD->isVariadic()) {
2360 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2361 return;
2362 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002363 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002364 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002365 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002366 const FunctionType *FT = Ty->isFunctionPointerType()
2367 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002368 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002369 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002370 int m = Ty->isFunctionPointerType() ? 0 : 1;
2371 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002372 return;
2373 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002374 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002375 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002376 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002377 return;
2378 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002379 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002380 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002381 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002382 return;
2383 }
Michael Han99315932013-01-24 16:46:58 +00002384 D->addAttr(::new (S.Context)
2385 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2386 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002387}
2388
Chandler Carruthedc2c642011-07-02 00:01:44 +00002389static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Alp Toker314cc812014-01-25 16:55:45 +00002390 if (D->getFunctionType() &&
2391 D->getFunctionType()->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002392 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2393 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002394 return;
2395 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002396 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002397 if (MD->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002398 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2399 << Attr.getName() << 1;
2400 return;
2401 }
2402
Michael Han99315932013-01-24 16:46:58 +00002403 D->addAttr(::new (S.Context)
2404 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2405 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002406}
2407
Chandler Carruthedc2c642011-07-02 00:01:44 +00002408static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002409 // weak_import only applies to variable & function declarations.
2410 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002411 if (!D->canBeWeakImported(isDef)) {
2412 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002413 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2414 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002415 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002416 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002417 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002418 // Nothing to warn about here.
2419 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002420 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002421 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002422
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002423 return;
2424 }
2425
Michael Han99315932013-01-24 16:46:58 +00002426 D->addAttr(::new (S.Context)
2427 WeakImportAttr(Attr.getRange(), S.Context,
2428 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002429}
2430
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002431// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002432template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002433static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002434 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002435 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002436 for (unsigned i = 0; i < 3; ++i) {
2437 const Expr *E = Attr.getArgAsExpr(i);
2438 if (!checkUInt32Argument(S, Attr, E, WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002439 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002440 if (WGSize[i] == 0) {
2441 S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero)
2442 << Attr.getName() << E->getSourceRange();
2443 return;
2444 }
2445 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002446
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002447 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2448 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2449 Existing->getYDim() == WGSize[1] &&
2450 Existing->getZDim() == WGSize[2]))
2451 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002452
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002453 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2454 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002455 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002456}
2457
Joey Goulyaba589c2013-03-08 09:42:32 +00002458static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002459 if (!Attr.hasParsedType()) {
2460 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2461 << Attr.getName() << 1;
2462 return;
2463 }
2464
Craig Topperc3ec1492014-05-26 06:22:03 +00002465 TypeSourceInfo *ParmTSI = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00002466 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2467 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002468
2469 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2470 (ParmType->isBooleanType() ||
2471 !ParmType->isIntegralType(S.getASTContext()))) {
2472 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2473 << ParmType;
2474 return;
2475 }
2476
Aaron Ballmana9e05402013-12-02 22:16:55 +00002477 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002478 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002479 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2480 return;
2481 }
2482 }
2483
2484 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002485 ParmTSI,
2486 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002487}
2488
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002489SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002490 StringRef Name,
2491 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002492 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2493 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002494 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002495 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2496 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002497 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002498 }
Michael Han99315932013-01-24 16:46:58 +00002499 return ::new (Context) SectionAttr(Range, Context, Name,
2500 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002501}
2502
Reid Kleckner2a133222015-03-04 23:39:17 +00002503bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2504 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2505 if (!Error.empty()) {
2506 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error;
2507 return false;
2508 }
2509 return true;
2510}
2511
Chandler Carruthedc2c642011-07-02 00:01:44 +00002512static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002513 // Make sure that there is a string literal as the sections's single
2514 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002515 StringRef Str;
2516 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002517 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002518 return;
Mike Stump11289f42009-09-09 15:08:12 +00002519
Reid Kleckner2a133222015-03-04 23:39:17 +00002520 if (!S.checkSectionName(LiteralLoc, Str))
2521 return;
2522
Chris Lattner30ba6742009-08-10 19:03:04 +00002523 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002524 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002525 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002526 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002527 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002528 return;
2529 }
Mike Stump11289f42009-09-09 15:08:12 +00002530
Michael Han99315932013-01-24 16:46:58 +00002531 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002532 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002533 if (NewAttr)
2534 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002535}
2536
Eric Christopher789a7ad2015-06-12 01:36:05 +00002537// Check for things we'd like to warn about, no errors or validation for now.
2538// TODO: Validation should use a backend target library that specifies
2539// the allowable subtarget features and cpus. We could use something like a
2540// TargetCodeGenInfo hook here to do validation.
2541void Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
2542 for (auto Str : {"tune=", "fpmath="})
2543 if (AttrStr.find(Str) != StringRef::npos)
2544 Diag(LiteralLoc, diag::warn_unsupported_target_attribute) << Str;
2545}
2546
Eric Christopher11acf732015-06-12 01:35:52 +00002547static void handleTargetAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eric Christopher11acf732015-06-12 01:35:52 +00002548 StringRef Str;
2549 SourceLocation LiteralLoc;
2550 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
2551 return;
Eric Christopher789a7ad2015-06-12 01:36:05 +00002552 S.checkTargetAttr(LiteralLoc, Str);
Eric Christopher11acf732015-06-12 01:35:52 +00002553 unsigned Index = Attr.getAttributeSpellingListIndex();
2554 TargetAttr *NewAttr =
2555 ::new (S.Context) TargetAttr(Attr.getRange(), S.Context, Str, Index);
2556 D->addAttr(NewAttr);
2557}
2558
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002559
Chandler Carruthedc2c642011-07-02 00:01:44 +00002560static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002561 VarDecl *VD = cast<VarDecl>(D);
2562 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002563 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002564 return;
2565 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002566
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002567 Expr *E = Attr.getArgAsExpr(0);
2568 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00002569 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002570 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002571
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002572 // gcc only allows for simple identifiers. Since we support more than gcc, we
2573 // will warn the user.
2574 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2575 if (DRE->hasQualifier())
2576 S.Diag(Loc, diag::warn_cleanup_ext);
2577 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2578 NI = DRE->getNameInfo();
2579 if (!FD) {
2580 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2581 << NI.getName();
2582 return;
2583 }
2584 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2585 if (ULE->hasExplicitTemplateArgs())
2586 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002587 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2588 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002589 if (!FD) {
2590 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2591 << NI.getName();
2592 if (ULE->getType() == S.Context.OverloadTy)
2593 S.NoteAllOverloadCandidates(ULE);
2594 return;
2595 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002596 } else {
2597 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002598 return;
2599 }
2600
Anders Carlssond277d792009-01-31 01:16:18 +00002601 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002602 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2603 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002604 return;
2605 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002606
Anders Carlsson723f55d2009-02-07 23:16:50 +00002607 // We're currently more strict than GCC about what function types we accept.
2608 // If this ever proves to be a problem it should be easy to fix.
2609 QualType Ty = S.Context.getPointerType(VD->getType());
2610 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002611 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2612 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002613 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2614 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002615 return;
2616 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002617
Michael Han99315932013-01-24 16:46:58 +00002618 D->addAttr(::new (S.Context)
2619 CleanupAttr(Attr.getRange(), S.Context, FD,
2620 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002621}
2622
Mike Stumpd3bb5572009-07-24 19:02:52 +00002623/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002624/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002625static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002626 Expr *IdxExpr = Attr.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00002627 uint64_t Idx;
2628 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002629 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002630
Eric Christopherb64963e2015-08-13 21:34:35 +00002631 // Make sure the format string is really a string.
Alp Toker601b22c2014-01-21 23:35:24 +00002632 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002633
Eric Christopherb64963e2015-08-13 21:34:35 +00002634 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
2635 if (NotNSStringTy &&
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002636 !isCFStringType(Ty, S.Context) &&
2637 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002638 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002639 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00002640 << "a string type" << IdxExpr->getSourceRange()
2641 << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002642 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002643 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002644 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002645 if (!isNSStringType(Ty, S.Context) &&
2646 !isCFStringType(Ty, S.Context) &&
2647 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002648 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002649 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00002650 << (NotNSStringTy ? "string type" : "NSString")
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00002651 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002652 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002653 }
2654
Alp Toker601b22c2014-01-21 23:35:24 +00002655 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002656 // because that has corrected for the implicit this parameter, and is zero-
2657 // based. The attribute expects what the user wrote explicitly.
2658 llvm::APSInt Val;
2659 IdxExpr->EvaluateAsInt(Val, S.Context);
2660
Michael Han99315932013-01-24 16:46:58 +00002661 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002662 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002663 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002664}
2665
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002666enum FormatAttrKind {
2667 CFStringFormat,
2668 NSStringFormat,
2669 StrftimeFormat,
2670 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002671 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002672 InvalidFormat
2673};
2674
2675/// getFormatAttrKind - Map from format attribute names to supported format
2676/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002677static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002678 return llvm::StringSwitch<FormatAttrKind>(Format)
2679 // Check for formats that get handled specially.
2680 .Case("NSString", NSStringFormat)
2681 .Case("CFString", CFStringFormat)
2682 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002683
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002684 // Otherwise, check for supported formats.
2685 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2686 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2687 .Case("kprintf", SupportedFormat) // OpenBSD.
Dimitry Andric6b5ed342015-02-19 22:32:33 +00002688 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
Fariborz Jahanianf8dce0f2015-02-21 00:45:58 +00002689 .Case("os_trace", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002690
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002691 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2692 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002693}
2694
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002695/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002696/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002697static void handleInitPriorityAttr(Sema &S, Decl *D,
2698 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002699 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002700 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2701 return;
2702 }
2703
Aaron Ballman4a611152013-11-27 16:34:09 +00002704 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002705 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2706 Attr.setInvalid();
2707 return;
2708 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002709 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002710 if (S.Context.getAsArrayType(T))
2711 T = S.Context.getBaseElementType(T);
2712 if (!T->getAs<RecordType>()) {
2713 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2714 Attr.setInvalid();
2715 return;
2716 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002717
2718 Expr *E = Attr.getArgAsExpr(0);
2719 uint32_t prioritynum;
2720 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002721 Attr.setInvalid();
2722 return;
2723 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002724
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002725 if (prioritynum < 101 || prioritynum > 65535) {
2726 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002727 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002728 Attr.setInvalid();
2729 return;
2730 }
Michael Han99315932013-01-24 16:46:58 +00002731 D->addAttr(::new (S.Context)
2732 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2733 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002734}
2735
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002736FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2737 IdentifierInfo *Format, int FormatIdx,
2738 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002739 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002740 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002741 for (auto *F : D->specific_attrs<FormatAttr>()) {
2742 if (F->getType() == Format &&
2743 F->getFormatIdx() == FormatIdx &&
2744 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002745 // If we don't have a valid location for this attribute, adopt the
2746 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002747 if (F->getLocation().isInvalid())
2748 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00002749 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00002750 }
2751 }
2752
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002753 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2754 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002755}
2756
Mike Stumpd3bb5572009-07-24 19:02:52 +00002757/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002758/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002759static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002760 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002761 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002762 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002763 return;
2764 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002765
Chandler Carruth743682b2010-11-16 08:35:43 +00002766 // In C++ the implicit 'this' function parameter also counts, and they are
2767 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002768 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00002769 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002770
Aaron Ballman00e99962013-08-31 01:11:41 +00002771 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2772 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002773
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00002774 if (normalizeName(Format)) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002775 // If we've modified the string name, we need a new identifier for it.
2776 II = &S.Context.Idents.get(Format);
2777 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002778
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002779 // Check for supported formats.
2780 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002781
2782 if (Kind == IgnoredFormat)
2783 return;
2784
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002785 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002786 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002787 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002788 return;
2789 }
2790
2791 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002792 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002793 uint32_t Idx;
2794 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002795 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002796
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002797 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002798 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002799 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002800 return;
2801 }
2802
2803 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002804 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002805
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002806 if (HasImplicitThisParam) {
2807 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002808 S.Diag(Attr.getLoc(),
2809 diag::err_format_attribute_implicit_this_format_string)
2810 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002811 return;
2812 }
2813 ArgIdx--;
2814 }
Mike Stump11289f42009-09-09 15:08:12 +00002815
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002816 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002817 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002818
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002819 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002820 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002821 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002822 << "a CFString" << IdxExpr->getSourceRange()
2823 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00002824 return;
2825 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002826 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002827 // FIXME: do we need to check if the type is NSString*? What are the
2828 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002829 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002830 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002831 << "an NSString" << IdxExpr->getSourceRange()
2832 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002833 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002834 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002835 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002836 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002837 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002838 << "a string type" << IdxExpr->getSourceRange()
2839 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002840 return;
2841 }
2842
2843 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002844 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002845 uint32_t FirstArg;
2846 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002847 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002848
2849 // check if the function is variadic if the 3rd argument non-zero
2850 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002851 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002852 ++NumArgs; // +1 for ...
2853 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002854 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002855 return;
2856 }
2857 }
2858
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002859 // strftime requires FirstArg to be 0 because it doesn't read from any
2860 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002861 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002862 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002863 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2864 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002865 return;
2866 }
2867 // if 0 it disables parameter checking (to use with e.g. va_list)
2868 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002869 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002870 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002871 return;
2872 }
2873
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002874 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002875 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002876 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002877 if (NewAttr)
2878 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002879}
2880
Chandler Carruthedc2c642011-07-02 00:01:44 +00002881static void handleTransparentUnionAttr(Sema &S, Decl *D,
2882 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002883 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00002884 RecordDecl *RD = nullptr;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002885 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002886 if (TD && TD->getUnderlyingType()->isUnionType())
2887 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2888 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002889 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002890
2891 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002892 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002893 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002894 return;
2895 }
2896
John McCallf937c022011-10-07 06:10:15 +00002897 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002898 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002899 diag::warn_transparent_union_attribute_not_definition);
2900 return;
2901 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002902
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002903 RecordDecl::field_iterator Field = RD->field_begin(),
2904 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002905 if (Field == FieldEnd) {
2906 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2907 return;
2908 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002909
David Blaikie40ed2972012-06-06 20:45:41 +00002910 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002911 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002912 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002913 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002914 diag::warn_transparent_union_attribute_floating)
2915 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002916 return;
2917 }
2918
2919 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2920 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2921 for (; Field != FieldEnd; ++Field) {
2922 QualType FieldType = Field->getType();
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002923 // FIXME: this isn't fully correct; we also need to test whether the
2924 // members of the union would all have the same calling convention as the
2925 // first member of the union. Checking just the size and alignment isn't
2926 // sufficient (consider structs passed on the stack instead of in registers
2927 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002928 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002929 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002930 // Warn if we drop the attribute.
2931 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002932 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002933 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002934 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002935 diag::warn_transparent_union_attribute_field_size_align)
2936 << isSize << Field->getDeclName() << FieldBits;
2937 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002938 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002939 diag::note_transparent_union_first_field_size_align)
2940 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002941 return;
2942 }
2943 }
2944
Michael Han99315932013-01-24 16:46:58 +00002945 RD->addAttr(::new (S.Context)
2946 TransparentUnionAttr(Attr.getRange(), S.Context,
2947 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002948}
2949
Chandler Carruthedc2c642011-07-02 00:01:44 +00002950static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002951 // Make sure that there is a string literal as the annotation's single
2952 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002953 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002954 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002955 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002956
2957 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002958 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
2959 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002960 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002961 }
Michael Han99315932013-01-24 16:46:58 +00002962
2963 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002964 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002965 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002966}
2967
Hal Finkel1b0d24e2014-10-02 21:21:25 +00002968static void handleAlignValueAttr(Sema &S, Decl *D,
2969 const AttributeList &Attr) {
2970 S.AddAlignValueAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
2971 Attr.getAttributeSpellingListIndex());
2972}
2973
2974void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
2975 unsigned SpellingListIndex) {
2976 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
2977 SourceLocation AttrLoc = AttrRange.getBegin();
2978
2979 QualType T;
2980 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
2981 T = TD->getUnderlyingType();
2982 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2983 T = VD->getType();
2984 else
2985 llvm_unreachable("Unknown decl type for align_value");
2986
2987 if (!T->isDependentType() && !T->isAnyPointerType() &&
2988 !T->isReferenceType() && !T->isMemberPointerType()) {
2989 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
2990 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
2991 return;
2992 }
2993
2994 if (!E->isValueDependent()) {
David Majnemer0be6bd02015-07-26 09:02:21 +00002995 llvm::APSInt Alignment;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00002996 ExprResult ICE
2997 = VerifyIntegerConstantExpression(E, &Alignment,
2998 diag::err_align_value_attribute_argument_not_int,
2999 /*AllowFold*/ false);
3000 if (ICE.isInvalid())
3001 return;
3002
3003 if (!Alignment.isPowerOf2()) {
3004 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3005 << E->getSourceRange();
3006 return;
3007 }
3008
3009 D->addAttr(::new (Context)
3010 AlignValueAttr(AttrRange, Context, ICE.get(),
3011 SpellingListIndex));
3012 return;
3013 }
3014
3015 // Save dependent expressions in the AST to be instantiated.
3016 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
3017 return;
3018}
3019
Chandler Carruthedc2c642011-07-02 00:01:44 +00003020static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003021 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003022 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003023 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3024 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003025 return;
3026 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003027
Richard Smith848e1f12013-02-01 08:12:08 +00003028 if (Attr.getNumArgs() == 0) {
3029 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
Craig Topperc3ec1492014-05-26 06:22:03 +00003030 true, nullptr, Attr.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00003031 return;
3032 }
3033
Aaron Ballman00e99962013-08-31 01:11:41 +00003034 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00003035 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3036 S.Diag(Attr.getEllipsisLoc(),
3037 diag::err_pack_expansion_without_parameter_packs);
3038 return;
3039 }
3040
3041 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3042 return;
3043
David Majnemer26a1e0e2015-04-07 02:37:09 +00003044 if (E->isValueDependent()) {
3045 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3046 if (!TND->getUnderlyingType()->isDependentType()) {
3047 S.Diag(Attr.getLoc(), diag::err_alignment_dependent_typedef_name)
3048 << E->getSourceRange();
3049 return;
3050 }
3051 }
3052 }
3053
Richard Smith44c247f2013-02-22 08:32:16 +00003054 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3055 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003056}
3057
3058void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003059 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003060 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3061 SourceLocation AttrLoc = AttrRange.getBegin();
3062
Richard Smith1dba27c2013-01-29 09:02:09 +00003063 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003064 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003065 // C++11 [dcl.align]p1:
3066 // An alignment-specifier may be applied to a variable or to a class
3067 // data member, but it shall not be applied to a bit-field, a function
3068 // parameter, the formal parameter of a catch clause, or a variable
3069 // declared with the register storage class specifier. An
3070 // alignment-specifier may also be applied to the declaration of a class
3071 // or enumeration type.
3072 // C11 6.7.5/2:
3073 // An alignment attribute shall not be specified in a declaration of
3074 // a typedef, or a bit-field, or a function, or a parameter, or an
3075 // object declared with the register storage-class specifier.
3076 int DiagKind = -1;
3077 if (isa<ParmVarDecl>(D)) {
3078 DiagKind = 0;
3079 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3080 if (VD->getStorageClass() == SC_Register)
3081 DiagKind = 1;
3082 if (VD->isExceptionVariable())
3083 DiagKind = 2;
3084 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3085 if (FD->isBitField())
3086 DiagKind = 3;
3087 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00003088 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00003089 << (TmpAttr.isC11() ? ExpectedVariableOrField
3090 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003091 return;
3092 }
3093 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003094 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00003095 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003096 return;
3097 }
3098 }
3099
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003100 if (E->isTypeDependent() || E->isValueDependent()) {
3101 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003102 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3103 AA->setPackExpansion(IsPackExpansion);
3104 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003105 return;
3106 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003107
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003108 // FIXME: Cache the number on the Attr object?
David Majnemer0be6bd02015-07-26 09:02:21 +00003109 llvm::APSInt Alignment;
Douglas Gregore2b37442012-05-04 22:38:52 +00003110 ExprResult ICE
3111 = VerifyIntegerConstantExpression(E, &Alignment,
3112 diag::err_aligned_attribute_argument_not_int,
3113 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003114 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003115 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003116
David Majnemer0be6bd02015-07-26 09:02:21 +00003117 uint64_t AlignVal = Alignment.getZExtValue();
3118
Richard Smith848e1f12013-02-01 08:12:08 +00003119 // C++11 [dcl.align]p2:
3120 // -- if the constant expression evaluates to zero, the alignment
3121 // specifier shall have no effect
3122 // C11 6.7.5p6:
3123 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003124 if (!(TmpAttr.isAlignas() && !Alignment)) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003125 if (!llvm::isPowerOf2_64(AlignVal)) {
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003126 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3127 << E->getSourceRange();
3128 return;
3129 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003130 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003131
David Majnemerabecae72014-02-12 20:36:10 +00003132 // Alignment calculations can wrap around if it's greater than 2**28.
David Majnemer29c69db2015-07-26 01:48:59 +00003133 unsigned MaxValidAlignment =
3134 Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192
3135 : 268435456;
David Majnemer0be6bd02015-07-26 09:02:21 +00003136 if (AlignVal > MaxValidAlignment) {
David Majnemerabecae72014-02-12 20:36:10 +00003137 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
3138 << E->getSourceRange();
3139 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003140 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003141
David Majnemer0be6bd02015-07-26 09:02:21 +00003142 if (Context.getTargetInfo().isTLSSupported()) {
3143 unsigned MaxTLSAlign =
3144 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3145 .getQuantity();
3146 auto *VD = dyn_cast<VarDecl>(D);
3147 if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD &&
3148 VD->getTLSKind() != VarDecl::TLS_None) {
3149 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3150 << (unsigned)AlignVal << VD << MaxTLSAlign;
3151 return;
3152 }
3153 }
3154
Richard Smith44c247f2013-02-22 08:32:16 +00003155 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003156 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00003157 AA->setPackExpansion(IsPackExpansion);
3158 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003159}
3160
Michael Hanaf02bbe2013-02-01 01:19:17 +00003161void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003162 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003163 // FIXME: Cache the number on the Attr object if non-dependent?
3164 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003165 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3166 SpellingListIndex);
3167 AA->setPackExpansion(IsPackExpansion);
3168 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003169}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003170
Richard Smith848e1f12013-02-01 08:12:08 +00003171void Sema::CheckAlignasUnderalignment(Decl *D) {
3172 assert(D->hasAttrs() && "no attributes on decl");
3173
David Majnemer475b25e2015-01-21 10:54:38 +00003174 QualType UnderlyingTy, DiagTy;
3175 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
3176 UnderlyingTy = DiagTy = VD->getType();
3177 } else {
3178 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
3179 if (EnumDecl *ED = dyn_cast<EnumDecl>(D))
3180 UnderlyingTy = ED->getIntegerType();
3181 }
3182 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003183 return;
3184
3185 // C++11 [dcl.align]p5, C11 6.7.5/4:
3186 // The combined effect of all alignment attributes in a declaration shall
3187 // not specify an alignment that is less strict than the alignment that
3188 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003189 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003190 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003191 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003192 if (I->isAlignmentDependent())
3193 return;
3194 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003195 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003196 Align = std::max(Align, I->getAlignment(Context));
3197 }
3198
3199 if (AlignasAttr && Align) {
3200 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003201 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003202 if (NaturalAlign > RequestedAlign)
3203 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003204 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003205 }
3206}
3207
David Majnemer2c4e00a2014-01-29 22:07:36 +00003208bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003209 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003210 MSInheritanceAttr::Spelling SemanticSpelling) {
3211 assert(RD->hasDefinition() && "RD has no definition!");
3212
David Majnemer98c9ee22014-02-07 00:43:07 +00003213 // We may not have seen base specifiers or any virtual methods yet. We will
3214 // have to wait until the record is defined to catch any mismatches.
3215 if (!RD->getDefinition()->isCompleteDefinition())
3216 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003217
David Majnemer98c9ee22014-02-07 00:43:07 +00003218 // The unspecified model never matches what a definition could need.
3219 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
3220 return false;
3221
David Majnemer4bb09802014-02-10 19:50:15 +00003222 if (BestCase) {
3223 if (RD->calculateInheritanceModel() == SemanticSpelling)
3224 return false;
3225 } else {
3226 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3227 return false;
3228 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003229
3230 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3231 << 0 /*definition*/;
3232 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3233 << RD->getNameAsString();
3234 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003235}
3236
Alexey Bataevf278eb12015-11-19 10:13:11 +00003237/// parseModeAttrArg - Parses attribute mode string and returns parsed type
3238/// attribute.
3239static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
3240 bool &IntegerMode, bool &ComplexMode) {
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]) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003244 case 'Q':
3245 DestWidth = 8;
3246 break;
3247 case 'H':
3248 DestWidth = 16;
3249 break;
3250 case 'S':
3251 DestWidth = 32;
3252 break;
3253 case 'D':
3254 DestWidth = 64;
3255 break;
3256 case 'X':
3257 DestWidth = 96;
3258 break;
3259 case 'T':
3260 DestWidth = 128;
3261 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003262 }
3263 if (Str[1] == 'F') {
3264 IntegerMode = false;
3265 } else if (Str[1] == 'C') {
3266 IntegerMode = false;
3267 ComplexMode = true;
3268 } else if (Str[1] != 'I') {
3269 DestWidth = 0;
3270 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003271 break;
3272 case 4:
3273 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3274 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003275 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003276 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003277 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003278 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003279 break;
3280 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003281 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003282 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003283 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003284 case 11:
3285 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003286 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003287 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003288 }
Alexey Bataevf278eb12015-11-19 10:13:11 +00003289}
3290
3291/// handleModeAttr - This attribute modifies the width of a decl with primitive
3292/// type.
3293///
3294/// Despite what would be logical, the mode attribute is a decl attribute, not a
3295/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3296/// HImode, not an intermediate pointer.
3297static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3298 // This attribute isn't documented, but glibc uses it. It changes
3299 // the width of an int or unsigned int to the specified size.
3300 if (!Attr.isArgIdent(0)) {
3301 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3302 << AANT_ArgumentIdentifier;
3303 return;
3304 }
3305
3306 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3307 StringRef Str = Name->getName();
3308
3309 normalizeName(Str);
3310
3311 unsigned DestWidth = 0;
3312 bool IntegerMode = true;
3313 bool ComplexMode = false;
3314 llvm::APInt VectorSize(64, 0);
3315 if (Str.size() >= 4 && Str[0] == 'V') {
3316 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
3317 size_t StrSize = Str.size();
3318 size_t VectorStringLength = 0;
3319 while ((VectorStringLength + 1) < StrSize &&
3320 isdigit(Str[VectorStringLength + 1]))
3321 ++VectorStringLength;
3322 if (VectorStringLength &&
3323 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
3324 VectorSize.isPowerOf2()) {
3325 parseModeAttrArg(S, Str.substr(VectorStringLength + 1), DestWidth,
3326 IntegerMode, ComplexMode);
3327 S.Diag(Attr.getLoc(), diag::warn_vector_mode_deprecated);
3328 } else {
3329 VectorSize = 0;
3330 }
3331 }
3332
3333 if (!VectorSize)
3334 parseModeAttrArg(S, Str, DestWidth, IntegerMode, ComplexMode);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003335
3336 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003337 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003338 OldTy = TD->getUnderlyingType();
3339 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3340 OldTy = VD->getType();
3341 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003342 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00003343 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003344 return;
3345 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003346
Alexey Bataev326057d2015-06-19 07:46:21 +00003347 // Base type can also be a vector type (see PR17453).
3348 // Distinguish between base type and base element type.
3349 QualType OldElemTy = OldTy;
3350 if (const VectorType *VT = OldTy->getAs<VectorType>())
3351 OldElemTy = VT->getElementType();
3352
3353 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003354 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3355 else if (IntegerMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003356 if (!OldElemTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003357 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3358 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003359 if (!OldElemTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003360 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3361 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00003362 if (!OldElemTy->isFloatingType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003363 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3364 }
3365
Mike Stump87c57ac2009-05-16 07:39:55 +00003366 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3367 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003368 // FIXME: Make sure floating-point mappings are accurate
3369 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003370 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00003371 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003372 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003373 }
3374
Alexey Bataev326057d2015-06-19 07:46:21 +00003375 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003376
3377 if (IntegerMode)
Alexey Bataev326057d2015-06-19 07:46:21 +00003378 NewElemTy = S.Context.getIntTypeForBitwidth(
3379 DestWidth, OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003380 else
Alexey Bataev326057d2015-06-19 07:46:21 +00003381 NewElemTy = S.Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003382
Alexey Bataev326057d2015-06-19 07:46:21 +00003383 if (NewElemTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00003384 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003385 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003386 }
3387
Eli Friedman4735374e2009-03-03 06:41:03 +00003388 if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003389 NewElemTy = S.Context.getComplexType(NewElemTy);
3390 }
3391
3392 QualType NewTy = NewElemTy;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003393 if (VectorSize.getBoolValue()) {
3394 NewTy = S.Context.getVectorType(NewTy, VectorSize.getZExtValue(),
3395 VectorType::GenericVector);
3396 } else if (const VectorType *OldVT = OldTy->getAs<VectorType>()) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003397 // Complex machine mode does not support base vector types.
3398 if (ComplexMode) {
3399 S.Diag(Attr.getLoc(), diag::err_complex_mode_vector_type);
3400 return;
3401 }
3402 unsigned NumElements = S.Context.getTypeSize(OldElemTy) *
3403 OldVT->getNumElements() /
3404 S.Context.getTypeSize(NewElemTy);
3405 NewTy =
3406 S.Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
3407 }
3408
3409 if (NewTy.isNull()) {
3410 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3411 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003412 }
3413
3414 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003415 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3416 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3417 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003418 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003419
3420 D->addAttr(::new (S.Context)
3421 ModeAttr(Attr.getRange(), S.Context, Name,
3422 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003423}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003424
Chandler Carruthedc2c642011-07-02 00:01:44 +00003425static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003426 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3427 if (!VD->hasGlobalStorage())
3428 S.Diag(Attr.getLoc(),
3429 diag::warn_attribute_requires_functions_or_static_globals)
3430 << Attr.getName();
3431 } else if (!isFunctionOrMethod(D)) {
3432 S.Diag(Attr.getLoc(),
3433 diag::warn_attribute_requires_functions_or_static_globals)
3434 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003435 return;
3436 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003437
Michael Han99315932013-01-24 16:46:58 +00003438 D->addAttr(::new (S.Context)
3439 NoDebugAttr(Attr.getRange(), S.Context,
3440 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003441}
3442
Paul Robinson30e41fb2014-12-15 18:57:28 +00003443AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range,
Paul Robinson080b1f32015-01-13 18:34:56 +00003444 IdentifierInfo *Ident,
Paul Robinson30e41fb2014-12-15 18:57:28 +00003445 unsigned AttrSpellingListIndex) {
3446 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003447 Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00003448 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3449 return nullptr;
3450 }
3451
3452 if (D->hasAttr<AlwaysInlineAttr>())
3453 return nullptr;
3454
3455 return ::new (Context) AlwaysInlineAttr(Range, Context,
3456 AttrSpellingListIndex);
3457}
3458
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003459CommonAttr *Sema::mergeCommonAttr(Decl *D, SourceRange Range,
3460 IdentifierInfo *Ident,
3461 unsigned AttrSpellingListIndex) {
3462 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, Range, Ident))
3463 return nullptr;
3464
3465 return ::new (Context) CommonAttr(Range, Context, AttrSpellingListIndex);
3466}
3467
3468InternalLinkageAttr *
3469Sema::mergeInternalLinkageAttr(Decl *D, SourceRange Range,
3470 IdentifierInfo *Ident,
3471 unsigned AttrSpellingListIndex) {
3472 if (auto VD = dyn_cast<VarDecl>(D)) {
3473 // Attribute applies to Var but not any subclass of it (like ParmVar,
3474 // ImplicitParm or VarTemplateSpecialization).
3475 if (VD->getKind() != Decl::Var) {
3476 Diag(Range.getBegin(), diag::warn_attribute_wrong_decl_type)
3477 << Ident << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
3478 : ExpectedVariableOrFunction);
3479 return nullptr;
3480 }
3481 // Attribute does not apply to non-static local variables.
3482 if (VD->hasLocalStorage()) {
3483 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
3484 return nullptr;
3485 }
3486 }
3487
3488 if (checkAttrMutualExclusion<CommonAttr>(*this, D, Range, Ident))
3489 return nullptr;
3490
3491 return ::new (Context)
3492 InternalLinkageAttr(Range, Context, AttrSpellingListIndex);
3493}
3494
Paul Robinson30e41fb2014-12-15 18:57:28 +00003495MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range,
3496 unsigned AttrSpellingListIndex) {
3497 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
3498 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'";
3499 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3500 return nullptr;
3501 }
3502
3503 if (D->hasAttr<MinSizeAttr>())
3504 return nullptr;
3505
3506 return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex);
3507}
3508
3509OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range,
3510 unsigned AttrSpellingListIndex) {
3511 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
3512 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
3513 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3514 D->dropAttr<AlwaysInlineAttr>();
3515 }
3516 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
3517 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
3518 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3519 D->dropAttr<MinSizeAttr>();
3520 }
3521
3522 if (D->hasAttr<OptimizeNoneAttr>())
3523 return nullptr;
3524
3525 return ::new (Context) OptimizeNoneAttr(Range, Context,
3526 AttrSpellingListIndex);
3527}
3528
Paul Robinsonf0674352014-03-31 22:29:15 +00003529static void handleAlwaysInlineAttr(Sema &S, Decl *D,
3530 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003531 if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, Attr.getRange(),
3532 Attr.getName()))
Akira Hatanakac8667622015-11-06 23:56:15 +00003533 return;
3534
Paul Robinson080b1f32015-01-13 18:34:56 +00003535 if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr(
3536 D, Attr.getRange(), Attr.getName(),
3537 Attr.getAttributeSpellingListIndex()))
3538 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00003539}
3540
Paul Robinson080b1f32015-01-13 18:34:56 +00003541static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3542 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(
3543 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3544 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00003545}
3546
Paul Robinsonf0674352014-03-31 22:29:15 +00003547static void handleOptimizeNoneAttr(Sema &S, Decl *D,
3548 const AttributeList &Attr) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003549 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(
3550 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3551 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00003552}
3553
Chandler Carruthedc2c642011-07-02 00:01:44 +00003554static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003555 FunctionDecl *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00003556 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00003557 SourceRange RTRange = FD->getReturnTypeSourceRange();
3558 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00003559 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00003560 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
3561 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00003562 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003563 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003564
Aaron Ballman3aff6332013-12-02 19:30:36 +00003565 D->addAttr(::new (S.Context)
3566 CUDAGlobalAttr(Attr.getRange(), S.Context,
Eli Bendersky83860042014-09-02 22:00:06 +00003567 Attr.getAttributeSpellingListIndex()));
Artem Belevichc3fa25d2015-09-22 17:22:51 +00003568
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003569}
3570
Chandler Carruthedc2c642011-07-02 00:01:44 +00003571static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003572 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003573 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003574 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003575 return;
3576 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003577
Michael Han99315932013-01-24 16:46:58 +00003578 D->addAttr(::new (S.Context)
3579 GNUInlineAttr(Attr.getRange(), S.Context,
3580 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003581}
3582
Chandler Carruthedc2c642011-07-02 00:01:44 +00003583static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003584 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003585
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003586 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003587 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3588 CallingConv CC;
Richard Smitheec7cb12015-05-28 23:38:53 +00003589 if (S.CheckCallingConvAttr(Attr, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00003590 return;
3591
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003592 if (!isa<ObjCMethodDecl>(D)) {
3593 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3594 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003595 return;
3596 }
3597
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003598 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003599 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003600 D->addAttr(::new (S.Context)
3601 FastCallAttr(Attr.getRange(), S.Context,
3602 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003603 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003604 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003605 D->addAttr(::new (S.Context)
3606 StdCallAttr(Attr.getRange(), S.Context,
3607 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003608 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003609 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003610 D->addAttr(::new (S.Context)
3611 ThisCallAttr(Attr.getRange(), S.Context,
3612 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003613 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003614 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003615 D->addAttr(::new (S.Context)
3616 CDeclAttr(Attr.getRange(), S.Context,
3617 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003618 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003619 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003620 D->addAttr(::new (S.Context)
3621 PascalAttr(Attr.getRange(), S.Context,
3622 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003623 return;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003624 case AttributeList::AT_VectorCall:
3625 D->addAttr(::new (S.Context)
3626 VectorCallAttr(Attr.getRange(), S.Context,
3627 Attr.getAttributeSpellingListIndex()));
3628 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003629 case AttributeList::AT_MSABI:
3630 D->addAttr(::new (S.Context)
3631 MSABIAttr(Attr.getRange(), S.Context,
3632 Attr.getAttributeSpellingListIndex()));
3633 return;
3634 case AttributeList::AT_SysVABI:
3635 D->addAttr(::new (S.Context)
3636 SysVABIAttr(Attr.getRange(), S.Context,
3637 Attr.getAttributeSpellingListIndex()));
3638 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003639 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003640 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003641 switch (CC) {
3642 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003643 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003644 break;
3645 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003646 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003647 break;
3648 default:
3649 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003650 }
3651
Michael Han99315932013-01-24 16:46:58 +00003652 D->addAttr(::new (S.Context)
3653 PcsAttr(Attr.getRange(), S.Context, PCS,
3654 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003655 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003656 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00003657 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003658 D->addAttr(::new (S.Context)
3659 IntelOclBiccAttr(Attr.getRange(), S.Context,
3660 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003661 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003662
Abramo Bagnara50099372010-04-30 13:10:51 +00003663 default:
3664 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003665 }
3666}
3667
Aaron Ballman02df2e02012-12-09 17:45:41 +00003668bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3669 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003670 if (attr.isInvalid())
3671 return true;
3672
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003673 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003674 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003675 attr.setInvalid();
3676 return true;
3677 }
3678
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003679 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003680 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003681 case AttributeList::AT_CDecl: CC = CC_C; break;
3682 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3683 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3684 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3685 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003686 case AttributeList::AT_VectorCall: CC = CC_X86VectorCall; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003687 case AttributeList::AT_MSABI:
3688 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3689 CC_X86_64Win64;
3690 break;
3691 case AttributeList::AT_SysVABI:
3692 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3693 CC_C;
3694 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003695 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003696 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003697 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003698 attr.setInvalid();
3699 return true;
3700 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003701 if (StrRef == "aapcs") {
3702 CC = CC_AAPCS;
3703 break;
3704 } else if (StrRef == "aapcs-vfp") {
3705 CC = CC_AAPCS_VFP;
3706 break;
3707 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003708
3709 attr.setInvalid();
3710 Diag(attr.getLoc(), diag::err_invalid_pcs);
3711 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003712 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00003713 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003714 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003715 }
3716
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003717 const TargetInfo &TI = Context.getTargetInfo();
3718 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
Reid Kleckner9fde2e02015-02-26 19:43:46 +00003719 if (A != TargetInfo::CCCR_OK) {
3720 if (A == TargetInfo::CCCR_Warning)
3721 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003722
Reid Kleckner9fde2e02015-02-26 19:43:46 +00003723 // This convention is not valid for the target. Use the default function or
3724 // method calling convention.
Aaron Ballman02df2e02012-12-09 17:45:41 +00003725 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3726 if (FD)
3727 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3728 TargetInfo::CCMT_NonMember;
3729 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003730 }
3731
John McCall3882ace2011-01-05 12:14:39 +00003732 return false;
3733}
3734
John McCall3882ace2011-01-05 12:14:39 +00003735/// Checks a regparm attribute, returning true if it is ill-formed and
3736/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003737bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3738 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003739 return true;
3740
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003741 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003742 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003743 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003744 }
Eli Friedman7044b762009-03-27 21:06:47 +00003745
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003746 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003747 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003748 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003749 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003750 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003751 }
3752
Douglas Gregore8bbc122011-09-02 00:18:52 +00003753 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003754 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003755 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003756 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003757 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003758 }
3759
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003760 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003761 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003762 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003763 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003764 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003765 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003766 }
3767
John McCall3882ace2011-01-05 12:14:39 +00003768 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003769}
3770
Artem Belevich7093e402015-04-21 22:55:54 +00003771// Checks whether an argument of launch_bounds attribute is acceptable
3772// May output an error.
3773static bool checkLaunchBoundsArgument(Sema &S, Expr *E,
3774 const CUDALaunchBoundsAttr &Attr,
3775 const unsigned Idx) {
3776
3777 if (S.DiagnoseUnexpandedParameterPack(E))
3778 return false;
3779
3780 // Accept template arguments for now as they depend on something else.
3781 // We'll get to check them when they eventually get instantiated.
3782 if (E->isValueDependent())
3783 return true;
3784
3785 llvm::APSInt I(64);
3786 if (!E->isIntegerConstantExpr(I, S.Context)) {
3787 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
3788 << &Attr << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
3789 return false;
3790 }
3791 // Make sure we can fit it in 32 bits.
3792 if (!I.isIntN(32)) {
3793 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
3794 << 32 << /* Unsigned */ 1;
3795 return false;
3796 }
3797 if (I < 0)
3798 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
3799 << &Attr << Idx << E->getSourceRange();
3800
3801 return true;
3802}
3803
3804void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads,
3805 Expr *MinBlocks, unsigned SpellingListIndex) {
3806 CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks,
3807 SpellingListIndex);
3808
3809 if (!checkLaunchBoundsArgument(*this, MaxThreads, TmpAttr, 0))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003810 return;
3811
Artem Belevich7093e402015-04-21 22:55:54 +00003812 if (MinBlocks && !checkLaunchBoundsArgument(*this, MinBlocks, TmpAttr, 1))
3813 return;
3814
3815 D->addAttr(::new (Context) CUDALaunchBoundsAttr(
3816 AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex));
3817}
3818
3819static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3820 const AttributeList &Attr) {
3821 if (!checkAttributeAtLeastNumArgs(S, Attr, 1) ||
3822 !checkAttributeAtMostNumArgs(S, Attr, 2))
3823 return;
3824
3825 S.AddLaunchBoundsAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
3826 Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr,
3827 Attr.getAttributeSpellingListIndex());
Peter Collingbourne827301e2010-12-12 23:03:07 +00003828}
3829
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003830static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3831 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003832 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003833 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003834 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003835 return;
3836 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003837
3838 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003839 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003840
Aaron Ballman00e99962013-08-31 01:11:41 +00003841 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003842
3843 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3844 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3845 << Attr.getName() << ExpectedFunctionOrMethod;
3846 return;
3847 }
3848
3849 uint64_t ArgumentIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003850 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3851 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003852 return;
3853
3854 uint64_t TypeTagIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003855 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3856 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003857 return;
3858
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003859 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003860 if (IsPointer) {
3861 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00003862 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003863 if (!BufferTy->isPointerType()) {
3864 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003865 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003866 }
3867 }
3868
Michael Han99315932013-01-24 16:46:58 +00003869 D->addAttr(::new (S.Context)
3870 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3871 ArgumentIdx, TypeTagIdx, IsPointer,
3872 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003873}
3874
3875static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3876 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003877 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003878 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003879 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003880 return;
3881 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003882
3883 if (!checkAttributeNumArgs(S, Attr, 1))
3884 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003885
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003886 if (!isa<VarDecl>(D)) {
3887 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3888 << Attr.getName() << ExpectedVariable;
3889 return;
3890 }
3891
Aaron Ballman00e99962013-08-31 01:11:41 +00003892 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00003893 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00003894 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3895 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003896
Michael Han99315932013-01-24 16:46:58 +00003897 D->addAttr(::new (S.Context)
3898 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003899 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003900 Attr.getLayoutCompatible(),
3901 Attr.getMustBeNull(),
3902 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003903}
3904
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003905//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003906// Checker-specific attribute handlers.
3907//===----------------------------------------------------------------------===//
3908
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003909static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) {
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003910 return type->isDependentType() ||
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003911 type->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003912}
3913
John McCalled433932011-01-25 03:31:58 +00003914static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003915 return type->isDependentType() ||
3916 type->isObjCObjectPointerType() ||
3917 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003918}
3919static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003920 return type->isDependentType() ||
3921 type->isPointerType() ||
3922 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003923}
3924
Chandler Carruthedc2c642011-07-02 00:01:44 +00003925static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003926 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003927 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003928
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003929 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003930 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3931 cf = false;
3932 } else {
3933 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3934 cf = true;
3935 }
3936
3937 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003938 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003939 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003940 return;
3941 }
3942
3943 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003944 param->addAttr(::new (S.Context)
3945 CFConsumedAttr(Attr.getRange(), S.Context,
3946 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003947 else
Michael Han99315932013-01-24 16:46:58 +00003948 param->addAttr(::new (S.Context)
3949 NSConsumedAttr(Attr.getRange(), S.Context,
3950 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003951}
3952
Chandler Carruthedc2c642011-07-02 00:01:44 +00003953static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3954 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003955
John McCalled433932011-01-25 03:31:58 +00003956 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003957
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003958 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003959 returnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003960 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003961 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003962 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003963 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3964 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003965 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003966 returnType = FD->getReturnType();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00003967 else if (auto *Param = dyn_cast<ParmVarDecl>(D)) {
3968 returnType = Param->getType()->getPointeeType();
3969 if (returnType.isNull()) {
3970 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
3971 << Attr.getName() << /*pointer-to-CF*/2
3972 << Attr.getRange();
3973 return;
3974 }
3975 } else {
3976 AttributeDeclKind ExpectedDeclKind;
3977 switch (Attr.getKind()) {
3978 default: llvm_unreachable("invalid ownership attribute");
3979 case AttributeList::AT_NSReturnsRetained:
3980 case AttributeList::AT_NSReturnsAutoreleased:
3981 case AttributeList::AT_NSReturnsNotRetained:
3982 ExpectedDeclKind = ExpectedFunctionOrMethod;
3983 break;
3984
3985 case AttributeList::AT_CFReturnsRetained:
3986 case AttributeList::AT_CFReturnsNotRetained:
3987 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
3988 break;
3989 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003990 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00003991 << Attr.getRange() << Attr.getName() << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003992 return;
3993 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003994
John McCalled433932011-01-25 03:31:58 +00003995 bool typeOK;
3996 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003997 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003998 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003999 case AttributeList::AT_NSReturnsRetained:
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00004000 typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType);
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004001 cf = false;
4002 break;
4003
4004 case AttributeList::AT_NSReturnsAutoreleased:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004005 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004006 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4007 cf = false;
4008 break;
4009
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004010 case AttributeList::AT_CFReturnsRetained:
4011 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004012 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4013 cf = true;
4014 break;
4015 }
4016
4017 if (!typeOK) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004018 if (isa<ParmVarDecl>(D)) {
4019 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
4020 << Attr.getName() << /*pointer-to-CF*/2
4021 << Attr.getRange();
4022 } else {
4023 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
4024 enum : unsigned {
4025 Function,
4026 Method,
4027 Property
4028 } SubjectKind = Function;
4029 if (isa<ObjCMethodDecl>(D))
4030 SubjectKind = Method;
4031 else if (isa<ObjCPropertyDecl>(D))
4032 SubjectKind = Property;
4033 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4034 << Attr.getName() << SubjectKind << cf
4035 << Attr.getRange();
4036 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004037 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004038 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004039
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004040 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004041 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004042 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004043 case AttributeList::AT_NSReturnsAutoreleased:
Nico Weber462fd1e2015-01-07 23:50:05 +00004044 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(
4045 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004046 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004047 case AttributeList::AT_CFReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004048 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(
4049 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004050 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004051 case AttributeList::AT_NSReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004052 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(
4053 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004054 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004055 case AttributeList::AT_CFReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004056 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(
4057 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004058 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004059 case AttributeList::AT_NSReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004060 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(
4061 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004062 return;
4063 };
4064}
4065
John McCallcf166702011-07-22 08:53:00 +00004066static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4067 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00004068 const int EP_ObjCMethod = 1;
4069 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004070
John McCallcf166702011-07-22 08:53:00 +00004071 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004072 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004073 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004074 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004075 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004076 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00004077
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004078 if (!resultType->isReferenceType() &&
4079 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004080 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00004081 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004082 << attr.getName()
4083 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004084 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00004085
4086 // Drop the attribute.
4087 return;
4088 }
4089
Nico Weber462fd1e2015-01-07 23:50:05 +00004090 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(
4091 attr.getRange(), S.Context, attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004092}
4093
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004094static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4095 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004096 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004097
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004098 DeclContext *DC = method->getDeclContext();
4099 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4100 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4101 << attr.getName() << 0;
4102 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4103 return;
4104 }
4105 if (method->getMethodFamily() == OMF_dealloc) {
4106 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4107 << attr.getName() << 1;
4108 return;
4109 }
4110
Michael Han99315932013-01-24 16:46:58 +00004111 method->addAttr(::new (S.Context)
4112 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4113 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004114}
4115
Aaron Ballmanfb763042013-12-02 18:05:46 +00004116static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
4117 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004118 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr.getRange(),
4119 Attr.getName()))
John McCall32f5fe12011-09-30 05:12:12 +00004120 return;
John McCall32f5fe12011-09-30 05:12:12 +00004121
Aaron Ballmanfb763042013-12-02 18:05:46 +00004122 D->addAttr(::new (S.Context)
4123 CFAuditedTransferAttr(Attr.getRange(), S.Context,
4124 Attr.getAttributeSpellingListIndex()));
4125}
4126
4127static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
4128 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004129 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr.getRange(),
4130 Attr.getName()))
Aaron Ballmanfb763042013-12-02 18:05:46 +00004131 return;
4132
4133 D->addAttr(::new (S.Context)
4134 CFUnknownTransferAttr(Attr.getRange(), S.Context,
4135 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004136}
4137
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004138static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
4139 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004140 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004141
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004142 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004143 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004144 return;
4145 }
John McCall28592582015-02-01 22:34:06 +00004146
4147 // Typedefs only allow objc_bridge(id) and have some additional checking.
4148 if (auto TD = dyn_cast<TypedefNameDecl>(D)) {
4149 if (!Parm->Ident->isStr("id")) {
4150 S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_id)
4151 << Attr.getName();
4152 return;
4153 }
4154
4155 // Only allow 'cv void *'.
4156 QualType T = TD->getUnderlyingType();
4157 if (!T->isVoidPointerType()) {
4158 S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
4159 return;
4160 }
4161 }
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004162
4163 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004164 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004165 Attr.getAttributeSpellingListIndex()));
4166}
4167
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004168static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
4169 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004170 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
4171
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004172 if (!Parm) {
4173 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
4174 return;
4175 }
4176
4177 D->addAttr(::new (S.Context)
4178 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
4179 Attr.getAttributeSpellingListIndex()));
4180}
4181
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004182static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
4183 const AttributeList &Attr) {
4184 IdentifierInfo *RelatedClass =
Craig Topperc3ec1492014-05-26 06:22:03 +00004185 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004186 if (!RelatedClass) {
4187 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
4188 return;
4189 }
4190 IdentifierInfo *ClassMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00004191 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004192 IdentifierInfo *InstanceMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00004193 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004194 D->addAttr(::new (S.Context)
4195 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
4196 ClassMethod, InstanceMethod,
4197 Attr.getAttributeSpellingListIndex()));
4198}
4199
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004200static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
4201 const AttributeList &Attr) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004202 ObjCInterfaceDecl *IFace;
Nico Weber462fd1e2015-01-07 23:50:05 +00004203 if (ObjCCategoryDecl *CatDecl =
4204 dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004205 IFace = CatDecl->getClassInterface();
4206 else
4207 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00004208
4209 if (!IFace)
4210 return;
4211
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00004212 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00004213 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004214 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
4215 Attr.getAttributeSpellingListIndex()));
4216}
4217
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004218static void handleObjCRuntimeName(Sema &S, Decl *D,
4219 const AttributeList &Attr) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004220 StringRef MetaDataName;
4221 if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName))
4222 return;
4223 D->addAttr(::new (S.Context)
4224 ObjCRuntimeNameAttr(Attr.getRange(), S.Context,
4225 MetaDataName,
4226 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004227}
4228
Alex Denisovfde64952015-06-26 05:28:36 +00004229// when a user wants to use objc_boxable with a union or struct
4230// but she doesn't have access to the declaration (legacy/third-party code)
4231// then she can 'enable' this feature via trick with a typedef
4232// e.g.:
4233// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
4234static void handleObjCBoxable(Sema &S, Decl *D, const AttributeList &Attr) {
4235 bool notify = false;
4236
4237 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4238 if (RD && RD->getDefinition()) {
4239 RD = RD->getDefinition();
4240 notify = true;
4241 }
4242
4243 if (RD) {
4244 ObjCBoxableAttr *BoxableAttr = ::new (S.Context)
4245 ObjCBoxableAttr(Attr.getRange(), S.Context,
4246 Attr.getAttributeSpellingListIndex());
4247 RD->addAttr(BoxableAttr);
4248 if (notify) {
4249 // we need to notify ASTReader/ASTWriter about
4250 // modification of existing declaration
4251 if (ASTMutationListener *L = S.getASTMutationListener())
4252 L->AddedAttributeToRecord(BoxableAttr, RD);
4253 }
4254 }
4255}
4256
Chandler Carruthedc2c642011-07-02 00:01:44 +00004257static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4258 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004259 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004260
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004261 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004262 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004263}
4264
Chandler Carruthedc2c642011-07-02 00:01:44 +00004265static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4266 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004267 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004268 QualType type = vd->getType();
4269
4270 if (!type->isDependentType() &&
4271 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004272 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004273 << type;
4274 return;
4275 }
4276
4277 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4278
4279 // If we have no lifetime yet, check the lifetime we're presumably
4280 // going to infer.
4281 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4282 lifetime = type->getObjCARCImplicitLifetime();
4283
4284 switch (lifetime) {
4285 case Qualifiers::OCL_None:
4286 assert(type->isDependentType() &&
4287 "didn't infer lifetime for non-dependent type?");
4288 break;
4289
4290 case Qualifiers::OCL_Weak: // meaningful
4291 case Qualifiers::OCL_Strong: // meaningful
4292 break;
4293
4294 case Qualifiers::OCL_ExplicitNone:
4295 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004296 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004297 << (lifetime == Qualifiers::OCL_Autoreleasing);
4298 break;
4299 }
4300
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004301 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004302 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4303 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004304}
4305
Francois Picheta83957a2010-12-19 06:50:37 +00004306//===----------------------------------------------------------------------===//
4307// Microsoft specific attribute handlers.
4308//===----------------------------------------------------------------------===//
4309
Chandler Carruthedc2c642011-07-02 00:01:44 +00004310static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00004311 if (!S.LangOpts.CPlusPlus) {
4312 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4313 << Attr.getName() << AttributeLangSupport::C;
4314 return;
4315 }
4316
Aaron Ballman60e705e2013-11-24 20:58:02 +00004317 if (!isa<CXXRecordDecl>(D)) {
4318 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4319 << Attr.getName() << ExpectedClass;
4320 return;
4321 }
4322
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004323 StringRef StrRef;
4324 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00004325 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00004326 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004327
David Majnemer89085342013-08-09 08:56:20 +00004328 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4329 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00004330 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4331 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00004332
Reid Kleckner140c4a72013-05-17 14:04:52 +00004333 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00004334 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004335 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004336 return;
4337 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004338
David Majnemer89085342013-08-09 08:56:20 +00004339 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004340 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00004341 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004342 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00004343 return;
4344 }
David Majnemer89085342013-08-09 08:56:20 +00004345 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004346 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004347 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004348 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004349 }
Francois Picheta83957a2010-12-19 06:50:37 +00004350
David Majnemer89085342013-08-09 08:56:20 +00004351 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4352 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004353}
4354
David Majnemer2c4e00a2014-01-29 22:07:36 +00004355static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4356 if (!S.LangOpts.CPlusPlus) {
4357 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4358 << Attr.getName() << AttributeLangSupport::C;
4359 return;
4360 }
4361 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
David Majnemer4bb09802014-02-10 19:50:15 +00004362 D, Attr.getRange(), /*BestCase=*/true,
4363 Attr.getAttributeSpellingListIndex(),
David Majnemer2c4e00a2014-01-29 22:07:36 +00004364 (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling());
4365 if (IA)
4366 D->addAttr(IA);
4367}
4368
Reid Kleckner7d6d2702014-05-01 03:16:47 +00004369static void handleDeclspecThreadAttr(Sema &S, Decl *D,
4370 const AttributeList &Attr) {
4371 VarDecl *VD = cast<VarDecl>(D);
4372 if (!S.Context.getTargetInfo().isTLSSupported()) {
4373 S.Diag(Attr.getLoc(), diag::err_thread_unsupported);
4374 return;
4375 }
4376 if (VD->getTSCSpec() != TSCS_unspecified) {
4377 S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable);
4378 return;
4379 }
4380 if (VD->hasLocalStorage()) {
4381 S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
4382 return;
4383 }
4384 VD->addAttr(::new (S.Context) ThreadAttr(
4385 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
4386}
4387
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004388static void handleARMInterruptAttr(Sema &S, Decl *D,
4389 const AttributeList &Attr) {
4390 // Check the attribute arguments.
4391 if (Attr.getNumArgs() > 1) {
4392 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
4393 << Attr.getName() << 1;
4394 return;
4395 }
4396
4397 StringRef Str;
4398 SourceLocation ArgLoc;
4399
4400 if (Attr.getNumArgs() == 0)
4401 Str = "";
4402 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
4403 return;
4404
4405 ARMInterruptAttr::InterruptType Kind;
4406 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
4407 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
4408 << Attr.getName() << Str << ArgLoc;
4409 return;
4410 }
4411
4412 unsigned Index = Attr.getAttributeSpellingListIndex();
4413 D->addAttr(::new (S.Context)
4414 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
4415}
4416
4417static void handleMSP430InterruptAttr(Sema &S, Decl *D,
4418 const AttributeList &Attr) {
4419 if (!checkAttributeNumArgs(S, Attr, 1))
4420 return;
4421
4422 if (!Attr.isArgExpr(0)) {
4423 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
4424 << AANT_ArgumentIntegerConstant;
4425 return;
4426 }
4427
4428 // FIXME: Check for decl - it should be void ()(void).
4429
4430 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4431 llvm::APSInt NumParams(32);
4432 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
4433 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4434 << Attr.getName() << AANT_ArgumentIntegerConstant
4435 << NumParamsExpr->getSourceRange();
4436 return;
4437 }
4438
4439 unsigned Num = NumParams.getLimitedValue(255);
4440 if ((Num & 1) || Num > 30) {
4441 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
4442 << Attr.getName() << (int)NumParams.getSExtValue()
4443 << NumParamsExpr->getSourceRange();
4444 return;
4445 }
4446
Aaron Ballman36a53502014-01-16 13:03:14 +00004447 D->addAttr(::new (S.Context)
4448 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
4449 Attr.getAttributeSpellingListIndex()));
4450 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004451}
4452
4453static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4454 // Dispatch the interrupt attribute based on the current target.
4455 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
4456 handleMSP430InterruptAttr(S, D, Attr);
4457 else
4458 handleARMInterruptAttr(S, D, Attr);
4459}
4460
Matt Arsenault43fae6c2014-12-04 20:38:18 +00004461static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D,
4462 const AttributeList &Attr) {
4463 uint32_t NumRegs;
4464 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4465 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4466 return;
4467
4468 D->addAttr(::new (S.Context)
4469 AMDGPUNumVGPRAttr(Attr.getLoc(), S.Context,
4470 NumRegs,
4471 Attr.getAttributeSpellingListIndex()));
4472}
4473
4474static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D,
4475 const AttributeList &Attr) {
4476 uint32_t NumRegs;
4477 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4478 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4479 return;
4480
4481 D->addAttr(::new (S.Context)
4482 AMDGPUNumSGPRAttr(Attr.getLoc(), S.Context,
4483 NumRegs,
4484 Attr.getAttributeSpellingListIndex()));
4485}
4486
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004487static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
4488 const AttributeList& Attr) {
4489 // If we try to apply it to a function pointer, don't warn, but don't
4490 // do anything, either. It doesn't matter anyway, because there's nothing
4491 // special about calling a force_align_arg_pointer function.
4492 ValueDecl *VD = dyn_cast<ValueDecl>(D);
4493 if (VD && VD->getType()->isFunctionPointerType())
4494 return;
4495 // Also don't warn on function pointer typedefs.
4496 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
4497 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
4498 TD->getUnderlyingType()->isFunctionType()))
4499 return;
4500 // Attribute can only be applied to function types.
4501 if (!isa<FunctionDecl>(D)) {
4502 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4503 << Attr.getName() << /* function */0;
4504 return;
4505 }
4506
Aaron Ballman36a53502014-01-16 13:03:14 +00004507 D->addAttr(::new (S.Context)
4508 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
4509 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004510}
4511
4512DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
4513 unsigned AttrSpellingListIndex) {
4514 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004515 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00004516 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004517 }
4518
4519 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004520 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004521
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004522 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004523}
4524
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004525DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
4526 unsigned AttrSpellingListIndex) {
4527 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004528 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004529 D->dropAttr<DLLImportAttr>();
4530 }
4531
4532 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004533 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004534
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004535 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004536}
4537
Hans Wennborge82f19c2014-06-24 23:57:05 +00004538static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00004539 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
4540 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4541 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored)
4542 << A.getName();
4543 return;
4544 }
4545
Hans Wennborg606bd6d2014-11-03 14:24:45 +00004546 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4547 if (FD->isInlined() && A.getKind() == AttributeList::AT_DLLImport &&
4548 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4549 // MinGW doesn't allow dllimport on inline functions.
4550 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
4551 << A.getName();
4552 return;
4553 }
4554 }
4555
Hans Wennborg5869ec42015-09-15 21:05:30 +00004556 if (auto *MD = dyn_cast<CXXMethodDecl>(D)) {
4557 if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
4558 MD->getParent()->isLambda()) {
4559 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A.getName();
4560 return;
4561 }
4562 }
4563
Hans Wennborge82f19c2014-06-24 23:57:05 +00004564 unsigned Index = A.getAttributeSpellingListIndex();
4565 Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport
4566 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
4567 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004568 if (NewAttr)
4569 D->addAttr(NewAttr);
4570}
4571
David Majnemer2c4e00a2014-01-29 22:07:36 +00004572MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00004573Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00004574 unsigned AttrSpellingListIndex,
4575 MSInheritanceAttr::Spelling SemanticSpelling) {
4576 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
4577 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00004578 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004579 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
4580 << 1 /*previous declaration*/;
4581 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
4582 D->dropAttr<MSInheritanceAttr>();
4583 }
4584
4585 CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
4586 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00004587 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
4588 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004589 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004590 }
4591 } else {
4592 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
4593 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4594 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004595 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004596 }
4597 if (RD->getDescribedClassTemplate()) {
4598 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4599 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004600 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004601 }
4602 }
4603
4604 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00004605 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00004606}
4607
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004608static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4609 // The capability attributes take a single string parameter for the name of
4610 // the capability they represent. The lockable attribute does not take any
4611 // parameters. However, semantically, both attributes represent the same
4612 // concept, and so they use the same semantic attribute. Eventually, the
4613 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00004614 //
Alp Toker958027b2014-07-14 19:42:55 +00004615 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00004616 // literal will be considered a "mutex."
4617 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004618 SourceLocation LiteralLoc;
4619 if (Attr.getKind() == AttributeList::AT_Capability &&
4620 !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc))
4621 return;
4622
Aaron Ballman6c810072014-03-05 21:47:13 +00004623 // Currently, there are only two names allowed for a capability: role and
4624 // mutex (case insensitive). Diagnose other capability names.
4625 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
4626 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
4627
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004628 D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N,
4629 Attr.getAttributeSpellingListIndex()));
4630}
4631
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004632static void handleAssertCapabilityAttr(Sema &S, Decl *D,
4633 const AttributeList &Attr) {
4634 D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context,
4635 Attr.getArgAsExpr(0),
4636 Attr.getAttributeSpellingListIndex()));
4637}
4638
4639static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
4640 const AttributeList &Attr) {
4641 SmallVector<Expr*, 1> Args;
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004642 if (!checkLockFunAttrCommon(S, D, Attr, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004643 return;
4644
4645 D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(),
4646 S.Context,
4647 Args.data(), Args.size(),
4648 Attr.getAttributeSpellingListIndex()));
4649}
4650
4651static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
4652 const AttributeList &Attr) {
4653 SmallVector<Expr*, 2> Args;
4654 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
4655 return;
4656
4657 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(),
4658 S.Context,
4659 Attr.getArgAsExpr(0),
4660 Args.data(),
4661 Args.size(),
4662 Attr.getAttributeSpellingListIndex()));
4663}
4664
4665static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
4666 const AttributeList &Attr) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004667 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004668 SmallVector<Expr *, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004669 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004670
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004671 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
4672 Attr.getRange(), S.Context, Args.data(), Args.size(),
4673 Attr.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004674}
4675
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004676static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
4677 const AttributeList &Attr) {
4678 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
4679 return;
4680
4681 // check that all arguments are lockable objects
4682 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004683 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004684 if (Args.empty())
4685 return;
4686
4687 RequiresCapabilityAttr *RCA = ::new (S.Context)
4688 RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(),
4689 Args.size(), Attr.getAttributeSpellingListIndex());
4690
4691 D->addAttr(RCA);
4692}
4693
Aaron Ballman43f40102014-11-14 22:34:56 +00004694static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4695 if (auto *NSD = dyn_cast<NamespaceDecl>(D)) {
4696 if (NSD->isAnonymousNamespace()) {
4697 S.Diag(Attr.getLoc(), diag::warn_deprecated_anonymous_namespace);
4698 // Do not want to attach the attribute to the namespace because that will
4699 // cause confusing diagnostic reports for uses of declarations within the
4700 // namespace.
4701 return;
4702 }
4703 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00004704
4705 if (!S.getLangOpts().CPlusPlus14)
4706 if (Attr.isCXX11Attribute() &&
4707 !(Attr.hasScope() && Attr.getScopeName()->isStr("gnu")))
Saleem Abdulrasoolb47d6062015-02-18 04:33:26 +00004708 S.Diag(Attr.getLoc(), diag::ext_deprecated_attr_is_a_cxx14_extension);
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00004709
Aaron Ballman43f40102014-11-14 22:34:56 +00004710 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
4711}
4712
Peter Collingbourne915df992015-05-15 18:33:32 +00004713static void handleNoSanitizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4714 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
4715 return;
4716
4717 std::vector<std::string> Sanitizers;
4718
4719 for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) {
4720 StringRef SanitizerName;
4721 SourceLocation LiteralLoc;
4722
4723 if (!S.checkStringLiteralArgumentAttr(Attr, I, SanitizerName, &LiteralLoc))
4724 return;
4725
4726 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0)
4727 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
4728
4729 Sanitizers.push_back(SanitizerName);
4730 }
4731
4732 D->addAttr(::new (S.Context) NoSanitizeAttr(
4733 Attr.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(),
4734 Attr.getAttributeSpellingListIndex()));
4735}
4736
4737static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
4738 const AttributeList &Attr) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00004739 StringRef AttrName = Attr.getName()->getName();
4740 normalizeName(AttrName);
Peter Collingbourne915df992015-05-15 18:33:32 +00004741 std::string SanitizerName =
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00004742 llvm::StringSwitch<std::string>(AttrName)
Peter Collingbourne915df992015-05-15 18:33:32 +00004743 .Case("no_address_safety_analysis", "address")
4744 .Case("no_sanitize_address", "address")
4745 .Case("no_sanitize_thread", "thread")
Peter Collingbourne94410942015-05-15 20:11:18 +00004746 .Case("no_sanitize_memory", "memory");
Peter Collingbourne915df992015-05-15 18:33:32 +00004747 D->addAttr(::new (S.Context)
4748 NoSanitizeAttr(Attr.getRange(), S.Context, &SanitizerName, 1,
4749 Attr.getAttributeSpellingListIndex()));
4750}
4751
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004752static void handleInternalLinkageAttr(Sema &S, Decl *D,
4753 const AttributeList &Attr) {
4754 if (InternalLinkageAttr *Internal =
4755 S.mergeInternalLinkageAttr(D, Attr.getRange(), Attr.getName(),
4756 Attr.getAttributeSpellingListIndex()))
4757 D->addAttr(Internal);
4758}
4759
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004760/// Handles semantic checking for features that are common to all attributes,
4761/// such as checking whether a parameter was properly specified, or the correct
4762/// number of arguments were passed, etc.
4763static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4764 const AttributeList &Attr) {
4765 // Several attributes carry different semantics than the parsing requires, so
4766 // those are opted out of the common handling.
4767 //
4768 // We also bail on unknown and ignored attributes because those are handled
4769 // as part of the target-specific handling logic.
4770 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004771 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004772 return false;
4773
Aaron Ballman3aff6332013-12-02 19:30:36 +00004774 // Check whether the attribute requires specific language extensions to be
4775 // enabled.
4776 if (!Attr.diagnoseLangOpts(S))
4777 return true;
4778
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00004779 if (Attr.getMinArgs() == Attr.getMaxArgs()) {
4780 // If there are no optional arguments, then checking for the argument count
4781 // is trivial.
4782 if (!checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4783 return true;
4784 } else {
4785 // There are optional arguments, so checking is slightly more involved.
4786 if (Attr.getMinArgs() &&
4787 !checkAttributeAtLeastNumArgs(S, Attr, Attr.getMinArgs()))
4788 return true;
4789 else if (!Attr.hasVariadicArg() && Attr.getMaxArgs() &&
4790 !checkAttributeAtMostNumArgs(S, Attr, Attr.getMaxArgs()))
4791 return true;
4792 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004793
4794 // Check whether the attribute appertains to the given subject.
4795 if (!Attr.diagnoseAppertainsTo(S, D))
4796 return true;
4797
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004798 return false;
4799}
4800
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004801//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004802// Top Level Sema Entry Points
4803//===----------------------------------------------------------------------===//
4804
Richard Smithf8a75c32013-08-29 00:47:48 +00004805/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4806/// the attribute applies to decls. If the attribute is a type attribute, just
4807/// silently ignore it if a GNU attribute.
4808static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4809 const AttributeList &Attr,
4810 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004811 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00004812 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004813
Richard Smithf8a75c32013-08-29 00:47:48 +00004814 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4815 // instead.
4816 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4817 return;
4818
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004819 // Unknown attributes are automatically warned on. Target-specific attributes
4820 // which do not apply to the current target architecture are treated as
4821 // though they were unknown attributes.
4822 if (Attr.getKind() == AttributeList::UnknownAttribute ||
Bob Wilson7c730832015-07-20 22:57:31 +00004823 !Attr.existsInTarget(S.Context.getTargetInfo())) {
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004824 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute()
4825 ? diag::warn_unhandled_ms_attribute_ignored
4826 : diag::warn_unknown_attribute_ignored)
4827 << Attr.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004828 return;
4829 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004830
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004831 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4832 return;
4833
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004834 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004835 default:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004836 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004837 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004838 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004839 case AttributeList::AT_Interrupt:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004840 handleInterruptAttr(S, D, Attr);
4841 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004842 case AttributeList::AT_X86ForceAlignArgPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004843 handleX86ForceAlignArgPointerAttr(S, D, Attr);
4844 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004845 case AttributeList::AT_DLLExport:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004846 case AttributeList::AT_DLLImport:
Hans Wennborge82f19c2014-06-24 23:57:05 +00004847 handleDLLAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004848 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004849 case AttributeList::AT_Mips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004850 handleSimpleAttribute<Mips16Attr>(S, D, Attr);
4851 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004852 case AttributeList::AT_NoMips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004853 handleSimpleAttribute<NoMips16Attr>(S, D, Attr);
4854 break;
Matt Arsenault43fae6c2014-12-04 20:38:18 +00004855 case AttributeList::AT_AMDGPUNumVGPR:
4856 handleAMDGPUNumVGPRAttr(S, D, Attr);
4857 break;
4858 case AttributeList::AT_AMDGPUNumSGPR:
4859 handleAMDGPUNumSGPRAttr(S, D, Attr);
4860 break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00004861 case AttributeList::AT_IBAction:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004862 handleSimpleAttribute<IBActionAttr>(S, D, Attr);
4863 break;
4864 case AttributeList::AT_IBOutlet:
4865 handleIBOutlet(S, D, Attr);
4866 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004867 case AttributeList::AT_IBOutletCollection:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004868 handleIBOutletCollection(S, D, Attr);
4869 break;
4870 case AttributeList::AT_Alias:
4871 handleAliasAttr(S, D, Attr);
4872 break;
4873 case AttributeList::AT_Aligned:
4874 handleAlignedAttr(S, D, Attr);
4875 break;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00004876 case AttributeList::AT_AlignValue:
4877 handleAlignValueAttr(S, D, Attr);
4878 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004879 case AttributeList::AT_AlwaysInline:
Paul Robinsonf0674352014-03-31 22:29:15 +00004880 handleAlwaysInlineAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004881 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004882 case AttributeList::AT_AnalyzerNoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004883 handleAnalyzerNoReturnAttr(S, D, Attr);
4884 break;
4885 case AttributeList::AT_TLSModel:
4886 handleTLSModelAttr(S, D, Attr);
4887 break;
4888 case AttributeList::AT_Annotate:
4889 handleAnnotateAttr(S, D, Attr);
4890 break;
4891 case AttributeList::AT_Availability:
4892 handleAvailabilityAttr(S, D, Attr);
4893 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004894 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004895 handleDependencyAttr(S, scope, D, Attr);
4896 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004897 case AttributeList::AT_Common:
4898 handleCommonAttr(S, D, Attr);
4899 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004900 case AttributeList::AT_CUDAConstant:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004901 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr);
4902 break;
4903 case AttributeList::AT_Constructor:
4904 handleConstructorAttr(S, D, Attr);
4905 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004906 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004907 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr);
4908 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004909 case AttributeList::AT_Deprecated:
Aaron Ballman43f40102014-11-14 22:34:56 +00004910 handleDeprecatedAttr(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004911 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004912 case AttributeList::AT_Destructor:
4913 handleDestructorAttr(S, D, Attr);
4914 break;
4915 case AttributeList::AT_EnableIf:
4916 handleEnableIfAttr(S, D, Attr);
4917 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004918 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004919 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004920 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004921 case AttributeList::AT_MinSize:
Paul Robinsonaae2fba2014-12-10 23:34:36 +00004922 handleMinSizeAttr(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00004923 break;
Paul Robinsonf0674352014-03-31 22:29:15 +00004924 case AttributeList::AT_OptimizeNone:
4925 handleOptimizeNoneAttr(S, D, Attr);
4926 break;
Alexis Hunt724f14e2014-11-28 00:53:20 +00004927 case AttributeList::AT_FlagEnum:
4928 handleSimpleAttribute<FlagEnumAttr>(S, D, Attr);
4929 break;
Peter Collingbourne41af7c22014-05-20 17:12:51 +00004930 case AttributeList::AT_Flatten:
4931 handleSimpleAttribute<FlattenAttr>(S, D, Attr);
4932 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004933 case AttributeList::AT_Format:
4934 handleFormatAttr(S, D, Attr);
4935 break;
4936 case AttributeList::AT_FormatArg:
4937 handleFormatArgAttr(S, D, Attr);
4938 break;
4939 case AttributeList::AT_CUDAGlobal:
4940 handleGlobalAttr(S, D, Attr);
4941 break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00004942 case AttributeList::AT_CUDADevice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004943 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr);
4944 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004945 case AttributeList::AT_CUDAHost:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004946 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr);
4947 break;
4948 case AttributeList::AT_GNUInline:
4949 handleGNUInlineAttr(S, D, Attr);
4950 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004951 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004952 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004953 break;
David Majnemer631a90b2015-02-04 07:23:21 +00004954 case AttributeList::AT_Restrict:
4955 handleRestrictAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004956 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004957 case AttributeList::AT_MayAlias:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004958 handleSimpleAttribute<MayAliasAttr>(S, D, Attr);
4959 break;
4960 case AttributeList::AT_Mode:
4961 handleModeAttr(S, D, Attr);
4962 break;
David Majnemer1bf0f8e2015-07-20 22:51:52 +00004963 case AttributeList::AT_NoAlias:
4964 handleSimpleAttribute<NoAliasAttr>(S, D, Attr);
4965 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004966 case AttributeList::AT_NoCommon:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004967 handleSimpleAttribute<NoCommonAttr>(S, D, Attr);
4968 break;
Peter Collingbourneb4728c12014-05-19 22:14:34 +00004969 case AttributeList::AT_NoSplitStack:
4970 handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr);
4971 break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00004972 case AttributeList::AT_NonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004973 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
4974 handleNonNullAttrParameter(S, PVD, Attr);
4975 else
4976 handleNonNullAttr(S, D, Attr);
4977 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00004978 case AttributeList::AT_ReturnsNonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004979 handleReturnsNonNullAttr(S, D, Attr);
4980 break;
Hal Finkelee90a222014-09-26 05:04:30 +00004981 case AttributeList::AT_AssumeAligned:
4982 handleAssumeAlignedAttr(S, D, Attr);
4983 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004984 case AttributeList::AT_Overloadable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004985 handleSimpleAttribute<OverloadableAttr>(S, D, Attr);
4986 break;
4987 case AttributeList::AT_Ownership:
4988 handleOwnershipAttr(S, D, Attr);
4989 break;
4990 case AttributeList::AT_Cold:
4991 handleColdAttr(S, D, Attr);
4992 break;
4993 case AttributeList::AT_Hot:
4994 handleHotAttr(S, D, Attr);
4995 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004996 case AttributeList::AT_Naked:
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00004997 handleNakedAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004998 break;
4999 case AttributeList::AT_NoReturn:
5000 handleNoReturnAttr(S, D, Attr);
5001 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00005002 case AttributeList::AT_NoThrow:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005003 handleSimpleAttribute<NoThrowAttr>(S, D, Attr);
5004 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005005 case AttributeList::AT_CUDAShared:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005006 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr);
5007 break;
5008 case AttributeList::AT_VecReturn:
5009 handleVecReturnAttr(S, D, Attr);
5010 break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005011
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005012 case AttributeList::AT_ObjCOwnership:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005013 handleObjCOwnershipAttr(S, D, Attr);
5014 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005015 case AttributeList::AT_ObjCPreciseLifetime:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005016 handleObjCPreciseLifetimeAttr(S, D, Attr);
5017 break;
John McCall31168b02011-06-15 23:02:42 +00005018
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005019 case AttributeList::AT_ObjCReturnsInnerPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005020 handleObjCReturnsInnerPointerAttr(S, D, Attr);
5021 break;
John McCallcf166702011-07-22 08:53:00 +00005022
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005023 case AttributeList::AT_ObjCRequiresSuper:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005024 handleObjCRequiresSuperAttr(S, D, Attr);
5025 break;
5026
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005027 case AttributeList::AT_ObjCBridge:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005028 handleObjCBridgeAttr(S, scope, D, Attr);
5029 break;
5030
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005031 case AttributeList::AT_ObjCBridgeMutable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005032 handleObjCBridgeMutableAttr(S, scope, D, Attr);
5033 break;
5034
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005035 case AttributeList::AT_ObjCBridgeRelated:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005036 handleObjCBridgeRelatedAttr(S, scope, D, Attr);
5037 break;
John McCallf1e8b342011-09-29 07:17:38 +00005038
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005039 case AttributeList::AT_ObjCDesignatedInitializer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005040 handleObjCDesignatedInitializer(S, D, Attr);
5041 break;
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005042
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00005043 case AttributeList::AT_ObjCRuntimeName:
5044 handleObjCRuntimeName(S, D, Attr);
5045 break;
Alex Denisovfde64952015-06-26 05:28:36 +00005046
5047 case AttributeList::AT_ObjCBoxable:
5048 handleObjCBoxable(S, D, Attr);
5049 break;
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00005050
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005051 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005052 handleCFAuditedTransferAttr(S, D, Attr);
5053 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005054 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005055 handleCFUnknownTransferAttr(S, D, Attr);
5056 break;
John McCall32f5fe12011-09-30 05:12:12 +00005057
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005058 case AttributeList::AT_CFConsumed:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005059 case AttributeList::AT_NSConsumed:
5060 handleNSConsumedAttr(S, D, Attr);
5061 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005062 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005063 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr);
5064 break;
John McCalled433932011-01-25 03:31:58 +00005065
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005066 case AttributeList::AT_NSReturnsAutoreleased:
5067 case AttributeList::AT_NSReturnsNotRetained:
5068 case AttributeList::AT_CFReturnsNotRetained:
5069 case AttributeList::AT_NSReturnsRetained:
5070 case AttributeList::AT_CFReturnsRetained:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005071 handleNSReturnsRetainedAttr(S, D, Attr);
5072 break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00005073 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005074 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr);
5075 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005076 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005077 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr);
5078 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00005079 case AttributeList::AT_VecTypeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005080 handleVecTypeHint(S, D, Attr);
5081 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00005082
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005083 case AttributeList::AT_InitPriority:
5084 handleInitPriorityAttr(S, D, Attr);
5085 break;
5086
5087 case AttributeList::AT_Packed:
5088 handlePackedAttr(S, D, Attr);
5089 break;
5090 case AttributeList::AT_Section:
5091 handleSectionAttr(S, D, Attr);
5092 break;
Eric Christopher11acf732015-06-12 01:35:52 +00005093 case AttributeList::AT_Target:
5094 handleTargetAttr(S, D, Attr);
5095 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005096 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00005097 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00005098 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005099 case AttributeList::AT_ArcWeakrefUnavailable:
5100 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr);
5101 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005102 case AttributeList::AT_ObjCRootClass:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005103 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr);
5104 break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00005105 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek438f8db2014-02-22 01:06:05 +00005106 handleObjCSuppresProtocolAttr(S, D, Attr);
Ted Kremenek28eace62013-11-23 01:01:34 +00005107 break;
5108 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005109 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr);
5110 break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00005111 case AttributeList::AT_Unused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005112 handleSimpleAttribute<UnusedAttr>(S, D, Attr);
5113 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005114 case AttributeList::AT_ReturnsTwice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005115 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr);
5116 break;
Akira Hatanakac8667622015-11-06 23:56:15 +00005117 case AttributeList::AT_NotTailCalled:
5118 handleNotTailCalledAttr(S, D, Attr);
5119 break;
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00005120 case AttributeList::AT_DisableTailCalls:
5121 handleDisableTailCallsAttr(S, D, Attr);
5122 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005123 case AttributeList::AT_Used:
5124 handleUsedAttr(S, D, Attr);
5125 break;
John McCalld041a9b2013-02-20 01:54:26 +00005126 case AttributeList::AT_Visibility:
5127 handleVisibilityAttr(S, D, Attr, false);
5128 break;
5129 case AttributeList::AT_TypeVisibility:
5130 handleVisibilityAttr(S, D, Attr, true);
5131 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00005132 case AttributeList::AT_WarnUnused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005133 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr);
5134 break;
5135 case AttributeList::AT_WarnUnusedResult:
5136 handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00005137 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00005138 case AttributeList::AT_Weak:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005139 handleSimpleAttribute<WeakAttr>(S, D, Attr);
5140 break;
5141 case AttributeList::AT_WeakRef:
5142 handleWeakRefAttr(S, D, Attr);
5143 break;
5144 case AttributeList::AT_WeakImport:
5145 handleWeakImportAttr(S, D, Attr);
5146 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005147 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005148 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005149 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005150 case AttributeList::AT_ObjCException:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005151 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr);
5152 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005153 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005154 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00005155 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005156 case AttributeList::AT_ObjCNSObject:
5157 handleObjCNSObject(S, D, Attr);
5158 break;
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00005159 case AttributeList::AT_ObjCIndependentClass:
5160 handleObjCIndependentClass(S, D, Attr);
5161 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005162 case AttributeList::AT_Blocks:
5163 handleBlocksAttr(S, D, Attr);
5164 break;
5165 case AttributeList::AT_Sentinel:
5166 handleSentinelAttr(S, D, Attr);
5167 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00005168 case AttributeList::AT_Const:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005169 handleSimpleAttribute<ConstAttr>(S, D, Attr);
5170 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005171 case AttributeList::AT_Pure:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005172 handleSimpleAttribute<PureAttr>(S, D, Attr);
5173 break;
5174 case AttributeList::AT_Cleanup:
5175 handleCleanupAttr(S, D, Attr);
5176 break;
5177 case AttributeList::AT_NoDebug:
5178 handleNoDebugAttr(S, D, Attr);
5179 break;
Aaron Ballman7c19ab12014-02-22 16:59:24 +00005180 case AttributeList::AT_NoDuplicate:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005181 handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr);
5182 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005183 case AttributeList::AT_NoInline:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005184 handleSimpleAttribute<NoInlineAttr>(S, D, Attr);
5185 break;
5186 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
5187 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr);
5188 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005189 case AttributeList::AT_StdCall:
5190 case AttributeList::AT_CDecl:
5191 case AttributeList::AT_FastCall:
5192 case AttributeList::AT_ThisCall:
5193 case AttributeList::AT_Pascal:
Reid Klecknerd7857f02014-10-24 17:42:17 +00005194 case AttributeList::AT_VectorCall:
Charles Davisb5a214e2013-08-30 04:39:01 +00005195 case AttributeList::AT_MSABI:
5196 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005197 case AttributeList::AT_Pcs:
Guy Benyeif0a014b2012-12-25 08:53:55 +00005198 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005199 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00005200 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005201 case AttributeList::AT_OpenCLKernel:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005202 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr);
5203 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00005204 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005205 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr);
5206 break;
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005207 case AttributeList::AT_InternalLinkage:
5208 handleInternalLinkageAttr(S, D, Attr);
5209 break;
John McCall8d32c052012-05-22 21:28:12 +00005210
5211 // Microsoft attributes:
David Majnemer8ab003a2015-02-02 19:30:52 +00005212 case AttributeList::AT_MSNoVTable:
5213 handleSimpleAttribute<MSNoVTableAttr>(S, D, Attr);
David Majnemer129f4172015-02-02 10:22:20 +00005214 break;
David Majnemer8ab003a2015-02-02 19:30:52 +00005215 case AttributeList::AT_MSStruct:
5216 handleSimpleAttribute<MSStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00005217 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005218 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005219 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00005220 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00005221 case AttributeList::AT_MSInheritance:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005222 handleMSInheritanceAttr(S, D, Attr);
5223 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00005224 case AttributeList::AT_SelectAny:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005225 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr);
5226 break;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005227 case AttributeList::AT_Thread:
5228 handleDeclspecThreadAttr(S, D, Attr);
5229 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005230
5231 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00005232 case AttributeList::AT_AssertExclusiveLock:
5233 handleAssertExclusiveLockAttr(S, D, Attr);
5234 break;
5235 case AttributeList::AT_AssertSharedLock:
5236 handleAssertSharedLockAttr(S, D, Attr);
5237 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005238 case AttributeList::AT_GuardedVar:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005239 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr);
5240 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005241 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00005242 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005243 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005244 case AttributeList::AT_ScopedLockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005245 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr);
5246 break;
Peter Collingbourne915df992015-05-15 18:33:32 +00005247 case AttributeList::AT_NoSanitize:
5248 handleNoSanitizeAttr(S, D, Attr);
5249 break;
5250 case AttributeList::AT_NoSanitizeSpecific:
5251 handleNoSanitizeSpecificAttr(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00005252 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005253 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005254 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00005255 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005256 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005257 handleGuardedByAttr(S, D, Attr);
5258 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005259 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00005260 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005261 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005262 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005263 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005264 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005265 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005266 handleLockReturnedAttr(S, D, Attr);
5267 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005268 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005269 handleLocksExcludedAttr(S, D, Attr);
5270 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005271 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005272 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005273 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005274 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00005275 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005276 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005277 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00005278 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005279 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005280
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005281 // Capability analysis attributes.
5282 case AttributeList::AT_Capability:
5283 case AttributeList::AT_Lockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005284 handleCapabilityAttr(S, D, Attr);
5285 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005286 case AttributeList::AT_RequiresCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005287 handleRequiresCapabilityAttr(S, D, Attr);
5288 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005289
5290 case AttributeList::AT_AssertCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005291 handleAssertCapabilityAttr(S, D, Attr);
5292 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005293 case AttributeList::AT_AcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005294 handleAcquireCapabilityAttr(S, D, Attr);
5295 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005296 case AttributeList::AT_ReleaseCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005297 handleReleaseCapabilityAttr(S, D, Attr);
5298 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005299 case AttributeList::AT_TryAcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005300 handleTryAcquireCapabilityAttr(S, D, Attr);
5301 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005302
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00005303 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00005304 case AttributeList::AT_Consumable:
5305 handleConsumableAttr(S, D, Attr);
5306 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00005307 case AttributeList::AT_ConsumableAutoCast:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005308 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr);
5309 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00005310 case AttributeList::AT_ConsumableSetOnRead:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005311 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr);
5312 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00005313 case AttributeList::AT_CallableWhen:
5314 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00005315 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00005316 case AttributeList::AT_ParamTypestate:
5317 handleParamTypestateAttr(S, D, Attr);
5318 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00005319 case AttributeList::AT_ReturnTypestate:
5320 handleReturnTypestateAttr(S, D, Attr);
5321 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00005322 case AttributeList::AT_SetTypestate:
5323 handleSetTypestateAttr(S, D, Attr);
5324 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00005325 case AttributeList::AT_TestTypestate:
5326 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00005327 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00005328
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00005329 // Type safety attributes.
5330 case AttributeList::AT_ArgumentWithTypeTag:
5331 handleArgumentWithTypeTagAttr(S, D, Attr);
5332 break;
5333 case AttributeList::AT_TypeTagForDatatype:
5334 handleTypeTagForDatatypeAttr(S, D, Attr);
5335 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005336 }
5337}
5338
5339/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5340/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00005341void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00005342 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00005343 bool IncludeCXX11Attributes) {
5344 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00005345 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00005346
Joey Gouly2cd9db12013-12-13 16:15:28 +00005347 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00005348 // GCC accepts
5349 // static int a9 __attribute__((weakref));
5350 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00005351 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00005352 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
5353 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00005354 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00005355 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005356 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00005357
Aaron Ballmanbe243a72014-12-04 22:45:31 +00005358 // FIXME: We should be able to handle this in TableGen as well. It would be
5359 // good to have a way to specify "these attributes must appear as a group",
5360 // for these. Additionally, it would be good to have a way to specify "these
5361 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00005362 if (!D->hasAttr<OpenCLKernelAttr>()) {
5363 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00005364 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00005365 // FIXME: This emits a different error message than
5366 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00005367 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005368 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00005369 } else if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00005370 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005371 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00005372 } else if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00005373 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005374 D->setInvalidDecl();
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00005375 } else if (Attr *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
5376 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
5377 << A << ExpectedKernelFunction;
5378 D->setInvalidDecl();
5379 } else if (Attr *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
5380 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
5381 << A << ExpectedKernelFunction;
5382 D->setInvalidDecl();
Joey Gouly2cd9db12013-12-13 16:15:28 +00005383 }
5384 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005385}
5386
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005387// Annotation attributes are the only attributes allowed after an access
5388// specifier.
5389bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5390 const AttributeList *AttrList) {
5391 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005392 if (l->getKind() == AttributeList::AT_Annotate) {
David Majnemer706f3152014-12-14 01:05:01 +00005393 ProcessDeclAttribute(*this, nullptr, ASDecl, *l, l->isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005394 } else {
5395 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5396 return true;
5397 }
5398 }
5399
5400 return false;
5401}
5402
John McCall42856de2011-10-01 05:17:03 +00005403/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5404/// contains any decl attributes that we should warn about.
5405static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5406 for ( ; A; A = A->getNext()) {
5407 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00005408 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00005409 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5410
5411 if (A->getKind() == AttributeList::UnknownAttribute) {
5412 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5413 << A->getName() << A->getRange();
5414 } else {
5415 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5416 << A->getName() << A->getRange();
5417 }
5418 }
5419}
5420
5421/// checkUnusedDeclAttributes - Given a declarator which is not being
5422/// used to build a declaration, complain about any decl attributes
5423/// which might be lying around on it.
5424void Sema::checkUnusedDeclAttributes(Declarator &D) {
5425 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5426 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5427 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5428 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5429}
5430
Ryan Flynn7d470f32009-07-30 03:15:39 +00005431/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00005432/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00005433NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5434 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00005435 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00005436 NamedDecl *NewD = nullptr;
Ryan Flynn7d470f32009-07-30 03:15:39 +00005437 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00005438 // FIXME: Mangling?
5439 // FIXME: Is the qualifier info correct?
5440 // FIXME: Is the DeclContext correct?
Alexander Musmanf97c8932015-11-26 09:34:30 +00005441
5442 LookupResult Previous(*this, II, Loc, LookupOrdinaryName);
5443 LookupParsedName(Previous, TUScope, nullptr, true);
5444
5445 auto NewFD = FunctionDecl::Create(
5446 FD->getASTContext(), FD->getDeclContext(), Loc, Loc,
5447 DeclarationName(II), FD->getType(), FD->getTypeSourceInfo(), SC_None,
5448 false /*isInlineSpecified*/, FD->hasPrototype(),
5449 false /*isConstexprSpecified*/);
5450
5451 CheckFunctionDeclaration(TUScope, NewFD, Previous,
5452 false /*IsExplicitSpecialization*/);
5453
Eli Friedmance3e2c82011-09-07 04:05:06 +00005454 NewD = NewFD;
5455
5456 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00005457 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00005458
5459 // Fake up parameter variables; they are declared as if this were
5460 // a typedef.
5461 QualType FDTy = FD->getType();
5462 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5463 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00005464 for (const auto &AI : FT->param_types()) {
5465 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00005466 Param->setScopeInfo(0, Params.size());
5467 Params.push_back(Param);
5468 }
David Blaikie9c70e042011-09-21 18:16:56 +00005469 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00005470 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005471 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5472 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00005473 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00005474 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005475 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00005476 if (VD->getQualifier()) {
5477 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00005478 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00005479 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005480 }
5481 return NewD;
5482}
5483
James Dennett634962f2012-06-14 21:40:34 +00005484/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00005485/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00005486void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00005487 if (W.getUsed()) return; // only do this once
5488 W.setUsed(true);
5489 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5490 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00005491 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00005492 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
5493 W.getLocation()));
5494 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00005495 WeakTopLevelDecl.push_back(NewD);
5496 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5497 // to insert Decl at TU scope, sorry.
5498 DeclContext *SavedContext = CurContext;
5499 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00005500 NewD->setDeclContext(CurContext);
5501 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00005502 PushOnScopeChains(NewD, S);
5503 CurContext = SavedContext;
5504 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00005505 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005506 }
5507}
5508
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005509void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5510 // It's valid to "forward-declare" #pragma weak, in which case we
5511 // have to do this.
5512 LoadExternalWeakUndeclaredIdentifiers();
5513 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00005514 NamedDecl *ND = nullptr;
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005515 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5516 if (VD->isExternC())
5517 ND = VD;
5518 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5519 if (FD->isExternC())
5520 ND = FD;
5521 if (ND) {
5522 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00005523 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005524 if (I != WeakUndeclaredIdentifiers.end()) {
5525 WeakInfo W = I->second;
5526 DeclApplyPragmaWeak(S, ND, W);
5527 WeakUndeclaredIdentifiers[Id] = W;
5528 }
5529 }
5530 }
5531 }
5532}
5533
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005534/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5535/// it, apply them to D. This is a bit tricky because PD can have attributes
5536/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00005537void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005538 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00005539 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00005540 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005541
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005542 // Walk the declarator structure, applying decl attributes that were in a type
5543 // position to the decl itself. This handles cases like:
5544 // int *__attr__(x)** D;
5545 // when X is a decl attribute.
5546 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5547 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00005548 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005549
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005550 // Finally, apply any attributes on the decl itself.
5551 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00005552 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005553}
John McCall28a6aea2009-11-04 02:18:39 +00005554
John McCall31168b02011-06-15 23:02:42 +00005555/// Is the given declaration allowed to use a forbidden type?
John McCallb61e14e2015-10-27 04:54:50 +00005556/// If so, it'll still be annotated with an attribute that makes it
5557/// illegal to actually use.
5558static bool isForbiddenTypeAllowed(Sema &S, Decl *decl,
5559 const DelayedDiagnostic &diag,
John McCallc6af8c62015-10-28 05:03:19 +00005560 UnavailableAttr::ImplicitReason &reason) {
John McCall31168b02011-06-15 23:02:42 +00005561 // Private ivars are always okay. Unfortunately, people don't
5562 // always properly make their ivars private, even in system headers.
5563 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005564 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5565 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005566 return false;
5567
John McCallc6af8c62015-10-28 05:03:19 +00005568 // Silently accept unsupported uses of __weak in both user and system
5569 // declarations when it's been disabled, for ease of integration with
5570 // -fno-objc-arc files. We do have to take some care against attempts
5571 // to define such things; for now, we've only done that for ivars
5572 // and properties.
5573 if ((isa<ObjCIvarDecl>(decl) || isa<ObjCPropertyDecl>(decl))) {
5574 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
5575 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
5576 reason = UnavailableAttr::IR_ForbiddenWeak;
5577 return true;
5578 }
John McCallb61e14e2015-10-27 04:54:50 +00005579 }
5580
John McCallc6af8c62015-10-28 05:03:19 +00005581 // Allow all sorts of things in system headers.
5582 if (S.Context.getSourceManager().isInSystemHeader(decl->getLocation())) {
5583 // Currently, all the failures dealt with this way are due to ARC
5584 // restrictions.
5585 reason = UnavailableAttr::IR_ARCForbiddenType;
5586 return true;
John McCallb61e14e2015-10-27 04:54:50 +00005587 }
5588
5589 return false;
John McCall31168b02011-06-15 23:02:42 +00005590}
5591
5592/// Handle a delayed forbidden-type diagnostic.
5593static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5594 Decl *decl) {
John McCallc6af8c62015-10-28 05:03:19 +00005595 auto reason = UnavailableAttr::IR_None;
5596 if (decl && isForbiddenTypeAllowed(S, decl, diag, reason)) {
5597 assert(reason && "didn't set reason?");
5598 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", reason,
5599 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00005600 return;
5601 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005602 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005603 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005604 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005605 // kind of forbidden type messages on unavailable functions.
5606 if (FD->hasAttr<UnavailableAttr>() &&
5607 diag.getForbiddenTypeDiagnostic() ==
5608 diag::err_arc_array_param_no_ownership) {
5609 diag.Triggered = true;
5610 return;
5611 }
5612 }
John McCall31168b02011-06-15 23:02:42 +00005613
5614 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5615 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5616 diag.Triggered = true;
5617}
5618
Aaron Ballmanfb237522014-10-15 15:37:51 +00005619
5620static bool isDeclDeprecated(Decl *D) {
5621 do {
5622 if (D->isDeprecated())
5623 return true;
5624 // A category implicitly has the availability of the interface.
5625 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005626 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
5627 return Interface->isDeprecated();
Aaron Ballmanfb237522014-10-15 15:37:51 +00005628 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5629 return false;
5630}
5631
5632static bool isDeclUnavailable(Decl *D) {
5633 do {
5634 if (D->isUnavailable())
5635 return true;
5636 // A category implicitly has the availability of the interface.
5637 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005638 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
5639 return Interface->isUnavailable();
Aaron Ballmanfb237522014-10-15 15:37:51 +00005640 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5641 return false;
5642}
5643
Nico Weber0055a192015-03-19 19:18:22 +00005644static void DoEmitAvailabilityWarning(Sema &S, Sema::AvailabilityDiagnostic K,
Aaron Ballmanfb237522014-10-15 15:37:51 +00005645 Decl *Ctx, const NamedDecl *D,
5646 StringRef Message, SourceLocation Loc,
5647 const ObjCInterfaceDecl *UnknownObjCClass,
5648 const ObjCPropertyDecl *ObjCProperty,
5649 bool ObjCPropertyAccess) {
5650 // Diagnostics for deprecated or unavailable.
5651 unsigned diag, diag_message, diag_fwdclass_message;
John McCallb61e14e2015-10-27 04:54:50 +00005652 unsigned diag_available_here = diag::note_availability_specified_here;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005653
5654 // Matches 'diag::note_property_attribute' options.
5655 unsigned property_note_select;
5656
5657 // Matches diag::note_availability_specified_here.
5658 unsigned available_here_select_kind;
5659
5660 // Don't warn if our current context is deprecated or unavailable.
5661 switch (K) {
Nico Weber0055a192015-03-19 19:18:22 +00005662 case Sema::AD_Deprecation:
Jordan Rosed17c03e2015-04-30 17:20:35 +00005663 if (isDeclDeprecated(Ctx) || isDeclUnavailable(Ctx))
Aaron Ballmanfb237522014-10-15 15:37:51 +00005664 return;
5665 diag = !ObjCPropertyAccess ? diag::warn_deprecated
5666 : diag::warn_property_method_deprecated;
5667 diag_message = diag::warn_deprecated_message;
5668 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
5669 property_note_select = /* deprecated */ 0;
5670 available_here_select_kind = /* deprecated */ 2;
5671 break;
5672
Nico Weber0055a192015-03-19 19:18:22 +00005673 case Sema::AD_Unavailable:
Aaron Ballmanfb237522014-10-15 15:37:51 +00005674 if (isDeclUnavailable(Ctx))
5675 return;
5676 diag = !ObjCPropertyAccess ? diag::err_unavailable
5677 : diag::err_property_method_unavailable;
5678 diag_message = diag::err_unavailable_message;
5679 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
5680 property_note_select = /* unavailable */ 1;
5681 available_here_select_kind = /* unavailable */ 0;
John McCallb61e14e2015-10-27 04:54:50 +00005682
John McCallc6af8c62015-10-28 05:03:19 +00005683 if (auto attr = D->getAttr<UnavailableAttr>()) {
5684 if (attr->isImplicit() && attr->getImplicitReason()) {
5685 // Most of these failures are due to extra restrictions in ARC;
5686 // reflect that in the primary diagnostic when applicable.
5687 auto flagARCError = [&] {
5688 if (S.getLangOpts().ObjCAutoRefCount &&
5689 S.getSourceManager().isInSystemHeader(D->getLocation()))
5690 diag = diag::err_unavailable_in_arc;
5691 };
5692
5693 switch (attr->getImplicitReason()) {
5694 case UnavailableAttr::IR_None: break;
5695
5696 case UnavailableAttr::IR_ARCForbiddenType:
5697 flagARCError();
5698 diag_available_here = diag::note_arc_forbidden_type;
5699 break;
5700
5701 case UnavailableAttr::IR_ForbiddenWeak:
5702 if (S.getLangOpts().ObjCWeakRuntime)
5703 diag_available_here = diag::note_arc_weak_disabled;
5704 else
5705 diag_available_here = diag::note_arc_weak_no_runtime;
5706 break;
5707
5708 case UnavailableAttr::IR_ARCForbiddenConversion:
5709 flagARCError();
5710 diag_available_here = diag::note_performs_forbidden_arc_conversion;
5711 break;
5712
5713 case UnavailableAttr::IR_ARCInitReturnsUnrelated:
5714 flagARCError();
5715 diag_available_here = diag::note_arc_init_returns_unrelated;
5716 break;
5717
5718 case UnavailableAttr::IR_ARCFieldWithOwnership:
5719 flagARCError();
5720 diag_available_here = diag::note_arc_field_with_ownership;
5721 break;
5722 }
5723 }
John McCallb61e14e2015-10-27 04:54:50 +00005724 }
5725
Aaron Ballmanfb237522014-10-15 15:37:51 +00005726 break;
5727
Nico Weber0055a192015-03-19 19:18:22 +00005728 case Sema::AD_Partial:
5729 diag = diag::warn_partial_availability;
5730 diag_message = diag::warn_partial_message;
5731 diag_fwdclass_message = diag::warn_partial_fwdclass_message;
5732 property_note_select = /* partial */ 2;
5733 available_here_select_kind = /* partial */ 3;
5734 break;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005735 }
5736
Aaron Ballmanfb237522014-10-15 15:37:51 +00005737 if (!Message.empty()) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005738 S.Diag(Loc, diag_message) << D << Message;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005739 if (ObjCProperty)
5740 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5741 << ObjCProperty->getDeclName() << property_note_select;
5742 } else if (!UnknownObjCClass) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005743 S.Diag(Loc, diag) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005744 if (ObjCProperty)
5745 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5746 << ObjCProperty->getDeclName() << property_note_select;
5747 } else {
Aaron Ballman43f40102014-11-14 22:34:56 +00005748 S.Diag(Loc, diag_fwdclass_message) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005749 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5750 }
5751
John McCallb61e14e2015-10-27 04:54:50 +00005752 S.Diag(D->getLocation(), diag_available_here)
Aaron Ballmanfb237522014-10-15 15:37:51 +00005753 << D << available_here_select_kind;
Nico Weber0055a192015-03-19 19:18:22 +00005754 if (K == Sema::AD_Partial)
5755 S.Diag(Loc, diag::note_partial_availability_silence) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005756}
5757
5758static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
5759 Decl *Ctx) {
Nico Weber0055a192015-03-19 19:18:22 +00005760 assert(DD.Kind == DelayedDiagnostic::Deprecation ||
5761 DD.Kind == DelayedDiagnostic::Unavailable);
5762 Sema::AvailabilityDiagnostic AD = DD.Kind == DelayedDiagnostic::Deprecation
5763 ? Sema::AD_Deprecation
5764 : Sema::AD_Unavailable;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005765 DD.Triggered = true;
Nico Weber0055a192015-03-19 19:18:22 +00005766 DoEmitAvailabilityWarning(
5767 S, AD, Ctx, DD.getDeprecationDecl(), DD.getDeprecationMessage(), DD.Loc,
5768 DD.getUnknownObjCClass(), DD.getObjCProperty(), false);
Aaron Ballmanfb237522014-10-15 15:37:51 +00005769}
5770
John McCall2ec85372012-05-07 06:16:41 +00005771void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5772 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005773 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005774 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005775
John McCall2ec85372012-05-07 06:16:41 +00005776 // When delaying diagnostics to run in the context of a parsed
5777 // declaration, we only want to actually emit anything if parsing
5778 // succeeds.
5779 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005780
John McCall2ec85372012-05-07 06:16:41 +00005781 // We emit all the active diagnostics in this pool or any of its
5782 // parents. In general, we'll get one pool for the decl spec
5783 // and a child pool for each declarator; in a decl group like:
5784 // deprecated_typedef foo, *bar, baz();
5785 // only the declarator pops will be passed decls. This is correct;
5786 // we really do need to consider delayed diagnostics from the decl spec
5787 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00005788 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00005789 do {
John McCall6347b682012-05-07 06:16:58 +00005790 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00005791 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5792 // This const_cast is a bit lame. Really, Triggered should be mutable.
5793 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00005794 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00005795 continue;
5796
John McCallc1465822011-02-14 07:13:47 +00005797 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00005798 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00005799 case DelayedDiagnostic::Unavailable:
5800 // Don't bother giving deprecation/unavailable diagnostics if
5801 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00005802 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00005803 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00005804 break;
5805
5806 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00005807 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005808 break;
John McCall31168b02011-06-15 23:02:42 +00005809
5810 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00005811 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00005812 break;
John McCall86121512010-01-27 03:50:35 +00005813 }
5814 }
John McCall2ec85372012-05-07 06:16:41 +00005815 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00005816}
5817
John McCall6347b682012-05-07 06:16:58 +00005818/// Given a set of delayed diagnostics, re-emit them as if they had
5819/// been delayed in the current context instead of in the given pool.
5820/// Essentially, this just moves them to the current pool.
5821void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5822 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5823 assert(curPool && "re-emitting in undelayed context not supported");
5824 curPool->steal(pool);
5825}
5826
Ted Kremenekb79ee572013-12-18 23:30:06 +00005827void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
5828 NamedDecl *D, StringRef Message,
5829 SourceLocation Loc,
5830 const ObjCInterfaceDecl *UnknownObjCClass,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00005831 const ObjCPropertyDecl *ObjCProperty,
5832 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00005833 // Delay if we're currently parsing a declaration.
Nico Weber0055a192015-03-19 19:18:22 +00005834 if (DelayedDiagnostics.shouldDelayDiagnostics() && AD != AD_Partial) {
Nico Weber462fd1e2015-01-07 23:50:05 +00005835 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(
5836 AD, Loc, D, UnknownObjCClass, ObjCProperty, Message,
5837 ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00005838 return;
5839 }
5840
Ted Kremenekb79ee572013-12-18 23:30:06 +00005841 Decl *Ctx = cast<Decl>(getCurLexicalContext());
Nico Weber0055a192015-03-19 19:18:22 +00005842 DoEmitAvailabilityWarning(*this, AD, Ctx, D, Message, Loc, UnknownObjCClass,
5843 ObjCProperty, ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00005844}