blob: 93e9eb15b36c8486c1d59424d9f2ccc2742a2193 [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
George Burgess IV3e3bb95b2015-12-02 21:58:08 +0000812static void handlePassObjectSizeAttr(Sema &S, Decl *D,
813 const AttributeList &Attr) {
814 if (D->hasAttr<PassObjectSizeAttr>()) {
815 S.Diag(D->getLocStart(), diag::err_attribute_only_once_per_parameter)
816 << Attr.getName();
817 return;
818 }
819
820 Expr *E = Attr.getArgAsExpr(0);
821 uint32_t Type;
822 if (!checkUInt32Argument(S, Attr, E, Type, /*Idx=*/1))
823 return;
824
825 // pass_object_size's argument is passed in as the second argument of
826 // __builtin_object_size. So, it has the same constraints as that second
827 // argument; namely, it must be in the range [0, 3].
828 if (Type > 3) {
829 S.Diag(E->getLocStart(), diag::err_attribute_argument_outof_range)
830 << Attr.getName() << 0 << 3 << E->getSourceRange();
831 return;
832 }
833
834 // pass_object_size is only supported on constant pointer parameters; as a
835 // kindness to users, we allow the parameter to be non-const for declarations.
836 // At this point, we have no clue if `D` belongs to a function declaration or
837 // definition, so we defer the constness check until later.
838 if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) {
839 S.Diag(D->getLocStart(), diag::err_attribute_pointers_only)
840 << Attr.getName() << 1;
841 return;
842 }
843
844 D->addAttr(::new (S.Context)
845 PassObjectSizeAttr(Attr.getRange(), S.Context, (int)Type,
846 Attr.getAttributeSpellingListIndex()));
847}
848
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000849static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000850 ConsumableAttr::ConsumedState DefaultState;
851
852 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000853 IdentifierLoc *IL = Attr.getArgAsIdent(0);
854 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
855 DefaultState)) {
856 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
857 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000858 return;
859 }
David Blaikie16f76d22013-09-06 01:28:43 +0000860 } else {
861 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
862 << Attr.getName() << AANT_ArgumentIdentifier;
863 return;
864 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000865
866 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000867 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000868 Attr.getAttributeSpellingListIndex()));
869}
870
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000871
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000872static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
873 const AttributeList &Attr) {
874 ASTContext &CurrContext = S.getASTContext();
875 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
876
877 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
878 if (!RD->hasAttr<ConsumableAttr>()) {
879 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
880 RD->getNameAsString();
881
882 return false;
883 }
884 }
885
886 return true;
887}
888
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000889
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000890static void handleCallableWhenAttr(Sema &S, Decl *D,
891 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000892 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
893 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000894
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000895 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
896 return;
897
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000898 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
899 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
900 CallableWhenAttr::ConsumedState CallableState;
901
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000902 StringRef StateString;
903 SourceLocation Loc;
Aaron Ballman55ef1512014-12-19 16:42:04 +0000904 if (Attr.isArgIdent(ArgIndex)) {
905 IdentifierLoc *Ident = Attr.getArgAsIdent(ArgIndex);
906 StateString = Ident->Ident->getName();
907 Loc = Ident->Loc;
908 } else {
909 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
910 return;
911 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000912
913 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000914 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000915 S.Diag(Loc, diag::warn_attribute_type_not_supported)
916 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000917 return;
918 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000919
920 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000921 }
922
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000923 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000924 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
925 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000926}
927
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000928
DeLesley Hutchins69391772013-10-17 23:23:53 +0000929static void handleParamTypestateAttr(Sema &S, Decl *D,
930 const AttributeList &Attr) {
DeLesley Hutchins69391772013-10-17 23:23:53 +0000931 ParamTypestateAttr::ConsumedState ParamState;
932
933 if (Attr.isArgIdent(0)) {
934 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
935 StringRef StateString = Ident->Ident->getName();
936
937 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
938 ParamState)) {
939 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
940 << Attr.getName() << StateString;
941 return;
942 }
943 } else {
944 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
945 Attr.getName() << AANT_ArgumentIdentifier;
946 return;
947 }
948
949 // FIXME: This check is currently being done in the analysis. It can be
950 // enabled here only after the parser propagates attributes at
951 // template specialization definition, not declaration.
952 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
953 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
954 //
955 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
956 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
957 // ReturnType.getAsString();
958 // return;
959 //}
960
961 D->addAttr(::new (S.Context)
962 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
963 Attr.getAttributeSpellingListIndex()));
964}
965
966
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000967static void handleReturnTypestateAttr(Sema &S, Decl *D,
968 const AttributeList &Attr) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000969 ReturnTypestateAttr::ConsumedState ReturnState;
970
971 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000972 IdentifierLoc *IL = Attr.getArgAsIdent(0);
973 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
974 ReturnState)) {
975 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
976 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000977 return;
978 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000979 } else {
980 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
981 Attr.getName() << AANT_ArgumentIdentifier;
982 return;
983 }
984
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000985 // FIXME: This check is currently being done in the analysis. It can be
986 // enabled here only after the parser propagates attributes at
987 // template specialization definition, not declaration.
988 //QualType ReturnType;
989 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000990 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
991 // ReturnType = Param->getType();
992 //
993 //} else if (const CXXConstructorDecl *Constructor =
994 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000995 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
996 //
997 //} else {
998 //
999 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1000 //}
1001 //
1002 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1003 //
1004 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1005 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1006 // ReturnType.getAsString();
1007 // return;
1008 //}
1009
1010 D->addAttr(::new (S.Context)
1011 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1012 Attr.getAttributeSpellingListIndex()));
1013}
1014
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001015
1016static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001017 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1018 return;
1019
1020 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001021 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001022 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1023 StringRef Param = Ident->Ident->getName();
1024 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1025 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1026 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001027 return;
1028 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001029 } else {
1030 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1031 Attr.getName() << AANT_ArgumentIdentifier;
1032 return;
1033 }
1034
1035 D->addAttr(::new (S.Context)
1036 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1037 Attr.getAttributeSpellingListIndex()));
1038}
1039
Chris Wailes9385f9f2013-10-29 20:28:41 +00001040static void handleTestTypestateAttr(Sema &S, Decl *D,
1041 const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001042 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1043 return;
1044
Chris Wailes9385f9f2013-10-29 20:28:41 +00001045 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001046 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001047 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1048 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001049 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001050 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1051 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001052 return;
1053 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001054 } else {
1055 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1056 Attr.getName() << AANT_ArgumentIdentifier;
1057 return;
1058 }
1059
1060 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001061 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001062 Attr.getAttributeSpellingListIndex()));
1063}
1064
Chandler Carruthedc2c642011-07-02 00:01:44 +00001065static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1066 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001067 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001068 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001069}
1070
Chandler Carruthedc2c642011-07-02 00:01:44 +00001071static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001072 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Aaron Ballman36a53502014-01-16 13:03:14 +00001073 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context,
1074 Attr.getAttributeSpellingListIndex()));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001075 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001076 // If the alignment is less than or equal to 8 bits, the packed attribute
1077 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001078 if (!FD->getType()->isDependentType() &&
1079 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001080 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001081 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001082 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001083 else
Michael Han99315932013-01-24 16:46:58 +00001084 FD->addAttr(::new (S.Context)
1085 PackedAttr(Attr.getRange(), S.Context,
1086 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001087 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001088 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001089}
1090
Ted Kremenek7fd17232011-09-29 07:02:25 +00001091static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1092 // The IBOutlet/IBOutletCollection attributes only apply to instance
1093 // variables or properties of Objective-C classes. The outlet must also
1094 // have an object reference type.
1095 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1096 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001097 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001098 << Attr.getName() << VD->getType() << 0;
1099 return false;
1100 }
1101 }
1102 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1103 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001104 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001105 << Attr.getName() << PD->getType() << 1;
1106 return false;
1107 }
1108 }
1109 else {
1110 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1111 return false;
1112 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001113
Ted Kremenek7fd17232011-09-29 07:02:25 +00001114 return true;
1115}
1116
Chandler Carruthedc2c642011-07-02 00:01:44 +00001117static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001118 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001119 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001120
Michael Han99315932013-01-24 16:46:58 +00001121 D->addAttr(::new (S.Context)
1122 IBOutletAttr(Attr.getRange(), S.Context,
1123 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001124}
1125
Chandler Carruthedc2c642011-07-02 00:01:44 +00001126static void handleIBOutletCollection(Sema &S, Decl *D,
1127 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001128
1129 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001130 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001131 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1132 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001133 return;
1134 }
1135
Ted Kremenek7fd17232011-09-29 07:02:25 +00001136 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001137 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001138
Richard Smithb1f9a282013-10-31 01:56:18 +00001139 ParsedType PT;
1140
1141 if (Attr.hasParsedType())
1142 PT = Attr.getTypeArg();
1143 else {
1144 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1145 S.getScopeForContext(D->getDeclContext()->getParent()));
1146 if (!PT) {
1147 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1148 return;
1149 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001150 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001151
Craig Topperc3ec1492014-05-26 06:22:03 +00001152 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001153 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1154 if (!QTLoc)
1155 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001156
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001157 // Diagnose use of non-object type in iboutletcollection attribute.
1158 // FIXME. Gnu attribute extension ignores use of builtin types in
1159 // attributes. So, __attribute__((iboutletcollection(char))) will be
1160 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001161 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001162 S.Diag(Attr.getLoc(),
1163 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1164 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001165 return;
1166 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001167
Michael Han99315932013-01-24 16:46:58 +00001168 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001169 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001170 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001171}
1172
Hal Finkelee90a222014-09-26 05:04:30 +00001173bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1174 if (RefOkay) {
1175 if (T->isReferenceType())
1176 return true;
1177 } else {
1178 T = T.getNonReferenceType();
1179 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001180
Hal Finkelee90a222014-09-26 05:04:30 +00001181 // The nonnull attribute, and other similar attributes, can be applied to a
1182 // transparent union that contains a pointer type.
Richard Smith588bd9b2014-08-27 04:59:42 +00001183 if (const RecordType *UT = T->getAsUnionType()) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001184 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1185 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001186 for (const auto *I : UD->fields()) {
1187 QualType QT = I->getType();
Richard Smith588bd9b2014-08-27 04:59:42 +00001188 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1189 return true;
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001190 }
1191 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001192 }
1193
1194 return T->isAnyPointerType() || T->isBlockPointerType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001195}
1196
Ted Kremenek9aedc152014-01-17 06:24:56 +00001197static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001198 SourceRange AttrParmRange,
Hal Finkelee90a222014-09-26 05:04:30 +00001199 SourceRange TypeRange,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001200 bool isReturnValue = false) {
Hal Finkelee90a222014-09-26 05:04:30 +00001201 if (!S.isValidPointerAttrType(T)) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001202 if (isReturnValue)
1203 S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only)
1204 << Attr.getName() << AttrParmRange << TypeRange;
1205 else
1206 S.Diag(Attr.getLoc(), diag::warn_attribute_pointers_only)
1207 << Attr.getName() << AttrParmRange << TypeRange << 0;
Ted Kremenek9aedc152014-01-17 06:24:56 +00001208 return false;
1209 }
1210 return true;
1211}
1212
Chandler Carruthedc2c642011-07-02 00:01:44 +00001213static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001214 SmallVector<unsigned, 8> NonNullArgs;
Richard Smith588bd9b2014-08-27 04:59:42 +00001215 for (unsigned I = 0; I < Attr.getNumArgs(); ++I) {
1216 Expr *Ex = Attr.getArgAsExpr(I);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001217 uint64_t Idx;
Richard Smith588bd9b2014-08-27 04:59:42 +00001218 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, I + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001219 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001220
1221 // Is the function argument a pointer type?
Richard Smith588bd9b2014-08-27 04:59:42 +00001222 if (Idx < getFunctionOrMethodNumParams(D) &&
1223 !attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001224 Ex->getSourceRange(),
1225 getFunctionOrMethodParamRange(D, Idx)))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001226 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001227
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001228 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001229 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001230
1231 // If no arguments were specified to __attribute__((nonnull)) then all pointer
Richard Smith588bd9b2014-08-27 04:59:42 +00001232 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1233 // check if the attribute came from a macro expansion or a template
1234 // instantiation.
1235 if (NonNullArgs.empty() && Attr.getLoc().isFileID() &&
1236 S.ActiveTemplateInstantiations.empty()) {
1237 bool AnyPointers = isFunctionOrMethodVariadic(D);
1238 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1239 I != E && !AnyPointers; ++I) {
1240 QualType T = getFunctionOrMethodParamType(D, I);
Hal Finkelee90a222014-09-26 05:04:30 +00001241 if (T->isDependentType() || S.isValidPointerAttrType(T))
Richard Smith588bd9b2014-08-27 04:59:42 +00001242 AnyPointers = true;
Ted Kremenek5fa50522008-11-18 06:52:58 +00001243 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001244
Richard Smith588bd9b2014-08-27 04:59:42 +00001245 if (!AnyPointers)
1246 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001247 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001248
Richard Smith588bd9b2014-08-27 04:59:42 +00001249 unsigned *Start = NonNullArgs.data();
1250 unsigned Size = NonNullArgs.size();
1251 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001252 D->addAttr(::new (S.Context)
Richard Smith588bd9b2014-08-27 04:59:42 +00001253 NonNullAttr(Attr.getRange(), S.Context, Start, Size,
Michael Han99315932013-01-24 16:46:58 +00001254 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001255}
1256
Jordan Rosec9399072014-02-11 17:27:59 +00001257static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
1258 const AttributeList &Attr) {
1259 if (Attr.getNumArgs() > 0) {
1260 if (D->getFunctionType()) {
1261 handleNonNullAttr(S, D, Attr);
1262 } else {
1263 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1264 << D->getSourceRange();
1265 }
1266 return;
1267 }
1268
1269 // Is the argument a pointer type?
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001270 if (!attrNonNullArgCheck(S, D->getType(), Attr, SourceRange(),
1271 D->getSourceRange()))
Jordan Rosec9399072014-02-11 17:27:59 +00001272 return;
1273
1274 D->addAttr(::new (S.Context)
Craig Topperc3ec1492014-05-26 06:22:03 +00001275 NonNullAttr(Attr.getRange(), S.Context, nullptr, 0,
Jordan Rosec9399072014-02-11 17:27:59 +00001276 Attr.getAttributeSpellingListIndex()));
1277}
1278
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001279static void handleReturnsNonNullAttr(Sema &S, Decl *D,
1280 const AttributeList &Attr) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001281 QualType ResultType = getFunctionOrMethodResultType(D);
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001282 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1283 if (!attrNonNullArgCheck(S, ResultType, Attr, SourceRange(), SR,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001284 /* isReturnValue */ true))
1285 return;
1286
1287 D->addAttr(::new (S.Context)
1288 ReturnsNonNullAttr(Attr.getRange(), S.Context,
1289 Attr.getAttributeSpellingListIndex()));
1290}
1291
Hal Finkelee90a222014-09-26 05:04:30 +00001292static void handleAssumeAlignedAttr(Sema &S, Decl *D,
1293 const AttributeList &Attr) {
1294 Expr *E = Attr.getArgAsExpr(0),
1295 *OE = Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr;
1296 S.AddAssumeAlignedAttr(Attr.getRange(), D, E, OE,
1297 Attr.getAttributeSpellingListIndex());
1298}
1299
1300void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
1301 Expr *OE, unsigned SpellingListIndex) {
1302 QualType ResultType = getFunctionOrMethodResultType(D);
1303 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1304
1305 AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex);
1306 SourceLocation AttrLoc = AttrRange.getBegin();
1307
1308 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1309 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1310 << &TmpAttr << AttrRange << SR;
1311 return;
1312 }
1313
1314 if (!E->isValueDependent()) {
1315 llvm::APSInt I(64);
1316 if (!E->isIntegerConstantExpr(I, Context)) {
1317 if (OE)
1318 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1319 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1320 << E->getSourceRange();
1321 else
1322 Diag(AttrLoc, diag::err_attribute_argument_type)
1323 << &TmpAttr << AANT_ArgumentIntegerConstant
1324 << E->getSourceRange();
1325 return;
1326 }
1327
1328 if (!I.isPowerOf2()) {
1329 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1330 << E->getSourceRange();
1331 return;
1332 }
1333 }
1334
1335 if (OE) {
1336 if (!OE->isValueDependent()) {
1337 llvm::APSInt I(64);
1338 if (!OE->isIntegerConstantExpr(I, Context)) {
1339 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1340 << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1341 << OE->getSourceRange();
1342 return;
1343 }
1344 }
1345 }
1346
1347 D->addAttr(::new (Context)
1348 AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex));
1349}
1350
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001351/// Normalize the attribute, __foo__ becomes foo.
1352/// Returns true if normalization was applied.
1353static bool normalizeName(StringRef &AttrName) {
Aaron Ballman62692362015-10-09 13:53:24 +00001354 if (AttrName.size() > 4 && AttrName.startswith("__") &&
1355 AttrName.endswith("__")) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001356 AttrName = AttrName.drop_front(2).drop_back(2);
1357 return true;
1358 }
1359 return false;
1360}
1361
Chandler Carruthedc2c642011-07-02 00:01:44 +00001362static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001363 // This attribute must be applied to a function declaration. The first
1364 // argument to the attribute must be an identifier, the name of the resource,
1365 // for example: malloc. The following arguments must be argument indexes, the
1366 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001367 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001368 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001369 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001370
Aaron Ballman00e99962013-08-31 01:11:41 +00001371 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001372 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001373 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001374 return;
1375 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001376
Richard Smith852e9ce2013-11-27 01:46:48 +00001377 // Figure out our Kind.
1378 OwnershipAttr::OwnershipKind K =
Craig Topperc3ec1492014-05-26 06:22:03 +00001379 OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0,
Richard Smith852e9ce2013-11-27 01:46:48 +00001380 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001381
Richard Smith852e9ce2013-11-27 01:46:48 +00001382 // Check arguments.
1383 switch (K) {
1384 case OwnershipAttr::Takes:
1385 case OwnershipAttr::Holds:
1386 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001387 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1388 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001389 return;
1390 }
1391 break;
1392 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001393 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001394 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1395 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001396 return;
1397 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001398 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001399 }
1400
Richard Smith852e9ce2013-11-27 01:46:48 +00001401 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001402
Richard Smith852e9ce2013-11-27 01:46:48 +00001403 StringRef ModuleName = Module->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001404 if (normalizeName(ModuleName)) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001405 Module = &S.PP.getIdentifierTable().get(ModuleName);
1406 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001407
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001408 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001409 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1410 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001411 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001412 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001413 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001414
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001415 // Is the function argument a pointer type?
Alp Toker601b22c2014-01-21 23:35:24 +00001416 QualType T = getFunctionOrMethodParamType(D, Idx);
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001417 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001418 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001419 case OwnershipAttr::Takes:
1420 case OwnershipAttr::Holds:
1421 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1422 Err = 0;
1423 break;
1424 case OwnershipAttr::Returns:
1425 if (!T->isIntegerType())
1426 Err = 1;
1427 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001428 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001429 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001430 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001431 << Ex->getSourceRange();
1432 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001433 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001434
1435 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001436 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001437 // Cannot have two ownership attributes of different kinds for the same
1438 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001439 if (I->getOwnKind() != K && I->args_end() !=
1440 std::find(I->args_begin(), I->args_end(), Idx)) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001441 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001442 << AL.getName() << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001443 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001444 } else if (K == OwnershipAttr::Returns &&
1445 I->getOwnKind() == OwnershipAttr::Returns) {
1446 // A returns attribute conflicts with any other returns attribute using
1447 // a different index. Note, diagnostic reporting is 1-based, but stored
1448 // argument indexes are 0-based.
1449 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1450 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
1451 << *(I->args_begin()) + 1;
1452 if (I->args_size())
1453 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
1454 << (unsigned)Idx + 1 << Ex->getSourceRange();
1455 return;
1456 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001457 }
1458 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001459 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001460 }
1461
1462 unsigned* start = OwnershipArgs.data();
1463 unsigned size = OwnershipArgs.size();
1464 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001465
Michael Han99315932013-01-24 16:46:58 +00001466 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001467 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001468 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001469}
1470
Chandler Carruthedc2c642011-07-02 00:01:44 +00001471static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001472 // Check the attribute arguments.
1473 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001474 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1475 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001476 return;
1477 }
1478
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001479 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001480
Rafael Espindolac18086a2010-02-23 22:00:30 +00001481 // gcc rejects
1482 // class c {
1483 // static int a __attribute__((weakref ("v2")));
1484 // static int b() __attribute__((weakref ("f3")));
1485 // };
1486 // and ignores the attributes of
1487 // void f(void) {
1488 // static int a __attribute__((weakref ("v2")));
1489 // }
1490 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001491 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001492 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001493 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1494 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001495 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001496 }
1497
1498 // The GCC manual says
1499 //
1500 // At present, a declaration to which `weakref' is attached can only
1501 // be `static'.
1502 //
1503 // It also says
1504 //
1505 // Without a TARGET,
1506 // given as an argument to `weakref' or to `alias', `weakref' is
1507 // equivalent to `weak'.
1508 //
1509 // gcc 4.4.1 will accept
1510 // int a7 __attribute__((weakref));
1511 // as
1512 // int a7 __attribute__((weak));
1513 // This looks like a bug in gcc. We reject that for now. We should revisit
1514 // it if this behaviour is actually used.
1515
Rafael Espindolac18086a2010-02-23 22:00:30 +00001516 // GCC rejects
1517 // static ((alias ("y"), weakref)).
1518 // Should we? How to check that weakref is before or after alias?
1519
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001520 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1521 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1522 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001523 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001524 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001525 // GCC will accept anything as the argument of weakref. Should we
1526 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001527 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1528 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001529
Michael Han99315932013-01-24 16:46:58 +00001530 D->addAttr(::new (S.Context)
1531 WeakRefAttr(Attr.getRange(), S.Context,
1532 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001533}
1534
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001535static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1536 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001537 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001538 return;
1539
Douglas Gregore8bbc122011-09-02 00:18:52 +00001540 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001541 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1542 return;
1543 }
1544
David Majnemer2dc81462015-01-19 09:00:28 +00001545 // Aliases should be on declarations, not definitions.
1546 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1547 if (FD->isThisDeclarationADefinition()) {
1548 S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << FD;
1549 return;
1550 }
1551 } else {
1552 const auto *VD = cast<VarDecl>(D);
1553 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
1554 S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << VD;
1555 return;
1556 }
1557 }
1558
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001559 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001560
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001561 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001562 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001563}
1564
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001565static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001566 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr.getRange(), Attr.getName()))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001567 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001568
Michael Han99315932013-01-24 16:46:58 +00001569 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1570 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001571}
1572
1573static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001574 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr.getRange(), Attr.getName()))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001575 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001576
Michael Han99315932013-01-24 16:46:58 +00001577 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1578 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001579}
1580
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001581static void handleTLSModelAttr(Sema &S, Decl *D,
1582 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001583 StringRef Model;
1584 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001585 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001586 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001587 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001588
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001589 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001590 if (Model != "global-dynamic" && Model != "local-dynamic"
1591 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001592 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001593 return;
1594 }
1595
Michael Han99315932013-01-24 16:46:58 +00001596 D->addAttr(::new (S.Context)
1597 TLSModelAttr(Attr.getRange(), S.Context, Model,
1598 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001599}
1600
David Majnemer631a90b2015-02-04 07:23:21 +00001601static void handleRestrictAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1602 QualType ResultType = getFunctionOrMethodResultType(D);
1603 if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
1604 D->addAttr(::new (S.Context) RestrictAttr(
1605 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1606 return;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001607 }
1608
David Majnemer631a90b2015-02-04 07:23:21 +00001609 S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only)
1610 << Attr.getName() << getFunctionOrMethodResultSourceRange(D);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001611}
1612
Chandler Carruthedc2c642011-07-02 00:01:44 +00001613static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001614 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001615 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001616 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001617 return;
1618 }
1619
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001620 if (CommonAttr *CA = S.mergeCommonAttr(D, Attr.getRange(), Attr.getName(),
1621 Attr.getAttributeSpellingListIndex()))
1622 D->addAttr(CA);
Eric Christopher8a2ee392010-12-02 02:45:55 +00001623}
1624
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00001625static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1626 if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, Attr.getRange(),
1627 Attr.getName()))
1628 return;
1629
1630 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context,
1631 Attr.getAttributeSpellingListIndex()));
1632}
1633
Chandler Carruthedc2c642011-07-02 00:01:44 +00001634static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001635 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001636
1637 if (S.CheckNoReturnAttr(attr)) return;
1638
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001639 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001640 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001641 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001642 return;
1643 }
1644
Michael Han99315932013-01-24 16:46:58 +00001645 D->addAttr(::new (S.Context)
1646 NoReturnAttr(attr.getRange(), S.Context,
1647 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001648}
1649
1650bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001651 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001652 attr.setInvalid();
1653 return true;
1654 }
1655
1656 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001657}
1658
Chandler Carruthedc2c642011-07-02 00:01:44 +00001659static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1660 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001661
1662 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1663 // because 'analyzer_noreturn' does not impact the type.
David Majnemer06864812015-04-07 06:01:53 +00001664 if (!isFunctionOrMethodOrBlock(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001665 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00001666 if (!VD || (!VD->getType()->isBlockPointerType() &&
1667 !VD->getType()->isFunctionPointerType())) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001668 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001669 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Craig Topper8f7f3ea2015-11-17 05:40:05 +00001670 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001671 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001672 return;
1673 }
1674 }
1675
Michael Han99315932013-01-24 16:46:58 +00001676 D->addAttr(::new (S.Context)
1677 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1678 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001679}
1680
John Thompsoncdb847ba2010-08-09 21:53:52 +00001681// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001682static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001683/*
1684 Returning a Vector Class in Registers
1685
Eric Christopherbc638a82010-12-01 22:13:54 +00001686 According to the PPU ABI specifications, a class with a single member of
1687 vector type is returned in memory when used as the return value of a function.
1688 This results in inefficient code when implementing vector classes. To return
1689 the value in a single vector register, add the vecreturn attribute to the
1690 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001691
1692 Example:
1693
1694 struct Vector
1695 {
1696 __vector float xyzw;
1697 } __attribute__((vecreturn));
1698
1699 Vector Add(Vector lhs, Vector rhs)
1700 {
1701 Vector result;
1702 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1703 return result; // This will be returned in a register
1704 }
1705*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001706 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1707 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001708 return;
1709 }
1710
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001711 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001712 int count = 0;
1713
1714 if (!isa<CXXRecordDecl>(record)) {
1715 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1716 return;
1717 }
1718
1719 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1720 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1721 return;
1722 }
1723
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001724 for (const auto *I : record->fields()) {
1725 if ((count == 1) || !I->getType()->isVectorType()) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001726 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1727 return;
1728 }
1729 count++;
1730 }
1731
Michael Han99315932013-01-24 16:46:58 +00001732 D->addAttr(::new (S.Context)
1733 VecReturnAttr(Attr.getRange(), S.Context,
1734 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001735}
1736
Richard Smithe233fbf2013-01-28 22:42:45 +00001737static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1738 const AttributeList &Attr) {
1739 if (isa<ParmVarDecl>(D)) {
1740 // [[carries_dependency]] can only be applied to a parameter if it is a
1741 // parameter of a function declaration or lambda.
1742 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1743 S.Diag(Attr.getLoc(),
1744 diag::err_carries_dependency_param_not_function_decl);
1745 return;
1746 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001747 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001748
1749 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1750 Attr.getRange(), S.Context,
1751 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001752}
1753
Akira Hatanakac8667622015-11-06 23:56:15 +00001754static void handleNotTailCalledAttr(Sema &S, Decl *D,
1755 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001756 if (checkAttrMutualExclusion<AlwaysInlineAttr>(S, D, Attr.getRange(),
1757 Attr.getName()))
Akira Hatanakac8667622015-11-06 23:56:15 +00001758 return;
1759
1760 D->addAttr(::new (S.Context) NotTailCalledAttr(
1761 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1762}
1763
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00001764static void handleDisableTailCallsAttr(Sema &S, Decl *D,
1765 const AttributeList &Attr) {
1766 if (checkAttrMutualExclusion<NakedAttr>(S, D, Attr.getRange(),
1767 Attr.getName()))
1768 return;
1769
1770 D->addAttr(::new (S.Context) DisableTailCallsAttr(
1771 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1772}
1773
Chandler Carruthedc2c642011-07-02 00:01:44 +00001774static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001775 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001776 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001777 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001778 return;
1779 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001780 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001781 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001782 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001783 return;
1784 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001785
Michael Han99315932013-01-24 16:46:58 +00001786 D->addAttr(::new (S.Context)
1787 UsedAttr(Attr.getRange(), S.Context,
1788 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001789}
1790
Chandler Carruthedc2c642011-07-02 00:01:44 +00001791static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001792 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001793 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001794 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1795 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001796
Michael Han99315932013-01-24 16:46:58 +00001797 D->addAttr(::new (S.Context)
1798 ConstructorAttr(Attr.getRange(), S.Context, priority,
1799 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001800}
1801
Chandler Carruthedc2c642011-07-02 00:01:44 +00001802static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00001803 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001804 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001805 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1806 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001807
Michael Han99315932013-01-24 16:46:58 +00001808 D->addAttr(::new (S.Context)
1809 DestructorAttr(Attr.getRange(), S.Context, priority,
1810 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001811}
1812
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001813template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001814static void handleAttrWithMessage(Sema &S, Decl *D,
1815 const AttributeList &Attr) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001816 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001817 StringRef Str;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001818 if (Attr.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001819 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001820
Michael Han99315932013-01-24 16:46:58 +00001821 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1822 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001823}
1824
Ted Kremenek438f8db2014-02-22 01:06:05 +00001825static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Ted Kremenek28eace62013-11-23 01:01:34 +00001826 const AttributeList &Attr) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00001827 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Ted Kremenek27cfe102014-02-21 22:49:04 +00001828 S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition)
1829 << Attr.getName() << Attr.getRange();
1830 return;
1831 }
1832
Ted Kremenek28eace62013-11-23 01:01:34 +00001833 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001834 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1835 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001836}
1837
Jordy Rose740b0c22012-05-08 03:27:22 +00001838static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1839 IdentifierInfo *Platform,
1840 VersionTuple Introduced,
1841 VersionTuple Deprecated,
1842 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001843 StringRef PlatformName
1844 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1845 if (PlatformName.empty())
1846 PlatformName = Platform->getName();
1847
1848 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1849 // of these steps are needed).
1850 if (!Introduced.empty() && !Deprecated.empty() &&
1851 !(Introduced <= Deprecated)) {
1852 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1853 << 1 << PlatformName << Deprecated.getAsString()
1854 << 0 << Introduced.getAsString();
1855 return true;
1856 }
1857
1858 if (!Introduced.empty() && !Obsoleted.empty() &&
1859 !(Introduced <= Obsoleted)) {
1860 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1861 << 2 << PlatformName << Obsoleted.getAsString()
1862 << 0 << Introduced.getAsString();
1863 return true;
1864 }
1865
1866 if (!Deprecated.empty() && !Obsoleted.empty() &&
1867 !(Deprecated <= Obsoleted)) {
1868 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1869 << 2 << PlatformName << Obsoleted.getAsString()
1870 << 1 << Deprecated.getAsString();
1871 return true;
1872 }
1873
1874 return false;
1875}
1876
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001877/// \brief Check whether the two versions match.
1878///
1879/// If either version tuple is empty, then they are assumed to match. If
1880/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1881static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1882 bool BeforeIsOkay) {
1883 if (X.empty() || Y.empty())
1884 return true;
1885
1886 if (X == Y)
1887 return true;
1888
1889 if (BeforeIsOkay && X < Y)
1890 return true;
1891
1892 return false;
1893}
1894
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001895AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001896 IdentifierInfo *Platform,
1897 VersionTuple Introduced,
1898 VersionTuple Deprecated,
1899 VersionTuple Obsoleted,
1900 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001901 StringRef Message,
Douglas Gregord2a713e2015-09-30 21:27:42 +00001902 AvailabilityMergeKind AMK,
Michael Han99315932013-01-24 16:46:58 +00001903 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001904 VersionTuple MergedIntroduced = Introduced;
1905 VersionTuple MergedDeprecated = Deprecated;
1906 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001907 bool FoundAny = false;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001908 bool OverrideOrImpl = false;
1909 switch (AMK) {
1910 case AMK_None:
1911 case AMK_Redeclaration:
1912 OverrideOrImpl = false;
1913 break;
1914
1915 case AMK_Override:
1916 case AMK_ProtocolImplementation:
1917 OverrideOrImpl = true;
1918 break;
1919 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001920
Rafael Espindolac67f2232012-05-10 02:50:16 +00001921 if (D->hasAttrs()) {
1922 AttrVec &Attrs = D->getAttrs();
1923 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1924 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1925 if (!OldAA) {
1926 ++i;
1927 continue;
1928 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001929
Rafael Espindolac67f2232012-05-10 02:50:16 +00001930 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1931 if (OldPlatform != Platform) {
1932 ++i;
1933 continue;
1934 }
1935
Tim Northover7a73cc72015-10-30 16:30:49 +00001936 // If there is an existing availability attribute for this platform that
1937 // is explicit and the new one is implicit use the explicit one and
1938 // discard the new implicit attribute.
1939 if (OldAA->getRange().isValid() && Range.isInvalid()) {
1940 return nullptr;
1941 }
1942
1943 // If there is an existing attribute for this platform that is implicit
1944 // and the new attribute is explicit then erase the old one and
1945 // continue processing the attributes.
1946 if (Range.isValid() && OldAA->getRange().isInvalid()) {
1947 Attrs.erase(Attrs.begin() + i);
1948 --e;
1949 continue;
1950 }
1951
Rafael Espindolac67f2232012-05-10 02:50:16 +00001952 FoundAny = true;
1953 VersionTuple OldIntroduced = OldAA->getIntroduced();
1954 VersionTuple OldDeprecated = OldAA->getDeprecated();
1955 VersionTuple OldObsoleted = OldAA->getObsoleted();
1956 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001957
Douglas Gregord2a713e2015-09-30 21:27:42 +00001958 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
1959 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
1960 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001961 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregord2a713e2015-09-30 21:27:42 +00001962 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
1963 if (OverrideOrImpl) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001964 int Which = -1;
1965 VersionTuple FirstVersion;
1966 VersionTuple SecondVersion;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001967 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001968 Which = 0;
1969 FirstVersion = OldIntroduced;
1970 SecondVersion = Introduced;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001971 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001972 Which = 1;
1973 FirstVersion = Deprecated;
1974 SecondVersion = OldDeprecated;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001975 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001976 Which = 2;
1977 FirstVersion = Obsoleted;
1978 SecondVersion = OldObsoleted;
1979 }
1980
1981 if (Which == -1) {
1982 Diag(OldAA->getLocation(),
1983 diag::warn_mismatched_availability_override_unavail)
Douglas Gregord2a713e2015-09-30 21:27:42 +00001984 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1985 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001986 } else {
1987 Diag(OldAA->getLocation(),
1988 diag::warn_mismatched_availability_override)
1989 << Which
1990 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
Douglas Gregord2a713e2015-09-30 21:27:42 +00001991 << FirstVersion.getAsString() << SecondVersion.getAsString()
1992 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001993 }
Douglas Gregord2a713e2015-09-30 21:27:42 +00001994 if (AMK == AMK_Override)
1995 Diag(Range.getBegin(), diag::note_overridden_method);
1996 else
1997 Diag(Range.getBegin(), diag::note_protocol_method);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001998 } else {
1999 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2000 Diag(Range.getBegin(), diag::note_previous_attribute);
2001 }
2002
Rafael Espindolac67f2232012-05-10 02:50:16 +00002003 Attrs.erase(Attrs.begin() + i);
2004 --e;
2005 continue;
2006 }
2007
2008 VersionTuple MergedIntroduced2 = MergedIntroduced;
2009 VersionTuple MergedDeprecated2 = MergedDeprecated;
2010 VersionTuple MergedObsoleted2 = MergedObsoleted;
2011
2012 if (MergedIntroduced2.empty())
2013 MergedIntroduced2 = OldIntroduced;
2014 if (MergedDeprecated2.empty())
2015 MergedDeprecated2 = OldDeprecated;
2016 if (MergedObsoleted2.empty())
2017 MergedObsoleted2 = OldObsoleted;
2018
2019 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2020 MergedIntroduced2, MergedDeprecated2,
2021 MergedObsoleted2)) {
2022 Attrs.erase(Attrs.begin() + i);
2023 --e;
2024 continue;
2025 }
2026
2027 MergedIntroduced = MergedIntroduced2;
2028 MergedDeprecated = MergedDeprecated2;
2029 MergedObsoleted = MergedObsoleted2;
2030 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002031 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002032 }
2033
2034 if (FoundAny &&
2035 MergedIntroduced == Introduced &&
2036 MergedDeprecated == Deprecated &&
2037 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00002038 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002039
Douglas Gregord2a713e2015-09-30 21:27:42 +00002040 // Only create a new attribute if !OverrideOrImpl, but we want to do
Ted Kremenekb5445722013-04-06 00:34:27 +00002041 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002042 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002043 MergedDeprecated, MergedObsoleted) &&
Douglas Gregord2a713e2015-09-30 21:27:42 +00002044 !OverrideOrImpl) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002045 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2046 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002047 Obsoleted, IsUnavailable, Message,
2048 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002049 }
Craig Topperc3ec1492014-05-26 06:22:03 +00002050 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002051}
2052
Chandler Carruthedc2c642011-07-02 00:01:44 +00002053static void handleAvailabilityAttr(Sema &S, Decl *D,
2054 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002055 if (!checkAttributeNumArgs(S, Attr, 1))
2056 return;
2057 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00002058 unsigned Index = Attr.getAttributeSpellingListIndex();
2059
Aaron Ballman00e99962013-08-31 01:11:41 +00002060 IdentifierInfo *II = Platform->Ident;
2061 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2062 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2063 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002064
Rafael Espindolac231fab2013-01-08 21:30:32 +00002065 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2066 if (!ND) {
2067 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2068 return;
2069 }
2070
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002071 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2072 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2073 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002074 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002075 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00002076 if (const StringLiteral *SE =
2077 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002078 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002079
Aaron Ballman00e99962013-08-31 01:11:41 +00002080 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002081 Introduced.Version,
2082 Deprecated.Version,
2083 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002084 IsUnavailable, Str,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002085 Sema::AMK_None,
Michael Han99315932013-01-24 16:46:58 +00002086 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002087 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002088 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002089
2090 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2091 // matches before the start of the watchOS platform.
2092 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2093 IdentifierInfo *NewII = nullptr;
2094 if (II->getName() == "ios")
2095 NewII = &S.Context.Idents.get("watchos");
2096 else if (II->getName() == "ios_app_extension")
2097 NewII = &S.Context.Idents.get("watchos_app_extension");
2098
2099 if (NewII) {
2100 auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple {
2101 if (Version.empty())
2102 return Version;
2103 auto Major = Version.getMajor();
2104 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2105 if (NewMajor >= 2) {
2106 if (Version.getMinor().hasValue()) {
2107 if (Version.getSubminor().hasValue())
2108 return VersionTuple(NewMajor, Version.getMinor().getValue(),
2109 Version.getSubminor().getValue());
2110 else
2111 return VersionTuple(NewMajor, Version.getMinor().getValue());
2112 }
2113 }
2114
2115 return VersionTuple(2, 0);
2116 };
2117
2118 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2119 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2120 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2121
2122 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
2123 SourceRange(),
2124 NewII,
2125 NewIntroduced,
2126 NewDeprecated,
2127 NewObsoleted,
2128 IsUnavailable, Str,
2129 Sema::AMK_None,
2130 Index);
2131 if (NewAttr)
2132 D->addAttr(NewAttr);
2133 }
2134 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2135 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2136 // matches before the start of the tvOS platform.
2137 IdentifierInfo *NewII = nullptr;
2138 if (II->getName() == "ios")
2139 NewII = &S.Context.Idents.get("tvos");
2140 else if (II->getName() == "ios_app_extension")
2141 NewII = &S.Context.Idents.get("tvos_app_extension");
2142
2143 if (NewII) {
2144 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
2145 SourceRange(),
2146 NewII,
2147 Introduced.Version,
2148 Deprecated.Version,
2149 Obsoleted.Version,
2150 IsUnavailable, Str,
2151 Sema::AMK_None,
2152 Index);
2153 if (NewAttr)
2154 D->addAttr(NewAttr);
2155 }
2156 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002157}
2158
John McCalld041a9b2013-02-20 01:54:26 +00002159template <class T>
2160static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2161 typename T::VisibilityType value,
2162 unsigned attrSpellingListIndex) {
2163 T *existingAttr = D->getAttr<T>();
2164 if (existingAttr) {
2165 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2166 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00002167 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00002168 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2169 S.Diag(range.getBegin(), diag::note_previous_attribute);
2170 D->dropAttr<T>();
2171 }
2172 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2173}
2174
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002175VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002176 VisibilityAttr::VisibilityType Vis,
2177 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002178 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2179 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002180}
2181
John McCalld041a9b2013-02-20 01:54:26 +00002182TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2183 TypeVisibilityAttr::VisibilityType Vis,
2184 unsigned AttrSpellingListIndex) {
2185 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2186 AttrSpellingListIndex);
2187}
2188
2189static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2190 bool isTypeVisibility) {
2191 // Visibility attributes don't mean anything on a typedef.
2192 if (isa<TypedefNameDecl>(D)) {
2193 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2194 << Attr.getName();
2195 return;
2196 }
2197
2198 // 'type_visibility' can only go on a type or namespace.
2199 if (isTypeVisibility &&
2200 !(isa<TagDecl>(D) ||
2201 isa<ObjCInterfaceDecl>(D) ||
2202 isa<NamespaceDecl>(D))) {
2203 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2204 << Attr.getName() << ExpectedTypeOrNamespace;
2205 return;
2206 }
2207
Benjamin Kramer70370212013-09-09 15:08:57 +00002208 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002209 StringRef TypeStr;
2210 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002211 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002212 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002213
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002214 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002215 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002216 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002217 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002218 return;
2219 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002220
2221 // Complain about attempts to use protected visibility on targets
2222 // (like Darwin) that don't support it.
2223 if (type == VisibilityAttr::Protected &&
2224 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2225 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2226 type = VisibilityAttr::Default;
2227 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002228
Michael Han99315932013-01-24 16:46:58 +00002229 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002230 clang::Attr *newAttr;
2231 if (isTypeVisibility) {
2232 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2233 (TypeVisibilityAttr::VisibilityType) type,
2234 Index);
2235 } else {
2236 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2237 }
2238 if (newAttr)
2239 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002240}
2241
Chandler Carruthedc2c642011-07-02 00:01:44 +00002242static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2243 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002244 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002245 if (!Attr.isArgIdent(0)) {
2246 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2247 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002248 return;
2249 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002250
Aaron Ballman682ee422013-09-11 19:47:58 +00002251 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2252 ObjCMethodFamilyAttr::FamilyKind F;
2253 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2254 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2255 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002256 return;
2257 }
2258
Alp Toker314cc812014-01-25 16:55:45 +00002259 if (F == ObjCMethodFamilyAttr::OMF_init &&
2260 !method->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00002261 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
Alp Toker314cc812014-01-25 16:55:45 +00002262 << method->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002263 // Ignore the attribute.
2264 return;
2265 }
2266
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002267 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00002268 S.Context, F,
2269 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002270}
2271
Chandler Carruthedc2c642011-07-02 00:01:44 +00002272static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002273 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002274 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002275 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002276 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2277 return;
2278 }
2279 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002280 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2281 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002282 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002283 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2284 return;
2285 }
2286 }
2287 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002288 // It is okay to include this attribute on properties, e.g.:
2289 //
2290 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2291 //
2292 // In this case it follows tradition and suppresses an error in the above
2293 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002294 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002295 }
Michael Han99315932013-01-24 16:46:58 +00002296 D->addAttr(::new (S.Context)
2297 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2298 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002299}
2300
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002301static void handleObjCIndependentClass(Sema &S, Decl *D, const AttributeList &Attr) {
2302 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
2303 QualType T = TD->getUnderlyingType();
2304 if (!T->isObjCObjectPointerType()) {
2305 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2306 return;
2307 }
2308 } else {
2309 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2310 return;
2311 }
2312 D->addAttr(::new (S.Context)
2313 ObjCIndependentClassAttr(Attr.getRange(), S.Context,
2314 Attr.getAttributeSpellingListIndex()));
2315}
2316
Chandler Carruthedc2c642011-07-02 00:01:44 +00002317static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002318 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002319 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002320 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002321 return;
2322 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002323
Aaron Ballman00e99962013-08-31 01:11:41 +00002324 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002325 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002326 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2327 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2328 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002329 return;
2330 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002331
Michael Han99315932013-01-24 16:46:58 +00002332 D->addAttr(::new (S.Context)
2333 BlocksAttr(Attr.getRange(), S.Context, type,
2334 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002335}
2336
Chandler Carruthedc2c642011-07-02 00:01:44 +00002337static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002338 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002339 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002340 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002341 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002342 if (E->isTypeDependent() || E->isValueDependent() ||
2343 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002344 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002345 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002346 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002347 return;
2348 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002349
John McCallb46f2872011-09-09 07:56:05 +00002350 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002351 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2352 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002353 return;
2354 }
John McCallb46f2872011-09-09 07:56:05 +00002355
2356 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002357 }
2358
Aaron Ballman18a78382013-11-21 00:28:23 +00002359 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002360 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002361 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002362 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002363 if (E->isTypeDependent() || E->isValueDependent() ||
2364 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002365 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002366 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002367 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002368 return;
2369 }
2370 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002371
John McCallb46f2872011-09-09 07:56:05 +00002372 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002373 // FIXME: This error message could be improved, it would be nice
2374 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002375 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2376 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002377 return;
2378 }
2379 }
2380
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002381 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002382 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002383 if (isa<FunctionNoProtoType>(FT)) {
2384 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2385 return;
2386 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002387
Chris Lattner9363e312009-03-17 23:03:47 +00002388 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002389 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002390 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002391 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002392 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002393 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002394 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002395 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002396 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002397 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2398 if (!BD->isVariadic()) {
2399 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2400 return;
2401 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002402 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002403 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002404 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002405 const FunctionType *FT = Ty->isFunctionPointerType()
2406 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002407 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002408 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002409 int m = Ty->isFunctionPointerType() ? 0 : 1;
2410 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002411 return;
2412 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002413 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002414 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002415 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002416 return;
2417 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002418 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002419 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002420 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002421 return;
2422 }
Michael Han99315932013-01-24 16:46:58 +00002423 D->addAttr(::new (S.Context)
2424 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2425 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002426}
2427
Chandler Carruthedc2c642011-07-02 00:01:44 +00002428static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Alp Toker314cc812014-01-25 16:55:45 +00002429 if (D->getFunctionType() &&
2430 D->getFunctionType()->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002431 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2432 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002433 return;
2434 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002435 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002436 if (MD->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002437 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2438 << Attr.getName() << 1;
2439 return;
2440 }
2441
Michael Han99315932013-01-24 16:46:58 +00002442 D->addAttr(::new (S.Context)
2443 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2444 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002445}
2446
Chandler Carruthedc2c642011-07-02 00:01:44 +00002447static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002448 // weak_import only applies to variable & function declarations.
2449 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002450 if (!D->canBeWeakImported(isDef)) {
2451 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002452 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2453 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002454 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002455 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002456 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002457 // Nothing to warn about here.
2458 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002459 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002460 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002461
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002462 return;
2463 }
2464
Michael Han99315932013-01-24 16:46:58 +00002465 D->addAttr(::new (S.Context)
2466 WeakImportAttr(Attr.getRange(), S.Context,
2467 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002468}
2469
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002470// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002471template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002472static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002473 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002474 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002475 for (unsigned i = 0; i < 3; ++i) {
2476 const Expr *E = Attr.getArgAsExpr(i);
2477 if (!checkUInt32Argument(S, Attr, E, WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002478 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002479 if (WGSize[i] == 0) {
2480 S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero)
2481 << Attr.getName() << E->getSourceRange();
2482 return;
2483 }
2484 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002485
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002486 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2487 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2488 Existing->getYDim() == WGSize[1] &&
2489 Existing->getZDim() == WGSize[2]))
2490 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002491
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002492 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2493 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002494 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002495}
2496
Joey Goulyaba589c2013-03-08 09:42:32 +00002497static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002498 if (!Attr.hasParsedType()) {
2499 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2500 << Attr.getName() << 1;
2501 return;
2502 }
2503
Craig Topperc3ec1492014-05-26 06:22:03 +00002504 TypeSourceInfo *ParmTSI = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00002505 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2506 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002507
2508 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2509 (ParmType->isBooleanType() ||
2510 !ParmType->isIntegralType(S.getASTContext()))) {
2511 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2512 << ParmType;
2513 return;
2514 }
2515
Aaron Ballmana9e05402013-12-02 22:16:55 +00002516 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002517 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002518 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2519 return;
2520 }
2521 }
2522
2523 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002524 ParmTSI,
2525 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002526}
2527
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002528SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002529 StringRef Name,
2530 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002531 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2532 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002533 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002534 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2535 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002536 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002537 }
Michael Han99315932013-01-24 16:46:58 +00002538 return ::new (Context) SectionAttr(Range, Context, Name,
2539 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002540}
2541
Reid Kleckner2a133222015-03-04 23:39:17 +00002542bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2543 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2544 if (!Error.empty()) {
2545 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error;
2546 return false;
2547 }
2548 return true;
2549}
2550
Chandler Carruthedc2c642011-07-02 00:01:44 +00002551static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002552 // Make sure that there is a string literal as the sections's single
2553 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002554 StringRef Str;
2555 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002556 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002557 return;
Mike Stump11289f42009-09-09 15:08:12 +00002558
Reid Kleckner2a133222015-03-04 23:39:17 +00002559 if (!S.checkSectionName(LiteralLoc, Str))
2560 return;
2561
Chris Lattner30ba6742009-08-10 19:03:04 +00002562 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002563 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002564 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002565 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002566 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002567 return;
2568 }
Mike Stump11289f42009-09-09 15:08:12 +00002569
Michael Han99315932013-01-24 16:46:58 +00002570 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002571 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002572 if (NewAttr)
2573 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002574}
2575
Eric Christopher789a7ad2015-06-12 01:36:05 +00002576// Check for things we'd like to warn about, no errors or validation for now.
2577// TODO: Validation should use a backend target library that specifies
2578// the allowable subtarget features and cpus. We could use something like a
2579// TargetCodeGenInfo hook here to do validation.
2580void Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
2581 for (auto Str : {"tune=", "fpmath="})
2582 if (AttrStr.find(Str) != StringRef::npos)
2583 Diag(LiteralLoc, diag::warn_unsupported_target_attribute) << Str;
2584}
2585
Eric Christopher11acf732015-06-12 01:35:52 +00002586static void handleTargetAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eric Christopher11acf732015-06-12 01:35:52 +00002587 StringRef Str;
2588 SourceLocation LiteralLoc;
2589 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
2590 return;
Eric Christopher789a7ad2015-06-12 01:36:05 +00002591 S.checkTargetAttr(LiteralLoc, Str);
Eric Christopher11acf732015-06-12 01:35:52 +00002592 unsigned Index = Attr.getAttributeSpellingListIndex();
2593 TargetAttr *NewAttr =
2594 ::new (S.Context) TargetAttr(Attr.getRange(), S.Context, Str, Index);
2595 D->addAttr(NewAttr);
2596}
2597
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002598
Chandler Carruthedc2c642011-07-02 00:01:44 +00002599static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002600 VarDecl *VD = cast<VarDecl>(D);
2601 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002602 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002603 return;
2604 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002605
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002606 Expr *E = Attr.getArgAsExpr(0);
2607 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00002608 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002609 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002610
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002611 // gcc only allows for simple identifiers. Since we support more than gcc, we
2612 // will warn the user.
2613 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2614 if (DRE->hasQualifier())
2615 S.Diag(Loc, diag::warn_cleanup_ext);
2616 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2617 NI = DRE->getNameInfo();
2618 if (!FD) {
2619 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2620 << NI.getName();
2621 return;
2622 }
2623 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2624 if (ULE->hasExplicitTemplateArgs())
2625 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002626 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2627 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002628 if (!FD) {
2629 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2630 << NI.getName();
2631 if (ULE->getType() == S.Context.OverloadTy)
2632 S.NoteAllOverloadCandidates(ULE);
2633 return;
2634 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002635 } else {
2636 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002637 return;
2638 }
2639
Anders Carlssond277d792009-01-31 01:16:18 +00002640 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002641 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2642 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002643 return;
2644 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002645
Anders Carlsson723f55d2009-02-07 23:16:50 +00002646 // We're currently more strict than GCC about what function types we accept.
2647 // If this ever proves to be a problem it should be easy to fix.
2648 QualType Ty = S.Context.getPointerType(VD->getType());
2649 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002650 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2651 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002652 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2653 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002654 return;
2655 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002656
Michael Han99315932013-01-24 16:46:58 +00002657 D->addAttr(::new (S.Context)
2658 CleanupAttr(Attr.getRange(), S.Context, FD,
2659 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002660}
2661
Mike Stumpd3bb5572009-07-24 19:02:52 +00002662/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002663/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002664static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002665 Expr *IdxExpr = Attr.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00002666 uint64_t Idx;
2667 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002668 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002669
Eric Christopherb64963e2015-08-13 21:34:35 +00002670 // Make sure the format string is really a string.
Alp Toker601b22c2014-01-21 23:35:24 +00002671 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002672
Eric Christopherb64963e2015-08-13 21:34:35 +00002673 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
2674 if (NotNSStringTy &&
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002675 !isCFStringType(Ty, S.Context) &&
2676 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002677 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002678 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00002679 << "a string type" << IdxExpr->getSourceRange()
2680 << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002681 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002682 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002683 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002684 if (!isNSStringType(Ty, S.Context) &&
2685 !isCFStringType(Ty, S.Context) &&
2686 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002687 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002688 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00002689 << (NotNSStringTy ? "string type" : "NSString")
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00002690 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002691 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002692 }
2693
Alp Toker601b22c2014-01-21 23:35:24 +00002694 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002695 // because that has corrected for the implicit this parameter, and is zero-
2696 // based. The attribute expects what the user wrote explicitly.
2697 llvm::APSInt Val;
2698 IdxExpr->EvaluateAsInt(Val, S.Context);
2699
Michael Han99315932013-01-24 16:46:58 +00002700 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002701 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002702 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002703}
2704
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002705enum FormatAttrKind {
2706 CFStringFormat,
2707 NSStringFormat,
2708 StrftimeFormat,
2709 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002710 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002711 InvalidFormat
2712};
2713
2714/// getFormatAttrKind - Map from format attribute names to supported format
2715/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002716static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002717 return llvm::StringSwitch<FormatAttrKind>(Format)
2718 // Check for formats that get handled specially.
2719 .Case("NSString", NSStringFormat)
2720 .Case("CFString", CFStringFormat)
2721 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002722
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002723 // Otherwise, check for supported formats.
2724 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2725 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2726 .Case("kprintf", SupportedFormat) // OpenBSD.
Dimitry Andric6b5ed342015-02-19 22:32:33 +00002727 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
Fariborz Jahanianf8dce0f2015-02-21 00:45:58 +00002728 .Case("os_trace", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002729
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002730 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2731 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002732}
2733
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002734/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002735/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002736static void handleInitPriorityAttr(Sema &S, Decl *D,
2737 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002738 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002739 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2740 return;
2741 }
2742
Aaron Ballman4a611152013-11-27 16:34:09 +00002743 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002744 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2745 Attr.setInvalid();
2746 return;
2747 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002748 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002749 if (S.Context.getAsArrayType(T))
2750 T = S.Context.getBaseElementType(T);
2751 if (!T->getAs<RecordType>()) {
2752 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2753 Attr.setInvalid();
2754 return;
2755 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002756
2757 Expr *E = Attr.getArgAsExpr(0);
2758 uint32_t prioritynum;
2759 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002760 Attr.setInvalid();
2761 return;
2762 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002763
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002764 if (prioritynum < 101 || prioritynum > 65535) {
2765 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00002766 << E->getSourceRange() << Attr.getName() << 101 << 65535;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002767 Attr.setInvalid();
2768 return;
2769 }
Michael Han99315932013-01-24 16:46:58 +00002770 D->addAttr(::new (S.Context)
2771 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2772 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002773}
2774
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002775FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2776 IdentifierInfo *Format, int FormatIdx,
2777 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002778 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002779 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002780 for (auto *F : D->specific_attrs<FormatAttr>()) {
2781 if (F->getType() == Format &&
2782 F->getFormatIdx() == FormatIdx &&
2783 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002784 // If we don't have a valid location for this attribute, adopt the
2785 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002786 if (F->getLocation().isInvalid())
2787 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00002788 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00002789 }
2790 }
2791
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002792 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2793 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002794}
2795
Mike Stumpd3bb5572009-07-24 19:02:52 +00002796/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002797/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002798static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002799 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002800 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002801 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002802 return;
2803 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002804
Chandler Carruth743682b2010-11-16 08:35:43 +00002805 // In C++ the implicit 'this' function parameter also counts, and they are
2806 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002807 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00002808 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002809
Aaron Ballman00e99962013-08-31 01:11:41 +00002810 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2811 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002812
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00002813 if (normalizeName(Format)) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002814 // If we've modified the string name, we need a new identifier for it.
2815 II = &S.Context.Idents.get(Format);
2816 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002817
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002818 // Check for supported formats.
2819 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002820
2821 if (Kind == IgnoredFormat)
2822 return;
2823
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002824 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002825 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002826 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002827 return;
2828 }
2829
2830 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002831 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002832 uint32_t Idx;
2833 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002834 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002835
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002836 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002837 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002838 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002839 return;
2840 }
2841
2842 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002843 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002844
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002845 if (HasImplicitThisParam) {
2846 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002847 S.Diag(Attr.getLoc(),
2848 diag::err_format_attribute_implicit_this_format_string)
2849 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002850 return;
2851 }
2852 ArgIdx--;
2853 }
Mike Stump11289f42009-09-09 15:08:12 +00002854
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002855 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002856 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002857
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002858 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002859 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002860 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002861 << "a CFString" << IdxExpr->getSourceRange()
2862 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00002863 return;
2864 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002865 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002866 // FIXME: do we need to check if the type is NSString*? What are the
2867 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002868 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002869 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002870 << "an NSString" << IdxExpr->getSourceRange()
2871 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002872 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002873 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002874 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002875 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002876 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002877 << "a string type" << IdxExpr->getSourceRange()
2878 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002879 return;
2880 }
2881
2882 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002883 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002884 uint32_t FirstArg;
2885 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002886 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002887
2888 // check if the function is variadic if the 3rd argument non-zero
2889 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002890 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002891 ++NumArgs; // +1 for ...
2892 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002893 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002894 return;
2895 }
2896 }
2897
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002898 // strftime requires FirstArg to be 0 because it doesn't read from any
2899 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002900 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002901 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002902 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2903 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002904 return;
2905 }
2906 // if 0 it disables parameter checking (to use with e.g. va_list)
2907 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002908 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002909 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002910 return;
2911 }
2912
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002913 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002914 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002915 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002916 if (NewAttr)
2917 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002918}
2919
Chandler Carruthedc2c642011-07-02 00:01:44 +00002920static void handleTransparentUnionAttr(Sema &S, Decl *D,
2921 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002922 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00002923 RecordDecl *RD = nullptr;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002924 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002925 if (TD && TD->getUnderlyingType()->isUnionType())
2926 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2927 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002928 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002929
2930 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002931 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002932 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002933 return;
2934 }
2935
John McCallf937c022011-10-07 06:10:15 +00002936 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002937 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002938 diag::warn_transparent_union_attribute_not_definition);
2939 return;
2940 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002941
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002942 RecordDecl::field_iterator Field = RD->field_begin(),
2943 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002944 if (Field == FieldEnd) {
2945 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2946 return;
2947 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002948
David Blaikie40ed2972012-06-06 20:45:41 +00002949 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002950 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002951 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002952 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002953 diag::warn_transparent_union_attribute_floating)
2954 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002955 return;
2956 }
2957
2958 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2959 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2960 for (; Field != FieldEnd; ++Field) {
2961 QualType FieldType = Field->getType();
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002962 // FIXME: this isn't fully correct; we also need to test whether the
2963 // members of the union would all have the same calling convention as the
2964 // first member of the union. Checking just the size and alignment isn't
2965 // sufficient (consider structs passed on the stack instead of in registers
2966 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002967 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002968 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002969 // Warn if we drop the attribute.
2970 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002971 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002972 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002973 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002974 diag::warn_transparent_union_attribute_field_size_align)
2975 << isSize << Field->getDeclName() << FieldBits;
2976 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002977 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002978 diag::note_transparent_union_first_field_size_align)
2979 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002980 return;
2981 }
2982 }
2983
Michael Han99315932013-01-24 16:46:58 +00002984 RD->addAttr(::new (S.Context)
2985 TransparentUnionAttr(Attr.getRange(), S.Context,
2986 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002987}
2988
Chandler Carruthedc2c642011-07-02 00:01:44 +00002989static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002990 // Make sure that there is a string literal as the annotation's single
2991 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002992 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002993 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002994 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002995
2996 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002997 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
2998 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002999 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003000 }
Michael Han99315932013-01-24 16:46:58 +00003001
3002 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003003 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00003004 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003005}
3006
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003007static void handleAlignValueAttr(Sema &S, Decl *D,
3008 const AttributeList &Attr) {
3009 S.AddAlignValueAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
3010 Attr.getAttributeSpellingListIndex());
3011}
3012
3013void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
3014 unsigned SpellingListIndex) {
3015 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
3016 SourceLocation AttrLoc = AttrRange.getBegin();
3017
3018 QualType T;
3019 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3020 T = TD->getUnderlyingType();
3021 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3022 T = VD->getType();
3023 else
3024 llvm_unreachable("Unknown decl type for align_value");
3025
3026 if (!T->isDependentType() && !T->isAnyPointerType() &&
3027 !T->isReferenceType() && !T->isMemberPointerType()) {
3028 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
3029 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
3030 return;
3031 }
3032
3033 if (!E->isValueDependent()) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003034 llvm::APSInt Alignment;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003035 ExprResult ICE
3036 = VerifyIntegerConstantExpression(E, &Alignment,
3037 diag::err_align_value_attribute_argument_not_int,
3038 /*AllowFold*/ false);
3039 if (ICE.isInvalid())
3040 return;
3041
3042 if (!Alignment.isPowerOf2()) {
3043 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3044 << E->getSourceRange();
3045 return;
3046 }
3047
3048 D->addAttr(::new (Context)
3049 AlignValueAttr(AttrRange, Context, ICE.get(),
3050 SpellingListIndex));
3051 return;
3052 }
3053
3054 // Save dependent expressions in the AST to be instantiated.
3055 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
3056 return;
3057}
3058
Chandler Carruthedc2c642011-07-02 00:01:44 +00003059static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003060 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003061 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003062 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3063 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003064 return;
3065 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003066
Richard Smith848e1f12013-02-01 08:12:08 +00003067 if (Attr.getNumArgs() == 0) {
3068 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
Craig Topperc3ec1492014-05-26 06:22:03 +00003069 true, nullptr, Attr.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00003070 return;
3071 }
3072
Aaron Ballman00e99962013-08-31 01:11:41 +00003073 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00003074 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3075 S.Diag(Attr.getEllipsisLoc(),
3076 diag::err_pack_expansion_without_parameter_packs);
3077 return;
3078 }
3079
3080 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3081 return;
3082
David Majnemer26a1e0e2015-04-07 02:37:09 +00003083 if (E->isValueDependent()) {
3084 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3085 if (!TND->getUnderlyingType()->isDependentType()) {
3086 S.Diag(Attr.getLoc(), diag::err_alignment_dependent_typedef_name)
3087 << E->getSourceRange();
3088 return;
3089 }
3090 }
3091 }
3092
Richard Smith44c247f2013-02-22 08:32:16 +00003093 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3094 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003095}
3096
3097void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003098 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003099 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3100 SourceLocation AttrLoc = AttrRange.getBegin();
3101
Richard Smith1dba27c2013-01-29 09:02:09 +00003102 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003103 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003104 // C++11 [dcl.align]p1:
3105 // An alignment-specifier may be applied to a variable or to a class
3106 // data member, but it shall not be applied to a bit-field, a function
3107 // parameter, the formal parameter of a catch clause, or a variable
3108 // declared with the register storage class specifier. An
3109 // alignment-specifier may also be applied to the declaration of a class
3110 // or enumeration type.
3111 // C11 6.7.5/2:
3112 // An alignment attribute shall not be specified in a declaration of
3113 // a typedef, or a bit-field, or a function, or a parameter, or an
3114 // object declared with the register storage-class specifier.
3115 int DiagKind = -1;
3116 if (isa<ParmVarDecl>(D)) {
3117 DiagKind = 0;
3118 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3119 if (VD->getStorageClass() == SC_Register)
3120 DiagKind = 1;
3121 if (VD->isExceptionVariable())
3122 DiagKind = 2;
3123 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3124 if (FD->isBitField())
3125 DiagKind = 3;
3126 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00003127 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00003128 << (TmpAttr.isC11() ? ExpectedVariableOrField
3129 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003130 return;
3131 }
3132 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003133 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00003134 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003135 return;
3136 }
3137 }
3138
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003139 if (E->isTypeDependent() || E->isValueDependent()) {
3140 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003141 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3142 AA->setPackExpansion(IsPackExpansion);
3143 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003144 return;
3145 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003146
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003147 // FIXME: Cache the number on the Attr object?
David Majnemer0be6bd02015-07-26 09:02:21 +00003148 llvm::APSInt Alignment;
Douglas Gregore2b37442012-05-04 22:38:52 +00003149 ExprResult ICE
3150 = VerifyIntegerConstantExpression(E, &Alignment,
3151 diag::err_aligned_attribute_argument_not_int,
3152 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003153 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003154 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003155
David Majnemer0be6bd02015-07-26 09:02:21 +00003156 uint64_t AlignVal = Alignment.getZExtValue();
3157
Richard Smith848e1f12013-02-01 08:12:08 +00003158 // C++11 [dcl.align]p2:
3159 // -- if the constant expression evaluates to zero, the alignment
3160 // specifier shall have no effect
3161 // C11 6.7.5p6:
3162 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003163 if (!(TmpAttr.isAlignas() && !Alignment)) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003164 if (!llvm::isPowerOf2_64(AlignVal)) {
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003165 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3166 << E->getSourceRange();
3167 return;
3168 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003169 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003170
David Majnemerabecae72014-02-12 20:36:10 +00003171 // Alignment calculations can wrap around if it's greater than 2**28.
David Majnemer29c69db2015-07-26 01:48:59 +00003172 unsigned MaxValidAlignment =
3173 Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192
3174 : 268435456;
David Majnemer0be6bd02015-07-26 09:02:21 +00003175 if (AlignVal > MaxValidAlignment) {
David Majnemerabecae72014-02-12 20:36:10 +00003176 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
3177 << E->getSourceRange();
3178 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003179 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003180
David Majnemer0be6bd02015-07-26 09:02:21 +00003181 if (Context.getTargetInfo().isTLSSupported()) {
3182 unsigned MaxTLSAlign =
3183 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3184 .getQuantity();
3185 auto *VD = dyn_cast<VarDecl>(D);
3186 if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD &&
3187 VD->getTLSKind() != VarDecl::TLS_None) {
3188 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3189 << (unsigned)AlignVal << VD << MaxTLSAlign;
3190 return;
3191 }
3192 }
3193
Richard Smith44c247f2013-02-22 08:32:16 +00003194 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003195 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00003196 AA->setPackExpansion(IsPackExpansion);
3197 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003198}
3199
Michael Hanaf02bbe2013-02-01 01:19:17 +00003200void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003201 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003202 // FIXME: Cache the number on the Attr object if non-dependent?
3203 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003204 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3205 SpellingListIndex);
3206 AA->setPackExpansion(IsPackExpansion);
3207 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003208}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003209
Richard Smith848e1f12013-02-01 08:12:08 +00003210void Sema::CheckAlignasUnderalignment(Decl *D) {
3211 assert(D->hasAttrs() && "no attributes on decl");
3212
David Majnemer475b25e2015-01-21 10:54:38 +00003213 QualType UnderlyingTy, DiagTy;
3214 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
3215 UnderlyingTy = DiagTy = VD->getType();
3216 } else {
3217 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
3218 if (EnumDecl *ED = dyn_cast<EnumDecl>(D))
3219 UnderlyingTy = ED->getIntegerType();
3220 }
3221 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003222 return;
3223
3224 // C++11 [dcl.align]p5, C11 6.7.5/4:
3225 // The combined effect of all alignment attributes in a declaration shall
3226 // not specify an alignment that is less strict than the alignment that
3227 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003228 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003229 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003230 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003231 if (I->isAlignmentDependent())
3232 return;
3233 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003234 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003235 Align = std::max(Align, I->getAlignment(Context));
3236 }
3237
3238 if (AlignasAttr && Align) {
3239 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003240 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003241 if (NaturalAlign > RequestedAlign)
3242 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003243 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003244 }
3245}
3246
David Majnemer2c4e00a2014-01-29 22:07:36 +00003247bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003248 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003249 MSInheritanceAttr::Spelling SemanticSpelling) {
3250 assert(RD->hasDefinition() && "RD has no definition!");
3251
David Majnemer98c9ee22014-02-07 00:43:07 +00003252 // We may not have seen base specifiers or any virtual methods yet. We will
3253 // have to wait until the record is defined to catch any mismatches.
3254 if (!RD->getDefinition()->isCompleteDefinition())
3255 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003256
David Majnemer98c9ee22014-02-07 00:43:07 +00003257 // The unspecified model never matches what a definition could need.
3258 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
3259 return false;
3260
David Majnemer4bb09802014-02-10 19:50:15 +00003261 if (BestCase) {
3262 if (RD->calculateInheritanceModel() == SemanticSpelling)
3263 return false;
3264 } else {
3265 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3266 return false;
3267 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003268
3269 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3270 << 0 /*definition*/;
3271 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3272 << RD->getNameAsString();
3273 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003274}
3275
Alexey Bataevf278eb12015-11-19 10:13:11 +00003276/// parseModeAttrArg - Parses attribute mode string and returns parsed type
3277/// attribute.
3278static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
3279 bool &IntegerMode, bool &ComplexMode) {
Daniel Dunbarafff4342009-10-18 02:09:24 +00003280 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003281 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003282 switch (Str[0]) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003283 case 'Q':
3284 DestWidth = 8;
3285 break;
3286 case 'H':
3287 DestWidth = 16;
3288 break;
3289 case 'S':
3290 DestWidth = 32;
3291 break;
3292 case 'D':
3293 DestWidth = 64;
3294 break;
3295 case 'X':
3296 DestWidth = 96;
3297 break;
3298 case 'T':
3299 DestWidth = 128;
3300 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003301 }
3302 if (Str[1] == 'F') {
3303 IntegerMode = false;
3304 } else if (Str[1] == 'C') {
3305 IntegerMode = false;
3306 ComplexMode = true;
3307 } else if (Str[1] != 'I') {
3308 DestWidth = 0;
3309 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003310 break;
3311 case 4:
3312 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3313 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003314 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003315 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003316 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003317 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003318 break;
3319 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003320 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003321 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003322 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003323 case 11:
3324 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003325 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003326 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003327 }
Alexey Bataevf278eb12015-11-19 10:13:11 +00003328}
3329
3330/// handleModeAttr - This attribute modifies the width of a decl with primitive
3331/// type.
3332///
3333/// Despite what would be logical, the mode attribute is a decl attribute, not a
3334/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3335/// HImode, not an intermediate pointer.
3336static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3337 // This attribute isn't documented, but glibc uses it. It changes
3338 // the width of an int or unsigned int to the specified size.
3339 if (!Attr.isArgIdent(0)) {
3340 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3341 << AANT_ArgumentIdentifier;
3342 return;
3343 }
3344
3345 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3346 StringRef Str = Name->getName();
3347
3348 normalizeName(Str);
3349
3350 unsigned DestWidth = 0;
3351 bool IntegerMode = true;
3352 bool ComplexMode = false;
3353 llvm::APInt VectorSize(64, 0);
3354 if (Str.size() >= 4 && Str[0] == 'V') {
3355 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
3356 size_t StrSize = Str.size();
3357 size_t VectorStringLength = 0;
3358 while ((VectorStringLength + 1) < StrSize &&
3359 isdigit(Str[VectorStringLength + 1]))
3360 ++VectorStringLength;
3361 if (VectorStringLength &&
3362 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
3363 VectorSize.isPowerOf2()) {
3364 parseModeAttrArg(S, Str.substr(VectorStringLength + 1), DestWidth,
3365 IntegerMode, ComplexMode);
3366 S.Diag(Attr.getLoc(), diag::warn_vector_mode_deprecated);
3367 } else {
3368 VectorSize = 0;
3369 }
3370 }
3371
3372 if (!VectorSize)
3373 parseModeAttrArg(S, Str, DestWidth, IntegerMode, ComplexMode);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003374
3375 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003376 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003377 OldTy = TD->getUnderlyingType();
3378 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3379 OldTy = VD->getType();
3380 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003381 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00003382 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003383 return;
3384 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003385
Alexey Bataev326057d2015-06-19 07:46:21 +00003386 // Base type can also be a vector type (see PR17453).
3387 // Distinguish between base type and base element type.
3388 QualType OldElemTy = OldTy;
3389 if (const VectorType *VT = OldTy->getAs<VectorType>())
3390 OldElemTy = VT->getElementType();
3391
3392 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003393 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3394 else if (IntegerMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003395 if (!OldElemTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003396 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3397 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003398 if (!OldElemTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003399 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3400 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00003401 if (!OldElemTy->isFloatingType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003402 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3403 }
3404
Mike Stump87c57ac2009-05-16 07:39:55 +00003405 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3406 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003407 // FIXME: Make sure floating-point mappings are accurate
3408 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003409 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00003410 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003411 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003412 }
3413
Alexey Bataev326057d2015-06-19 07:46:21 +00003414 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003415
3416 if (IntegerMode)
Alexey Bataev326057d2015-06-19 07:46:21 +00003417 NewElemTy = S.Context.getIntTypeForBitwidth(
3418 DestWidth, OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003419 else
Alexey Bataev326057d2015-06-19 07:46:21 +00003420 NewElemTy = S.Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003421
Alexey Bataev326057d2015-06-19 07:46:21 +00003422 if (NewElemTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00003423 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003424 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003425 }
3426
Eli Friedman4735374e2009-03-03 06:41:03 +00003427 if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003428 NewElemTy = S.Context.getComplexType(NewElemTy);
3429 }
3430
3431 QualType NewTy = NewElemTy;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003432 if (VectorSize.getBoolValue()) {
3433 NewTy = S.Context.getVectorType(NewTy, VectorSize.getZExtValue(),
3434 VectorType::GenericVector);
3435 } else if (const VectorType *OldVT = OldTy->getAs<VectorType>()) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003436 // Complex machine mode does not support base vector types.
3437 if (ComplexMode) {
3438 S.Diag(Attr.getLoc(), diag::err_complex_mode_vector_type);
3439 return;
3440 }
3441 unsigned NumElements = S.Context.getTypeSize(OldElemTy) *
3442 OldVT->getNumElements() /
3443 S.Context.getTypeSize(NewElemTy);
3444 NewTy =
3445 S.Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
3446 }
3447
3448 if (NewTy.isNull()) {
3449 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3450 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003451 }
3452
3453 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003454 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3455 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3456 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003457 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003458
3459 D->addAttr(::new (S.Context)
3460 ModeAttr(Attr.getRange(), S.Context, Name,
3461 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003462}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003463
Chandler Carruthedc2c642011-07-02 00:01:44 +00003464static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003465 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3466 if (!VD->hasGlobalStorage())
3467 S.Diag(Attr.getLoc(),
3468 diag::warn_attribute_requires_functions_or_static_globals)
3469 << Attr.getName();
3470 } else if (!isFunctionOrMethod(D)) {
3471 S.Diag(Attr.getLoc(),
3472 diag::warn_attribute_requires_functions_or_static_globals)
3473 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003474 return;
3475 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003476
Michael Han99315932013-01-24 16:46:58 +00003477 D->addAttr(::new (S.Context)
3478 NoDebugAttr(Attr.getRange(), S.Context,
3479 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003480}
3481
Paul Robinson30e41fb2014-12-15 18:57:28 +00003482AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range,
Paul Robinson080b1f32015-01-13 18:34:56 +00003483 IdentifierInfo *Ident,
Paul Robinson30e41fb2014-12-15 18:57:28 +00003484 unsigned AttrSpellingListIndex) {
3485 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003486 Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00003487 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3488 return nullptr;
3489 }
3490
3491 if (D->hasAttr<AlwaysInlineAttr>())
3492 return nullptr;
3493
3494 return ::new (Context) AlwaysInlineAttr(Range, Context,
3495 AttrSpellingListIndex);
3496}
3497
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003498CommonAttr *Sema::mergeCommonAttr(Decl *D, SourceRange Range,
3499 IdentifierInfo *Ident,
3500 unsigned AttrSpellingListIndex) {
3501 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, Range, Ident))
3502 return nullptr;
3503
3504 return ::new (Context) CommonAttr(Range, Context, AttrSpellingListIndex);
3505}
3506
3507InternalLinkageAttr *
3508Sema::mergeInternalLinkageAttr(Decl *D, SourceRange Range,
3509 IdentifierInfo *Ident,
3510 unsigned AttrSpellingListIndex) {
3511 if (auto VD = dyn_cast<VarDecl>(D)) {
3512 // Attribute applies to Var but not any subclass of it (like ParmVar,
3513 // ImplicitParm or VarTemplateSpecialization).
3514 if (VD->getKind() != Decl::Var) {
3515 Diag(Range.getBegin(), diag::warn_attribute_wrong_decl_type)
3516 << Ident << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
3517 : ExpectedVariableOrFunction);
3518 return nullptr;
3519 }
3520 // Attribute does not apply to non-static local variables.
3521 if (VD->hasLocalStorage()) {
3522 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
3523 return nullptr;
3524 }
3525 }
3526
3527 if (checkAttrMutualExclusion<CommonAttr>(*this, D, Range, Ident))
3528 return nullptr;
3529
3530 return ::new (Context)
3531 InternalLinkageAttr(Range, Context, AttrSpellingListIndex);
3532}
3533
Paul Robinson30e41fb2014-12-15 18:57:28 +00003534MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range,
3535 unsigned AttrSpellingListIndex) {
3536 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
3537 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'";
3538 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3539 return nullptr;
3540 }
3541
3542 if (D->hasAttr<MinSizeAttr>())
3543 return nullptr;
3544
3545 return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex);
3546}
3547
3548OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range,
3549 unsigned AttrSpellingListIndex) {
3550 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
3551 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
3552 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3553 D->dropAttr<AlwaysInlineAttr>();
3554 }
3555 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
3556 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
3557 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3558 D->dropAttr<MinSizeAttr>();
3559 }
3560
3561 if (D->hasAttr<OptimizeNoneAttr>())
3562 return nullptr;
3563
3564 return ::new (Context) OptimizeNoneAttr(Range, Context,
3565 AttrSpellingListIndex);
3566}
3567
Paul Robinsonf0674352014-03-31 22:29:15 +00003568static void handleAlwaysInlineAttr(Sema &S, Decl *D,
3569 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003570 if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, Attr.getRange(),
3571 Attr.getName()))
Akira Hatanakac8667622015-11-06 23:56:15 +00003572 return;
3573
Paul Robinson080b1f32015-01-13 18:34:56 +00003574 if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr(
3575 D, Attr.getRange(), Attr.getName(),
3576 Attr.getAttributeSpellingListIndex()))
3577 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00003578}
3579
Paul Robinson080b1f32015-01-13 18:34:56 +00003580static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3581 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(
3582 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3583 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00003584}
3585
Paul Robinsonf0674352014-03-31 22:29:15 +00003586static void handleOptimizeNoneAttr(Sema &S, Decl *D,
3587 const AttributeList &Attr) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003588 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(
3589 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3590 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00003591}
3592
Chandler Carruthedc2c642011-07-02 00:01:44 +00003593static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003594 FunctionDecl *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00003595 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00003596 SourceRange RTRange = FD->getReturnTypeSourceRange();
3597 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00003598 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00003599 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
3600 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00003601 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003602 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003603
Aaron Ballman3aff6332013-12-02 19:30:36 +00003604 D->addAttr(::new (S.Context)
3605 CUDAGlobalAttr(Attr.getRange(), S.Context,
Eli Bendersky83860042014-09-02 22:00:06 +00003606 Attr.getAttributeSpellingListIndex()));
Artem Belevichc3fa25d2015-09-22 17:22:51 +00003607
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003608}
3609
Chandler Carruthedc2c642011-07-02 00:01:44 +00003610static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003611 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003612 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003613 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003614 return;
3615 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003616
Michael Han99315932013-01-24 16:46:58 +00003617 D->addAttr(::new (S.Context)
3618 GNUInlineAttr(Attr.getRange(), S.Context,
3619 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003620}
3621
Chandler Carruthedc2c642011-07-02 00:01:44 +00003622static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003623 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003624
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003625 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003626 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3627 CallingConv CC;
Richard Smitheec7cb12015-05-28 23:38:53 +00003628 if (S.CheckCallingConvAttr(Attr, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00003629 return;
3630
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003631 if (!isa<ObjCMethodDecl>(D)) {
3632 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3633 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003634 return;
3635 }
3636
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003637 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003638 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003639 D->addAttr(::new (S.Context)
3640 FastCallAttr(Attr.getRange(), S.Context,
3641 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003642 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003643 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003644 D->addAttr(::new (S.Context)
3645 StdCallAttr(Attr.getRange(), S.Context,
3646 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003647 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003648 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003649 D->addAttr(::new (S.Context)
3650 ThisCallAttr(Attr.getRange(), S.Context,
3651 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003652 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003653 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003654 D->addAttr(::new (S.Context)
3655 CDeclAttr(Attr.getRange(), S.Context,
3656 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003657 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003658 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003659 D->addAttr(::new (S.Context)
3660 PascalAttr(Attr.getRange(), S.Context,
3661 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003662 return;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003663 case AttributeList::AT_VectorCall:
3664 D->addAttr(::new (S.Context)
3665 VectorCallAttr(Attr.getRange(), S.Context,
3666 Attr.getAttributeSpellingListIndex()));
3667 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003668 case AttributeList::AT_MSABI:
3669 D->addAttr(::new (S.Context)
3670 MSABIAttr(Attr.getRange(), S.Context,
3671 Attr.getAttributeSpellingListIndex()));
3672 return;
3673 case AttributeList::AT_SysVABI:
3674 D->addAttr(::new (S.Context)
3675 SysVABIAttr(Attr.getRange(), S.Context,
3676 Attr.getAttributeSpellingListIndex()));
3677 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003678 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003679 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003680 switch (CC) {
3681 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003682 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003683 break;
3684 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003685 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003686 break;
3687 default:
3688 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003689 }
3690
Michael Han99315932013-01-24 16:46:58 +00003691 D->addAttr(::new (S.Context)
3692 PcsAttr(Attr.getRange(), S.Context, PCS,
3693 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003694 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003695 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00003696 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003697 D->addAttr(::new (S.Context)
3698 IntelOclBiccAttr(Attr.getRange(), S.Context,
3699 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003700 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003701
Abramo Bagnara50099372010-04-30 13:10:51 +00003702 default:
3703 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003704 }
3705}
3706
Aaron Ballman02df2e02012-12-09 17:45:41 +00003707bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3708 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003709 if (attr.isInvalid())
3710 return true;
3711
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003712 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003713 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003714 attr.setInvalid();
3715 return true;
3716 }
3717
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003718 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003719 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003720 case AttributeList::AT_CDecl: CC = CC_C; break;
3721 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3722 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3723 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3724 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003725 case AttributeList::AT_VectorCall: CC = CC_X86VectorCall; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003726 case AttributeList::AT_MSABI:
3727 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3728 CC_X86_64Win64;
3729 break;
3730 case AttributeList::AT_SysVABI:
3731 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3732 CC_C;
3733 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003734 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003735 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003736 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003737 attr.setInvalid();
3738 return true;
3739 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003740 if (StrRef == "aapcs") {
3741 CC = CC_AAPCS;
3742 break;
3743 } else if (StrRef == "aapcs-vfp") {
3744 CC = CC_AAPCS_VFP;
3745 break;
3746 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003747
3748 attr.setInvalid();
3749 Diag(attr.getLoc(), diag::err_invalid_pcs);
3750 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003751 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00003752 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003753 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003754 }
3755
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003756 const TargetInfo &TI = Context.getTargetInfo();
3757 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
Reid Kleckner9fde2e02015-02-26 19:43:46 +00003758 if (A != TargetInfo::CCCR_OK) {
3759 if (A == TargetInfo::CCCR_Warning)
3760 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003761
Reid Kleckner9fde2e02015-02-26 19:43:46 +00003762 // This convention is not valid for the target. Use the default function or
3763 // method calling convention.
Aaron Ballman02df2e02012-12-09 17:45:41 +00003764 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3765 if (FD)
3766 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3767 TargetInfo::CCMT_NonMember;
3768 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003769 }
3770
John McCall3882ace2011-01-05 12:14:39 +00003771 return false;
3772}
3773
John McCall3882ace2011-01-05 12:14:39 +00003774/// Checks a regparm attribute, returning true if it is ill-formed and
3775/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003776bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3777 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003778 return true;
3779
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003780 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003781 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003782 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003783 }
Eli Friedman7044b762009-03-27 21:06:47 +00003784
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003785 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003786 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003787 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003788 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003789 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003790 }
3791
Douglas Gregore8bbc122011-09-02 00:18:52 +00003792 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003793 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003794 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003795 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003796 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003797 }
3798
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003799 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003800 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003801 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003802 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003803 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003804 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003805 }
3806
John McCall3882ace2011-01-05 12:14:39 +00003807 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003808}
3809
Artem Belevich7093e402015-04-21 22:55:54 +00003810// Checks whether an argument of launch_bounds attribute is acceptable
3811// May output an error.
3812static bool checkLaunchBoundsArgument(Sema &S, Expr *E,
3813 const CUDALaunchBoundsAttr &Attr,
3814 const unsigned Idx) {
3815
3816 if (S.DiagnoseUnexpandedParameterPack(E))
3817 return false;
3818
3819 // Accept template arguments for now as they depend on something else.
3820 // We'll get to check them when they eventually get instantiated.
3821 if (E->isValueDependent())
3822 return true;
3823
3824 llvm::APSInt I(64);
3825 if (!E->isIntegerConstantExpr(I, S.Context)) {
3826 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
3827 << &Attr << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
3828 return false;
3829 }
3830 // Make sure we can fit it in 32 bits.
3831 if (!I.isIntN(32)) {
3832 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
3833 << 32 << /* Unsigned */ 1;
3834 return false;
3835 }
3836 if (I < 0)
3837 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
3838 << &Attr << Idx << E->getSourceRange();
3839
3840 return true;
3841}
3842
3843void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads,
3844 Expr *MinBlocks, unsigned SpellingListIndex) {
3845 CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks,
3846 SpellingListIndex);
3847
3848 if (!checkLaunchBoundsArgument(*this, MaxThreads, TmpAttr, 0))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003849 return;
3850
Artem Belevich7093e402015-04-21 22:55:54 +00003851 if (MinBlocks && !checkLaunchBoundsArgument(*this, MinBlocks, TmpAttr, 1))
3852 return;
3853
3854 D->addAttr(::new (Context) CUDALaunchBoundsAttr(
3855 AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex));
3856}
3857
3858static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3859 const AttributeList &Attr) {
3860 if (!checkAttributeAtLeastNumArgs(S, Attr, 1) ||
3861 !checkAttributeAtMostNumArgs(S, Attr, 2))
3862 return;
3863
3864 S.AddLaunchBoundsAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
3865 Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr,
3866 Attr.getAttributeSpellingListIndex());
Peter Collingbourne827301e2010-12-12 23:03:07 +00003867}
3868
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003869static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3870 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003871 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003872 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003873 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003874 return;
3875 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003876
3877 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003878 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003879
Aaron Ballman00e99962013-08-31 01:11:41 +00003880 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003881
3882 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3883 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3884 << Attr.getName() << ExpectedFunctionOrMethod;
3885 return;
3886 }
3887
3888 uint64_t ArgumentIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003889 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3890 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003891 return;
3892
3893 uint64_t TypeTagIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003894 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3895 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003896 return;
3897
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003898 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003899 if (IsPointer) {
3900 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00003901 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003902 if (!BufferTy->isPointerType()) {
3903 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00003904 << Attr.getName() << 0;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003905 }
3906 }
3907
Michael Han99315932013-01-24 16:46:58 +00003908 D->addAttr(::new (S.Context)
3909 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3910 ArgumentIdx, TypeTagIdx, IsPointer,
3911 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003912}
3913
3914static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3915 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003916 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003917 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003918 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003919 return;
3920 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003921
3922 if (!checkAttributeNumArgs(S, Attr, 1))
3923 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003924
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003925 if (!isa<VarDecl>(D)) {
3926 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3927 << Attr.getName() << ExpectedVariable;
3928 return;
3929 }
3930
Aaron Ballman00e99962013-08-31 01:11:41 +00003931 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00003932 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00003933 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3934 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003935
Michael Han99315932013-01-24 16:46:58 +00003936 D->addAttr(::new (S.Context)
3937 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003938 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003939 Attr.getLayoutCompatible(),
3940 Attr.getMustBeNull(),
3941 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003942}
3943
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003944//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003945// Checker-specific attribute handlers.
3946//===----------------------------------------------------------------------===//
3947
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003948static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) {
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003949 return type->isDependentType() ||
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003950 type->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003951}
3952
John McCalled433932011-01-25 03:31:58 +00003953static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003954 return type->isDependentType() ||
3955 type->isObjCObjectPointerType() ||
3956 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003957}
3958static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003959 return type->isDependentType() ||
3960 type->isPointerType() ||
3961 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003962}
3963
Chandler Carruthedc2c642011-07-02 00:01:44 +00003964static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003965 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003966 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003967
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003968 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003969 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3970 cf = false;
3971 } else {
3972 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3973 cf = true;
3974 }
3975
3976 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003977 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003978 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003979 return;
3980 }
3981
3982 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003983 param->addAttr(::new (S.Context)
3984 CFConsumedAttr(Attr.getRange(), S.Context,
3985 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003986 else
Michael Han99315932013-01-24 16:46:58 +00003987 param->addAttr(::new (S.Context)
3988 NSConsumedAttr(Attr.getRange(), S.Context,
3989 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003990}
3991
Chandler Carruthedc2c642011-07-02 00:01:44 +00003992static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3993 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003994
John McCalled433932011-01-25 03:31:58 +00003995 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003996
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003997 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003998 returnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003999 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004000 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004001 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00004002 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4003 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004004 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004005 returnType = FD->getReturnType();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004006 else if (auto *Param = dyn_cast<ParmVarDecl>(D)) {
4007 returnType = Param->getType()->getPointeeType();
4008 if (returnType.isNull()) {
4009 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
4010 << Attr.getName() << /*pointer-to-CF*/2
4011 << Attr.getRange();
4012 return;
4013 }
4014 } else {
4015 AttributeDeclKind ExpectedDeclKind;
4016 switch (Attr.getKind()) {
4017 default: llvm_unreachable("invalid ownership attribute");
4018 case AttributeList::AT_NSReturnsRetained:
4019 case AttributeList::AT_NSReturnsAutoreleased:
4020 case AttributeList::AT_NSReturnsNotRetained:
4021 ExpectedDeclKind = ExpectedFunctionOrMethod;
4022 break;
4023
4024 case AttributeList::AT_CFReturnsRetained:
4025 case AttributeList::AT_CFReturnsNotRetained:
4026 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
4027 break;
4028 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004029 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004030 << Attr.getRange() << Attr.getName() << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004031 return;
4032 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004033
John McCalled433932011-01-25 03:31:58 +00004034 bool typeOK;
4035 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004036 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004037 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004038 case AttributeList::AT_NSReturnsRetained:
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00004039 typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType);
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004040 cf = false;
4041 break;
4042
4043 case AttributeList::AT_NSReturnsAutoreleased:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004044 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004045 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4046 cf = false;
4047 break;
4048
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004049 case AttributeList::AT_CFReturnsRetained:
4050 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004051 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4052 cf = true;
4053 break;
4054 }
4055
4056 if (!typeOK) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004057 if (isa<ParmVarDecl>(D)) {
4058 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
4059 << Attr.getName() << /*pointer-to-CF*/2
4060 << Attr.getRange();
4061 } else {
4062 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
4063 enum : unsigned {
4064 Function,
4065 Method,
4066 Property
4067 } SubjectKind = Function;
4068 if (isa<ObjCMethodDecl>(D))
4069 SubjectKind = Method;
4070 else if (isa<ObjCPropertyDecl>(D))
4071 SubjectKind = Property;
4072 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4073 << Attr.getName() << SubjectKind << cf
4074 << Attr.getRange();
4075 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004076 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004077 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004078
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004079 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004080 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004081 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004082 case AttributeList::AT_NSReturnsAutoreleased:
Nico Weber462fd1e2015-01-07 23:50:05 +00004083 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(
4084 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004085 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004086 case AttributeList::AT_CFReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004087 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(
4088 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004089 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004090 case AttributeList::AT_NSReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004091 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(
4092 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004093 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004094 case AttributeList::AT_CFReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004095 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(
4096 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004097 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004098 case AttributeList::AT_NSReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004099 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(
4100 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004101 return;
4102 };
4103}
4104
John McCallcf166702011-07-22 08:53:00 +00004105static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4106 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00004107 const int EP_ObjCMethod = 1;
4108 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004109
John McCallcf166702011-07-22 08:53:00 +00004110 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004111 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004112 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004113 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004114 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004115 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00004116
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004117 if (!resultType->isReferenceType() &&
4118 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004119 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00004120 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004121 << attr.getName()
4122 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004123 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00004124
4125 // Drop the attribute.
4126 return;
4127 }
4128
Nico Weber462fd1e2015-01-07 23:50:05 +00004129 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(
4130 attr.getRange(), S.Context, attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004131}
4132
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004133static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4134 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004135 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004136
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004137 DeclContext *DC = method->getDeclContext();
4138 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4139 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4140 << attr.getName() << 0;
4141 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4142 return;
4143 }
4144 if (method->getMethodFamily() == OMF_dealloc) {
4145 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4146 << attr.getName() << 1;
4147 return;
4148 }
4149
Michael Han99315932013-01-24 16:46:58 +00004150 method->addAttr(::new (S.Context)
4151 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4152 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004153}
4154
Aaron Ballmanfb763042013-12-02 18:05:46 +00004155static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
4156 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004157 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr.getRange(),
4158 Attr.getName()))
John McCall32f5fe12011-09-30 05:12:12 +00004159 return;
John McCall32f5fe12011-09-30 05:12:12 +00004160
Aaron Ballmanfb763042013-12-02 18:05:46 +00004161 D->addAttr(::new (S.Context)
4162 CFAuditedTransferAttr(Attr.getRange(), S.Context,
4163 Attr.getAttributeSpellingListIndex()));
4164}
4165
4166static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
4167 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004168 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr.getRange(),
4169 Attr.getName()))
Aaron Ballmanfb763042013-12-02 18:05:46 +00004170 return;
4171
4172 D->addAttr(::new (S.Context)
4173 CFUnknownTransferAttr(Attr.getRange(), S.Context,
4174 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004175}
4176
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004177static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
4178 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004179 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004180
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004181 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004182 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004183 return;
4184 }
John McCall28592582015-02-01 22:34:06 +00004185
4186 // Typedefs only allow objc_bridge(id) and have some additional checking.
4187 if (auto TD = dyn_cast<TypedefNameDecl>(D)) {
4188 if (!Parm->Ident->isStr("id")) {
4189 S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_id)
4190 << Attr.getName();
4191 return;
4192 }
4193
4194 // Only allow 'cv void *'.
4195 QualType T = TD->getUnderlyingType();
4196 if (!T->isVoidPointerType()) {
4197 S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
4198 return;
4199 }
4200 }
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004201
4202 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004203 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004204 Attr.getAttributeSpellingListIndex()));
4205}
4206
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004207static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
4208 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004209 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
4210
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004211 if (!Parm) {
4212 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
4213 return;
4214 }
4215
4216 D->addAttr(::new (S.Context)
4217 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
4218 Attr.getAttributeSpellingListIndex()));
4219}
4220
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004221static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
4222 const AttributeList &Attr) {
4223 IdentifierInfo *RelatedClass =
Craig Topperc3ec1492014-05-26 06:22:03 +00004224 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004225 if (!RelatedClass) {
4226 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
4227 return;
4228 }
4229 IdentifierInfo *ClassMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00004230 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004231 IdentifierInfo *InstanceMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00004232 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004233 D->addAttr(::new (S.Context)
4234 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
4235 ClassMethod, InstanceMethod,
4236 Attr.getAttributeSpellingListIndex()));
4237}
4238
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004239static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
4240 const AttributeList &Attr) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004241 ObjCInterfaceDecl *IFace;
Nico Weber462fd1e2015-01-07 23:50:05 +00004242 if (ObjCCategoryDecl *CatDecl =
4243 dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004244 IFace = CatDecl->getClassInterface();
4245 else
4246 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00004247
4248 if (!IFace)
4249 return;
4250
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00004251 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00004252 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004253 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
4254 Attr.getAttributeSpellingListIndex()));
4255}
4256
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004257static void handleObjCRuntimeName(Sema &S, Decl *D,
4258 const AttributeList &Attr) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004259 StringRef MetaDataName;
4260 if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName))
4261 return;
4262 D->addAttr(::new (S.Context)
4263 ObjCRuntimeNameAttr(Attr.getRange(), S.Context,
4264 MetaDataName,
4265 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004266}
4267
Alex Denisovfde64952015-06-26 05:28:36 +00004268// when a user wants to use objc_boxable with a union or struct
4269// but she doesn't have access to the declaration (legacy/third-party code)
4270// then she can 'enable' this feature via trick with a typedef
4271// e.g.:
4272// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
4273static void handleObjCBoxable(Sema &S, Decl *D, const AttributeList &Attr) {
4274 bool notify = false;
4275
4276 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4277 if (RD && RD->getDefinition()) {
4278 RD = RD->getDefinition();
4279 notify = true;
4280 }
4281
4282 if (RD) {
4283 ObjCBoxableAttr *BoxableAttr = ::new (S.Context)
4284 ObjCBoxableAttr(Attr.getRange(), S.Context,
4285 Attr.getAttributeSpellingListIndex());
4286 RD->addAttr(BoxableAttr);
4287 if (notify) {
4288 // we need to notify ASTReader/ASTWriter about
4289 // modification of existing declaration
4290 if (ASTMutationListener *L = S.getASTMutationListener())
4291 L->AddedAttributeToRecord(BoxableAttr, RD);
4292 }
4293 }
4294}
4295
Chandler Carruthedc2c642011-07-02 00:01:44 +00004296static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4297 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004298 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004299
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004300 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004301 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004302}
4303
Chandler Carruthedc2c642011-07-02 00:01:44 +00004304static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4305 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004306 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004307 QualType type = vd->getType();
4308
4309 if (!type->isDependentType() &&
4310 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004311 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004312 << type;
4313 return;
4314 }
4315
4316 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4317
4318 // If we have no lifetime yet, check the lifetime we're presumably
4319 // going to infer.
4320 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4321 lifetime = type->getObjCARCImplicitLifetime();
4322
4323 switch (lifetime) {
4324 case Qualifiers::OCL_None:
4325 assert(type->isDependentType() &&
4326 "didn't infer lifetime for non-dependent type?");
4327 break;
4328
4329 case Qualifiers::OCL_Weak: // meaningful
4330 case Qualifiers::OCL_Strong: // meaningful
4331 break;
4332
4333 case Qualifiers::OCL_ExplicitNone:
4334 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004335 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004336 << (lifetime == Qualifiers::OCL_Autoreleasing);
4337 break;
4338 }
4339
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004340 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004341 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4342 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004343}
4344
Francois Picheta83957a2010-12-19 06:50:37 +00004345//===----------------------------------------------------------------------===//
4346// Microsoft specific attribute handlers.
4347//===----------------------------------------------------------------------===//
4348
Chandler Carruthedc2c642011-07-02 00:01:44 +00004349static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00004350 if (!S.LangOpts.CPlusPlus) {
4351 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4352 << Attr.getName() << AttributeLangSupport::C;
4353 return;
4354 }
4355
Aaron Ballman60e705e2013-11-24 20:58:02 +00004356 if (!isa<CXXRecordDecl>(D)) {
4357 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4358 << Attr.getName() << ExpectedClass;
4359 return;
4360 }
4361
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004362 StringRef StrRef;
4363 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00004364 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00004365 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004366
David Majnemer89085342013-08-09 08:56:20 +00004367 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4368 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00004369 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4370 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00004371
Reid Kleckner140c4a72013-05-17 14:04:52 +00004372 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00004373 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004374 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004375 return;
4376 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004377
David Majnemer89085342013-08-09 08:56:20 +00004378 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004379 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00004380 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004381 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00004382 return;
4383 }
David Majnemer89085342013-08-09 08:56:20 +00004384 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004385 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004386 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004387 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004388 }
Francois Picheta83957a2010-12-19 06:50:37 +00004389
David Majnemer89085342013-08-09 08:56:20 +00004390 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4391 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004392}
4393
David Majnemer2c4e00a2014-01-29 22:07:36 +00004394static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4395 if (!S.LangOpts.CPlusPlus) {
4396 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4397 << Attr.getName() << AttributeLangSupport::C;
4398 return;
4399 }
4400 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
David Majnemer4bb09802014-02-10 19:50:15 +00004401 D, Attr.getRange(), /*BestCase=*/true,
4402 Attr.getAttributeSpellingListIndex(),
David Majnemer2c4e00a2014-01-29 22:07:36 +00004403 (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling());
4404 if (IA)
4405 D->addAttr(IA);
4406}
4407
Reid Kleckner7d6d2702014-05-01 03:16:47 +00004408static void handleDeclspecThreadAttr(Sema &S, Decl *D,
4409 const AttributeList &Attr) {
4410 VarDecl *VD = cast<VarDecl>(D);
4411 if (!S.Context.getTargetInfo().isTLSSupported()) {
4412 S.Diag(Attr.getLoc(), diag::err_thread_unsupported);
4413 return;
4414 }
4415 if (VD->getTSCSpec() != TSCS_unspecified) {
4416 S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable);
4417 return;
4418 }
4419 if (VD->hasLocalStorage()) {
4420 S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
4421 return;
4422 }
4423 VD->addAttr(::new (S.Context) ThreadAttr(
4424 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
4425}
4426
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004427static void handleARMInterruptAttr(Sema &S, Decl *D,
4428 const AttributeList &Attr) {
4429 // Check the attribute arguments.
4430 if (Attr.getNumArgs() > 1) {
4431 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
4432 << Attr.getName() << 1;
4433 return;
4434 }
4435
4436 StringRef Str;
4437 SourceLocation ArgLoc;
4438
4439 if (Attr.getNumArgs() == 0)
4440 Str = "";
4441 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
4442 return;
4443
4444 ARMInterruptAttr::InterruptType Kind;
4445 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
4446 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
4447 << Attr.getName() << Str << ArgLoc;
4448 return;
4449 }
4450
4451 unsigned Index = Attr.getAttributeSpellingListIndex();
4452 D->addAttr(::new (S.Context)
4453 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
4454}
4455
4456static void handleMSP430InterruptAttr(Sema &S, Decl *D,
4457 const AttributeList &Attr) {
4458 if (!checkAttributeNumArgs(S, Attr, 1))
4459 return;
4460
4461 if (!Attr.isArgExpr(0)) {
4462 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
4463 << AANT_ArgumentIntegerConstant;
4464 return;
4465 }
4466
4467 // FIXME: Check for decl - it should be void ()(void).
4468
4469 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4470 llvm::APSInt NumParams(32);
4471 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
4472 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4473 << Attr.getName() << AANT_ArgumentIntegerConstant
4474 << NumParamsExpr->getSourceRange();
4475 return;
4476 }
4477
4478 unsigned Num = NumParams.getLimitedValue(255);
4479 if ((Num & 1) || Num > 30) {
4480 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
4481 << Attr.getName() << (int)NumParams.getSExtValue()
4482 << NumParamsExpr->getSourceRange();
4483 return;
4484 }
4485
Aaron Ballman36a53502014-01-16 13:03:14 +00004486 D->addAttr(::new (S.Context)
4487 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
4488 Attr.getAttributeSpellingListIndex()));
4489 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004490}
4491
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00004492static void handleMipsInterruptAttr(Sema &S, Decl *D,
4493 const AttributeList &Attr) {
4494 // Only one optional argument permitted.
4495 if (Attr.getNumArgs() > 1) {
4496 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
4497 << Attr.getName() << 1;
4498 return;
4499 }
4500
4501 StringRef Str;
4502 SourceLocation ArgLoc;
4503
4504 if (Attr.getNumArgs() == 0)
4505 Str = "";
4506 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
4507 return;
4508
4509 // Semantic checks for a function with the 'interrupt' attribute for MIPS:
4510 // a) Must be a function.
4511 // b) Must have no parameters.
4512 // c) Must have the 'void' return type.
4513 // d) Cannot have the 'mips16' attribute, as that instruction set
4514 // lacks the 'eret' instruction.
4515 // e) The attribute itself must either have no argument or one of the
4516 // valid interrupt types, see [MipsInterruptDocs].
4517
4518 if (!isFunctionOrMethod(D)) {
4519 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
4520 << "'interrupt'" << ExpectedFunctionOrMethod;
4521 return;
4522 }
4523
4524 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
4525 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
4526 << 0;
4527 return;
4528 }
4529
4530 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
4531 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
4532 << 1;
4533 return;
4534 }
4535
4536 if (checkAttrMutualExclusion<Mips16Attr>(S, D, Attr.getRange(),
4537 Attr.getName()))
4538 return;
4539
4540 MipsInterruptAttr::InterruptType Kind;
4541 if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
4542 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
4543 << Attr.getName() << "'" + std::string(Str) + "'";
4544 return;
4545 }
4546
4547 D->addAttr(::new (S.Context) MipsInterruptAttr(
4548 Attr.getLoc(), S.Context, Kind, Attr.getAttributeSpellingListIndex()));
4549}
4550
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004551static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4552 // Dispatch the interrupt attribute based on the current target.
4553 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
4554 handleMSP430InterruptAttr(S, D, Attr);
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00004555 else if (S.Context.getTargetInfo().getTriple().getArch() ==
4556 llvm::Triple::mipsel ||
4557 S.Context.getTargetInfo().getTriple().getArch() ==
4558 llvm::Triple::mips)
4559 handleMipsInterruptAttr(S, D, Attr);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004560 else
4561 handleARMInterruptAttr(S, D, Attr);
4562}
4563
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00004564static void handleMips16Attribute(Sema &S, Decl *D, const AttributeList &Attr) {
4565 if (checkAttrMutualExclusion<MipsInterruptAttr>(S, D, Attr.getRange(),
4566 Attr.getName()))
4567 return;
4568
4569 handleSimpleAttribute<Mips16Attr>(S, D, Attr);
4570}
4571
Matt Arsenault43fae6c2014-12-04 20:38:18 +00004572static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D,
4573 const AttributeList &Attr) {
4574 uint32_t NumRegs;
4575 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4576 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4577 return;
4578
4579 D->addAttr(::new (S.Context)
4580 AMDGPUNumVGPRAttr(Attr.getLoc(), S.Context,
4581 NumRegs,
4582 Attr.getAttributeSpellingListIndex()));
4583}
4584
4585static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D,
4586 const AttributeList &Attr) {
4587 uint32_t NumRegs;
4588 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4589 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4590 return;
4591
4592 D->addAttr(::new (S.Context)
4593 AMDGPUNumSGPRAttr(Attr.getLoc(), S.Context,
4594 NumRegs,
4595 Attr.getAttributeSpellingListIndex()));
4596}
4597
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004598static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
4599 const AttributeList& Attr) {
4600 // If we try to apply it to a function pointer, don't warn, but don't
4601 // do anything, either. It doesn't matter anyway, because there's nothing
4602 // special about calling a force_align_arg_pointer function.
4603 ValueDecl *VD = dyn_cast<ValueDecl>(D);
4604 if (VD && VD->getType()->isFunctionPointerType())
4605 return;
4606 // Also don't warn on function pointer typedefs.
4607 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
4608 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
4609 TD->getUnderlyingType()->isFunctionType()))
4610 return;
4611 // Attribute can only be applied to function types.
4612 if (!isa<FunctionDecl>(D)) {
4613 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4614 << Attr.getName() << /* function */0;
4615 return;
4616 }
4617
Aaron Ballman36a53502014-01-16 13:03:14 +00004618 D->addAttr(::new (S.Context)
4619 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
4620 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004621}
4622
4623DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
4624 unsigned AttrSpellingListIndex) {
4625 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004626 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00004627 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004628 }
4629
4630 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004631 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004632
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004633 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004634}
4635
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004636DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
4637 unsigned AttrSpellingListIndex) {
4638 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004639 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004640 D->dropAttr<DLLImportAttr>();
4641 }
4642
4643 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004644 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004645
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004646 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004647}
4648
Hans Wennborge82f19c2014-06-24 23:57:05 +00004649static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00004650 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
4651 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4652 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored)
4653 << A.getName();
4654 return;
4655 }
4656
Hans Wennborg606bd6d2014-11-03 14:24:45 +00004657 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4658 if (FD->isInlined() && A.getKind() == AttributeList::AT_DLLImport &&
4659 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4660 // MinGW doesn't allow dllimport on inline functions.
4661 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
4662 << A.getName();
4663 return;
4664 }
4665 }
4666
Hans Wennborg5869ec42015-09-15 21:05:30 +00004667 if (auto *MD = dyn_cast<CXXMethodDecl>(D)) {
4668 if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
4669 MD->getParent()->isLambda()) {
4670 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A.getName();
4671 return;
4672 }
4673 }
4674
Hans Wennborge82f19c2014-06-24 23:57:05 +00004675 unsigned Index = A.getAttributeSpellingListIndex();
4676 Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport
4677 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
4678 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004679 if (NewAttr)
4680 D->addAttr(NewAttr);
4681}
4682
David Majnemer2c4e00a2014-01-29 22:07:36 +00004683MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00004684Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00004685 unsigned AttrSpellingListIndex,
4686 MSInheritanceAttr::Spelling SemanticSpelling) {
4687 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
4688 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00004689 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004690 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
4691 << 1 /*previous declaration*/;
4692 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
4693 D->dropAttr<MSInheritanceAttr>();
4694 }
4695
4696 CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
4697 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00004698 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
4699 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004700 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004701 }
4702 } else {
4703 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
4704 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4705 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004706 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004707 }
4708 if (RD->getDescribedClassTemplate()) {
4709 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4710 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004711 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004712 }
4713 }
4714
4715 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00004716 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00004717}
4718
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004719static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4720 // The capability attributes take a single string parameter for the name of
4721 // the capability they represent. The lockable attribute does not take any
4722 // parameters. However, semantically, both attributes represent the same
4723 // concept, and so they use the same semantic attribute. Eventually, the
4724 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00004725 //
Alp Toker958027b2014-07-14 19:42:55 +00004726 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00004727 // literal will be considered a "mutex."
4728 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004729 SourceLocation LiteralLoc;
4730 if (Attr.getKind() == AttributeList::AT_Capability &&
4731 !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc))
4732 return;
4733
Aaron Ballman6c810072014-03-05 21:47:13 +00004734 // Currently, there are only two names allowed for a capability: role and
4735 // mutex (case insensitive). Diagnose other capability names.
4736 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
4737 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
4738
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004739 D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N,
4740 Attr.getAttributeSpellingListIndex()));
4741}
4742
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004743static void handleAssertCapabilityAttr(Sema &S, Decl *D,
4744 const AttributeList &Attr) {
4745 D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context,
4746 Attr.getArgAsExpr(0),
4747 Attr.getAttributeSpellingListIndex()));
4748}
4749
4750static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
4751 const AttributeList &Attr) {
4752 SmallVector<Expr*, 1> Args;
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004753 if (!checkLockFunAttrCommon(S, D, Attr, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004754 return;
4755
4756 D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(),
4757 S.Context,
4758 Args.data(), Args.size(),
4759 Attr.getAttributeSpellingListIndex()));
4760}
4761
4762static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
4763 const AttributeList &Attr) {
4764 SmallVector<Expr*, 2> Args;
4765 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
4766 return;
4767
4768 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(),
4769 S.Context,
4770 Attr.getArgAsExpr(0),
4771 Args.data(),
4772 Args.size(),
4773 Attr.getAttributeSpellingListIndex()));
4774}
4775
4776static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
4777 const AttributeList &Attr) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004778 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004779 SmallVector<Expr *, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004780 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004781
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004782 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
4783 Attr.getRange(), S.Context, Args.data(), Args.size(),
4784 Attr.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004785}
4786
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004787static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
4788 const AttributeList &Attr) {
4789 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
4790 return;
4791
4792 // check that all arguments are lockable objects
4793 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004794 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004795 if (Args.empty())
4796 return;
4797
4798 RequiresCapabilityAttr *RCA = ::new (S.Context)
4799 RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(),
4800 Args.size(), Attr.getAttributeSpellingListIndex());
4801
4802 D->addAttr(RCA);
4803}
4804
Aaron Ballman43f40102014-11-14 22:34:56 +00004805static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4806 if (auto *NSD = dyn_cast<NamespaceDecl>(D)) {
4807 if (NSD->isAnonymousNamespace()) {
4808 S.Diag(Attr.getLoc(), diag::warn_deprecated_anonymous_namespace);
4809 // Do not want to attach the attribute to the namespace because that will
4810 // cause confusing diagnostic reports for uses of declarations within the
4811 // namespace.
4812 return;
4813 }
4814 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00004815
4816 if (!S.getLangOpts().CPlusPlus14)
4817 if (Attr.isCXX11Attribute() &&
4818 !(Attr.hasScope() && Attr.getScopeName()->isStr("gnu")))
Saleem Abdulrasoolb47d6062015-02-18 04:33:26 +00004819 S.Diag(Attr.getLoc(), diag::ext_deprecated_attr_is_a_cxx14_extension);
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00004820
Aaron Ballman43f40102014-11-14 22:34:56 +00004821 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
4822}
4823
Peter Collingbourne915df992015-05-15 18:33:32 +00004824static void handleNoSanitizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4825 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
4826 return;
4827
4828 std::vector<std::string> Sanitizers;
4829
4830 for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) {
4831 StringRef SanitizerName;
4832 SourceLocation LiteralLoc;
4833
4834 if (!S.checkStringLiteralArgumentAttr(Attr, I, SanitizerName, &LiteralLoc))
4835 return;
4836
4837 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0)
4838 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
4839
4840 Sanitizers.push_back(SanitizerName);
4841 }
4842
4843 D->addAttr(::new (S.Context) NoSanitizeAttr(
4844 Attr.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(),
4845 Attr.getAttributeSpellingListIndex()));
4846}
4847
4848static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
4849 const AttributeList &Attr) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00004850 StringRef AttrName = Attr.getName()->getName();
4851 normalizeName(AttrName);
Peter Collingbourne915df992015-05-15 18:33:32 +00004852 std::string SanitizerName =
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00004853 llvm::StringSwitch<std::string>(AttrName)
Peter Collingbourne915df992015-05-15 18:33:32 +00004854 .Case("no_address_safety_analysis", "address")
4855 .Case("no_sanitize_address", "address")
4856 .Case("no_sanitize_thread", "thread")
Peter Collingbourne94410942015-05-15 20:11:18 +00004857 .Case("no_sanitize_memory", "memory");
Peter Collingbourne915df992015-05-15 18:33:32 +00004858 D->addAttr(::new (S.Context)
4859 NoSanitizeAttr(Attr.getRange(), S.Context, &SanitizerName, 1,
4860 Attr.getAttributeSpellingListIndex()));
4861}
4862
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004863static void handleInternalLinkageAttr(Sema &S, Decl *D,
4864 const AttributeList &Attr) {
4865 if (InternalLinkageAttr *Internal =
4866 S.mergeInternalLinkageAttr(D, Attr.getRange(), Attr.getName(),
4867 Attr.getAttributeSpellingListIndex()))
4868 D->addAttr(Internal);
4869}
4870
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004871/// Handles semantic checking for features that are common to all attributes,
4872/// such as checking whether a parameter was properly specified, or the correct
4873/// number of arguments were passed, etc.
4874static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4875 const AttributeList &Attr) {
4876 // Several attributes carry different semantics than the parsing requires, so
4877 // those are opted out of the common handling.
4878 //
4879 // We also bail on unknown and ignored attributes because those are handled
4880 // as part of the target-specific handling logic.
4881 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004882 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004883 return false;
4884
Aaron Ballman3aff6332013-12-02 19:30:36 +00004885 // Check whether the attribute requires specific language extensions to be
4886 // enabled.
4887 if (!Attr.diagnoseLangOpts(S))
4888 return true;
4889
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00004890 if (Attr.getMinArgs() == Attr.getMaxArgs()) {
4891 // If there are no optional arguments, then checking for the argument count
4892 // is trivial.
4893 if (!checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4894 return true;
4895 } else {
4896 // There are optional arguments, so checking is slightly more involved.
4897 if (Attr.getMinArgs() &&
4898 !checkAttributeAtLeastNumArgs(S, Attr, Attr.getMinArgs()))
4899 return true;
4900 else if (!Attr.hasVariadicArg() && Attr.getMaxArgs() &&
4901 !checkAttributeAtMostNumArgs(S, Attr, Attr.getMaxArgs()))
4902 return true;
4903 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004904
4905 // Check whether the attribute appertains to the given subject.
4906 if (!Attr.diagnoseAppertainsTo(S, D))
4907 return true;
4908
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004909 return false;
4910}
4911
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004912//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004913// Top Level Sema Entry Points
4914//===----------------------------------------------------------------------===//
4915
Richard Smithf8a75c32013-08-29 00:47:48 +00004916/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4917/// the attribute applies to decls. If the attribute is a type attribute, just
4918/// silently ignore it if a GNU attribute.
4919static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4920 const AttributeList &Attr,
4921 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004922 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00004923 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004924
Richard Smithf8a75c32013-08-29 00:47:48 +00004925 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4926 // instead.
4927 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4928 return;
4929
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004930 // Unknown attributes are automatically warned on. Target-specific attributes
4931 // which do not apply to the current target architecture are treated as
4932 // though they were unknown attributes.
4933 if (Attr.getKind() == AttributeList::UnknownAttribute ||
Bob Wilson7c730832015-07-20 22:57:31 +00004934 !Attr.existsInTarget(S.Context.getTargetInfo())) {
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004935 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute()
4936 ? diag::warn_unhandled_ms_attribute_ignored
4937 : diag::warn_unknown_attribute_ignored)
4938 << Attr.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004939 return;
4940 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004941
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004942 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4943 return;
4944
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004945 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004946 default:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004947 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004948 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004949 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004950 case AttributeList::AT_Interrupt:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004951 handleInterruptAttr(S, D, Attr);
4952 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004953 case AttributeList::AT_X86ForceAlignArgPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004954 handleX86ForceAlignArgPointerAttr(S, D, Attr);
4955 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004956 case AttributeList::AT_DLLExport:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004957 case AttributeList::AT_DLLImport:
Hans Wennborge82f19c2014-06-24 23:57:05 +00004958 handleDLLAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004959 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004960 case AttributeList::AT_Mips16:
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00004961 handleMips16Attribute(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004962 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004963 case AttributeList::AT_NoMips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004964 handleSimpleAttribute<NoMips16Attr>(S, D, Attr);
4965 break;
Matt Arsenault43fae6c2014-12-04 20:38:18 +00004966 case AttributeList::AT_AMDGPUNumVGPR:
4967 handleAMDGPUNumVGPRAttr(S, D, Attr);
4968 break;
4969 case AttributeList::AT_AMDGPUNumSGPR:
4970 handleAMDGPUNumSGPRAttr(S, D, Attr);
4971 break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00004972 case AttributeList::AT_IBAction:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004973 handleSimpleAttribute<IBActionAttr>(S, D, Attr);
4974 break;
4975 case AttributeList::AT_IBOutlet:
4976 handleIBOutlet(S, D, Attr);
4977 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004978 case AttributeList::AT_IBOutletCollection:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004979 handleIBOutletCollection(S, D, Attr);
4980 break;
4981 case AttributeList::AT_Alias:
4982 handleAliasAttr(S, D, Attr);
4983 break;
4984 case AttributeList::AT_Aligned:
4985 handleAlignedAttr(S, D, Attr);
4986 break;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00004987 case AttributeList::AT_AlignValue:
4988 handleAlignValueAttr(S, D, Attr);
4989 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004990 case AttributeList::AT_AlwaysInline:
Paul Robinsonf0674352014-03-31 22:29:15 +00004991 handleAlwaysInlineAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004992 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004993 case AttributeList::AT_AnalyzerNoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004994 handleAnalyzerNoReturnAttr(S, D, Attr);
4995 break;
4996 case AttributeList::AT_TLSModel:
4997 handleTLSModelAttr(S, D, Attr);
4998 break;
4999 case AttributeList::AT_Annotate:
5000 handleAnnotateAttr(S, D, Attr);
5001 break;
5002 case AttributeList::AT_Availability:
5003 handleAvailabilityAttr(S, D, Attr);
5004 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005005 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00005006 handleDependencyAttr(S, scope, D, Attr);
5007 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005008 case AttributeList::AT_Common:
5009 handleCommonAttr(S, D, Attr);
5010 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005011 case AttributeList::AT_CUDAConstant:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005012 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr);
5013 break;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00005014 case AttributeList::AT_PassObjectSize:
5015 handlePassObjectSizeAttr(S, D, Attr);
5016 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005017 case AttributeList::AT_Constructor:
5018 handleConstructorAttr(S, D, Attr);
5019 break;
Richard Smith10876ef2013-01-17 01:30:42 +00005020 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005021 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr);
5022 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005023 case AttributeList::AT_Deprecated:
Aaron Ballman43f40102014-11-14 22:34:56 +00005024 handleDeprecatedAttr(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00005025 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005026 case AttributeList::AT_Destructor:
5027 handleDestructorAttr(S, D, Attr);
5028 break;
5029 case AttributeList::AT_EnableIf:
5030 handleEnableIfAttr(S, D, Attr);
5031 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005032 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005033 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005034 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00005035 case AttributeList::AT_MinSize:
Paul Robinsonaae2fba2014-12-10 23:34:36 +00005036 handleMinSizeAttr(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00005037 break;
Paul Robinsonf0674352014-03-31 22:29:15 +00005038 case AttributeList::AT_OptimizeNone:
5039 handleOptimizeNoneAttr(S, D, Attr);
5040 break;
Alexis Hunt724f14e2014-11-28 00:53:20 +00005041 case AttributeList::AT_FlagEnum:
5042 handleSimpleAttribute<FlagEnumAttr>(S, D, Attr);
5043 break;
Peter Collingbourne41af7c22014-05-20 17:12:51 +00005044 case AttributeList::AT_Flatten:
5045 handleSimpleAttribute<FlattenAttr>(S, D, Attr);
5046 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005047 case AttributeList::AT_Format:
5048 handleFormatAttr(S, D, Attr);
5049 break;
5050 case AttributeList::AT_FormatArg:
5051 handleFormatArgAttr(S, D, Attr);
5052 break;
5053 case AttributeList::AT_CUDAGlobal:
5054 handleGlobalAttr(S, D, Attr);
5055 break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00005056 case AttributeList::AT_CUDADevice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005057 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr);
5058 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005059 case AttributeList::AT_CUDAHost:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005060 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr);
5061 break;
5062 case AttributeList::AT_GNUInline:
5063 handleGNUInlineAttr(S, D, Attr);
5064 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005065 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005066 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00005067 break;
David Majnemer631a90b2015-02-04 07:23:21 +00005068 case AttributeList::AT_Restrict:
5069 handleRestrictAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005070 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005071 case AttributeList::AT_MayAlias:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005072 handleSimpleAttribute<MayAliasAttr>(S, D, Attr);
5073 break;
5074 case AttributeList::AT_Mode:
5075 handleModeAttr(S, D, Attr);
5076 break;
David Majnemer1bf0f8e2015-07-20 22:51:52 +00005077 case AttributeList::AT_NoAlias:
5078 handleSimpleAttribute<NoAliasAttr>(S, D, Attr);
5079 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005080 case AttributeList::AT_NoCommon:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005081 handleSimpleAttribute<NoCommonAttr>(S, D, Attr);
5082 break;
Peter Collingbourneb4728c12014-05-19 22:14:34 +00005083 case AttributeList::AT_NoSplitStack:
5084 handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr);
5085 break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00005086 case AttributeList::AT_NonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005087 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
5088 handleNonNullAttrParameter(S, PVD, Attr);
5089 else
5090 handleNonNullAttr(S, D, Attr);
5091 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00005092 case AttributeList::AT_ReturnsNonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005093 handleReturnsNonNullAttr(S, D, Attr);
5094 break;
Hal Finkelee90a222014-09-26 05:04:30 +00005095 case AttributeList::AT_AssumeAligned:
5096 handleAssumeAlignedAttr(S, D, Attr);
5097 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005098 case AttributeList::AT_Overloadable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005099 handleSimpleAttribute<OverloadableAttr>(S, D, Attr);
5100 break;
5101 case AttributeList::AT_Ownership:
5102 handleOwnershipAttr(S, D, Attr);
5103 break;
5104 case AttributeList::AT_Cold:
5105 handleColdAttr(S, D, Attr);
5106 break;
5107 case AttributeList::AT_Hot:
5108 handleHotAttr(S, D, Attr);
5109 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005110 case AttributeList::AT_Naked:
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00005111 handleNakedAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005112 break;
5113 case AttributeList::AT_NoReturn:
5114 handleNoReturnAttr(S, D, Attr);
5115 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00005116 case AttributeList::AT_NoThrow:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005117 handleSimpleAttribute<NoThrowAttr>(S, D, Attr);
5118 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005119 case AttributeList::AT_CUDAShared:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005120 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr);
5121 break;
5122 case AttributeList::AT_VecReturn:
5123 handleVecReturnAttr(S, D, Attr);
5124 break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005125
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005126 case AttributeList::AT_ObjCOwnership:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005127 handleObjCOwnershipAttr(S, D, Attr);
5128 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005129 case AttributeList::AT_ObjCPreciseLifetime:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005130 handleObjCPreciseLifetimeAttr(S, D, Attr);
5131 break;
John McCall31168b02011-06-15 23:02:42 +00005132
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005133 case AttributeList::AT_ObjCReturnsInnerPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005134 handleObjCReturnsInnerPointerAttr(S, D, Attr);
5135 break;
John McCallcf166702011-07-22 08:53:00 +00005136
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005137 case AttributeList::AT_ObjCRequiresSuper:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005138 handleObjCRequiresSuperAttr(S, D, Attr);
5139 break;
5140
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005141 case AttributeList::AT_ObjCBridge:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005142 handleObjCBridgeAttr(S, scope, D, Attr);
5143 break;
5144
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005145 case AttributeList::AT_ObjCBridgeMutable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005146 handleObjCBridgeMutableAttr(S, scope, D, Attr);
5147 break;
5148
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005149 case AttributeList::AT_ObjCBridgeRelated:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005150 handleObjCBridgeRelatedAttr(S, scope, D, Attr);
5151 break;
John McCallf1e8b342011-09-29 07:17:38 +00005152
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005153 case AttributeList::AT_ObjCDesignatedInitializer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005154 handleObjCDesignatedInitializer(S, D, Attr);
5155 break;
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005156
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00005157 case AttributeList::AT_ObjCRuntimeName:
5158 handleObjCRuntimeName(S, D, Attr);
5159 break;
Alex Denisovfde64952015-06-26 05:28:36 +00005160
5161 case AttributeList::AT_ObjCBoxable:
5162 handleObjCBoxable(S, D, Attr);
5163 break;
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00005164
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005165 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005166 handleCFAuditedTransferAttr(S, D, Attr);
5167 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005168 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005169 handleCFUnknownTransferAttr(S, D, Attr);
5170 break;
John McCall32f5fe12011-09-30 05:12:12 +00005171
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005172 case AttributeList::AT_CFConsumed:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005173 case AttributeList::AT_NSConsumed:
5174 handleNSConsumedAttr(S, D, Attr);
5175 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005176 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005177 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr);
5178 break;
John McCalled433932011-01-25 03:31:58 +00005179
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005180 case AttributeList::AT_NSReturnsAutoreleased:
5181 case AttributeList::AT_NSReturnsNotRetained:
5182 case AttributeList::AT_CFReturnsNotRetained:
5183 case AttributeList::AT_NSReturnsRetained:
5184 case AttributeList::AT_CFReturnsRetained:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005185 handleNSReturnsRetainedAttr(S, D, Attr);
5186 break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00005187 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005188 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr);
5189 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005190 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005191 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr);
5192 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00005193 case AttributeList::AT_VecTypeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005194 handleVecTypeHint(S, D, Attr);
5195 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00005196
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005197 case AttributeList::AT_InitPriority:
5198 handleInitPriorityAttr(S, D, Attr);
5199 break;
5200
5201 case AttributeList::AT_Packed:
5202 handlePackedAttr(S, D, Attr);
5203 break;
5204 case AttributeList::AT_Section:
5205 handleSectionAttr(S, D, Attr);
5206 break;
Eric Christopher11acf732015-06-12 01:35:52 +00005207 case AttributeList::AT_Target:
5208 handleTargetAttr(S, D, Attr);
5209 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005210 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00005211 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00005212 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005213 case AttributeList::AT_ArcWeakrefUnavailable:
5214 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr);
5215 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005216 case AttributeList::AT_ObjCRootClass:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005217 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr);
5218 break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00005219 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek438f8db2014-02-22 01:06:05 +00005220 handleObjCSuppresProtocolAttr(S, D, Attr);
Ted Kremenek28eace62013-11-23 01:01:34 +00005221 break;
5222 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005223 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr);
5224 break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00005225 case AttributeList::AT_Unused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005226 handleSimpleAttribute<UnusedAttr>(S, D, Attr);
5227 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005228 case AttributeList::AT_ReturnsTwice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005229 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr);
5230 break;
Akira Hatanakac8667622015-11-06 23:56:15 +00005231 case AttributeList::AT_NotTailCalled:
5232 handleNotTailCalledAttr(S, D, Attr);
5233 break;
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00005234 case AttributeList::AT_DisableTailCalls:
5235 handleDisableTailCallsAttr(S, D, Attr);
5236 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005237 case AttributeList::AT_Used:
5238 handleUsedAttr(S, D, Attr);
5239 break;
John McCalld041a9b2013-02-20 01:54:26 +00005240 case AttributeList::AT_Visibility:
5241 handleVisibilityAttr(S, D, Attr, false);
5242 break;
5243 case AttributeList::AT_TypeVisibility:
5244 handleVisibilityAttr(S, D, Attr, true);
5245 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00005246 case AttributeList::AT_WarnUnused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005247 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr);
5248 break;
5249 case AttributeList::AT_WarnUnusedResult:
5250 handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00005251 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00005252 case AttributeList::AT_Weak:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005253 handleSimpleAttribute<WeakAttr>(S, D, Attr);
5254 break;
5255 case AttributeList::AT_WeakRef:
5256 handleWeakRefAttr(S, D, Attr);
5257 break;
5258 case AttributeList::AT_WeakImport:
5259 handleWeakImportAttr(S, D, Attr);
5260 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005261 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005262 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005263 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005264 case AttributeList::AT_ObjCException:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005265 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr);
5266 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005267 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005268 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00005269 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005270 case AttributeList::AT_ObjCNSObject:
5271 handleObjCNSObject(S, D, Attr);
5272 break;
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00005273 case AttributeList::AT_ObjCIndependentClass:
5274 handleObjCIndependentClass(S, D, Attr);
5275 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005276 case AttributeList::AT_Blocks:
5277 handleBlocksAttr(S, D, Attr);
5278 break;
5279 case AttributeList::AT_Sentinel:
5280 handleSentinelAttr(S, D, Attr);
5281 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00005282 case AttributeList::AT_Const:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005283 handleSimpleAttribute<ConstAttr>(S, D, Attr);
5284 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005285 case AttributeList::AT_Pure:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005286 handleSimpleAttribute<PureAttr>(S, D, Attr);
5287 break;
5288 case AttributeList::AT_Cleanup:
5289 handleCleanupAttr(S, D, Attr);
5290 break;
5291 case AttributeList::AT_NoDebug:
5292 handleNoDebugAttr(S, D, Attr);
5293 break;
Aaron Ballman7c19ab12014-02-22 16:59:24 +00005294 case AttributeList::AT_NoDuplicate:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005295 handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr);
5296 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005297 case AttributeList::AT_NoInline:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005298 handleSimpleAttribute<NoInlineAttr>(S, D, Attr);
5299 break;
5300 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
5301 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr);
5302 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005303 case AttributeList::AT_StdCall:
5304 case AttributeList::AT_CDecl:
5305 case AttributeList::AT_FastCall:
5306 case AttributeList::AT_ThisCall:
5307 case AttributeList::AT_Pascal:
Reid Klecknerd7857f02014-10-24 17:42:17 +00005308 case AttributeList::AT_VectorCall:
Charles Davisb5a214e2013-08-30 04:39:01 +00005309 case AttributeList::AT_MSABI:
5310 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005311 case AttributeList::AT_Pcs:
Guy Benyeif0a014b2012-12-25 08:53:55 +00005312 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005313 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00005314 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005315 case AttributeList::AT_OpenCLKernel:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005316 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr);
5317 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00005318 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005319 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr);
5320 break;
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005321 case AttributeList::AT_InternalLinkage:
5322 handleInternalLinkageAttr(S, D, Attr);
5323 break;
John McCall8d32c052012-05-22 21:28:12 +00005324
5325 // Microsoft attributes:
David Majnemer8ab003a2015-02-02 19:30:52 +00005326 case AttributeList::AT_MSNoVTable:
5327 handleSimpleAttribute<MSNoVTableAttr>(S, D, Attr);
David Majnemer129f4172015-02-02 10:22:20 +00005328 break;
David Majnemer8ab003a2015-02-02 19:30:52 +00005329 case AttributeList::AT_MSStruct:
5330 handleSimpleAttribute<MSStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00005331 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005332 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005333 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00005334 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00005335 case AttributeList::AT_MSInheritance:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005336 handleMSInheritanceAttr(S, D, Attr);
5337 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00005338 case AttributeList::AT_SelectAny:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005339 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr);
5340 break;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005341 case AttributeList::AT_Thread:
5342 handleDeclspecThreadAttr(S, D, Attr);
5343 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005344
5345 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00005346 case AttributeList::AT_AssertExclusiveLock:
5347 handleAssertExclusiveLockAttr(S, D, Attr);
5348 break;
5349 case AttributeList::AT_AssertSharedLock:
5350 handleAssertSharedLockAttr(S, D, Attr);
5351 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005352 case AttributeList::AT_GuardedVar:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005353 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr);
5354 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005355 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00005356 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005357 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005358 case AttributeList::AT_ScopedLockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005359 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr);
5360 break;
Peter Collingbourne915df992015-05-15 18:33:32 +00005361 case AttributeList::AT_NoSanitize:
5362 handleNoSanitizeAttr(S, D, Attr);
5363 break;
5364 case AttributeList::AT_NoSanitizeSpecific:
5365 handleNoSanitizeSpecificAttr(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00005366 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005367 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005368 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00005369 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005370 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005371 handleGuardedByAttr(S, D, Attr);
5372 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005373 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00005374 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005375 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005376 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005377 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005378 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005379 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005380 handleLockReturnedAttr(S, D, Attr);
5381 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005382 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005383 handleLocksExcludedAttr(S, D, Attr);
5384 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005385 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005386 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005387 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005388 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00005389 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005390 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005391 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00005392 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005393 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005394
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005395 // Capability analysis attributes.
5396 case AttributeList::AT_Capability:
5397 case AttributeList::AT_Lockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005398 handleCapabilityAttr(S, D, Attr);
5399 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005400 case AttributeList::AT_RequiresCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005401 handleRequiresCapabilityAttr(S, D, Attr);
5402 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005403
5404 case AttributeList::AT_AssertCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005405 handleAssertCapabilityAttr(S, D, Attr);
5406 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005407 case AttributeList::AT_AcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005408 handleAcquireCapabilityAttr(S, D, Attr);
5409 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005410 case AttributeList::AT_ReleaseCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005411 handleReleaseCapabilityAttr(S, D, Attr);
5412 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005413 case AttributeList::AT_TryAcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005414 handleTryAcquireCapabilityAttr(S, D, Attr);
5415 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005416
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00005417 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00005418 case AttributeList::AT_Consumable:
5419 handleConsumableAttr(S, D, Attr);
5420 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00005421 case AttributeList::AT_ConsumableAutoCast:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005422 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr);
5423 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00005424 case AttributeList::AT_ConsumableSetOnRead:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005425 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr);
5426 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00005427 case AttributeList::AT_CallableWhen:
5428 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00005429 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00005430 case AttributeList::AT_ParamTypestate:
5431 handleParamTypestateAttr(S, D, Attr);
5432 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00005433 case AttributeList::AT_ReturnTypestate:
5434 handleReturnTypestateAttr(S, D, Attr);
5435 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00005436 case AttributeList::AT_SetTypestate:
5437 handleSetTypestateAttr(S, D, Attr);
5438 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00005439 case AttributeList::AT_TestTypestate:
5440 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00005441 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00005442
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00005443 // Type safety attributes.
5444 case AttributeList::AT_ArgumentWithTypeTag:
5445 handleArgumentWithTypeTagAttr(S, D, Attr);
5446 break;
5447 case AttributeList::AT_TypeTagForDatatype:
5448 handleTypeTagForDatatypeAttr(S, D, Attr);
5449 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005450 }
5451}
5452
5453/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5454/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00005455void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00005456 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00005457 bool IncludeCXX11Attributes) {
5458 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00005459 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00005460
Joey Gouly2cd9db12013-12-13 16:15:28 +00005461 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00005462 // GCC accepts
5463 // static int a9 __attribute__((weakref));
5464 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00005465 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00005466 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
5467 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00005468 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00005469 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005470 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00005471
Aaron Ballmanbe243a72014-12-04 22:45:31 +00005472 // FIXME: We should be able to handle this in TableGen as well. It would be
5473 // good to have a way to specify "these attributes must appear as a group",
5474 // for these. Additionally, it would be good to have a way to specify "these
5475 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00005476 if (!D->hasAttr<OpenCLKernelAttr>()) {
5477 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00005478 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00005479 // FIXME: This emits a different error message than
5480 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00005481 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005482 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00005483 } else if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00005484 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005485 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00005486 } else if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00005487 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005488 D->setInvalidDecl();
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00005489 } else if (Attr *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
5490 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
5491 << A << ExpectedKernelFunction;
5492 D->setInvalidDecl();
5493 } else if (Attr *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
5494 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
5495 << A << ExpectedKernelFunction;
5496 D->setInvalidDecl();
Joey Gouly2cd9db12013-12-13 16:15:28 +00005497 }
5498 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005499}
5500
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005501// Annotation attributes are the only attributes allowed after an access
5502// specifier.
5503bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5504 const AttributeList *AttrList) {
5505 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005506 if (l->getKind() == AttributeList::AT_Annotate) {
David Majnemer706f3152014-12-14 01:05:01 +00005507 ProcessDeclAttribute(*this, nullptr, ASDecl, *l, l->isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005508 } else {
5509 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5510 return true;
5511 }
5512 }
5513
5514 return false;
5515}
5516
John McCall42856de2011-10-01 05:17:03 +00005517/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5518/// contains any decl attributes that we should warn about.
5519static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5520 for ( ; A; A = A->getNext()) {
5521 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00005522 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00005523 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5524
5525 if (A->getKind() == AttributeList::UnknownAttribute) {
5526 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5527 << A->getName() << A->getRange();
5528 } else {
5529 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5530 << A->getName() << A->getRange();
5531 }
5532 }
5533}
5534
5535/// checkUnusedDeclAttributes - Given a declarator which is not being
5536/// used to build a declaration, complain about any decl attributes
5537/// which might be lying around on it.
5538void Sema::checkUnusedDeclAttributes(Declarator &D) {
5539 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5540 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5541 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5542 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5543}
5544
Ryan Flynn7d470f32009-07-30 03:15:39 +00005545/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00005546/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00005547NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5548 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00005549 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00005550 NamedDecl *NewD = nullptr;
Ryan Flynn7d470f32009-07-30 03:15:39 +00005551 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00005552 // FIXME: Mangling?
5553 // FIXME: Is the qualifier info correct?
5554 // FIXME: Is the DeclContext correct?
Alexander Musmanf97c8932015-11-26 09:34:30 +00005555
5556 LookupResult Previous(*this, II, Loc, LookupOrdinaryName);
5557 LookupParsedName(Previous, TUScope, nullptr, true);
5558
5559 auto NewFD = FunctionDecl::Create(
5560 FD->getASTContext(), FD->getDeclContext(), Loc, Loc,
5561 DeclarationName(II), FD->getType(), FD->getTypeSourceInfo(), SC_None,
5562 false /*isInlineSpecified*/, FD->hasPrototype(),
5563 false /*isConstexprSpecified*/);
5564
5565 CheckFunctionDeclaration(TUScope, NewFD, Previous,
5566 false /*IsExplicitSpecialization*/);
5567
Eli Friedmance3e2c82011-09-07 04:05:06 +00005568 NewD = NewFD;
5569
5570 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00005571 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00005572
5573 // Fake up parameter variables; they are declared as if this were
5574 // a typedef.
5575 QualType FDTy = FD->getType();
5576 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5577 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00005578 for (const auto &AI : FT->param_types()) {
5579 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00005580 Param->setScopeInfo(0, Params.size());
5581 Params.push_back(Param);
5582 }
David Blaikie9c70e042011-09-21 18:16:56 +00005583 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00005584 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005585 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5586 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00005587 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00005588 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005589 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00005590 if (VD->getQualifier()) {
5591 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00005592 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00005593 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005594 }
5595 return NewD;
5596}
5597
James Dennett634962f2012-06-14 21:40:34 +00005598/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00005599/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00005600void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00005601 if (W.getUsed()) return; // only do this once
5602 W.setUsed(true);
5603 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5604 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00005605 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00005606 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
5607 W.getLocation()));
5608 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00005609 WeakTopLevelDecl.push_back(NewD);
5610 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5611 // to insert Decl at TU scope, sorry.
5612 DeclContext *SavedContext = CurContext;
5613 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00005614 NewD->setDeclContext(CurContext);
5615 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00005616 PushOnScopeChains(NewD, S);
5617 CurContext = SavedContext;
5618 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00005619 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005620 }
5621}
5622
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005623void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5624 // It's valid to "forward-declare" #pragma weak, in which case we
5625 // have to do this.
5626 LoadExternalWeakUndeclaredIdentifiers();
5627 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00005628 NamedDecl *ND = nullptr;
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005629 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5630 if (VD->isExternC())
5631 ND = VD;
5632 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5633 if (FD->isExternC())
5634 ND = FD;
5635 if (ND) {
5636 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00005637 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005638 if (I != WeakUndeclaredIdentifiers.end()) {
5639 WeakInfo W = I->second;
5640 DeclApplyPragmaWeak(S, ND, W);
5641 WeakUndeclaredIdentifiers[Id] = W;
5642 }
5643 }
5644 }
5645 }
5646}
5647
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005648/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5649/// it, apply them to D. This is a bit tricky because PD can have attributes
5650/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00005651void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005652 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00005653 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00005654 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005655
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005656 // Walk the declarator structure, applying decl attributes that were in a type
5657 // position to the decl itself. This handles cases like:
5658 // int *__attr__(x)** D;
5659 // when X is a decl attribute.
5660 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5661 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00005662 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005663
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005664 // Finally, apply any attributes on the decl itself.
5665 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00005666 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005667}
John McCall28a6aea2009-11-04 02:18:39 +00005668
John McCall31168b02011-06-15 23:02:42 +00005669/// Is the given declaration allowed to use a forbidden type?
John McCallb61e14e2015-10-27 04:54:50 +00005670/// If so, it'll still be annotated with an attribute that makes it
5671/// illegal to actually use.
5672static bool isForbiddenTypeAllowed(Sema &S, Decl *decl,
5673 const DelayedDiagnostic &diag,
John McCallc6af8c62015-10-28 05:03:19 +00005674 UnavailableAttr::ImplicitReason &reason) {
John McCall31168b02011-06-15 23:02:42 +00005675 // Private ivars are always okay. Unfortunately, people don't
5676 // always properly make their ivars private, even in system headers.
5677 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005678 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5679 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005680 return false;
5681
John McCallc6af8c62015-10-28 05:03:19 +00005682 // Silently accept unsupported uses of __weak in both user and system
5683 // declarations when it's been disabled, for ease of integration with
5684 // -fno-objc-arc files. We do have to take some care against attempts
5685 // to define such things; for now, we've only done that for ivars
5686 // and properties.
5687 if ((isa<ObjCIvarDecl>(decl) || isa<ObjCPropertyDecl>(decl))) {
5688 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
5689 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
5690 reason = UnavailableAttr::IR_ForbiddenWeak;
5691 return true;
5692 }
John McCallb61e14e2015-10-27 04:54:50 +00005693 }
5694
John McCallc6af8c62015-10-28 05:03:19 +00005695 // Allow all sorts of things in system headers.
5696 if (S.Context.getSourceManager().isInSystemHeader(decl->getLocation())) {
5697 // Currently, all the failures dealt with this way are due to ARC
5698 // restrictions.
5699 reason = UnavailableAttr::IR_ARCForbiddenType;
5700 return true;
John McCallb61e14e2015-10-27 04:54:50 +00005701 }
5702
5703 return false;
John McCall31168b02011-06-15 23:02:42 +00005704}
5705
5706/// Handle a delayed forbidden-type diagnostic.
5707static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5708 Decl *decl) {
John McCallc6af8c62015-10-28 05:03:19 +00005709 auto reason = UnavailableAttr::IR_None;
5710 if (decl && isForbiddenTypeAllowed(S, decl, diag, reason)) {
5711 assert(reason && "didn't set reason?");
5712 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", reason,
5713 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00005714 return;
5715 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005716 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005717 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005718 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005719 // kind of forbidden type messages on unavailable functions.
5720 if (FD->hasAttr<UnavailableAttr>() &&
5721 diag.getForbiddenTypeDiagnostic() ==
5722 diag::err_arc_array_param_no_ownership) {
5723 diag.Triggered = true;
5724 return;
5725 }
5726 }
John McCall31168b02011-06-15 23:02:42 +00005727
5728 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5729 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5730 diag.Triggered = true;
5731}
5732
Aaron Ballmanfb237522014-10-15 15:37:51 +00005733
5734static bool isDeclDeprecated(Decl *D) {
5735 do {
5736 if (D->isDeprecated())
5737 return true;
5738 // A category implicitly has the availability of the interface.
5739 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005740 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
5741 return Interface->isDeprecated();
Aaron Ballmanfb237522014-10-15 15:37:51 +00005742 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5743 return false;
5744}
5745
5746static bool isDeclUnavailable(Decl *D) {
5747 do {
5748 if (D->isUnavailable())
5749 return true;
5750 // A category implicitly has the availability of the interface.
5751 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005752 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
5753 return Interface->isUnavailable();
Aaron Ballmanfb237522014-10-15 15:37:51 +00005754 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5755 return false;
5756}
5757
Nico Weber0055a192015-03-19 19:18:22 +00005758static void DoEmitAvailabilityWarning(Sema &S, Sema::AvailabilityDiagnostic K,
Aaron Ballmanfb237522014-10-15 15:37:51 +00005759 Decl *Ctx, const NamedDecl *D,
5760 StringRef Message, SourceLocation Loc,
5761 const ObjCInterfaceDecl *UnknownObjCClass,
5762 const ObjCPropertyDecl *ObjCProperty,
5763 bool ObjCPropertyAccess) {
5764 // Diagnostics for deprecated or unavailable.
5765 unsigned diag, diag_message, diag_fwdclass_message;
John McCallb61e14e2015-10-27 04:54:50 +00005766 unsigned diag_available_here = diag::note_availability_specified_here;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005767
5768 // Matches 'diag::note_property_attribute' options.
5769 unsigned property_note_select;
5770
5771 // Matches diag::note_availability_specified_here.
5772 unsigned available_here_select_kind;
5773
5774 // Don't warn if our current context is deprecated or unavailable.
5775 switch (K) {
Nico Weber0055a192015-03-19 19:18:22 +00005776 case Sema::AD_Deprecation:
Jordan Rosed17c03e2015-04-30 17:20:35 +00005777 if (isDeclDeprecated(Ctx) || isDeclUnavailable(Ctx))
Aaron Ballmanfb237522014-10-15 15:37:51 +00005778 return;
5779 diag = !ObjCPropertyAccess ? diag::warn_deprecated
5780 : diag::warn_property_method_deprecated;
5781 diag_message = diag::warn_deprecated_message;
5782 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
5783 property_note_select = /* deprecated */ 0;
5784 available_here_select_kind = /* deprecated */ 2;
5785 break;
5786
Nico Weber0055a192015-03-19 19:18:22 +00005787 case Sema::AD_Unavailable:
Aaron Ballmanfb237522014-10-15 15:37:51 +00005788 if (isDeclUnavailable(Ctx))
5789 return;
5790 diag = !ObjCPropertyAccess ? diag::err_unavailable
5791 : diag::err_property_method_unavailable;
5792 diag_message = diag::err_unavailable_message;
5793 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
5794 property_note_select = /* unavailable */ 1;
5795 available_here_select_kind = /* unavailable */ 0;
John McCallb61e14e2015-10-27 04:54:50 +00005796
John McCallc6af8c62015-10-28 05:03:19 +00005797 if (auto attr = D->getAttr<UnavailableAttr>()) {
5798 if (attr->isImplicit() && attr->getImplicitReason()) {
5799 // Most of these failures are due to extra restrictions in ARC;
5800 // reflect that in the primary diagnostic when applicable.
5801 auto flagARCError = [&] {
5802 if (S.getLangOpts().ObjCAutoRefCount &&
5803 S.getSourceManager().isInSystemHeader(D->getLocation()))
5804 diag = diag::err_unavailable_in_arc;
5805 };
5806
5807 switch (attr->getImplicitReason()) {
5808 case UnavailableAttr::IR_None: break;
5809
5810 case UnavailableAttr::IR_ARCForbiddenType:
5811 flagARCError();
5812 diag_available_here = diag::note_arc_forbidden_type;
5813 break;
5814
5815 case UnavailableAttr::IR_ForbiddenWeak:
5816 if (S.getLangOpts().ObjCWeakRuntime)
5817 diag_available_here = diag::note_arc_weak_disabled;
5818 else
5819 diag_available_here = diag::note_arc_weak_no_runtime;
5820 break;
5821
5822 case UnavailableAttr::IR_ARCForbiddenConversion:
5823 flagARCError();
5824 diag_available_here = diag::note_performs_forbidden_arc_conversion;
5825 break;
5826
5827 case UnavailableAttr::IR_ARCInitReturnsUnrelated:
5828 flagARCError();
5829 diag_available_here = diag::note_arc_init_returns_unrelated;
5830 break;
5831
5832 case UnavailableAttr::IR_ARCFieldWithOwnership:
5833 flagARCError();
5834 diag_available_here = diag::note_arc_field_with_ownership;
5835 break;
5836 }
5837 }
John McCallb61e14e2015-10-27 04:54:50 +00005838 }
5839
Aaron Ballmanfb237522014-10-15 15:37:51 +00005840 break;
5841
Nico Weber0055a192015-03-19 19:18:22 +00005842 case Sema::AD_Partial:
5843 diag = diag::warn_partial_availability;
5844 diag_message = diag::warn_partial_message;
5845 diag_fwdclass_message = diag::warn_partial_fwdclass_message;
5846 property_note_select = /* partial */ 2;
5847 available_here_select_kind = /* partial */ 3;
5848 break;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005849 }
5850
Aaron Ballmanfb237522014-10-15 15:37:51 +00005851 if (!Message.empty()) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005852 S.Diag(Loc, diag_message) << D << Message;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005853 if (ObjCProperty)
5854 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5855 << ObjCProperty->getDeclName() << property_note_select;
5856 } else if (!UnknownObjCClass) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005857 S.Diag(Loc, diag) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005858 if (ObjCProperty)
5859 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5860 << ObjCProperty->getDeclName() << property_note_select;
5861 } else {
Aaron Ballman43f40102014-11-14 22:34:56 +00005862 S.Diag(Loc, diag_fwdclass_message) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005863 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5864 }
5865
John McCallb61e14e2015-10-27 04:54:50 +00005866 S.Diag(D->getLocation(), diag_available_here)
Aaron Ballmanfb237522014-10-15 15:37:51 +00005867 << D << available_here_select_kind;
Nico Weber0055a192015-03-19 19:18:22 +00005868 if (K == Sema::AD_Partial)
5869 S.Diag(Loc, diag::note_partial_availability_silence) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005870}
5871
5872static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
5873 Decl *Ctx) {
Nico Weber0055a192015-03-19 19:18:22 +00005874 assert(DD.Kind == DelayedDiagnostic::Deprecation ||
5875 DD.Kind == DelayedDiagnostic::Unavailable);
5876 Sema::AvailabilityDiagnostic AD = DD.Kind == DelayedDiagnostic::Deprecation
5877 ? Sema::AD_Deprecation
5878 : Sema::AD_Unavailable;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005879 DD.Triggered = true;
Nico Weber0055a192015-03-19 19:18:22 +00005880 DoEmitAvailabilityWarning(
5881 S, AD, Ctx, DD.getDeprecationDecl(), DD.getDeprecationMessage(), DD.Loc,
5882 DD.getUnknownObjCClass(), DD.getObjCProperty(), false);
Aaron Ballmanfb237522014-10-15 15:37:51 +00005883}
5884
John McCall2ec85372012-05-07 06:16:41 +00005885void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5886 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005887 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005888 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005889
John McCall2ec85372012-05-07 06:16:41 +00005890 // When delaying diagnostics to run in the context of a parsed
5891 // declaration, we only want to actually emit anything if parsing
5892 // succeeds.
5893 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005894
John McCall2ec85372012-05-07 06:16:41 +00005895 // We emit all the active diagnostics in this pool or any of its
5896 // parents. In general, we'll get one pool for the decl spec
5897 // and a child pool for each declarator; in a decl group like:
5898 // deprecated_typedef foo, *bar, baz();
5899 // only the declarator pops will be passed decls. This is correct;
5900 // we really do need to consider delayed diagnostics from the decl spec
5901 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00005902 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00005903 do {
John McCall6347b682012-05-07 06:16:58 +00005904 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00005905 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5906 // This const_cast is a bit lame. Really, Triggered should be mutable.
5907 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00005908 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00005909 continue;
5910
John McCallc1465822011-02-14 07:13:47 +00005911 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00005912 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00005913 case DelayedDiagnostic::Unavailable:
5914 // Don't bother giving deprecation/unavailable diagnostics if
5915 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00005916 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00005917 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00005918 break;
5919
5920 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00005921 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005922 break;
John McCall31168b02011-06-15 23:02:42 +00005923
5924 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00005925 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00005926 break;
John McCall86121512010-01-27 03:50:35 +00005927 }
5928 }
John McCall2ec85372012-05-07 06:16:41 +00005929 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00005930}
5931
John McCall6347b682012-05-07 06:16:58 +00005932/// Given a set of delayed diagnostics, re-emit them as if they had
5933/// been delayed in the current context instead of in the given pool.
5934/// Essentially, this just moves them to the current pool.
5935void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5936 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5937 assert(curPool && "re-emitting in undelayed context not supported");
5938 curPool->steal(pool);
5939}
5940
Ted Kremenekb79ee572013-12-18 23:30:06 +00005941void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
5942 NamedDecl *D, StringRef Message,
5943 SourceLocation Loc,
5944 const ObjCInterfaceDecl *UnknownObjCClass,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00005945 const ObjCPropertyDecl *ObjCProperty,
5946 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00005947 // Delay if we're currently parsing a declaration.
Nico Weber0055a192015-03-19 19:18:22 +00005948 if (DelayedDiagnostics.shouldDelayDiagnostics() && AD != AD_Partial) {
Nico Weber462fd1e2015-01-07 23:50:05 +00005949 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(
5950 AD, Loc, D, UnknownObjCClass, ObjCProperty, Message,
5951 ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00005952 return;
5953 }
5954
Ted Kremenekb79ee572013-12-18 23:30:06 +00005955 Decl *Ctx = cast<Decl>(getCurLexicalContext());
Nico Weber0055a192015-03-19 19:18:22 +00005956 DoEmitAvailabilityWarning(*this, AD, Ctx, D, Message, Loc, UnknownObjCClass,
5957 ObjCProperty, ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00005958}