blob: e0ce3adce4243c2d38f2338b2318aad1a1911b06 [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
Justin Lebar3eaaf862016-01-13 01:07:35 +0000351template <typename AttrType>
352static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
353 const AttributeList &Attr) {
354 handleSimpleAttribute<AttrType>(S, D, Attr);
355}
356
357/// \brief Applies the given attribute to the Decl so long as the Decl doesn't
358/// already have one of the given incompatible attributes.
359template <typename AttrType, typename IncompatibleAttrType,
360 typename... IncompatibleAttrTypes>
361static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
362 const AttributeList &Attr) {
363 if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, Attr.getRange(),
364 Attr.getName()))
365 return;
366 handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
367 Attr);
368}
369
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000370/// \brief Check if the passed-in expression is of type int or bool.
371static bool isIntOrBool(Expr *Exp) {
372 QualType QT = Exp->getType();
373 return QT->isBooleanType() || QT->isIntegerType();
374}
375
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000376
377// Check to see if the type is a smart pointer of some kind. We assume
378// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000379static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
Richard Smithcf4bdde2015-02-21 02:45:19 +0000380 DeclContextLookupResult Res1 = RT->getDecl()->lookup(
381 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000382 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000383 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000384
Richard Smithcf4bdde2015-02-21 02:45:19 +0000385 DeclContextLookupResult Res2 = RT->getDecl()->lookup(
386 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000387 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000388 return false;
389
390 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000391}
392
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000393/// \brief Check if passed in Decl is a pointer type.
394/// Note that this function may produce an error message.
395/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000396static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
397 const AttributeList &Attr) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000398 const ValueDecl *vd = cast<ValueDecl>(D);
399 QualType QT = vd->getType();
400 if (QT->isAnyPointerType())
401 return true;
402
403 if (const RecordType *RT = QT->getAs<RecordType>()) {
404 // If it's an incomplete type, it could be a smart pointer; skip it.
405 // (We don't want to force template instantiation if we can avoid it,
406 // since that would alter the order in which templates are instantiated.)
407 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000408 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000409
Aaron Ballman553e6812013-12-26 14:54:11 +0000410 if (threadSafetyCheckIsSmartPointer(S, RT))
411 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000412 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000413
414 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Aaron Ballman68289452013-12-26 15:06:01 +0000415 << Attr.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000416 return false;
417}
418
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000419/// \brief Checks that the passed in QualType either is of RecordType or points
420/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000421static const RecordType *getRecordType(QualType QT) {
422 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000423 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000424
425 // Now check if we point to record type.
426 if (const PointerType *PT = QT->getAs<PointerType>())
427 return PT->getPointeeType()->getAs<RecordType>();
428
Craig Topperc3ec1492014-05-26 06:22:03 +0000429 return nullptr;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000430}
431
Aaron Ballman76050722014-04-04 15:13:57 +0000432static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000433 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000434
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000435 if (!RT)
436 return false;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000437
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000438 // Don't check for the capability if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000439 if (RT->isIncompleteType())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000440 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000441
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000442 // Allow smart pointers to be used as capability objects.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000443 // FIXME -- Check the type that the smart pointer points to.
444 if (threadSafetyCheckIsSmartPointer(S, RT))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000445 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000446
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000447 // Check if the record itself has a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000448 RecordDecl *RD = RT->getDecl();
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000449 if (RD->hasAttr<CapabilityAttr>())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000450 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000451
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000452 // Else check if any base classes have a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000453 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
454 CXXBasePaths BPaths(false, false);
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000455 if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &) {
456 const auto *Type = BS->getType()->getAs<RecordType>();
457 return Type->getDecl()->hasAttr<CapabilityAttr>();
458 }, BPaths))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000459 return true;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000460 }
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000461 return false;
462}
463
Aaron Ballman76050722014-04-04 15:13:57 +0000464static bool checkTypedefTypeForCapability(QualType Ty) {
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000465 const auto *TD = Ty->getAs<TypedefType>();
466 if (!TD)
467 return false;
468
469 TypedefNameDecl *TN = TD->getDecl();
470 if (!TN)
471 return false;
472
473 return TN->hasAttr<CapabilityAttr>();
474}
475
Aaron Ballman76050722014-04-04 15:13:57 +0000476static bool typeHasCapability(Sema &S, QualType Ty) {
477 if (checkTypedefTypeForCapability(Ty))
478 return true;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000479
Aaron Ballman76050722014-04-04 15:13:57 +0000480 if (checkRecordTypeForCapability(S, Ty))
481 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000482
Aaron Ballman76050722014-04-04 15:13:57 +0000483 return false;
484}
485
486static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
487 // Capability expressions are simple expressions involving the boolean logic
488 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
489 // a DeclRefExpr is found, its type should be checked to determine whether it
490 // is a capability or not.
491
492 if (const auto *E = dyn_cast<DeclRefExpr>(Ex))
493 return typeHasCapability(S, E->getType());
494 else if (const auto *E = dyn_cast<CastExpr>(Ex))
495 return isCapabilityExpr(S, E->getSubExpr());
496 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
497 return isCapabilityExpr(S, E->getSubExpr());
498 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
499 if (E->getOpcode() == UO_LNot)
500 return isCapabilityExpr(S, E->getSubExpr());
501 return false;
502 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
503 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
504 return isCapabilityExpr(S, E->getLHS()) &&
505 isCapabilityExpr(S, E->getRHS());
506 return false;
507 }
508
509 return false;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000510}
511
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000512/// \brief Checks that all attribute arguments, starting from Sidx, resolve to
513/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000514/// \param Sidx The attribute argument index to start checking with.
515/// \param ParamIdxOk Whether an argument can be indexing into a function
516/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000517static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
518 const AttributeList &Attr,
519 SmallVectorImpl<Expr *> &Args,
520 int Sidx = 0,
521 bool ParamIdxOk = false) {
522 for (unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000523 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000524
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000525 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000526 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000527 Args.push_back(ArgExp);
528 continue;
529 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000530
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000531 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000532 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000533 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000534 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000535 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000536 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000537 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000538 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000539
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000540 // We allow constant strings to be used as a placeholder for expressions
541 // that are not valid C++ syntax, but warn that they are ignored.
542 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
543 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000544 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000545 continue;
546 }
547
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000548 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000549
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000550 // A pointer to member expression of the form &MyClass::mu is treated
551 // specially -- we need to look at the type of the member.
552 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
553 if (UOp->getOpcode() == UO_AddrOf)
554 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
555 if (DRE->getDecl()->isCXXInstanceMember())
556 ArgTy = DRE->getDecl()->getType();
557
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000558 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000559 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000560
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000561 // Now check if we index into a record type function param.
562 if(!RT && ParamIdxOk) {
563 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000564 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
565 if(FD && IL) {
566 unsigned int NumParams = FD->getNumParams();
567 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000568 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
569 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
570 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000571 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
572 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000573 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000574 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000575 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000576 }
577 }
578
Aaron Ballman76050722014-04-04 15:13:57 +0000579 // If the type does not have a capability, see if the components of the
580 // expression have capabilities. This allows for writing C code where the
581 // capability may be on the type, and the expression is a capability
582 // boolean logic expression. Eg) requires_capability(A || B && !C)
583 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
584 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
585 << Attr.getName() << ArgTy;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000586
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000587 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000588 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000589}
590
Chris Lattner58418ff2008-06-29 00:16:31 +0000591//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000592// Attribute Implementations
593//===----------------------------------------------------------------------===//
594
Michael Hana9171bc2012-08-03 17:40:43 +0000595static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000596 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000597 if (!threadSafetyCheckIsPointer(S, D, Attr))
598 return;
599
Michael Han99315932013-01-24 16:46:58 +0000600 D->addAttr(::new (S.Context)
601 PtGuardedVarAttr(Attr.getRange(), S.Context,
602 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000603}
604
Michael Hana9171bc2012-08-03 17:40:43 +0000605static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
606 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000607 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000608 SmallVector<Expr*, 1> Args;
609 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000610 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000611 unsigned Size = Args.size();
612 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000613 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000614
Michael Han3be3b442012-07-23 18:48:41 +0000615 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000616
Michael Han3be3b442012-07-23 18:48:41 +0000617 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000618}
619
Michael Han3be3b442012-07-23 18:48:41 +0000620static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000621 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000622 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
623 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000624
Aaron Ballman36a53502014-01-16 13:03:14 +0000625 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg,
626 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000627}
628
Michael Hana9171bc2012-08-03 17:40:43 +0000629static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000630 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000631 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000632 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
633 return;
634
635 if (!threadSafetyCheckIsPointer(S, D, Attr))
636 return;
637
638 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +0000639 S.Context, Arg,
640 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000641}
642
Michael Hana9171bc2012-08-03 17:40:43 +0000643static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
644 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000645 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000646 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000647 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000648
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000649 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000650 QualType QT = cast<ValueDecl>(D)->getType();
DeLesley Hutchins2b504dc2015-09-29 16:24:18 +0000651 if (!QT->isDependentType() && !typeHasCapability(S, QT)) {
652 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
653 << Attr.getName();
654 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000655 }
656
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000657 // Check that all arguments are lockable objects.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000658 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000659 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000660 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000661
Michael Han3be3b442012-07-23 18:48:41 +0000662 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000663}
664
Michael Hana9171bc2012-08-03 17:40:43 +0000665static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000666 const AttributeList &Attr) {
667 SmallVector<Expr*, 1> Args;
668 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
669 return;
670
671 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000672 D->addAttr(::new (S.Context)
673 AcquiredAfterAttr(Attr.getRange(), S.Context,
674 StartArg, Args.size(),
675 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000676}
677
Michael Hana9171bc2012-08-03 17:40:43 +0000678static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000679 const AttributeList &Attr) {
680 SmallVector<Expr*, 1> Args;
681 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
682 return;
683
684 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000685 D->addAttr(::new (S.Context)
686 AcquiredBeforeAttr(Attr.getRange(), S.Context,
687 StartArg, Args.size(),
688 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000689}
690
Michael Hana9171bc2012-08-03 17:40:43 +0000691static bool checkLockFunAttrCommon(Sema &S, Decl *D,
692 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000693 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000694 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000695 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000696 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000697
Michael Han3be3b442012-07-23 18:48:41 +0000698 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000699}
700
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000701static void handleAssertSharedLockAttr(Sema &S, Decl *D,
702 const AttributeList &Attr) {
703 SmallVector<Expr*, 1> Args;
704 if (!checkLockFunAttrCommon(S, D, Attr, Args))
705 return;
706
707 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000708 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000709 D->addAttr(::new (S.Context)
710 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
711 Attr.getAttributeSpellingListIndex()));
712}
713
714static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
715 const AttributeList &Attr) {
716 SmallVector<Expr*, 1> Args;
717 if (!checkLockFunAttrCommon(S, D, Attr, Args))
718 return;
719
720 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000721 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000722 D->addAttr(::new (S.Context)
723 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
724 StartArg, Size,
725 Attr.getAttributeSpellingListIndex()));
726}
727
728
Michael Hana9171bc2012-08-03 17:40:43 +0000729static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
730 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000731 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000732 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000733 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000734
Aaron Ballman00e99962013-08-31 01:11:41 +0000735 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000736 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000737 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000738 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000739 }
740
741 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000742 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000743
Michael Han3be3b442012-07-23 18:48:41 +0000744 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000745}
746
Michael Hana9171bc2012-08-03 17:40:43 +0000747static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000748 const AttributeList &Attr) {
749 SmallVector<Expr*, 2> Args;
750 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
751 return;
752
Michael Han99315932013-01-24 16:46:58 +0000753 D->addAttr(::new (S.Context)
754 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000755 Attr.getArgAsExpr(0),
756 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000757 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000758}
759
Michael Hana9171bc2012-08-03 17:40:43 +0000760static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000761 const AttributeList &Attr) {
762 SmallVector<Expr*, 2> Args;
763 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
764 return;
765
Nico Weber462fd1e2015-01-07 23:50:05 +0000766 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(
767 Attr.getRange(), S.Context, Attr.getArgAsExpr(0), Args.data(),
768 Args.size(), Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000769}
770
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000771static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000772 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000773 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000774 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000775 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000776 unsigned Size = Args.size();
777 if (Size == 0)
778 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000779
Michael Han99315932013-01-24 16:46:58 +0000780 D->addAttr(::new (S.Context)
781 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
782 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000783}
784
785static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000786 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000787 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000788 return;
789
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000790 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000791 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000792 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000793 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000794 if (Size == 0)
795 return;
796 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000797
Michael Han99315932013-01-24 16:46:58 +0000798 D->addAttr(::new (S.Context)
799 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
800 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000801}
802
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000803static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) {
804 Expr *Cond = Attr.getArgAsExpr(0);
805 if (!Cond->isTypeDependent()) {
806 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
807 if (Converted.isInvalid())
808 return;
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000809 Cond = Converted.get();
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000810 }
811
812 StringRef Msg;
813 if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg))
814 return;
815
816 SmallVector<PartialDiagnosticAt, 8> Diags;
817 if (!Cond->isValueDependent() &&
818 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
819 Diags)) {
820 S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr);
821 for (int I = 0, N = Diags.size(); I != N; ++I)
822 S.Diag(Diags[I].first, Diags[I].second);
823 return;
824 }
825
826 D->addAttr(::new (S.Context)
827 EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg,
828 Attr.getAttributeSpellingListIndex()));
829}
830
George Burgess IV3e3bb95b2015-12-02 21:58:08 +0000831static void handlePassObjectSizeAttr(Sema &S, Decl *D,
832 const AttributeList &Attr) {
833 if (D->hasAttr<PassObjectSizeAttr>()) {
834 S.Diag(D->getLocStart(), diag::err_attribute_only_once_per_parameter)
835 << Attr.getName();
836 return;
837 }
838
839 Expr *E = Attr.getArgAsExpr(0);
840 uint32_t Type;
841 if (!checkUInt32Argument(S, Attr, E, Type, /*Idx=*/1))
842 return;
843
844 // pass_object_size's argument is passed in as the second argument of
845 // __builtin_object_size. So, it has the same constraints as that second
846 // argument; namely, it must be in the range [0, 3].
847 if (Type > 3) {
848 S.Diag(E->getLocStart(), diag::err_attribute_argument_outof_range)
849 << Attr.getName() << 0 << 3 << E->getSourceRange();
850 return;
851 }
852
853 // pass_object_size is only supported on constant pointer parameters; as a
854 // kindness to users, we allow the parameter to be non-const for declarations.
855 // At this point, we have no clue if `D` belongs to a function declaration or
856 // definition, so we defer the constness check until later.
857 if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) {
858 S.Diag(D->getLocStart(), diag::err_attribute_pointers_only)
859 << Attr.getName() << 1;
860 return;
861 }
862
863 D->addAttr(::new (S.Context)
864 PassObjectSizeAttr(Attr.getRange(), S.Context, (int)Type,
865 Attr.getAttributeSpellingListIndex()));
866}
867
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000868static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000869 ConsumableAttr::ConsumedState DefaultState;
870
871 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000872 IdentifierLoc *IL = Attr.getArgAsIdent(0);
873 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
874 DefaultState)) {
875 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
876 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000877 return;
878 }
David Blaikie16f76d22013-09-06 01:28:43 +0000879 } else {
880 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
881 << Attr.getName() << AANT_ArgumentIdentifier;
882 return;
883 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000884
885 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000886 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000887 Attr.getAttributeSpellingListIndex()));
888}
889
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000890
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000891static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
892 const AttributeList &Attr) {
893 ASTContext &CurrContext = S.getASTContext();
894 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
895
896 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
897 if (!RD->hasAttr<ConsumableAttr>()) {
898 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
899 RD->getNameAsString();
900
901 return false;
902 }
903 }
904
905 return true;
906}
907
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000908
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000909static void handleCallableWhenAttr(Sema &S, Decl *D,
910 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000911 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
912 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000913
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000914 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
915 return;
916
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000917 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
918 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
919 CallableWhenAttr::ConsumedState CallableState;
920
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000921 StringRef StateString;
922 SourceLocation Loc;
Aaron Ballman55ef1512014-12-19 16:42:04 +0000923 if (Attr.isArgIdent(ArgIndex)) {
924 IdentifierLoc *Ident = Attr.getArgAsIdent(ArgIndex);
925 StateString = Ident->Ident->getName();
926 Loc = Ident->Loc;
927 } else {
928 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
929 return;
930 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000931
932 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000933 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000934 S.Diag(Loc, diag::warn_attribute_type_not_supported)
935 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000936 return;
937 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000938
939 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000940 }
941
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000942 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000943 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
944 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000945}
946
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000947
DeLesley Hutchins69391772013-10-17 23:23:53 +0000948static void handleParamTypestateAttr(Sema &S, Decl *D,
949 const AttributeList &Attr) {
DeLesley Hutchins69391772013-10-17 23:23:53 +0000950 ParamTypestateAttr::ConsumedState ParamState;
951
952 if (Attr.isArgIdent(0)) {
953 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
954 StringRef StateString = Ident->Ident->getName();
955
956 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
957 ParamState)) {
958 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
959 << Attr.getName() << StateString;
960 return;
961 }
962 } else {
963 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
964 Attr.getName() << AANT_ArgumentIdentifier;
965 return;
966 }
967
968 // FIXME: This check is currently being done in the analysis. It can be
969 // enabled here only after the parser propagates attributes at
970 // template specialization definition, not declaration.
971 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
972 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
973 //
974 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
975 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
976 // ReturnType.getAsString();
977 // return;
978 //}
979
980 D->addAttr(::new (S.Context)
981 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
982 Attr.getAttributeSpellingListIndex()));
983}
984
985
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000986static void handleReturnTypestateAttr(Sema &S, Decl *D,
987 const AttributeList &Attr) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000988 ReturnTypestateAttr::ConsumedState ReturnState;
989
990 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000991 IdentifierLoc *IL = Attr.getArgAsIdent(0);
992 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
993 ReturnState)) {
994 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
995 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000996 return;
997 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000998 } else {
999 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1000 Attr.getName() << AANT_ArgumentIdentifier;
1001 return;
1002 }
1003
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001004 // FIXME: This check is currently being done in the analysis. It can be
1005 // enabled here only after the parser propagates attributes at
1006 // template specialization definition, not declaration.
1007 //QualType ReturnType;
1008 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001009 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1010 // ReturnType = Param->getType();
1011 //
1012 //} else if (const CXXConstructorDecl *Constructor =
1013 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001014 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1015 //
1016 //} else {
1017 //
1018 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1019 //}
1020 //
1021 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1022 //
1023 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1024 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1025 // ReturnType.getAsString();
1026 // return;
1027 //}
1028
1029 D->addAttr(::new (S.Context)
1030 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1031 Attr.getAttributeSpellingListIndex()));
1032}
1033
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001034
1035static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001036 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1037 return;
1038
1039 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001040 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001041 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1042 StringRef Param = Ident->Ident->getName();
1043 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1044 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1045 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001046 return;
1047 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001048 } else {
1049 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1050 Attr.getName() << AANT_ArgumentIdentifier;
1051 return;
1052 }
1053
1054 D->addAttr(::new (S.Context)
1055 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1056 Attr.getAttributeSpellingListIndex()));
1057}
1058
Chris Wailes9385f9f2013-10-29 20:28:41 +00001059static void handleTestTypestateAttr(Sema &S, Decl *D,
1060 const AttributeList &Attr) {
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001061 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1062 return;
1063
Chris Wailes9385f9f2013-10-29 20:28:41 +00001064 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001065 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001066 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1067 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001068 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001069 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1070 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001071 return;
1072 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001073 } else {
1074 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1075 Attr.getName() << AANT_ArgumentIdentifier;
1076 return;
1077 }
1078
1079 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001080 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001081 Attr.getAttributeSpellingListIndex()));
1082}
1083
Chandler Carruthedc2c642011-07-02 00:01:44 +00001084static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1085 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001086 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001087 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001088}
1089
Chandler Carruthedc2c642011-07-02 00:01:44 +00001090static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001091 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Aaron Ballman36a53502014-01-16 13:03:14 +00001092 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context,
1093 Attr.getAttributeSpellingListIndex()));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001094 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001095 // Report warning about changed offset in the newer compiler versions.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001096 if (!FD->getType()->isDependentType() &&
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001097 !FD->getType()->isIncompleteType() && FD->isBitField() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001098 S.Context.getTypeAlign(FD->getType()) <= 8)
Alexey Bataev830dfcc2015-12-03 09:34:49 +00001099 S.Diag(Attr.getLoc(), diag::warn_attribute_packed_for_bitfield);
1100
1101 FD->addAttr(::new (S.Context) PackedAttr(
1102 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001103 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001104 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001105}
1106
Ted Kremenek7fd17232011-09-29 07:02:25 +00001107static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1108 // The IBOutlet/IBOutletCollection attributes only apply to instance
1109 // variables or properties of Objective-C classes. The outlet must also
1110 // have an object reference type.
1111 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1112 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001113 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001114 << Attr.getName() << VD->getType() << 0;
1115 return false;
1116 }
1117 }
1118 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1119 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001120 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001121 << Attr.getName() << PD->getType() << 1;
1122 return false;
1123 }
1124 }
1125 else {
1126 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1127 return false;
1128 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001129
Ted Kremenek7fd17232011-09-29 07:02:25 +00001130 return true;
1131}
1132
Chandler Carruthedc2c642011-07-02 00:01:44 +00001133static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001134 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001135 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001136
Michael Han99315932013-01-24 16:46:58 +00001137 D->addAttr(::new (S.Context)
1138 IBOutletAttr(Attr.getRange(), S.Context,
1139 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001140}
1141
Chandler Carruthedc2c642011-07-02 00:01:44 +00001142static void handleIBOutletCollection(Sema &S, Decl *D,
1143 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001144
1145 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001146 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001147 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1148 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001149 return;
1150 }
1151
Ted Kremenek7fd17232011-09-29 07:02:25 +00001152 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001153 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001154
Richard Smithb1f9a282013-10-31 01:56:18 +00001155 ParsedType PT;
1156
1157 if (Attr.hasParsedType())
1158 PT = Attr.getTypeArg();
1159 else {
1160 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1161 S.getScopeForContext(D->getDeclContext()->getParent()));
1162 if (!PT) {
1163 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1164 return;
1165 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001166 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001167
Craig Topperc3ec1492014-05-26 06:22:03 +00001168 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001169 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1170 if (!QTLoc)
1171 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001172
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001173 // Diagnose use of non-object type in iboutletcollection attribute.
1174 // FIXME. Gnu attribute extension ignores use of builtin types in
1175 // attributes. So, __attribute__((iboutletcollection(char))) will be
1176 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001177 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001178 S.Diag(Attr.getLoc(),
1179 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1180 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001181 return;
1182 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001183
Michael Han99315932013-01-24 16:46:58 +00001184 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001185 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001186 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001187}
1188
Hal Finkelee90a222014-09-26 05:04:30 +00001189bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1190 if (RefOkay) {
1191 if (T->isReferenceType())
1192 return true;
1193 } else {
1194 T = T.getNonReferenceType();
1195 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001196
Hal Finkelee90a222014-09-26 05:04:30 +00001197 // The nonnull attribute, and other similar attributes, can be applied to a
1198 // transparent union that contains a pointer type.
Richard Smith588bd9b2014-08-27 04:59:42 +00001199 if (const RecordType *UT = T->getAsUnionType()) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001200 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1201 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001202 for (const auto *I : UD->fields()) {
1203 QualType QT = I->getType();
Richard Smith588bd9b2014-08-27 04:59:42 +00001204 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1205 return true;
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001206 }
1207 }
Richard Smith588bd9b2014-08-27 04:59:42 +00001208 }
1209
1210 return T->isAnyPointerType() || T->isBlockPointerType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001211}
1212
Ted Kremenek9aedc152014-01-17 06:24:56 +00001213static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001214 SourceRange AttrParmRange,
Hal Finkelee90a222014-09-26 05:04:30 +00001215 SourceRange TypeRange,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001216 bool isReturnValue = false) {
Hal Finkelee90a222014-09-26 05:04:30 +00001217 if (!S.isValidPointerAttrType(T)) {
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00001218 if (isReturnValue)
1219 S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only)
1220 << Attr.getName() << AttrParmRange << TypeRange;
1221 else
1222 S.Diag(Attr.getLoc(), diag::warn_attribute_pointers_only)
1223 << Attr.getName() << AttrParmRange << TypeRange << 0;
Ted Kremenek9aedc152014-01-17 06:24:56 +00001224 return false;
1225 }
1226 return true;
1227}
1228
Chandler Carruthedc2c642011-07-02 00:01:44 +00001229static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001230 SmallVector<unsigned, 8> NonNullArgs;
Richard Smith588bd9b2014-08-27 04:59:42 +00001231 for (unsigned I = 0; I < Attr.getNumArgs(); ++I) {
1232 Expr *Ex = Attr.getArgAsExpr(I);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001233 uint64_t Idx;
Richard Smith588bd9b2014-08-27 04:59:42 +00001234 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, I + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001235 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001236
1237 // Is the function argument a pointer type?
Richard Smith588bd9b2014-08-27 04:59:42 +00001238 if (Idx < getFunctionOrMethodNumParams(D) &&
1239 !attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr,
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001240 Ex->getSourceRange(),
1241 getFunctionOrMethodParamRange(D, Idx)))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001242 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001243
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001244 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001245 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001246
1247 // If no arguments were specified to __attribute__((nonnull)) then all pointer
Richard Smith588bd9b2014-08-27 04:59:42 +00001248 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1249 // check if the attribute came from a macro expansion or a template
1250 // instantiation.
1251 if (NonNullArgs.empty() && Attr.getLoc().isFileID() &&
1252 S.ActiveTemplateInstantiations.empty()) {
1253 bool AnyPointers = isFunctionOrMethodVariadic(D);
1254 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1255 I != E && !AnyPointers; ++I) {
1256 QualType T = getFunctionOrMethodParamType(D, I);
Hal Finkelee90a222014-09-26 05:04:30 +00001257 if (T->isDependentType() || S.isValidPointerAttrType(T))
Richard Smith588bd9b2014-08-27 04:59:42 +00001258 AnyPointers = true;
Ted Kremenek5fa50522008-11-18 06:52:58 +00001259 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001260
Richard Smith588bd9b2014-08-27 04:59:42 +00001261 if (!AnyPointers)
1262 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001263 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001264
Richard Smith588bd9b2014-08-27 04:59:42 +00001265 unsigned *Start = NonNullArgs.data();
1266 unsigned Size = NonNullArgs.size();
1267 llvm::array_pod_sort(Start, Start + Size);
Michael Han99315932013-01-24 16:46:58 +00001268 D->addAttr(::new (S.Context)
Richard Smith588bd9b2014-08-27 04:59:42 +00001269 NonNullAttr(Attr.getRange(), S.Context, Start, Size,
Michael Han99315932013-01-24 16:46:58 +00001270 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001271}
1272
Jordan Rosec9399072014-02-11 17:27:59 +00001273static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
1274 const AttributeList &Attr) {
1275 if (Attr.getNumArgs() > 0) {
1276 if (D->getFunctionType()) {
1277 handleNonNullAttr(S, D, Attr);
1278 } else {
1279 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1280 << D->getSourceRange();
1281 }
1282 return;
1283 }
1284
1285 // Is the argument a pointer type?
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001286 if (!attrNonNullArgCheck(S, D->getType(), Attr, SourceRange(),
1287 D->getSourceRange()))
Jordan Rosec9399072014-02-11 17:27:59 +00001288 return;
1289
1290 D->addAttr(::new (S.Context)
Craig Topperc3ec1492014-05-26 06:22:03 +00001291 NonNullAttr(Attr.getRange(), S.Context, nullptr, 0,
Jordan Rosec9399072014-02-11 17:27:59 +00001292 Attr.getAttributeSpellingListIndex()));
1293}
1294
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001295static void handleReturnsNonNullAttr(Sema &S, Decl *D,
1296 const AttributeList &Attr) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001297 QualType ResultType = getFunctionOrMethodResultType(D);
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001298 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1299 if (!attrNonNullArgCheck(S, ResultType, Attr, SourceRange(), SR,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001300 /* isReturnValue */ true))
1301 return;
1302
1303 D->addAttr(::new (S.Context)
1304 ReturnsNonNullAttr(Attr.getRange(), S.Context,
1305 Attr.getAttributeSpellingListIndex()));
1306}
1307
Hal Finkelee90a222014-09-26 05:04:30 +00001308static void handleAssumeAlignedAttr(Sema &S, Decl *D,
1309 const AttributeList &Attr) {
1310 Expr *E = Attr.getArgAsExpr(0),
1311 *OE = Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr;
1312 S.AddAssumeAlignedAttr(Attr.getRange(), D, E, OE,
1313 Attr.getAttributeSpellingListIndex());
1314}
1315
1316void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
1317 Expr *OE, unsigned SpellingListIndex) {
1318 QualType ResultType = getFunctionOrMethodResultType(D);
1319 SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1320
1321 AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex);
1322 SourceLocation AttrLoc = AttrRange.getBegin();
1323
1324 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1325 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1326 << &TmpAttr << AttrRange << SR;
1327 return;
1328 }
1329
1330 if (!E->isValueDependent()) {
1331 llvm::APSInt I(64);
1332 if (!E->isIntegerConstantExpr(I, Context)) {
1333 if (OE)
1334 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1335 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1336 << E->getSourceRange();
1337 else
1338 Diag(AttrLoc, diag::err_attribute_argument_type)
1339 << &TmpAttr << AANT_ArgumentIntegerConstant
1340 << E->getSourceRange();
1341 return;
1342 }
1343
1344 if (!I.isPowerOf2()) {
1345 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1346 << E->getSourceRange();
1347 return;
1348 }
1349 }
1350
1351 if (OE) {
1352 if (!OE->isValueDependent()) {
1353 llvm::APSInt I(64);
1354 if (!OE->isIntegerConstantExpr(I, Context)) {
1355 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1356 << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1357 << OE->getSourceRange();
1358 return;
1359 }
1360 }
1361 }
1362
1363 D->addAttr(::new (Context)
1364 AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex));
1365}
1366
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001367/// Normalize the attribute, __foo__ becomes foo.
1368/// Returns true if normalization was applied.
1369static bool normalizeName(StringRef &AttrName) {
Aaron Ballman62692362015-10-09 13:53:24 +00001370 if (AttrName.size() > 4 && AttrName.startswith("__") &&
1371 AttrName.endswith("__")) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001372 AttrName = AttrName.drop_front(2).drop_back(2);
1373 return true;
1374 }
1375 return false;
1376}
1377
Chandler Carruthedc2c642011-07-02 00:01:44 +00001378static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001379 // This attribute must be applied to a function declaration. The first
1380 // argument to the attribute must be an identifier, the name of the resource,
1381 // for example: malloc. The following arguments must be argument indexes, the
1382 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001383 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001384 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001385 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001386
Aaron Ballman00e99962013-08-31 01:11:41 +00001387 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001388 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001389 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001390 return;
1391 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001392
Richard Smith852e9ce2013-11-27 01:46:48 +00001393 // Figure out our Kind.
1394 OwnershipAttr::OwnershipKind K =
Craig Topperc3ec1492014-05-26 06:22:03 +00001395 OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0,
Richard Smith852e9ce2013-11-27 01:46:48 +00001396 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001397
Richard Smith852e9ce2013-11-27 01:46:48 +00001398 // Check arguments.
1399 switch (K) {
1400 case OwnershipAttr::Takes:
1401 case OwnershipAttr::Holds:
1402 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001403 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1404 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001405 return;
1406 }
1407 break;
1408 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001409 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001410 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1411 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001412 return;
1413 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001414 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001415 }
1416
Richard Smith852e9ce2013-11-27 01:46:48 +00001417 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001418
Richard Smith852e9ce2013-11-27 01:46:48 +00001419 StringRef ModuleName = Module->getName();
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00001420 if (normalizeName(ModuleName)) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001421 Module = &S.PP.getIdentifierTable().get(ModuleName);
1422 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001423
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001424 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001425 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1426 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001427 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001428 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001429 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001430
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001431 // Is the function argument a pointer type?
Alp Toker601b22c2014-01-21 23:35:24 +00001432 QualType T = getFunctionOrMethodParamType(D, Idx);
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001433 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001434 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001435 case OwnershipAttr::Takes:
1436 case OwnershipAttr::Holds:
1437 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1438 Err = 0;
1439 break;
1440 case OwnershipAttr::Returns:
1441 if (!T->isIntegerType())
1442 Err = 1;
1443 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001444 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001445 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001446 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001447 << Ex->getSourceRange();
1448 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001449 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001450
1451 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001452 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001453 // Cannot have two ownership attributes of different kinds for the same
1454 // index.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001455 if (I->getOwnKind() != K && I->args_end() !=
1456 std::find(I->args_begin(), I->args_end(), Idx)) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001457 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001458 << AL.getName() << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001459 return;
Aaron Ballmanef7aef82014-07-31 20:44:26 +00001460 } else if (K == OwnershipAttr::Returns &&
1461 I->getOwnKind() == OwnershipAttr::Returns) {
1462 // A returns attribute conflicts with any other returns attribute using
1463 // a different index. Note, diagnostic reporting is 1-based, but stored
1464 // argument indexes are 0-based.
1465 if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1466 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
1467 << *(I->args_begin()) + 1;
1468 if (I->args_size())
1469 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
1470 << (unsigned)Idx + 1 << Ex->getSourceRange();
1471 return;
1472 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001473 }
1474 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001475 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001476 }
1477
1478 unsigned* start = OwnershipArgs.data();
1479 unsigned size = OwnershipArgs.size();
1480 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001481
Michael Han99315932013-01-24 16:46:58 +00001482 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001483 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001484 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001485}
1486
Chandler Carruthedc2c642011-07-02 00:01:44 +00001487static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001488 // Check the attribute arguments.
1489 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001490 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1491 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001492 return;
1493 }
1494
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001495 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001496
Rafael Espindolac18086a2010-02-23 22:00:30 +00001497 // gcc rejects
1498 // class c {
1499 // static int a __attribute__((weakref ("v2")));
1500 // static int b() __attribute__((weakref ("f3")));
1501 // };
1502 // and ignores the attributes of
1503 // void f(void) {
1504 // static int a __attribute__((weakref ("v2")));
1505 // }
1506 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001507 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001508 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001509 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1510 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001511 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001512 }
1513
1514 // The GCC manual says
1515 //
1516 // At present, a declaration to which `weakref' is attached can only
1517 // be `static'.
1518 //
1519 // It also says
1520 //
1521 // Without a TARGET,
1522 // given as an argument to `weakref' or to `alias', `weakref' is
1523 // equivalent to `weak'.
1524 //
1525 // gcc 4.4.1 will accept
1526 // int a7 __attribute__((weakref));
1527 // as
1528 // int a7 __attribute__((weak));
1529 // This looks like a bug in gcc. We reject that for now. We should revisit
1530 // it if this behaviour is actually used.
1531
Rafael Espindolac18086a2010-02-23 22:00:30 +00001532 // GCC rejects
1533 // static ((alias ("y"), weakref)).
1534 // Should we? How to check that weakref is before or after alias?
1535
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001536 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1537 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1538 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001539 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001540 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001541 // GCC will accept anything as the argument of weakref. Should we
1542 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001543 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1544 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001545
Michael Han99315932013-01-24 16:46:58 +00001546 D->addAttr(::new (S.Context)
1547 WeakRefAttr(Attr.getRange(), S.Context,
1548 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001549}
1550
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001551static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1552 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001553 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001554 return;
1555
Douglas Gregore8bbc122011-09-02 00:18:52 +00001556 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001557 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1558 return;
1559 }
1560
David Majnemer2dc81462015-01-19 09:00:28 +00001561 // Aliases should be on declarations, not definitions.
1562 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1563 if (FD->isThisDeclarationADefinition()) {
1564 S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << FD;
1565 return;
1566 }
1567 } else {
1568 const auto *VD = cast<VarDecl>(D);
1569 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
1570 S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << VD;
1571 return;
1572 }
1573 }
1574
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001575 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001576
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001577 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001578 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001579}
1580
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001581static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001582 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr.getRange(), Attr.getName()))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001583 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001584
Michael Han99315932013-01-24 16:46:58 +00001585 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1586 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001587}
1588
1589static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001590 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr.getRange(), Attr.getName()))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001591 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001592
Michael Han99315932013-01-24 16:46:58 +00001593 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1594 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001595}
1596
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001597static void handleTLSModelAttr(Sema &S, Decl *D,
1598 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001599 StringRef Model;
1600 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001601 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001602 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001603 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001604
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001605 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001606 if (Model != "global-dynamic" && Model != "local-dynamic"
1607 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001608 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001609 return;
1610 }
1611
Michael Han99315932013-01-24 16:46:58 +00001612 D->addAttr(::new (S.Context)
1613 TLSModelAttr(Attr.getRange(), S.Context, Model,
1614 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001615}
1616
David Majnemer631a90b2015-02-04 07:23:21 +00001617static void handleRestrictAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1618 QualType ResultType = getFunctionOrMethodResultType(D);
1619 if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
1620 D->addAttr(::new (S.Context) RestrictAttr(
1621 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1622 return;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001623 }
1624
David Majnemer631a90b2015-02-04 07:23:21 +00001625 S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only)
1626 << Attr.getName() << getFunctionOrMethodResultSourceRange(D);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001627}
1628
Chandler Carruthedc2c642011-07-02 00:01:44 +00001629static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001630 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001631 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001632 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001633 return;
1634 }
1635
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001636 if (CommonAttr *CA = S.mergeCommonAttr(D, Attr.getRange(), Attr.getName(),
1637 Attr.getAttributeSpellingListIndex()))
1638 D->addAttr(CA);
Eric Christopher8a2ee392010-12-02 02:45:55 +00001639}
1640
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00001641static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1642 if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, Attr.getRange(),
1643 Attr.getName()))
1644 return;
1645
1646 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context,
1647 Attr.getAttributeSpellingListIndex()));
1648}
1649
Chandler Carruthedc2c642011-07-02 00:01:44 +00001650static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001651 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001652
1653 if (S.CheckNoReturnAttr(attr)) return;
1654
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001655 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001656 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001657 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001658 return;
1659 }
1660
Michael Han99315932013-01-24 16:46:58 +00001661 D->addAttr(::new (S.Context)
1662 NoReturnAttr(attr.getRange(), S.Context,
1663 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001664}
1665
1666bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001667 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001668 attr.setInvalid();
1669 return true;
1670 }
1671
1672 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001673}
1674
Chandler Carruthedc2c642011-07-02 00:01:44 +00001675static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1676 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001677
1678 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1679 // because 'analyzer_noreturn' does not impact the type.
David Majnemer06864812015-04-07 06:01:53 +00001680 if (!isFunctionOrMethodOrBlock(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001681 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00001682 if (!VD || (!VD->getType()->isBlockPointerType() &&
1683 !VD->getType()->isFunctionPointerType())) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001684 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001685 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Craig Topper8f7f3ea2015-11-17 05:40:05 +00001686 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001687 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001688 return;
1689 }
1690 }
1691
Michael Han99315932013-01-24 16:46:58 +00001692 D->addAttr(::new (S.Context)
1693 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1694 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001695}
1696
John Thompsoncdb847ba2010-08-09 21:53:52 +00001697// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001698static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001699/*
1700 Returning a Vector Class in Registers
1701
Eric Christopherbc638a82010-12-01 22:13:54 +00001702 According to the PPU ABI specifications, a class with a single member of
1703 vector type is returned in memory when used as the return value of a function.
1704 This results in inefficient code when implementing vector classes. To return
1705 the value in a single vector register, add the vecreturn attribute to the
1706 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001707
1708 Example:
1709
1710 struct Vector
1711 {
1712 __vector float xyzw;
1713 } __attribute__((vecreturn));
1714
1715 Vector Add(Vector lhs, Vector rhs)
1716 {
1717 Vector result;
1718 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1719 return result; // This will be returned in a register
1720 }
1721*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001722 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1723 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001724 return;
1725 }
1726
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001727 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001728 int count = 0;
1729
1730 if (!isa<CXXRecordDecl>(record)) {
1731 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1732 return;
1733 }
1734
1735 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1736 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1737 return;
1738 }
1739
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001740 for (const auto *I : record->fields()) {
1741 if ((count == 1) || !I->getType()->isVectorType()) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001742 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1743 return;
1744 }
1745 count++;
1746 }
1747
Michael Han99315932013-01-24 16:46:58 +00001748 D->addAttr(::new (S.Context)
1749 VecReturnAttr(Attr.getRange(), S.Context,
1750 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001751}
1752
Richard Smithe233fbf2013-01-28 22:42:45 +00001753static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1754 const AttributeList &Attr) {
1755 if (isa<ParmVarDecl>(D)) {
1756 // [[carries_dependency]] can only be applied to a parameter if it is a
1757 // parameter of a function declaration or lambda.
1758 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1759 S.Diag(Attr.getLoc(),
1760 diag::err_carries_dependency_param_not_function_decl);
1761 return;
1762 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001763 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001764
1765 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1766 Attr.getRange(), S.Context,
1767 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001768}
1769
Akira Hatanakac8667622015-11-06 23:56:15 +00001770static void handleNotTailCalledAttr(Sema &S, Decl *D,
1771 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001772 if (checkAttrMutualExclusion<AlwaysInlineAttr>(S, D, Attr.getRange(),
1773 Attr.getName()))
Akira Hatanakac8667622015-11-06 23:56:15 +00001774 return;
1775
1776 D->addAttr(::new (S.Context) NotTailCalledAttr(
1777 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1778}
1779
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00001780static void handleDisableTailCallsAttr(Sema &S, Decl *D,
1781 const AttributeList &Attr) {
1782 if (checkAttrMutualExclusion<NakedAttr>(S, D, Attr.getRange(),
1783 Attr.getName()))
1784 return;
1785
1786 D->addAttr(::new (S.Context) DisableTailCallsAttr(
1787 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
1788}
1789
Chandler Carruthedc2c642011-07-02 00:01:44 +00001790static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001791 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001792 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001793 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001794 return;
1795 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001796 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001797 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001798 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001799 return;
1800 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001801
Michael Han99315932013-01-24 16:46:58 +00001802 D->addAttr(::new (S.Context)
1803 UsedAttr(Attr.getRange(), S.Context,
1804 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001805}
1806
Chandler Carruthedc2c642011-07-02 00:01:44 +00001807static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001808 uint32_t priority = ConstructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001809 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001810 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1811 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001812
Michael Han99315932013-01-24 16:46:58 +00001813 D->addAttr(::new (S.Context)
1814 ConstructorAttr(Attr.getRange(), S.Context, priority,
1815 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001816}
1817
Chandler Carruthedc2c642011-07-02 00:01:44 +00001818static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanf28e4992014-01-20 15:22:57 +00001819 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001820 if (Attr.getNumArgs() &&
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001821 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1822 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001823
Michael Han99315932013-01-24 16:46:58 +00001824 D->addAttr(::new (S.Context)
1825 DestructorAttr(Attr.getRange(), S.Context, priority,
1826 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001827}
1828
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001829template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001830static void handleAttrWithMessage(Sema &S, Decl *D,
1831 const AttributeList &Attr) {
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001832 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001833 StringRef Str;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00001834 if (Attr.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001835 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001836
Michael Han99315932013-01-24 16:46:58 +00001837 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1838 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001839}
1840
Ted Kremenek438f8db2014-02-22 01:06:05 +00001841static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Ted Kremenek28eace62013-11-23 01:01:34 +00001842 const AttributeList &Attr) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00001843 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Ted Kremenek27cfe102014-02-21 22:49:04 +00001844 S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition)
1845 << Attr.getName() << Attr.getRange();
1846 return;
1847 }
1848
Ted Kremenek28eace62013-11-23 01:01:34 +00001849 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001850 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1851 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001852}
1853
Jordy Rose740b0c22012-05-08 03:27:22 +00001854static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1855 IdentifierInfo *Platform,
1856 VersionTuple Introduced,
1857 VersionTuple Deprecated,
1858 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001859 StringRef PlatformName
1860 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1861 if (PlatformName.empty())
1862 PlatformName = Platform->getName();
1863
1864 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1865 // of these steps are needed).
1866 if (!Introduced.empty() && !Deprecated.empty() &&
1867 !(Introduced <= Deprecated)) {
1868 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1869 << 1 << PlatformName << Deprecated.getAsString()
1870 << 0 << Introduced.getAsString();
1871 return true;
1872 }
1873
1874 if (!Introduced.empty() && !Obsoleted.empty() &&
1875 !(Introduced <= Obsoleted)) {
1876 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1877 << 2 << PlatformName << Obsoleted.getAsString()
1878 << 0 << Introduced.getAsString();
1879 return true;
1880 }
1881
1882 if (!Deprecated.empty() && !Obsoleted.empty() &&
1883 !(Deprecated <= Obsoleted)) {
1884 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1885 << 2 << PlatformName << Obsoleted.getAsString()
1886 << 1 << Deprecated.getAsString();
1887 return true;
1888 }
1889
1890 return false;
1891}
1892
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001893/// \brief Check whether the two versions match.
1894///
1895/// If either version tuple is empty, then they are assumed to match. If
1896/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1897static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1898 bool BeforeIsOkay) {
1899 if (X.empty() || Y.empty())
1900 return true;
1901
1902 if (X == Y)
1903 return true;
1904
1905 if (BeforeIsOkay && X < Y)
1906 return true;
1907
1908 return false;
1909}
1910
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001911AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001912 IdentifierInfo *Platform,
1913 VersionTuple Introduced,
1914 VersionTuple Deprecated,
1915 VersionTuple Obsoleted,
1916 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001917 StringRef Message,
Douglas Gregord2a713e2015-09-30 21:27:42 +00001918 AvailabilityMergeKind AMK,
Michael Han99315932013-01-24 16:46:58 +00001919 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001920 VersionTuple MergedIntroduced = Introduced;
1921 VersionTuple MergedDeprecated = Deprecated;
1922 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001923 bool FoundAny = false;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001924 bool OverrideOrImpl = false;
1925 switch (AMK) {
1926 case AMK_None:
1927 case AMK_Redeclaration:
1928 OverrideOrImpl = false;
1929 break;
1930
1931 case AMK_Override:
1932 case AMK_ProtocolImplementation:
1933 OverrideOrImpl = true;
1934 break;
1935 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001936
Rafael Espindolac67f2232012-05-10 02:50:16 +00001937 if (D->hasAttrs()) {
1938 AttrVec &Attrs = D->getAttrs();
1939 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1940 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1941 if (!OldAA) {
1942 ++i;
1943 continue;
1944 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001945
Rafael Espindolac67f2232012-05-10 02:50:16 +00001946 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1947 if (OldPlatform != Platform) {
1948 ++i;
1949 continue;
1950 }
1951
Tim Northover7a73cc72015-10-30 16:30:49 +00001952 // If there is an existing availability attribute for this platform that
1953 // is explicit and the new one is implicit use the explicit one and
1954 // discard the new implicit attribute.
1955 if (OldAA->getRange().isValid() && Range.isInvalid()) {
1956 return nullptr;
1957 }
1958
1959 // If there is an existing attribute for this platform that is implicit
1960 // and the new attribute is explicit then erase the old one and
1961 // continue processing the attributes.
1962 if (Range.isValid() && OldAA->getRange().isInvalid()) {
1963 Attrs.erase(Attrs.begin() + i);
1964 --e;
1965 continue;
1966 }
1967
Rafael Espindolac67f2232012-05-10 02:50:16 +00001968 FoundAny = true;
1969 VersionTuple OldIntroduced = OldAA->getIntroduced();
1970 VersionTuple OldDeprecated = OldAA->getDeprecated();
1971 VersionTuple OldObsoleted = OldAA->getObsoleted();
1972 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001973
Douglas Gregord2a713e2015-09-30 21:27:42 +00001974 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
1975 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
1976 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001977 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregord2a713e2015-09-30 21:27:42 +00001978 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
1979 if (OverrideOrImpl) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001980 int Which = -1;
1981 VersionTuple FirstVersion;
1982 VersionTuple SecondVersion;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001983 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001984 Which = 0;
1985 FirstVersion = OldIntroduced;
1986 SecondVersion = Introduced;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001987 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001988 Which = 1;
1989 FirstVersion = Deprecated;
1990 SecondVersion = OldDeprecated;
Douglas Gregord2a713e2015-09-30 21:27:42 +00001991 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001992 Which = 2;
1993 FirstVersion = Obsoleted;
1994 SecondVersion = OldObsoleted;
1995 }
1996
1997 if (Which == -1) {
1998 Diag(OldAA->getLocation(),
1999 diag::warn_mismatched_availability_override_unavail)
Douglas Gregord2a713e2015-09-30 21:27:42 +00002000 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2001 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002002 } else {
2003 Diag(OldAA->getLocation(),
2004 diag::warn_mismatched_availability_override)
2005 << Which
2006 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
Douglas Gregord2a713e2015-09-30 21:27:42 +00002007 << FirstVersion.getAsString() << SecondVersion.getAsString()
2008 << (AMK == AMK_Override);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002009 }
Douglas Gregord2a713e2015-09-30 21:27:42 +00002010 if (AMK == AMK_Override)
2011 Diag(Range.getBegin(), diag::note_overridden_method);
2012 else
2013 Diag(Range.getBegin(), diag::note_protocol_method);
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002014 } else {
2015 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2016 Diag(Range.getBegin(), diag::note_previous_attribute);
2017 }
2018
Rafael Espindolac67f2232012-05-10 02:50:16 +00002019 Attrs.erase(Attrs.begin() + i);
2020 --e;
2021 continue;
2022 }
2023
2024 VersionTuple MergedIntroduced2 = MergedIntroduced;
2025 VersionTuple MergedDeprecated2 = MergedDeprecated;
2026 VersionTuple MergedObsoleted2 = MergedObsoleted;
2027
2028 if (MergedIntroduced2.empty())
2029 MergedIntroduced2 = OldIntroduced;
2030 if (MergedDeprecated2.empty())
2031 MergedDeprecated2 = OldDeprecated;
2032 if (MergedObsoleted2.empty())
2033 MergedObsoleted2 = OldObsoleted;
2034
2035 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2036 MergedIntroduced2, MergedDeprecated2,
2037 MergedObsoleted2)) {
2038 Attrs.erase(Attrs.begin() + i);
2039 --e;
2040 continue;
2041 }
2042
2043 MergedIntroduced = MergedIntroduced2;
2044 MergedDeprecated = MergedDeprecated2;
2045 MergedObsoleted = MergedObsoleted2;
2046 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002047 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002048 }
2049
2050 if (FoundAny &&
2051 MergedIntroduced == Introduced &&
2052 MergedDeprecated == Deprecated &&
2053 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00002054 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002055
Douglas Gregord2a713e2015-09-30 21:27:42 +00002056 // Only create a new attribute if !OverrideOrImpl, but we want to do
Ted Kremenekb5445722013-04-06 00:34:27 +00002057 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002058 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002059 MergedDeprecated, MergedObsoleted) &&
Douglas Gregord2a713e2015-09-30 21:27:42 +00002060 !OverrideOrImpl) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002061 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2062 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002063 Obsoleted, IsUnavailable, Message,
2064 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002065 }
Craig Topperc3ec1492014-05-26 06:22:03 +00002066 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002067}
2068
Chandler Carruthedc2c642011-07-02 00:01:44 +00002069static void handleAvailabilityAttr(Sema &S, Decl *D,
2070 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002071 if (!checkAttributeNumArgs(S, Attr, 1))
2072 return;
2073 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00002074 unsigned Index = Attr.getAttributeSpellingListIndex();
2075
Aaron Ballman00e99962013-08-31 01:11:41 +00002076 IdentifierInfo *II = Platform->Ident;
2077 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2078 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2079 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002080
Rafael Espindolac231fab2013-01-08 21:30:32 +00002081 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2082 if (!ND) {
2083 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2084 return;
2085 }
2086
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002087 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2088 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2089 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002090 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002091 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00002092 if (const StringLiteral *SE =
2093 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002094 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002095
Aaron Ballman00e99962013-08-31 01:11:41 +00002096 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002097 Introduced.Version,
2098 Deprecated.Version,
2099 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002100 IsUnavailable, Str,
Douglas Gregord2a713e2015-09-30 21:27:42 +00002101 Sema::AMK_None,
Michael Han99315932013-01-24 16:46:58 +00002102 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002103 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002104 D->addAttr(NewAttr);
Tim Northover7a73cc72015-10-30 16:30:49 +00002105
2106 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2107 // matches before the start of the watchOS platform.
2108 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2109 IdentifierInfo *NewII = nullptr;
2110 if (II->getName() == "ios")
2111 NewII = &S.Context.Idents.get("watchos");
2112 else if (II->getName() == "ios_app_extension")
2113 NewII = &S.Context.Idents.get("watchos_app_extension");
2114
2115 if (NewII) {
2116 auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple {
2117 if (Version.empty())
2118 return Version;
2119 auto Major = Version.getMajor();
2120 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2121 if (NewMajor >= 2) {
2122 if (Version.getMinor().hasValue()) {
2123 if (Version.getSubminor().hasValue())
2124 return VersionTuple(NewMajor, Version.getMinor().getValue(),
2125 Version.getSubminor().getValue());
2126 else
2127 return VersionTuple(NewMajor, Version.getMinor().getValue());
2128 }
2129 }
2130
2131 return VersionTuple(2, 0);
2132 };
2133
2134 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2135 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2136 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2137
2138 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
2139 SourceRange(),
2140 NewII,
2141 NewIntroduced,
2142 NewDeprecated,
2143 NewObsoleted,
2144 IsUnavailable, Str,
2145 Sema::AMK_None,
2146 Index);
2147 if (NewAttr)
2148 D->addAttr(NewAttr);
2149 }
2150 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2151 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2152 // matches before the start of the tvOS platform.
2153 IdentifierInfo *NewII = nullptr;
2154 if (II->getName() == "ios")
2155 NewII = &S.Context.Idents.get("tvos");
2156 else if (II->getName() == "ios_app_extension")
2157 NewII = &S.Context.Idents.get("tvos_app_extension");
2158
2159 if (NewII) {
2160 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND,
2161 SourceRange(),
2162 NewII,
2163 Introduced.Version,
2164 Deprecated.Version,
2165 Obsoleted.Version,
2166 IsUnavailable, Str,
2167 Sema::AMK_None,
2168 Index);
2169 if (NewAttr)
2170 D->addAttr(NewAttr);
2171 }
2172 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002173}
2174
John McCalld041a9b2013-02-20 01:54:26 +00002175template <class T>
2176static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2177 typename T::VisibilityType value,
2178 unsigned attrSpellingListIndex) {
2179 T *existingAttr = D->getAttr<T>();
2180 if (existingAttr) {
2181 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2182 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00002183 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00002184 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2185 S.Diag(range.getBegin(), diag::note_previous_attribute);
2186 D->dropAttr<T>();
2187 }
2188 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2189}
2190
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002191VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002192 VisibilityAttr::VisibilityType Vis,
2193 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002194 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2195 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002196}
2197
John McCalld041a9b2013-02-20 01:54:26 +00002198TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2199 TypeVisibilityAttr::VisibilityType Vis,
2200 unsigned AttrSpellingListIndex) {
2201 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2202 AttrSpellingListIndex);
2203}
2204
2205static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2206 bool isTypeVisibility) {
2207 // Visibility attributes don't mean anything on a typedef.
2208 if (isa<TypedefNameDecl>(D)) {
2209 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2210 << Attr.getName();
2211 return;
2212 }
2213
2214 // 'type_visibility' can only go on a type or namespace.
2215 if (isTypeVisibility &&
2216 !(isa<TagDecl>(D) ||
2217 isa<ObjCInterfaceDecl>(D) ||
2218 isa<NamespaceDecl>(D))) {
2219 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2220 << Attr.getName() << ExpectedTypeOrNamespace;
2221 return;
2222 }
2223
Benjamin Kramer70370212013-09-09 15:08:57 +00002224 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002225 StringRef TypeStr;
2226 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002227 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002228 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002229
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002230 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002231 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002232 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002233 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002234 return;
2235 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002236
2237 // Complain about attempts to use protected visibility on targets
2238 // (like Darwin) that don't support it.
2239 if (type == VisibilityAttr::Protected &&
2240 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2241 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2242 type = VisibilityAttr::Default;
2243 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002244
Michael Han99315932013-01-24 16:46:58 +00002245 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002246 clang::Attr *newAttr;
2247 if (isTypeVisibility) {
2248 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2249 (TypeVisibilityAttr::VisibilityType) type,
2250 Index);
2251 } else {
2252 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2253 }
2254 if (newAttr)
2255 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002256}
2257
Chandler Carruthedc2c642011-07-02 00:01:44 +00002258static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2259 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002260 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002261 if (!Attr.isArgIdent(0)) {
2262 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2263 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002264 return;
2265 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002266
Aaron Ballman682ee422013-09-11 19:47:58 +00002267 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2268 ObjCMethodFamilyAttr::FamilyKind F;
2269 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2270 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2271 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002272 return;
2273 }
2274
Alp Toker314cc812014-01-25 16:55:45 +00002275 if (F == ObjCMethodFamilyAttr::OMF_init &&
2276 !method->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00002277 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
Alp Toker314cc812014-01-25 16:55:45 +00002278 << method->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00002279 // Ignore the attribute.
2280 return;
2281 }
2282
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002283 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00002284 S.Context, F,
2285 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00002286}
2287
Chandler Carruthedc2c642011-07-02 00:01:44 +00002288static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002289 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002290 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002291 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002292 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2293 return;
2294 }
2295 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002296 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2297 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002298 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002299 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2300 return;
2301 }
2302 }
2303 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002304 // It is okay to include this attribute on properties, e.g.:
2305 //
2306 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2307 //
2308 // In this case it follows tradition and suppresses an error in the above
2309 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002310 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002311 }
Michael Han99315932013-01-24 16:46:58 +00002312 D->addAttr(::new (S.Context)
2313 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2314 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002315}
2316
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00002317static void handleObjCIndependentClass(Sema &S, Decl *D, const AttributeList &Attr) {
2318 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
2319 QualType T = TD->getUnderlyingType();
2320 if (!T->isObjCObjectPointerType()) {
2321 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2322 return;
2323 }
2324 } else {
2325 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2326 return;
2327 }
2328 D->addAttr(::new (S.Context)
2329 ObjCIndependentClassAttr(Attr.getRange(), S.Context,
2330 Attr.getAttributeSpellingListIndex()));
2331}
2332
Chandler Carruthedc2c642011-07-02 00:01:44 +00002333static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002334 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002335 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002336 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002337 return;
2338 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002339
Aaron Ballman00e99962013-08-31 01:11:41 +00002340 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002341 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002342 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2343 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2344 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002345 return;
2346 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002347
Michael Han99315932013-01-24 16:46:58 +00002348 D->addAttr(::new (S.Context)
2349 BlocksAttr(Attr.getRange(), S.Context, type,
2350 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002351}
2352
Chandler Carruthedc2c642011-07-02 00:01:44 +00002353static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman18a78382013-11-21 00:28:23 +00002354 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002355 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002356 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002357 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002358 if (E->isTypeDependent() || E->isValueDependent() ||
2359 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002360 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002361 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002362 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002363 return;
2364 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002365
John McCallb46f2872011-09-09 07:56:05 +00002366 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002367 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2368 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002369 return;
2370 }
John McCallb46f2872011-09-09 07:56:05 +00002371
2372 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002373 }
2374
Aaron Ballman18a78382013-11-21 00:28:23 +00002375 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002376 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002377 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002378 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002379 if (E->isTypeDependent() || E->isValueDependent() ||
2380 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002381 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002382 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002383 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002384 return;
2385 }
2386 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002387
John McCallb46f2872011-09-09 07:56:05 +00002388 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002389 // FIXME: This error message could be improved, it would be nice
2390 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002391 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2392 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002393 return;
2394 }
2395 }
2396
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002397 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002398 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002399 if (isa<FunctionNoProtoType>(FT)) {
2400 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2401 return;
2402 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002403
Chris Lattner9363e312009-03-17 23:03:47 +00002404 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002405 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002406 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002407 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002408 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002409 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002410 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002411 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002412 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002413 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2414 if (!BD->isVariadic()) {
2415 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2416 return;
2417 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002418 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002419 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002420 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002421 const FunctionType *FT = Ty->isFunctionPointerType()
2422 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002423 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002424 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002425 int m = Ty->isFunctionPointerType() ? 0 : 1;
2426 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002427 return;
2428 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002429 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002430 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002431 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002432 return;
2433 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002434 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002435 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002436 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002437 return;
2438 }
Michael Han99315932013-01-24 16:46:58 +00002439 D->addAttr(::new (S.Context)
2440 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2441 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002442}
2443
Chandler Carruthedc2c642011-07-02 00:01:44 +00002444static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Alp Toker314cc812014-01-25 16:55:45 +00002445 if (D->getFunctionType() &&
2446 D->getFunctionType()->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002447 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2448 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002449 return;
2450 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002451 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002452 if (MD->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002453 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2454 << Attr.getName() << 1;
2455 return;
2456 }
2457
Michael Han99315932013-01-24 16:46:58 +00002458 D->addAttr(::new (S.Context)
2459 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2460 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002461}
2462
Chandler Carruthedc2c642011-07-02 00:01:44 +00002463static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002464 // weak_import only applies to variable & function declarations.
2465 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002466 if (!D->canBeWeakImported(isDef)) {
2467 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002468 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2469 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002470 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002471 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002472 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002473 // Nothing to warn about here.
2474 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002475 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002476 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002477
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002478 return;
2479 }
2480
Michael Han99315932013-01-24 16:46:58 +00002481 D->addAttr(::new (S.Context)
2482 WeakImportAttr(Attr.getRange(), S.Context,
2483 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002484}
2485
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002486// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002487template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002488static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002489 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002490 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002491 for (unsigned i = 0; i < 3; ++i) {
2492 const Expr *E = Attr.getArgAsExpr(i);
2493 if (!checkUInt32Argument(S, Attr, E, WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002494 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002495 if (WGSize[i] == 0) {
2496 S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero)
2497 << Attr.getName() << E->getSourceRange();
2498 return;
2499 }
2500 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002501
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002502 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2503 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2504 Existing->getYDim() == WGSize[1] &&
2505 Existing->getZDim() == WGSize[2]))
2506 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002507
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002508 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2509 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002510 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002511}
2512
Joey Goulyaba589c2013-03-08 09:42:32 +00002513static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002514 if (!Attr.hasParsedType()) {
2515 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2516 << Attr.getName() << 1;
2517 return;
2518 }
2519
Craig Topperc3ec1492014-05-26 06:22:03 +00002520 TypeSourceInfo *ParmTSI = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00002521 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2522 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002523
2524 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2525 (ParmType->isBooleanType() ||
2526 !ParmType->isIntegralType(S.getASTContext()))) {
2527 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2528 << ParmType;
2529 return;
2530 }
2531
Aaron Ballmana9e05402013-12-02 22:16:55 +00002532 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002533 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002534 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2535 return;
2536 }
2537 }
2538
2539 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002540 ParmTSI,
2541 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002542}
2543
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002544SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002545 StringRef Name,
2546 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002547 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2548 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002549 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002550 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2551 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002552 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002553 }
Michael Han99315932013-01-24 16:46:58 +00002554 return ::new (Context) SectionAttr(Range, Context, Name,
2555 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002556}
2557
Reid Kleckner2a133222015-03-04 23:39:17 +00002558bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2559 std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2560 if (!Error.empty()) {
2561 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error;
2562 return false;
2563 }
2564 return true;
2565}
2566
Chandler Carruthedc2c642011-07-02 00:01:44 +00002567static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002568 // Make sure that there is a string literal as the sections's single
2569 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002570 StringRef Str;
2571 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002572 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002573 return;
Mike Stump11289f42009-09-09 15:08:12 +00002574
Reid Kleckner2a133222015-03-04 23:39:17 +00002575 if (!S.checkSectionName(LiteralLoc, Str))
2576 return;
2577
Chris Lattner30ba6742009-08-10 19:03:04 +00002578 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002579 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002580 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002581 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002582 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002583 return;
2584 }
Mike Stump11289f42009-09-09 15:08:12 +00002585
Michael Han99315932013-01-24 16:46:58 +00002586 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002587 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002588 if (NewAttr)
2589 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002590}
2591
Eric Christopher789a7ad2015-06-12 01:36:05 +00002592// Check for things we'd like to warn about, no errors or validation for now.
2593// TODO: Validation should use a backend target library that specifies
2594// the allowable subtarget features and cpus. We could use something like a
2595// TargetCodeGenInfo hook here to do validation.
2596void Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
2597 for (auto Str : {"tune=", "fpmath="})
2598 if (AttrStr.find(Str) != StringRef::npos)
2599 Diag(LiteralLoc, diag::warn_unsupported_target_attribute) << Str;
2600}
2601
Eric Christopher11acf732015-06-12 01:35:52 +00002602static void handleTargetAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eric Christopher11acf732015-06-12 01:35:52 +00002603 StringRef Str;
2604 SourceLocation LiteralLoc;
2605 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
2606 return;
Eric Christopher789a7ad2015-06-12 01:36:05 +00002607 S.checkTargetAttr(LiteralLoc, Str);
Eric Christopher11acf732015-06-12 01:35:52 +00002608 unsigned Index = Attr.getAttributeSpellingListIndex();
2609 TargetAttr *NewAttr =
2610 ::new (S.Context) TargetAttr(Attr.getRange(), S.Context, Str, Index);
2611 D->addAttr(NewAttr);
2612}
2613
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002614
Chandler Carruthedc2c642011-07-02 00:01:44 +00002615static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002616 VarDecl *VD = cast<VarDecl>(D);
2617 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002618 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002619 return;
2620 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002621
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002622 Expr *E = Attr.getArgAsExpr(0);
2623 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00002624 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002625 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002626
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002627 // gcc only allows for simple identifiers. Since we support more than gcc, we
2628 // will warn the user.
2629 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2630 if (DRE->hasQualifier())
2631 S.Diag(Loc, diag::warn_cleanup_ext);
2632 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2633 NI = DRE->getNameInfo();
2634 if (!FD) {
2635 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2636 << NI.getName();
2637 return;
2638 }
2639 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2640 if (ULE->hasExplicitTemplateArgs())
2641 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002642 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2643 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002644 if (!FD) {
2645 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2646 << NI.getName();
2647 if (ULE->getType() == S.Context.OverloadTy)
2648 S.NoteAllOverloadCandidates(ULE);
2649 return;
2650 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002651 } else {
2652 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002653 return;
2654 }
2655
Anders Carlssond277d792009-01-31 01:16:18 +00002656 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002657 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2658 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002659 return;
2660 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002661
Anders Carlsson723f55d2009-02-07 23:16:50 +00002662 // We're currently more strict than GCC about what function types we accept.
2663 // If this ever proves to be a problem it should be easy to fix.
2664 QualType Ty = S.Context.getPointerType(VD->getType());
2665 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002666 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2667 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002668 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2669 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002670 return;
2671 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002672
Michael Han99315932013-01-24 16:46:58 +00002673 D->addAttr(::new (S.Context)
2674 CleanupAttr(Attr.getRange(), S.Context, FD,
2675 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002676}
2677
Mike Stumpd3bb5572009-07-24 19:02:52 +00002678/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002679/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002680static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002681 Expr *IdxExpr = Attr.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00002682 uint64_t Idx;
2683 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002684 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002685
Eric Christopherb64963e2015-08-13 21:34:35 +00002686 // Make sure the format string is really a string.
Alp Toker601b22c2014-01-21 23:35:24 +00002687 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002688
Eric Christopherb64963e2015-08-13 21:34:35 +00002689 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
2690 if (NotNSStringTy &&
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002691 !isCFStringType(Ty, S.Context) &&
2692 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002693 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002694 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00002695 << "a string type" << IdxExpr->getSourceRange()
2696 << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002697 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002698 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002699 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002700 if (!isNSStringType(Ty, S.Context) &&
2701 !isCFStringType(Ty, S.Context) &&
2702 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002703 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002704 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Eric Christopherb64963e2015-08-13 21:34:35 +00002705 << (NotNSStringTy ? "string type" : "NSString")
Aaron Ballman2f9e88b2014-08-04 15:17:29 +00002706 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002707 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002708 }
2709
Alp Toker601b22c2014-01-21 23:35:24 +00002710 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002711 // because that has corrected for the implicit this parameter, and is zero-
2712 // based. The attribute expects what the user wrote explicitly.
2713 llvm::APSInt Val;
2714 IdxExpr->EvaluateAsInt(Val, S.Context);
2715
Michael Han99315932013-01-24 16:46:58 +00002716 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002717 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002718 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002719}
2720
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002721enum FormatAttrKind {
2722 CFStringFormat,
2723 NSStringFormat,
2724 StrftimeFormat,
2725 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002726 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002727 InvalidFormat
2728};
2729
2730/// getFormatAttrKind - Map from format attribute names to supported format
2731/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002732static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002733 return llvm::StringSwitch<FormatAttrKind>(Format)
2734 // Check for formats that get handled specially.
2735 .Case("NSString", NSStringFormat)
2736 .Case("CFString", CFStringFormat)
2737 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002738
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002739 // Otherwise, check for supported formats.
2740 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2741 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2742 .Case("kprintf", SupportedFormat) // OpenBSD.
Dimitry Andric6b5ed342015-02-19 22:32:33 +00002743 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
Fariborz Jahanianf8dce0f2015-02-21 00:45:58 +00002744 .Case("os_trace", SupportedFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002745
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002746 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2747 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002748}
2749
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002750/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002751/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002752static void handleInitPriorityAttr(Sema &S, Decl *D,
2753 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002754 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002755 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2756 return;
2757 }
2758
Aaron Ballman4a611152013-11-27 16:34:09 +00002759 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002760 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2761 Attr.setInvalid();
2762 return;
2763 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002764 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002765 if (S.Context.getAsArrayType(T))
2766 T = S.Context.getBaseElementType(T);
2767 if (!T->getAs<RecordType>()) {
2768 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2769 Attr.setInvalid();
2770 return;
2771 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002772
2773 Expr *E = Attr.getArgAsExpr(0);
2774 uint32_t prioritynum;
2775 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002776 Attr.setInvalid();
2777 return;
2778 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002779
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002780 if (prioritynum < 101 || prioritynum > 65535) {
2781 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00002782 << E->getSourceRange() << Attr.getName() << 101 << 65535;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002783 Attr.setInvalid();
2784 return;
2785 }
Michael Han99315932013-01-24 16:46:58 +00002786 D->addAttr(::new (S.Context)
2787 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2788 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002789}
2790
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002791FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2792 IdentifierInfo *Format, int FormatIdx,
2793 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002794 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002795 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002796 for (auto *F : D->specific_attrs<FormatAttr>()) {
2797 if (F->getType() == Format &&
2798 F->getFormatIdx() == FormatIdx &&
2799 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002800 // If we don't have a valid location for this attribute, adopt the
2801 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002802 if (F->getLocation().isInvalid())
2803 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00002804 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00002805 }
2806 }
2807
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002808 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2809 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002810}
2811
Mike Stumpd3bb5572009-07-24 19:02:52 +00002812/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002813/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002814static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002815 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002816 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002817 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002818 return;
2819 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002820
Chandler Carruth743682b2010-11-16 08:35:43 +00002821 // In C++ the implicit 'this' function parameter also counts, and they are
2822 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002823 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00002824 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002825
Aaron Ballman00e99962013-08-31 01:11:41 +00002826 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2827 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002828
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00002829 if (normalizeName(Format)) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002830 // If we've modified the string name, we need a new identifier for it.
2831 II = &S.Context.Idents.get(Format);
2832 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002833
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002834 // Check for supported formats.
2835 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002836
2837 if (Kind == IgnoredFormat)
2838 return;
2839
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002840 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002841 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002842 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002843 return;
2844 }
2845
2846 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002847 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002848 uint32_t Idx;
2849 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002850 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002851
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002852 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002853 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002854 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002855 return;
2856 }
2857
2858 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002859 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002860
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002861 if (HasImplicitThisParam) {
2862 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002863 S.Diag(Attr.getLoc(),
2864 diag::err_format_attribute_implicit_this_format_string)
2865 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002866 return;
2867 }
2868 ArgIdx--;
2869 }
Mike Stump11289f42009-09-09 15:08:12 +00002870
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002871 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002872 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002873
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002874 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002875 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002876 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002877 << "a CFString" << IdxExpr->getSourceRange()
2878 << getFunctionOrMethodParamRange(D, ArgIdx);
Daniel Dunbar980c6692008-09-26 03:32:58 +00002879 return;
2880 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002881 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002882 // FIXME: do we need to check if the type is NSString*? What are the
2883 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002884 if (!isNSStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002885 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002886 << "an NSString" << IdxExpr->getSourceRange()
2887 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002888 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002889 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002890 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002891 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002892 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Aaron Ballmandfe8cc52014-08-04 15:26:33 +00002893 << "a string type" << IdxExpr->getSourceRange()
2894 << getFunctionOrMethodParamRange(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002895 return;
2896 }
2897
2898 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002899 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002900 uint32_t FirstArg;
2901 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002902 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002903
2904 // check if the function is variadic if the 3rd argument non-zero
2905 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002906 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002907 ++NumArgs; // +1 for ...
2908 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002909 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002910 return;
2911 }
2912 }
2913
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002914 // strftime requires FirstArg to be 0 because it doesn't read from any
2915 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002916 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002917 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002918 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2919 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002920 return;
2921 }
2922 // if 0 it disables parameter checking (to use with e.g. va_list)
2923 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002924 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002925 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002926 return;
2927 }
2928
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002929 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002930 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002931 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002932 if (NewAttr)
2933 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002934}
2935
Chandler Carruthedc2c642011-07-02 00:01:44 +00002936static void handleTransparentUnionAttr(Sema &S, Decl *D,
2937 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002938 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00002939 RecordDecl *RD = nullptr;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002940 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002941 if (TD && TD->getUnderlyingType()->isUnionType())
2942 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2943 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002944 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002945
2946 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002947 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002948 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002949 return;
2950 }
2951
John McCallf937c022011-10-07 06:10:15 +00002952 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002953 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002954 diag::warn_transparent_union_attribute_not_definition);
2955 return;
2956 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002957
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002958 RecordDecl::field_iterator Field = RD->field_begin(),
2959 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002960 if (Field == FieldEnd) {
2961 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2962 return;
2963 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002964
David Blaikie40ed2972012-06-06 20:45:41 +00002965 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002966 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002967 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002968 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002969 diag::warn_transparent_union_attribute_floating)
2970 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002971 return;
2972 }
2973
2974 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2975 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2976 for (; Field != FieldEnd; ++Field) {
2977 QualType FieldType = Field->getType();
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002978 // FIXME: this isn't fully correct; we also need to test whether the
2979 // members of the union would all have the same calling convention as the
2980 // first member of the union. Checking just the size and alignment isn't
2981 // sufficient (consider structs passed on the stack instead of in registers
2982 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002983 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002984 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002985 // Warn if we drop the attribute.
2986 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002987 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002988 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002989 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002990 diag::warn_transparent_union_attribute_field_size_align)
2991 << isSize << Field->getDeclName() << FieldBits;
2992 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002993 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002994 diag::note_transparent_union_first_field_size_align)
2995 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002996 return;
2997 }
2998 }
2999
Michael Han99315932013-01-24 16:46:58 +00003000 RD->addAttr(::new (S.Context)
3001 TransparentUnionAttr(Attr.getRange(), S.Context,
3002 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003003}
3004
Chandler Carruthedc2c642011-07-02 00:01:44 +00003005static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003006 // Make sure that there is a string literal as the annotation's single
3007 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003008 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003009 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003010 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003011
3012 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003013 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
3014 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003015 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003016 }
Michael Han99315932013-01-24 16:46:58 +00003017
3018 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003019 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00003020 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003021}
3022
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003023static void handleAlignValueAttr(Sema &S, Decl *D,
3024 const AttributeList &Attr) {
3025 S.AddAlignValueAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
3026 Attr.getAttributeSpellingListIndex());
3027}
3028
3029void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E,
3030 unsigned SpellingListIndex) {
3031 AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex);
3032 SourceLocation AttrLoc = AttrRange.getBegin();
3033
3034 QualType T;
3035 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3036 T = TD->getUnderlyingType();
3037 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3038 T = VD->getType();
3039 else
3040 llvm_unreachable("Unknown decl type for align_value");
3041
3042 if (!T->isDependentType() && !T->isAnyPointerType() &&
3043 !T->isReferenceType() && !T->isMemberPointerType()) {
3044 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
3045 << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange();
3046 return;
3047 }
3048
3049 if (!E->isValueDependent()) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003050 llvm::APSInt Alignment;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00003051 ExprResult ICE
3052 = VerifyIntegerConstantExpression(E, &Alignment,
3053 diag::err_align_value_attribute_argument_not_int,
3054 /*AllowFold*/ false);
3055 if (ICE.isInvalid())
3056 return;
3057
3058 if (!Alignment.isPowerOf2()) {
3059 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3060 << E->getSourceRange();
3061 return;
3062 }
3063
3064 D->addAttr(::new (Context)
3065 AlignValueAttr(AttrRange, Context, ICE.get(),
3066 SpellingListIndex));
3067 return;
3068 }
3069
3070 // Save dependent expressions in the AST to be instantiated.
3071 D->addAttr(::new (Context) AlignValueAttr(TmpAttr));
3072 return;
3073}
3074
Chandler Carruthedc2c642011-07-02 00:01:44 +00003075static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003076 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003077 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003078 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3079 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003080 return;
3081 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003082
Richard Smith848e1f12013-02-01 08:12:08 +00003083 if (Attr.getNumArgs() == 0) {
3084 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
Craig Topperc3ec1492014-05-26 06:22:03 +00003085 true, nullptr, Attr.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00003086 return;
3087 }
3088
Aaron Ballman00e99962013-08-31 01:11:41 +00003089 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00003090 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3091 S.Diag(Attr.getEllipsisLoc(),
3092 diag::err_pack_expansion_without_parameter_packs);
3093 return;
3094 }
3095
3096 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3097 return;
3098
David Majnemer26a1e0e2015-04-07 02:37:09 +00003099 if (E->isValueDependent()) {
3100 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3101 if (!TND->getUnderlyingType()->isDependentType()) {
3102 S.Diag(Attr.getLoc(), diag::err_alignment_dependent_typedef_name)
3103 << E->getSourceRange();
3104 return;
3105 }
3106 }
3107 }
3108
Richard Smith44c247f2013-02-22 08:32:16 +00003109 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3110 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003111}
3112
3113void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003114 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003115 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3116 SourceLocation AttrLoc = AttrRange.getBegin();
3117
Richard Smith1dba27c2013-01-29 09:02:09 +00003118 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003119 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003120 // C++11 [dcl.align]p1:
3121 // An alignment-specifier may be applied to a variable or to a class
3122 // data member, but it shall not be applied to a bit-field, a function
3123 // parameter, the formal parameter of a catch clause, or a variable
3124 // declared with the register storage class specifier. An
3125 // alignment-specifier may also be applied to the declaration of a class
3126 // or enumeration type.
3127 // C11 6.7.5/2:
3128 // An alignment attribute shall not be specified in a declaration of
3129 // a typedef, or a bit-field, or a function, or a parameter, or an
3130 // object declared with the register storage-class specifier.
3131 int DiagKind = -1;
3132 if (isa<ParmVarDecl>(D)) {
3133 DiagKind = 0;
3134 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3135 if (VD->getStorageClass() == SC_Register)
3136 DiagKind = 1;
3137 if (VD->isExceptionVariable())
3138 DiagKind = 2;
3139 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3140 if (FD->isBitField())
3141 DiagKind = 3;
3142 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00003143 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00003144 << (TmpAttr.isC11() ? ExpectedVariableOrField
3145 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003146 return;
3147 }
3148 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003149 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00003150 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003151 return;
3152 }
3153 }
3154
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003155 if (E->isTypeDependent() || E->isValueDependent()) {
3156 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003157 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3158 AA->setPackExpansion(IsPackExpansion);
3159 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003160 return;
3161 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003162
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003163 // FIXME: Cache the number on the Attr object?
David Majnemer0be6bd02015-07-26 09:02:21 +00003164 llvm::APSInt Alignment;
Douglas Gregore2b37442012-05-04 22:38:52 +00003165 ExprResult ICE
3166 = VerifyIntegerConstantExpression(E, &Alignment,
3167 diag::err_aligned_attribute_argument_not_int,
3168 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003169 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003170 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003171
David Majnemer0be6bd02015-07-26 09:02:21 +00003172 uint64_t AlignVal = Alignment.getZExtValue();
3173
Richard Smith848e1f12013-02-01 08:12:08 +00003174 // C++11 [dcl.align]p2:
3175 // -- if the constant expression evaluates to zero, the alignment
3176 // specifier shall have no effect
3177 // C11 6.7.5p6:
3178 // An alignment specification of zero has no effect.
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003179 if (!(TmpAttr.isAlignas() && !Alignment)) {
David Majnemer0be6bd02015-07-26 09:02:21 +00003180 if (!llvm::isPowerOf2_64(AlignVal)) {
Paul Robinsond30e2ee2015-07-14 20:52:32 +00003181 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3182 << E->getSourceRange();
3183 return;
3184 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003185 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003186
David Majnemerabecae72014-02-12 20:36:10 +00003187 // Alignment calculations can wrap around if it's greater than 2**28.
David Majnemer29c69db2015-07-26 01:48:59 +00003188 unsigned MaxValidAlignment =
3189 Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192
3190 : 268435456;
David Majnemer0be6bd02015-07-26 09:02:21 +00003191 if (AlignVal > MaxValidAlignment) {
David Majnemerabecae72014-02-12 20:36:10 +00003192 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
3193 << E->getSourceRange();
3194 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00003195 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003196
David Majnemer0be6bd02015-07-26 09:02:21 +00003197 if (Context.getTargetInfo().isTLSSupported()) {
3198 unsigned MaxTLSAlign =
3199 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3200 .getQuantity();
3201 auto *VD = dyn_cast<VarDecl>(D);
3202 if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD &&
3203 VD->getTLSKind() != VarDecl::TLS_None) {
3204 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3205 << (unsigned)AlignVal << VD << MaxTLSAlign;
3206 return;
3207 }
3208 }
3209
Richard Smith44c247f2013-02-22 08:32:16 +00003210 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003211 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00003212 AA->setPackExpansion(IsPackExpansion);
3213 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003214}
3215
Michael Hanaf02bbe2013-02-01 01:19:17 +00003216void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003217 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003218 // FIXME: Cache the number on the Attr object if non-dependent?
3219 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003220 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3221 SpellingListIndex);
3222 AA->setPackExpansion(IsPackExpansion);
3223 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003224}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003225
Richard Smith848e1f12013-02-01 08:12:08 +00003226void Sema::CheckAlignasUnderalignment(Decl *D) {
3227 assert(D->hasAttrs() && "no attributes on decl");
3228
David Majnemer475b25e2015-01-21 10:54:38 +00003229 QualType UnderlyingTy, DiagTy;
3230 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
3231 UnderlyingTy = DiagTy = VD->getType();
3232 } else {
3233 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
3234 if (EnumDecl *ED = dyn_cast<EnumDecl>(D))
3235 UnderlyingTy = ED->getIntegerType();
3236 }
3237 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003238 return;
3239
3240 // C++11 [dcl.align]p5, C11 6.7.5/4:
3241 // The combined effect of all alignment attributes in a declaration shall
3242 // not specify an alignment that is less strict than the alignment that
3243 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00003244 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00003245 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003246 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00003247 if (I->isAlignmentDependent())
3248 return;
3249 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00003250 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00003251 Align = std::max(Align, I->getAlignment(Context));
3252 }
3253
3254 if (AlignasAttr && Align) {
3255 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
David Majnemer475b25e2015-01-21 10:54:38 +00003256 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
Richard Smith848e1f12013-02-01 08:12:08 +00003257 if (NaturalAlign > RequestedAlign)
3258 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
David Majnemer475b25e2015-01-21 10:54:38 +00003259 << DiagTy << (unsigned)NaturalAlign.getQuantity();
Richard Smith848e1f12013-02-01 08:12:08 +00003260 }
3261}
3262
David Majnemer2c4e00a2014-01-29 22:07:36 +00003263bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00003264 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003265 MSInheritanceAttr::Spelling SemanticSpelling) {
3266 assert(RD->hasDefinition() && "RD has no definition!");
3267
David Majnemer98c9ee22014-02-07 00:43:07 +00003268 // We may not have seen base specifiers or any virtual methods yet. We will
3269 // have to wait until the record is defined to catch any mismatches.
3270 if (!RD->getDefinition()->isCompleteDefinition())
3271 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003272
David Majnemer98c9ee22014-02-07 00:43:07 +00003273 // The unspecified model never matches what a definition could need.
3274 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
3275 return false;
3276
David Majnemer4bb09802014-02-10 19:50:15 +00003277 if (BestCase) {
3278 if (RD->calculateInheritanceModel() == SemanticSpelling)
3279 return false;
3280 } else {
3281 if (RD->calculateInheritanceModel() <= SemanticSpelling)
3282 return false;
3283 }
David Majnemer98c9ee22014-02-07 00:43:07 +00003284
3285 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3286 << 0 /*definition*/;
3287 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
3288 << RD->getNameAsString();
3289 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003290}
3291
Alexey Bataevf278eb12015-11-19 10:13:11 +00003292/// parseModeAttrArg - Parses attribute mode string and returns parsed type
3293/// attribute.
3294static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
3295 bool &IntegerMode, bool &ComplexMode) {
Daniel Dunbarafff4342009-10-18 02:09:24 +00003296 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003297 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003298 switch (Str[0]) {
Alexey Bataevf278eb12015-11-19 10:13:11 +00003299 case 'Q':
3300 DestWidth = 8;
3301 break;
3302 case 'H':
3303 DestWidth = 16;
3304 break;
3305 case 'S':
3306 DestWidth = 32;
3307 break;
3308 case 'D':
3309 DestWidth = 64;
3310 break;
3311 case 'X':
3312 DestWidth = 96;
3313 break;
3314 case 'T':
3315 DestWidth = 128;
3316 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003317 }
3318 if (Str[1] == 'F') {
3319 IntegerMode = false;
3320 } else if (Str[1] == 'C') {
3321 IntegerMode = false;
3322 ComplexMode = true;
3323 } else if (Str[1] != 'I') {
3324 DestWidth = 0;
3325 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003326 break;
3327 case 4:
3328 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3329 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003330 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003331 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003332 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003333 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003334 break;
3335 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003336 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003337 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003338 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003339 case 11:
3340 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003341 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003342 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003343 }
Alexey Bataevf278eb12015-11-19 10:13:11 +00003344}
3345
3346/// handleModeAttr - This attribute modifies the width of a decl with primitive
3347/// type.
3348///
3349/// Despite what would be logical, the mode attribute is a decl attribute, not a
3350/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3351/// HImode, not an intermediate pointer.
3352static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3353 // This attribute isn't documented, but glibc uses it. It changes
3354 // the width of an int or unsigned int to the specified size.
3355 if (!Attr.isArgIdent(0)) {
3356 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3357 << AANT_ArgumentIdentifier;
3358 return;
3359 }
3360
3361 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3362 StringRef Str = Name->getName();
3363
3364 normalizeName(Str);
3365
3366 unsigned DestWidth = 0;
3367 bool IntegerMode = true;
3368 bool ComplexMode = false;
3369 llvm::APInt VectorSize(64, 0);
3370 if (Str.size() >= 4 && Str[0] == 'V') {
3371 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
3372 size_t StrSize = Str.size();
3373 size_t VectorStringLength = 0;
3374 while ((VectorStringLength + 1) < StrSize &&
3375 isdigit(Str[VectorStringLength + 1]))
3376 ++VectorStringLength;
3377 if (VectorStringLength &&
3378 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
3379 VectorSize.isPowerOf2()) {
3380 parseModeAttrArg(S, Str.substr(VectorStringLength + 1), DestWidth,
3381 IntegerMode, ComplexMode);
3382 S.Diag(Attr.getLoc(), diag::warn_vector_mode_deprecated);
3383 } else {
3384 VectorSize = 0;
3385 }
3386 }
3387
3388 if (!VectorSize)
3389 parseModeAttrArg(S, Str, DestWidth, IntegerMode, ComplexMode);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003390
3391 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003392 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003393 OldTy = TD->getUnderlyingType();
Alexey Bataev2c485a72016-01-15 04:36:32 +00003394 else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003395 OldTy = cast<ValueDecl>(D)->getType();
Eli Friedman4735374e2009-03-03 06:41:03 +00003396
Alexey Bataev326057d2015-06-19 07:46:21 +00003397 // Base type can also be a vector type (see PR17453).
3398 // Distinguish between base type and base element type.
3399 QualType OldElemTy = OldTy;
3400 if (const VectorType *VT = OldTy->getAs<VectorType>())
3401 OldElemTy = VT->getElementType();
3402
3403 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003404 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3405 else if (IntegerMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003406 if (!OldElemTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003407 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3408 } else if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003409 if (!OldElemTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003410 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3411 } else {
Alexey Bataev326057d2015-06-19 07:46:21 +00003412 if (!OldElemTy->isFloatingType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003413 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3414 }
3415
Mike Stump87c57ac2009-05-16 07:39:55 +00003416 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3417 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003418 // FIXME: Make sure floating-point mappings are accurate
3419 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003420 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00003421 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003422 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003423 }
3424
Alexey Bataev326057d2015-06-19 07:46:21 +00003425 QualType NewElemTy;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003426
3427 if (IntegerMode)
Alexey Bataev326057d2015-06-19 07:46:21 +00003428 NewElemTy = S.Context.getIntTypeForBitwidth(
3429 DestWidth, OldElemTy->isSignedIntegerType());
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003430 else
Alexey Bataev326057d2015-06-19 07:46:21 +00003431 NewElemTy = S.Context.getRealTypeForBitwidth(DestWidth);
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003432
Alexey Bataev326057d2015-06-19 07:46:21 +00003433 if (NewElemTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00003434 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003435 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003436 }
3437
Eli Friedman4735374e2009-03-03 06:41:03 +00003438 if (ComplexMode) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003439 NewElemTy = S.Context.getComplexType(NewElemTy);
3440 }
3441
3442 QualType NewTy = NewElemTy;
Alexey Bataevf278eb12015-11-19 10:13:11 +00003443 if (VectorSize.getBoolValue()) {
3444 NewTy = S.Context.getVectorType(NewTy, VectorSize.getZExtValue(),
3445 VectorType::GenericVector);
3446 } else if (const VectorType *OldVT = OldTy->getAs<VectorType>()) {
Alexey Bataev326057d2015-06-19 07:46:21 +00003447 // Complex machine mode does not support base vector types.
3448 if (ComplexMode) {
3449 S.Diag(Attr.getLoc(), diag::err_complex_mode_vector_type);
3450 return;
3451 }
3452 unsigned NumElements = S.Context.getTypeSize(OldElemTy) *
3453 OldVT->getNumElements() /
3454 S.Context.getTypeSize(NewElemTy);
3455 NewTy =
3456 S.Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
3457 }
3458
3459 if (NewTy.isNull()) {
3460 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3461 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003462 }
3463
3464 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003465 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3466 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3467 else
Aaron Ballman6c8848a2016-01-19 22:54:26 +00003468 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003469
3470 D->addAttr(::new (S.Context)
3471 ModeAttr(Attr.getRange(), S.Context, Name,
3472 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003473}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003474
Chandler Carruthedc2c642011-07-02 00:01:44 +00003475static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003476 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3477 if (!VD->hasGlobalStorage())
3478 S.Diag(Attr.getLoc(),
3479 diag::warn_attribute_requires_functions_or_static_globals)
3480 << Attr.getName();
3481 } else if (!isFunctionOrMethod(D)) {
3482 S.Diag(Attr.getLoc(),
3483 diag::warn_attribute_requires_functions_or_static_globals)
3484 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003485 return;
3486 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003487
Michael Han99315932013-01-24 16:46:58 +00003488 D->addAttr(::new (S.Context)
3489 NoDebugAttr(Attr.getRange(), S.Context,
3490 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003491}
3492
Paul Robinson30e41fb2014-12-15 18:57:28 +00003493AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range,
Paul Robinson080b1f32015-01-13 18:34:56 +00003494 IdentifierInfo *Ident,
Paul Robinson30e41fb2014-12-15 18:57:28 +00003495 unsigned AttrSpellingListIndex) {
3496 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003497 Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident;
Paul Robinson30e41fb2014-12-15 18:57:28 +00003498 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3499 return nullptr;
3500 }
3501
3502 if (D->hasAttr<AlwaysInlineAttr>())
3503 return nullptr;
3504
3505 return ::new (Context) AlwaysInlineAttr(Range, Context,
3506 AttrSpellingListIndex);
3507}
3508
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003509CommonAttr *Sema::mergeCommonAttr(Decl *D, SourceRange Range,
3510 IdentifierInfo *Ident,
3511 unsigned AttrSpellingListIndex) {
3512 if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, Range, Ident))
3513 return nullptr;
3514
3515 return ::new (Context) CommonAttr(Range, Context, AttrSpellingListIndex);
3516}
3517
3518InternalLinkageAttr *
3519Sema::mergeInternalLinkageAttr(Decl *D, SourceRange Range,
3520 IdentifierInfo *Ident,
3521 unsigned AttrSpellingListIndex) {
3522 if (auto VD = dyn_cast<VarDecl>(D)) {
3523 // Attribute applies to Var but not any subclass of it (like ParmVar,
3524 // ImplicitParm or VarTemplateSpecialization).
3525 if (VD->getKind() != Decl::Var) {
3526 Diag(Range.getBegin(), diag::warn_attribute_wrong_decl_type)
3527 << Ident << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
3528 : ExpectedVariableOrFunction);
3529 return nullptr;
3530 }
3531 // Attribute does not apply to non-static local variables.
3532 if (VD->hasLocalStorage()) {
3533 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
3534 return nullptr;
3535 }
3536 }
3537
3538 if (checkAttrMutualExclusion<CommonAttr>(*this, D, Range, Ident))
3539 return nullptr;
3540
3541 return ::new (Context)
3542 InternalLinkageAttr(Range, Context, AttrSpellingListIndex);
3543}
3544
Paul Robinson30e41fb2014-12-15 18:57:28 +00003545MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range,
3546 unsigned AttrSpellingListIndex) {
3547 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
3548 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'";
3549 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
3550 return nullptr;
3551 }
3552
3553 if (D->hasAttr<MinSizeAttr>())
3554 return nullptr;
3555
3556 return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex);
3557}
3558
3559OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range,
3560 unsigned AttrSpellingListIndex) {
3561 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
3562 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
3563 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3564 D->dropAttr<AlwaysInlineAttr>();
3565 }
3566 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
3567 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
3568 Diag(Range.getBegin(), diag::note_conflicting_attribute);
3569 D->dropAttr<MinSizeAttr>();
3570 }
3571
3572 if (D->hasAttr<OptimizeNoneAttr>())
3573 return nullptr;
3574
3575 return ::new (Context) OptimizeNoneAttr(Range, Context,
3576 AttrSpellingListIndex);
3577}
3578
Paul Robinsonf0674352014-03-31 22:29:15 +00003579static void handleAlwaysInlineAttr(Sema &S, Decl *D,
3580 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00003581 if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, Attr.getRange(),
3582 Attr.getName()))
Akira Hatanakac8667622015-11-06 23:56:15 +00003583 return;
3584
Paul Robinson080b1f32015-01-13 18:34:56 +00003585 if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr(
3586 D, Attr.getRange(), Attr.getName(),
3587 Attr.getAttributeSpellingListIndex()))
3588 D->addAttr(Inline);
Paul Robinsonf0674352014-03-31 22:29:15 +00003589}
3590
Paul Robinson080b1f32015-01-13 18:34:56 +00003591static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3592 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(
3593 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3594 D->addAttr(MinSize);
Paul Robinsonaae2fba2014-12-10 23:34:36 +00003595}
3596
Paul Robinsonf0674352014-03-31 22:29:15 +00003597static void handleOptimizeNoneAttr(Sema &S, Decl *D,
3598 const AttributeList &Attr) {
Paul Robinson080b1f32015-01-13 18:34:56 +00003599 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(
3600 D, Attr.getRange(), Attr.getAttributeSpellingListIndex()))
3601 D->addAttr(Optnone);
Paul Robinsonf0674352014-03-31 22:29:15 +00003602}
3603
Chandler Carruthedc2c642011-07-02 00:01:44 +00003604static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Justin Lebar3eaaf862016-01-13 01:07:35 +00003605 if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, Attr.getRange(),
3606 Attr.getName()) ||
3607 checkAttrMutualExclusion<CUDAHostAttr>(S, D, Attr.getRange(),
3608 Attr.getName())) {
3609 return;
3610 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003611 FunctionDecl *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00003612 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00003613 SourceRange RTRange = FD->getReturnTypeSourceRange();
3614 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00003615 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00003616 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
3617 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00003618 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003619 }
Justin Lebarc66a1062016-01-20 00:26:57 +00003620 if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) {
3621 if (Method->isInstance()) {
3622 S.Diag(Method->getLocStart(), diag::err_kern_is_nonstatic_method)
3623 << Method;
3624 return;
3625 }
3626 S.Diag(Method->getLocStart(), diag::warn_kern_is_method) << Method;
3627 }
3628 // Only warn for "inline" when compiling for host, to cut down on noise.
3629 if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice)
3630 S.Diag(FD->getLocStart(), diag::warn_kern_is_inline) << FD;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003631
Aaron Ballman3aff6332013-12-02 19:30:36 +00003632 D->addAttr(::new (S.Context)
3633 CUDAGlobalAttr(Attr.getRange(), S.Context,
Eli Bendersky83860042014-09-02 22:00:06 +00003634 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003635}
3636
Chandler Carruthedc2c642011-07-02 00:01:44 +00003637static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003638 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003639 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003640 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003641 return;
3642 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003643
Michael Han99315932013-01-24 16:46:58 +00003644 D->addAttr(::new (S.Context)
3645 GNUInlineAttr(Attr.getRange(), S.Context,
3646 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003647}
3648
Chandler Carruthedc2c642011-07-02 00:01:44 +00003649static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003650 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003651
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003652 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003653 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3654 CallingConv CC;
Richard Smitheec7cb12015-05-28 23:38:53 +00003655 if (S.CheckCallingConvAttr(Attr, CC, /*FD*/nullptr))
John McCall3882ace2011-01-05 12:14:39 +00003656 return;
3657
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003658 if (!isa<ObjCMethodDecl>(D)) {
3659 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3660 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003661 return;
3662 }
3663
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003664 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003665 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003666 D->addAttr(::new (S.Context)
3667 FastCallAttr(Attr.getRange(), S.Context,
3668 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003669 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003670 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003671 D->addAttr(::new (S.Context)
3672 StdCallAttr(Attr.getRange(), S.Context,
3673 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003674 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003675 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003676 D->addAttr(::new (S.Context)
3677 ThisCallAttr(Attr.getRange(), S.Context,
3678 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003679 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003680 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003681 D->addAttr(::new (S.Context)
3682 CDeclAttr(Attr.getRange(), S.Context,
3683 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003684 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003685 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003686 D->addAttr(::new (S.Context)
3687 PascalAttr(Attr.getRange(), S.Context,
3688 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003689 return;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003690 case AttributeList::AT_VectorCall:
3691 D->addAttr(::new (S.Context)
3692 VectorCallAttr(Attr.getRange(), S.Context,
3693 Attr.getAttributeSpellingListIndex()));
3694 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003695 case AttributeList::AT_MSABI:
3696 D->addAttr(::new (S.Context)
3697 MSABIAttr(Attr.getRange(), S.Context,
3698 Attr.getAttributeSpellingListIndex()));
3699 return;
3700 case AttributeList::AT_SysVABI:
3701 D->addAttr(::new (S.Context)
3702 SysVABIAttr(Attr.getRange(), S.Context,
3703 Attr.getAttributeSpellingListIndex()));
3704 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003705 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003706 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003707 switch (CC) {
3708 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003709 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003710 break;
3711 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003712 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003713 break;
3714 default:
3715 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003716 }
3717
Michael Han99315932013-01-24 16:46:58 +00003718 D->addAttr(::new (S.Context)
3719 PcsAttr(Attr.getRange(), S.Context, PCS,
3720 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003721 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003722 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00003723 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003724 D->addAttr(::new (S.Context)
3725 IntelOclBiccAttr(Attr.getRange(), S.Context,
3726 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003727 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003728
Abramo Bagnara50099372010-04-30 13:10:51 +00003729 default:
3730 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003731 }
3732}
3733
Aaron Ballman02df2e02012-12-09 17:45:41 +00003734bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3735 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003736 if (attr.isInvalid())
3737 return true;
3738
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003739 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003740 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003741 attr.setInvalid();
3742 return true;
3743 }
3744
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003745 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003746 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003747 case AttributeList::AT_CDecl: CC = CC_C; break;
3748 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3749 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3750 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3751 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Reid Klecknerd7857f02014-10-24 17:42:17 +00003752 case AttributeList::AT_VectorCall: CC = CC_X86VectorCall; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003753 case AttributeList::AT_MSABI:
3754 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3755 CC_X86_64Win64;
3756 break;
3757 case AttributeList::AT_SysVABI:
3758 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3759 CC_C;
3760 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003761 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003762 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003763 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003764 attr.setInvalid();
3765 return true;
3766 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003767 if (StrRef == "aapcs") {
3768 CC = CC_AAPCS;
3769 break;
3770 } else if (StrRef == "aapcs-vfp") {
3771 CC = CC_AAPCS_VFP;
3772 break;
3773 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003774
3775 attr.setInvalid();
3776 Diag(attr.getLoc(), diag::err_invalid_pcs);
3777 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003778 }
Guy Benyeif0a014b2012-12-25 08:53:55 +00003779 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003780 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003781 }
3782
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003783 const TargetInfo &TI = Context.getTargetInfo();
3784 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
Reid Kleckner9fde2e02015-02-26 19:43:46 +00003785 if (A != TargetInfo::CCCR_OK) {
3786 if (A == TargetInfo::CCCR_Warning)
3787 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003788
Reid Kleckner9fde2e02015-02-26 19:43:46 +00003789 // This convention is not valid for the target. Use the default function or
3790 // method calling convention.
Aaron Ballman02df2e02012-12-09 17:45:41 +00003791 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3792 if (FD)
3793 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3794 TargetInfo::CCMT_NonMember;
3795 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003796 }
3797
John McCall3882ace2011-01-05 12:14:39 +00003798 return false;
3799}
3800
John McCall3882ace2011-01-05 12:14:39 +00003801/// Checks a regparm attribute, returning true if it is ill-formed and
3802/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003803bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3804 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003805 return true;
3806
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003807 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003808 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003809 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003810 }
Eli Friedman7044b762009-03-27 21:06:47 +00003811
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003812 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003813 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003814 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003815 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003816 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003817 }
3818
Douglas Gregore8bbc122011-09-02 00:18:52 +00003819 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003820 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003821 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003822 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003823 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003824 }
3825
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003826 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003827 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003828 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003829 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003830 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003831 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003832 }
3833
John McCall3882ace2011-01-05 12:14:39 +00003834 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003835}
3836
Artem Belevich7093e402015-04-21 22:55:54 +00003837// Checks whether an argument of launch_bounds attribute is acceptable
3838// May output an error.
3839static bool checkLaunchBoundsArgument(Sema &S, Expr *E,
3840 const CUDALaunchBoundsAttr &Attr,
3841 const unsigned Idx) {
3842
3843 if (S.DiagnoseUnexpandedParameterPack(E))
3844 return false;
3845
3846 // Accept template arguments for now as they depend on something else.
3847 // We'll get to check them when they eventually get instantiated.
3848 if (E->isValueDependent())
3849 return true;
3850
3851 llvm::APSInt I(64);
3852 if (!E->isIntegerConstantExpr(I, S.Context)) {
3853 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
3854 << &Attr << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
3855 return false;
3856 }
3857 // Make sure we can fit it in 32 bits.
3858 if (!I.isIntN(32)) {
3859 S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false)
3860 << 32 << /* Unsigned */ 1;
3861 return false;
3862 }
3863 if (I < 0)
3864 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
3865 << &Attr << Idx << E->getSourceRange();
3866
3867 return true;
3868}
3869
3870void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads,
3871 Expr *MinBlocks, unsigned SpellingListIndex) {
3872 CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks,
3873 SpellingListIndex);
3874
3875 if (!checkLaunchBoundsArgument(*this, MaxThreads, TmpAttr, 0))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003876 return;
3877
Artem Belevich7093e402015-04-21 22:55:54 +00003878 if (MinBlocks && !checkLaunchBoundsArgument(*this, MinBlocks, TmpAttr, 1))
3879 return;
3880
3881 D->addAttr(::new (Context) CUDALaunchBoundsAttr(
3882 AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex));
3883}
3884
3885static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3886 const AttributeList &Attr) {
3887 if (!checkAttributeAtLeastNumArgs(S, Attr, 1) ||
3888 !checkAttributeAtMostNumArgs(S, Attr, 2))
3889 return;
3890
3891 S.AddLaunchBoundsAttr(Attr.getRange(), D, Attr.getArgAsExpr(0),
3892 Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr,
3893 Attr.getAttributeSpellingListIndex());
Peter Collingbourne827301e2010-12-12 23:03:07 +00003894}
3895
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003896static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3897 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003898 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003899 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003900 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003901 return;
3902 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003903
3904 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003905 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003906
Aaron Ballman00e99962013-08-31 01:11:41 +00003907 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003908
3909 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3910 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3911 << Attr.getName() << ExpectedFunctionOrMethod;
3912 return;
3913 }
3914
3915 uint64_t ArgumentIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003916 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3917 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003918 return;
3919
3920 uint64_t TypeTagIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003921 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3922 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003923 return;
3924
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003925 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003926 if (IsPointer) {
3927 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00003928 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003929 if (!BufferTy->isPointerType()) {
3930 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00003931 << Attr.getName() << 0;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003932 }
3933 }
3934
Michael Han99315932013-01-24 16:46:58 +00003935 D->addAttr(::new (S.Context)
3936 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3937 ArgumentIdx, TypeTagIdx, IsPointer,
3938 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003939}
3940
3941static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3942 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003943 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003944 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003945 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003946 return;
3947 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003948
3949 if (!checkAttributeNumArgs(S, Attr, 1))
3950 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003951
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003952 if (!isa<VarDecl>(D)) {
3953 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3954 << Attr.getName() << ExpectedVariable;
3955 return;
3956 }
3957
Aaron Ballman00e99962013-08-31 01:11:41 +00003958 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00003959 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00003960 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3961 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003962
Michael Han99315932013-01-24 16:46:58 +00003963 D->addAttr(::new (S.Context)
3964 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003965 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003966 Attr.getLayoutCompatible(),
3967 Attr.getMustBeNull(),
3968 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003969}
3970
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003971//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003972// Checker-specific attribute handlers.
3973//===----------------------------------------------------------------------===//
3974
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003975static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) {
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003976 return type->isDependentType() ||
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003977 type->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003978}
3979
John McCalled433932011-01-25 03:31:58 +00003980static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003981 return type->isDependentType() ||
3982 type->isObjCObjectPointerType() ||
3983 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003984}
3985static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003986 return type->isDependentType() ||
3987 type->isPointerType() ||
3988 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003989}
3990
Chandler Carruthedc2c642011-07-02 00:01:44 +00003991static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003992 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003993 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003994
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003995 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003996 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3997 cf = false;
3998 } else {
3999 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4000 cf = true;
4001 }
4002
4003 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004004 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004005 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00004006 return;
4007 }
4008
4009 if (cf)
Michael Han99315932013-01-24 16:46:58 +00004010 param->addAttr(::new (S.Context)
4011 CFConsumedAttr(Attr.getRange(), S.Context,
4012 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004013 else
Michael Han99315932013-01-24 16:46:58 +00004014 param->addAttr(::new (S.Context)
4015 NSConsumedAttr(Attr.getRange(), S.Context,
4016 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004017}
4018
Chandler Carruthedc2c642011-07-02 00:01:44 +00004019static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4020 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004021
John McCalled433932011-01-25 03:31:58 +00004022 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004023
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004024 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004025 returnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004026 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004027 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004028 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00004029 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4030 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004031 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004032 returnType = FD->getReturnType();
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004033 else if (auto *Param = dyn_cast<ParmVarDecl>(D)) {
4034 returnType = Param->getType()->getPointeeType();
4035 if (returnType.isNull()) {
4036 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
4037 << Attr.getName() << /*pointer-to-CF*/2
4038 << Attr.getRange();
4039 return;
4040 }
4041 } else {
4042 AttributeDeclKind ExpectedDeclKind;
4043 switch (Attr.getKind()) {
4044 default: llvm_unreachable("invalid ownership attribute");
4045 case AttributeList::AT_NSReturnsRetained:
4046 case AttributeList::AT_NSReturnsAutoreleased:
4047 case AttributeList::AT_NSReturnsNotRetained:
4048 ExpectedDeclKind = ExpectedFunctionOrMethod;
4049 break;
4050
4051 case AttributeList::AT_CFReturnsRetained:
4052 case AttributeList::AT_CFReturnsNotRetained:
4053 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
4054 break;
4055 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004056 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004057 << Attr.getRange() << Attr.getName() << ExpectedDeclKind;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004058 return;
4059 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004060
John McCalled433932011-01-25 03:31:58 +00004061 bool typeOK;
4062 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004063 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004064 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004065 case AttributeList::AT_NSReturnsRetained:
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00004066 typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType);
Fariborz Jahanian9c100322014-06-11 21:22:53 +00004067 cf = false;
4068 break;
4069
4070 case AttributeList::AT_NSReturnsAutoreleased:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004071 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004072 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4073 cf = false;
4074 break;
4075
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004076 case AttributeList::AT_CFReturnsRetained:
4077 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004078 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4079 cf = true;
4080 break;
4081 }
4082
4083 if (!typeOK) {
Douglas Gregoreb6e64c2015-06-19 23:17:46 +00004084 if (isa<ParmVarDecl>(D)) {
4085 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
4086 << Attr.getName() << /*pointer-to-CF*/2
4087 << Attr.getRange();
4088 } else {
4089 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
4090 enum : unsigned {
4091 Function,
4092 Method,
4093 Property
4094 } SubjectKind = Function;
4095 if (isa<ObjCMethodDecl>(D))
4096 SubjectKind = Method;
4097 else if (isa<ObjCPropertyDecl>(D))
4098 SubjectKind = Property;
4099 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4100 << Attr.getName() << SubjectKind << cf
4101 << Attr.getRange();
4102 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004103 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004104 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004105
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004106 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004107 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004108 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004109 case AttributeList::AT_NSReturnsAutoreleased:
Nico Weber462fd1e2015-01-07 23:50:05 +00004110 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(
4111 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004112 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004113 case AttributeList::AT_CFReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004114 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(
4115 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004116 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004117 case AttributeList::AT_NSReturnsNotRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004118 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(
4119 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004120 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004121 case AttributeList::AT_CFReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004122 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(
4123 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004124 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004125 case AttributeList::AT_NSReturnsRetained:
Nico Weber462fd1e2015-01-07 23:50:05 +00004126 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(
4127 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004128 return;
4129 };
4130}
4131
John McCallcf166702011-07-22 08:53:00 +00004132static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4133 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00004134 const int EP_ObjCMethod = 1;
4135 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004136
John McCallcf166702011-07-22 08:53:00 +00004137 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004138 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004139 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00004140 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004141 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004142 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00004143
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004144 if (!resultType->isReferenceType() &&
4145 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004146 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00004147 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004148 << attr.getName()
4149 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004150 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00004151
4152 // Drop the attribute.
4153 return;
4154 }
4155
Nico Weber462fd1e2015-01-07 23:50:05 +00004156 D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr(
4157 attr.getRange(), S.Context, attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004158}
4159
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004160static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4161 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004162 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004163
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004164 DeclContext *DC = method->getDeclContext();
4165 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4166 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4167 << attr.getName() << 0;
4168 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4169 return;
4170 }
4171 if (method->getMethodFamily() == OMF_dealloc) {
4172 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4173 << attr.getName() << 1;
4174 return;
4175 }
4176
Michael Han99315932013-01-24 16:46:58 +00004177 method->addAttr(::new (S.Context)
4178 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4179 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004180}
4181
Aaron Ballmanfb763042013-12-02 18:05:46 +00004182static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
4183 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004184 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr.getRange(),
4185 Attr.getName()))
John McCall32f5fe12011-09-30 05:12:12 +00004186 return;
John McCall32f5fe12011-09-30 05:12:12 +00004187
Aaron Ballmanfb763042013-12-02 18:05:46 +00004188 D->addAttr(::new (S.Context)
4189 CFAuditedTransferAttr(Attr.getRange(), S.Context,
4190 Attr.getAttributeSpellingListIndex()));
4191}
4192
4193static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
4194 const AttributeList &Attr) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004195 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr.getRange(),
4196 Attr.getName()))
Aaron Ballmanfb763042013-12-02 18:05:46 +00004197 return;
4198
4199 D->addAttr(::new (S.Context)
4200 CFUnknownTransferAttr(Attr.getRange(), S.Context,
4201 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004202}
4203
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004204static void handleObjCBridgeAttr(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;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004207
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004208 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004209 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004210 return;
4211 }
John McCall28592582015-02-01 22:34:06 +00004212
4213 // Typedefs only allow objc_bridge(id) and have some additional checking.
4214 if (auto TD = dyn_cast<TypedefNameDecl>(D)) {
4215 if (!Parm->Ident->isStr("id")) {
4216 S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_id)
4217 << Attr.getName();
4218 return;
4219 }
4220
4221 // Only allow 'cv void *'.
4222 QualType T = TD->getUnderlyingType();
4223 if (!T->isVoidPointerType()) {
4224 S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_void_pointer);
4225 return;
4226 }
4227 }
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004228
4229 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004230 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004231 Attr.getAttributeSpellingListIndex()));
4232}
4233
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004234static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
4235 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004236 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
4237
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004238 if (!Parm) {
4239 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
4240 return;
4241 }
4242
4243 D->addAttr(::new (S.Context)
4244 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
4245 Attr.getAttributeSpellingListIndex()));
4246}
4247
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004248static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
4249 const AttributeList &Attr) {
4250 IdentifierInfo *RelatedClass =
Craig Topperc3ec1492014-05-26 06:22:03 +00004251 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004252 if (!RelatedClass) {
4253 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
4254 return;
4255 }
4256 IdentifierInfo *ClassMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00004257 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004258 IdentifierInfo *InstanceMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00004259 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004260 D->addAttr(::new (S.Context)
4261 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
4262 ClassMethod, InstanceMethod,
4263 Attr.getAttributeSpellingListIndex()));
4264}
4265
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004266static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
4267 const AttributeList &Attr) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004268 ObjCInterfaceDecl *IFace;
Nico Weber462fd1e2015-01-07 23:50:05 +00004269 if (ObjCCategoryDecl *CatDecl =
4270 dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00004271 IFace = CatDecl->getClassInterface();
4272 else
4273 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00004274
4275 if (!IFace)
4276 return;
4277
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00004278 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00004279 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004280 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
4281 Attr.getAttributeSpellingListIndex()));
4282}
4283
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004284static void handleObjCRuntimeName(Sema &S, Decl *D,
4285 const AttributeList &Attr) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00004286 StringRef MetaDataName;
4287 if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName))
4288 return;
4289 D->addAttr(::new (S.Context)
4290 ObjCRuntimeNameAttr(Attr.getRange(), S.Context,
4291 MetaDataName,
4292 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004293}
4294
Alex Denisovfde64952015-06-26 05:28:36 +00004295// when a user wants to use objc_boxable with a union or struct
4296// but she doesn't have access to the declaration (legacy/third-party code)
4297// then she can 'enable' this feature via trick with a typedef
4298// e.g.:
4299// typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
4300static void handleObjCBoxable(Sema &S, Decl *D, const AttributeList &Attr) {
4301 bool notify = false;
4302
4303 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4304 if (RD && RD->getDefinition()) {
4305 RD = RD->getDefinition();
4306 notify = true;
4307 }
4308
4309 if (RD) {
4310 ObjCBoxableAttr *BoxableAttr = ::new (S.Context)
4311 ObjCBoxableAttr(Attr.getRange(), S.Context,
4312 Attr.getAttributeSpellingListIndex());
4313 RD->addAttr(BoxableAttr);
4314 if (notify) {
4315 // we need to notify ASTReader/ASTWriter about
4316 // modification of existing declaration
4317 if (ASTMutationListener *L = S.getASTMutationListener())
4318 L->AddedAttributeToRecord(BoxableAttr, RD);
4319 }
4320 }
4321}
4322
Chandler Carruthedc2c642011-07-02 00:01:44 +00004323static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4324 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004325 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004326
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004327 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004328 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004329}
4330
Chandler Carruthedc2c642011-07-02 00:01:44 +00004331static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4332 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004333 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004334 QualType type = vd->getType();
4335
4336 if (!type->isDependentType() &&
4337 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004338 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004339 << type;
4340 return;
4341 }
4342
4343 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4344
4345 // If we have no lifetime yet, check the lifetime we're presumably
4346 // going to infer.
4347 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4348 lifetime = type->getObjCARCImplicitLifetime();
4349
4350 switch (lifetime) {
4351 case Qualifiers::OCL_None:
4352 assert(type->isDependentType() &&
4353 "didn't infer lifetime for non-dependent type?");
4354 break;
4355
4356 case Qualifiers::OCL_Weak: // meaningful
4357 case Qualifiers::OCL_Strong: // meaningful
4358 break;
4359
4360 case Qualifiers::OCL_ExplicitNone:
4361 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004362 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004363 << (lifetime == Qualifiers::OCL_Autoreleasing);
4364 break;
4365 }
4366
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004367 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004368 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4369 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004370}
4371
Francois Picheta83957a2010-12-19 06:50:37 +00004372//===----------------------------------------------------------------------===//
4373// Microsoft specific attribute handlers.
4374//===----------------------------------------------------------------------===//
4375
Chandler Carruthedc2c642011-07-02 00:01:44 +00004376static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00004377 if (!S.LangOpts.CPlusPlus) {
4378 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4379 << Attr.getName() << AttributeLangSupport::C;
4380 return;
4381 }
4382
Aaron Ballman60e705e2013-11-24 20:58:02 +00004383 if (!isa<CXXRecordDecl>(D)) {
4384 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4385 << Attr.getName() << ExpectedClass;
4386 return;
4387 }
4388
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004389 StringRef StrRef;
4390 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00004391 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00004392 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004393
David Majnemer89085342013-08-09 08:56:20 +00004394 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4395 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00004396 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4397 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00004398
Reid Kleckner140c4a72013-05-17 14:04:52 +00004399 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00004400 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004401 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004402 return;
4403 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004404
David Majnemer89085342013-08-09 08:56:20 +00004405 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004406 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00004407 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004408 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00004409 return;
4410 }
David Majnemer89085342013-08-09 08:56:20 +00004411 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004412 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004413 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004414 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004415 }
Francois Picheta83957a2010-12-19 06:50:37 +00004416
David Majnemer89085342013-08-09 08:56:20 +00004417 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4418 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004419}
4420
David Majnemer2c4e00a2014-01-29 22:07:36 +00004421static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4422 if (!S.LangOpts.CPlusPlus) {
4423 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4424 << Attr.getName() << AttributeLangSupport::C;
4425 return;
4426 }
4427 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
David Majnemer4bb09802014-02-10 19:50:15 +00004428 D, Attr.getRange(), /*BestCase=*/true,
4429 Attr.getAttributeSpellingListIndex(),
David Majnemer2c4e00a2014-01-29 22:07:36 +00004430 (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling());
4431 if (IA)
4432 D->addAttr(IA);
4433}
4434
Reid Kleckner7d6d2702014-05-01 03:16:47 +00004435static void handleDeclspecThreadAttr(Sema &S, Decl *D,
4436 const AttributeList &Attr) {
4437 VarDecl *VD = cast<VarDecl>(D);
4438 if (!S.Context.getTargetInfo().isTLSSupported()) {
4439 S.Diag(Attr.getLoc(), diag::err_thread_unsupported);
4440 return;
4441 }
4442 if (VD->getTSCSpec() != TSCS_unspecified) {
4443 S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable);
4444 return;
4445 }
4446 if (VD->hasLocalStorage()) {
4447 S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
4448 return;
4449 }
4450 VD->addAttr(::new (S.Context) ThreadAttr(
4451 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
4452}
4453
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004454static void handleARMInterruptAttr(Sema &S, Decl *D,
4455 const AttributeList &Attr) {
4456 // Check the attribute arguments.
4457 if (Attr.getNumArgs() > 1) {
4458 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
4459 << Attr.getName() << 1;
4460 return;
4461 }
4462
4463 StringRef Str;
4464 SourceLocation ArgLoc;
4465
4466 if (Attr.getNumArgs() == 0)
4467 Str = "";
4468 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
4469 return;
4470
4471 ARMInterruptAttr::InterruptType Kind;
4472 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
4473 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
4474 << Attr.getName() << Str << ArgLoc;
4475 return;
4476 }
4477
4478 unsigned Index = Attr.getAttributeSpellingListIndex();
4479 D->addAttr(::new (S.Context)
4480 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
4481}
4482
4483static void handleMSP430InterruptAttr(Sema &S, Decl *D,
4484 const AttributeList &Attr) {
4485 if (!checkAttributeNumArgs(S, Attr, 1))
4486 return;
4487
4488 if (!Attr.isArgExpr(0)) {
4489 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
4490 << AANT_ArgumentIntegerConstant;
4491 return;
4492 }
4493
4494 // FIXME: Check for decl - it should be void ()(void).
4495
4496 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4497 llvm::APSInt NumParams(32);
4498 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
4499 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4500 << Attr.getName() << AANT_ArgumentIntegerConstant
4501 << NumParamsExpr->getSourceRange();
4502 return;
4503 }
4504
4505 unsigned Num = NumParams.getLimitedValue(255);
4506 if ((Num & 1) || Num > 30) {
4507 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
4508 << Attr.getName() << (int)NumParams.getSExtValue()
4509 << NumParamsExpr->getSourceRange();
4510 return;
4511 }
4512
Aaron Ballman36a53502014-01-16 13:03:14 +00004513 D->addAttr(::new (S.Context)
4514 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
4515 Attr.getAttributeSpellingListIndex()));
4516 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004517}
4518
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00004519static void handleMipsInterruptAttr(Sema &S, Decl *D,
4520 const AttributeList &Attr) {
4521 // Only one optional argument permitted.
4522 if (Attr.getNumArgs() > 1) {
4523 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
4524 << Attr.getName() << 1;
4525 return;
4526 }
4527
4528 StringRef Str;
4529 SourceLocation ArgLoc;
4530
4531 if (Attr.getNumArgs() == 0)
4532 Str = "";
4533 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
4534 return;
4535
4536 // Semantic checks for a function with the 'interrupt' attribute for MIPS:
4537 // a) Must be a function.
4538 // b) Must have no parameters.
4539 // c) Must have the 'void' return type.
4540 // d) Cannot have the 'mips16' attribute, as that instruction set
4541 // lacks the 'eret' instruction.
4542 // e) The attribute itself must either have no argument or one of the
4543 // valid interrupt types, see [MipsInterruptDocs].
4544
4545 if (!isFunctionOrMethod(D)) {
4546 S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
4547 << "'interrupt'" << ExpectedFunctionOrMethod;
4548 return;
4549 }
4550
4551 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
4552 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
4553 << 0;
4554 return;
4555 }
4556
4557 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
4558 S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute)
4559 << 1;
4560 return;
4561 }
4562
4563 if (checkAttrMutualExclusion<Mips16Attr>(S, D, Attr.getRange(),
4564 Attr.getName()))
4565 return;
4566
4567 MipsInterruptAttr::InterruptType Kind;
4568 if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
4569 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
4570 << Attr.getName() << "'" + std::string(Str) + "'";
4571 return;
4572 }
4573
4574 D->addAttr(::new (S.Context) MipsInterruptAttr(
4575 Attr.getLoc(), S.Context, Kind, Attr.getAttributeSpellingListIndex()));
4576}
4577
Alexey Bataevd51e9932016-01-15 04:06:31 +00004578static void handleAnyX86InterruptAttr(Sema &S, Decl *D,
4579 const AttributeList &Attr) {
4580 // Semantic checks for a function with the 'interrupt' attribute.
4581 // a) Must be a function.
4582 // b) Must have the 'void' return type.
4583 // c) Must take 1 or 2 arguments.
4584 // d) The 1st argument must be a pointer.
4585 // e) The 2nd argument (if any) must be an unsigned integer.
4586 if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) ||
4587 CXXMethodDecl::isStaticOverloadedOperator(
4588 cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) {
4589 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4590 << Attr.getName() << ExpectedFunctionWithProtoType;
4591 return;
4592 }
4593 // Interrupt handler must have void return type.
4594 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
4595 S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(),
4596 diag::err_anyx86_interrupt_attribute)
4597 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
4598 ? 0
4599 : 1)
4600 << 0;
4601 return;
4602 }
4603 // Interrupt handler must have 1 or 2 parameters.
4604 unsigned NumParams = getFunctionOrMethodNumParams(D);
4605 if (NumParams < 1 || NumParams > 2) {
4606 S.Diag(D->getLocStart(), diag::err_anyx86_interrupt_attribute)
4607 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
4608 ? 0
4609 : 1)
4610 << 1;
4611 return;
4612 }
4613 // The first argument must be a pointer.
4614 if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) {
4615 S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(),
4616 diag::err_anyx86_interrupt_attribute)
4617 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
4618 ? 0
4619 : 1)
4620 << 2;
4621 return;
4622 }
4623 // The second argument, if present, must be an unsigned integer.
4624 unsigned TypeSize =
4625 S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64
4626 ? 64
4627 : 32;
4628 if (NumParams == 2 &&
4629 (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() ||
4630 S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) {
4631 S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(),
4632 diag::err_anyx86_interrupt_attribute)
4633 << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
4634 ? 0
4635 : 1)
4636 << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false);
4637 return;
4638 }
4639 D->addAttr(::new (S.Context) AnyX86InterruptAttr(
4640 Attr.getLoc(), S.Context, Attr.getAttributeSpellingListIndex()));
4641 D->addAttr(UsedAttr::CreateImplicit(S.Context));
4642}
4643
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004644static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4645 // Dispatch the interrupt attribute based on the current target.
Alexey Bataevd51e9932016-01-15 04:06:31 +00004646 switch (S.Context.getTargetInfo().getTriple().getArch()) {
4647 case llvm::Triple::msp430:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004648 handleMSP430InterruptAttr(S, D, Attr);
Alexey Bataevd51e9932016-01-15 04:06:31 +00004649 break;
4650 case llvm::Triple::mipsel:
4651 case llvm::Triple::mips:
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00004652 handleMipsInterruptAttr(S, D, Attr);
Alexey Bataevd51e9932016-01-15 04:06:31 +00004653 break;
4654 case llvm::Triple::x86:
4655 case llvm::Triple::x86_64:
4656 handleAnyX86InterruptAttr(S, D, Attr);
4657 break;
4658 default:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004659 handleARMInterruptAttr(S, D, Attr);
Alexey Bataevd51e9932016-01-15 04:06:31 +00004660 break;
4661 }
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004662}
4663
Matt Arsenault43fae6c2014-12-04 20:38:18 +00004664static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D,
4665 const AttributeList &Attr) {
4666 uint32_t NumRegs;
4667 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4668 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4669 return;
4670
4671 D->addAttr(::new (S.Context)
4672 AMDGPUNumVGPRAttr(Attr.getLoc(), S.Context,
4673 NumRegs,
4674 Attr.getAttributeSpellingListIndex()));
4675}
4676
4677static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D,
4678 const AttributeList &Attr) {
4679 uint32_t NumRegs;
4680 Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
4681 if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs))
4682 return;
4683
4684 D->addAttr(::new (S.Context)
4685 AMDGPUNumSGPRAttr(Attr.getLoc(), S.Context,
4686 NumRegs,
4687 Attr.getAttributeSpellingListIndex()));
4688}
4689
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004690static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
4691 const AttributeList& Attr) {
4692 // If we try to apply it to a function pointer, don't warn, but don't
4693 // do anything, either. It doesn't matter anyway, because there's nothing
4694 // special about calling a force_align_arg_pointer function.
4695 ValueDecl *VD = dyn_cast<ValueDecl>(D);
4696 if (VD && VD->getType()->isFunctionPointerType())
4697 return;
4698 // Also don't warn on function pointer typedefs.
4699 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
4700 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
4701 TD->getUnderlyingType()->isFunctionType()))
4702 return;
4703 // Attribute can only be applied to function types.
4704 if (!isa<FunctionDecl>(D)) {
4705 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4706 << Attr.getName() << /* function */0;
4707 return;
4708 }
4709
Aaron Ballman36a53502014-01-16 13:03:14 +00004710 D->addAttr(::new (S.Context)
4711 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
4712 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004713}
4714
4715DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
4716 unsigned AttrSpellingListIndex) {
4717 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004718 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00004719 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004720 }
4721
4722 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004723 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004724
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004725 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004726}
4727
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004728DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
4729 unsigned AttrSpellingListIndex) {
4730 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00004731 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004732 D->dropAttr<DLLImportAttr>();
4733 }
4734
4735 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00004736 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004737
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00004738 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004739}
4740
Hans Wennborge82f19c2014-06-24 23:57:05 +00004741static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00004742 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
4743 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4744 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored)
4745 << A.getName();
4746 return;
4747 }
4748
Hans Wennborg606bd6d2014-11-03 14:24:45 +00004749 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4750 if (FD->isInlined() && A.getKind() == AttributeList::AT_DLLImport &&
4751 !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4752 // MinGW doesn't allow dllimport on inline functions.
4753 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
4754 << A.getName();
4755 return;
4756 }
4757 }
4758
Hans Wennborg5869ec42015-09-15 21:05:30 +00004759 if (auto *MD = dyn_cast<CXXMethodDecl>(D)) {
4760 if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
4761 MD->getParent()->isLambda()) {
4762 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A.getName();
4763 return;
4764 }
4765 }
4766
Hans Wennborge82f19c2014-06-24 23:57:05 +00004767 unsigned Index = A.getAttributeSpellingListIndex();
4768 Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport
4769 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
4770 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004771 if (NewAttr)
4772 D->addAttr(NewAttr);
4773}
4774
David Majnemer2c4e00a2014-01-29 22:07:36 +00004775MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00004776Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00004777 unsigned AttrSpellingListIndex,
4778 MSInheritanceAttr::Spelling SemanticSpelling) {
4779 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
4780 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00004781 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004782 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
4783 << 1 /*previous declaration*/;
4784 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
4785 D->dropAttr<MSInheritanceAttr>();
4786 }
4787
4788 CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
4789 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00004790 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
4791 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004792 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004793 }
4794 } else {
4795 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
4796 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4797 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004798 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004799 }
4800 if (RD->getDescribedClassTemplate()) {
4801 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
4802 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00004803 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00004804 }
4805 }
4806
4807 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00004808 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00004809}
4810
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004811static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4812 // The capability attributes take a single string parameter for the name of
4813 // the capability they represent. The lockable attribute does not take any
4814 // parameters. However, semantically, both attributes represent the same
4815 // concept, and so they use the same semantic attribute. Eventually, the
4816 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00004817 //
Alp Toker958027b2014-07-14 19:42:55 +00004818 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00004819 // literal will be considered a "mutex."
4820 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004821 SourceLocation LiteralLoc;
4822 if (Attr.getKind() == AttributeList::AT_Capability &&
4823 !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc))
4824 return;
4825
Aaron Ballman6c810072014-03-05 21:47:13 +00004826 // Currently, there are only two names allowed for a capability: role and
4827 // mutex (case insensitive). Diagnose other capability names.
4828 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
4829 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
4830
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004831 D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N,
4832 Attr.getAttributeSpellingListIndex()));
4833}
4834
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004835static void handleAssertCapabilityAttr(Sema &S, Decl *D,
4836 const AttributeList &Attr) {
4837 D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context,
4838 Attr.getArgAsExpr(0),
4839 Attr.getAttributeSpellingListIndex()));
4840}
4841
4842static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
4843 const AttributeList &Attr) {
4844 SmallVector<Expr*, 1> Args;
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004845 if (!checkLockFunAttrCommon(S, D, Attr, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004846 return;
4847
4848 D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(),
4849 S.Context,
4850 Args.data(), Args.size(),
4851 Attr.getAttributeSpellingListIndex()));
4852}
4853
4854static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
4855 const AttributeList &Attr) {
4856 SmallVector<Expr*, 2> Args;
4857 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
4858 return;
4859
4860 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(),
4861 S.Context,
4862 Attr.getArgAsExpr(0),
4863 Args.data(),
4864 Args.size(),
4865 Attr.getAttributeSpellingListIndex()));
4866}
4867
4868static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
4869 const AttributeList &Attr) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004870 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004871 SmallVector<Expr *, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004872 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004873
Aaron Ballman18d85ae2014-03-20 16:02:49 +00004874 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
4875 Attr.getRange(), S.Context, Args.data(), Args.size(),
4876 Attr.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004877}
4878
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004879static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
4880 const AttributeList &Attr) {
4881 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
4882 return;
4883
4884 // check that all arguments are lockable objects
4885 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00004886 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004887 if (Args.empty())
4888 return;
4889
4890 RequiresCapabilityAttr *RCA = ::new (S.Context)
4891 RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(),
4892 Args.size(), Attr.getAttributeSpellingListIndex());
4893
4894 D->addAttr(RCA);
4895}
4896
Aaron Ballman43f40102014-11-14 22:34:56 +00004897static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4898 if (auto *NSD = dyn_cast<NamespaceDecl>(D)) {
4899 if (NSD->isAnonymousNamespace()) {
4900 S.Diag(Attr.getLoc(), diag::warn_deprecated_anonymous_namespace);
4901 // Do not want to attach the attribute to the namespace because that will
4902 // cause confusing diagnostic reports for uses of declarations within the
4903 // namespace.
4904 return;
4905 }
4906 }
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00004907
4908 if (!S.getLangOpts().CPlusPlus14)
4909 if (Attr.isCXX11Attribute() &&
4910 !(Attr.hasScope() && Attr.getScopeName()->isStr("gnu")))
Saleem Abdulrasoolb47d6062015-02-18 04:33:26 +00004911 S.Diag(Attr.getLoc(), diag::ext_deprecated_attr_is_a_cxx14_extension);
Saleem Abdulrasoolf931a382015-02-16 22:27:01 +00004912
Aaron Ballman43f40102014-11-14 22:34:56 +00004913 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
4914}
4915
Peter Collingbourne915df992015-05-15 18:33:32 +00004916static void handleNoSanitizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4917 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
4918 return;
4919
4920 std::vector<std::string> Sanitizers;
4921
4922 for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) {
4923 StringRef SanitizerName;
4924 SourceLocation LiteralLoc;
4925
4926 if (!S.checkStringLiteralArgumentAttr(Attr, I, SanitizerName, &LiteralLoc))
4927 return;
4928
4929 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0)
4930 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
4931
4932 Sanitizers.push_back(SanitizerName);
4933 }
4934
4935 D->addAttr(::new (S.Context) NoSanitizeAttr(
4936 Attr.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(),
4937 Attr.getAttributeSpellingListIndex()));
4938}
4939
4940static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
4941 const AttributeList &Attr) {
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00004942 StringRef AttrName = Attr.getName()->getName();
4943 normalizeName(AttrName);
Peter Collingbourne915df992015-05-15 18:33:32 +00004944 std::string SanitizerName =
Aaron Ballman8b5e7ba2015-10-08 19:24:08 +00004945 llvm::StringSwitch<std::string>(AttrName)
Peter Collingbourne915df992015-05-15 18:33:32 +00004946 .Case("no_address_safety_analysis", "address")
4947 .Case("no_sanitize_address", "address")
4948 .Case("no_sanitize_thread", "thread")
Peter Collingbourne94410942015-05-15 20:11:18 +00004949 .Case("no_sanitize_memory", "memory");
Peter Collingbourne915df992015-05-15 18:33:32 +00004950 D->addAttr(::new (S.Context)
4951 NoSanitizeAttr(Attr.getRange(), S.Context, &SanitizerName, 1,
4952 Attr.getAttributeSpellingListIndex()));
4953}
4954
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00004955static void handleInternalLinkageAttr(Sema &S, Decl *D,
4956 const AttributeList &Attr) {
4957 if (InternalLinkageAttr *Internal =
4958 S.mergeInternalLinkageAttr(D, Attr.getRange(), Attr.getName(),
4959 Attr.getAttributeSpellingListIndex()))
4960 D->addAttr(Internal);
4961}
4962
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004963/// Handles semantic checking for features that are common to all attributes,
4964/// such as checking whether a parameter was properly specified, or the correct
4965/// number of arguments were passed, etc.
4966static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4967 const AttributeList &Attr) {
4968 // Several attributes carry different semantics than the parsing requires, so
4969 // those are opted out of the common handling.
4970 //
4971 // We also bail on unknown and ignored attributes because those are handled
4972 // as part of the target-specific handling logic.
4973 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004974 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004975 return false;
4976
Aaron Ballman3aff6332013-12-02 19:30:36 +00004977 // Check whether the attribute requires specific language extensions to be
4978 // enabled.
4979 if (!Attr.diagnoseLangOpts(S))
4980 return true;
4981
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00004982 if (Attr.getMinArgs() == Attr.getMaxArgs()) {
4983 // If there are no optional arguments, then checking for the argument count
4984 // is trivial.
4985 if (!checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4986 return true;
4987 } else {
4988 // There are optional arguments, so checking is slightly more involved.
4989 if (Attr.getMinArgs() &&
4990 !checkAttributeAtLeastNumArgs(S, Attr, Attr.getMinArgs()))
4991 return true;
4992 else if (!Attr.hasVariadicArg() && Attr.getMaxArgs() &&
4993 !checkAttributeAtMostNumArgs(S, Attr, Attr.getMaxArgs()))
4994 return true;
4995 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004996
4997 // Check whether the attribute appertains to the given subject.
4998 if (!Attr.diagnoseAppertainsTo(S, D))
4999 return true;
5000
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005001 return false;
5002}
5003
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005004//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005005// Top Level Sema Entry Points
5006//===----------------------------------------------------------------------===//
5007
Richard Smithf8a75c32013-08-29 00:47:48 +00005008/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
5009/// the attribute applies to decls. If the attribute is a type attribute, just
5010/// silently ignore it if a GNU attribute.
5011static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
5012 const AttributeList &Attr,
5013 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005014 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00005015 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00005016
Richard Smithf8a75c32013-08-29 00:47:48 +00005017 // Ignore C++11 attributes on declarator chunks: they appertain to the type
5018 // instead.
5019 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
5020 return;
5021
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005022 // Unknown attributes are automatically warned on. Target-specific attributes
5023 // which do not apply to the current target architecture are treated as
5024 // though they were unknown attributes.
5025 if (Attr.getKind() == AttributeList::UnknownAttribute ||
Bob Wilson7c730832015-07-20 22:57:31 +00005026 !Attr.existsInTarget(S.Context.getTargetInfo())) {
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005027 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute()
5028 ? diag::warn_unhandled_ms_attribute_ignored
5029 : diag::warn_unknown_attribute_ignored)
5030 << Attr.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005031 return;
5032 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005033
Aaron Ballman8ee40b72013-09-09 23:33:17 +00005034 if (handleCommonAttributeFeatures(S, scope, D, Attr))
5035 return;
5036
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005037 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005038 default:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005039 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005040 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005041 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005042 case AttributeList::AT_Interrupt:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005043 handleInterruptAttr(S, D, Attr);
5044 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005045 case AttributeList::AT_X86ForceAlignArgPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005046 handleX86ForceAlignArgPointerAttr(S, D, Attr);
5047 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005048 case AttributeList::AT_DLLExport:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005049 case AttributeList::AT_DLLImport:
Hans Wennborge82f19c2014-06-24 23:57:05 +00005050 handleDLLAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005051 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005052 case AttributeList::AT_Mips16:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005053 handleSimpleAttributeWithExclusions<Mips16Attr, MipsInterruptAttr>(S, D,
5054 Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005055 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00005056 case AttributeList::AT_NoMips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005057 handleSimpleAttribute<NoMips16Attr>(S, D, Attr);
5058 break;
Matt Arsenault43fae6c2014-12-04 20:38:18 +00005059 case AttributeList::AT_AMDGPUNumVGPR:
5060 handleAMDGPUNumVGPRAttr(S, D, Attr);
5061 break;
5062 case AttributeList::AT_AMDGPUNumSGPR:
5063 handleAMDGPUNumSGPRAttr(S, D, Attr);
5064 break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00005065 case AttributeList::AT_IBAction:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005066 handleSimpleAttribute<IBActionAttr>(S, D, Attr);
5067 break;
5068 case AttributeList::AT_IBOutlet:
5069 handleIBOutlet(S, D, Attr);
5070 break;
Richard Smith10876ef2013-01-17 01:30:42 +00005071 case AttributeList::AT_IBOutletCollection:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005072 handleIBOutletCollection(S, D, Attr);
5073 break;
5074 case AttributeList::AT_Alias:
5075 handleAliasAttr(S, D, Attr);
5076 break;
5077 case AttributeList::AT_Aligned:
5078 handleAlignedAttr(S, D, Attr);
5079 break;
Hal Finkel1b0d24e2014-10-02 21:21:25 +00005080 case AttributeList::AT_AlignValue:
5081 handleAlignValueAttr(S, D, Attr);
5082 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005083 case AttributeList::AT_AlwaysInline:
Paul Robinsonf0674352014-03-31 22:29:15 +00005084 handleAlwaysInlineAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005085 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005086 case AttributeList::AT_AnalyzerNoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005087 handleAnalyzerNoReturnAttr(S, D, Attr);
5088 break;
5089 case AttributeList::AT_TLSModel:
5090 handleTLSModelAttr(S, D, Attr);
5091 break;
5092 case AttributeList::AT_Annotate:
5093 handleAnnotateAttr(S, D, Attr);
5094 break;
5095 case AttributeList::AT_Availability:
5096 handleAvailabilityAttr(S, D, Attr);
5097 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005098 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00005099 handleDependencyAttr(S, scope, D, Attr);
5100 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005101 case AttributeList::AT_Common:
5102 handleCommonAttr(S, D, Attr);
5103 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005104 case AttributeList::AT_CUDAConstant:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005105 handleSimpleAttributeWithExclusions<CUDAConstantAttr, CUDASharedAttr>(S, D,
5106 Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005107 break;
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00005108 case AttributeList::AT_PassObjectSize:
5109 handlePassObjectSizeAttr(S, D, Attr);
5110 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005111 case AttributeList::AT_Constructor:
5112 handleConstructorAttr(S, D, Attr);
5113 break;
Richard Smith10876ef2013-01-17 01:30:42 +00005114 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005115 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr);
5116 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005117 case AttributeList::AT_Deprecated:
Aaron Ballman43f40102014-11-14 22:34:56 +00005118 handleDeprecatedAttr(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00005119 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005120 case AttributeList::AT_Destructor:
5121 handleDestructorAttr(S, D, Attr);
5122 break;
5123 case AttributeList::AT_EnableIf:
5124 handleEnableIfAttr(S, D, Attr);
5125 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005126 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005127 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005128 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00005129 case AttributeList::AT_MinSize:
Paul Robinsonaae2fba2014-12-10 23:34:36 +00005130 handleMinSizeAttr(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00005131 break;
Paul Robinsonf0674352014-03-31 22:29:15 +00005132 case AttributeList::AT_OptimizeNone:
5133 handleOptimizeNoneAttr(S, D, Attr);
5134 break;
Alexis Hunt724f14e2014-11-28 00:53:20 +00005135 case AttributeList::AT_FlagEnum:
5136 handleSimpleAttribute<FlagEnumAttr>(S, D, Attr);
5137 break;
Peter Collingbourne41af7c22014-05-20 17:12:51 +00005138 case AttributeList::AT_Flatten:
5139 handleSimpleAttribute<FlattenAttr>(S, D, Attr);
5140 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005141 case AttributeList::AT_Format:
5142 handleFormatAttr(S, D, Attr);
5143 break;
5144 case AttributeList::AT_FormatArg:
5145 handleFormatArgAttr(S, D, Attr);
5146 break;
5147 case AttributeList::AT_CUDAGlobal:
5148 handleGlobalAttr(S, D, Attr);
5149 break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00005150 case AttributeList::AT_CUDADevice:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005151 handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D,
5152 Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005153 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005154 case AttributeList::AT_CUDAHost:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005155 handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D,
5156 Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005157 break;
5158 case AttributeList::AT_GNUInline:
5159 handleGNUInlineAttr(S, D, Attr);
5160 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005161 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005162 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00005163 break;
David Majnemer631a90b2015-02-04 07:23:21 +00005164 case AttributeList::AT_Restrict:
5165 handleRestrictAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005166 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005167 case AttributeList::AT_MayAlias:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005168 handleSimpleAttribute<MayAliasAttr>(S, D, Attr);
5169 break;
5170 case AttributeList::AT_Mode:
5171 handleModeAttr(S, D, Attr);
5172 break;
David Majnemer1bf0f8e2015-07-20 22:51:52 +00005173 case AttributeList::AT_NoAlias:
5174 handleSimpleAttribute<NoAliasAttr>(S, D, Attr);
5175 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005176 case AttributeList::AT_NoCommon:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005177 handleSimpleAttribute<NoCommonAttr>(S, D, Attr);
5178 break;
Peter Collingbourneb4728c12014-05-19 22:14:34 +00005179 case AttributeList::AT_NoSplitStack:
5180 handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr);
5181 break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00005182 case AttributeList::AT_NonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005183 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
5184 handleNonNullAttrParameter(S, PVD, Attr);
5185 else
5186 handleNonNullAttr(S, D, Attr);
5187 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00005188 case AttributeList::AT_ReturnsNonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005189 handleReturnsNonNullAttr(S, D, Attr);
5190 break;
Hal Finkelee90a222014-09-26 05:04:30 +00005191 case AttributeList::AT_AssumeAligned:
5192 handleAssumeAlignedAttr(S, D, Attr);
5193 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005194 case AttributeList::AT_Overloadable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005195 handleSimpleAttribute<OverloadableAttr>(S, D, Attr);
5196 break;
5197 case AttributeList::AT_Ownership:
5198 handleOwnershipAttr(S, D, Attr);
5199 break;
5200 case AttributeList::AT_Cold:
5201 handleColdAttr(S, D, Attr);
5202 break;
5203 case AttributeList::AT_Hot:
5204 handleHotAttr(S, D, Attr);
5205 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005206 case AttributeList::AT_Naked:
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00005207 handleNakedAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005208 break;
5209 case AttributeList::AT_NoReturn:
5210 handleNoReturnAttr(S, D, Attr);
5211 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00005212 case AttributeList::AT_NoThrow:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005213 handleSimpleAttribute<NoThrowAttr>(S, D, Attr);
5214 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00005215 case AttributeList::AT_CUDAShared:
Justin Lebar3eaaf862016-01-13 01:07:35 +00005216 handleSimpleAttributeWithExclusions<CUDASharedAttr, CUDAConstantAttr>(S, D,
5217 Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005218 break;
5219 case AttributeList::AT_VecReturn:
5220 handleVecReturnAttr(S, D, Attr);
5221 break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00005222
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005223 case AttributeList::AT_ObjCOwnership:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005224 handleObjCOwnershipAttr(S, D, Attr);
5225 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005226 case AttributeList::AT_ObjCPreciseLifetime:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005227 handleObjCPreciseLifetimeAttr(S, D, Attr);
5228 break;
John McCall31168b02011-06-15 23:02:42 +00005229
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005230 case AttributeList::AT_ObjCReturnsInnerPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005231 handleObjCReturnsInnerPointerAttr(S, D, Attr);
5232 break;
John McCallcf166702011-07-22 08:53:00 +00005233
Fariborz Jahanian566fff02012-09-07 23:46:23 +00005234 case AttributeList::AT_ObjCRequiresSuper:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005235 handleObjCRequiresSuperAttr(S, D, Attr);
5236 break;
5237
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00005238 case AttributeList::AT_ObjCBridge:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005239 handleObjCBridgeAttr(S, scope, D, Attr);
5240 break;
5241
Fariborz Jahanian87c77912013-11-21 20:50:32 +00005242 case AttributeList::AT_ObjCBridgeMutable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005243 handleObjCBridgeMutableAttr(S, scope, D, Attr);
5244 break;
5245
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00005246 case AttributeList::AT_ObjCBridgeRelated:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005247 handleObjCBridgeRelatedAttr(S, scope, D, Attr);
5248 break;
John McCallf1e8b342011-09-29 07:17:38 +00005249
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005250 case AttributeList::AT_ObjCDesignatedInitializer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005251 handleObjCDesignatedInitializer(S, D, Attr);
5252 break;
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00005253
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00005254 case AttributeList::AT_ObjCRuntimeName:
5255 handleObjCRuntimeName(S, D, Attr);
5256 break;
Alex Denisovfde64952015-06-26 05:28:36 +00005257
5258 case AttributeList::AT_ObjCBoxable:
5259 handleObjCBoxable(S, D, Attr);
5260 break;
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00005261
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005262 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005263 handleCFAuditedTransferAttr(S, D, Attr);
5264 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005265 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005266 handleCFUnknownTransferAttr(S, D, Attr);
5267 break;
John McCall32f5fe12011-09-30 05:12:12 +00005268
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005269 case AttributeList::AT_CFConsumed:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005270 case AttributeList::AT_NSConsumed:
5271 handleNSConsumedAttr(S, D, Attr);
5272 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005273 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005274 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr);
5275 break;
John McCalled433932011-01-25 03:31:58 +00005276
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005277 case AttributeList::AT_NSReturnsAutoreleased:
5278 case AttributeList::AT_NSReturnsNotRetained:
5279 case AttributeList::AT_CFReturnsNotRetained:
5280 case AttributeList::AT_NSReturnsRetained:
5281 case AttributeList::AT_CFReturnsRetained:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005282 handleNSReturnsRetainedAttr(S, D, Attr);
5283 break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00005284 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005285 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr);
5286 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005287 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005288 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr);
5289 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00005290 case AttributeList::AT_VecTypeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005291 handleVecTypeHint(S, D, Attr);
5292 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00005293
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005294 case AttributeList::AT_InitPriority:
5295 handleInitPriorityAttr(S, D, Attr);
5296 break;
5297
5298 case AttributeList::AT_Packed:
5299 handlePackedAttr(S, D, Attr);
5300 break;
5301 case AttributeList::AT_Section:
5302 handleSectionAttr(S, D, Attr);
5303 break;
Eric Christopher11acf732015-06-12 01:35:52 +00005304 case AttributeList::AT_Target:
5305 handleTargetAttr(S, D, Attr);
5306 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005307 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00005308 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00005309 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005310 case AttributeList::AT_ArcWeakrefUnavailable:
5311 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr);
5312 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005313 case AttributeList::AT_ObjCRootClass:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005314 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr);
5315 break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00005316 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek438f8db2014-02-22 01:06:05 +00005317 handleObjCSuppresProtocolAttr(S, D, Attr);
Ted Kremenek28eace62013-11-23 01:01:34 +00005318 break;
5319 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005320 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr);
5321 break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00005322 case AttributeList::AT_Unused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005323 handleSimpleAttribute<UnusedAttr>(S, D, Attr);
5324 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005325 case AttributeList::AT_ReturnsTwice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005326 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr);
5327 break;
Akira Hatanakac8667622015-11-06 23:56:15 +00005328 case AttributeList::AT_NotTailCalled:
5329 handleNotTailCalledAttr(S, D, Attr);
5330 break;
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00005331 case AttributeList::AT_DisableTailCalls:
5332 handleDisableTailCallsAttr(S, D, Attr);
5333 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005334 case AttributeList::AT_Used:
5335 handleUsedAttr(S, D, Attr);
5336 break;
John McCalld041a9b2013-02-20 01:54:26 +00005337 case AttributeList::AT_Visibility:
5338 handleVisibilityAttr(S, D, Attr, false);
5339 break;
5340 case AttributeList::AT_TypeVisibility:
5341 handleVisibilityAttr(S, D, Attr, true);
5342 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00005343 case AttributeList::AT_WarnUnused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005344 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr);
5345 break;
5346 case AttributeList::AT_WarnUnusedResult:
5347 handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00005348 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00005349 case AttributeList::AT_Weak:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005350 handleSimpleAttribute<WeakAttr>(S, D, Attr);
5351 break;
5352 case AttributeList::AT_WeakRef:
5353 handleWeakRefAttr(S, D, Attr);
5354 break;
5355 case AttributeList::AT_WeakImport:
5356 handleWeakImportAttr(S, D, Attr);
5357 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005358 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005359 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005360 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005361 case AttributeList::AT_ObjCException:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005362 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr);
5363 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005364 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005365 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00005366 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005367 case AttributeList::AT_ObjCNSObject:
5368 handleObjCNSObject(S, D, Attr);
5369 break;
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00005370 case AttributeList::AT_ObjCIndependentClass:
5371 handleObjCIndependentClass(S, D, Attr);
5372 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005373 case AttributeList::AT_Blocks:
5374 handleBlocksAttr(S, D, Attr);
5375 break;
5376 case AttributeList::AT_Sentinel:
5377 handleSentinelAttr(S, D, Attr);
5378 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00005379 case AttributeList::AT_Const:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005380 handleSimpleAttribute<ConstAttr>(S, D, Attr);
5381 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005382 case AttributeList::AT_Pure:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005383 handleSimpleAttribute<PureAttr>(S, D, Attr);
5384 break;
5385 case AttributeList::AT_Cleanup:
5386 handleCleanupAttr(S, D, Attr);
5387 break;
5388 case AttributeList::AT_NoDebug:
5389 handleNoDebugAttr(S, D, Attr);
5390 break;
Aaron Ballman7c19ab12014-02-22 16:59:24 +00005391 case AttributeList::AT_NoDuplicate:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005392 handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr);
5393 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005394 case AttributeList::AT_NoInline:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005395 handleSimpleAttribute<NoInlineAttr>(S, D, Attr);
5396 break;
5397 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
5398 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr);
5399 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005400 case AttributeList::AT_StdCall:
5401 case AttributeList::AT_CDecl:
5402 case AttributeList::AT_FastCall:
5403 case AttributeList::AT_ThisCall:
5404 case AttributeList::AT_Pascal:
Reid Klecknerd7857f02014-10-24 17:42:17 +00005405 case AttributeList::AT_VectorCall:
Charles Davisb5a214e2013-08-30 04:39:01 +00005406 case AttributeList::AT_MSABI:
5407 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005408 case AttributeList::AT_Pcs:
Guy Benyeif0a014b2012-12-25 08:53:55 +00005409 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005410 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00005411 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005412 case AttributeList::AT_OpenCLKernel:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005413 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr);
5414 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00005415 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005416 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr);
5417 break;
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00005418 case AttributeList::AT_InternalLinkage:
5419 handleInternalLinkageAttr(S, D, Attr);
5420 break;
John McCall8d32c052012-05-22 21:28:12 +00005421
5422 // Microsoft attributes:
David Majnemer8ab003a2015-02-02 19:30:52 +00005423 case AttributeList::AT_MSNoVTable:
5424 handleSimpleAttribute<MSNoVTableAttr>(S, D, Attr);
David Majnemer129f4172015-02-02 10:22:20 +00005425 break;
David Majnemer8ab003a2015-02-02 19:30:52 +00005426 case AttributeList::AT_MSStruct:
5427 handleSimpleAttribute<MSStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00005428 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005429 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00005430 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00005431 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00005432 case AttributeList::AT_MSInheritance:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005433 handleMSInheritanceAttr(S, D, Attr);
5434 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00005435 case AttributeList::AT_SelectAny:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005436 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr);
5437 break;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00005438 case AttributeList::AT_Thread:
5439 handleDeclspecThreadAttr(S, D, Attr);
5440 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005441
5442 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00005443 case AttributeList::AT_AssertExclusiveLock:
5444 handleAssertExclusiveLockAttr(S, D, Attr);
5445 break;
5446 case AttributeList::AT_AssertSharedLock:
5447 handleAssertSharedLockAttr(S, D, Attr);
5448 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005449 case AttributeList::AT_GuardedVar:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005450 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr);
5451 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005452 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00005453 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005454 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005455 case AttributeList::AT_ScopedLockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005456 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr);
5457 break;
Peter Collingbourne915df992015-05-15 18:33:32 +00005458 case AttributeList::AT_NoSanitize:
5459 handleNoSanitizeAttr(S, D, Attr);
5460 break;
5461 case AttributeList::AT_NoSanitizeSpecific:
5462 handleNoSanitizeSpecificAttr(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00005463 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005464 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00005465 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00005466 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005467 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005468 handleGuardedByAttr(S, D, Attr);
5469 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005470 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00005471 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005472 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005473 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005474 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005475 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005476 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005477 handleLockReturnedAttr(S, D, Attr);
5478 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005479 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005480 handleLocksExcludedAttr(S, D, Attr);
5481 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005482 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00005483 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005484 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005485 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00005486 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005487 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005488 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00005489 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00005490 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00005491
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005492 // Capability analysis attributes.
5493 case AttributeList::AT_Capability:
5494 case AttributeList::AT_Lockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005495 handleCapabilityAttr(S, D, Attr);
5496 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005497 case AttributeList::AT_RequiresCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005498 handleRequiresCapabilityAttr(S, D, Attr);
5499 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005500
5501 case AttributeList::AT_AssertCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005502 handleAssertCapabilityAttr(S, D, Attr);
5503 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005504 case AttributeList::AT_AcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005505 handleAcquireCapabilityAttr(S, D, Attr);
5506 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005507 case AttributeList::AT_ReleaseCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005508 handleReleaseCapabilityAttr(S, D, Attr);
5509 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00005510 case AttributeList::AT_TryAcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005511 handleTryAcquireCapabilityAttr(S, D, Attr);
5512 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00005513
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00005514 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00005515 case AttributeList::AT_Consumable:
5516 handleConsumableAttr(S, D, Attr);
5517 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00005518 case AttributeList::AT_ConsumableAutoCast:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005519 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr);
5520 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00005521 case AttributeList::AT_ConsumableSetOnRead:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00005522 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr);
5523 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00005524 case AttributeList::AT_CallableWhen:
5525 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00005526 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00005527 case AttributeList::AT_ParamTypestate:
5528 handleParamTypestateAttr(S, D, Attr);
5529 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00005530 case AttributeList::AT_ReturnTypestate:
5531 handleReturnTypestateAttr(S, D, Attr);
5532 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00005533 case AttributeList::AT_SetTypestate:
5534 handleSetTypestateAttr(S, D, Attr);
5535 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00005536 case AttributeList::AT_TestTypestate:
5537 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00005538 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00005539
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00005540 // Type safety attributes.
5541 case AttributeList::AT_ArgumentWithTypeTag:
5542 handleArgumentWithTypeTagAttr(S, D, Attr);
5543 break;
5544 case AttributeList::AT_TypeTagForDatatype:
5545 handleTypeTagForDatatypeAttr(S, D, Attr);
5546 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005547 }
5548}
5549
5550/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5551/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00005552void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00005553 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00005554 bool IncludeCXX11Attributes) {
5555 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00005556 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00005557
Joey Gouly2cd9db12013-12-13 16:15:28 +00005558 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00005559 // GCC accepts
5560 // static int a9 __attribute__((weakref));
5561 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00005562 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00005563 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
5564 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00005565 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00005566 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005567 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00005568
Aaron Ballmanbe243a72014-12-04 22:45:31 +00005569 // FIXME: We should be able to handle this in TableGen as well. It would be
5570 // good to have a way to specify "these attributes must appear as a group",
5571 // for these. Additionally, it would be good to have a way to specify "these
5572 // attribute must never appear as a group" for attributes like cold and hot.
Joey Gouly2cd9db12013-12-13 16:15:28 +00005573 if (!D->hasAttr<OpenCLKernelAttr>()) {
5574 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00005575 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00005576 // FIXME: This emits a different error message than
5577 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
Aaron Ballman3e424b52013-12-26 18:30:57 +00005578 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005579 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00005580 } else if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00005581 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005582 D->setInvalidDecl();
Matt Arsenault43cfcbc2014-12-05 18:03:55 +00005583 } else if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
Aaron Ballman3e424b52013-12-26 18:30:57 +00005584 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00005585 D->setInvalidDecl();
Matt Arsenaultb9e9dc52014-12-05 18:03:58 +00005586 } else if (Attr *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
5587 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
5588 << A << ExpectedKernelFunction;
5589 D->setInvalidDecl();
5590 } else if (Attr *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
5591 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
5592 << A << ExpectedKernelFunction;
5593 D->setInvalidDecl();
Joey Gouly2cd9db12013-12-13 16:15:28 +00005594 }
5595 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00005596}
5597
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005598// Annotation attributes are the only attributes allowed after an access
5599// specifier.
5600bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5601 const AttributeList *AttrList) {
5602 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00005603 if (l->getKind() == AttributeList::AT_Annotate) {
David Majnemer706f3152014-12-14 01:05:01 +00005604 ProcessDeclAttribute(*this, nullptr, ASDecl, *l, l->isCXX11Attribute());
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00005605 } else {
5606 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5607 return true;
5608 }
5609 }
5610
5611 return false;
5612}
5613
John McCall42856de2011-10-01 05:17:03 +00005614/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5615/// contains any decl attributes that we should warn about.
5616static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5617 for ( ; A; A = A->getNext()) {
5618 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00005619 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00005620 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5621
5622 if (A->getKind() == AttributeList::UnknownAttribute) {
5623 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5624 << A->getName() << A->getRange();
5625 } else {
5626 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5627 << A->getName() << A->getRange();
5628 }
5629 }
5630}
5631
5632/// checkUnusedDeclAttributes - Given a declarator which is not being
5633/// used to build a declaration, complain about any decl attributes
5634/// which might be lying around on it.
5635void Sema::checkUnusedDeclAttributes(Declarator &D) {
5636 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5637 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5638 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5639 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5640}
5641
Ryan Flynn7d470f32009-07-30 03:15:39 +00005642/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00005643/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00005644NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5645 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00005646 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00005647 NamedDecl *NewD = nullptr;
Ryan Flynn7d470f32009-07-30 03:15:39 +00005648 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Alexander Kornienko061900f2015-12-03 11:37:28 +00005649 FunctionDecl *NewFD;
5650 // FIXME: Missing call to CheckFunctionDeclaration().
Eli Friedmance3e2c82011-09-07 04:05:06 +00005651 // FIXME: Mangling?
5652 // FIXME: Is the qualifier info correct?
5653 // FIXME: Is the DeclContext correct?
Alexander Kornienko061900f2015-12-03 11:37:28 +00005654 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5655 Loc, Loc, DeclarationName(II),
5656 FD->getType(), FD->getTypeSourceInfo(),
5657 SC_None, false/*isInlineSpecified*/,
5658 FD->hasPrototype(),
5659 false/*isConstexprSpecified*/);
Eli Friedmance3e2c82011-09-07 04:05:06 +00005660 NewD = NewFD;
5661
5662 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00005663 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00005664
5665 // Fake up parameter variables; they are declared as if this were
5666 // a typedef.
5667 QualType FDTy = FD->getType();
5668 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5669 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00005670 for (const auto &AI : FT->param_types()) {
5671 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00005672 Param->setScopeInfo(0, Params.size());
5673 Params.push_back(Param);
5674 }
David Blaikie9c70e042011-09-21 18:16:56 +00005675 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00005676 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005677 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5678 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00005679 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00005680 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005681 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00005682 if (VD->getQualifier()) {
5683 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00005684 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00005685 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005686 }
5687 return NewD;
5688}
5689
James Dennett634962f2012-06-14 21:40:34 +00005690/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00005691/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00005692void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00005693 if (W.getUsed()) return; // only do this once
5694 W.setUsed(true);
5695 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5696 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00005697 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00005698 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
5699 W.getLocation()));
5700 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00005701 WeakTopLevelDecl.push_back(NewD);
5702 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5703 // to insert Decl at TU scope, sorry.
5704 DeclContext *SavedContext = CurContext;
5705 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00005706 NewD->setDeclContext(CurContext);
5707 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00005708 PushOnScopeChains(NewD, S);
5709 CurContext = SavedContext;
5710 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00005711 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005712 }
5713}
5714
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005715void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5716 // It's valid to "forward-declare" #pragma weak, in which case we
5717 // have to do this.
5718 LoadExternalWeakUndeclaredIdentifiers();
5719 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00005720 NamedDecl *ND = nullptr;
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005721 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5722 if (VD->isExternC())
5723 ND = VD;
5724 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5725 if (FD->isExternC())
5726 ND = FD;
5727 if (ND) {
5728 if (IdentifierInfo *Id = ND->getIdentifier()) {
Chandler Carruthf85d9822015-03-26 08:32:49 +00005729 auto I = WeakUndeclaredIdentifiers.find(Id);
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005730 if (I != WeakUndeclaredIdentifiers.end()) {
5731 WeakInfo W = I->second;
5732 DeclApplyPragmaWeak(S, ND, W);
5733 WeakUndeclaredIdentifiers[Id] = W;
5734 }
5735 }
5736 }
5737 }
5738}
5739
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005740/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5741/// it, apply them to D. This is a bit tricky because PD can have attributes
5742/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00005743void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005744 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00005745 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00005746 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005747
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005748 // Walk the declarator structure, applying decl attributes that were in a type
5749 // position to the decl itself. This handles cases like:
5750 // int *__attr__(x)** D;
5751 // when X is a decl attribute.
5752 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5753 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00005754 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005755
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005756 // Finally, apply any attributes on the decl itself.
5757 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00005758 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005759}
John McCall28a6aea2009-11-04 02:18:39 +00005760
John McCall31168b02011-06-15 23:02:42 +00005761/// Is the given declaration allowed to use a forbidden type?
John McCallb61e14e2015-10-27 04:54:50 +00005762/// If so, it'll still be annotated with an attribute that makes it
5763/// illegal to actually use.
5764static bool isForbiddenTypeAllowed(Sema &S, Decl *decl,
5765 const DelayedDiagnostic &diag,
John McCallc6af8c62015-10-28 05:03:19 +00005766 UnavailableAttr::ImplicitReason &reason) {
John McCall31168b02011-06-15 23:02:42 +00005767 // Private ivars are always okay. Unfortunately, people don't
5768 // always properly make their ivars private, even in system headers.
5769 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005770 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5771 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005772 return false;
5773
John McCallc6af8c62015-10-28 05:03:19 +00005774 // Silently accept unsupported uses of __weak in both user and system
5775 // declarations when it's been disabled, for ease of integration with
5776 // -fno-objc-arc files. We do have to take some care against attempts
5777 // to define such things; for now, we've only done that for ivars
5778 // and properties.
5779 if ((isa<ObjCIvarDecl>(decl) || isa<ObjCPropertyDecl>(decl))) {
5780 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
5781 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
5782 reason = UnavailableAttr::IR_ForbiddenWeak;
5783 return true;
5784 }
John McCallb61e14e2015-10-27 04:54:50 +00005785 }
5786
John McCallc6af8c62015-10-28 05:03:19 +00005787 // Allow all sorts of things in system headers.
5788 if (S.Context.getSourceManager().isInSystemHeader(decl->getLocation())) {
5789 // Currently, all the failures dealt with this way are due to ARC
5790 // restrictions.
5791 reason = UnavailableAttr::IR_ARCForbiddenType;
5792 return true;
John McCallb61e14e2015-10-27 04:54:50 +00005793 }
5794
5795 return false;
John McCall31168b02011-06-15 23:02:42 +00005796}
5797
5798/// Handle a delayed forbidden-type diagnostic.
5799static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5800 Decl *decl) {
John McCallc6af8c62015-10-28 05:03:19 +00005801 auto reason = UnavailableAttr::IR_None;
5802 if (decl && isForbiddenTypeAllowed(S, decl, diag, reason)) {
5803 assert(reason && "didn't set reason?");
5804 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", reason,
5805 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00005806 return;
5807 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005808 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005809 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005810 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005811 // kind of forbidden type messages on unavailable functions.
5812 if (FD->hasAttr<UnavailableAttr>() &&
5813 diag.getForbiddenTypeDiagnostic() ==
5814 diag::err_arc_array_param_no_ownership) {
5815 diag.Triggered = true;
5816 return;
5817 }
5818 }
John McCall31168b02011-06-15 23:02:42 +00005819
5820 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5821 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5822 diag.Triggered = true;
5823}
5824
Aaron Ballmanfb237522014-10-15 15:37:51 +00005825
5826static bool isDeclDeprecated(Decl *D) {
5827 do {
5828 if (D->isDeprecated())
5829 return true;
5830 // A category implicitly has the availability of the interface.
5831 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005832 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
5833 return Interface->isDeprecated();
Aaron Ballmanfb237522014-10-15 15:37:51 +00005834 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5835 return false;
5836}
5837
5838static bool isDeclUnavailable(Decl *D) {
5839 do {
5840 if (D->isUnavailable())
5841 return true;
5842 // A category implicitly has the availability of the interface.
5843 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
Ben Langmuirc91ac9e2015-01-20 20:41:36 +00005844 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
5845 return Interface->isUnavailable();
Aaron Ballmanfb237522014-10-15 15:37:51 +00005846 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5847 return false;
5848}
5849
Nico Weber0055a192015-03-19 19:18:22 +00005850static void DoEmitAvailabilityWarning(Sema &S, Sema::AvailabilityDiagnostic K,
Aaron Ballmanfb237522014-10-15 15:37:51 +00005851 Decl *Ctx, const NamedDecl *D,
5852 StringRef Message, SourceLocation Loc,
5853 const ObjCInterfaceDecl *UnknownObjCClass,
5854 const ObjCPropertyDecl *ObjCProperty,
5855 bool ObjCPropertyAccess) {
5856 // Diagnostics for deprecated or unavailable.
5857 unsigned diag, diag_message, diag_fwdclass_message;
John McCallb61e14e2015-10-27 04:54:50 +00005858 unsigned diag_available_here = diag::note_availability_specified_here;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005859
5860 // Matches 'diag::note_property_attribute' options.
5861 unsigned property_note_select;
5862
5863 // Matches diag::note_availability_specified_here.
5864 unsigned available_here_select_kind;
5865
5866 // Don't warn if our current context is deprecated or unavailable.
5867 switch (K) {
Nico Weber0055a192015-03-19 19:18:22 +00005868 case Sema::AD_Deprecation:
Jordan Rosed17c03e2015-04-30 17:20:35 +00005869 if (isDeclDeprecated(Ctx) || isDeclUnavailable(Ctx))
Aaron Ballmanfb237522014-10-15 15:37:51 +00005870 return;
5871 diag = !ObjCPropertyAccess ? diag::warn_deprecated
5872 : diag::warn_property_method_deprecated;
5873 diag_message = diag::warn_deprecated_message;
5874 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
5875 property_note_select = /* deprecated */ 0;
5876 available_here_select_kind = /* deprecated */ 2;
5877 break;
5878
Nico Weber0055a192015-03-19 19:18:22 +00005879 case Sema::AD_Unavailable:
Aaron Ballmanfb237522014-10-15 15:37:51 +00005880 if (isDeclUnavailable(Ctx))
5881 return;
5882 diag = !ObjCPropertyAccess ? diag::err_unavailable
5883 : diag::err_property_method_unavailable;
5884 diag_message = diag::err_unavailable_message;
5885 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
5886 property_note_select = /* unavailable */ 1;
5887 available_here_select_kind = /* unavailable */ 0;
John McCallb61e14e2015-10-27 04:54:50 +00005888
John McCallc6af8c62015-10-28 05:03:19 +00005889 if (auto attr = D->getAttr<UnavailableAttr>()) {
5890 if (attr->isImplicit() && attr->getImplicitReason()) {
5891 // Most of these failures are due to extra restrictions in ARC;
5892 // reflect that in the primary diagnostic when applicable.
5893 auto flagARCError = [&] {
5894 if (S.getLangOpts().ObjCAutoRefCount &&
5895 S.getSourceManager().isInSystemHeader(D->getLocation()))
5896 diag = diag::err_unavailable_in_arc;
5897 };
5898
5899 switch (attr->getImplicitReason()) {
5900 case UnavailableAttr::IR_None: break;
5901
5902 case UnavailableAttr::IR_ARCForbiddenType:
5903 flagARCError();
5904 diag_available_here = diag::note_arc_forbidden_type;
5905 break;
5906
5907 case UnavailableAttr::IR_ForbiddenWeak:
5908 if (S.getLangOpts().ObjCWeakRuntime)
5909 diag_available_here = diag::note_arc_weak_disabled;
5910 else
5911 diag_available_here = diag::note_arc_weak_no_runtime;
5912 break;
5913
5914 case UnavailableAttr::IR_ARCForbiddenConversion:
5915 flagARCError();
5916 diag_available_here = diag::note_performs_forbidden_arc_conversion;
5917 break;
5918
5919 case UnavailableAttr::IR_ARCInitReturnsUnrelated:
5920 flagARCError();
5921 diag_available_here = diag::note_arc_init_returns_unrelated;
5922 break;
5923
5924 case UnavailableAttr::IR_ARCFieldWithOwnership:
5925 flagARCError();
5926 diag_available_here = diag::note_arc_field_with_ownership;
5927 break;
5928 }
5929 }
John McCallb61e14e2015-10-27 04:54:50 +00005930 }
5931
Aaron Ballmanfb237522014-10-15 15:37:51 +00005932 break;
5933
Nico Weber0055a192015-03-19 19:18:22 +00005934 case Sema::AD_Partial:
5935 diag = diag::warn_partial_availability;
5936 diag_message = diag::warn_partial_message;
5937 diag_fwdclass_message = diag::warn_partial_fwdclass_message;
5938 property_note_select = /* partial */ 2;
5939 available_here_select_kind = /* partial */ 3;
5940 break;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005941 }
5942
Aaron Ballmanfb237522014-10-15 15:37:51 +00005943 if (!Message.empty()) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005944 S.Diag(Loc, diag_message) << D << Message;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005945 if (ObjCProperty)
5946 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5947 << ObjCProperty->getDeclName() << property_note_select;
5948 } else if (!UnknownObjCClass) {
Aaron Ballman43f40102014-11-14 22:34:56 +00005949 S.Diag(Loc, diag) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005950 if (ObjCProperty)
5951 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
5952 << ObjCProperty->getDeclName() << property_note_select;
5953 } else {
Aaron Ballman43f40102014-11-14 22:34:56 +00005954 S.Diag(Loc, diag_fwdclass_message) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005955 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5956 }
5957
John McCallb61e14e2015-10-27 04:54:50 +00005958 S.Diag(D->getLocation(), diag_available_here)
Aaron Ballmanfb237522014-10-15 15:37:51 +00005959 << D << available_here_select_kind;
Nico Weber0055a192015-03-19 19:18:22 +00005960 if (K == Sema::AD_Partial)
5961 S.Diag(Loc, diag::note_partial_availability_silence) << D;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005962}
5963
5964static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD,
5965 Decl *Ctx) {
Nico Weber0055a192015-03-19 19:18:22 +00005966 assert(DD.Kind == DelayedDiagnostic::Deprecation ||
5967 DD.Kind == DelayedDiagnostic::Unavailable);
5968 Sema::AvailabilityDiagnostic AD = DD.Kind == DelayedDiagnostic::Deprecation
5969 ? Sema::AD_Deprecation
5970 : Sema::AD_Unavailable;
Aaron Ballmanfb237522014-10-15 15:37:51 +00005971 DD.Triggered = true;
Nico Weber0055a192015-03-19 19:18:22 +00005972 DoEmitAvailabilityWarning(
5973 S, AD, Ctx, DD.getDeprecationDecl(), DD.getDeprecationMessage(), DD.Loc,
5974 DD.getUnknownObjCClass(), DD.getObjCProperty(), false);
Aaron Ballmanfb237522014-10-15 15:37:51 +00005975}
5976
John McCall2ec85372012-05-07 06:16:41 +00005977void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5978 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005979 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005980 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005981
John McCall2ec85372012-05-07 06:16:41 +00005982 // When delaying diagnostics to run in the context of a parsed
5983 // declaration, we only want to actually emit anything if parsing
5984 // succeeds.
5985 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005986
John McCall2ec85372012-05-07 06:16:41 +00005987 // We emit all the active diagnostics in this pool or any of its
5988 // parents. In general, we'll get one pool for the decl spec
5989 // and a child pool for each declarator; in a decl group like:
5990 // deprecated_typedef foo, *bar, baz();
5991 // only the declarator pops will be passed decls. This is correct;
5992 // we really do need to consider delayed diagnostics from the decl spec
5993 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00005994 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00005995 do {
John McCall6347b682012-05-07 06:16:58 +00005996 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00005997 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5998 // This const_cast is a bit lame. Really, Triggered should be mutable.
5999 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00006000 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00006001 continue;
6002
John McCallc1465822011-02-14 07:13:47 +00006003 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00006004 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00006005 case DelayedDiagnostic::Unavailable:
6006 // Don't bother giving deprecation/unavailable diagnostics if
6007 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00006008 if (!decl->isInvalidDecl())
Aaron Ballmanfb237522014-10-15 15:37:51 +00006009 handleDelayedAvailabilityCheck(*this, diag, decl);
John McCall86121512010-01-27 03:50:35 +00006010 break;
6011
6012 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00006013 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00006014 break;
John McCall31168b02011-06-15 23:02:42 +00006015
6016 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00006017 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00006018 break;
John McCall86121512010-01-27 03:50:35 +00006019 }
6020 }
John McCall2ec85372012-05-07 06:16:41 +00006021 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00006022}
6023
John McCall6347b682012-05-07 06:16:58 +00006024/// Given a set of delayed diagnostics, re-emit them as if they had
6025/// been delayed in the current context instead of in the given pool.
6026/// Essentially, this just moves them to the current pool.
6027void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
6028 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
6029 assert(curPool && "re-emitting in undelayed context not supported");
6030 curPool->steal(pool);
6031}
6032
Ted Kremenekb79ee572013-12-18 23:30:06 +00006033void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
6034 NamedDecl *D, StringRef Message,
6035 SourceLocation Loc,
6036 const ObjCInterfaceDecl *UnknownObjCClass,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00006037 const ObjCPropertyDecl *ObjCProperty,
6038 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00006039 // Delay if we're currently parsing a declaration.
Nico Weber0055a192015-03-19 19:18:22 +00006040 if (DelayedDiagnostics.shouldDelayDiagnostics() && AD != AD_Partial) {
Nico Weber462fd1e2015-01-07 23:50:05 +00006041 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(
6042 AD, Loc, D, UnknownObjCClass, ObjCProperty, Message,
6043 ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00006044 return;
6045 }
6046
Ted Kremenekb79ee572013-12-18 23:30:06 +00006047 Decl *Ctx = cast<Decl>(getCurLexicalContext());
Nico Weber0055a192015-03-19 19:18:22 +00006048 DoEmitAvailabilityWarning(*this, AD, Ctx, D, Message, Loc, UnknownObjCClass,
6049 ObjCProperty, ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00006050}