blob: fed22c934ce1375a80e4bece78fcadb376c86cdc [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)) {
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001076 // Report warning about changed offset in the newer compiler versions.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001077 if (!FD->getType()->isDependentType() &&
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001078 !FD->getType()->isIncompleteType() && FD->isBitField() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001079 S.Context.getTypeAlign(FD->getType()) <= 8)
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001080 S.Diag(Attr.getLoc(), diag::warn_attribute_packed_for_bitfield);
1081
1082 FD->addAttr(::new (S.Context) PackedAttr(
1083 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001084 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001085 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001086}
1087
Ted Kremenek7fd17232011-09-29 07:02:25 +00001088static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1089 // The IBOutlet/IBOutletCollection attributes only apply to instance
1090 // variables or properties of Objective-C classes. The outlet must also
1091 // have an object reference type.
1092 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1093 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001094 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001095 << Attr.getName() << VD->getType() << 0;
1096 return false;
1097 }
1098 }
1099 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1100 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001101 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001102 << Attr.getName() << PD->getType() << 1;
1103 return false;
1104 }
1105 }
1106 else {
1107 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1108 return false;
1109 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001110
Ted Kremenek7fd17232011-09-29 07:02:25 +00001111 return true;
1112}
1113
Chandler Carruthedc2c642011-07-02 00:01:44 +00001114static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001115 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001116 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001117
Michael Han99315932013-01-24 16:46:58 +00001118 D->addAttr(::new (S.Context)
1119 IBOutletAttr(Attr.getRange(), S.Context,
1120 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001121}
1122
Chandler Carruthedc2c642011-07-02 00:01:44 +00001123static void handleIBOutletCollection(Sema &S, Decl *D,
1124 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001125
1126 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001127 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001128 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1129 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001130 return;
1131 }
1132
Ted Kremenek7fd17232011-09-29 07:02:25 +00001133 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001134 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001135
Richard Smithb1f9a282013-10-31 01:56:18 +00001136 ParsedType PT;
1137
1138 if (Attr.hasParsedType())
1139 PT = Attr.getTypeArg();
1140 else {
1141 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1142 S.getScopeForContext(D->getDeclContext()->getParent()));
1143 if (!PT) {
1144 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1145 return;
1146 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001147 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001148
Craig Topperc3ec1492014-05-26 06:22:03 +00001149 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001150 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1151 if (!QTLoc)
1152 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001153
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001154 // Diagnose use of non-object type in iboutletcollection attribute.
1155 // FIXME. Gnu attribute extension ignores use of builtin types in
1156 // attributes. So, __attribute__((iboutletcollection(char))) will be
1157 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001158 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001159 S.Diag(Attr.getLoc(),
1160 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1161 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001162 return;
1163 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001164
Michael Han99315932013-01-24 16:46:58 +00001165 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001166 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001167 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001168}
1169
Hal Finkelee90a222014-09-26 05:04:30 +00001170bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1171 if (RefOkay) {
1172 if (T->isReferenceType())
1173 return true;
1174 } else {
1175 T = T.getNonReferenceType();
1176 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001177
Hal Finkelee90a222014-09-26 05:04:30 +00001178 // The nonnull attribute, and other similar attributes, can be applied to a
1179 // transparent union that contains a pointer type.
Richard Smith588bd9b2014-08-27 04:59:42 +00001180 if (const RecordType *UT = T->getAsUnionType()) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001181 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1182 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001183 for (const auto *I : UD->fields()) {
1184 QualType QT = I->getType();
Richard Smith588bd9b2014-08-27 04:59:42 +00001185 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1186 return true;
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001187 }
1188 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001189 }
1190
1191 return T->isAnyPointerType() || T->isBlockPointerType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001192}
1193
Ted Kremenek9aedc152014-01-17 06:24:56 +00001194static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001195 SourceRange AttrParmRange,
Hal Finkelee90a222014-09-26 05:04:30 +00001196 SourceRange TypeRange,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001197 bool isReturnValue = false) {
Hal Finkelee90a222014-09-26 05:04:30 +00001198 if (!S.isValidPointerAttrType(T)) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001199 if (isReturnValue)
1200 S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only)
1201 << Attr.getName() << AttrParmRange << TypeRange;
1202 else
1203 S.Diag(Attr.getLoc(), diag::warn_attribute_pointers_only)
1204 << Attr.getName() << AttrParmRange << TypeRange << 0;
Ted Kremenek9aedc152014-01-17 06:24:56 +00001205 return false;
1206 }
1207 return true;
1208}
1209
Chandler Carruthedc2c642011-07-02 00:01:44 +00001210static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001211 SmallVector<unsigned, 8> NonNullArgs;
Richard Smith588bd9b2014-08-27 04:59:42 +00001212 for (unsigned I = 0; I < Attr.getNumArgs(); ++I) {
1213 Expr *Ex = Attr.getArgAsExpr(I);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001214 uint64_t Idx;
Richard Smith588bd9b2014-08-27 04:59:42 +00001215 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, I + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001216 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001217
1218 // Is the function argument a pointer type?
Richard Smith588bd9b2014-08-27 04:59:42 +00001219 if (Idx < getFunctionOrMethodNumParams(D) &&
1220 !attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001221 Ex->getSourceRange(),
1222 getFunctionOrMethodParamRange(D, Idx)))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001223 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001224
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001225 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001226 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001227
1228 // If no arguments were specified to __attribute__((nonnull)) then all pointer
Richard Smith588bd9b2014-08-27 04:59:42 +00001229 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1230 // check if the attribute came from a macro expansion or a template
1231 // instantiation.
1232 if (NonNullArgs.empty() && Attr.getLoc().isFileID() &&
1233 S.ActiveTemplateInstantiations.empty()) {
1234 bool AnyPointers = isFunctionOrMethodVariadic(D);
1235 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1236 I != E && !AnyPointers; ++I) {
1237 QualType T = getFunctionOrMethodParamType(D, I);
Hal Finkelee90a222014-09-26 05:04:30 +00001238 if (T->isDependentType() || S.isValidPointerAttrType(T))
Richard Smith588bd9b2014-08-27 04:59:42 +00001239 AnyPointers = true;
Ted Kremenek5fa50522008-11-18 06:52:58 +00001240 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001241
Richard Smith588bd9b2014-08-27 04:59:42 +00001242 if (!AnyPointers)
1243 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001244 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001245
Richard Smith588bd9b2014-08-27 04:59:42 +00001246 unsigned *Start = NonNullArgs.data();
1247 unsigned Size = NonNullArgs.size();
1248 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001249 D->addAttr(::new (S.Context)
Richard Smith588bd9b2014-08-27 04:59:42 +00001250 NonNullAttr(Attr.getRange(), S.Context, Start, Size,
Michael Han99315932013-01-24 16:46:58 +00001251 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001252}
1253
Jordan Rosec9399072014-02-11 17:27:59 +00001254static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
1255 const AttributeList &Attr) {
1256 if (Attr.getNumArgs() > 0) {
1257 if (D->getFunctionType()) {
1258 handleNonNullAttr(S, D, Attr);
1259 } else {
1260 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1261 << D->getSourceRange();
1262 }
1263 return;
1264 }
1265
1266 // Is the argument a pointer type?
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001267 if (!attrNonNullArgCheck(S, D->getType(), Attr, SourceRange(),
1268 D->getSourceRange()))
Jordan Rosec9399072014-02-11 17:27:59 +00001269 return;
1270
1271 D->addAttr(::new (S.Context)
Craig Topperc3ec1492014-05-26 06:22:03 +00001272 NonNullAttr(Attr.getRange(), S.Context, nullptr, 0,
Jordan Rosec9399072014-02-11 17:27:59 +00001273 Attr.getAttributeSpellingListIndex()));
1274}
1275
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001276static void handleReturnsNonNullAttr(Sema &S, Decl *D,
1277 const AttributeList &Attr) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001278 QualType ResultType = getFunctionOrMethodResultType(D);
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001279 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1280 if (!attrNonNullArgCheck(S, ResultType, Attr, SourceRange(), SR,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001281 /* isReturnValue */ true))
1282 return;
1283
1284 D->addAttr(::new (S.Context)
1285 ReturnsNonNullAttr(Attr.getRange(), S.Context,
1286 Attr.getAttributeSpellingListIndex()));
1287}
1288
Hal Finkelee90a222014-09-26 05:04:30 +00001289static void handleAssumeAlignedAttr(Sema &S, Decl *D,
1290 const AttributeList &Attr) {
1291 Expr *E = Attr.getArgAsExpr(0),
1292 *OE = Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr;
1293 S.AddAssumeAlignedAttr(Attr.getRange(), D, E, OE,
1294 Attr.getAttributeSpellingListIndex());
1295}
1296
1297void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
1298 Expr *OE, unsigned SpellingListIndex) {
1299 QualType ResultType = getFunctionOrMethodResultType(D);
1300 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1301
1302 AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex);
1303 SourceLocation AttrLoc = AttrRange.getBegin();
1304
1305 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1306 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1307 << &TmpAttr << AttrRange << SR;
1308 return;
1309 }
1310
1311 if (!E->isValueDependent()) {
1312 llvm::APSInt I(64);
1313 if (!E->isIntegerConstantExpr(I, Context)) {
1314 if (OE)
1315 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1316 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1317 << E->getSourceRange();
1318 else
1319 Diag(AttrLoc, diag::err_attribute_argument_type)
1320 << &TmpAttr << AANT_ArgumentIntegerConstant
1321 << E->getSourceRange();
1322 return;
1323 }
1324
1325 if (!I.isPowerOf2()) {
1326 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1327 << E->getSourceRange();
1328 return;
1329 }
1330 }
1331
1332 if (OE) {
1333 if (!OE->isValueDependent()) {
1334 llvm::APSInt I(64);
1335 if (!OE->isIntegerConstantExpr(I, Context)) {
1336 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1337 << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1338 << OE->getSourceRange();
1339 return;
1340 }
1341 }
1342 }
1343
1344 D->addAttr(::new (Context)
1345 AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex));
1346}
1347
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001348/// Normalize the attribute, __foo__ becomes foo.
1349/// Returns true if normalization was applied.
1350static bool normalizeName(StringRef &AttrName) {
Aaron Ballman62692362015-10-09 13:53:24 +00001351 if (AttrName.size() > 4 && AttrName.startswith("__") &&
1352 AttrName.endswith("__")) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001353 AttrName = AttrName.drop_front(2).drop_back(2);
1354 return true;
1355 }
1356 return false;
1357}
1358
Chandler Carruthedc2c642011-07-02 00:01:44 +00001359static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001360 // This attribute must be applied to a function declaration. The first
1361 // argument to the attribute must be an identifier, the name of the resource,
1362 // for example: malloc. The following arguments must be argument indexes, the
1363 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001364 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001365 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001366 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001367
Aaron Ballman00e99962013-08-31 01:11:41 +00001368 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001369 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001370 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001371 return;
1372 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001373
Richard Smith852e9ce2013-11-27 01:46:48 +00001374 // Figure out our Kind.
1375 OwnershipAttr::OwnershipKind K =
Craig Topperc3ec1492014-05-26 06:22:03 +00001376 OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0,
Richard Smith852e9ce2013-11-27 01:46:48 +00001377 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001378
Richard Smith852e9ce2013-11-27 01:46:48 +00001379 // Check arguments.
1380 switch (K) {
1381 case OwnershipAttr::Takes:
1382 case OwnershipAttr::Holds:
1383 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001384 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1385 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001386 return;
1387 }
1388 break;
1389 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001390 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001391 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1392 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001393 return;
1394 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001395 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001396 }
1397
Richard Smith852e9ce2013-11-27 01:46:48 +00001398 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001399
Richard Smith852e9ce2013-11-27 01:46:48 +00001400 StringRef ModuleName = Module->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001401 if (normalizeName(ModuleName)) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001402 Module = &S.PP.getIdentifierTable().get(ModuleName);
1403 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001404
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001405 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001406 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1407 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001408 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001409 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001410 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001411
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001412 // Is the function argument a pointer type?
Alp Toker601b22c2014-01-21 23:35:24 +00001413 QualType T = getFunctionOrMethodParamType(D, Idx);
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001414 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001415 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001416 case OwnershipAttr::Takes:
1417 case OwnershipAttr::Holds:
1418 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1419 Err = 0;
1420 break;
1421 case OwnershipAttr::Returns:
1422 if (!T->isIntegerType())
1423 Err = 1;
1424 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001425 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001426 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001427 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001428 << Ex->getSourceRange();
1429 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001430 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001431
1432 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001433 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001434 // Cannot have two ownership attributes of different kinds for the same
1435 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001436 if (I->getOwnKind() != K && I->args_end() !=
1437 std::find(I->args_begin(), I->args_end(), Idx)) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001438 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001439 << AL.getName() << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001440 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001441 } else if (K == OwnershipAttr::Returns &&
1442 I->getOwnKind() == OwnershipAttr::Returns) {
1443 // A returns attribute conflicts with any other returns attribute using
1444 // a different index. Note, diagnostic reporting is 1-based, but stored
1445 // argument indexes are 0-based.
1446 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1447 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
1448 << *(I->args_begin()) + 1;
1449 if (I->args_size())
1450 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
1451 << (unsigned)Idx + 1 << Ex->getSourceRange();
1452 return;
1453 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001454 }
1455 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001456 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001457 }
1458
1459 unsigned* start = OwnershipArgs.data();
1460 unsigned size = OwnershipArgs.size();
1461 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001462
Michael Han99315932013-01-24 16:46:58 +00001463 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001464 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001465 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001466}
1467
Chandler Carruthedc2c642011-07-02 00:01:44 +00001468static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001469 // Check the attribute arguments.
1470 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001471 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1472 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001473 return;
1474 }
1475
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001476 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001477
Rafael Espindolac18086a2010-02-23 22:00:30 +00001478 // gcc rejects
1479 // class c {
1480 // static int a __attribute__((weakref ("v2")));
1481 // static int b() __attribute__((weakref ("f3")));
1482 // };
1483 // and ignores the attributes of
1484 // void f(void) {
1485 // static int a __attribute__((weakref ("v2")));
1486 // }
1487 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001488 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001489 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001490 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1491 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001492 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001493 }
1494
1495 // The GCC manual says
1496 //
1497 // At present, a declaration to which `weakref' is attached can only
1498 // be `static'.
1499 //
1500 // It also says
1501 //
1502 // Without a TARGET,
1503 // given as an argument to `weakref' or to `alias', `weakref' is
1504 // equivalent to `weak'.
1505 //
1506 // gcc 4.4.1 will accept
1507 // int a7 __attribute__((weakref));
1508 // as
1509 // int a7 __attribute__((weak));
1510 // This looks like a bug in gcc. We reject that for now. We should revisit
1511 // it if this behaviour is actually used.
1512
Rafael Espindolac18086a2010-02-23 22:00:30 +00001513 // GCC rejects
1514 // static ((alias ("y"), weakref)).
1515 // Should we? How to check that weakref is before or after alias?
1516
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001517 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1518 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1519 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001520 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001521 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001522 // GCC will accept anything as the argument of weakref. Should we
1523 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001524 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1525 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001526
Michael Han99315932013-01-24 16:46:58 +00001527 D->addAttr(::new (S.Context)
1528 WeakRefAttr(Attr.getRange(), S.Context,
1529 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001530}
1531
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001532static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1533 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001534 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001535 return;
1536
Douglas Gregore8bbc122011-09-02 00:18:52 +00001537 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001538 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1539 return;
1540 }
1541
David Majnemer2dc81462015-01-19 09:00:28 +00001542 // Aliases should be on declarations, not definitions.
1543 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1544 if (FD->isThisDeclarationADefinition()) {
1545 S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << FD;
1546 return;
1547 }
1548 } else {
1549 const auto *VD = cast<VarDecl>(D);
1550 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
1551 S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << VD;
1552 return;
1553 }
1554 }
1555
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001556 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001557
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001558 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001559 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001560}
1561
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001562static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001563 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr.getRange(), Attr.getName()))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001564 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001565
Michael Han99315932013-01-24 16:46:58 +00001566 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1567 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001568}
1569
1570static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001571 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr.getRange(), Attr.getName()))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001572 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001573
Michael Han99315932013-01-24 16:46:58 +00001574 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1575 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001576}
1577
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001578static void handleTLSModelAttr(Sema &S, Decl *D,
1579 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001580 StringRef Model;
1581 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001582 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001583 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001584 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001585
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001586 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001587 if (Model != "global-dynamic" && Model != "local-dynamic"
1588 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001589 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001590 return;
1591 }
1592
Michael Han99315932013-01-24 16:46:58 +00001593 D->addAttr(::new (S.Context)
1594 TLSModelAttr(Attr.getRange(), S.Context, Model,
1595 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001596}
1597
David Majnemer631a90b2015-02-04 07:23:21 +00001598static void handleRestrictAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1599 QualType ResultType = getFunctionOrMethodResultType(D);
1600 if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
1601 D->addAttr(::new (S.Context) RestrictAttr(
1602 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1603 return;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001604 }
1605
David Majnemer631a90b2015-02-04 07:23:21 +00001606 S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only)
1607 << Attr.getName() << getFunctionOrMethodResultSourceRange(D);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001608}
1609
Chandler Carruthedc2c642011-07-02 00:01:44 +00001610static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001611 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001612 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001613 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001614 return;
1615 }
1616
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001617 if (CommonAttr *CA = S.mergeCommonAttr(D, Attr.getRange(), Attr.getName(),
1618 Attr.getAttributeSpellingListIndex()))
1619 D->addAttr(CA);
Eric Christopher8a2ee392010-12-02 02:45:55 +00001620}
1621
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00001622static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1623 if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, Attr.getRange(),
1624 Attr.getName()))
1625 return;
1626
1627 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context,
1628 Attr.getAttributeSpellingListIndex()));
1629}
1630
Chandler Carruthedc2c642011-07-02 00:01:44 +00001631static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001632 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001633
1634 if (S.CheckNoReturnAttr(attr)) return;
1635
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001636 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001637 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001638 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001639 return;
1640 }
1641
Michael Han99315932013-01-24 16:46:58 +00001642 D->addAttr(::new (S.Context)
1643 NoReturnAttr(attr.getRange(), S.Context,
1644 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001645}
1646
1647bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001648 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001649 attr.setInvalid();
1650 return true;
1651 }
1652
1653 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001654}
1655
Chandler Carruthedc2c642011-07-02 00:01:44 +00001656static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1657 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001658
1659 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1660 // because 'analyzer_noreturn' does not impact the type.
David Majnemer06864812015-04-07 06:01:53 +00001661 if (!isFunctionOrMethodOrBlock(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001662 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00001663 if (!VD || (!VD->getType()->isBlockPointerType() &&
1664 !VD->getType()->isFunctionPointerType())) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001665 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001666 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Craig Topper8f7f3ea2015-11-17 05:40:05 +00001667 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001668 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001669 return;
1670 }
1671 }
1672
Michael Han99315932013-01-24 16:46:58 +00001673 D->addAttr(::new (S.Context)
1674 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1675 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001676}
1677
John Thompsoncdb847ba2010-08-09 21:53:52 +00001678// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001679static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001680/*
1681 Returning a Vector Class in Registers
1682
Eric Christopherbc638a82010-12-01 22:13:54 +00001683 According to the PPU ABI specifications, a class with a single member of
1684 vector type is returned in memory when used as the return value of a function.
1685 This results in inefficient code when implementing vector classes. To return
1686 the value in a single vector register, add the vecreturn attribute to the
1687 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001688
1689 Example:
1690
1691 struct Vector
1692 {
1693 __vector float xyzw;
1694 } __attribute__((vecreturn));
1695
1696 Vector Add(Vector lhs, Vector rhs)
1697 {
1698 Vector result;
1699 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1700 return result; // This will be returned in a register
1701 }
1702*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001703 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1704 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001705 return;
1706 }
1707
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001708 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001709 int count = 0;
1710
1711 if (!isa<CXXRecordDecl>(record)) {
1712 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1713 return;
1714 }
1715
1716 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1717 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1718 return;
1719 }
1720
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001721 for (const auto *I : record->fields()) {
1722 if ((count == 1) || !I->getType()->isVectorType()) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001723 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1724 return;
1725 }
1726 count++;
1727 }
1728
Michael Han99315932013-01-24 16:46:58 +00001729 D->addAttr(::new (S.Context)
1730 VecReturnAttr(Attr.getRange(), S.Context,
1731 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001732}
1733
Richard Smithe233fbf2013-01-28 22:42:45 +00001734static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1735 const AttributeList &Attr) {
1736 if (isa<ParmVarDecl>(D)) {
1737 // [[carries_dependency]] can only be applied to a parameter if it is a
1738 // parameter of a function declaration or lambda.
1739 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1740 S.Diag(Attr.getLoc(),
1741 diag::err_carries_dependency_param_not_function_decl);
1742 return;
1743 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001744 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001745
1746 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1747 Attr.getRange(), S.Context,
1748 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001749}
1750
Akira Hatanakac8667622015-11-06 23:56:15 +00001751static void handleNotTailCalledAttr(Sema &S, Decl *D,
1752 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001753 if (checkAttrMutualExclusion<AlwaysInlineAttr>(S, D, Attr.getRange(),
1754 Attr.getName()))
Akira Hatanakac8667622015-11-06 23:56:15 +00001755 return;
1756
1757 D->addAttr(::new (S.Context) NotTailCalledAttr(
1758 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1759}
1760
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00001761static void handleDisableTailCallsAttr(Sema &S, Decl *D,
1762 const AttributeList &Attr) {
1763 if (checkAttrMutualExclusion<NakedAttr>(S, D, Attr.getRange(),
1764 Attr.getName()))
1765 return;
1766
1767 D->addAttr(::new (S.Context) DisableTailCallsAttr(
1768 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1769}
1770
Chandler Carruthedc2c642011-07-02 00:01:44 +00001771static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001772 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001773 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001774 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001775 return;
1776 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001777 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001778 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001779 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001780 return;
1781 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001782
Michael Han99315932013-01-24 16:46:58 +00001783 D->addAttr(::new (S.Context)
1784 UsedAttr(Attr.getRange(), S.Context,
1785 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001786}
1787
Chandler Carruthedc2c642011-07-02 00:01:44 +00001788static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001789 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001790 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001791 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1792 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001793
Michael Han99315932013-01-24 16:46:58 +00001794 D->addAttr(::new (S.Context)
1795 ConstructorAttr(Attr.getRange(), S.Context, priority,
1796 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001797}
1798
Chandler Carruthedc2c642011-07-02 00:01:44 +00001799static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00001800 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001801 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001802 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1803 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001804
Michael Han99315932013-01-24 16:46:58 +00001805 D->addAttr(::new (S.Context)
1806 DestructorAttr(Attr.getRange(), S.Context, priority,
1807 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001808}
1809
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001810template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001811static void handleAttrWithMessage(Sema &S, Decl *D,
1812 const AttributeList &Attr) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001813 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001814 StringRef Str;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001815 if (Attr.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001816 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001817
Michael Han99315932013-01-24 16:46:58 +00001818 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1819 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001820}
1821
Ted Kremenek438f8db2014-02-22 01:06:05 +00001822static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Ted Kremenek28eace62013-11-23 01:01:34 +00001823 const AttributeList &Attr) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00001824 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Ted Kremenek27cfe102014-02-21 22:49:04 +00001825 S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition)
1826 << Attr.getName() << Attr.getRange();
1827 return;
1828 }
1829
Ted Kremenek28eace62013-11-23 01:01:34 +00001830 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001831 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1832 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001833}
1834
Jordy Rose740b0c22012-05-08 03:27:22 +00001835static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1836 IdentifierInfo *Platform,
1837 VersionTuple Introduced,
1838 VersionTuple Deprecated,
1839 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001840 StringRef PlatformName
1841 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1842 if (PlatformName.empty())
1843 PlatformName = Platform->getName();
1844
1845 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1846 // of these steps are needed).
1847 if (!Introduced.empty() && !Deprecated.empty() &&
1848 !(Introduced <= Deprecated)) {
1849 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1850 << 1 << PlatformName << Deprecated.getAsString()
1851 << 0 << Introduced.getAsString();
1852 return true;
1853 }
1854
1855 if (!Introduced.empty() && !Obsoleted.empty() &&
1856 !(Introduced <= Obsoleted)) {
1857 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1858 << 2 << PlatformName << Obsoleted.getAsString()
1859 << 0 << Introduced.getAsString();
1860 return true;
1861 }
1862
1863 if (!Deprecated.empty() && !Obsoleted.empty() &&
1864 !(Deprecated <= Obsoleted)) {
1865 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1866 << 2 << PlatformName << Obsoleted.getAsString()
1867 << 1 << Deprecated.getAsString();
1868 return true;
1869 }
1870
1871 return false;
1872}
1873
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001874/// \brief Check whether the two versions match.
1875///
1876/// If either version tuple is empty, then they are assumed to match. If
1877/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1878static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1879 bool BeforeIsOkay) {
1880 if (X.empty() || Y.empty())
1881 return true;
1882
1883 if (X == Y)
1884 return true;
1885
1886 if (BeforeIsOkay && X < Y)
1887 return true;
1888
1889 return false;
1890}
1891
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001892AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001893 IdentifierInfo *Platform,
1894 VersionTuple Introduced,
1895 VersionTuple Deprecated,
1896 VersionTuple Obsoleted,
1897 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001898 StringRef Message,
Douglas Gregord2a713e2015-09-30 21:27:42 +00001899 AvailabilityMergeKind AMK,
Michael Han99315932013-01-24 16:46:58 +00001900 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001901 VersionTuple MergedIntroduced = Introduced;
1902 VersionTuple MergedDeprecated = Deprecated;
1903 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001904 bool FoundAny = false;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001905 bool OverrideOrImpl = false;
1906 switch (AMK) {
1907 case AMK_None:
1908 case AMK_Redeclaration:
1909 OverrideOrImpl = false;
1910 break;
1911
1912 case AMK_Override:
1913 case AMK_ProtocolImplementation:
1914 OverrideOrImpl = true;
1915 break;
1916 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001917
Rafael Espindolac67f2232012-05-10 02:50:16 +00001918 if (D->hasAttrs()) {
1919 AttrVec &Attrs = D->getAttrs();
1920 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1921 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1922 if (!OldAA) {
1923 ++i;
1924 continue;
1925 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001926
Rafael Espindolac67f2232012-05-10 02:50:16 +00001927 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1928 if (OldPlatform != Platform) {
1929 ++i;
1930 continue;
1931 }
1932
Tim Northover7a73cc72015-10-30 16:30:49 +00001933 // If there is an existing availability attribute for this platform that
1934 // is explicit and the new one is implicit use the explicit one and
1935 // discard the new implicit attribute.
1936 if (OldAA->getRange().isValid() && Range.isInvalid()) {
1937 return nullptr;
1938 }
1939
1940 // If there is an existing attribute for this platform that is implicit
1941 // and the new attribute is explicit then erase the old one and
1942 // continue processing the attributes.
1943 if (Range.isValid() && OldAA->getRange().isInvalid()) {
1944 Attrs.erase(Attrs.begin() + i);
1945 --e;
1946 continue;
1947 }
1948
Rafael Espindolac67f2232012-05-10 02:50:16 +00001949 FoundAny = true;
1950 VersionTuple OldIntroduced = OldAA->getIntroduced();
1951 VersionTuple OldDeprecated = OldAA->getDeprecated();
1952 VersionTuple OldObsoleted = OldAA->getObsoleted();
1953 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001954
Douglas Gregord2a713e2015-09-30 21:27:42 +00001955 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
1956 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
1957 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001958 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregord2a713e2015-09-30 21:27:42 +00001959 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
1960 if (OverrideOrImpl) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001961 int Which = -1;
1962 VersionTuple FirstVersion;
1963 VersionTuple SecondVersion;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001964 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001965 Which = 0;
1966 FirstVersion = OldIntroduced;
1967 SecondVersion = Introduced;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001968 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001969 Which = 1;
1970 FirstVersion = Deprecated;
1971 SecondVersion = OldDeprecated;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001972 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001973 Which = 2;
1974 FirstVersion = Obsoleted;
1975 SecondVersion = OldObsoleted;
1976 }
1977
1978 if (Which == -1) {
1979 Diag(OldAA->getLocation(),
1980 diag::warn_mismatched_availability_override_unavail)
Douglas Gregord2a713e2015-09-30 21:27:42 +00001981 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1982 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001983 } else {
1984 Diag(OldAA->getLocation(),
1985 diag::warn_mismatched_availability_override)
1986 << Which
1987 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
Douglas Gregord2a713e2015-09-30 21:27:42 +00001988 << FirstVersion.getAsString() << SecondVersion.getAsString()
1989 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001990 }
Douglas Gregord2a713e2015-09-30 21:27:42 +00001991 if (AMK == AMK_Override)
1992 Diag(Range.getBegin(), diag::note_overridden_method);
1993 else
1994 Diag(Range.getBegin(), diag::note_protocol_method);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001995 } else {
1996 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1997 Diag(Range.getBegin(), diag::note_previous_attribute);
1998 }
1999
Rafael Espindolac67f2232012-05-10 02:50:16 +00002000 Attrs.erase(Attrs.begin() + i);
2001 --e;
2002 continue;
2003 }
2004
2005 VersionTuple MergedIntroduced2 = MergedIntroduced;
2006 VersionTuple MergedDeprecated2 = MergedDeprecated;
2007 VersionTuple MergedObsoleted2 = MergedObsoleted;
2008
2009 if (MergedIntroduced2.empty())
2010 MergedIntroduced2 = OldIntroduced;
2011 if (MergedDeprecated2.empty())
2012 MergedDeprecated2 = OldDeprecated;
2013 if (MergedObsoleted2.empty())
2014 MergedObsoleted2 = OldObsoleted;
2015
2016 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2017 MergedIntroduced2, MergedDeprecated2,
2018 MergedObsoleted2)) {
2019 Attrs.erase(Attrs.begin() + i);
2020 --e;
2021 continue;
2022 }
2023
2024 MergedIntroduced = MergedIntroduced2;
2025 MergedDeprecated = MergedDeprecated2;
2026 MergedObsoleted = MergedObsoleted2;
2027 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002028 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002029 }
2030
2031 if (FoundAny &&
2032 MergedIntroduced == Introduced &&
2033 MergedDeprecated == Deprecated &&
2034 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00002035 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002036
Douglas Gregord2a713e2015-09-30 21:27:42 +00002037 // Only create a new attribute if !OverrideOrImpl, but we want to do
Ted Kremenekb5445722013-04-06 00:34:27 +00002038 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002039 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002040 MergedDeprecated, MergedObsoleted) &&
Douglas Gregord2a713e2015-09-30 21:27:42 +00002041 !OverrideOrImpl) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002042 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2043 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002044 Obsoleted, IsUnavailable, Message,
2045 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002046 }
Craig Topperc3ec1492014-05-26 06:22:03 +00002047 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002048}
2049
Chandler Carruthedc2c642011-07-02 00:01:44 +00002050static void handleAvailabilityAttr(Sema &S, Decl *D,
2051 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002052 if (!checkAttributeNumArgs(S, Attr, 1))
2053 return;
2054 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00002055 unsigned Index = Attr.getAttributeSpellingListIndex();
2056
Aaron Ballman00e99962013-08-31 01:11:41 +00002057 IdentifierInfo *II = Platform->Ident;
2058 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2059 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2060 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002061
Rafael Espindolac231fab2013-01-08 21:30:32 +00002062 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2063 if (!ND) {
2064 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2065 return;
2066 }
2067
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002068 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2069 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2070 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002071 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002072 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00002073 if (const StringLiteral *SE =
2074 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002075 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002076
Aaron Ballman00e99962013-08-31 01:11:41 +00002077 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002078 Introduced.Version,
2079 Deprecated.Version,
2080 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002081 IsUnavailable, Str,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002082 Sema::AMK_None,
Michael Han99315932013-01-24 16:46:58 +00002083 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002084 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002085 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002086
2087 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2088 // matches before the start of the watchOS platform.
2089 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2090 IdentifierInfo *NewII = nullptr;
2091 if (II->getName() == "ios")
2092 NewII = &S.Context.Idents.get("watchos");
2093 else if (II->getName() == "ios_app_extension")
2094 NewII = &S.Context.Idents.get("watchos_app_extension");
2095
2096 if (NewII) {
2097 auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple {
2098 if (Version.empty())
2099 return Version;
2100 auto Major = Version.getMajor();
2101 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2102 if (NewMajor >= 2) {
2103 if (Version.getMinor().hasValue()) {
2104 if (Version.getSubminor().hasValue())
2105 return VersionTuple(NewMajor, Version.getMinor().getValue(),
2106 Version.getSubminor().getValue());
2107 else
2108 return VersionTuple(NewMajor, Version.getMinor().getValue());
2109 }
2110 }
2111
2112 return VersionTuple(2, 0);
2113 };
2114
2115 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2116 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2117 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2118
2119 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
2120 SourceRange(),
2121 NewII,
2122 NewIntroduced,
2123 NewDeprecated,
2124 NewObsoleted,
2125 IsUnavailable, Str,
2126 Sema::AMK_None,
2127 Index);
2128 if (NewAttr)
2129 D->addAttr(NewAttr);
2130 }
2131 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2132 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2133 // matches before the start of the tvOS platform.
2134 IdentifierInfo *NewII = nullptr;
2135 if (II->getName() == "ios")
2136 NewII = &S.Context.Idents.get("tvos");
2137 else if (II->getName() == "ios_app_extension")
2138 NewII = &S.Context.Idents.get("tvos_app_extension");
2139
2140 if (NewII) {
2141 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
2142 SourceRange(),
2143 NewII,
2144 Introduced.Version,
2145 Deprecated.Version,
2146 Obsoleted.Version,
2147 IsUnavailable, Str,
2148 Sema::AMK_None,
2149 Index);
2150 if (NewAttr)
2151 D->addAttr(NewAttr);
2152 }
2153 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002154}
2155
John McCalld041a9b2013-02-20 01:54:26 +00002156template <class T>
2157static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2158 typename T::VisibilityType value,
2159 unsigned attrSpellingListIndex) {
2160 T *existingAttr = D->getAttr<T>();
2161 if (existingAttr) {
2162 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2163 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00002164 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00002165 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2166 S.Diag(range.getBegin(), diag::note_previous_attribute);
2167 D->dropAttr<T>();
2168 }
2169 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2170}
2171
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002172VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002173 VisibilityAttr::VisibilityType Vis,
2174 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002175 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2176 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002177}
2178
John McCalld041a9b2013-02-20 01:54:26 +00002179TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2180 TypeVisibilityAttr::VisibilityType Vis,
2181 unsigned AttrSpellingListIndex) {
2182 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2183 AttrSpellingListIndex);
2184}
2185
2186static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2187 bool isTypeVisibility) {
2188 // Visibility attributes don't mean anything on a typedef.
2189 if (isa<TypedefNameDecl>(D)) {
2190 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2191 << Attr.getName();
2192 return;
2193 }
2194
2195 // 'type_visibility' can only go on a type or namespace.
2196 if (isTypeVisibility &&
2197 !(isa<TagDecl>(D) ||
2198 isa<ObjCInterfaceDecl>(D) ||
2199 isa<NamespaceDecl>(D))) {
2200 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2201 << Attr.getName() << ExpectedTypeOrNamespace;
2202 return;
2203 }
2204
Benjamin Kramer70370212013-09-09 15:08:57 +00002205 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002206 StringRef TypeStr;
2207 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002208 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002209 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002210
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002211 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002212 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002213 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002214 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002215 return;
2216 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002217
2218 // Complain about attempts to use protected visibility on targets
2219 // (like Darwin) that don't support it.
2220 if (type == VisibilityAttr::Protected &&
2221 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2222 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2223 type = VisibilityAttr::Default;
2224 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002225
Michael Han99315932013-01-24 16:46:58 +00002226 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002227 clang::Attr *newAttr;
2228 if (isTypeVisibility) {
2229 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2230 (TypeVisibilityAttr::VisibilityType) type,
2231 Index);
2232 } else {
2233 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2234 }
2235 if (newAttr)
2236 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002237}
2238
Chandler Carruthedc2c642011-07-02 00:01:44 +00002239static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2240 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002241 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002242 if (!Attr.isArgIdent(0)) {
2243 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2244 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002245 return;
2246 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002247
Aaron Ballman682ee422013-09-11 19:47:58 +00002248 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2249 ObjCMethodFamilyAttr::FamilyKind F;
2250 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2251 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2252 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002253 return;
2254 }
2255
Alp Toker314cc812014-01-25 16:55:45 +00002256 if (F == ObjCMethodFamilyAttr::OMF_init &&
2257 !method->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00002258 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
Alp Toker314cc812014-01-25 16:55:45 +00002259 << method->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002260 // Ignore the attribute.
2261 return;
2262 }
2263
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002264 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00002265 S.Context, F,
2266 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002267}
2268
Chandler Carruthedc2c642011-07-02 00:01:44 +00002269static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002270 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002271 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002272 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002273 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2274 return;
2275 }
2276 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002277 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2278 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002279 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002280 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2281 return;
2282 }
2283 }
2284 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002285 // It is okay to include this attribute on properties, e.g.:
2286 //
2287 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2288 //
2289 // In this case it follows tradition and suppresses an error in the above
2290 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002291 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002292 }
Michael Han99315932013-01-24 16:46:58 +00002293 D->addAttr(::new (S.Context)
2294 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2295 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002296}
2297
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002298static void handleObjCIndependentClass(Sema &S, Decl *D, const AttributeList &Attr) {
2299 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
2300 QualType T = TD->getUnderlyingType();
2301 if (!T->isObjCObjectPointerType()) {
2302 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2303 return;
2304 }
2305 } else {
2306 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2307 return;
2308 }
2309 D->addAttr(::new (S.Context)
2310 ObjCIndependentClassAttr(Attr.getRange(), S.Context,
2311 Attr.getAttributeSpellingListIndex()));
2312}
2313
Chandler Carruthedc2c642011-07-02 00:01:44 +00002314static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002315 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002316 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002317 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002318 return;
2319 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002320
Aaron Ballman00e99962013-08-31 01:11:41 +00002321 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002322 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002323 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2324 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2325 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002326 return;
2327 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002328
Michael Han99315932013-01-24 16:46:58 +00002329 D->addAttr(::new (S.Context)
2330 BlocksAttr(Attr.getRange(), S.Context, type,
2331 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002332}
2333
Chandler Carruthedc2c642011-07-02 00:01:44 +00002334static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002335 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002336 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002337 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002338 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002339 if (E->isTypeDependent() || E->isValueDependent() ||
2340 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002341 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002342 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002343 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002344 return;
2345 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002346
John McCallb46f2872011-09-09 07:56:05 +00002347 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002348 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2349 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002350 return;
2351 }
John McCallb46f2872011-09-09 07:56:05 +00002352
2353 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002354 }
2355
Aaron Ballman18a78382013-11-21 00:28:23 +00002356 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002357 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002358 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002359 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002360 if (E->isTypeDependent() || E->isValueDependent() ||
2361 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002362 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002363 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002364 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002365 return;
2366 }
2367 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002368
John McCallb46f2872011-09-09 07:56:05 +00002369 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002370 // FIXME: This error message could be improved, it would be nice
2371 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002372 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2373 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002374 return;
2375 }
2376 }
2377
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002378 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002379 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002380 if (isa<FunctionNoProtoType>(FT)) {
2381 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2382 return;
2383 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002384
Chris Lattner9363e312009-03-17 23:03:47 +00002385 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002386 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002387 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002388 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002389 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002390 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002391 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002392 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002393 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002394 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2395 if (!BD->isVariadic()) {
2396 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2397 return;
2398 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002399 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002400 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002401 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002402 const FunctionType *FT = Ty->isFunctionPointerType()
2403 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002404 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002405 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002406 int m = Ty->isFunctionPointerType() ? 0 : 1;
2407 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002408 return;
2409 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002410 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002411 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002412 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002413 return;
2414 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002415 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002416 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002417 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002418 return;
2419 }
Michael Han99315932013-01-24 16:46:58 +00002420 D->addAttr(::new (S.Context)
2421 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2422 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002423}
2424
Chandler Carruthedc2c642011-07-02 00:01:44 +00002425static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Alp Toker314cc812014-01-25 16:55:45 +00002426 if (D->getFunctionType() &&
2427 D->getFunctionType()->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002428 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2429 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002430 return;
2431 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002432 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002433 if (MD->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002434 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2435 << Attr.getName() << 1;
2436 return;
2437 }
2438
Michael Han99315932013-01-24 16:46:58 +00002439 D->addAttr(::new (S.Context)
2440 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2441 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002442}
2443
Chandler Carruthedc2c642011-07-02 00:01:44 +00002444static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002445 // weak_import only applies to variable & function declarations.
2446 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002447 if (!D->canBeWeakImported(isDef)) {
2448 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002449 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2450 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002451 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002452 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002453 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002454 // Nothing to warn about here.
2455 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002456 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002457 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002458
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002459 return;
2460 }
2461
Michael Han99315932013-01-24 16:46:58 +00002462 D->addAttr(::new (S.Context)
2463 WeakImportAttr(Attr.getRange(), S.Context,
2464 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002465}
2466
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002467// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002468template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002469static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002470 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002471 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002472 for (unsigned i = 0; i < 3; ++i) {
2473 const Expr *E = Attr.getArgAsExpr(i);
2474 if (!checkUInt32Argument(S, Attr, E, WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002475 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002476 if (WGSize[i] == 0) {
2477 S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero)
2478 << Attr.getName() << E->getSourceRange();
2479 return;
2480 }
2481 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002482
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002483 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2484 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2485 Existing->getYDim() == WGSize[1] &&
2486 Existing->getZDim() == WGSize[2]))
2487 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002488
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002489 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2490 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002491 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002492}
2493
Joey Goulyaba589c2013-03-08 09:42:32 +00002494static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002495 if (!Attr.hasParsedType()) {
2496 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2497 << Attr.getName() << 1;
2498 return;
2499 }
2500
Craig Topperc3ec1492014-05-26 06:22:03 +00002501 TypeSourceInfo *ParmTSI = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00002502 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2503 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002504
2505 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2506 (ParmType->isBooleanType() ||
2507 !ParmType->isIntegralType(S.getASTContext()))) {
2508 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2509 << ParmType;
2510 return;
2511 }
2512
Aaron Ballmana9e05402013-12-02 22:16:55 +00002513 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002514 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002515 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2516 return;
2517 }
2518 }
2519
2520 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002521 ParmTSI,
2522 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002523}
2524
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002525SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002526 StringRef Name,
2527 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002528 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2529 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002530 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002531 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2532 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002533 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002534 }
Michael Han99315932013-01-24 16:46:58 +00002535 return ::new (Context) SectionAttr(Range, Context, Name,
2536 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002537}
2538
Reid Kleckner2a133222015-03-04 23:39:17 +00002539bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2540 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2541 if (!Error.empty()) {
2542 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error;
2543 return false;
2544 }
2545 return true;
2546}
2547
Chandler Carruthedc2c642011-07-02 00:01:44 +00002548static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002549 // Make sure that there is a string literal as the sections's single
2550 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002551 StringRef Str;
2552 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002553 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002554 return;
Mike Stump11289f42009-09-09 15:08:12 +00002555
Reid Kleckner2a133222015-03-04 23:39:17 +00002556 if (!S.checkSectionName(LiteralLoc, Str))
2557 return;
2558
Chris Lattner30ba6742009-08-10 19:03:04 +00002559 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002560 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002561 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002562 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002563 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002564 return;
2565 }
Mike Stump11289f42009-09-09 15:08:12 +00002566
Michael Han99315932013-01-24 16:46:58 +00002567 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002568 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002569 if (NewAttr)
2570 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002571}
2572
Eric Christopher789a7ad2015-06-12 01:36:05 +00002573// Check for things we'd like to warn about, no errors or validation for now.
2574// TODO: Validation should use a backend target library that specifies
2575// the allowable subtarget features and cpus. We could use something like a
2576// TargetCodeGenInfo hook here to do validation.
2577void Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
2578 for (auto Str : {"tune=", "fpmath="})
2579 if (AttrStr.find(Str) != StringRef::npos)
2580 Diag(LiteralLoc, diag::warn_unsupported_target_attribute) << Str;
2581}
2582
Eric Christopher11acf732015-06-12 01:35:52 +00002583static void handleTargetAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eric Christopher11acf732015-06-12 01:35:52 +00002584 StringRef Str;
2585 SourceLocation LiteralLoc;
2586 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
2587 return;
Eric Christopher789a7ad2015-06-12 01:36:05 +00002588 S.checkTargetAttr(LiteralLoc, Str);
Eric Christopher11acf732015-06-12 01:35:52 +00002589 unsigned Index = Attr.getAttributeSpellingListIndex();
2590 TargetAttr *NewAttr =
2591 ::new (S.Context) TargetAttr(Attr.getRange(), S.Context, Str, Index);
2592 D->addAttr(NewAttr);
2593}
2594
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002595
Chandler Carruthedc2c642011-07-02 00:01:44 +00002596static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002597 VarDecl *VD = cast<VarDecl>(D);
2598 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002599 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002600 return;
2601 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002602
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002603 Expr *E = Attr.getArgAsExpr(0);
2604 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00002605 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002606 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002607
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002608 // gcc only allows for simple identifiers. Since we support more than gcc, we
2609 // will warn the user.
2610 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2611 if (DRE->hasQualifier())
2612 S.Diag(Loc, diag::warn_cleanup_ext);
2613 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2614 NI = DRE->getNameInfo();
2615 if (!FD) {
2616 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2617 << NI.getName();
2618 return;
2619 }
2620 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2621 if (ULE->hasExplicitTemplateArgs())
2622 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002623 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2624 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002625 if (!FD) {
2626 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2627 << NI.getName();
2628 if (ULE->getType() == S.Context.OverloadTy)
2629 S.NoteAllOverloadCandidates(ULE);
2630 return;
2631 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002632 } else {
2633 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002634 return;
2635 }
2636
Anders Carlssond277d792009-01-31 01:16:18 +00002637 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002638 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2639 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002640 return;
2641 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002642
Anders Carlsson723f55d2009-02-07 23:16:50 +00002643 // We're currently more strict than GCC about what function types we accept.
2644 // If this ever proves to be a problem it should be easy to fix.
2645 QualType Ty = S.Context.getPointerType(VD->getType());
2646 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002647 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2648 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002649 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2650 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002651 return;
2652 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002653
Michael Han99315932013-01-24 16:46:58 +00002654 D->addAttr(::new (S.Context)
2655 CleanupAttr(Attr.getRange(), S.Context, FD,
2656 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002657}
2658
Mike Stumpd3bb5572009-07-24 19:02:52 +00002659/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002660/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002661static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002662 Expr *IdxExpr = Attr.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00002663 uint64_t Idx;
2664 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002665 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002666
Eric Christopherb64963e2015-08-13 21:34:35 +00002667 // Make sure the format string is really a string.
Alp Toker601b22c2014-01-21 23:35:24 +00002668 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002669
Eric Christopherb64963e2015-08-13 21:34:35 +00002670 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
2671 if (NotNSStringTy &&
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002672 !isCFStringType(Ty, S.Context) &&
2673 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002674 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002675 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00002676 << "a string type" << IdxExpr->getSourceRange()
2677 << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002678 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002679 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002680 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002681 if (!isNSStringType(Ty, S.Context) &&
2682 !isCFStringType(Ty, S.Context) &&
2683 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002684 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002685 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00002686 << (NotNSStringTy ? "string type" : "NSString")
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00002687 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002688 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002689 }
2690
Alp Toker601b22c2014-01-21 23:35:24 +00002691 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002692 // because that has corrected for the implicit this parameter, and is zero-
2693 // based. The attribute expects what the user wrote explicitly.
2694 llvm::APSInt Val;
2695 IdxExpr->EvaluateAsInt(Val, S.Context);
2696
Michael Han99315932013-01-24 16:46:58 +00002697 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002698 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002699 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002700}
2701
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002702enum FormatAttrKind {
2703 CFStringFormat,
2704 NSStringFormat,
2705 StrftimeFormat,
2706 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002707 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002708 InvalidFormat
2709};
2710
2711/// getFormatAttrKind - Map from format attribute names to supported format
2712/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002713static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002714 return llvm::StringSwitch<FormatAttrKind>(Format)
2715 // Check for formats that get handled specially.
2716 .Case("NSString", NSStringFormat)
2717 .Case("CFString", CFStringFormat)
2718 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002719
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002720 // Otherwise, check for supported formats.
2721 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2722 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2723 .Case("kprintf", SupportedFormat) // OpenBSD.
Dimitry Andric6b5ed342015-02-19 22:32:33 +00002724 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
Fariborz Jahanianf8dce0f2015-02-21 00:45:58 +00002725 .Case("os_trace", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002726
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002727 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2728 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002729}
2730
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002731/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002732/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002733static void handleInitPriorityAttr(Sema &S, Decl *D,
2734 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002735 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002736 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2737 return;
2738 }
2739
Aaron Ballman4a611152013-11-27 16:34:09 +00002740 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002741 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2742 Attr.setInvalid();
2743 return;
2744 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002745 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002746 if (S.Context.getAsArrayType(T))
2747 T = S.Context.getBaseElementType(T);
2748 if (!T->getAs<RecordType>()) {
2749 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2750 Attr.setInvalid();
2751 return;
2752 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002753
2754 Expr *E = Attr.getArgAsExpr(0);
2755 uint32_t prioritynum;
2756 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002757 Attr.setInvalid();
2758 return;
2759 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002760
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002761 if (prioritynum < 101 || prioritynum > 65535) {
2762 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00002763 << E->getSourceRange() << Attr.getName() << 101 << 65535;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002764 Attr.setInvalid();
2765 return;
2766 }
Michael Han99315932013-01-24 16:46:58 +00002767 D->addAttr(::new (S.Context)
2768 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2769 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002770}
2771
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002772FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2773 IdentifierInfo *Format, int FormatIdx,
2774 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002775 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002776 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002777 for (auto *F : D->specific_attrs<FormatAttr>()) {
2778 if (F->getType() == Format &&
2779 F->getFormatIdx() == FormatIdx &&
2780 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002781 // If we don't have a valid location for this attribute, adopt the
2782 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002783 if (F->getLocation().isInvalid())
2784 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00002785 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00002786 }
2787 }
2788
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002789 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2790 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002791}
2792
Mike Stumpd3bb5572009-07-24 19:02:52 +00002793/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002794/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002795static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002796 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002797 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002798 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002799 return;
2800 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002801
Chandler Carruth743682b2010-11-16 08:35:43 +00002802 // In C++ the implicit 'this' function parameter also counts, and they are
2803 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002804 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00002805 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002806
Aaron Ballman00e99962013-08-31 01:11:41 +00002807 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2808 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002809
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00002810 if (normalizeName(Format)) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002811 // If we've modified the string name, we need a new identifier for it.
2812 II = &S.Context.Idents.get(Format);
2813 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002814
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002815 // Check for supported formats.
2816 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002817
2818 if (Kind == IgnoredFormat)
2819 return;
2820
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002821 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002822 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002823 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002824 return;
2825 }
2826
2827 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002828 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002829 uint32_t Idx;
2830 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002831 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002832
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002833 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002834 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002835 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002836 return;
2837 }
2838
2839 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002840 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002841
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002842 if (HasImplicitThisParam) {
2843 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002844 S.Diag(Attr.getLoc(),
2845 diag::err_format_attribute_implicit_this_format_string)
2846 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002847 return;
2848 }
2849 ArgIdx--;
2850 }
Mike Stump11289f42009-09-09 15:08:12 +00002851
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002852 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002853 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002854
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002855 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002856 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002857 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002858 << "a CFString" << IdxExpr->getSourceRange()
2859 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00002860 return;
2861 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002862 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002863 // FIXME: do we need to check if the type is NSString*? What are the
2864 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002865 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002866 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002867 << "an NSString" << IdxExpr->getSourceRange()
2868 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002869 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002870 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002871 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002872 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002873 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002874 << "a string type" << IdxExpr->getSourceRange()
2875 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002876 return;
2877 }
2878
2879 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002880 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002881 uint32_t FirstArg;
2882 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002883 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002884
2885 // check if the function is variadic if the 3rd argument non-zero
2886 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002887 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002888 ++NumArgs; // +1 for ...
2889 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002890 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002891 return;
2892 }
2893 }
2894
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002895 // strftime requires FirstArg to be 0 because it doesn't read from any
2896 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002897 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002898 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002899 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2900 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002901 return;
2902 }
2903 // if 0 it disables parameter checking (to use with e.g. va_list)
2904 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002905 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002906 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002907 return;
2908 }
2909
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002910 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002911 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002912 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002913 if (NewAttr)
2914 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002915}
2916
Chandler Carruthedc2c642011-07-02 00:01:44 +00002917static void handleTransparentUnionAttr(Sema &S, Decl *D,
2918 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002919 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00002920 RecordDecl *RD = nullptr;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002921 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002922 if (TD && TD->getUnderlyingType()->isUnionType())
2923 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2924 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002925 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002926
2927 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002928 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002929 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002930 return;
2931 }
2932
John McCallf937c022011-10-07 06:10:15 +00002933 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002934 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002935 diag::warn_transparent_union_attribute_not_definition);
2936 return;
2937 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002938
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002939 RecordDecl::field_iterator Field = RD->field_begin(),
2940 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002941 if (Field == FieldEnd) {
2942 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2943 return;
2944 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002945
David Blaikie40ed2972012-06-06 20:45:41 +00002946 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002947 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002948 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002949 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002950 diag::warn_transparent_union_attribute_floating)
2951 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002952 return;
2953 }
2954
2955 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2956 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2957 for (; Field != FieldEnd; ++Field) {
2958 QualType FieldType = Field->getType();
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002959 // FIXME: this isn't fully correct; we also need to test whether the
2960 // members of the union would all have the same calling convention as the
2961 // first member of the union. Checking just the size and alignment isn't
2962 // sufficient (consider structs passed on the stack instead of in registers
2963 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002964 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002965 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002966 // Warn if we drop the attribute.
2967 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002968 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002969 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002970 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002971 diag::warn_transparent_union_attribute_field_size_align)
2972 << isSize << Field->getDeclName() << FieldBits;
2973 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002974 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002975 diag::note_transparent_union_first_field_size_align)
2976 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002977 return;
2978 }
2979 }
2980
Michael Han99315932013-01-24 16:46:58 +00002981 RD->addAttr(::new (S.Context)
2982 TransparentUnionAttr(Attr.getRange(), S.Context,
2983 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002984}
2985
Chandler Carruthedc2c642011-07-02 00:01:44 +00002986static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002987 // Make sure that there is a string literal as the annotation's single
2988 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002989 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002990 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002991 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002992
2993 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002994 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
2995 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002996 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002997 }
Michael Han99315932013-01-24 16:46:58 +00002998
2999 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003000 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00003001 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003002}
3003
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003004static void handleAlignValueAttr(Sema &S, Decl *D,
3005 const AttributeList &Attr) {
3006 S.AddAlignValueAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
3007 Attr.getAttributeSpellingListIndex());
3008}
3009
3010void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
3011 unsigned SpellingListIndex) {
3012 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
3013 SourceLocation AttrLoc = AttrRange.getBegin();
3014
3015 QualType T;
3016 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3017 T = TD->getUnderlyingType();
3018 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3019 T = VD->getType();
3020 else
3021 llvm_unreachable("Unknown decl type for align_value");
3022
3023 if (!T->isDependentType() && !T->isAnyPointerType() &&
3024 !T->isReferenceType() && !T->isMemberPointerType()) {
3025 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
3026 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
3027 return;
3028 }
3029
3030 if (!E->isValueDependent()) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003031 llvm::APSInt Alignment;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003032 ExprResult ICE
3033 = VerifyIntegerConstantExpression(E, &Alignment,
3034 diag::err_align_value_attribute_argument_not_int,
3035 /*AllowFold*/ false);
3036 if (ICE.isInvalid())
3037 return;
3038
3039 if (!Alignment.isPowerOf2()) {
3040 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3041 << E->getSourceRange();
3042 return;
3043 }
3044
3045 D->addAttr(::new (Context)
3046 AlignValueAttr(AttrRange, Context, ICE.get(),
3047 SpellingListIndex));
3048 return;
3049 }
3050
3051 // Save dependent expressions in the AST to be instantiated.
3052 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
3053 return;
3054}
3055
Chandler Carruthedc2c642011-07-02 00:01:44 +00003056static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003057 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003058 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003059 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3060 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003061 return;
3062 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003063
Richard Smith848e1f12013-02-01 08:12:08 +00003064 if (Attr.getNumArgs() == 0) {
3065 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
Craig Topperc3ec1492014-05-26 06:22:03 +00003066 true, nullptr, Attr.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00003067 return;
3068 }
3069
Aaron Ballman00e99962013-08-31 01:11:41 +00003070 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00003071 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3072 S.Diag(Attr.getEllipsisLoc(),
3073 diag::err_pack_expansion_without_parameter_packs);
3074 return;
3075 }
3076
3077 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3078 return;
3079
David Majnemer26a1e0e2015-04-07 02:37:09 +00003080 if (E->isValueDependent()) {
3081 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3082 if (!TND->getUnderlyingType()->isDependentType()) {
3083 S.Diag(Attr.getLoc(), diag::err_alignment_dependent_typedef_name)
3084 << E->getSourceRange();
3085 return;
3086 }
3087 }
3088 }
3089
Richard Smith44c247f2013-02-22 08:32:16 +00003090 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3091 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003092}
3093
3094void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003095 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003096 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3097 SourceLocation AttrLoc = AttrRange.getBegin();
3098
Richard Smith1dba27c2013-01-29 09:02:09 +00003099 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003100 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003101 // C++11 [dcl.align]p1:
3102 // An alignment-specifier may be applied to a variable or to a class
3103 // data member, but it shall not be applied to a bit-field, a function
3104 // parameter, the formal parameter of a catch clause, or a variable
3105 // declared with the register storage class specifier. An
3106 // alignment-specifier may also be applied to the declaration of a class
3107 // or enumeration type.
3108 // C11 6.7.5/2:
3109 // An alignment attribute shall not be specified in a declaration of
3110 // a typedef, or a bit-field, or a function, or a parameter, or an
3111 // object declared with the register storage-class specifier.
3112 int DiagKind = -1;
3113 if (isa<ParmVarDecl>(D)) {
3114 DiagKind = 0;
3115 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3116 if (VD->getStorageClass() == SC_Register)
3117 DiagKind = 1;
3118 if (VD->isExceptionVariable())
3119 DiagKind = 2;
3120 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3121 if (FD->isBitField())
3122 DiagKind = 3;
3123 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00003124 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00003125 << (TmpAttr.isC11() ? ExpectedVariableOrField
3126 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003127 return;
3128 }
3129 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003130 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00003131 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003132 return;
3133 }
3134 }
3135
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003136 if (E->isTypeDependent() || E->isValueDependent()) {
3137 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003138 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3139 AA->setPackExpansion(IsPackExpansion);
3140 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003141 return;
3142 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003143
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003144 // FIXME: Cache the number on the Attr object?
David Majnemer0be6bd02015-07-26 09:02:21 +00003145 llvm::APSInt Alignment;
Douglas Gregore2b37442012-05-04 22:38:52 +00003146 ExprResult ICE
3147 = VerifyIntegerConstantExpression(E, &Alignment,
3148 diag::err_aligned_attribute_argument_not_int,
3149 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003150 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003151 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003152
David Majnemer0be6bd02015-07-26 09:02:21 +00003153 uint64_t AlignVal = Alignment.getZExtValue();
3154
Richard Smith848e1f12013-02-01 08:12:08 +00003155 // C++11 [dcl.align]p2:
3156 // -- if the constant expression evaluates to zero, the alignment
3157 // specifier shall have no effect
3158 // C11 6.7.5p6:
3159 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003160 if (!(TmpAttr.isAlignas() && !Alignment)) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003161 if (!llvm::isPowerOf2_64(AlignVal)) {
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003162 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3163 << E->getSourceRange();
3164 return;
3165 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003166 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003167
David Majnemerabecae72014-02-12 20:36:10 +00003168 // Alignment calculations can wrap around if it's greater than 2**28.
David Majnemer29c69db2015-07-26 01:48:59 +00003169 unsigned MaxValidAlignment =
3170 Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192
3171 : 268435456;
David Majnemer0be6bd02015-07-26 09:02:21 +00003172 if (AlignVal > MaxValidAlignment) {
David Majnemerabecae72014-02-12 20:36:10 +00003173 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
3174 << E->getSourceRange();
3175 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003176 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003177
David Majnemer0be6bd02015-07-26 09:02:21 +00003178 if (Context.getTargetInfo().isTLSSupported()) {
3179 unsigned MaxTLSAlign =
3180 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3181 .getQuantity();
3182 auto *VD = dyn_cast<VarDecl>(D);
3183 if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD &&
3184 VD->getTLSKind() != VarDecl::TLS_None) {
3185 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3186 << (unsigned)AlignVal << VD << MaxTLSAlign;
3187 return;
3188 }
3189 }
3190
Richard Smith44c247f2013-02-22 08:32:16 +00003191 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003192 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00003193 AA->setPackExpansion(IsPackExpansion);
3194 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003195}
3196
Michael Hanaf02bbe2013-02-01 01:19:17 +00003197void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003198 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003199 // FIXME: Cache the number on the Attr object if non-dependent?
3200 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003201 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3202 SpellingListIndex);
3203 AA->setPackExpansion(IsPackExpansion);
3204 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003205}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003206
Richard Smith848e1f12013-02-01 08:12:08 +00003207void Sema::CheckAlignasUnderalignment(Decl *D) {
3208 assert(D->hasAttrs() && "no attributes on decl");
3209
David Majnemer475b25e2015-01-21 10:54:38 +00003210 QualType UnderlyingTy, DiagTy;
3211 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
3212 UnderlyingTy = DiagTy = VD->getType();
3213 } else {
3214 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
3215 if (EnumDecl *ED = dyn_cast<EnumDecl>(D))
3216 UnderlyingTy = ED->getIntegerType();
3217 }
3218 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003219 return;
3220
3221 // C++11 [dcl.align]p5, C11 6.7.5/4:
3222 // The combined effect of all alignment attributes in a declaration shall
3223 // not specify an alignment that is less strict than the alignment that
3224 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003225 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003226 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003227 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003228 if (I->isAlignmentDependent())
3229 return;
3230 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003231 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003232 Align = std::max(Align, I->getAlignment(Context));
3233 }
3234
3235 if (AlignasAttr && Align) {
3236 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003237 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003238 if (NaturalAlign > RequestedAlign)
3239 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003240 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003241 }
3242}
3243
David Majnemer2c4e00a2014-01-29 22:07:36 +00003244bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003245 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003246 MSInheritanceAttr::Spelling SemanticSpelling) {
3247 assert(RD->hasDefinition() && "RD has no definition!");
3248
David Majnemer98c9ee22014-02-07 00:43:07 +00003249 // We may not have seen base specifiers or any virtual methods yet. We will
3250 // have to wait until the record is defined to catch any mismatches.
3251 if (!RD->getDefinition()->isCompleteDefinition())
3252 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003253
David Majnemer98c9ee22014-02-07 00:43:07 +00003254 // The unspecified model never matches what a definition could need.
3255 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
3256 return false;
3257
David Majnemer4bb09802014-02-10 19:50:15 +00003258 if (BestCase) {
3259 if (RD->calculateInheritanceModel() == SemanticSpelling)
3260 return false;
3261 } else {
3262 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3263 return false;
3264 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003265
3266 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3267 << 0 /*definition*/;
3268 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3269 << RD->getNameAsString();
3270 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003271}
3272
Alexey Bataevf278eb12015-11-19 10:13:11 +00003273/// parseModeAttrArg - Parses attribute mode string and returns parsed type
3274/// attribute.
3275static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
3276 bool &IntegerMode, bool &ComplexMode) {
Daniel Dunbarafff4342009-10-18 02:09:24 +00003277 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003278 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003279 switch (Str[0]) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003280 case 'Q':
3281 DestWidth = 8;
3282 break;
3283 case 'H':
3284 DestWidth = 16;
3285 break;
3286 case 'S':
3287 DestWidth = 32;
3288 break;
3289 case 'D':
3290 DestWidth = 64;
3291 break;
3292 case 'X':
3293 DestWidth = 96;
3294 break;
3295 case 'T':
3296 DestWidth = 128;
3297 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003298 }
3299 if (Str[1] == 'F') {
3300 IntegerMode = false;
3301 } else if (Str[1] == 'C') {
3302 IntegerMode = false;
3303 ComplexMode = true;
3304 } else if (Str[1] != 'I') {
3305 DestWidth = 0;
3306 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003307 break;
3308 case 4:
3309 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3310 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003311 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003312 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003313 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003314 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003315 break;
3316 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003317 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003318 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003319 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003320 case 11:
3321 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003322 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003323 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003324 }
Alexey Bataevf278eb12015-11-19 10:13:11 +00003325}
3326
3327/// handleModeAttr - This attribute modifies the width of a decl with primitive
3328/// type.
3329///
3330/// Despite what would be logical, the mode attribute is a decl attribute, not a
3331/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3332/// HImode, not an intermediate pointer.
3333static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3334 // This attribute isn't documented, but glibc uses it. It changes
3335 // the width of an int or unsigned int to the specified size.
3336 if (!Attr.isArgIdent(0)) {
3337 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3338 << AANT_ArgumentIdentifier;
3339 return;
3340 }
3341
3342 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3343 StringRef Str = Name->getName();
3344
3345 normalizeName(Str);
3346
3347 unsigned DestWidth = 0;
3348 bool IntegerMode = true;
3349 bool ComplexMode = false;
3350 llvm::APInt VectorSize(64, 0);
3351 if (Str.size() >= 4 && Str[0] == 'V') {
3352 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
3353 size_t StrSize = Str.size();
3354 size_t VectorStringLength = 0;
3355 while ((VectorStringLength + 1) < StrSize &&
3356 isdigit(Str[VectorStringLength + 1]))
3357 ++VectorStringLength;
3358 if (VectorStringLength &&
3359 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
3360 VectorSize.isPowerOf2()) {
3361 parseModeAttrArg(S, Str.substr(VectorStringLength + 1), DestWidth,
3362 IntegerMode, ComplexMode);
3363 S.Diag(Attr.getLoc(), diag::warn_vector_mode_deprecated);
3364 } else {
3365 VectorSize = 0;
3366 }
3367 }
3368
3369 if (!VectorSize)
3370 parseModeAttrArg(S, Str, DestWidth, IntegerMode, ComplexMode);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003371
3372 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003373 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003374 OldTy = TD->getUnderlyingType();
3375 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3376 OldTy = VD->getType();
3377 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003378 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00003379 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003380 return;
3381 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003382
Alexey Bataev326057d2015-06-19 07:46:21 +00003383 // Base type can also be a vector type (see PR17453).
3384 // Distinguish between base type and base element type.
3385 QualType OldElemTy = OldTy;
3386 if (const VectorType *VT = OldTy->getAs<VectorType>())
3387 OldElemTy = VT->getElementType();
3388
3389 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003390 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3391 else if (IntegerMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003392 if (!OldElemTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003393 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3394 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003395 if (!OldElemTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003396 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3397 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00003398 if (!OldElemTy->isFloatingType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003399 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3400 }
3401
Mike Stump87c57ac2009-05-16 07:39:55 +00003402 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3403 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003404 // FIXME: Make sure floating-point mappings are accurate
3405 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003406 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00003407 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003408 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003409 }
3410
Alexey Bataev326057d2015-06-19 07:46:21 +00003411 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003412
3413 if (IntegerMode)
Alexey Bataev326057d2015-06-19 07:46:21 +00003414 NewElemTy = S.Context.getIntTypeForBitwidth(
3415 DestWidth, OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003416 else
Alexey Bataev326057d2015-06-19 07:46:21 +00003417 NewElemTy = S.Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003418
Alexey Bataev326057d2015-06-19 07:46:21 +00003419 if (NewElemTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00003420 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003421 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003422 }
3423
Eli Friedman4735374e2009-03-03 06:41:03 +00003424 if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003425 NewElemTy = S.Context.getComplexType(NewElemTy);
3426 }
3427
3428 QualType NewTy = NewElemTy;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003429 if (VectorSize.getBoolValue()) {
3430 NewTy = S.Context.getVectorType(NewTy, VectorSize.getZExtValue(),
3431 VectorType::GenericVector);
3432 } else if (const VectorType *OldVT = OldTy->getAs<VectorType>()) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003433 // Complex machine mode does not support base vector types.
3434 if (ComplexMode) {
3435 S.Diag(Attr.getLoc(), diag::err_complex_mode_vector_type);
3436 return;
3437 }
3438 unsigned NumElements = S.Context.getTypeSize(OldElemTy) *
3439 OldVT->getNumElements() /
3440 S.Context.getTypeSize(NewElemTy);
3441 NewTy =
3442 S.Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
3443 }
3444
3445 if (NewTy.isNull()) {
3446 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3447 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003448 }
3449
3450 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003451 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3452 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3453 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003454 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003455
3456 D->addAttr(::new (S.Context)
3457 ModeAttr(Attr.getRange(), S.Context, Name,
3458 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003459}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003460
Chandler Carruthedc2c642011-07-02 00:01:44 +00003461static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003462 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3463 if (!VD->hasGlobalStorage())
3464 S.Diag(Attr.getLoc(),
3465 diag::warn_attribute_requires_functions_or_static_globals)
3466 << Attr.getName();
3467 } else if (!isFunctionOrMethod(D)) {
3468 S.Diag(Attr.getLoc(),
3469 diag::warn_attribute_requires_functions_or_static_globals)
3470 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003471 return;
3472 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003473
Michael Han99315932013-01-24 16:46:58 +00003474 D->addAttr(::new (S.Context)
3475 NoDebugAttr(Attr.getRange(), S.Context,
3476 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003477}
3478
Paul Robinson30e41fb2014-12-15 18:57:28 +00003479AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range,
Paul Robinson080b1f32015-01-13 18:34:56 +00003480 IdentifierInfo *Ident,
Paul Robinson30e41fb2014-12-15 18:57:28 +00003481 unsigned AttrSpellingListIndex) {
3482 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003483 Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00003484 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3485 return nullptr;
3486 }
3487
3488 if (D->hasAttr<AlwaysInlineAttr>())
3489 return nullptr;
3490
3491 return ::new (Context) AlwaysInlineAttr(Range, Context,
3492 AttrSpellingListIndex);
3493}
3494
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003495CommonAttr *Sema::mergeCommonAttr(Decl *D, SourceRange Range,
3496 IdentifierInfo *Ident,
3497 unsigned AttrSpellingListIndex) {
3498 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, Range, Ident))
3499 return nullptr;
3500
3501 return ::new (Context) CommonAttr(Range, Context, AttrSpellingListIndex);
3502}
3503
3504InternalLinkageAttr *
3505Sema::mergeInternalLinkageAttr(Decl *D, SourceRange Range,
3506 IdentifierInfo *Ident,
3507 unsigned AttrSpellingListIndex) {
3508 if (auto VD = dyn_cast<VarDecl>(D)) {
3509 // Attribute applies to Var but not any subclass of it (like ParmVar,
3510 // ImplicitParm or VarTemplateSpecialization).
3511 if (VD->getKind() != Decl::Var) {
3512 Diag(Range.getBegin(), diag::warn_attribute_wrong_decl_type)
3513 << Ident << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
3514 : ExpectedVariableOrFunction);
3515 return nullptr;
3516 }
3517 // Attribute does not apply to non-static local variables.
3518 if (VD->hasLocalStorage()) {
3519 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
3520 return nullptr;
3521 }
3522 }
3523
3524 if (checkAttrMutualExclusion<CommonAttr>(*this, D, Range, Ident))
3525 return nullptr;
3526
3527 return ::new (Context)
3528 InternalLinkageAttr(Range, Context, AttrSpellingListIndex);
3529}
3530
Paul Robinson30e41fb2014-12-15 18:57:28 +00003531MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range,
3532 unsigned AttrSpellingListIndex) {
3533 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
3534 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'";
3535 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3536 return nullptr;
3537 }
3538
3539 if (D->hasAttr<MinSizeAttr>())
3540 return nullptr;
3541
3542 return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex);
3543}
3544
3545OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range,
3546 unsigned AttrSpellingListIndex) {
3547 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
3548 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
3549 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3550 D->dropAttr<AlwaysInlineAttr>();
3551 }
3552 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
3553 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
3554 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3555 D->dropAttr<MinSizeAttr>();
3556 }
3557
3558 if (D->hasAttr<OptimizeNoneAttr>())
3559 return nullptr;
3560
3561 return ::new (Context) OptimizeNoneAttr(Range, Context,
3562 AttrSpellingListIndex);
3563}
3564
Paul Robinsonf0674352014-03-31 22:29:15 +00003565static void handleAlwaysInlineAttr(Sema &S, Decl *D,
3566 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003567 if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, Attr.getRange(),
3568 Attr.getName()))
Akira Hatanakac8667622015-11-06 23:56:15 +00003569 return;
3570
Paul Robinson080b1f32015-01-13 18:34:56 +00003571 if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr(
3572 D, Attr.getRange(), Attr.getName(),
3573 Attr.getAttributeSpellingListIndex()))
3574 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00003575}
3576
Paul Robinson080b1f32015-01-13 18:34:56 +00003577static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3578 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(
3579 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3580 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00003581}
3582
Paul Robinsonf0674352014-03-31 22:29:15 +00003583static void handleOptimizeNoneAttr(Sema &S, Decl *D,
3584 const AttributeList &Attr) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003585 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(
3586 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3587 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00003588}
3589
Chandler Carruthedc2c642011-07-02 00:01:44 +00003590static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003591 FunctionDecl *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00003592 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00003593 SourceRange RTRange = FD->getReturnTypeSourceRange();
3594 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00003595 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00003596 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
3597 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00003598 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003599 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003600
Aaron Ballman3aff6332013-12-02 19:30:36 +00003601 D->addAttr(::new (S.Context)
3602 CUDAGlobalAttr(Attr.getRange(), S.Context,
Eli Bendersky83860042014-09-02 22:00:06 +00003603 Attr.getAttributeSpellingListIndex()));
Artem Belevichc3fa25d2015-09-22 17:22:51 +00003604
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003605}
3606
Chandler Carruthedc2c642011-07-02 00:01:44 +00003607static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003608 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003609 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003610 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003611 return;
3612 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003613
Michael Han99315932013-01-24 16:46:58 +00003614 D->addAttr(::new (S.Context)
3615 GNUInlineAttr(Attr.getRange(), S.Context,
3616 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003617}
3618
Chandler Carruthedc2c642011-07-02 00:01:44 +00003619static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003620 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003621
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003622 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003623 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3624 CallingConv CC;
Richard Smitheec7cb12015-05-28 23:38:53 +00003625 if (S.CheckCallingConvAttr(Attr, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00003626 return;
3627
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003628 if (!isa<ObjCMethodDecl>(D)) {
3629 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3630 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003631 return;
3632 }
3633
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003634 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003635 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003636 D->addAttr(::new (S.Context)
3637 FastCallAttr(Attr.getRange(), S.Context,
3638 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003639 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003640 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003641 D->addAttr(::new (S.Context)
3642 StdCallAttr(Attr.getRange(), S.Context,
3643 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003644 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003645 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003646 D->addAttr(::new (S.Context)
3647 ThisCallAttr(Attr.getRange(), S.Context,
3648 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003649 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003650 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003651 D->addAttr(::new (S.Context)
3652 CDeclAttr(Attr.getRange(), S.Context,
3653 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003654 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003655 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003656 D->addAttr(::new (S.Context)
3657 PascalAttr(Attr.getRange(), S.Context,
3658 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003659 return;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003660 case AttributeList::AT_VectorCall:
3661 D->addAttr(::new (S.Context)
3662 VectorCallAttr(Attr.getRange(), S.Context,
3663 Attr.getAttributeSpellingListIndex()));
3664 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003665 case AttributeList::AT_MSABI:
3666 D->addAttr(::new (S.Context)
3667 MSABIAttr(Attr.getRange(), S.Context,
3668 Attr.getAttributeSpellingListIndex()));
3669 return;
3670 case AttributeList::AT_SysVABI:
3671 D->addAttr(::new (S.Context)
3672 SysVABIAttr(Attr.getRange(), S.Context,
3673 Attr.getAttributeSpellingListIndex()));
3674 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003675 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003676 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003677 switch (CC) {
3678 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003679 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003680 break;
3681 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003682 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003683 break;
3684 default:
3685 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003686 }
3687
Michael Han99315932013-01-24 16:46:58 +00003688 D->addAttr(::new (S.Context)
3689 PcsAttr(Attr.getRange(), S.Context, PCS,
3690 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003691 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003692 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00003693 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003694 D->addAttr(::new (S.Context)
3695 IntelOclBiccAttr(Attr.getRange(), S.Context,
3696 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003697 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003698
Abramo Bagnara50099372010-04-30 13:10:51 +00003699 default:
3700 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003701 }
3702}
3703
Aaron Ballman02df2e02012-12-09 17:45:41 +00003704bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3705 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003706 if (attr.isInvalid())
3707 return true;
3708
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003709 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003710 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003711 attr.setInvalid();
3712 return true;
3713 }
3714
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003715 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003716 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003717 case AttributeList::AT_CDecl: CC = CC_C; break;
3718 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3719 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3720 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3721 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003722 case AttributeList::AT_VectorCall: CC = CC_X86VectorCall; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003723 case AttributeList::AT_MSABI:
3724 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3725 CC_X86_64Win64;
3726 break;
3727 case AttributeList::AT_SysVABI:
3728 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3729 CC_C;
3730 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003731 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003732 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003733 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003734 attr.setInvalid();
3735 return true;
3736 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003737 if (StrRef == "aapcs") {
3738 CC = CC_AAPCS;
3739 break;
3740 } else if (StrRef == "aapcs-vfp") {
3741 CC = CC_AAPCS_VFP;
3742 break;
3743 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003744
3745 attr.setInvalid();
3746 Diag(attr.getLoc(), diag::err_invalid_pcs);
3747 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003748 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00003749 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003750 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003751 }
3752
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003753 const TargetInfo &TI = Context.getTargetInfo();
3754 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
Reid Kleckner9fde2e02015-02-26 19:43:46 +00003755 if (A != TargetInfo::CCCR_OK) {
3756 if (A == TargetInfo::CCCR_Warning)
3757 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003758
Reid Kleckner9fde2e02015-02-26 19:43:46 +00003759 // This convention is not valid for the target. Use the default function or
3760 // method calling convention.
Aaron Ballman02df2e02012-12-09 17:45:41 +00003761 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3762 if (FD)
3763 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3764 TargetInfo::CCMT_NonMember;
3765 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003766 }
3767
John McCall3882ace2011-01-05 12:14:39 +00003768 return false;
3769}
3770
John McCall3882ace2011-01-05 12:14:39 +00003771/// Checks a regparm attribute, returning true if it is ill-formed and
3772/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003773bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3774 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003775 return true;
3776
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003777 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003778 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003779 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003780 }
Eli Friedman7044b762009-03-27 21:06:47 +00003781
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003782 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003783 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003784 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003785 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003786 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003787 }
3788
Douglas Gregore8bbc122011-09-02 00:18:52 +00003789 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003790 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003791 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003792 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003793 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003794 }
3795
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003796 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003797 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003798 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003799 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003800 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003801 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003802 }
3803
John McCall3882ace2011-01-05 12:14:39 +00003804 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003805}
3806
Artem Belevich7093e402015-04-21 22:55:54 +00003807// Checks whether an argument of launch_bounds attribute is acceptable
3808// May output an error.
3809static bool checkLaunchBoundsArgument(Sema &S, Expr *E,
3810 const CUDALaunchBoundsAttr &Attr,
3811 const unsigned Idx) {
3812
3813 if (S.DiagnoseUnexpandedParameterPack(E))
3814 return false;
3815
3816 // Accept template arguments for now as they depend on something else.
3817 // We'll get to check them when they eventually get instantiated.
3818 if (E->isValueDependent())
3819 return true;
3820
3821 llvm::APSInt I(64);
3822 if (!E->isIntegerConstantExpr(I, S.Context)) {
3823 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
3824 << &Attr << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
3825 return false;
3826 }
3827 // Make sure we can fit it in 32 bits.
3828 if (!I.isIntN(32)) {
3829 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
3830 << 32 << /* Unsigned */ 1;
3831 return false;
3832 }
3833 if (I < 0)
3834 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
3835 << &Attr << Idx << E->getSourceRange();
3836
3837 return true;
3838}
3839
3840void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads,
3841 Expr *MinBlocks, unsigned SpellingListIndex) {
3842 CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks,
3843 SpellingListIndex);
3844
3845 if (!checkLaunchBoundsArgument(*this, MaxThreads, TmpAttr, 0))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003846 return;
3847
Artem Belevich7093e402015-04-21 22:55:54 +00003848 if (MinBlocks && !checkLaunchBoundsArgument(*this, MinBlocks, TmpAttr, 1))
3849 return;
3850
3851 D->addAttr(::new (Context) CUDALaunchBoundsAttr(
3852 AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex));
3853}
3854
3855static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3856 const AttributeList &Attr) {
3857 if (!checkAttributeAtLeastNumArgs(S, Attr, 1) ||
3858 !checkAttributeAtMostNumArgs(S, Attr, 2))
3859 return;
3860
3861 S.AddLaunchBoundsAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
3862 Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr,
3863 Attr.getAttributeSpellingListIndex());
Peter Collingbourne827301e2010-12-12 23:03:07 +00003864}
3865
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003866static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3867 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003868 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003869 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003870 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003871 return;
3872 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003873
3874 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003875 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003876
Aaron Ballman00e99962013-08-31 01:11:41 +00003877 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003878
3879 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3880 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3881 << Attr.getName() << ExpectedFunctionOrMethod;
3882 return;
3883 }
3884
3885 uint64_t ArgumentIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003886 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3887 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003888 return;
3889
3890 uint64_t TypeTagIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003891 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3892 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003893 return;
3894
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003895 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003896 if (IsPointer) {
3897 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00003898 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003899 if (!BufferTy->isPointerType()) {
3900 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00003901 << Attr.getName() << 0;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003902 }
3903 }
3904
Michael Han99315932013-01-24 16:46:58 +00003905 D->addAttr(::new (S.Context)
3906 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3907 ArgumentIdx, TypeTagIdx, IsPointer,
3908 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003909}
3910
3911static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3912 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003913 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003914 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003915 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003916 return;
3917 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003918
3919 if (!checkAttributeNumArgs(S, Attr, 1))
3920 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003921
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003922 if (!isa<VarDecl>(D)) {
3923 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3924 << Attr.getName() << ExpectedVariable;
3925 return;
3926 }
3927
Aaron Ballman00e99962013-08-31 01:11:41 +00003928 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00003929 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00003930 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3931 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003932
Michael Han99315932013-01-24 16:46:58 +00003933 D->addAttr(::new (S.Context)
3934 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003935 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003936 Attr.getLayoutCompatible(),
3937 Attr.getMustBeNull(),
3938 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003939}
3940
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003941//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003942// Checker-specific attribute handlers.
3943//===----------------------------------------------------------------------===//
3944
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003945static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) {
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003946 return type->isDependentType() ||
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003947 type->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003948}
3949
John McCalled433932011-01-25 03:31:58 +00003950static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003951 return type->isDependentType() ||
3952 type->isObjCObjectPointerType() ||
3953 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003954}
3955static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003956 return type->isDependentType() ||
3957 type->isPointerType() ||
3958 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003959}
3960
Chandler Carruthedc2c642011-07-02 00:01:44 +00003961static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003962 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003963 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003964
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003965 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003966 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3967 cf = false;
3968 } else {
3969 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3970 cf = true;
3971 }
3972
3973 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003974 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003975 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003976 return;
3977 }
3978
3979 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003980 param->addAttr(::new (S.Context)
3981 CFConsumedAttr(Attr.getRange(), S.Context,
3982 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003983 else
Michael Han99315932013-01-24 16:46:58 +00003984 param->addAttr(::new (S.Context)
3985 NSConsumedAttr(Attr.getRange(), S.Context,
3986 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003987}
3988
Chandler Carruthedc2c642011-07-02 00:01:44 +00003989static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3990 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003991
John McCalled433932011-01-25 03:31:58 +00003992 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003993
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003994 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003995 returnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003996 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003997 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003998 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003999 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4000 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004001 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004002 returnType = FD->getReturnType();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004003 else if (auto *Param = dyn_cast<ParmVarDecl>(D)) {
4004 returnType = Param->getType()->getPointeeType();
4005 if (returnType.isNull()) {
4006 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
4007 << Attr.getName() << /*pointer-to-CF*/2
4008 << Attr.getRange();
4009 return;
4010 }
4011 } else {
4012 AttributeDeclKind ExpectedDeclKind;
4013 switch (Attr.getKind()) {
4014 default: llvm_unreachable("invalid ownership attribute");
4015 case AttributeList::AT_NSReturnsRetained:
4016 case AttributeList::AT_NSReturnsAutoreleased:
4017 case AttributeList::AT_NSReturnsNotRetained:
4018 ExpectedDeclKind = ExpectedFunctionOrMethod;
4019 break;
4020
4021 case AttributeList::AT_CFReturnsRetained:
4022 case AttributeList::AT_CFReturnsNotRetained:
4023 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
4024 break;
4025 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004026 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004027 << Attr.getRange() << Attr.getName() << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004028 return;
4029 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004030
John McCalled433932011-01-25 03:31:58 +00004031 bool typeOK;
4032 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004033 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004034 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004035 case AttributeList::AT_NSReturnsRetained:
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00004036 typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType);
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004037 cf = false;
4038 break;
4039
4040 case AttributeList::AT_NSReturnsAutoreleased:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004041 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004042 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4043 cf = false;
4044 break;
4045
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004046 case AttributeList::AT_CFReturnsRetained:
4047 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004048 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4049 cf = true;
4050 break;
4051 }
4052
4053 if (!typeOK) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004054 if (isa<ParmVarDecl>(D)) {
4055 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
4056 << Attr.getName() << /*pointer-to-CF*/2
4057 << Attr.getRange();
4058 } else {
4059 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
4060 enum : unsigned {
4061 Function,
4062 Method,
4063 Property
4064 } SubjectKind = Function;
4065 if (isa<ObjCMethodDecl>(D))
4066 SubjectKind = Method;
4067 else if (isa<ObjCPropertyDecl>(D))
4068 SubjectKind = Property;
4069 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4070 << Attr.getName() << SubjectKind << cf
4071 << Attr.getRange();
4072 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004073 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004074 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004075
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004076 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004077 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004078 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004079 case AttributeList::AT_NSReturnsAutoreleased:
Nico Weber462fd1e2015-01-07 23:50:05 +00004080 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(
4081 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004082 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004083 case AttributeList::AT_CFReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004084 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(
4085 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004086 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004087 case AttributeList::AT_NSReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004088 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(
4089 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004090 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004091 case AttributeList::AT_CFReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004092 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(
4093 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004094 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004095 case AttributeList::AT_NSReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004096 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(
4097 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004098 return;
4099 };
4100}
4101
John McCallcf166702011-07-22 08:53:00 +00004102static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4103 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00004104 const int EP_ObjCMethod = 1;
4105 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004106
John McCallcf166702011-07-22 08:53:00 +00004107 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004108 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004109 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004110 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004111 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004112 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00004113
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004114 if (!resultType->isReferenceType() &&
4115 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004116 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00004117 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004118 << attr.getName()
4119 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004120 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00004121
4122 // Drop the attribute.
4123 return;
4124 }
4125
Nico Weber462fd1e2015-01-07 23:50:05 +00004126 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(
4127 attr.getRange(), S.Context, attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004128}
4129
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004130static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4131 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004132 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004133
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004134 DeclContext *DC = method->getDeclContext();
4135 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4136 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4137 << attr.getName() << 0;
4138 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4139 return;
4140 }
4141 if (method->getMethodFamily() == OMF_dealloc) {
4142 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4143 << attr.getName() << 1;
4144 return;
4145 }
4146
Michael Han99315932013-01-24 16:46:58 +00004147 method->addAttr(::new (S.Context)
4148 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4149 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004150}
4151
Aaron Ballmanfb763042013-12-02 18:05:46 +00004152static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
4153 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004154 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr.getRange(),
4155 Attr.getName()))
John McCall32f5fe12011-09-30 05:12:12 +00004156 return;
John McCall32f5fe12011-09-30 05:12:12 +00004157
Aaron Ballmanfb763042013-12-02 18:05:46 +00004158 D->addAttr(::new (S.Context)
4159 CFAuditedTransferAttr(Attr.getRange(), S.Context,
4160 Attr.getAttributeSpellingListIndex()));
4161}
4162
4163static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
4164 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004165 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr.getRange(),
4166 Attr.getName()))
Aaron Ballmanfb763042013-12-02 18:05:46 +00004167 return;
4168
4169 D->addAttr(::new (S.Context)
4170 CFUnknownTransferAttr(Attr.getRange(), S.Context,
4171 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004172}
4173
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004174static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
4175 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004176 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004177
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004178 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004179 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004180 return;
4181 }
John McCall28592582015-02-01 22:34:06 +00004182
4183 // Typedefs only allow objc_bridge(id) and have some additional checking.
4184 if (auto TD = dyn_cast<TypedefNameDecl>(D)) {
4185 if (!Parm->Ident->isStr("id")) {
4186 S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_id)
4187 << Attr.getName();
4188 return;
4189 }
4190
4191 // Only allow 'cv void *'.
4192 QualType T = TD->getUnderlyingType();
4193 if (!T->isVoidPointerType()) {
4194 S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
4195 return;
4196 }
4197 }
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004198
4199 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004200 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004201 Attr.getAttributeSpellingListIndex()));
4202}
4203
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004204static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
4205 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004206 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
4207
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004208 if (!Parm) {
4209 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
4210 return;
4211 }
4212
4213 D->addAttr(::new (S.Context)
4214 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
4215 Attr.getAttributeSpellingListIndex()));
4216}
4217
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004218static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
4219 const AttributeList &Attr) {
4220 IdentifierInfo *RelatedClass =
Craig Topperc3ec1492014-05-26 06:22:03 +00004221 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004222 if (!RelatedClass) {
4223 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
4224 return;
4225 }
4226 IdentifierInfo *ClassMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00004227 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004228 IdentifierInfo *InstanceMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00004229 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004230 D->addAttr(::new (S.Context)
4231 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
4232 ClassMethod, InstanceMethod,
4233 Attr.getAttributeSpellingListIndex()));
4234}
4235
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004236static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
4237 const AttributeList &Attr) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004238 ObjCInterfaceDecl *IFace;
Nico Weber462fd1e2015-01-07 23:50:05 +00004239 if (ObjCCategoryDecl *CatDecl =
4240 dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004241 IFace = CatDecl->getClassInterface();
4242 else
4243 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00004244
4245 if (!IFace)
4246 return;
4247
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00004248 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00004249 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004250 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
4251 Attr.getAttributeSpellingListIndex()));
4252}
4253
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004254static void handleObjCRuntimeName(Sema &S, Decl *D,
4255 const AttributeList &Attr) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004256 StringRef MetaDataName;
4257 if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName))
4258 return;
4259 D->addAttr(::new (S.Context)
4260 ObjCRuntimeNameAttr(Attr.getRange(), S.Context,
4261 MetaDataName,
4262 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004263}
4264
Alex Denisovfde64952015-06-26 05:28:36 +00004265// when a user wants to use objc_boxable with a union or struct
4266// but she doesn't have access to the declaration (legacy/third-party code)
4267// then she can 'enable' this feature via trick with a typedef
4268// e.g.:
4269// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
4270static void handleObjCBoxable(Sema &S, Decl *D, const AttributeList &Attr) {
4271 bool notify = false;
4272
4273 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4274 if (RD && RD->getDefinition()) {
4275 RD = RD->getDefinition();
4276 notify = true;
4277 }
4278
4279 if (RD) {
4280 ObjCBoxableAttr *BoxableAttr = ::new (S.Context)
4281 ObjCBoxableAttr(Attr.getRange(), S.Context,
4282 Attr.getAttributeSpellingListIndex());
4283 RD->addAttr(BoxableAttr);
4284 if (notify) {
4285 // we need to notify ASTReader/ASTWriter about
4286 // modification of existing declaration
4287 if (ASTMutationListener *L = S.getASTMutationListener())
4288 L->AddedAttributeToRecord(BoxableAttr, RD);
4289 }
4290 }
4291}
4292
Chandler Carruthedc2c642011-07-02 00:01:44 +00004293static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4294 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004295 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004296
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004297 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004298 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004299}
4300
Chandler Carruthedc2c642011-07-02 00:01:44 +00004301static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4302 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004303 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004304 QualType type = vd->getType();
4305
4306 if (!type->isDependentType() &&
4307 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004308 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004309 << type;
4310 return;
4311 }
4312
4313 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4314
4315 // If we have no lifetime yet, check the lifetime we're presumably
4316 // going to infer.
4317 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4318 lifetime = type->getObjCARCImplicitLifetime();
4319
4320 switch (lifetime) {
4321 case Qualifiers::OCL_None:
4322 assert(type->isDependentType() &&
4323 "didn't infer lifetime for non-dependent type?");
4324 break;
4325
4326 case Qualifiers::OCL_Weak: // meaningful
4327 case Qualifiers::OCL_Strong: // meaningful
4328 break;
4329
4330 case Qualifiers::OCL_ExplicitNone:
4331 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004332 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004333 << (lifetime == Qualifiers::OCL_Autoreleasing);
4334 break;
4335 }
4336
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004337 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004338 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4339 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004340}
4341
Francois Picheta83957a2010-12-19 06:50:37 +00004342//===----------------------------------------------------------------------===//
4343// Microsoft specific attribute handlers.
4344//===----------------------------------------------------------------------===//
4345
Chandler Carruthedc2c642011-07-02 00:01:44 +00004346static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00004347 if (!S.LangOpts.CPlusPlus) {
4348 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4349 << Attr.getName() << AttributeLangSupport::C;
4350 return;
4351 }
4352
Aaron Ballman60e705e2013-11-24 20:58:02 +00004353 if (!isa<CXXRecordDecl>(D)) {
4354 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4355 << Attr.getName() << ExpectedClass;
4356 return;
4357 }
4358
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004359 StringRef StrRef;
4360 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00004361 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00004362 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004363
David Majnemer89085342013-08-09 08:56:20 +00004364 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4365 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00004366 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4367 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00004368
Reid Kleckner140c4a72013-05-17 14:04:52 +00004369 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00004370 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004371 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004372 return;
4373 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004374
David Majnemer89085342013-08-09 08:56:20 +00004375 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004376 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00004377 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004378 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00004379 return;
4380 }
David Majnemer89085342013-08-09 08:56:20 +00004381 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004382 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004383 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004384 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004385 }
Francois Picheta83957a2010-12-19 06:50:37 +00004386
David Majnemer89085342013-08-09 08:56:20 +00004387 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4388 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004389}
4390
David Majnemer2c4e00a2014-01-29 22:07:36 +00004391static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4392 if (!S.LangOpts.CPlusPlus) {
4393 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4394 << Attr.getName() << AttributeLangSupport::C;
4395 return;
4396 }
4397 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
David Majnemer4bb09802014-02-10 19:50:15 +00004398 D, Attr.getRange(), /*BestCase=*/true,
4399 Attr.getAttributeSpellingListIndex(),
David Majnemer2c4e00a2014-01-29 22:07:36 +00004400 (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling());
4401 if (IA)
4402 D->addAttr(IA);
4403}
4404
Reid Kleckner7d6d2702014-05-01 03:16:47 +00004405static void handleDeclspecThreadAttr(Sema &S, Decl *D,
4406 const AttributeList &Attr) {
4407 VarDecl *VD = cast<VarDecl>(D);
4408 if (!S.Context.getTargetInfo().isTLSSupported()) {
4409 S.Diag(Attr.getLoc(), diag::err_thread_unsupported);
4410 return;
4411 }
4412 if (VD->getTSCSpec() != TSCS_unspecified) {
4413 S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable);
4414 return;
4415 }
4416 if (VD->hasLocalStorage()) {
4417 S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
4418 return;
4419 }
4420 VD->addAttr(::new (S.Context) ThreadAttr(
4421 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
4422}
4423
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004424static void handleARMInterruptAttr(Sema &S, Decl *D,
4425 const AttributeList &Attr) {
4426 // Check the attribute arguments.
4427 if (Attr.getNumArgs() > 1) {
4428 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
4429 << Attr.getName() << 1;
4430 return;
4431 }
4432
4433 StringRef Str;
4434 SourceLocation ArgLoc;
4435
4436 if (Attr.getNumArgs() == 0)
4437 Str = "";
4438 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
4439 return;
4440
4441 ARMInterruptAttr::InterruptType Kind;
4442 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
4443 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
4444 << Attr.getName() << Str << ArgLoc;
4445 return;
4446 }
4447
4448 unsigned Index = Attr.getAttributeSpellingListIndex();
4449 D->addAttr(::new (S.Context)
4450 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
4451}
4452
4453static void handleMSP430InterruptAttr(Sema &S, Decl *D,
4454 const AttributeList &Attr) {
4455 if (!checkAttributeNumArgs(S, Attr, 1))
4456 return;
4457
4458 if (!Attr.isArgExpr(0)) {
4459 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
4460 << AANT_ArgumentIntegerConstant;
4461 return;
4462 }
4463
4464 // FIXME: Check for decl - it should be void ()(void).
4465
4466 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4467 llvm::APSInt NumParams(32);
4468 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
4469 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4470 << Attr.getName() << AANT_ArgumentIntegerConstant
4471 << NumParamsExpr->getSourceRange();
4472 return;
4473 }
4474
4475 unsigned Num = NumParams.getLimitedValue(255);
4476 if ((Num & 1) || Num > 30) {
4477 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
4478 << Attr.getName() << (int)NumParams.getSExtValue()
4479 << NumParamsExpr->getSourceRange();
4480 return;
4481 }
4482
Aaron Ballman36a53502014-01-16 13:03:14 +00004483 D->addAttr(::new (S.Context)
4484 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
4485 Attr.getAttributeSpellingListIndex()));
4486 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004487}
4488
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00004489static void handleMipsInterruptAttr(Sema &S, Decl *D,
4490 const AttributeList &Attr) {
4491 // Only one optional argument permitted.
4492 if (Attr.getNumArgs() > 1) {
4493 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
4494 << Attr.getName() << 1;
4495 return;
4496 }
4497
4498 StringRef Str;
4499 SourceLocation ArgLoc;
4500
4501 if (Attr.getNumArgs() == 0)
4502 Str = "";
4503 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
4504 return;
4505
4506 // Semantic checks for a function with the 'interrupt' attribute for MIPS:
4507 // a) Must be a function.
4508 // b) Must have no parameters.
4509 // c) Must have the 'void' return type.
4510 // d) Cannot have the 'mips16' attribute, as that instruction set
4511 // lacks the 'eret' instruction.
4512 // e) The attribute itself must either have no argument or one of the
4513 // valid interrupt types, see [MipsInterruptDocs].
4514
4515 if (!isFunctionOrMethod(D)) {
4516 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
4517 << "'interrupt'" << ExpectedFunctionOrMethod;
4518 return;
4519 }
4520
4521 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
4522 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
4523 << 0;
4524 return;
4525 }
4526
4527 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
4528 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
4529 << 1;
4530 return;
4531 }
4532
4533 if (checkAttrMutualExclusion<Mips16Attr>(S, D, Attr.getRange(),
4534 Attr.getName()))
4535 return;
4536
4537 MipsInterruptAttr::InterruptType Kind;
4538 if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
4539 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
4540 << Attr.getName() << "'" + std::string(Str) + "'";
4541 return;
4542 }
4543
4544 D->addAttr(::new (S.Context) MipsInterruptAttr(
4545 Attr.getLoc(), S.Context, Kind, Attr.getAttributeSpellingListIndex()));
4546}
4547
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004548static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4549 // Dispatch the interrupt attribute based on the current target.
4550 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
4551 handleMSP430InterruptAttr(S, D, Attr);
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00004552 else if (S.Context.getTargetInfo().getTriple().getArch() ==
4553 llvm::Triple::mipsel ||
4554 S.Context.getTargetInfo().getTriple().getArch() ==
4555 llvm::Triple::mips)
4556 handleMipsInterruptAttr(S, D, Attr);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004557 else
4558 handleARMInterruptAttr(S, D, Attr);
4559}
4560
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00004561static void handleMips16Attribute(Sema &S, Decl *D, const AttributeList &Attr) {
4562 if (checkAttrMutualExclusion<MipsInterruptAttr>(S, D, Attr.getRange(),
4563 Attr.getName()))
4564 return;
4565
4566 handleSimpleAttribute<Mips16Attr>(S, D, Attr);
4567}
4568
Matt Arsenault43fae6c2014-12-04 20:38:18 +00004569static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D,
4570 const AttributeList &Attr) {
4571 uint32_t NumRegs;
4572 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4573 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4574 return;
4575
4576 D->addAttr(::new (S.Context)
4577 AMDGPUNumVGPRAttr(Attr.getLoc(), S.Context,
4578 NumRegs,
4579 Attr.getAttributeSpellingListIndex()));
4580}
4581
4582static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D,
4583 const AttributeList &Attr) {
4584 uint32_t NumRegs;
4585 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4586 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4587 return;
4588
4589 D->addAttr(::new (S.Context)
4590 AMDGPUNumSGPRAttr(Attr.getLoc(), S.Context,
4591 NumRegs,
4592 Attr.getAttributeSpellingListIndex()));
4593}
4594
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004595static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
4596 const AttributeList& Attr) {
4597 // If we try to apply it to a function pointer, don't warn, but don't
4598 // do anything, either. It doesn't matter anyway, because there's nothing
4599 // special about calling a force_align_arg_pointer function.
4600 ValueDecl *VD = dyn_cast<ValueDecl>(D);
4601 if (VD && VD->getType()->isFunctionPointerType())
4602 return;
4603 // Also don't warn on function pointer typedefs.
4604 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
4605 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
4606 TD->getUnderlyingType()->isFunctionType()))
4607 return;
4608 // Attribute can only be applied to function types.
4609 if (!isa<FunctionDecl>(D)) {
4610 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4611 << Attr.getName() << /* function */0;
4612 return;
4613 }
4614
Aaron Ballman36a53502014-01-16 13:03:14 +00004615 D->addAttr(::new (S.Context)
4616 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
4617 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004618}
4619
4620DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
4621 unsigned AttrSpellingListIndex) {
4622 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004623 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00004624 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004625 }
4626
4627 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004628 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004629
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004630 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004631}
4632
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004633DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
4634 unsigned AttrSpellingListIndex) {
4635 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004636 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004637 D->dropAttr<DLLImportAttr>();
4638 }
4639
4640 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004641 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004642
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004643 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004644}
4645
Hans Wennborge82f19c2014-06-24 23:57:05 +00004646static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00004647 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
4648 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4649 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored)
4650 << A.getName();
4651 return;
4652 }
4653
Hans Wennborg606bd6d2014-11-03 14:24:45 +00004654 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4655 if (FD->isInlined() && A.getKind() == AttributeList::AT_DLLImport &&
4656 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4657 // MinGW doesn't allow dllimport on inline functions.
4658 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
4659 << A.getName();
4660 return;
4661 }
4662 }
4663
Hans Wennborg5869ec42015-09-15 21:05:30 +00004664 if (auto *MD = dyn_cast<CXXMethodDecl>(D)) {
4665 if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
4666 MD->getParent()->isLambda()) {
4667 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A.getName();
4668 return;
4669 }
4670 }
4671
Hans Wennborge82f19c2014-06-24 23:57:05 +00004672 unsigned Index = A.getAttributeSpellingListIndex();
4673 Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport
4674 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
4675 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004676 if (NewAttr)
4677 D->addAttr(NewAttr);
4678}
4679
David Majnemer2c4e00a2014-01-29 22:07:36 +00004680MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00004681Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00004682 unsigned AttrSpellingListIndex,
4683 MSInheritanceAttr::Spelling SemanticSpelling) {
4684 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
4685 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00004686 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004687 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
4688 << 1 /*previous declaration*/;
4689 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
4690 D->dropAttr<MSInheritanceAttr>();
4691 }
4692
4693 CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
4694 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00004695 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
4696 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004697 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004698 }
4699 } else {
4700 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
4701 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4702 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004703 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004704 }
4705 if (RD->getDescribedClassTemplate()) {
4706 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4707 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004708 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004709 }
4710 }
4711
4712 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00004713 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00004714}
4715
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004716static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4717 // The capability attributes take a single string parameter for the name of
4718 // the capability they represent. The lockable attribute does not take any
4719 // parameters. However, semantically, both attributes represent the same
4720 // concept, and so they use the same semantic attribute. Eventually, the
4721 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00004722 //
Alp Toker958027b2014-07-14 19:42:55 +00004723 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00004724 // literal will be considered a "mutex."
4725 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004726 SourceLocation LiteralLoc;
4727 if (Attr.getKind() == AttributeList::AT_Capability &&
4728 !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc))
4729 return;
4730
Aaron Ballman6c810072014-03-05 21:47:13 +00004731 // Currently, there are only two names allowed for a capability: role and
4732 // mutex (case insensitive). Diagnose other capability names.
4733 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
4734 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
4735
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004736 D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N,
4737 Attr.getAttributeSpellingListIndex()));
4738}
4739
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004740static void handleAssertCapabilityAttr(Sema &S, Decl *D,
4741 const AttributeList &Attr) {
4742 D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context,
4743 Attr.getArgAsExpr(0),
4744 Attr.getAttributeSpellingListIndex()));
4745}
4746
4747static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
4748 const AttributeList &Attr) {
4749 SmallVector<Expr*, 1> Args;
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004750 if (!checkLockFunAttrCommon(S, D, Attr, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004751 return;
4752
4753 D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(),
4754 S.Context,
4755 Args.data(), Args.size(),
4756 Attr.getAttributeSpellingListIndex()));
4757}
4758
4759static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
4760 const AttributeList &Attr) {
4761 SmallVector<Expr*, 2> Args;
4762 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
4763 return;
4764
4765 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(),
4766 S.Context,
4767 Attr.getArgAsExpr(0),
4768 Args.data(),
4769 Args.size(),
4770 Attr.getAttributeSpellingListIndex()));
4771}
4772
4773static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
4774 const AttributeList &Attr) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004775 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004776 SmallVector<Expr *, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004777 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004778
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004779 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
4780 Attr.getRange(), S.Context, Args.data(), Args.size(),
4781 Attr.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004782}
4783
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004784static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
4785 const AttributeList &Attr) {
4786 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
4787 return;
4788
4789 // check that all arguments are lockable objects
4790 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004791 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004792 if (Args.empty())
4793 return;
4794
4795 RequiresCapabilityAttr *RCA = ::new (S.Context)
4796 RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(),
4797 Args.size(), Attr.getAttributeSpellingListIndex());
4798
4799 D->addAttr(RCA);
4800}
4801
Aaron Ballman43f40102014-11-14 22:34:56 +00004802static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4803 if (auto *NSD = dyn_cast<NamespaceDecl>(D)) {
4804 if (NSD->isAnonymousNamespace()) {
4805 S.Diag(Attr.getLoc(), diag::warn_deprecated_anonymous_namespace);
4806 // Do not want to attach the attribute to the namespace because that will
4807 // cause confusing diagnostic reports for uses of declarations within the
4808 // namespace.
4809 return;
4810 }
4811 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00004812
4813 if (!S.getLangOpts().CPlusPlus14)
4814 if (Attr.isCXX11Attribute() &&
4815 !(Attr.hasScope() && Attr.getScopeName()->isStr("gnu")))
Saleem Abdulrasoolb47d6062015-02-18 04:33:26 +00004816 S.Diag(Attr.getLoc(), diag::ext_deprecated_attr_is_a_cxx14_extension);
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00004817
Aaron Ballman43f40102014-11-14 22:34:56 +00004818 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
4819}
4820
Peter Collingbourne915df992015-05-15 18:33:32 +00004821static void handleNoSanitizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4822 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
4823 return;
4824
4825 std::vector<std::string> Sanitizers;
4826
4827 for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) {
4828 StringRef SanitizerName;
4829 SourceLocation LiteralLoc;
4830
4831 if (!S.checkStringLiteralArgumentAttr(Attr, I, SanitizerName, &LiteralLoc))
4832 return;
4833
4834 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0)
4835 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
4836
4837 Sanitizers.push_back(SanitizerName);
4838 }
4839
4840 D->addAttr(::new (S.Context) NoSanitizeAttr(
4841 Attr.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(),
4842 Attr.getAttributeSpellingListIndex()));
4843}
4844
4845static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
4846 const AttributeList &Attr) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00004847 StringRef AttrName = Attr.getName()->getName();
4848 normalizeName(AttrName);
Peter Collingbourne915df992015-05-15 18:33:32 +00004849 std::string SanitizerName =
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00004850 llvm::StringSwitch<std::string>(AttrName)
Peter Collingbourne915df992015-05-15 18:33:32 +00004851 .Case("no_address_safety_analysis", "address")
4852 .Case("no_sanitize_address", "address")
4853 .Case("no_sanitize_thread", "thread")
Peter Collingbourne94410942015-05-15 20:11:18 +00004854 .Case("no_sanitize_memory", "memory");
Peter Collingbourne915df992015-05-15 18:33:32 +00004855 D->addAttr(::new (S.Context)
4856 NoSanitizeAttr(Attr.getRange(), S.Context, &SanitizerName, 1,
4857 Attr.getAttributeSpellingListIndex()));
4858}
4859
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004860static void handleInternalLinkageAttr(Sema &S, Decl *D,
4861 const AttributeList &Attr) {
4862 if (InternalLinkageAttr *Internal =
4863 S.mergeInternalLinkageAttr(D, Attr.getRange(), Attr.getName(),
4864 Attr.getAttributeSpellingListIndex()))
4865 D->addAttr(Internal);
4866}
4867
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004868/// Handles semantic checking for features that are common to all attributes,
4869/// such as checking whether a parameter was properly specified, or the correct
4870/// number of arguments were passed, etc.
4871static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4872 const AttributeList &Attr) {
4873 // Several attributes carry different semantics than the parsing requires, so
4874 // those are opted out of the common handling.
4875 //
4876 // We also bail on unknown and ignored attributes because those are handled
4877 // as part of the target-specific handling logic.
4878 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004879 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004880 return false;
4881
Aaron Ballman3aff6332013-12-02 19:30:36 +00004882 // Check whether the attribute requires specific language extensions to be
4883 // enabled.
4884 if (!Attr.diagnoseLangOpts(S))
4885 return true;
4886
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00004887 if (Attr.getMinArgs() == Attr.getMaxArgs()) {
4888 // If there are no optional arguments, then checking for the argument count
4889 // is trivial.
4890 if (!checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4891 return true;
4892 } else {
4893 // There are optional arguments, so checking is slightly more involved.
4894 if (Attr.getMinArgs() &&
4895 !checkAttributeAtLeastNumArgs(S, Attr, Attr.getMinArgs()))
4896 return true;
4897 else if (!Attr.hasVariadicArg() && Attr.getMaxArgs() &&
4898 !checkAttributeAtMostNumArgs(S, Attr, Attr.getMaxArgs()))
4899 return true;
4900 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004901
4902 // Check whether the attribute appertains to the given subject.
4903 if (!Attr.diagnoseAppertainsTo(S, D))
4904 return true;
4905
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004906 return false;
4907}
4908
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004909//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004910// Top Level Sema Entry Points
4911//===----------------------------------------------------------------------===//
4912
Richard Smithf8a75c32013-08-29 00:47:48 +00004913/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4914/// the attribute applies to decls. If the attribute is a type attribute, just
4915/// silently ignore it if a GNU attribute.
4916static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4917 const AttributeList &Attr,
4918 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004919 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00004920 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004921
Richard Smithf8a75c32013-08-29 00:47:48 +00004922 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4923 // instead.
4924 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4925 return;
4926
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004927 // Unknown attributes are automatically warned on. Target-specific attributes
4928 // which do not apply to the current target architecture are treated as
4929 // though they were unknown attributes.
4930 if (Attr.getKind() == AttributeList::UnknownAttribute ||
Bob Wilson7c730832015-07-20 22:57:31 +00004931 !Attr.existsInTarget(S.Context.getTargetInfo())) {
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004932 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute()
4933 ? diag::warn_unhandled_ms_attribute_ignored
4934 : diag::warn_unknown_attribute_ignored)
4935 << Attr.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004936 return;
4937 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004938
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004939 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4940 return;
4941
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004942 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004943 default:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004944 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004945 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004946 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004947 case AttributeList::AT_Interrupt:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004948 handleInterruptAttr(S, D, Attr);
4949 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004950 case AttributeList::AT_X86ForceAlignArgPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004951 handleX86ForceAlignArgPointerAttr(S, D, Attr);
4952 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004953 case AttributeList::AT_DLLExport:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004954 case AttributeList::AT_DLLImport:
Hans Wennborge82f19c2014-06-24 23:57:05 +00004955 handleDLLAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004956 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004957 case AttributeList::AT_Mips16:
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00004958 handleMips16Attribute(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004959 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004960 case AttributeList::AT_NoMips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004961 handleSimpleAttribute<NoMips16Attr>(S, D, Attr);
4962 break;
Matt Arsenault43fae6c2014-12-04 20:38:18 +00004963 case AttributeList::AT_AMDGPUNumVGPR:
4964 handleAMDGPUNumVGPRAttr(S, D, Attr);
4965 break;
4966 case AttributeList::AT_AMDGPUNumSGPR:
4967 handleAMDGPUNumSGPRAttr(S, D, Attr);
4968 break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00004969 case AttributeList::AT_IBAction:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004970 handleSimpleAttribute<IBActionAttr>(S, D, Attr);
4971 break;
4972 case AttributeList::AT_IBOutlet:
4973 handleIBOutlet(S, D, Attr);
4974 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004975 case AttributeList::AT_IBOutletCollection:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004976 handleIBOutletCollection(S, D, Attr);
4977 break;
4978 case AttributeList::AT_Alias:
4979 handleAliasAttr(S, D, Attr);
4980 break;
4981 case AttributeList::AT_Aligned:
4982 handleAlignedAttr(S, D, Attr);
4983 break;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00004984 case AttributeList::AT_AlignValue:
4985 handleAlignValueAttr(S, D, Attr);
4986 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004987 case AttributeList::AT_AlwaysInline:
Paul Robinsonf0674352014-03-31 22:29:15 +00004988 handleAlwaysInlineAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004989 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004990 case AttributeList::AT_AnalyzerNoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004991 handleAnalyzerNoReturnAttr(S, D, Attr);
4992 break;
4993 case AttributeList::AT_TLSModel:
4994 handleTLSModelAttr(S, D, Attr);
4995 break;
4996 case AttributeList::AT_Annotate:
4997 handleAnnotateAttr(S, D, Attr);
4998 break;
4999 case AttributeList::AT_Availability:
5000 handleAvailabilityAttr(S, D, Attr);
5001 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005002 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00005003 handleDependencyAttr(S, scope, D, Attr);
5004 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005005 case AttributeList::AT_Common:
5006 handleCommonAttr(S, D, Attr);
5007 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005008 case AttributeList::AT_CUDAConstant:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005009 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr);
5010 break;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00005011 case AttributeList::AT_PassObjectSize:
5012 handlePassObjectSizeAttr(S, D, Attr);
5013 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005014 case AttributeList::AT_Constructor:
5015 handleConstructorAttr(S, D, Attr);
5016 break;
Richard Smith10876ef2013-01-17 01:30:42 +00005017 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005018 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr);
5019 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005020 case AttributeList::AT_Deprecated:
Aaron Ballman43f40102014-11-14 22:34:56 +00005021 handleDeprecatedAttr(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00005022 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005023 case AttributeList::AT_Destructor:
5024 handleDestructorAttr(S, D, Attr);
5025 break;
5026 case AttributeList::AT_EnableIf:
5027 handleEnableIfAttr(S, D, Attr);
5028 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005029 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005030 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005031 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00005032 case AttributeList::AT_MinSize:
Paul Robinsonaae2fba2014-12-10 23:34:36 +00005033 handleMinSizeAttr(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00005034 break;
Paul Robinsonf0674352014-03-31 22:29:15 +00005035 case AttributeList::AT_OptimizeNone:
5036 handleOptimizeNoneAttr(S, D, Attr);
5037 break;
Alexis Hunt724f14e2014-11-28 00:53:20 +00005038 case AttributeList::AT_FlagEnum:
5039 handleSimpleAttribute<FlagEnumAttr>(S, D, Attr);
5040 break;
Peter Collingbourne41af7c22014-05-20 17:12:51 +00005041 case AttributeList::AT_Flatten:
5042 handleSimpleAttribute<FlattenAttr>(S, D, Attr);
5043 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005044 case AttributeList::AT_Format:
5045 handleFormatAttr(S, D, Attr);
5046 break;
5047 case AttributeList::AT_FormatArg:
5048 handleFormatArgAttr(S, D, Attr);
5049 break;
5050 case AttributeList::AT_CUDAGlobal:
5051 handleGlobalAttr(S, D, Attr);
5052 break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00005053 case AttributeList::AT_CUDADevice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005054 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr);
5055 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005056 case AttributeList::AT_CUDAHost:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005057 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr);
5058 break;
5059 case AttributeList::AT_GNUInline:
5060 handleGNUInlineAttr(S, D, Attr);
5061 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005062 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005063 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00005064 break;
David Majnemer631a90b2015-02-04 07:23:21 +00005065 case AttributeList::AT_Restrict:
5066 handleRestrictAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005067 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005068 case AttributeList::AT_MayAlias:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005069 handleSimpleAttribute<MayAliasAttr>(S, D, Attr);
5070 break;
5071 case AttributeList::AT_Mode:
5072 handleModeAttr(S, D, Attr);
5073 break;
David Majnemer1bf0f8e2015-07-20 22:51:52 +00005074 case AttributeList::AT_NoAlias:
5075 handleSimpleAttribute<NoAliasAttr>(S, D, Attr);
5076 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005077 case AttributeList::AT_NoCommon:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005078 handleSimpleAttribute<NoCommonAttr>(S, D, Attr);
5079 break;
Peter Collingbourneb4728c12014-05-19 22:14:34 +00005080 case AttributeList::AT_NoSplitStack:
5081 handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr);
5082 break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00005083 case AttributeList::AT_NonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005084 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
5085 handleNonNullAttrParameter(S, PVD, Attr);
5086 else
5087 handleNonNullAttr(S, D, Attr);
5088 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00005089 case AttributeList::AT_ReturnsNonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005090 handleReturnsNonNullAttr(S, D, Attr);
5091 break;
Hal Finkelee90a222014-09-26 05:04:30 +00005092 case AttributeList::AT_AssumeAligned:
5093 handleAssumeAlignedAttr(S, D, Attr);
5094 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005095 case AttributeList::AT_Overloadable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005096 handleSimpleAttribute<OverloadableAttr>(S, D, Attr);
5097 break;
5098 case AttributeList::AT_Ownership:
5099 handleOwnershipAttr(S, D, Attr);
5100 break;
5101 case AttributeList::AT_Cold:
5102 handleColdAttr(S, D, Attr);
5103 break;
5104 case AttributeList::AT_Hot:
5105 handleHotAttr(S, D, Attr);
5106 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005107 case AttributeList::AT_Naked:
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00005108 handleNakedAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005109 break;
5110 case AttributeList::AT_NoReturn:
5111 handleNoReturnAttr(S, D, Attr);
5112 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00005113 case AttributeList::AT_NoThrow:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005114 handleSimpleAttribute<NoThrowAttr>(S, D, Attr);
5115 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005116 case AttributeList::AT_CUDAShared:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005117 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr);
5118 break;
5119 case AttributeList::AT_VecReturn:
5120 handleVecReturnAttr(S, D, Attr);
5121 break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005122
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005123 case AttributeList::AT_ObjCOwnership:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005124 handleObjCOwnershipAttr(S, D, Attr);
5125 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005126 case AttributeList::AT_ObjCPreciseLifetime:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005127 handleObjCPreciseLifetimeAttr(S, D, Attr);
5128 break;
John McCall31168b02011-06-15 23:02:42 +00005129
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005130 case AttributeList::AT_ObjCReturnsInnerPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005131 handleObjCReturnsInnerPointerAttr(S, D, Attr);
5132 break;
John McCallcf166702011-07-22 08:53:00 +00005133
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005134 case AttributeList::AT_ObjCRequiresSuper:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005135 handleObjCRequiresSuperAttr(S, D, Attr);
5136 break;
5137
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005138 case AttributeList::AT_ObjCBridge:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005139 handleObjCBridgeAttr(S, scope, D, Attr);
5140 break;
5141
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005142 case AttributeList::AT_ObjCBridgeMutable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005143 handleObjCBridgeMutableAttr(S, scope, D, Attr);
5144 break;
5145
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005146 case AttributeList::AT_ObjCBridgeRelated:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005147 handleObjCBridgeRelatedAttr(S, scope, D, Attr);
5148 break;
John McCallf1e8b342011-09-29 07:17:38 +00005149
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005150 case AttributeList::AT_ObjCDesignatedInitializer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005151 handleObjCDesignatedInitializer(S, D, Attr);
5152 break;
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005153
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00005154 case AttributeList::AT_ObjCRuntimeName:
5155 handleObjCRuntimeName(S, D, Attr);
5156 break;
Alex Denisovfde64952015-06-26 05:28:36 +00005157
5158 case AttributeList::AT_ObjCBoxable:
5159 handleObjCBoxable(S, D, Attr);
5160 break;
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00005161
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005162 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005163 handleCFAuditedTransferAttr(S, D, Attr);
5164 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005165 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005166 handleCFUnknownTransferAttr(S, D, Attr);
5167 break;
John McCall32f5fe12011-09-30 05:12:12 +00005168
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005169 case AttributeList::AT_CFConsumed:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005170 case AttributeList::AT_NSConsumed:
5171 handleNSConsumedAttr(S, D, Attr);
5172 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005173 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005174 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr);
5175 break;
John McCalled433932011-01-25 03:31:58 +00005176
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005177 case AttributeList::AT_NSReturnsAutoreleased:
5178 case AttributeList::AT_NSReturnsNotRetained:
5179 case AttributeList::AT_CFReturnsNotRetained:
5180 case AttributeList::AT_NSReturnsRetained:
5181 case AttributeList::AT_CFReturnsRetained:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005182 handleNSReturnsRetainedAttr(S, D, Attr);
5183 break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00005184 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005185 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr);
5186 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005187 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005188 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr);
5189 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00005190 case AttributeList::AT_VecTypeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005191 handleVecTypeHint(S, D, Attr);
5192 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00005193
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005194 case AttributeList::AT_InitPriority:
5195 handleInitPriorityAttr(S, D, Attr);
5196 break;
5197
5198 case AttributeList::AT_Packed:
5199 handlePackedAttr(S, D, Attr);
5200 break;
5201 case AttributeList::AT_Section:
5202 handleSectionAttr(S, D, Attr);
5203 break;
Eric Christopher11acf732015-06-12 01:35:52 +00005204 case AttributeList::AT_Target:
5205 handleTargetAttr(S, D, Attr);
5206 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005207 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00005208 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00005209 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005210 case AttributeList::AT_ArcWeakrefUnavailable:
5211 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr);
5212 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005213 case AttributeList::AT_ObjCRootClass:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005214 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr);
5215 break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00005216 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek438f8db2014-02-22 01:06:05 +00005217 handleObjCSuppresProtocolAttr(S, D, Attr);
Ted Kremenek28eace62013-11-23 01:01:34 +00005218 break;
5219 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005220 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr);
5221 break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00005222 case AttributeList::AT_Unused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005223 handleSimpleAttribute<UnusedAttr>(S, D, Attr);
5224 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005225 case AttributeList::AT_ReturnsTwice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005226 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr);
5227 break;
Akira Hatanakac8667622015-11-06 23:56:15 +00005228 case AttributeList::AT_NotTailCalled:
5229 handleNotTailCalledAttr(S, D, Attr);
5230 break;
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00005231 case AttributeList::AT_DisableTailCalls:
5232 handleDisableTailCallsAttr(S, D, Attr);
5233 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005234 case AttributeList::AT_Used:
5235 handleUsedAttr(S, D, Attr);
5236 break;
John McCalld041a9b2013-02-20 01:54:26 +00005237 case AttributeList::AT_Visibility:
5238 handleVisibilityAttr(S, D, Attr, false);
5239 break;
5240 case AttributeList::AT_TypeVisibility:
5241 handleVisibilityAttr(S, D, Attr, true);
5242 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00005243 case AttributeList::AT_WarnUnused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005244 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr);
5245 break;
5246 case AttributeList::AT_WarnUnusedResult:
5247 handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00005248 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00005249 case AttributeList::AT_Weak:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005250 handleSimpleAttribute<WeakAttr>(S, D, Attr);
5251 break;
5252 case AttributeList::AT_WeakRef:
5253 handleWeakRefAttr(S, D, Attr);
5254 break;
5255 case AttributeList::AT_WeakImport:
5256 handleWeakImportAttr(S, D, Attr);
5257 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005258 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005259 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005260 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005261 case AttributeList::AT_ObjCException:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005262 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr);
5263 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005264 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005265 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00005266 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005267 case AttributeList::AT_ObjCNSObject:
5268 handleObjCNSObject(S, D, Attr);
5269 break;
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00005270 case AttributeList::AT_ObjCIndependentClass:
5271 handleObjCIndependentClass(S, D, Attr);
5272 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005273 case AttributeList::AT_Blocks:
5274 handleBlocksAttr(S, D, Attr);
5275 break;
5276 case AttributeList::AT_Sentinel:
5277 handleSentinelAttr(S, D, Attr);
5278 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00005279 case AttributeList::AT_Const:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005280 handleSimpleAttribute<ConstAttr>(S, D, Attr);
5281 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005282 case AttributeList::AT_Pure:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005283 handleSimpleAttribute<PureAttr>(S, D, Attr);
5284 break;
5285 case AttributeList::AT_Cleanup:
5286 handleCleanupAttr(S, D, Attr);
5287 break;
5288 case AttributeList::AT_NoDebug:
5289 handleNoDebugAttr(S, D, Attr);
5290 break;
Aaron Ballman7c19ab12014-02-22 16:59:24 +00005291 case AttributeList::AT_NoDuplicate:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005292 handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr);
5293 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005294 case AttributeList::AT_NoInline:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005295 handleSimpleAttribute<NoInlineAttr>(S, D, Attr);
5296 break;
5297 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
5298 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr);
5299 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005300 case AttributeList::AT_StdCall:
5301 case AttributeList::AT_CDecl:
5302 case AttributeList::AT_FastCall:
5303 case AttributeList::AT_ThisCall:
5304 case AttributeList::AT_Pascal:
Reid Klecknerd7857f02014-10-24 17:42:17 +00005305 case AttributeList::AT_VectorCall:
Charles Davisb5a214e2013-08-30 04:39:01 +00005306 case AttributeList::AT_MSABI:
5307 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005308 case AttributeList::AT_Pcs:
Guy Benyeif0a014b2012-12-25 08:53:55 +00005309 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005310 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00005311 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005312 case AttributeList::AT_OpenCLKernel:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005313 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr);
5314 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00005315 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005316 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr);
5317 break;
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005318 case AttributeList::AT_InternalLinkage:
5319 handleInternalLinkageAttr(S, D, Attr);
5320 break;
John McCall8d32c052012-05-22 21:28:12 +00005321
5322 // Microsoft attributes:
David Majnemer8ab003a2015-02-02 19:30:52 +00005323 case AttributeList::AT_MSNoVTable:
5324 handleSimpleAttribute<MSNoVTableAttr>(S, D, Attr);
David Majnemer129f4172015-02-02 10:22:20 +00005325 break;
David Majnemer8ab003a2015-02-02 19:30:52 +00005326 case AttributeList::AT_MSStruct:
5327 handleSimpleAttribute<MSStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00005328 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005329 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005330 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00005331 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00005332 case AttributeList::AT_MSInheritance:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005333 handleMSInheritanceAttr(S, D, Attr);
5334 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00005335 case AttributeList::AT_SelectAny:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005336 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr);
5337 break;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005338 case AttributeList::AT_Thread:
5339 handleDeclspecThreadAttr(S, D, Attr);
5340 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005341
5342 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00005343 case AttributeList::AT_AssertExclusiveLock:
5344 handleAssertExclusiveLockAttr(S, D, Attr);
5345 break;
5346 case AttributeList::AT_AssertSharedLock:
5347 handleAssertSharedLockAttr(S, D, Attr);
5348 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005349 case AttributeList::AT_GuardedVar:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005350 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr);
5351 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005352 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00005353 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005354 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005355 case AttributeList::AT_ScopedLockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005356 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr);
5357 break;
Peter Collingbourne915df992015-05-15 18:33:32 +00005358 case AttributeList::AT_NoSanitize:
5359 handleNoSanitizeAttr(S, D, Attr);
5360 break;
5361 case AttributeList::AT_NoSanitizeSpecific:
5362 handleNoSanitizeSpecificAttr(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00005363 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005364 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005365 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00005366 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005367 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005368 handleGuardedByAttr(S, D, Attr);
5369 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005370 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00005371 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005372 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005373 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005374 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005375 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005376 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005377 handleLockReturnedAttr(S, D, Attr);
5378 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005379 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005380 handleLocksExcludedAttr(S, D, Attr);
5381 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005382 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005383 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005384 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005385 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00005386 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005387 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005388 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00005389 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005390 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005391
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005392 // Capability analysis attributes.
5393 case AttributeList::AT_Capability:
5394 case AttributeList::AT_Lockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005395 handleCapabilityAttr(S, D, Attr);
5396 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005397 case AttributeList::AT_RequiresCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005398 handleRequiresCapabilityAttr(S, D, Attr);
5399 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005400
5401 case AttributeList::AT_AssertCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005402 handleAssertCapabilityAttr(S, D, Attr);
5403 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005404 case AttributeList::AT_AcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005405 handleAcquireCapabilityAttr(S, D, Attr);
5406 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005407 case AttributeList::AT_ReleaseCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005408 handleReleaseCapabilityAttr(S, D, Attr);
5409 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005410 case AttributeList::AT_TryAcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005411 handleTryAcquireCapabilityAttr(S, D, Attr);
5412 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005413
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00005414 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00005415 case AttributeList::AT_Consumable:
5416 handleConsumableAttr(S, D, Attr);
5417 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00005418 case AttributeList::AT_ConsumableAutoCast:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005419 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr);
5420 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00005421 case AttributeList::AT_ConsumableSetOnRead:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005422 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr);
5423 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00005424 case AttributeList::AT_CallableWhen:
5425 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00005426 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00005427 case AttributeList::AT_ParamTypestate:
5428 handleParamTypestateAttr(S, D, Attr);
5429 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00005430 case AttributeList::AT_ReturnTypestate:
5431 handleReturnTypestateAttr(S, D, Attr);
5432 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00005433 case AttributeList::AT_SetTypestate:
5434 handleSetTypestateAttr(S, D, Attr);
5435 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00005436 case AttributeList::AT_TestTypestate:
5437 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00005438 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00005439
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00005440 // Type safety attributes.
5441 case AttributeList::AT_ArgumentWithTypeTag:
5442 handleArgumentWithTypeTagAttr(S, D, Attr);
5443 break;
5444 case AttributeList::AT_TypeTagForDatatype:
5445 handleTypeTagForDatatypeAttr(S, D, Attr);
5446 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005447 }
5448}
5449
5450/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5451/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00005452void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00005453 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00005454 bool IncludeCXX11Attributes) {
5455 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00005456 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00005457
Joey Gouly2cd9db12013-12-13 16:15:28 +00005458 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00005459 // GCC accepts
5460 // static int a9 __attribute__((weakref));
5461 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00005462 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00005463 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
5464 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00005465 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00005466 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005467 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00005468
Aaron Ballmanbe243a72014-12-04 22:45:31 +00005469 // FIXME: We should be able to handle this in TableGen as well. It would be
5470 // good to have a way to specify "these attributes must appear as a group",
5471 // for these. Additionally, it would be good to have a way to specify "these
5472 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00005473 if (!D->hasAttr<OpenCLKernelAttr>()) {
5474 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00005475 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00005476 // FIXME: This emits a different error message than
5477 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00005478 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005479 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00005480 } else if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
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<VecTypeHintAttr>()) {
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 Arsenaultb9e9dc52014-12-05 18:03:58 +00005486 } else if (Attr *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
5487 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
5488 << A << ExpectedKernelFunction;
5489 D->setInvalidDecl();
5490 } else if (Attr *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
5491 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
5492 << A << ExpectedKernelFunction;
5493 D->setInvalidDecl();
Joey Gouly2cd9db12013-12-13 16:15:28 +00005494 }
5495 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005496}
5497
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005498// Annotation attributes are the only attributes allowed after an access
5499// specifier.
5500bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5501 const AttributeList *AttrList) {
5502 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005503 if (l->getKind() == AttributeList::AT_Annotate) {
David Majnemer706f3152014-12-14 01:05:01 +00005504 ProcessDeclAttribute(*this, nullptr, ASDecl, *l, l->isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005505 } else {
5506 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5507 return true;
5508 }
5509 }
5510
5511 return false;
5512}
5513
John McCall42856de2011-10-01 05:17:03 +00005514/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5515/// contains any decl attributes that we should warn about.
5516static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5517 for ( ; A; A = A->getNext()) {
5518 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00005519 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00005520 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5521
5522 if (A->getKind() == AttributeList::UnknownAttribute) {
5523 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5524 << A->getName() << A->getRange();
5525 } else {
5526 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5527 << A->getName() << A->getRange();
5528 }
5529 }
5530}
5531
5532/// checkUnusedDeclAttributes - Given a declarator which is not being
5533/// used to build a declaration, complain about any decl attributes
5534/// which might be lying around on it.
5535void Sema::checkUnusedDeclAttributes(Declarator &D) {
5536 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5537 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5538 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5539 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5540}
5541
Ryan Flynn7d470f32009-07-30 03:15:39 +00005542/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00005543/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00005544NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5545 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00005546 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00005547 NamedDecl *NewD = nullptr;
Ryan Flynn7d470f32009-07-30 03:15:39 +00005548 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00005549 // FIXME: Mangling?
5550 // FIXME: Is the qualifier info correct?
5551 // FIXME: Is the DeclContext correct?
Alexander Musmanf97c8932015-11-26 09:34:30 +00005552
5553 LookupResult Previous(*this, II, Loc, LookupOrdinaryName);
5554 LookupParsedName(Previous, TUScope, nullptr, true);
5555
5556 auto NewFD = FunctionDecl::Create(
5557 FD->getASTContext(), FD->getDeclContext(), Loc, Loc,
5558 DeclarationName(II), FD->getType(), FD->getTypeSourceInfo(), SC_None,
5559 false /*isInlineSpecified*/, FD->hasPrototype(),
5560 false /*isConstexprSpecified*/);
5561
5562 CheckFunctionDeclaration(TUScope, NewFD, Previous,
5563 false /*IsExplicitSpecialization*/);
5564
Eli Friedmance3e2c82011-09-07 04:05:06 +00005565 NewD = NewFD;
5566
5567 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00005568 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00005569
5570 // Fake up parameter variables; they are declared as if this were
5571 // a typedef.
5572 QualType FDTy = FD->getType();
5573 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5574 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00005575 for (const auto &AI : FT->param_types()) {
5576 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00005577 Param->setScopeInfo(0, Params.size());
5578 Params.push_back(Param);
5579 }
David Blaikie9c70e042011-09-21 18:16:56 +00005580 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00005581 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005582 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5583 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00005584 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00005585 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005586 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00005587 if (VD->getQualifier()) {
5588 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00005589 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00005590 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005591 }
5592 return NewD;
5593}
5594
James Dennett634962f2012-06-14 21:40:34 +00005595/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00005596/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00005597void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00005598 if (W.getUsed()) return; // only do this once
5599 W.setUsed(true);
5600 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5601 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00005602 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00005603 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
5604 W.getLocation()));
5605 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00005606 WeakTopLevelDecl.push_back(NewD);
5607 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5608 // to insert Decl at TU scope, sorry.
5609 DeclContext *SavedContext = CurContext;
5610 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00005611 NewD->setDeclContext(CurContext);
5612 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00005613 PushOnScopeChains(NewD, S);
5614 CurContext = SavedContext;
5615 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00005616 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005617 }
5618}
5619
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005620void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5621 // It's valid to "forward-declare" #pragma weak, in which case we
5622 // have to do this.
5623 LoadExternalWeakUndeclaredIdentifiers();
5624 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00005625 NamedDecl *ND = nullptr;
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005626 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5627 if (VD->isExternC())
5628 ND = VD;
5629 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5630 if (FD->isExternC())
5631 ND = FD;
5632 if (ND) {
5633 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00005634 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005635 if (I != WeakUndeclaredIdentifiers.end()) {
5636 WeakInfo W = I->second;
5637 DeclApplyPragmaWeak(S, ND, W);
5638 WeakUndeclaredIdentifiers[Id] = W;
5639 }
5640 }
5641 }
5642 }
5643}
5644
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005645/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5646/// it, apply them to D. This is a bit tricky because PD can have attributes
5647/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00005648void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005649 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00005650 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00005651 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005652
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005653 // Walk the declarator structure, applying decl attributes that were in a type
5654 // position to the decl itself. This handles cases like:
5655 // int *__attr__(x)** D;
5656 // when X is a decl attribute.
5657 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5658 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00005659 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005660
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005661 // Finally, apply any attributes on the decl itself.
5662 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00005663 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005664}
John McCall28a6aea2009-11-04 02:18:39 +00005665
John McCall31168b02011-06-15 23:02:42 +00005666/// Is the given declaration allowed to use a forbidden type?
John McCallb61e14e2015-10-27 04:54:50 +00005667/// If so, it'll still be annotated with an attribute that makes it
5668/// illegal to actually use.
5669static bool isForbiddenTypeAllowed(Sema &S, Decl *decl,
5670 const DelayedDiagnostic &diag,
John McCallc6af8c62015-10-28 05:03:19 +00005671 UnavailableAttr::ImplicitReason &reason) {
John McCall31168b02011-06-15 23:02:42 +00005672 // Private ivars are always okay. Unfortunately, people don't
5673 // always properly make their ivars private, even in system headers.
5674 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005675 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5676 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005677 return false;
5678
John McCallc6af8c62015-10-28 05:03:19 +00005679 // Silently accept unsupported uses of __weak in both user and system
5680 // declarations when it's been disabled, for ease of integration with
5681 // -fno-objc-arc files. We do have to take some care against attempts
5682 // to define such things; for now, we've only done that for ivars
5683 // and properties.
5684 if ((isa<ObjCIvarDecl>(decl) || isa<ObjCPropertyDecl>(decl))) {
5685 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
5686 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
5687 reason = UnavailableAttr::IR_ForbiddenWeak;
5688 return true;
5689 }
John McCallb61e14e2015-10-27 04:54:50 +00005690 }
5691
John McCallc6af8c62015-10-28 05:03:19 +00005692 // Allow all sorts of things in system headers.
5693 if (S.Context.getSourceManager().isInSystemHeader(decl->getLocation())) {
5694 // Currently, all the failures dealt with this way are due to ARC
5695 // restrictions.
5696 reason = UnavailableAttr::IR_ARCForbiddenType;
5697 return true;
John McCallb61e14e2015-10-27 04:54:50 +00005698 }
5699
5700 return false;
John McCall31168b02011-06-15 23:02:42 +00005701}
5702
5703/// Handle a delayed forbidden-type diagnostic.
5704static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5705 Decl *decl) {
John McCallc6af8c62015-10-28 05:03:19 +00005706 auto reason = UnavailableAttr::IR_None;
5707 if (decl && isForbiddenTypeAllowed(S, decl, diag, reason)) {
5708 assert(reason && "didn't set reason?");
5709 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", reason,
5710 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00005711 return;
5712 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005713 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005714 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005715 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005716 // kind of forbidden type messages on unavailable functions.
5717 if (FD->hasAttr<UnavailableAttr>() &&
5718 diag.getForbiddenTypeDiagnostic() ==
5719 diag::err_arc_array_param_no_ownership) {
5720 diag.Triggered = true;
5721 return;
5722 }
5723 }
John McCall31168b02011-06-15 23:02:42 +00005724
5725 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5726 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5727 diag.Triggered = true;
5728}
5729
Aaron Ballmanfb237522014-10-15 15:37:51 +00005730
5731static bool isDeclDeprecated(Decl *D) {
5732 do {
5733 if (D->isDeprecated())
5734 return true;
5735 // A category implicitly has the availability of the interface.
5736 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005737 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
5738 return Interface->isDeprecated();
Aaron Ballmanfb237522014-10-15 15:37:51 +00005739 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5740 return false;
5741}
5742
5743static bool isDeclUnavailable(Decl *D) {
5744 do {
5745 if (D->isUnavailable())
5746 return true;
5747 // A category implicitly has the availability of the interface.
5748 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005749 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
5750 return Interface->isUnavailable();
Aaron Ballmanfb237522014-10-15 15:37:51 +00005751 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5752 return false;
5753}
5754
Nico Weber0055a192015-03-19 19:18:22 +00005755static void DoEmitAvailabilityWarning(Sema &S, Sema::AvailabilityDiagnostic K,
Aaron Ballmanfb237522014-10-15 15:37:51 +00005756 Decl *Ctx, const NamedDecl *D,
5757 StringRef Message, SourceLocation Loc,
5758 const ObjCInterfaceDecl *UnknownObjCClass,
5759 const ObjCPropertyDecl *ObjCProperty,
5760 bool ObjCPropertyAccess) {
5761 // Diagnostics for deprecated or unavailable.
5762 unsigned diag, diag_message, diag_fwdclass_message;
John McCallb61e14e2015-10-27 04:54:50 +00005763 unsigned diag_available_here = diag::note_availability_specified_here;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005764
5765 // Matches 'diag::note_property_attribute' options.
5766 unsigned property_note_select;
5767
5768 // Matches diag::note_availability_specified_here.
5769 unsigned available_here_select_kind;
5770
5771 // Don't warn if our current context is deprecated or unavailable.
5772 switch (K) {
Nico Weber0055a192015-03-19 19:18:22 +00005773 case Sema::AD_Deprecation:
Jordan Rosed17c03e2015-04-30 17:20:35 +00005774 if (isDeclDeprecated(Ctx) || isDeclUnavailable(Ctx))
Aaron Ballmanfb237522014-10-15 15:37:51 +00005775 return;
5776 diag = !ObjCPropertyAccess ? diag::warn_deprecated
5777 : diag::warn_property_method_deprecated;
5778 diag_message = diag::warn_deprecated_message;
5779 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
5780 property_note_select = /* deprecated */ 0;
5781 available_here_select_kind = /* deprecated */ 2;
5782 break;
5783
Nico Weber0055a192015-03-19 19:18:22 +00005784 case Sema::AD_Unavailable:
Aaron Ballmanfb237522014-10-15 15:37:51 +00005785 if (isDeclUnavailable(Ctx))
5786 return;
5787 diag = !ObjCPropertyAccess ? diag::err_unavailable
5788 : diag::err_property_method_unavailable;
5789 diag_message = diag::err_unavailable_message;
5790 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
5791 property_note_select = /* unavailable */ 1;
5792 available_here_select_kind = /* unavailable */ 0;
John McCallb61e14e2015-10-27 04:54:50 +00005793
John McCallc6af8c62015-10-28 05:03:19 +00005794 if (auto attr = D->getAttr<UnavailableAttr>()) {
5795 if (attr->isImplicit() && attr->getImplicitReason()) {
5796 // Most of these failures are due to extra restrictions in ARC;
5797 // reflect that in the primary diagnostic when applicable.
5798 auto flagARCError = [&] {
5799 if (S.getLangOpts().ObjCAutoRefCount &&
5800 S.getSourceManager().isInSystemHeader(D->getLocation()))
5801 diag = diag::err_unavailable_in_arc;
5802 };
5803
5804 switch (attr->getImplicitReason()) {
5805 case UnavailableAttr::IR_None: break;
5806
5807 case UnavailableAttr::IR_ARCForbiddenType:
5808 flagARCError();
5809 diag_available_here = diag::note_arc_forbidden_type;
5810 break;
5811
5812 case UnavailableAttr::IR_ForbiddenWeak:
5813 if (S.getLangOpts().ObjCWeakRuntime)
5814 diag_available_here = diag::note_arc_weak_disabled;
5815 else
5816 diag_available_here = diag::note_arc_weak_no_runtime;
5817 break;
5818
5819 case UnavailableAttr::IR_ARCForbiddenConversion:
5820 flagARCError();
5821 diag_available_here = diag::note_performs_forbidden_arc_conversion;
5822 break;
5823
5824 case UnavailableAttr::IR_ARCInitReturnsUnrelated:
5825 flagARCError();
5826 diag_available_here = diag::note_arc_init_returns_unrelated;
5827 break;
5828
5829 case UnavailableAttr::IR_ARCFieldWithOwnership:
5830 flagARCError();
5831 diag_available_here = diag::note_arc_field_with_ownership;
5832 break;
5833 }
5834 }
John McCallb61e14e2015-10-27 04:54:50 +00005835 }
5836
Aaron Ballmanfb237522014-10-15 15:37:51 +00005837 break;
5838
Nico Weber0055a192015-03-19 19:18:22 +00005839 case Sema::AD_Partial:
5840 diag = diag::warn_partial_availability;
5841 diag_message = diag::warn_partial_message;
5842 diag_fwdclass_message = diag::warn_partial_fwdclass_message;
5843 property_note_select = /* partial */ 2;
5844 available_here_select_kind = /* partial */ 3;
5845 break;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005846 }
5847
Aaron Ballmanfb237522014-10-15 15:37:51 +00005848 if (!Message.empty()) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005849 S.Diag(Loc, diag_message) << D << Message;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005850 if (ObjCProperty)
5851 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5852 << ObjCProperty->getDeclName() << property_note_select;
5853 } else if (!UnknownObjCClass) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005854 S.Diag(Loc, diag) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005855 if (ObjCProperty)
5856 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5857 << ObjCProperty->getDeclName() << property_note_select;
5858 } else {
Aaron Ballman43f40102014-11-14 22:34:56 +00005859 S.Diag(Loc, diag_fwdclass_message) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005860 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5861 }
5862
John McCallb61e14e2015-10-27 04:54:50 +00005863 S.Diag(D->getLocation(), diag_available_here)
Aaron Ballmanfb237522014-10-15 15:37:51 +00005864 << D << available_here_select_kind;
Nico Weber0055a192015-03-19 19:18:22 +00005865 if (K == Sema::AD_Partial)
5866 S.Diag(Loc, diag::note_partial_availability_silence) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005867}
5868
5869static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
5870 Decl *Ctx) {
Nico Weber0055a192015-03-19 19:18:22 +00005871 assert(DD.Kind == DelayedDiagnostic::Deprecation ||
5872 DD.Kind == DelayedDiagnostic::Unavailable);
5873 Sema::AvailabilityDiagnostic AD = DD.Kind == DelayedDiagnostic::Deprecation
5874 ? Sema::AD_Deprecation
5875 : Sema::AD_Unavailable;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005876 DD.Triggered = true;
Nico Weber0055a192015-03-19 19:18:22 +00005877 DoEmitAvailabilityWarning(
5878 S, AD, Ctx, DD.getDeprecationDecl(), DD.getDeprecationMessage(), DD.Loc,
5879 DD.getUnknownObjCClass(), DD.getObjCProperty(), false);
Aaron Ballmanfb237522014-10-15 15:37:51 +00005880}
5881
John McCall2ec85372012-05-07 06:16:41 +00005882void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5883 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005884 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005885 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005886
John McCall2ec85372012-05-07 06:16:41 +00005887 // When delaying diagnostics to run in the context of a parsed
5888 // declaration, we only want to actually emit anything if parsing
5889 // succeeds.
5890 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005891
John McCall2ec85372012-05-07 06:16:41 +00005892 // We emit all the active diagnostics in this pool or any of its
5893 // parents. In general, we'll get one pool for the decl spec
5894 // and a child pool for each declarator; in a decl group like:
5895 // deprecated_typedef foo, *bar, baz();
5896 // only the declarator pops will be passed decls. This is correct;
5897 // we really do need to consider delayed diagnostics from the decl spec
5898 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00005899 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00005900 do {
John McCall6347b682012-05-07 06:16:58 +00005901 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00005902 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5903 // This const_cast is a bit lame. Really, Triggered should be mutable.
5904 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00005905 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00005906 continue;
5907
John McCallc1465822011-02-14 07:13:47 +00005908 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00005909 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00005910 case DelayedDiagnostic::Unavailable:
5911 // Don't bother giving deprecation/unavailable diagnostics if
5912 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00005913 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00005914 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00005915 break;
5916
5917 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00005918 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005919 break;
John McCall31168b02011-06-15 23:02:42 +00005920
5921 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00005922 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00005923 break;
John McCall86121512010-01-27 03:50:35 +00005924 }
5925 }
John McCall2ec85372012-05-07 06:16:41 +00005926 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00005927}
5928
John McCall6347b682012-05-07 06:16:58 +00005929/// Given a set of delayed diagnostics, re-emit them as if they had
5930/// been delayed in the current context instead of in the given pool.
5931/// Essentially, this just moves them to the current pool.
5932void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5933 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5934 assert(curPool && "re-emitting in undelayed context not supported");
5935 curPool->steal(pool);
5936}
5937
Ted Kremenekb79ee572013-12-18 23:30:06 +00005938void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
5939 NamedDecl *D, StringRef Message,
5940 SourceLocation Loc,
5941 const ObjCInterfaceDecl *UnknownObjCClass,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00005942 const ObjCPropertyDecl *ObjCProperty,
5943 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00005944 // Delay if we're currently parsing a declaration.
Nico Weber0055a192015-03-19 19:18:22 +00005945 if (DelayedDiagnostics.shouldDelayDiagnostics() && AD != AD_Partial) {
Nico Weber462fd1e2015-01-07 23:50:05 +00005946 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(
5947 AD, Loc, D, UnknownObjCClass, ObjCProperty, Message,
5948 ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00005949 return;
5950 }
5951
Ted Kremenekb79ee572013-12-18 23:30:06 +00005952 Decl *Ctx = cast<Decl>(getCurLexicalContext());
Nico Weber0055a192015-03-19 19:18:22 +00005953 DoEmitAvailabilityWarning(*this, AD, Ctx, D, Message, Loc, UnknownObjCClass,
5954 ObjCProperty, ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00005955}