blob: 973b63de309288e927618c7a0ce95496f491e780 [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"
Jordan Rosea7d03842013-02-08 22:30:41 +000023#include "clang/Basic/CharInfo.h"
John McCall31168b02011-06-15 23:02:42 +000024#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000025#include "clang/Basic/TargetInfo.h"
Benjamin Kramer6ee15622013-09-13 15:35:43 +000026#include "clang/Lex/Preprocessor.h"
John McCall8b0666c2010-08-20 18:27:03 +000027#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000028#include "clang/Sema/DelayedDiagnostic.h"
John McCallf1e8b342011-09-29 07:17:38 +000029#include "clang/Sema/Lookup.h"
Richard Smithe233fbf2013-01-28 22:42:45 +000030#include "clang/Sema/Scope.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000031#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000032using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000033using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000034
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000035namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000036 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000037 C,
38 Cpp,
39 ObjC
40 };
41}
42
Chris Lattner58418ff2008-06-29 00:16:31 +000043//===----------------------------------------------------------------------===//
44// Helper functions
45//===----------------------------------------------------------------------===//
46
Ted Kremenek527042b2009-08-14 20:49:40 +000047/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000048/// type (function or function-typed variable) or an Objective-C
49/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000050static bool isFunctionOrMethod(const Decl *D) {
Craig Topperc3ec1492014-05-26 06:22:03 +000051 return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000052}
53
John McCall3882ace2011-01-05 12:14:39 +000054/// Return true if the given decl has a declarator that should have
55/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000056static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000057 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000058 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
59 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +000060}
61
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000062/// hasFunctionProto - Return true if the given decl has a argument
63/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000064/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000065static bool hasFunctionProto(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000066 if (const FunctionType *FnTy = D->getFunctionType())
Douglas Gregordeaad8c2009-02-26 23:50:07 +000067 return isa<FunctionProtoType>(FnTy);
Aaron Ballman12b9f652014-01-16 13:55:42 +000068 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000069}
70
Alp Toker601b22c2014-01-21 23:35:24 +000071/// getFunctionOrMethodNumParams - Return number of function or method
72/// parameters. It is an error to call this on a K&R function (use
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000073/// hasFunctionProto first).
Alp Toker601b22c2014-01-21 23:35:24 +000074static unsigned getFunctionOrMethodNumParams(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000075 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000076 return cast<FunctionProtoType>(FnTy)->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000077 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000078 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000079 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000080}
81
Alp Toker601b22c2014-01-21 23:35:24 +000082static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000083 if (const FunctionType *FnTy = D->getFunctionType())
Alp Toker9cacbab2014-01-20 20:26:09 +000084 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +000085 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +000086 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +000087
Alp Toker03376dc2014-07-07 09:02:20 +000088 return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000089}
90
Chandler Carruthff4c4f02011-07-01 23:49:12 +000091static QualType getFunctionOrMethodResultType(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000092 if (const FunctionType *FnTy = D->getFunctionType())
Aaron Ballman6288d062014-07-11 16:31:29 +000093 return cast<FunctionType>(FnTy)->getReturnType();
Alp Toker314cc812014-01-25 16:55:45 +000094 return cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +000095}
96
Chandler Carruthff4c4f02011-07-01 23:49:12 +000097static bool isFunctionOrMethodVariadic(const Decl *D) {
Aaron Ballman12b9f652014-01-16 13:55:42 +000098 if (const FunctionType *FnTy = D->getFunctionType()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +000099 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000100 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000101 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000102 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000103 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000104 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000105 }
106}
107
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000108static bool isInstanceMethod(const Decl *D) {
109 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000110 return MethodDecl->isInstance();
111 return false;
112}
113
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000114static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000115 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000116 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000117 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000118
John McCall96fa4842010-05-17 21:00:27 +0000119 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
120 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000121 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000122
John McCall96fa4842010-05-17 21:00:27 +0000123 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000124
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000125 // FIXME: Should we walk the chain of classes?
126 return ClsName == &Ctx.Idents.get("NSString") ||
127 ClsName == &Ctx.Idents.get("NSMutableString");
128}
129
Daniel Dunbar980c6692008-09-26 03:32:58 +0000130static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000131 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000132 if (!PT)
133 return false;
134
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000135 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000136 if (!RT)
137 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000138
Daniel Dunbar980c6692008-09-26 03:32:58 +0000139 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000140 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000141 return false;
142
143 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
144}
145
Richard Smithb87c4652013-10-31 21:23:20 +0000146static unsigned getNumAttributeArgs(const AttributeList &Attr) {
147 // FIXME: Include the type in the argument list.
148 return Attr.getNumArgs() + Attr.hasParsedType();
149}
150
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000151/// \brief Check if the attribute has exactly as many args as Num. May
152/// output an error.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000153static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000154 unsigned Num) {
155 if (getNumAttributeArgs(Attr) != Num) {
Aaron Ballmanb7243382013-07-23 19:30:11 +0000156 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
157 << Attr.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000158 return false;
159 }
160
161 return true;
162}
163
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000164/// \brief Check if the attribute has at least as many args as Num. May
165/// output an error.
166static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000167 unsigned Num) {
168 if (getNumAttributeArgs(Attr) < Num) {
Aaron Ballman05e420a2014-01-02 21:26:14 +0000169 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments)
170 << Attr.getName() << Num;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000171 return false;
172 }
173
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000174 return true;
175}
176
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000177/// \brief If Expr is a valid integer constant, get the value of the integer
178/// expression and return success or failure. May output an error.
179static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
180 const Expr *Expr, uint32_t &Val,
181 unsigned Idx = UINT_MAX) {
182 llvm::APSInt I(32);
183 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
184 !Expr->isIntegerConstantExpr(I, S.Context)) {
185 if (Idx != UINT_MAX)
186 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
187 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
188 << Expr->getSourceRange();
189 else
190 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
191 << Attr.getName() << AANT_ArgumentIntegerConstant
192 << Expr->getSourceRange();
193 return false;
194 }
Aaron Ballmanadfdde52014-07-22 14:09:34 +0000195
196 if (!I.isIntN(32)) {
197 S.Diag(Expr->getExprLoc(), diag::err_integer_too_large) << 32;
198 return false;
199 }
200
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000201 Val = (uint32_t)I.getZExtValue();
202 return true;
203}
204
Aaron Ballmanfb763042013-12-02 18:05:46 +0000205/// \brief Diagnose mutually exclusive attributes when present on a given
206/// declaration. Returns true if diagnosed.
207template <typename AttrTy>
208static bool checkAttrMutualExclusion(Sema &S, Decl *D,
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000209 const AttributeList &Attr) {
210 if (AttrTy *A = D->getAttr<AttrTy>()) {
Aaron Ballmanfb763042013-12-02 18:05:46 +0000211 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballman2cfbc002014-01-03 16:23:46 +0000212 << Attr.getName() << A;
Aaron Ballmanfb763042013-12-02 18:05:46 +0000213 return true;
214 }
215 return false;
216}
217
Alp Toker601b22c2014-01-21 23:35:24 +0000218/// \brief Check if IdxExpr is a valid parameter index for a function or
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000219/// instance method D. May output an error.
220///
221/// \returns true if IdxExpr is a valid index.
Alp Toker601b22c2014-01-21 23:35:24 +0000222static bool checkFunctionOrMethodParameterIndex(Sema &S, const Decl *D,
223 const AttributeList &Attr,
224 unsigned AttrArgNum,
225 const Expr *IdxExpr,
226 uint64_t &Idx) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000227 assert(isFunctionOrMethod(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000228
229 // In C++ the implicit 'this' function parameter also counts.
230 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000231 bool HP = hasFunctionProto(D);
232 bool HasImplicitThisParam = isInstanceMethod(D);
233 bool IV = HP && isFunctionOrMethodVariadic(D);
Alp Toker601b22c2014-01-21 23:35:24 +0000234 unsigned NumParams =
235 (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000236
237 llvm::APSInt IdxInt;
238 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
239 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000240 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
241 << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant
242 << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000243 return false;
244 }
245
246 Idx = IdxInt.getLimitedValue();
Alp Toker601b22c2014-01-21 23:35:24 +0000247 if (Idx < 1 || (!IV && Idx > NumParams)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000248 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
249 << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000250 return false;
251 }
252 Idx--; // Convert to zero-based.
253 if (HasImplicitThisParam) {
254 if (Idx == 0) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000255 S.Diag(Attr.getLoc(),
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000256 diag::err_attribute_invalid_implicit_this_argument)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000257 << Attr.getName() << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000258 return false;
259 }
260 --Idx;
261 }
262
263 return true;
264}
265
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000266/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
267/// If not emit an error and return false. If the argument is an identifier it
268/// will emit an error with a fixit hint and treat it as if it was a string
269/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000270bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
271 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000272 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000273 // Look for identifiers. If we have one emit a hint to fix it to a literal.
274 if (Attr.isArgIdent(ArgNum)) {
275 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000276 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000277 << Attr.getName() << AANT_ArgumentString
278 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000279 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000280 Str = Loc->Ident->getName();
281 if (ArgLocation)
282 *ArgLocation = Loc->Loc;
283 return true;
284 }
285
286 // Now check for an actual string literal.
287 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
288 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
289 if (ArgLocation)
290 *ArgLocation = ArgExpr->getLocStart();
291
292 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000293 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000294 << Attr.getName() << AANT_ArgumentString;
295 return false;
296 }
297
298 Str = Literal->getString();
299 return true;
300}
301
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000302/// \brief Applies the given attribute to the Decl without performing any
303/// additional semantic checking.
304template <typename AttrType>
305static void handleSimpleAttribute(Sema &S, Decl *D,
306 const AttributeList &Attr) {
307 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
308 Attr.getAttributeSpellingListIndex()));
309}
310
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000311/// \brief Check if the passed-in expression is of type int or bool.
312static bool isIntOrBool(Expr *Exp) {
313 QualType QT = Exp->getType();
314 return QT->isBooleanType() || QT->isIntegerType();
315}
316
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000317
318// Check to see if the type is a smart pointer of some kind. We assume
319// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000320static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
321 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
322 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000323 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000324 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000325
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000326 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
327 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000328 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000329 return false;
330
331 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000332}
333
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000334/// \brief Check if passed in Decl is a pointer type.
335/// Note that this function may produce an error message.
336/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000337static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
338 const AttributeList &Attr) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000339 const ValueDecl *vd = cast<ValueDecl>(D);
340 QualType QT = vd->getType();
341 if (QT->isAnyPointerType())
342 return true;
343
344 if (const RecordType *RT = QT->getAs<RecordType>()) {
345 // If it's an incomplete type, it could be a smart pointer; skip it.
346 // (We don't want to force template instantiation if we can avoid it,
347 // since that would alter the order in which templates are instantiated.)
348 if (RT->isIncompleteType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000349 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000350
Aaron Ballman553e6812013-12-26 14:54:11 +0000351 if (threadSafetyCheckIsSmartPointer(S, RT))
352 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000353 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000354
355 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Aaron Ballman68289452013-12-26 15:06:01 +0000356 << Attr.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000357 return false;
358}
359
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000360/// \brief Checks that the passed in QualType either is of RecordType or points
361/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000362static const RecordType *getRecordType(QualType QT) {
363 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000364 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000365
366 // Now check if we point to record type.
367 if (const PointerType *PT = QT->getAs<PointerType>())
368 return PT->getPointeeType()->getAs<RecordType>();
369
Craig Topperc3ec1492014-05-26 06:22:03 +0000370 return nullptr;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000371}
372
Aaron Ballman76050722014-04-04 15:13:57 +0000373static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000374 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000375
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000376 if (!RT)
377 return false;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000378
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000379 // Don't check for the capability if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000380 if (RT->isIncompleteType())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000381 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000382
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000383 // Allow smart pointers to be used as capability objects.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000384 // FIXME -- Check the type that the smart pointer points to.
385 if (threadSafetyCheckIsSmartPointer(S, RT))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000386 return true;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000387
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000388 // Check if the record itself has a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000389 RecordDecl *RD = RT->getDecl();
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000390 if (RD->hasAttr<CapabilityAttr>())
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000391 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000392
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000393 // Else check if any base classes have a capability.
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000394 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
395 CXXBasePaths BPaths(false, false);
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000396 if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &P,
397 void *) {
398 return BS->getType()->getAs<RecordType>()
399 ->getDecl()->hasAttr<CapabilityAttr>();
Craig Topperc3ec1492014-05-26 06:22:03 +0000400 }, nullptr, BPaths))
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000401 return true;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000402 }
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000403 return false;
404}
405
Aaron Ballman76050722014-04-04 15:13:57 +0000406static bool checkTypedefTypeForCapability(QualType Ty) {
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000407 const auto *TD = Ty->getAs<TypedefType>();
408 if (!TD)
409 return false;
410
411 TypedefNameDecl *TN = TD->getDecl();
412 if (!TN)
413 return false;
414
415 return TN->hasAttr<CapabilityAttr>();
416}
417
Aaron Ballman76050722014-04-04 15:13:57 +0000418static bool typeHasCapability(Sema &S, QualType Ty) {
419 if (checkTypedefTypeForCapability(Ty))
420 return true;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000421
Aaron Ballman76050722014-04-04 15:13:57 +0000422 if (checkRecordTypeForCapability(S, Ty))
423 return true;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000424
Aaron Ballman76050722014-04-04 15:13:57 +0000425 return false;
426}
427
428static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
429 // Capability expressions are simple expressions involving the boolean logic
430 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
431 // a DeclRefExpr is found, its type should be checked to determine whether it
432 // is a capability or not.
433
434 if (const auto *E = dyn_cast<DeclRefExpr>(Ex))
435 return typeHasCapability(S, E->getType());
436 else if (const auto *E = dyn_cast<CastExpr>(Ex))
437 return isCapabilityExpr(S, E->getSubExpr());
438 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
439 return isCapabilityExpr(S, E->getSubExpr());
440 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
441 if (E->getOpcode() == UO_LNot)
442 return isCapabilityExpr(S, E->getSubExpr());
443 return false;
444 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
445 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
446 return isCapabilityExpr(S, E->getLHS()) &&
447 isCapabilityExpr(S, E->getRHS());
448 return false;
449 }
450
451 return false;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000452}
453
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000454/// \brief Checks that all attribute arguments, starting from Sidx, resolve to
455/// a capability object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000456/// \param Sidx The attribute argument index to start checking with.
457/// \param ParamIdxOk Whether an argument can be indexing into a function
458/// parameter list.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000459static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
460 const AttributeList &Attr,
461 SmallVectorImpl<Expr *> &Args,
462 int Sidx = 0,
463 bool ParamIdxOk = false) {
464 for (unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000465 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000466
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000467 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000468 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000469 Args.push_back(ArgExp);
470 continue;
471 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000472
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000473 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000474 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000475 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000476 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000477 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000478 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000479 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000480 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000481
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000482 // We allow constant strings to be used as a placeholder for expressions
483 // that are not valid C++ syntax, but warn that they are ignored.
484 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
485 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000486 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000487 continue;
488 }
489
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000490 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000491
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000492 // A pointer to member expression of the form &MyClass::mu is treated
493 // specially -- we need to look at the type of the member.
494 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
495 if (UOp->getOpcode() == UO_AddrOf)
496 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
497 if (DRE->getDecl()->isCXXInstanceMember())
498 ArgTy = DRE->getDecl()->getType();
499
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000500 // First see if we can just cast to record type, or pointer to record type.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000501 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000502
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000503 // Now check if we index into a record type function param.
504 if(!RT && ParamIdxOk) {
505 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000506 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
507 if(FD && IL) {
508 unsigned int NumParams = FD->getNumParams();
509 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000510 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
511 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
512 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000513 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
514 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000515 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000516 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000517 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000518 }
519 }
520
Aaron Ballman76050722014-04-04 15:13:57 +0000521 // If the type does not have a capability, see if the components of the
522 // expression have capabilities. This allows for writing C code where the
523 // capability may be on the type, and the expression is a capability
524 // boolean logic expression. Eg) requires_capability(A || B && !C)
525 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
526 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
527 << Attr.getName() << ArgTy;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000528
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000529 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000530 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000531}
532
Chris Lattner58418ff2008-06-29 00:16:31 +0000533//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000534// Attribute Implementations
535//===----------------------------------------------------------------------===//
536
Daniel Dunbar032db472008-07-31 22:40:48 +0000537// FIXME: All this manual attribute parsing code is gross. At the
538// least add some helper functions to check most argument patterns (#
539// and types of args).
540
Michael Hana9171bc2012-08-03 17:40:43 +0000541static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000542 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000543 if (!threadSafetyCheckIsPointer(S, D, Attr))
544 return;
545
Michael Han99315932013-01-24 16:46:58 +0000546 D->addAttr(::new (S.Context)
547 PtGuardedVarAttr(Attr.getRange(), S.Context,
548 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000549}
550
Michael Hana9171bc2012-08-03 17:40:43 +0000551static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
552 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000553 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000554 SmallVector<Expr*, 1> Args;
555 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000556 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000557 unsigned Size = Args.size();
558 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000559 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000560
Michael Han3be3b442012-07-23 18:48:41 +0000561 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000562
Michael Han3be3b442012-07-23 18:48:41 +0000563 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000564}
565
Michael Han3be3b442012-07-23 18:48:41 +0000566static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000567 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000568 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
569 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000570
Aaron Ballman36a53502014-01-16 13:03:14 +0000571 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg,
572 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000573}
574
Michael Hana9171bc2012-08-03 17:40:43 +0000575static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000576 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000577 Expr *Arg = nullptr;
Michael Han3be3b442012-07-23 18:48:41 +0000578 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
579 return;
580
581 if (!threadSafetyCheckIsPointer(S, D, Attr))
582 return;
583
584 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +0000585 S.Context, Arg,
586 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000587}
588
Michael Hana9171bc2012-08-03 17:40:43 +0000589static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
590 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000591 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000592 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000593 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000594
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000595 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000596 QualType QT = cast<ValueDecl>(D)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000597 if (!QT->isDependentType()) {
598 const RecordType *RT = getRecordType(QT);
Aaron Ballmanefe348e2014-02-18 17:36:50 +0000599 if (!RT || !RT->getDecl()->hasAttr<CapabilityAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000600 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000601 << Attr.getName();
602 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000603 }
604 }
605
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000606 // Check that all arguments are lockable objects.
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000607 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000608 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000609 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000610
Michael Han3be3b442012-07-23 18:48:41 +0000611 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000612}
613
Michael Hana9171bc2012-08-03 17:40:43 +0000614static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000615 const AttributeList &Attr) {
616 SmallVector<Expr*, 1> Args;
617 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
618 return;
619
620 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000621 D->addAttr(::new (S.Context)
622 AcquiredAfterAttr(Attr.getRange(), S.Context,
623 StartArg, Args.size(),
624 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000625}
626
Michael Hana9171bc2012-08-03 17:40:43 +0000627static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000628 const AttributeList &Attr) {
629 SmallVector<Expr*, 1> Args;
630 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
631 return;
632
633 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000634 D->addAttr(::new (S.Context)
635 AcquiredBeforeAttr(Attr.getRange(), S.Context,
636 StartArg, Args.size(),
637 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000638}
639
Michael Hana9171bc2012-08-03 17:40:43 +0000640static bool checkLockFunAttrCommon(Sema &S, Decl *D,
641 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000642 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000643 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000644 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000645 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000646
Michael Han3be3b442012-07-23 18:48:41 +0000647 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000648}
649
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000650static void handleAssertSharedLockAttr(Sema &S, Decl *D,
651 const AttributeList &Attr) {
652 SmallVector<Expr*, 1> Args;
653 if (!checkLockFunAttrCommon(S, D, Attr, Args))
654 return;
655
656 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000657 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000658 D->addAttr(::new (S.Context)
659 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
660 Attr.getAttributeSpellingListIndex()));
661}
662
663static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
664 const AttributeList &Attr) {
665 SmallVector<Expr*, 1> Args;
666 if (!checkLockFunAttrCommon(S, D, Attr, Args))
667 return;
668
669 unsigned Size = Args.size();
Craig Topperc3ec1492014-05-26 06:22:03 +0000670 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000671 D->addAttr(::new (S.Context)
672 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
673 StartArg, Size,
674 Attr.getAttributeSpellingListIndex()));
675}
676
677
Michael Hana9171bc2012-08-03 17:40:43 +0000678static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
679 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000680 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000681 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000682 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000683
Aaron Ballman00e99962013-08-31 01:11:41 +0000684 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000685 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000686 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000687 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000688 }
689
690 // check that all arguments are lockable objects
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000691 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000692
Michael Han3be3b442012-07-23 18:48:41 +0000693 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000694}
695
Michael Hana9171bc2012-08-03 17:40:43 +0000696static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000697 const AttributeList &Attr) {
698 SmallVector<Expr*, 2> Args;
699 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
700 return;
701
Michael Han99315932013-01-24 16:46:58 +0000702 D->addAttr(::new (S.Context)
703 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000704 Attr.getArgAsExpr(0),
705 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000706 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000707}
708
Michael Hana9171bc2012-08-03 17:40:43 +0000709static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000710 const AttributeList &Attr) {
711 SmallVector<Expr*, 2> Args;
712 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
713 return;
714
Michael Han99315932013-01-24 16:46:58 +0000715 D->addAttr(::new (S.Context)
716 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000717 Attr.getArgAsExpr(0),
718 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000719 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000720}
721
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000722static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000723 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000724 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000725 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000726 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000727 unsigned Size = Args.size();
728 if (Size == 0)
729 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000730
Michael Han99315932013-01-24 16:46:58 +0000731 D->addAttr(::new (S.Context)
732 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
733 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000734}
735
736static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000737 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000738 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000739 return;
740
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000741 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000742 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +0000743 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000744 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000745 if (Size == 0)
746 return;
747 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000748
Michael Han99315932013-01-24 16:46:58 +0000749 D->addAttr(::new (S.Context)
750 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
751 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000752}
753
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000754static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) {
755 Expr *Cond = Attr.getArgAsExpr(0);
756 if (!Cond->isTypeDependent()) {
757 ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
758 if (Converted.isInvalid())
759 return;
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000760 Cond = Converted.get();
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000761 }
762
763 StringRef Msg;
764 if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg))
765 return;
766
767 SmallVector<PartialDiagnosticAt, 8> Diags;
768 if (!Cond->isValueDependent() &&
769 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
770 Diags)) {
771 S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr);
772 for (int I = 0, N = Diags.size(); I != N; ++I)
773 S.Diag(Diags[I].first, Diags[I].second);
774 return;
775 }
776
777 D->addAttr(::new (S.Context)
778 EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg,
779 Attr.getAttributeSpellingListIndex()));
780}
781
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000782static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000783 ConsumableAttr::ConsumedState DefaultState;
784
785 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000786 IdentifierLoc *IL = Attr.getArgAsIdent(0);
787 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
788 DefaultState)) {
789 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
790 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000791 return;
792 }
David Blaikie16f76d22013-09-06 01:28:43 +0000793 } else {
794 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
795 << Attr.getName() << AANT_ArgumentIdentifier;
796 return;
797 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000798
799 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000800 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000801 Attr.getAttributeSpellingListIndex()));
802}
803
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000804
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000805static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
806 const AttributeList &Attr) {
807 ASTContext &CurrContext = S.getASTContext();
808 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
809
810 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
811 if (!RD->hasAttr<ConsumableAttr>()) {
812 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
813 RD->getNameAsString();
814
815 return false;
816 }
817 }
818
819 return true;
820}
821
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000822
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000823static void handleCallableWhenAttr(Sema &S, Decl *D,
824 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000825 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
826 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000827
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000828 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
829 return;
830
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000831 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
832 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
833 CallableWhenAttr::ConsumedState CallableState;
834
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000835 StringRef StateString;
836 SourceLocation Loc;
837 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
838 return;
839
840 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000841 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000842 S.Diag(Loc, diag::warn_attribute_type_not_supported)
843 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000844 return;
845 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000846
847 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000848 }
849
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000850 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000851 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
852 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000853}
854
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000855
DeLesley Hutchins69391772013-10-17 23:23:53 +0000856static void handleParamTypestateAttr(Sema &S, Decl *D,
857 const AttributeList &Attr) {
858 if (!checkAttributeNumArgs(S, Attr, 1)) return;
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000859
DeLesley Hutchins69391772013-10-17 23:23:53 +0000860 ParamTypestateAttr::ConsumedState ParamState;
861
862 if (Attr.isArgIdent(0)) {
863 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
864 StringRef StateString = Ident->Ident->getName();
865
866 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
867 ParamState)) {
868 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
869 << Attr.getName() << StateString;
870 return;
871 }
872 } else {
873 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
874 Attr.getName() << AANT_ArgumentIdentifier;
875 return;
876 }
877
878 // FIXME: This check is currently being done in the analysis. It can be
879 // enabled here only after the parser propagates attributes at
880 // template specialization definition, not declaration.
881 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
882 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
883 //
884 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
885 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
886 // ReturnType.getAsString();
887 // return;
888 //}
889
890 D->addAttr(::new (S.Context)
891 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
892 Attr.getAttributeSpellingListIndex()));
893}
894
895
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000896static void handleReturnTypestateAttr(Sema &S, Decl *D,
897 const AttributeList &Attr) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000898 if (!checkAttributeNumArgs(S, Attr, 1)) return;
899
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000900 ReturnTypestateAttr::ConsumedState ReturnState;
901
902 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000903 IdentifierLoc *IL = Attr.getArgAsIdent(0);
904 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
905 ReturnState)) {
906 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
907 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000908 return;
909 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000910 } else {
911 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
912 Attr.getName() << AANT_ArgumentIdentifier;
913 return;
914 }
915
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000916 // FIXME: This check is currently being done in the analysis. It can be
917 // enabled here only after the parser propagates attributes at
918 // template specialization definition, not declaration.
919 //QualType ReturnType;
920 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000921 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
922 // ReturnType = Param->getType();
923 //
924 //} else if (const CXXConstructorDecl *Constructor =
925 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000926 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
927 //
928 //} else {
929 //
930 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
931 //}
932 //
933 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
934 //
935 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
936 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
937 // ReturnType.getAsString();
938 // return;
939 //}
940
941 D->addAttr(::new (S.Context)
942 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
943 Attr.getAttributeSpellingListIndex()));
944}
945
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000946
947static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000948 if (!checkAttributeNumArgs(S, Attr, 1))
949 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000950
951 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
952 return;
953
954 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000955 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000956 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
957 StringRef Param = Ident->Ident->getName();
958 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
959 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
960 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000961 return;
962 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000963 } else {
964 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
965 Attr.getName() << AANT_ArgumentIdentifier;
966 return;
967 }
968
969 D->addAttr(::new (S.Context)
970 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
971 Attr.getAttributeSpellingListIndex()));
972}
973
Chris Wailes9385f9f2013-10-29 20:28:41 +0000974static void handleTestTypestateAttr(Sema &S, Decl *D,
975 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000976 if (!checkAttributeNumArgs(S, Attr, 1))
977 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000978
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000979 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
980 return;
981
Chris Wailes9385f9f2013-10-29 20:28:41 +0000982 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000983 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000984 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
985 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +0000986 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000987 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
988 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000989 return;
990 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000991 } else {
992 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
993 Attr.getName() << AANT_ArgumentIdentifier;
994 return;
995 }
996
997 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +0000998 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000999 Attr.getAttributeSpellingListIndex()));
1000}
1001
Chandler Carruthedc2c642011-07-02 00:01:44 +00001002static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1003 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001004 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001005 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001006}
1007
Chandler Carruthedc2c642011-07-02 00:01:44 +00001008static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001009 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Aaron Ballman36a53502014-01-16 13:03:14 +00001010 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context,
1011 Attr.getAttributeSpellingListIndex()));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001012 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001013 // If the alignment is less than or equal to 8 bits, the packed attribute
1014 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001015 if (!FD->getType()->isDependentType() &&
1016 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001017 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001018 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001019 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001020 else
Michael Han99315932013-01-24 16:46:58 +00001021 FD->addAttr(::new (S.Context)
1022 PackedAttr(Attr.getRange(), S.Context,
1023 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001024 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001025 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001026}
1027
Ted Kremenek7fd17232011-09-29 07:02:25 +00001028static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1029 // The IBOutlet/IBOutletCollection attributes only apply to instance
1030 // variables or properties of Objective-C classes. The outlet must also
1031 // have an object reference type.
1032 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1033 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001034 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001035 << Attr.getName() << VD->getType() << 0;
1036 return false;
1037 }
1038 }
1039 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1040 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001041 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001042 << Attr.getName() << PD->getType() << 1;
1043 return false;
1044 }
1045 }
1046 else {
1047 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1048 return false;
1049 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001050
Ted Kremenek7fd17232011-09-29 07:02:25 +00001051 return true;
1052}
1053
Chandler Carruthedc2c642011-07-02 00:01:44 +00001054static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001055 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001056 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001057
Michael Han99315932013-01-24 16:46:58 +00001058 D->addAttr(::new (S.Context)
1059 IBOutletAttr(Attr.getRange(), S.Context,
1060 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001061}
1062
Chandler Carruthedc2c642011-07-02 00:01:44 +00001063static void handleIBOutletCollection(Sema &S, Decl *D,
1064 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001065
1066 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001067 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001068 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1069 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001070 return;
1071 }
1072
Ted Kremenek7fd17232011-09-29 07:02:25 +00001073 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001074 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001075
Richard Smithb1f9a282013-10-31 01:56:18 +00001076 ParsedType PT;
1077
1078 if (Attr.hasParsedType())
1079 PT = Attr.getTypeArg();
1080 else {
1081 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1082 S.getScopeForContext(D->getDeclContext()->getParent()));
1083 if (!PT) {
1084 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1085 return;
1086 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001087 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001088
Craig Topperc3ec1492014-05-26 06:22:03 +00001089 TypeSourceInfo *QTLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00001090 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1091 if (!QTLoc)
1092 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001093
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001094 // Diagnose use of non-object type in iboutletcollection attribute.
1095 // FIXME. Gnu attribute extension ignores use of builtin types in
1096 // attributes. So, __attribute__((iboutletcollection(char))) will be
1097 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001098 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001099 S.Diag(Attr.getLoc(),
1100 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1101 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001102 return;
1103 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001104
Michael Han99315932013-01-24 16:46:58 +00001105 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001106 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001107 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001108}
1109
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001110static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001111 if (const RecordType *UT = T->getAsUnionType())
1112 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1113 RecordDecl *UD = UT->getDecl();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001114 for (const auto *I : UD->fields()) {
1115 QualType QT = I->getType();
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001116 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1117 T = QT;
1118 return;
1119 }
1120 }
1121 }
1122}
1123
Ted Kremenek9aedc152014-01-17 06:24:56 +00001124static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr,
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001125 SourceRange R, bool isReturnValue = false) {
Ted Kremenek9aedc152014-01-17 06:24:56 +00001126 T = T.getNonReferenceType();
1127 possibleTransparentUnionPointerType(T);
1128
1129 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001130 S.Diag(Attr.getLoc(),
1131 isReturnValue ? diag::warn_attribute_return_pointers_only
1132 : diag::warn_attribute_pointers_only)
Ted Kremenek9aedc152014-01-17 06:24:56 +00001133 << Attr.getName() << R;
1134 return false;
1135 }
1136 return true;
1137}
1138
Chandler Carruthedc2c642011-07-02 00:01:44 +00001139static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001140 SmallVector<unsigned, 8> NonNullArgs;
1141 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001142 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001143 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001144 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001145 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001146
1147 // Is the function argument a pointer type?
Ted Kremenek9aedc152014-01-17 06:24:56 +00001148 // FIXME: Should also highlight argument in decl in the diagnostic.
Alp Toker601b22c2014-01-21 23:35:24 +00001149 if (!attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr,
1150 Ex->getSourceRange()))
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001151 continue;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001152
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001153 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001154 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001155
1156 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1157 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001158 if (NonNullArgs.empty()) {
Alp Toker601b22c2014-01-21 23:35:24 +00001159 for (unsigned i = 0, e = getFunctionOrMethodNumParams(D); i != e; ++i) {
1160 QualType T = getFunctionOrMethodParamType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001161 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001162 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001163 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001164 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001165
Ted Kremenek22813f42010-10-21 18:49:36 +00001166 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001167 if (NonNullArgs.empty()) {
1168 // Warn the trivial case only if attribute is not coming from a
1169 // macro instantiation.
1170 if (Attr.getLoc().isFileID())
1171 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001172 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001173 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001174 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001175
Nick Lewyckye1121512013-01-24 01:12:16 +00001176 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001177 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001178 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001179 D->addAttr(::new (S.Context)
1180 NonNullAttr(Attr.getRange(), S.Context, start, size,
1181 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001182}
1183
Jordan Rosec9399072014-02-11 17:27:59 +00001184static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
1185 const AttributeList &Attr) {
1186 if (Attr.getNumArgs() > 0) {
1187 if (D->getFunctionType()) {
1188 handleNonNullAttr(S, D, Attr);
1189 } else {
1190 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1191 << D->getSourceRange();
1192 }
1193 return;
1194 }
1195
1196 // Is the argument a pointer type?
1197 if (!attrNonNullArgCheck(S, D->getType(), Attr, D->getSourceRange()))
1198 return;
1199
1200 D->addAttr(::new (S.Context)
Craig Topperc3ec1492014-05-26 06:22:03 +00001201 NonNullAttr(Attr.getRange(), S.Context, nullptr, 0,
Jordan Rosec9399072014-02-11 17:27:59 +00001202 Attr.getAttributeSpellingListIndex()));
1203}
1204
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001205static void handleReturnsNonNullAttr(Sema &S, Decl *D,
1206 const AttributeList &Attr) {
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00001207 QualType ResultType = getFunctionOrMethodResultType(D);
Ted Kremenekdbf62e32014-01-20 05:50:47 +00001208 if (!attrNonNullArgCheck(S, ResultType, Attr, Attr.getRange(),
1209 /* isReturnValue */ true))
1210 return;
1211
1212 D->addAttr(::new (S.Context)
1213 ReturnsNonNullAttr(Attr.getRange(), S.Context,
1214 Attr.getAttributeSpellingListIndex()));
1215}
1216
Chandler Carruthedc2c642011-07-02 00:01:44 +00001217static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001218 // This attribute must be applied to a function declaration. The first
1219 // argument to the attribute must be an identifier, the name of the resource,
1220 // for example: malloc. The following arguments must be argument indexes, the
1221 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001222 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001223 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001224 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001225
Aaron Ballman00e99962013-08-31 01:11:41 +00001226 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001227 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001228 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001229 return;
1230 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001231
Richard Smith852e9ce2013-11-27 01:46:48 +00001232 // Figure out our Kind.
1233 OwnershipAttr::OwnershipKind K =
Craig Topperc3ec1492014-05-26 06:22:03 +00001234 OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0,
Richard Smith852e9ce2013-11-27 01:46:48 +00001235 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001236
Richard Smith852e9ce2013-11-27 01:46:48 +00001237 // Check arguments.
1238 switch (K) {
1239 case OwnershipAttr::Takes:
1240 case OwnershipAttr::Holds:
1241 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001242 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1243 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001244 return;
1245 }
1246 break;
1247 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001248 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001249 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1250 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001251 return;
1252 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001253 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001254 }
1255
Richard Smith852e9ce2013-11-27 01:46:48 +00001256 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001257
1258 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001259 StringRef ModuleName = Module->getName();
1260 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1261 ModuleName.size() > 4) {
1262 ModuleName = ModuleName.drop_front(2).drop_back(2);
1263 Module = &S.PP.getIdentifierTable().get(ModuleName);
1264 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001265
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001266 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001267 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1268 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001269 uint64_t Idx;
Alp Toker601b22c2014-01-21 23:35:24 +00001270 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001271 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001272
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001273 // Is the function argument a pointer type?
Alp Toker601b22c2014-01-21 23:35:24 +00001274 QualType T = getFunctionOrMethodParamType(D, Idx);
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001275 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001276 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001277 case OwnershipAttr::Takes:
1278 case OwnershipAttr::Holds:
1279 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1280 Err = 0;
1281 break;
1282 case OwnershipAttr::Returns:
1283 if (!T->isIntegerType())
1284 Err = 1;
1285 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001286 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001287 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001288 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001289 << Ex->getSourceRange();
1290 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001291 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001292
1293 // Check we don't have a conflict with another ownership attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001294 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001295 // FIXME: A returns attribute should conflict with any returns attribute
1296 // with a different index too.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001297 if (I->getOwnKind() != K && I->args_end() !=
1298 std::find(I->args_begin(), I->args_end(), Idx)) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001299 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00001300 << AL.getName() << I;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001301 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001302 }
1303 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001304 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001305 }
1306
1307 unsigned* start = OwnershipArgs.data();
1308 unsigned size = OwnershipArgs.size();
1309 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001310
Michael Han99315932013-01-24 16:46:58 +00001311 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001312 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001313 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001314}
1315
Chandler Carruthedc2c642011-07-02 00:01:44 +00001316static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001317 // Check the attribute arguments.
1318 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001319 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1320 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001321 return;
1322 }
1323
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001324 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001325
Rafael Espindolac18086a2010-02-23 22:00:30 +00001326 // gcc rejects
1327 // class c {
1328 // static int a __attribute__((weakref ("v2")));
1329 // static int b() __attribute__((weakref ("f3")));
1330 // };
1331 // and ignores the attributes of
1332 // void f(void) {
1333 // static int a __attribute__((weakref ("v2")));
1334 // }
1335 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001336 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001337 if (!Ctx->isFileContext()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00001338 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context)
1339 << nd;
Sebastian Redl50c68252010-08-31 00:36:30 +00001340 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001341 }
1342
1343 // The GCC manual says
1344 //
1345 // At present, a declaration to which `weakref' is attached can only
1346 // be `static'.
1347 //
1348 // It also says
1349 //
1350 // Without a TARGET,
1351 // given as an argument to `weakref' or to `alias', `weakref' is
1352 // equivalent to `weak'.
1353 //
1354 // gcc 4.4.1 will accept
1355 // int a7 __attribute__((weakref));
1356 // as
1357 // int a7 __attribute__((weak));
1358 // This looks like a bug in gcc. We reject that for now. We should revisit
1359 // it if this behaviour is actually used.
1360
Rafael Espindolac18086a2010-02-23 22:00:30 +00001361 // GCC rejects
1362 // static ((alias ("y"), weakref)).
1363 // Should we? How to check that weakref is before or after alias?
1364
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001365 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1366 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1367 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001368 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001369 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001370 // GCC will accept anything as the argument of weakref. Should we
1371 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001372 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1373 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001374
Michael Han99315932013-01-24 16:46:58 +00001375 D->addAttr(::new (S.Context)
1376 WeakRefAttr(Attr.getRange(), S.Context,
1377 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001378}
1379
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001380static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1381 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001382 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001383 return;
1384
Douglas Gregore8bbc122011-09-02 00:18:52 +00001385 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001386 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1387 return;
1388 }
1389
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001390 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001391
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001392 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001393 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001394}
1395
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001396static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001397 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001398 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001399
Michael Han99315932013-01-24 16:46:58 +00001400 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1401 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001402}
1403
1404static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00001405 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001406 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001407
Michael Han99315932013-01-24 16:46:58 +00001408 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1409 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001410}
1411
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001412static void handleTLSModelAttr(Sema &S, Decl *D,
1413 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001414 StringRef Model;
1415 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001416 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001417 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001418 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001419
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001420 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001421 if (Model != "global-dynamic" && Model != "local-dynamic"
1422 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001423 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001424 return;
1425 }
1426
Michael Han99315932013-01-24 16:46:58 +00001427 D->addAttr(::new (S.Context)
1428 TLSModelAttr(Attr.getRange(), S.Context, Model,
1429 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001430}
1431
Chandler Carruthedc2c642011-07-02 00:01:44 +00001432static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001433 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Alp Toker314cc812014-01-25 16:55:45 +00001434 QualType RetTy = FD->getReturnType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001435 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001436 D->addAttr(::new (S.Context)
1437 MallocAttr(Attr.getRange(), S.Context,
1438 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001439 return;
1440 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001441 }
1442
Ted Kremenek08479ae2009-08-15 00:51:46 +00001443 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001444}
1445
Chandler Carruthedc2c642011-07-02 00:01:44 +00001446static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001447 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001448 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1449 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001450 return;
1451 }
1452
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001453 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1454 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001455}
1456
Chandler Carruthedc2c642011-07-02 00:01:44 +00001457static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001458 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001459
1460 if (S.CheckNoReturnAttr(attr)) return;
1461
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001462 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001463 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001464 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001465 return;
1466 }
1467
Michael Han99315932013-01-24 16:46:58 +00001468 D->addAttr(::new (S.Context)
1469 NoReturnAttr(attr.getRange(), S.Context,
1470 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001471}
1472
1473bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001474 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001475 attr.setInvalid();
1476 return true;
1477 }
1478
1479 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001480}
1481
Chandler Carruthedc2c642011-07-02 00:01:44 +00001482static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1483 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001484
1485 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1486 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001487 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1488 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Craig Topperc3ec1492014-05-26 06:22:03 +00001489 if (!VD || (!VD->getType()->isBlockPointerType() &&
1490 !VD->getType()->isFunctionPointerType())) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001491 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001492 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001493 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001494 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001495 return;
1496 }
1497 }
1498
Michael Han99315932013-01-24 16:46:58 +00001499 D->addAttr(::new (S.Context)
1500 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1501 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001502}
1503
John Thompsoncdb847ba2010-08-09 21:53:52 +00001504// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001505static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001506/*
1507 Returning a Vector Class in Registers
1508
Eric Christopherbc638a82010-12-01 22:13:54 +00001509 According to the PPU ABI specifications, a class with a single member of
1510 vector type is returned in memory when used as the return value of a function.
1511 This results in inefficient code when implementing vector classes. To return
1512 the value in a single vector register, add the vecreturn attribute to the
1513 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001514
1515 Example:
1516
1517 struct Vector
1518 {
1519 __vector float xyzw;
1520 } __attribute__((vecreturn));
1521
1522 Vector Add(Vector lhs, Vector rhs)
1523 {
1524 Vector result;
1525 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1526 return result; // This will be returned in a register
1527 }
1528*/
Aaron Ballman3e424b52013-12-26 18:30:57 +00001529 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1530 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001531 return;
1532 }
1533
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001534 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001535 int count = 0;
1536
1537 if (!isa<CXXRecordDecl>(record)) {
1538 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1539 return;
1540 }
1541
1542 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1543 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1544 return;
1545 }
1546
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001547 for (const auto *I : record->fields()) {
1548 if ((count == 1) || !I->getType()->isVectorType()) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001549 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1550 return;
1551 }
1552 count++;
1553 }
1554
Michael Han99315932013-01-24 16:46:58 +00001555 D->addAttr(::new (S.Context)
1556 VecReturnAttr(Attr.getRange(), S.Context,
1557 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001558}
1559
Richard Smithe233fbf2013-01-28 22:42:45 +00001560static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1561 const AttributeList &Attr) {
1562 if (isa<ParmVarDecl>(D)) {
1563 // [[carries_dependency]] can only be applied to a parameter if it is a
1564 // parameter of a function declaration or lambda.
1565 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1566 S.Diag(Attr.getLoc(),
1567 diag::err_carries_dependency_param_not_function_decl);
1568 return;
1569 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001570 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001571
1572 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1573 Attr.getRange(), S.Context,
1574 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001575}
1576
Chandler Carruthedc2c642011-07-02 00:01:44 +00001577static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001578 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001579 if (VD->hasLocalStorage()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001580 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001581 return;
1582 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001583 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001584 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001585 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001586 return;
1587 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001588
Michael Han99315932013-01-24 16:46:58 +00001589 D->addAttr(::new (S.Context)
1590 UsedAttr(Attr.getRange(), S.Context,
1591 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001592}
1593
Chandler Carruthedc2c642011-07-02 00:01:44 +00001594static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001595 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001596 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001597 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1598 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001599 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001600 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001601
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001602 uint32_t priority = ConstructorAttr::DefaultPriority;
1603 if (Attr.getNumArgs() > 0 &&
1604 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1605 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001606
Michael Han99315932013-01-24 16:46:58 +00001607 D->addAttr(::new (S.Context)
1608 ConstructorAttr(Attr.getRange(), S.Context, priority,
1609 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001610}
1611
Chandler Carruthedc2c642011-07-02 00:01:44 +00001612static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001613 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001614 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001615 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1616 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001617 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001618 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001619
Aaron Ballmanf28e4992014-01-20 15:22:57 +00001620 uint32_t priority = DestructorAttr::DefaultPriority;
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001621 if (Attr.getNumArgs() > 0 &&
1622 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1623 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001624
Michael Han99315932013-01-24 16:46:58 +00001625 D->addAttr(::new (S.Context)
1626 DestructorAttr(Attr.getRange(), S.Context, priority,
1627 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001628}
1629
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001630template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001631static void handleAttrWithMessage(Sema &S, Decl *D,
1632 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001633 unsigned NumArgs = Attr.getNumArgs();
1634 if (NumArgs > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001635 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1636 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001637 return;
1638 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001639
1640 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001641 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001642 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001643 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001644
Michael Han99315932013-01-24 16:46:58 +00001645 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1646 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001647}
1648
Ted Kremenek438f8db2014-02-22 01:06:05 +00001649static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
Ted Kremenek28eace62013-11-23 01:01:34 +00001650 const AttributeList &Attr) {
Ted Kremenek438f8db2014-02-22 01:06:05 +00001651 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
Ted Kremenek27cfe102014-02-21 22:49:04 +00001652 S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition)
1653 << Attr.getName() << Attr.getRange();
1654 return;
1655 }
1656
Ted Kremenek28eace62013-11-23 01:01:34 +00001657 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001658 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1659 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001660}
1661
Jordy Rose740b0c22012-05-08 03:27:22 +00001662static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1663 IdentifierInfo *Platform,
1664 VersionTuple Introduced,
1665 VersionTuple Deprecated,
1666 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001667 StringRef PlatformName
1668 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1669 if (PlatformName.empty())
1670 PlatformName = Platform->getName();
1671
1672 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1673 // of these steps are needed).
1674 if (!Introduced.empty() && !Deprecated.empty() &&
1675 !(Introduced <= Deprecated)) {
1676 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1677 << 1 << PlatformName << Deprecated.getAsString()
1678 << 0 << Introduced.getAsString();
1679 return true;
1680 }
1681
1682 if (!Introduced.empty() && !Obsoleted.empty() &&
1683 !(Introduced <= Obsoleted)) {
1684 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1685 << 2 << PlatformName << Obsoleted.getAsString()
1686 << 0 << Introduced.getAsString();
1687 return true;
1688 }
1689
1690 if (!Deprecated.empty() && !Obsoleted.empty() &&
1691 !(Deprecated <= Obsoleted)) {
1692 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1693 << 2 << PlatformName << Obsoleted.getAsString()
1694 << 1 << Deprecated.getAsString();
1695 return true;
1696 }
1697
1698 return false;
1699}
1700
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001701/// \brief Check whether the two versions match.
1702///
1703/// If either version tuple is empty, then they are assumed to match. If
1704/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1705static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1706 bool BeforeIsOkay) {
1707 if (X.empty() || Y.empty())
1708 return true;
1709
1710 if (X == Y)
1711 return true;
1712
1713 if (BeforeIsOkay && X < Y)
1714 return true;
1715
1716 return false;
1717}
1718
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001719AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001720 IdentifierInfo *Platform,
1721 VersionTuple Introduced,
1722 VersionTuple Deprecated,
1723 VersionTuple Obsoleted,
1724 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001725 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001726 bool Override,
1727 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001728 VersionTuple MergedIntroduced = Introduced;
1729 VersionTuple MergedDeprecated = Deprecated;
1730 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001731 bool FoundAny = false;
1732
Rafael Espindolac67f2232012-05-10 02:50:16 +00001733 if (D->hasAttrs()) {
1734 AttrVec &Attrs = D->getAttrs();
1735 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1736 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1737 if (!OldAA) {
1738 ++i;
1739 continue;
1740 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001741
Rafael Espindolac67f2232012-05-10 02:50:16 +00001742 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1743 if (OldPlatform != Platform) {
1744 ++i;
1745 continue;
1746 }
1747
1748 FoundAny = true;
1749 VersionTuple OldIntroduced = OldAA->getIntroduced();
1750 VersionTuple OldDeprecated = OldAA->getDeprecated();
1751 VersionTuple OldObsoleted = OldAA->getObsoleted();
1752 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001753
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001754 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1755 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1756 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1757 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001758 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001759 if (Override) {
1760 int Which = -1;
1761 VersionTuple FirstVersion;
1762 VersionTuple SecondVersion;
1763 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1764 Which = 0;
1765 FirstVersion = OldIntroduced;
1766 SecondVersion = Introduced;
1767 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1768 Which = 1;
1769 FirstVersion = Deprecated;
1770 SecondVersion = OldDeprecated;
1771 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1772 Which = 2;
1773 FirstVersion = Obsoleted;
1774 SecondVersion = OldObsoleted;
1775 }
1776
1777 if (Which == -1) {
1778 Diag(OldAA->getLocation(),
1779 diag::warn_mismatched_availability_override_unavail)
1780 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1781 } else {
1782 Diag(OldAA->getLocation(),
1783 diag::warn_mismatched_availability_override)
1784 << Which
1785 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1786 << FirstVersion.getAsString() << SecondVersion.getAsString();
1787 }
1788 Diag(Range.getBegin(), diag::note_overridden_method);
1789 } else {
1790 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1791 Diag(Range.getBegin(), diag::note_previous_attribute);
1792 }
1793
Rafael Espindolac67f2232012-05-10 02:50:16 +00001794 Attrs.erase(Attrs.begin() + i);
1795 --e;
1796 continue;
1797 }
1798
1799 VersionTuple MergedIntroduced2 = MergedIntroduced;
1800 VersionTuple MergedDeprecated2 = MergedDeprecated;
1801 VersionTuple MergedObsoleted2 = MergedObsoleted;
1802
1803 if (MergedIntroduced2.empty())
1804 MergedIntroduced2 = OldIntroduced;
1805 if (MergedDeprecated2.empty())
1806 MergedDeprecated2 = OldDeprecated;
1807 if (MergedObsoleted2.empty())
1808 MergedObsoleted2 = OldObsoleted;
1809
1810 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1811 MergedIntroduced2, MergedDeprecated2,
1812 MergedObsoleted2)) {
1813 Attrs.erase(Attrs.begin() + i);
1814 --e;
1815 continue;
1816 }
1817
1818 MergedIntroduced = MergedIntroduced2;
1819 MergedDeprecated = MergedDeprecated2;
1820 MergedObsoleted = MergedObsoleted2;
1821 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001822 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001823 }
1824
1825 if (FoundAny &&
1826 MergedIntroduced == Introduced &&
1827 MergedDeprecated == Deprecated &&
1828 MergedObsoleted == Obsoleted)
Craig Topperc3ec1492014-05-26 06:22:03 +00001829 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001830
Ted Kremenekb5445722013-04-06 00:34:27 +00001831 // Only create a new attribute if !Override, but we want to do
1832 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001833 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001834 MergedDeprecated, MergedObsoleted) &&
1835 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001836 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1837 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001838 Obsoleted, IsUnavailable, Message,
1839 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001840 }
Craig Topperc3ec1492014-05-26 06:22:03 +00001841 return nullptr;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001842}
1843
Chandler Carruthedc2c642011-07-02 00:01:44 +00001844static void handleAvailabilityAttr(Sema &S, Decl *D,
1845 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001846 if (!checkAttributeNumArgs(S, Attr, 1))
1847 return;
1848 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001849 unsigned Index = Attr.getAttributeSpellingListIndex();
1850
Aaron Ballman00e99962013-08-31 01:11:41 +00001851 IdentifierInfo *II = Platform->Ident;
1852 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1853 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1854 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001855
Rafael Espindolac231fab2013-01-08 21:30:32 +00001856 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1857 if (!ND) {
1858 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1859 return;
1860 }
1861
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001862 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1863 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1864 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001865 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001866 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001867 if (const StringLiteral *SE =
1868 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001869 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001870
Aaron Ballman00e99962013-08-31 01:11:41 +00001871 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001872 Introduced.Version,
1873 Deprecated.Version,
1874 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001875 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001876 /*Override=*/false,
1877 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001878 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001879 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001880}
1881
John McCalld041a9b2013-02-20 01:54:26 +00001882template <class T>
1883static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1884 typename T::VisibilityType value,
1885 unsigned attrSpellingListIndex) {
1886 T *existingAttr = D->getAttr<T>();
1887 if (existingAttr) {
1888 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1889 if (existingValue == value)
Craig Topperc3ec1492014-05-26 06:22:03 +00001890 return nullptr;
John McCalld041a9b2013-02-20 01:54:26 +00001891 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1892 S.Diag(range.getBegin(), diag::note_previous_attribute);
1893 D->dropAttr<T>();
1894 }
1895 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1896}
1897
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001898VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001899 VisibilityAttr::VisibilityType Vis,
1900 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001901 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1902 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001903}
1904
John McCalld041a9b2013-02-20 01:54:26 +00001905TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1906 TypeVisibilityAttr::VisibilityType Vis,
1907 unsigned AttrSpellingListIndex) {
1908 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1909 AttrSpellingListIndex);
1910}
1911
1912static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1913 bool isTypeVisibility) {
1914 // Visibility attributes don't mean anything on a typedef.
1915 if (isa<TypedefNameDecl>(D)) {
1916 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
1917 << Attr.getName();
1918 return;
1919 }
1920
1921 // 'type_visibility' can only go on a type or namespace.
1922 if (isTypeVisibility &&
1923 !(isa<TagDecl>(D) ||
1924 isa<ObjCInterfaceDecl>(D) ||
1925 isa<NamespaceDecl>(D))) {
1926 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
1927 << Attr.getName() << ExpectedTypeOrNamespace;
1928 return;
1929 }
1930
Benjamin Kramer70370212013-09-09 15:08:57 +00001931 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001932 StringRef TypeStr;
1933 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001934 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001935 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001936
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001937 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00001938 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001939 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00001940 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001941 return;
1942 }
Aaron Ballman682ee422013-09-11 19:47:58 +00001943
1944 // Complain about attempts to use protected visibility on targets
1945 // (like Darwin) that don't support it.
1946 if (type == VisibilityAttr::Protected &&
1947 !S.Context.getTargetInfo().hasProtectedVisibility()) {
1948 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1949 type = VisibilityAttr::Default;
1950 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001951
Michael Han99315932013-01-24 16:46:58 +00001952 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00001953 clang::Attr *newAttr;
1954 if (isTypeVisibility) {
1955 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
1956 (TypeVisibilityAttr::VisibilityType) type,
1957 Index);
1958 } else {
1959 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
1960 }
1961 if (newAttr)
1962 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001963}
1964
Chandler Carruthedc2c642011-07-02 00:01:44 +00001965static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1966 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001967 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00001968 if (!Attr.isArgIdent(0)) {
1969 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
1970 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00001971 return;
1972 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001973
Aaron Ballman682ee422013-09-11 19:47:58 +00001974 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1975 ObjCMethodFamilyAttr::FamilyKind F;
1976 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
1977 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
1978 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00001979 return;
1980 }
1981
Alp Toker314cc812014-01-25 16:55:45 +00001982 if (F == ObjCMethodFamilyAttr::OMF_init &&
1983 !method->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00001984 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
Alp Toker314cc812014-01-25 16:55:45 +00001985 << method->getReturnType();
John McCall31168b02011-06-15 23:02:42 +00001986 // Ignore the attribute.
1987 return;
1988 }
1989
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001990 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman36a53502014-01-16 13:03:14 +00001991 S.Context, F,
1992 Attr.getAttributeSpellingListIndex()));
John McCall86bc21f2011-03-02 11:33:24 +00001993}
1994
Chandler Carruthedc2c642011-07-02 00:01:44 +00001995static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00001996 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001997 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00001998 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001999 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2000 return;
2001 }
2002 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002003 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2004 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002005 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002006 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2007 return;
2008 }
2009 }
2010 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002011 // It is okay to include this attribute on properties, e.g.:
2012 //
2013 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2014 //
2015 // In this case it follows tradition and suppresses an error in the above
2016 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002017 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002018 }
Michael Han99315932013-01-24 16:46:58 +00002019 D->addAttr(::new (S.Context)
2020 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2021 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002022}
2023
Chandler Carruthedc2c642011-07-02 00:01:44 +00002024static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002025 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002026 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002027 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002028 return;
2029 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002030
Aaron Ballman00e99962013-08-31 01:11:41 +00002031 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002032 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002033 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2034 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2035 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002036 return;
2037 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002038
Michael Han99315932013-01-24 16:46:58 +00002039 D->addAttr(::new (S.Context)
2040 BlocksAttr(Attr.getRange(), S.Context, type,
2041 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002042}
2043
Chandler Carruthedc2c642011-07-02 00:01:44 +00002044static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002045 // check the attribute arguments.
2046 if (Attr.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00002047 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
2048 << Attr.getName() << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002049 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002050 }
2051
Aaron Ballman18a78382013-11-21 00:28:23 +00002052 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002053 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002054 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002055 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002056 if (E->isTypeDependent() || E->isValueDependent() ||
2057 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002058 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002059 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002060 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002061 return;
2062 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002063
John McCallb46f2872011-09-09 07:56:05 +00002064 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002065 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2066 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002067 return;
2068 }
John McCallb46f2872011-09-09 07:56:05 +00002069
2070 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002071 }
2072
Aaron Ballman18a78382013-11-21 00:28:23 +00002073 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002074 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002075 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002076 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002077 if (E->isTypeDependent() || E->isValueDependent() ||
2078 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002079 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002080 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002081 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002082 return;
2083 }
2084 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002085
John McCallb46f2872011-09-09 07:56:05 +00002086 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002087 // FIXME: This error message could be improved, it would be nice
2088 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002089 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2090 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002091 return;
2092 }
2093 }
2094
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002095 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002096 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002097 if (isa<FunctionNoProtoType>(FT)) {
2098 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2099 return;
2100 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002101
Chris Lattner9363e312009-03-17 23:03:47 +00002102 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002103 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002104 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002105 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002106 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002107 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002108 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002109 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002110 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002111 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2112 if (!BD->isVariadic()) {
2113 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2114 return;
2115 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002116 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002117 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002118 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002119 const FunctionType *FT = Ty->isFunctionPointerType()
2120 ? D->getFunctionType()
Eric Christopherbc638a82010-12-01 22:13:54 +00002121 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002122 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002123 int m = Ty->isFunctionPointerType() ? 0 : 1;
2124 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002125 return;
2126 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002127 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002128 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002129 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002130 return;
2131 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002132 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002133 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002134 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002135 return;
2136 }
Michael Han99315932013-01-24 16:46:58 +00002137 D->addAttr(::new (S.Context)
2138 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2139 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002140}
2141
Chandler Carruthedc2c642011-07-02 00:01:44 +00002142static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Alp Toker314cc812014-01-25 16:55:45 +00002143 if (D->getFunctionType() &&
2144 D->getFunctionType()->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002145 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2146 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002147 return;
2148 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002149 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00002150 if (MD->getReturnType()->isVoidType()) {
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002151 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2152 << Attr.getName() << 1;
2153 return;
2154 }
2155
Michael Han99315932013-01-24 16:46:58 +00002156 D->addAttr(::new (S.Context)
2157 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2158 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002159}
2160
Chandler Carruthedc2c642011-07-02 00:01:44 +00002161static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002162 // weak_import only applies to variable & function declarations.
2163 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002164 if (!D->canBeWeakImported(isDef)) {
2165 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002166 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2167 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002168 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002169 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002170 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002171 // Nothing to warn about here.
2172 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002173 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002174 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002175
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002176 return;
2177 }
2178
Michael Han99315932013-01-24 16:46:58 +00002179 D->addAttr(::new (S.Context)
2180 WeakImportAttr(Attr.getRange(), S.Context,
2181 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002182}
2183
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002184// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002185template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002186static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002187 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002188 uint32_t WGSize[3];
Joey Goulyb1d23a82014-05-19 14:41:38 +00002189 for (unsigned i = 0; i < 3; ++i) {
2190 const Expr *E = Attr.getArgAsExpr(i);
2191 if (!checkUInt32Argument(S, Attr, E, WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002192 return;
Joey Goulyb1d23a82014-05-19 14:41:38 +00002193 if (WGSize[i] == 0) {
2194 S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero)
2195 << Attr.getName() << E->getSourceRange();
2196 return;
2197 }
2198 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002199
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002200 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2201 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2202 Existing->getYDim() == WGSize[1] &&
2203 Existing->getZDim() == WGSize[2]))
2204 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002205
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002206 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2207 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002208 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002209}
2210
Joey Goulyaba589c2013-03-08 09:42:32 +00002211static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002212 if (!Attr.hasParsedType()) {
2213 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2214 << Attr.getName() << 1;
2215 return;
2216 }
2217
Craig Topperc3ec1492014-05-26 06:22:03 +00002218 TypeSourceInfo *ParmTSI = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00002219 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2220 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002221
2222 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2223 (ParmType->isBooleanType() ||
2224 !ParmType->isIntegralType(S.getASTContext()))) {
2225 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2226 << ParmType;
2227 return;
2228 }
2229
Aaron Ballmana9e05402013-12-02 22:16:55 +00002230 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002231 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002232 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2233 return;
2234 }
2235 }
2236
2237 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Aaron Ballman36a53502014-01-16 13:03:14 +00002238 ParmTSI,
2239 Attr.getAttributeSpellingListIndex()));
Joey Goulyaba589c2013-03-08 09:42:32 +00002240}
2241
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002242SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002243 StringRef Name,
2244 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002245 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2246 if (ExistingAttr->getName() == Name)
Craig Topperc3ec1492014-05-26 06:22:03 +00002247 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002248 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2249 Diag(Range.getBegin(), diag::note_previous_attribute);
Craig Topperc3ec1492014-05-26 06:22:03 +00002250 return nullptr;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002251 }
Michael Han99315932013-01-24 16:46:58 +00002252 return ::new (Context) SectionAttr(Range, Context, Name,
2253 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002254}
2255
Chandler Carruthedc2c642011-07-02 00:01:44 +00002256static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002257 // Make sure that there is a string literal as the sections's single
2258 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002259 StringRef Str;
2260 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002261 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002262 return;
Mike Stump11289f42009-09-09 15:08:12 +00002263
Chris Lattner30ba6742009-08-10 19:03:04 +00002264 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002265 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002266 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002267 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002268 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002269 return;
2270 }
Mike Stump11289f42009-09-09 15:08:12 +00002271
Michael Han99315932013-01-24 16:46:58 +00002272 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002273 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002274 if (NewAttr)
2275 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002276}
2277
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002278
Chandler Carruthedc2c642011-07-02 00:01:44 +00002279static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002280 VarDecl *VD = cast<VarDecl>(D);
2281 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002282 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002283 return;
2284 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002285
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002286 Expr *E = Attr.getArgAsExpr(0);
2287 SourceLocation Loc = E->getExprLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00002288 FunctionDecl *FD = nullptr;
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002289 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002290
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002291 // gcc only allows for simple identifiers. Since we support more than gcc, we
2292 // will warn the user.
2293 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2294 if (DRE->hasQualifier())
2295 S.Diag(Loc, diag::warn_cleanup_ext);
2296 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2297 NI = DRE->getNameInfo();
2298 if (!FD) {
2299 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2300 << NI.getName();
2301 return;
2302 }
2303 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2304 if (ULE->hasExplicitTemplateArgs())
2305 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002306 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2307 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002308 if (!FD) {
2309 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2310 << NI.getName();
2311 if (ULE->getType() == S.Context.OverloadTy)
2312 S.NoteAllOverloadCandidates(ULE);
2313 return;
2314 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002315 } else {
2316 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002317 return;
2318 }
2319
Anders Carlssond277d792009-01-31 01:16:18 +00002320 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002321 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2322 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002323 return;
2324 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002325
Anders Carlsson723f55d2009-02-07 23:16:50 +00002326 // We're currently more strict than GCC about what function types we accept.
2327 // If this ever proves to be a problem it should be easy to fix.
2328 QualType Ty = S.Context.getPointerType(VD->getType());
2329 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002330 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2331 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002332 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2333 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002334 return;
2335 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002336
Michael Han99315932013-01-24 16:46:58 +00002337 D->addAttr(::new (S.Context)
2338 CleanupAttr(Attr.getRange(), S.Context, FD,
2339 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002340}
2341
Mike Stumpd3bb5572009-07-24 19:02:52 +00002342/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002343/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002344static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002345 Expr *IdxExpr = Attr.getArgAsExpr(0);
Alp Toker601b22c2014-01-21 23:35:24 +00002346 uint64_t Idx;
2347 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002348 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002349
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002350 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002351 QualType Ty = getFunctionOrMethodParamType(D, Idx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002352
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002353 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2354 if (not_nsstring_type &&
2355 !isCFStringType(Ty, S.Context) &&
2356 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002357 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002358 // FIXME: Should highlight the actual expression that has the wrong type.
2359 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002360 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002361 << IdxExpr->getSourceRange();
2362 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002363 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002364 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002365 if (!isNSStringType(Ty, S.Context) &&
2366 !isCFStringType(Ty, S.Context) &&
2367 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002368 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002369 // FIXME: Should highlight the actual expression that has the wrong type.
2370 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002371 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002372 << IdxExpr->getSourceRange();
2373 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002374 }
2375
Alp Toker601b22c2014-01-21 23:35:24 +00002376 // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002377 // because that has corrected for the implicit this parameter, and is zero-
2378 // based. The attribute expects what the user wrote explicitly.
2379 llvm::APSInt Val;
2380 IdxExpr->EvaluateAsInt(Val, S.Context);
2381
Michael Han99315932013-01-24 16:46:58 +00002382 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002383 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002384 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002385}
2386
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002387enum FormatAttrKind {
2388 CFStringFormat,
2389 NSStringFormat,
2390 StrftimeFormat,
2391 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002392 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002393 InvalidFormat
2394};
2395
2396/// getFormatAttrKind - Map from format attribute names to supported format
2397/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002398static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002399 return llvm::StringSwitch<FormatAttrKind>(Format)
2400 // Check for formats that get handled specially.
2401 .Case("NSString", NSStringFormat)
2402 .Case("CFString", CFStringFormat)
2403 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002404
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002405 // Otherwise, check for supported formats.
2406 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2407 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2408 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002409
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002410 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2411 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002412}
2413
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002414/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002415/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002416static void handleInitPriorityAttr(Sema &S, Decl *D,
2417 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002418 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002419 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2420 return;
2421 }
2422
Aaron Ballman4a611152013-11-27 16:34:09 +00002423 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002424 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2425 Attr.setInvalid();
2426 return;
2427 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002428 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002429 if (S.Context.getAsArrayType(T))
2430 T = S.Context.getBaseElementType(T);
2431 if (!T->getAs<RecordType>()) {
2432 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2433 Attr.setInvalid();
2434 return;
2435 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002436
2437 Expr *E = Attr.getArgAsExpr(0);
2438 uint32_t prioritynum;
2439 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002440 Attr.setInvalid();
2441 return;
2442 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002443
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002444 if (prioritynum < 101 || prioritynum > 65535) {
2445 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002446 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002447 Attr.setInvalid();
2448 return;
2449 }
Michael Han99315932013-01-24 16:46:58 +00002450 D->addAttr(::new (S.Context)
2451 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2452 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002453}
2454
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002455FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2456 IdentifierInfo *Format, int FormatIdx,
2457 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002458 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002459 // Check whether we already have an equivalent format attribute.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002460 for (auto *F : D->specific_attrs<FormatAttr>()) {
2461 if (F->getType() == Format &&
2462 F->getFormatIdx() == FormatIdx &&
2463 F->getFirstArg() == FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002464 // If we don't have a valid location for this attribute, adopt the
2465 // location.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002466 if (F->getLocation().isInvalid())
2467 F->setRange(Range);
Craig Topperc3ec1492014-05-26 06:22:03 +00002468 return nullptr;
Rafael Espindola92d49452012-05-11 00:36:07 +00002469 }
2470 }
2471
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002472 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2473 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002474}
2475
Mike Stumpd3bb5572009-07-24 19:02:52 +00002476/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002477/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002478static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002479 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002480 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002481 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002482 return;
2483 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002484
Chandler Carruth743682b2010-11-16 08:35:43 +00002485 // In C++ the implicit 'this' function parameter also counts, and they are
2486 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002487 bool HasImplicitThisParam = isInstanceMethod(D);
Alp Toker601b22c2014-01-21 23:35:24 +00002488 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002489
Aaron Ballman00e99962013-08-31 01:11:41 +00002490 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2491 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002492
2493 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002494 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002495 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002496 // If we've modified the string name, we need a new identifier for it.
2497 II = &S.Context.Idents.get(Format);
2498 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002499
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002500 // Check for supported formats.
2501 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002502
2503 if (Kind == IgnoredFormat)
2504 return;
2505
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002506 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002507 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002508 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002509 return;
2510 }
2511
2512 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002513 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002514 uint32_t Idx;
2515 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002516 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002517
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002518 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002519 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002520 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002521 return;
2522 }
2523
2524 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002525 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002526
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002527 if (HasImplicitThisParam) {
2528 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002529 S.Diag(Attr.getLoc(),
2530 diag::err_format_attribute_implicit_this_format_string)
2531 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002532 return;
2533 }
2534 ArgIdx--;
2535 }
Mike Stump11289f42009-09-09 15:08:12 +00002536
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002537 // make sure the format string is really a string
Alp Toker601b22c2014-01-21 23:35:24 +00002538 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002539
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002540 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002541 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002542 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2543 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002544 return;
2545 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002546 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002547 // FIXME: do we need to check if the type is NSString*? What are the
2548 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002549 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002550 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002551 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2552 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002553 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002554 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002555 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002556 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002557 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002558 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2559 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002560 return;
2561 }
2562
2563 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002564 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002565 uint32_t FirstArg;
2566 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002567 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002568
2569 // check if the function is variadic if the 3rd argument non-zero
2570 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002571 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002572 ++NumArgs; // +1 for ...
2573 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002574 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002575 return;
2576 }
2577 }
2578
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002579 // strftime requires FirstArg to be 0 because it doesn't read from any
2580 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002581 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002582 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002583 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2584 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002585 return;
2586 }
2587 // if 0 it disables parameter checking (to use with e.g. va_list)
2588 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002589 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002590 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002591 return;
2592 }
2593
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002594 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002595 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002596 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002597 if (NewAttr)
2598 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002599}
2600
Chandler Carruthedc2c642011-07-02 00:01:44 +00002601static void handleTransparentUnionAttr(Sema &S, Decl *D,
2602 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002603 // Try to find the underlying union declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00002604 RecordDecl *RD = nullptr;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002605 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002606 if (TD && TD->getUnderlyingType()->isUnionType())
2607 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2608 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002609 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002610
2611 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002612 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002613 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002614 return;
2615 }
2616
John McCallf937c022011-10-07 06:10:15 +00002617 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002618 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002619 diag::warn_transparent_union_attribute_not_definition);
2620 return;
2621 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002622
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002623 RecordDecl::field_iterator Field = RD->field_begin(),
2624 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002625 if (Field == FieldEnd) {
2626 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2627 return;
2628 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002629
David Blaikie40ed2972012-06-06 20:45:41 +00002630 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002631 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002632 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002633 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002634 diag::warn_transparent_union_attribute_floating)
2635 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002636 return;
2637 }
2638
2639 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2640 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2641 for (; Field != FieldEnd; ++Field) {
2642 QualType FieldType = Field->getType();
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002643 // FIXME: this isn't fully correct; we also need to test whether the
2644 // members of the union would all have the same calling convention as the
2645 // first member of the union. Checking just the size and alignment isn't
2646 // sufficient (consider structs passed on the stack instead of in registers
2647 // as an example).
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002648 if (S.Context.getTypeSize(FieldType) != FirstSize ||
Aaron Ballman54fe5eb2014-01-28 01:47:34 +00002649 S.Context.getTypeAlign(FieldType) > FirstAlign) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002650 // Warn if we drop the attribute.
2651 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002652 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002653 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002654 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002655 diag::warn_transparent_union_attribute_field_size_align)
2656 << isSize << Field->getDeclName() << FieldBits;
2657 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002658 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002659 diag::note_transparent_union_first_field_size_align)
2660 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002661 return;
2662 }
2663 }
2664
Michael Han99315932013-01-24 16:46:58 +00002665 RD->addAttr(::new (S.Context)
2666 TransparentUnionAttr(Attr.getRange(), S.Context,
2667 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002668}
2669
Chandler Carruthedc2c642011-07-02 00:01:44 +00002670static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002671 // Make sure that there is a string literal as the annotation's single
2672 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002673 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002674 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002675 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002676
2677 // Don't duplicate annotations that are already set.
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002678 for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
2679 if (I->getAnnotation() == Str)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002680 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002681 }
Michael Han99315932013-01-24 16:46:58 +00002682
2683 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002684 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002685 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002686}
2687
Chandler Carruthedc2c642011-07-02 00:01:44 +00002688static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002689 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002690 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002691 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2692 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002693 return;
2694 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002695
Richard Smith848e1f12013-02-01 08:12:08 +00002696 if (Attr.getNumArgs() == 0) {
2697 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
Craig Topperc3ec1492014-05-26 06:22:03 +00002698 true, nullptr, Attr.getAttributeSpellingListIndex()));
Richard Smith848e1f12013-02-01 08:12:08 +00002699 return;
2700 }
2701
Aaron Ballman00e99962013-08-31 01:11:41 +00002702 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002703 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2704 S.Diag(Attr.getEllipsisLoc(),
2705 diag::err_pack_expansion_without_parameter_packs);
2706 return;
2707 }
2708
2709 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2710 return;
2711
2712 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2713 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002714}
2715
2716void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002717 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002718 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2719 SourceLocation AttrLoc = AttrRange.getBegin();
2720
Richard Smith1dba27c2013-01-29 09:02:09 +00002721 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002722 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002723 // C++11 [dcl.align]p1:
2724 // An alignment-specifier may be applied to a variable or to a class
2725 // data member, but it shall not be applied to a bit-field, a function
2726 // parameter, the formal parameter of a catch clause, or a variable
2727 // declared with the register storage class specifier. An
2728 // alignment-specifier may also be applied to the declaration of a class
2729 // or enumeration type.
2730 // C11 6.7.5/2:
2731 // An alignment attribute shall not be specified in a declaration of
2732 // a typedef, or a bit-field, or a function, or a parameter, or an
2733 // object declared with the register storage-class specifier.
2734 int DiagKind = -1;
2735 if (isa<ParmVarDecl>(D)) {
2736 DiagKind = 0;
2737 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2738 if (VD->getStorageClass() == SC_Register)
2739 DiagKind = 1;
2740 if (VD->isExceptionVariable())
2741 DiagKind = 2;
2742 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2743 if (FD->isBitField())
2744 DiagKind = 3;
2745 } else if (!isa<TagDecl>(D)) {
Aaron Ballman3d216a52014-01-02 23:39:11 +00002746 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
Richard Smith9eaab4b2013-02-01 08:25:07 +00002747 << (TmpAttr.isC11() ? ExpectedVariableOrField
2748 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002749 return;
2750 }
2751 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002752 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Aaron Ballman3d216a52014-01-02 23:39:11 +00002753 << &TmpAttr << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002754 return;
2755 }
2756 }
2757
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002758 if (E->isTypeDependent() || E->isValueDependent()) {
2759 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002760 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2761 AA->setPackExpansion(IsPackExpansion);
2762 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002763 return;
2764 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002765
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002766 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002767 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002768 ExprResult ICE
2769 = VerifyIntegerConstantExpression(E, &Alignment,
2770 diag::err_aligned_attribute_argument_not_int,
2771 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002772 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002773 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002774
2775 // C++11 [dcl.align]p2:
2776 // -- if the constant expression evaluates to zero, the alignment
2777 // specifier shall have no effect
2778 // C11 6.7.5p6:
2779 // An alignment specification of zero has no effect.
2780 if (!(TmpAttr.isAlignas() && !Alignment) &&
2781 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002782 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2783 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002784 return;
2785 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002786
David Majnemerabecae72014-02-12 20:36:10 +00002787 // Alignment calculations can wrap around if it's greater than 2**28.
2788 unsigned MaxValidAlignment = TmpAttr.isDeclspec() ? 8192 : 268435456;
2789 if (Alignment.getZExtValue() > MaxValidAlignment) {
2790 Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
2791 << E->getSourceRange();
2792 return;
Aaron Ballman478faed2012-06-19 22:09:27 +00002793 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002794
Richard Smith44c247f2013-02-22 08:32:16 +00002795 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002796 ICE.get(), SpellingListIndex);
Richard Smith44c247f2013-02-22 08:32:16 +00002797 AA->setPackExpansion(IsPackExpansion);
2798 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002799}
2800
Michael Hanaf02bbe2013-02-01 01:19:17 +00002801void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002802 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002803 // FIXME: Cache the number on the Attr object if non-dependent?
2804 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002805 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2806 SpellingListIndex);
2807 AA->setPackExpansion(IsPackExpansion);
2808 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002809}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002810
Richard Smith848e1f12013-02-01 08:12:08 +00002811void Sema::CheckAlignasUnderalignment(Decl *D) {
2812 assert(D->hasAttrs() && "no attributes on decl");
2813
2814 QualType Ty;
2815 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2816 Ty = VD->getType();
2817 else
2818 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002819 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002820 return;
2821
2822 // C++11 [dcl.align]p5, C11 6.7.5/4:
2823 // The combined effect of all alignment attributes in a declaration shall
2824 // not specify an alignment that is less strict than the alignment that
2825 // would otherwise be required for the entity being declared.
Craig Topperc3ec1492014-05-26 06:22:03 +00002826 AlignedAttr *AlignasAttr = nullptr;
Richard Smith848e1f12013-02-01 08:12:08 +00002827 unsigned Align = 0;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002828 for (auto *I : D->specific_attrs<AlignedAttr>()) {
Richard Smith848e1f12013-02-01 08:12:08 +00002829 if (I->isAlignmentDependent())
2830 return;
2831 if (I->isAlignas())
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002832 AlignasAttr = I;
Richard Smith848e1f12013-02-01 08:12:08 +00002833 Align = std::max(Align, I->getAlignment(Context));
2834 }
2835
2836 if (AlignasAttr && Align) {
2837 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2838 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2839 if (NaturalAlign > RequestedAlign)
2840 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2841 << Ty << (unsigned)NaturalAlign.getQuantity();
2842 }
2843}
2844
David Majnemer2c4e00a2014-01-29 22:07:36 +00002845bool Sema::checkMSInheritanceAttrOnDefinition(
David Majnemer4bb09802014-02-10 19:50:15 +00002846 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00002847 MSInheritanceAttr::Spelling SemanticSpelling) {
2848 assert(RD->hasDefinition() && "RD has no definition!");
2849
David Majnemer98c9ee22014-02-07 00:43:07 +00002850 // We may not have seen base specifiers or any virtual methods yet. We will
2851 // have to wait until the record is defined to catch any mismatches.
2852 if (!RD->getDefinition()->isCompleteDefinition())
2853 return false;
David Majnemer2c4e00a2014-01-29 22:07:36 +00002854
David Majnemer98c9ee22014-02-07 00:43:07 +00002855 // The unspecified model never matches what a definition could need.
2856 if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance)
2857 return false;
2858
David Majnemer4bb09802014-02-10 19:50:15 +00002859 if (BestCase) {
2860 if (RD->calculateInheritanceModel() == SemanticSpelling)
2861 return false;
2862 } else {
2863 if (RD->calculateInheritanceModel() <= SemanticSpelling)
2864 return false;
2865 }
David Majnemer98c9ee22014-02-07 00:43:07 +00002866
2867 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
2868 << 0 /*definition*/;
2869 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here)
2870 << RD->getNameAsString();
2871 return true;
David Majnemer2c4e00a2014-01-29 22:07:36 +00002872}
2873
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002874/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002875/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002876///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002877/// Despite what would be logical, the mode attribute is a decl attribute, not a
2878/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2879/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002880static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002881 // This attribute isn't documented, but glibc uses it. It changes
2882 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00002883 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00002884 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
2885 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002886 return;
2887 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002888
Aaron Ballman00e99962013-08-31 01:11:41 +00002889 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
2890 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002891
2892 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002893 if (Str.startswith("__") && Str.endswith("__"))
2894 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002895
2896 unsigned DestWidth = 0;
2897 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002898 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002899 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002900 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002901 switch (Str[0]) {
2902 case 'Q': DestWidth = 8; break;
2903 case 'H': DestWidth = 16; break;
2904 case 'S': DestWidth = 32; break;
2905 case 'D': DestWidth = 64; break;
2906 case 'X': DestWidth = 96; break;
2907 case 'T': DestWidth = 128; break;
2908 }
2909 if (Str[1] == 'F') {
2910 IntegerMode = false;
2911 } else if (Str[1] == 'C') {
2912 IntegerMode = false;
2913 ComplexMode = true;
2914 } else if (Str[1] != 'I') {
2915 DestWidth = 0;
2916 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002917 break;
2918 case 4:
2919 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2920 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002921 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002922 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002923 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002924 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002925 break;
2926 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002927 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002928 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002929 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002930 case 11:
2931 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00002932 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002933 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002934 }
2935
2936 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002937 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002938 OldTy = TD->getUnderlyingType();
2939 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2940 OldTy = VD->getType();
2941 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002942 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00002943 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002944 return;
2945 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002946
John McCall9dd450b2009-09-21 23:43:11 +00002947 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002948 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2949 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002950 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002951 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2952 } else if (ComplexMode) {
2953 if (!OldTy->isComplexType())
2954 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2955 } else {
2956 if (!OldTy->isFloatingType())
2957 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2958 }
2959
Mike Stump87c57ac2009-05-16 07:39:55 +00002960 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2961 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002962 // FIXME: Make sure floating-point mappings are accurate
2963 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002964 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00002965 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002966 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002967 }
2968
2969 QualType NewTy;
2970
2971 if (IntegerMode)
2972 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
2973 OldTy->isSignedIntegerType());
2974 else
2975 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
2976
2977 if (NewTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00002978 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002979 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002980 }
2981
Eli Friedman4735374e2009-03-03 06:41:03 +00002982 if (ComplexMode) {
2983 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002984 }
2985
2986 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002987 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
2988 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
2989 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00002990 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002991
2992 D->addAttr(::new (S.Context)
2993 ModeAttr(Attr.getRange(), S.Context, Name,
2994 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00002995}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002996
Chandler Carruthedc2c642011-07-02 00:01:44 +00002997static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00002998 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2999 if (!VD->hasGlobalStorage())
3000 S.Diag(Attr.getLoc(),
3001 diag::warn_attribute_requires_functions_or_static_globals)
3002 << Attr.getName();
3003 } else if (!isFunctionOrMethod(D)) {
3004 S.Diag(Attr.getLoc(),
3005 diag::warn_attribute_requires_functions_or_static_globals)
3006 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003007 return;
3008 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003009
Michael Han99315932013-01-24 16:46:58 +00003010 D->addAttr(::new (S.Context)
3011 NoDebugAttr(Attr.getRange(), S.Context,
3012 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003013}
3014
Paul Robinsonf0674352014-03-31 22:29:15 +00003015static void handleAlwaysInlineAttr(Sema &S, Decl *D,
3016 const AttributeList &Attr) {
3017 if (checkAttrMutualExclusion<OptimizeNoneAttr>(S, D, Attr))
3018 return;
3019
3020 D->addAttr(::new (S.Context)
3021 AlwaysInlineAttr(Attr.getRange(), S.Context,
3022 Attr.getAttributeSpellingListIndex()));
3023}
3024
3025static void handleOptimizeNoneAttr(Sema &S, Decl *D,
3026 const AttributeList &Attr) {
3027 if (checkAttrMutualExclusion<AlwaysInlineAttr>(S, D, Attr))
3028 return;
3029
3030 D->addAttr(::new (S.Context)
3031 OptimizeNoneAttr(Attr.getRange(), S.Context,
3032 Attr.getAttributeSpellingListIndex()));
3033}
3034
Chandler Carruthedc2c642011-07-02 00:01:44 +00003035static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003036 FunctionDecl *FD = cast<FunctionDecl>(D);
Alp Toker314cc812014-01-25 16:55:45 +00003037 if (!FD->getReturnType()->isVoidType()) {
Alp Tokerf5b10792014-07-02 12:55:58 +00003038 SourceRange RTRange = FD->getReturnTypeSourceRange();
3039 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
Aaron Ballman3aff6332013-12-02 19:30:36 +00003040 << FD->getType()
Alp Tokerf5b10792014-07-02 12:55:58 +00003041 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
3042 : FixItHint());
Aaron Ballman3aff6332013-12-02 19:30:36 +00003043 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003044 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003045
Aaron Ballman3aff6332013-12-02 19:30:36 +00003046 D->addAttr(::new (S.Context)
3047 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00003048 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003049}
3050
Chandler Carruthedc2c642011-07-02 00:01:44 +00003051static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003052 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003053 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003054 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003055 return;
3056 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003057
Michael Han99315932013-01-24 16:46:58 +00003058 D->addAttr(::new (S.Context)
3059 GNUInlineAttr(Attr.getRange(), S.Context,
3060 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003061}
3062
Chandler Carruthedc2c642011-07-02 00:01:44 +00003063static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003064 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003065
Aaron Ballman02df2e02012-12-09 17:45:41 +00003066 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003067 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003068 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3069 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003070 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003071 return;
3072
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003073 if (!isa<ObjCMethodDecl>(D)) {
3074 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3075 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003076 return;
3077 }
3078
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003079 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003080 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003081 D->addAttr(::new (S.Context)
3082 FastCallAttr(Attr.getRange(), S.Context,
3083 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003084 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003085 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003086 D->addAttr(::new (S.Context)
3087 StdCallAttr(Attr.getRange(), S.Context,
3088 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003089 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003090 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003091 D->addAttr(::new (S.Context)
3092 ThisCallAttr(Attr.getRange(), S.Context,
3093 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003094 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003095 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003096 D->addAttr(::new (S.Context)
3097 CDeclAttr(Attr.getRange(), S.Context,
3098 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003099 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003100 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003101 D->addAttr(::new (S.Context)
3102 PascalAttr(Attr.getRange(), S.Context,
3103 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003104 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003105 case AttributeList::AT_MSABI:
3106 D->addAttr(::new (S.Context)
3107 MSABIAttr(Attr.getRange(), S.Context,
3108 Attr.getAttributeSpellingListIndex()));
3109 return;
3110 case AttributeList::AT_SysVABI:
3111 D->addAttr(::new (S.Context)
3112 SysVABIAttr(Attr.getRange(), S.Context,
3113 Attr.getAttributeSpellingListIndex()));
3114 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003115 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003116 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003117 switch (CC) {
3118 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003119 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003120 break;
3121 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003122 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003123 break;
3124 default:
3125 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003126 }
3127
Michael Han99315932013-01-24 16:46:58 +00003128 D->addAttr(::new (S.Context)
3129 PcsAttr(Attr.getRange(), S.Context, PCS,
3130 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003131 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003132 }
Derek Schuffa2020962012-10-16 22:30:41 +00003133 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003134 D->addAttr(::new (S.Context)
3135 PnaclCallAttr(Attr.getRange(), S.Context,
3136 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003137 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003138 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003139 D->addAttr(::new (S.Context)
3140 IntelOclBiccAttr(Attr.getRange(), S.Context,
3141 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003142 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003143
Abramo Bagnara50099372010-04-30 13:10:51 +00003144 default:
3145 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003146 }
3147}
3148
Aaron Ballman02df2e02012-12-09 17:45:41 +00003149bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3150 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003151 if (attr.isInvalid())
3152 return true;
3153
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003154 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003155 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003156 attr.setInvalid();
3157 return true;
3158 }
3159
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003160 // TODO: diagnose uses of these conventions on the wrong target.
John McCall3882ace2011-01-05 12:14:39 +00003161 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003162 case AttributeList::AT_CDecl: CC = CC_C; break;
3163 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3164 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3165 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3166 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003167 case AttributeList::AT_MSABI:
3168 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3169 CC_X86_64Win64;
3170 break;
3171 case AttributeList::AT_SysVABI:
3172 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3173 CC_C;
3174 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003175 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003176 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003177 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003178 attr.setInvalid();
3179 return true;
3180 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003181 if (StrRef == "aapcs") {
3182 CC = CC_AAPCS;
3183 break;
3184 } else if (StrRef == "aapcs-vfp") {
3185 CC = CC_AAPCS_VFP;
3186 break;
3187 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003188
3189 attr.setInvalid();
3190 Diag(attr.getLoc(), diag::err_invalid_pcs);
3191 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003192 }
Derek Schuffa2020962012-10-16 22:30:41 +00003193 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003194 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003195 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003196 }
3197
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003198 const TargetInfo &TI = Context.getTargetInfo();
3199 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3200 if (A == TargetInfo::CCCR_Warning) {
3201 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003202
3203 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3204 if (FD)
3205 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3206 TargetInfo::CCMT_NonMember;
3207 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003208 }
3209
John McCall3882ace2011-01-05 12:14:39 +00003210 return false;
3211}
3212
John McCall3882ace2011-01-05 12:14:39 +00003213/// Checks a regparm attribute, returning true if it is ill-formed and
3214/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003215bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3216 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003217 return true;
3218
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003219 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003220 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003221 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003222 }
Eli Friedman7044b762009-03-27 21:06:47 +00003223
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003224 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003225 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003226 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003227 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003228 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003229 }
3230
Douglas Gregore8bbc122011-09-02 00:18:52 +00003231 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003232 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003233 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003234 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003235 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003236 }
3237
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003238 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003239 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003240 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003241 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003242 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003243 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003244 }
3245
John McCall3882ace2011-01-05 12:14:39 +00003246 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003247}
3248
Aaron Ballman66039932013-12-19 00:41:31 +00003249static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3250 const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003251 // check the attribute arguments.
3252 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
3253 // FIXME: 0 is not okay.
Aaron Ballman05e420a2014-01-02 21:26:14 +00003254 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3255 << Attr.getName() << 2;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003256 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003257 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003258
Aaron Ballman66039932013-12-19 00:41:31 +00003259 uint32_t MaxThreads, MinBlocks = 0;
3260 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1))
3261 return;
3262 if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr,
3263 Attr.getArgAsExpr(1),
3264 MinBlocks, 2))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003265 return;
3266
3267 D->addAttr(::new (S.Context)
3268 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3269 MaxThreads, MinBlocks,
3270 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003271}
3272
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003273static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3274 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003275 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003276 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003277 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003278 return;
3279 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003280
3281 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003282 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003283
Aaron Ballman00e99962013-08-31 01:11:41 +00003284 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003285
3286 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3287 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3288 << Attr.getName() << ExpectedFunctionOrMethod;
3289 return;
3290 }
3291
3292 uint64_t ArgumentIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003293 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3294 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003295 return;
3296
3297 uint64_t TypeTagIdx;
Alp Toker601b22c2014-01-21 23:35:24 +00003298 if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3299 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003300 return;
3301
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003302 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003303 if (IsPointer) {
3304 // Ensure that buffer has a pointer type.
Alp Toker601b22c2014-01-21 23:35:24 +00003305 QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003306 if (!BufferTy->isPointerType()) {
3307 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003308 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003309 }
3310 }
3311
Michael Han99315932013-01-24 16:46:58 +00003312 D->addAttr(::new (S.Context)
3313 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3314 ArgumentIdx, TypeTagIdx, IsPointer,
3315 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003316}
3317
3318static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3319 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003320 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003321 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003322 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003323 return;
3324 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003325
3326 if (!checkAttributeNumArgs(S, Attr, 1))
3327 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003328
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003329 if (!isa<VarDecl>(D)) {
3330 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3331 << Attr.getName() << ExpectedVariable;
3332 return;
3333 }
3334
Aaron Ballman00e99962013-08-31 01:11:41 +00003335 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Craig Topperc3ec1492014-05-26 06:22:03 +00003336 TypeSourceInfo *MatchingCTypeLoc = nullptr;
Richard Smithb87c4652013-10-31 21:23:20 +00003337 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3338 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003339
Michael Han99315932013-01-24 16:46:58 +00003340 D->addAttr(::new (S.Context)
3341 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003342 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003343 Attr.getLayoutCompatible(),
3344 Attr.getMustBeNull(),
3345 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003346}
3347
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003348//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003349// Checker-specific attribute handlers.
3350//===----------------------------------------------------------------------===//
3351
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003352static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) {
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003353 return type->isDependentType() ||
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003354 type->isObjCRetainableType();
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003355}
3356
John McCalled433932011-01-25 03:31:58 +00003357static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003358 return type->isDependentType() ||
3359 type->isObjCObjectPointerType() ||
3360 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003361}
3362static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003363 return type->isDependentType() ||
3364 type->isPointerType() ||
3365 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003366}
3367
Chandler Carruthedc2c642011-07-02 00:01:44 +00003368static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003369 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003370 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003371
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003372 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003373 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3374 cf = false;
3375 } else {
3376 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3377 cf = true;
3378 }
3379
3380 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003381 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003382 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003383 return;
3384 }
3385
3386 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003387 param->addAttr(::new (S.Context)
3388 CFConsumedAttr(Attr.getRange(), S.Context,
3389 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003390 else
Michael Han99315932013-01-24 16:46:58 +00003391 param->addAttr(::new (S.Context)
3392 NSConsumedAttr(Attr.getRange(), S.Context,
3393 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003394}
3395
Chandler Carruthedc2c642011-07-02 00:01:44 +00003396static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3397 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003398
John McCalled433932011-01-25 03:31:58 +00003399 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003400
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003401 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003402 returnType = MD->getReturnType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003403 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003404 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003405 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003406 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3407 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003408 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003409 returnType = FD->getReturnType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003410 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003411 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003412 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003413 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003414 return;
3415 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003416
John McCalled433932011-01-25 03:31:58 +00003417 bool typeOK;
3418 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003419 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003420 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003421 case AttributeList::AT_NSReturnsRetained:
Fariborz Jahanian4eba3dc2014-06-12 16:12:30 +00003422 typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType);
Fariborz Jahanian9c100322014-06-11 21:22:53 +00003423 cf = false;
3424 break;
3425
3426 case AttributeList::AT_NSReturnsAutoreleased:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003427 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003428 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3429 cf = false;
3430 break;
3431
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003432 case AttributeList::AT_CFReturnsRetained:
3433 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003434 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3435 cf = true;
3436 break;
3437 }
3438
3439 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003440 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003441 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003442 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003443 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003444
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003445 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003446 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003447 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003448 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003449 D->addAttr(::new (S.Context)
3450 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3451 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003452 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003453 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003454 D->addAttr(::new (S.Context)
3455 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3456 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003457 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003458 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003459 D->addAttr(::new (S.Context)
3460 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3461 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003462 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003463 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003464 D->addAttr(::new (S.Context)
3465 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3466 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003467 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003468 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003469 D->addAttr(::new (S.Context)
3470 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3471 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003472 return;
3473 };
3474}
3475
John McCallcf166702011-07-22 08:53:00 +00003476static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3477 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003478 const int EP_ObjCMethod = 1;
3479 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003480
John McCallcf166702011-07-22 08:53:00 +00003481 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003482 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003483 if (isa<ObjCMethodDecl>(D))
Alp Toker314cc812014-01-25 16:55:45 +00003484 resultType = cast<ObjCMethodDecl>(D)->getReturnType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003485 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003486 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003487
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003488 if (!resultType->isReferenceType() &&
3489 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003490 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003491 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003492 << attr.getName()
3493 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003494 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003495
3496 // Drop the attribute.
3497 return;
3498 }
3499
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003500 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003501 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3502 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003503}
3504
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003505static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3506 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003507 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003508
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003509 DeclContext *DC = method->getDeclContext();
3510 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3511 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3512 << attr.getName() << 0;
3513 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3514 return;
3515 }
3516 if (method->getMethodFamily() == OMF_dealloc) {
3517 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3518 << attr.getName() << 1;
3519 return;
3520 }
3521
Michael Han99315932013-01-24 16:46:58 +00003522 method->addAttr(::new (S.Context)
3523 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3524 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003525}
3526
Aaron Ballmanfb763042013-12-02 18:05:46 +00003527static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3528 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003529 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr))
John McCall32f5fe12011-09-30 05:12:12 +00003530 return;
John McCall32f5fe12011-09-30 05:12:12 +00003531
Aaron Ballmanfb763042013-12-02 18:05:46 +00003532 D->addAttr(::new (S.Context)
3533 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3534 Attr.getAttributeSpellingListIndex()));
3535}
3536
3537static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3538 const AttributeList &Attr) {
Aaron Ballman2cfbc002014-01-03 16:23:46 +00003539 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr))
Aaron Ballmanfb763042013-12-02 18:05:46 +00003540 return;
3541
3542 D->addAttr(::new (S.Context)
3543 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3544 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003545}
3546
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003547static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3548 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00003549 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003550
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003551 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003552 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003553 return;
3554 }
3555
3556 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003557 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003558 Attr.getAttributeSpellingListIndex()));
3559}
3560
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003561static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3562 const AttributeList &Attr) {
Craig Topperc3ec1492014-05-26 06:22:03 +00003563 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr;
3564
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003565 if (!Parm) {
3566 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3567 return;
3568 }
3569
3570 D->addAttr(::new (S.Context)
3571 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3572 Attr.getAttributeSpellingListIndex()));
3573}
3574
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003575static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3576 const AttributeList &Attr) {
3577 IdentifierInfo *RelatedClass =
Craig Topperc3ec1492014-05-26 06:22:03 +00003578 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003579 if (!RelatedClass) {
3580 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3581 return;
3582 }
3583 IdentifierInfo *ClassMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00003584 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003585 IdentifierInfo *InstanceMethod =
Craig Topperc3ec1492014-05-26 06:22:03 +00003586 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003587 D->addAttr(::new (S.Context)
3588 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3589 ClassMethod, InstanceMethod,
3590 Attr.getAttributeSpellingListIndex()));
3591}
3592
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003593static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3594 const AttributeList &Attr) {
Fariborz Jahanian6efab6e2014-03-14 18:19:46 +00003595 ObjCInterfaceDecl *IFace;
3596 if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
3597 IFace = CatDecl->getClassInterface();
3598 else
3599 IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003600 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003601 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003602 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3603 Attr.getAttributeSpellingListIndex()));
3604}
3605
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00003606static void handleObjCRuntimeName(Sema &S, Decl *D,
3607 const AttributeList &Attr) {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00003608 StringRef MetaDataName;
3609 if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName))
3610 return;
3611 D->addAttr(::new (S.Context)
3612 ObjCRuntimeNameAttr(Attr.getRange(), S.Context,
3613 MetaDataName,
3614 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00003615}
3616
Chandler Carruthedc2c642011-07-02 00:01:44 +00003617static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3618 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003619 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003620
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003621 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003622 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003623}
3624
Chandler Carruthedc2c642011-07-02 00:01:44 +00003625static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3626 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003627 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003628 QualType type = vd->getType();
3629
3630 if (!type->isDependentType() &&
3631 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003632 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003633 << type;
3634 return;
3635 }
3636
3637 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3638
3639 // If we have no lifetime yet, check the lifetime we're presumably
3640 // going to infer.
3641 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3642 lifetime = type->getObjCARCImplicitLifetime();
3643
3644 switch (lifetime) {
3645 case Qualifiers::OCL_None:
3646 assert(type->isDependentType() &&
3647 "didn't infer lifetime for non-dependent type?");
3648 break;
3649
3650 case Qualifiers::OCL_Weak: // meaningful
3651 case Qualifiers::OCL_Strong: // meaningful
3652 break;
3653
3654 case Qualifiers::OCL_ExplicitNone:
3655 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003656 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003657 << (lifetime == Qualifiers::OCL_Autoreleasing);
3658 break;
3659 }
3660
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003661 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003662 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3663 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003664}
3665
Francois Picheta83957a2010-12-19 06:50:37 +00003666//===----------------------------------------------------------------------===//
3667// Microsoft specific attribute handlers.
3668//===----------------------------------------------------------------------===//
3669
Chandler Carruthedc2c642011-07-02 00:01:44 +00003670static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003671 if (!S.LangOpts.CPlusPlus) {
3672 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3673 << Attr.getName() << AttributeLangSupport::C;
3674 return;
3675 }
3676
Aaron Ballman60e705e2013-11-24 20:58:02 +00003677 if (!isa<CXXRecordDecl>(D)) {
3678 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3679 << Attr.getName() << ExpectedClass;
3680 return;
3681 }
3682
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003683 StringRef StrRef;
3684 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003685 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003686 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003687
David Majnemer89085342013-08-09 08:56:20 +00003688 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3689 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003690 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3691 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003692
Reid Kleckner140c4a72013-05-17 14:04:52 +00003693 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003694 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003695 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003696 return;
3697 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003698
David Majnemer89085342013-08-09 08:56:20 +00003699 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003700 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003701 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003702 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003703 return;
3704 }
David Majnemer89085342013-08-09 08:56:20 +00003705 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003706 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003707 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003708 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003709 }
Francois Picheta83957a2010-12-19 06:50:37 +00003710
David Majnemer89085342013-08-09 08:56:20 +00003711 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3712 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003713}
3714
David Majnemer2c4e00a2014-01-29 22:07:36 +00003715static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3716 if (!S.LangOpts.CPlusPlus) {
3717 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3718 << Attr.getName() << AttributeLangSupport::C;
3719 return;
3720 }
3721 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
David Majnemer4bb09802014-02-10 19:50:15 +00003722 D, Attr.getRange(), /*BestCase=*/true,
3723 Attr.getAttributeSpellingListIndex(),
David Majnemer2c4e00a2014-01-29 22:07:36 +00003724 (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling());
3725 if (IA)
3726 D->addAttr(IA);
3727}
3728
Reid Kleckner7d6d2702014-05-01 03:16:47 +00003729static void handleDeclspecThreadAttr(Sema &S, Decl *D,
3730 const AttributeList &Attr) {
3731 VarDecl *VD = cast<VarDecl>(D);
3732 if (!S.Context.getTargetInfo().isTLSSupported()) {
3733 S.Diag(Attr.getLoc(), diag::err_thread_unsupported);
3734 return;
3735 }
3736 if (VD->getTSCSpec() != TSCS_unspecified) {
3737 S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable);
3738 return;
3739 }
3740 if (VD->hasLocalStorage()) {
3741 S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
3742 return;
3743 }
3744 VD->addAttr(::new (S.Context) ThreadAttr(
3745 Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
3746}
3747
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003748static void handleARMInterruptAttr(Sema &S, Decl *D,
3749 const AttributeList &Attr) {
3750 // Check the attribute arguments.
3751 if (Attr.getNumArgs() > 1) {
3752 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3753 << Attr.getName() << 1;
3754 return;
3755 }
3756
3757 StringRef Str;
3758 SourceLocation ArgLoc;
3759
3760 if (Attr.getNumArgs() == 0)
3761 Str = "";
3762 else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc))
3763 return;
3764
3765 ARMInterruptAttr::InterruptType Kind;
3766 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
3767 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
3768 << Attr.getName() << Str << ArgLoc;
3769 return;
3770 }
3771
3772 unsigned Index = Attr.getAttributeSpellingListIndex();
3773 D->addAttr(::new (S.Context)
3774 ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index));
3775}
3776
3777static void handleMSP430InterruptAttr(Sema &S, Decl *D,
3778 const AttributeList &Attr) {
3779 if (!checkAttributeNumArgs(S, Attr, 1))
3780 return;
3781
3782 if (!Attr.isArgExpr(0)) {
3783 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3784 << AANT_ArgumentIntegerConstant;
3785 return;
3786 }
3787
3788 // FIXME: Check for decl - it should be void ()(void).
3789
3790 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0));
3791 llvm::APSInt NumParams(32);
3792 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
3793 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3794 << Attr.getName() << AANT_ArgumentIntegerConstant
3795 << NumParamsExpr->getSourceRange();
3796 return;
3797 }
3798
3799 unsigned Num = NumParams.getLimitedValue(255);
3800 if ((Num & 1) || Num > 30) {
3801 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
3802 << Attr.getName() << (int)NumParams.getSExtValue()
3803 << NumParamsExpr->getSourceRange();
3804 return;
3805 }
3806
Aaron Ballman36a53502014-01-16 13:03:14 +00003807 D->addAttr(::new (S.Context)
3808 MSP430InterruptAttr(Attr.getLoc(), S.Context, Num,
3809 Attr.getAttributeSpellingListIndex()));
3810 D->addAttr(UsedAttr::CreateImplicit(S.Context));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003811}
3812
3813static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3814 // Dispatch the interrupt attribute based on the current target.
3815 if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430)
3816 handleMSP430InterruptAttr(S, D, Attr);
3817 else
3818 handleARMInterruptAttr(S, D, Attr);
3819}
3820
3821static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D,
3822 const AttributeList& Attr) {
3823 // If we try to apply it to a function pointer, don't warn, but don't
3824 // do anything, either. It doesn't matter anyway, because there's nothing
3825 // special about calling a force_align_arg_pointer function.
3826 ValueDecl *VD = dyn_cast<ValueDecl>(D);
3827 if (VD && VD->getType()->isFunctionPointerType())
3828 return;
3829 // Also don't warn on function pointer typedefs.
3830 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
3831 if (TD && (TD->getUnderlyingType()->isFunctionPointerType() ||
3832 TD->getUnderlyingType()->isFunctionType()))
3833 return;
3834 // Attribute can only be applied to function types.
3835 if (!isa<FunctionDecl>(D)) {
3836 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3837 << Attr.getName() << /* function */0;
3838 return;
3839 }
3840
Aaron Ballman36a53502014-01-16 13:03:14 +00003841 D->addAttr(::new (S.Context)
3842 X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context,
3843 Attr.getAttributeSpellingListIndex()));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003844}
3845
3846DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range,
3847 unsigned AttrSpellingListIndex) {
3848 if (D->hasAttr<DLLExportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00003849 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'";
Craig Topperc3ec1492014-05-26 06:22:03 +00003850 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003851 }
3852
3853 if (D->hasAttr<DLLImportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00003854 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003855
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003856 return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003857}
3858
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003859DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range,
3860 unsigned AttrSpellingListIndex) {
3861 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
Nico Rieck60478662014-02-22 19:47:30 +00003862 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003863 D->dropAttr<DLLImportAttr>();
3864 }
3865
3866 if (D->hasAttr<DLLExportAttr>())
Craig Topperc3ec1492014-05-26 06:22:03 +00003867 return nullptr;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003868
Aaron Ballman3f5f3e72014-01-20 16:15:55 +00003869 return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003870}
3871
Hans Wennborge82f19c2014-06-24 23:57:05 +00003872static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) {
Hans Wennborg5e645282014-06-24 23:57:13 +00003873 if (isa<ClassTemplatePartialSpecializationDecl>(D) &&
3874 S.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
3875 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored)
3876 << A.getName();
3877 return;
3878 }
3879
Hans Wennborge82f19c2014-06-24 23:57:05 +00003880 unsigned Index = A.getAttributeSpellingListIndex();
3881 Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport
3882 ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index)
3883 : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003884 if (NewAttr)
3885 D->addAttr(NewAttr);
3886}
3887
David Majnemer2c4e00a2014-01-29 22:07:36 +00003888MSInheritanceAttr *
David Majnemer4bb09802014-02-10 19:50:15 +00003889Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase,
David Majnemer2c4e00a2014-01-29 22:07:36 +00003890 unsigned AttrSpellingListIndex,
3891 MSInheritanceAttr::Spelling SemanticSpelling) {
3892 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
3893 if (IA->getSemanticSpelling() == SemanticSpelling)
Craig Topperc3ec1492014-05-26 06:22:03 +00003894 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003895 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
3896 << 1 /*previous declaration*/;
3897 Diag(Range.getBegin(), diag::note_previous_ms_inheritance);
3898 D->dropAttr<MSInheritanceAttr>();
3899 }
3900
3901 CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
3902 if (RD->hasDefinition()) {
David Majnemer4bb09802014-02-10 19:50:15 +00003903 if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase,
3904 SemanticSpelling)) {
Craig Topperc3ec1492014-05-26 06:22:03 +00003905 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003906 }
3907 } else {
3908 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
3909 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
3910 << 1 /*partial specialization*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00003911 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003912 }
3913 if (RD->getDescribedClassTemplate()) {
3914 Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance)
3915 << 0 /*primary template*/;
Craig Topperc3ec1492014-05-26 06:22:03 +00003916 return nullptr;
David Majnemer2c4e00a2014-01-29 22:07:36 +00003917 }
3918 }
3919
3920 return ::new (Context)
David Majnemer4bb09802014-02-10 19:50:15 +00003921 MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex);
David Majnemer2c4e00a2014-01-29 22:07:36 +00003922}
3923
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003924static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3925 // The capability attributes take a single string parameter for the name of
3926 // the capability they represent. The lockable attribute does not take any
3927 // parameters. However, semantically, both attributes represent the same
3928 // concept, and so they use the same semantic attribute. Eventually, the
3929 // lockable attribute will be removed.
Aaron Ballman6c810072014-03-05 21:47:13 +00003930 //
Alp Toker958027b2014-07-14 19:42:55 +00003931 // For backward compatibility, any capability which has no specified string
Aaron Ballman6c810072014-03-05 21:47:13 +00003932 // literal will be considered a "mutex."
3933 StringRef N("mutex");
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003934 SourceLocation LiteralLoc;
3935 if (Attr.getKind() == AttributeList::AT_Capability &&
3936 !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc))
3937 return;
3938
Aaron Ballman6c810072014-03-05 21:47:13 +00003939 // Currently, there are only two names allowed for a capability: role and
3940 // mutex (case insensitive). Diagnose other capability names.
3941 if (!N.equals_lower("mutex") && !N.equals_lower("role"))
3942 S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N;
3943
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003944 D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N,
3945 Attr.getAttributeSpellingListIndex()));
3946}
3947
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003948static void handleAssertCapabilityAttr(Sema &S, Decl *D,
3949 const AttributeList &Attr) {
3950 D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context,
3951 Attr.getArgAsExpr(0),
3952 Attr.getAttributeSpellingListIndex()));
3953}
3954
3955static void handleAcquireCapabilityAttr(Sema &S, Decl *D,
3956 const AttributeList &Attr) {
3957 SmallVector<Expr*, 1> Args;
Aaron Ballman18d85ae2014-03-20 16:02:49 +00003958 if (!checkLockFunAttrCommon(S, D, Attr, Args))
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003959 return;
3960
3961 D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(),
3962 S.Context,
3963 Args.data(), Args.size(),
3964 Attr.getAttributeSpellingListIndex()));
3965}
3966
3967static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
3968 const AttributeList &Attr) {
3969 SmallVector<Expr*, 2> Args;
3970 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
3971 return;
3972
3973 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(),
3974 S.Context,
3975 Attr.getArgAsExpr(0),
3976 Args.data(),
3977 Args.size(),
3978 Attr.getAttributeSpellingListIndex()));
3979}
3980
3981static void handleReleaseCapabilityAttr(Sema &S, Decl *D,
3982 const AttributeList &Attr) {
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003983 // Check that all arguments are lockable objects.
Aaron Ballman18d85ae2014-03-20 16:02:49 +00003984 SmallVector<Expr *, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00003985 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true);
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003986
Aaron Ballman18d85ae2014-03-20 16:02:49 +00003987 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(
3988 Attr.getRange(), S.Context, Args.data(), Args.size(),
3989 Attr.getAttributeSpellingListIndex()));
Aaron Ballman9e9d1842014-02-21 21:05:14 +00003990}
3991
Aaron Ballmanefe348e2014-02-18 17:36:50 +00003992static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
3993 const AttributeList &Attr) {
3994 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
3995 return;
3996
3997 // check that all arguments are lockable objects
3998 SmallVector<Expr*, 1> Args;
Aaron Ballman69e6e7c2014-03-24 19:29:19 +00003999 checkAttrArgsAreCapabilityObjs(S, D, Attr, Args);
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004000 if (Args.empty())
4001 return;
4002
4003 RequiresCapabilityAttr *RCA = ::new (S.Context)
4004 RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(),
4005 Args.size(), Attr.getAttributeSpellingListIndex());
4006
4007 D->addAttr(RCA);
4008}
4009
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004010/// Handles semantic checking for features that are common to all attributes,
4011/// such as checking whether a parameter was properly specified, or the correct
4012/// number of arguments were passed, etc.
4013static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4014 const AttributeList &Attr) {
4015 // Several attributes carry different semantics than the parsing requires, so
4016 // those are opted out of the common handling.
4017 //
4018 // We also bail on unknown and ignored attributes because those are handled
4019 // as part of the target-specific handling logic.
4020 if (Attr.hasCustomParsing() ||
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004021 Attr.getKind() == AttributeList::UnknownAttribute)
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004022 return false;
4023
Aaron Ballman3aff6332013-12-02 19:30:36 +00004024 // Check whether the attribute requires specific language extensions to be
4025 // enabled.
4026 if (!Attr.diagnoseLangOpts(S))
4027 return true;
4028
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004029 // If there are no optional arguments, then checking for the argument count
4030 // is trivial.
4031 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
4032 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4033 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004034
4035 // Check whether the attribute appertains to the given subject.
4036 if (!Attr.diagnoseAppertainsTo(S, D))
4037 return true;
4038
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004039 return false;
4040}
4041
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004042//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004043// Top Level Sema Entry Points
4044//===----------------------------------------------------------------------===//
4045
Richard Smithf8a75c32013-08-29 00:47:48 +00004046/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4047/// the attribute applies to decls. If the attribute is a type attribute, just
4048/// silently ignore it if a GNU attribute.
4049static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4050 const AttributeList &Attr,
4051 bool IncludeCXX11Attributes) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004052 if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute)
Richard Smithf8a75c32013-08-29 00:47:48 +00004053 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004054
Richard Smithf8a75c32013-08-29 00:47:48 +00004055 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4056 // instead.
4057 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4058 return;
4059
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004060 // Unknown attributes are automatically warned on. Target-specific attributes
4061 // which do not apply to the current target architecture are treated as
4062 // though they were unknown attributes.
4063 if (Attr.getKind() == AttributeList::UnknownAttribute ||
4064 !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) {
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004065 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute()
4066 ? diag::warn_unhandled_ms_attribute_ignored
4067 : diag::warn_unknown_attribute_ignored)
4068 << Attr.getName();
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004069 return;
4070 }
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004071
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004072 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4073 return;
4074
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004075 switch (Attr.getKind()) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004076 default:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004077 // Type attributes are handled elsewhere; silently move on.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004078 assert(Attr.isTypeAttr() && "Non-type attribute not handled");
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004079 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004080 case AttributeList::AT_Interrupt:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004081 handleInterruptAttr(S, D, Attr);
4082 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004083 case AttributeList::AT_X86ForceAlignArgPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004084 handleX86ForceAlignArgPointerAttr(S, D, Attr);
4085 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004086 case AttributeList::AT_DLLExport:
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004087 case AttributeList::AT_DLLImport:
Hans Wennborge82f19c2014-06-24 23:57:05 +00004088 handleDLLAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004089 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004090 case AttributeList::AT_Mips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004091 handleSimpleAttribute<Mips16Attr>(S, D, Attr);
4092 break;
Aaron Ballmanab7691c2014-01-09 22:48:32 +00004093 case AttributeList::AT_NoMips16:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004094 handleSimpleAttribute<NoMips16Attr>(S, D, Attr);
4095 break;
Aaron Ballman9beb5172013-12-02 15:13:14 +00004096 case AttributeList::AT_IBAction:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004097 handleSimpleAttribute<IBActionAttr>(S, D, Attr);
4098 break;
4099 case AttributeList::AT_IBOutlet:
4100 handleIBOutlet(S, D, Attr);
4101 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004102 case AttributeList::AT_IBOutletCollection:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004103 handleIBOutletCollection(S, D, Attr);
4104 break;
4105 case AttributeList::AT_Alias:
4106 handleAliasAttr(S, D, Attr);
4107 break;
4108 case AttributeList::AT_Aligned:
4109 handleAlignedAttr(S, D, Attr);
4110 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004111 case AttributeList::AT_AlwaysInline:
Paul Robinsonf0674352014-03-31 22:29:15 +00004112 handleAlwaysInlineAttr(S, D, Attr);
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004113 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004114 case AttributeList::AT_AnalyzerNoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004115 handleAnalyzerNoReturnAttr(S, D, Attr);
4116 break;
4117 case AttributeList::AT_TLSModel:
4118 handleTLSModelAttr(S, D, Attr);
4119 break;
4120 case AttributeList::AT_Annotate:
4121 handleAnnotateAttr(S, D, Attr);
4122 break;
4123 case AttributeList::AT_Availability:
4124 handleAvailabilityAttr(S, D, Attr);
4125 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004126 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004127 handleDependencyAttr(S, scope, D, Attr);
4128 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004129 case AttributeList::AT_Common:
4130 handleCommonAttr(S, D, Attr);
4131 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004132 case AttributeList::AT_CUDAConstant:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004133 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr);
4134 break;
4135 case AttributeList::AT_Constructor:
4136 handleConstructorAttr(S, D, Attr);
4137 break;
Richard Smith10876ef2013-01-17 01:30:42 +00004138 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004139 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr);
4140 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004141 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004142 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004143 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004144 case AttributeList::AT_Destructor:
4145 handleDestructorAttr(S, D, Attr);
4146 break;
4147 case AttributeList::AT_EnableIf:
4148 handleEnableIfAttr(S, D, Attr);
4149 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004150 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004151 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004152 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004153 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004154 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00004155 break;
Paul Robinsonf0674352014-03-31 22:29:15 +00004156 case AttributeList::AT_OptimizeNone:
4157 handleOptimizeNoneAttr(S, D, Attr);
4158 break;
Peter Collingbourne41af7c22014-05-20 17:12:51 +00004159 case AttributeList::AT_Flatten:
4160 handleSimpleAttribute<FlattenAttr>(S, D, Attr);
4161 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004162 case AttributeList::AT_Format:
4163 handleFormatAttr(S, D, Attr);
4164 break;
4165 case AttributeList::AT_FormatArg:
4166 handleFormatArgAttr(S, D, Attr);
4167 break;
4168 case AttributeList::AT_CUDAGlobal:
4169 handleGlobalAttr(S, D, Attr);
4170 break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00004171 case AttributeList::AT_CUDADevice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004172 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr);
4173 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004174 case AttributeList::AT_CUDAHost:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004175 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr);
4176 break;
4177 case AttributeList::AT_GNUInline:
4178 handleGNUInlineAttr(S, D, Attr);
4179 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004180 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004181 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004182 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004183 case AttributeList::AT_Malloc:
4184 handleMallocAttr(S, D, Attr);
4185 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004186 case AttributeList::AT_MayAlias:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004187 handleSimpleAttribute<MayAliasAttr>(S, D, Attr);
4188 break;
4189 case AttributeList::AT_Mode:
4190 handleModeAttr(S, D, Attr);
4191 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004192 case AttributeList::AT_NoCommon:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004193 handleSimpleAttribute<NoCommonAttr>(S, D, Attr);
4194 break;
Peter Collingbourneb4728c12014-05-19 22:14:34 +00004195 case AttributeList::AT_NoSplitStack:
4196 handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr);
4197 break;
Ted Kremenek9aedc152014-01-17 06:24:56 +00004198 case AttributeList::AT_NonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004199 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D))
4200 handleNonNullAttrParameter(S, PVD, Attr);
4201 else
4202 handleNonNullAttr(S, D, Attr);
4203 break;
Aaron Ballmanfc1951c2014-01-20 14:19:44 +00004204 case AttributeList::AT_ReturnsNonNull:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004205 handleReturnsNonNullAttr(S, D, Attr);
4206 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004207 case AttributeList::AT_Overloadable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004208 handleSimpleAttribute<OverloadableAttr>(S, D, Attr);
4209 break;
4210 case AttributeList::AT_Ownership:
4211 handleOwnershipAttr(S, D, Attr);
4212 break;
4213 case AttributeList::AT_Cold:
4214 handleColdAttr(S, D, Attr);
4215 break;
4216 case AttributeList::AT_Hot:
4217 handleHotAttr(S, D, Attr);
4218 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004219 case AttributeList::AT_Naked:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004220 handleSimpleAttribute<NakedAttr>(S, D, Attr);
4221 break;
4222 case AttributeList::AT_NoReturn:
4223 handleNoReturnAttr(S, D, Attr);
4224 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004225 case AttributeList::AT_NoThrow:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004226 handleSimpleAttribute<NoThrowAttr>(S, D, Attr);
4227 break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00004228 case AttributeList::AT_CUDAShared:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004229 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr);
4230 break;
4231 case AttributeList::AT_VecReturn:
4232 handleVecReturnAttr(S, D, Attr);
4233 break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004234
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004235 case AttributeList::AT_ObjCOwnership:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004236 handleObjCOwnershipAttr(S, D, Attr);
4237 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004238 case AttributeList::AT_ObjCPreciseLifetime:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004239 handleObjCPreciseLifetimeAttr(S, D, Attr);
4240 break;
John McCall31168b02011-06-15 23:02:42 +00004241
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004242 case AttributeList::AT_ObjCReturnsInnerPointer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004243 handleObjCReturnsInnerPointerAttr(S, D, Attr);
4244 break;
John McCallcf166702011-07-22 08:53:00 +00004245
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004246 case AttributeList::AT_ObjCRequiresSuper:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004247 handleObjCRequiresSuperAttr(S, D, Attr);
4248 break;
4249
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004250 case AttributeList::AT_ObjCBridge:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004251 handleObjCBridgeAttr(S, scope, D, Attr);
4252 break;
4253
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004254 case AttributeList::AT_ObjCBridgeMutable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004255 handleObjCBridgeMutableAttr(S, scope, D, Attr);
4256 break;
4257
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00004258 case AttributeList::AT_ObjCBridgeRelated:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004259 handleObjCBridgeRelatedAttr(S, scope, D, Attr);
4260 break;
John McCallf1e8b342011-09-29 07:17:38 +00004261
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004262 case AttributeList::AT_ObjCDesignatedInitializer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004263 handleObjCDesignatedInitializer(S, D, Attr);
4264 break;
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00004265
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00004266 case AttributeList::AT_ObjCRuntimeName:
4267 handleObjCRuntimeName(S, D, Attr);
4268 break;
4269
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004270 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004271 handleCFAuditedTransferAttr(S, D, Attr);
4272 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004273 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004274 handleCFUnknownTransferAttr(S, D, Attr);
4275 break;
John McCall32f5fe12011-09-30 05:12:12 +00004276
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004277 case AttributeList::AT_CFConsumed:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004278 case AttributeList::AT_NSConsumed:
4279 handleNSConsumedAttr(S, D, Attr);
4280 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004281 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004282 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr);
4283 break;
John McCalled433932011-01-25 03:31:58 +00004284
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004285 case AttributeList::AT_NSReturnsAutoreleased:
4286 case AttributeList::AT_NSReturnsNotRetained:
4287 case AttributeList::AT_CFReturnsNotRetained:
4288 case AttributeList::AT_NSReturnsRetained:
4289 case AttributeList::AT_CFReturnsRetained:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004290 handleNSReturnsRetainedAttr(S, D, Attr);
4291 break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004292 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004293 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr);
4294 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004295 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004296 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr);
4297 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004298 case AttributeList::AT_VecTypeHint:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004299 handleVecTypeHint(S, D, Attr);
4300 break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004301
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004302 case AttributeList::AT_InitPriority:
4303 handleInitPriorityAttr(S, D, Attr);
4304 break;
4305
4306 case AttributeList::AT_Packed:
4307 handlePackedAttr(S, D, Attr);
4308 break;
4309 case AttributeList::AT_Section:
4310 handleSectionAttr(S, D, Attr);
4311 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004312 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004313 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004314 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004315 case AttributeList::AT_ArcWeakrefUnavailable:
4316 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr);
4317 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004318 case AttributeList::AT_ObjCRootClass:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004319 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr);
4320 break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00004321 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek438f8db2014-02-22 01:06:05 +00004322 handleObjCSuppresProtocolAttr(S, D, Attr);
Ted Kremenek28eace62013-11-23 01:01:34 +00004323 break;
4324 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004325 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr);
4326 break;
Aaron Ballman12b9f652014-01-16 13:55:42 +00004327 case AttributeList::AT_Unused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004328 handleSimpleAttribute<UnusedAttr>(S, D, Attr);
4329 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004330 case AttributeList::AT_ReturnsTwice:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004331 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr);
4332 break;
4333 case AttributeList::AT_Used:
4334 handleUsedAttr(S, D, Attr);
4335 break;
John McCalld041a9b2013-02-20 01:54:26 +00004336 case AttributeList::AT_Visibility:
4337 handleVisibilityAttr(S, D, Attr, false);
4338 break;
4339 case AttributeList::AT_TypeVisibility:
4340 handleVisibilityAttr(S, D, Attr, true);
4341 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004342 case AttributeList::AT_WarnUnused:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004343 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr);
4344 break;
4345 case AttributeList::AT_WarnUnusedResult:
4346 handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004347 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004348 case AttributeList::AT_Weak:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004349 handleSimpleAttribute<WeakAttr>(S, D, Attr);
4350 break;
4351 case AttributeList::AT_WeakRef:
4352 handleWeakRefAttr(S, D, Attr);
4353 break;
4354 case AttributeList::AT_WeakImport:
4355 handleWeakImportAttr(S, D, Attr);
4356 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004357 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004358 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004359 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004360 case AttributeList::AT_ObjCException:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004361 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr);
4362 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004363 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004364 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004365 break;
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004366 case AttributeList::AT_ObjCNSObject:
4367 handleObjCNSObject(S, D, Attr);
4368 break;
4369 case AttributeList::AT_Blocks:
4370 handleBlocksAttr(S, D, Attr);
4371 break;
4372 case AttributeList::AT_Sentinel:
4373 handleSentinelAttr(S, D, Attr);
4374 break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00004375 case AttributeList::AT_Const:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004376 handleSimpleAttribute<ConstAttr>(S, D, Attr);
4377 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004378 case AttributeList::AT_Pure:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004379 handleSimpleAttribute<PureAttr>(S, D, Attr);
4380 break;
4381 case AttributeList::AT_Cleanup:
4382 handleCleanupAttr(S, D, Attr);
4383 break;
4384 case AttributeList::AT_NoDebug:
4385 handleNoDebugAttr(S, D, Attr);
4386 break;
Aaron Ballman7c19ab12014-02-22 16:59:24 +00004387 case AttributeList::AT_NoDuplicate:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004388 handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr);
4389 break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004390 case AttributeList::AT_NoInline:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004391 handleSimpleAttribute<NoInlineAttr>(S, D, Attr);
4392 break;
4393 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
4394 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr);
4395 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004396 case AttributeList::AT_StdCall:
4397 case AttributeList::AT_CDecl:
4398 case AttributeList::AT_FastCall:
4399 case AttributeList::AT_ThisCall:
4400 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004401 case AttributeList::AT_MSABI:
4402 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004403 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004404 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004405 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004406 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004407 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004408 case AttributeList::AT_OpenCLKernel:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004409 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr);
4410 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004411 case AttributeList::AT_OpenCLImageAccess:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004412 handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr);
4413 break;
John McCall8d32c052012-05-22 21:28:12 +00004414
4415 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004416 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004417 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004418 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004419 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004420 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004421 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004422 case AttributeList::AT_MSInheritance:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004423 handleMSInheritanceAttr(S, D, Attr);
4424 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004425 case AttributeList::AT_SelectAny:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004426 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr);
4427 break;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00004428 case AttributeList::AT_Thread:
4429 handleDeclspecThreadAttr(S, D, Attr);
4430 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004431
4432 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004433 case AttributeList::AT_AssertExclusiveLock:
4434 handleAssertExclusiveLockAttr(S, D, Attr);
4435 break;
4436 case AttributeList::AT_AssertSharedLock:
4437 handleAssertSharedLockAttr(S, D, Attr);
4438 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004439 case AttributeList::AT_GuardedVar:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004440 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr);
4441 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004442 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004443 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004444 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004445 case AttributeList::AT_ScopedLockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004446 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr);
4447 break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004448 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004449 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004450 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004451 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004452 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004453 break;
4454 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004455 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004456 break;
4457 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004458 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004459 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004460 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004461 handleGuardedByAttr(S, D, Attr);
4462 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004463 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004464 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004465 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004466 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004467 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004468 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004469 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004470 handleLockReturnedAttr(S, D, Attr);
4471 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004472 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004473 handleLocksExcludedAttr(S, D, Attr);
4474 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004475 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004476 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004477 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004478 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004479 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004480 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004481 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004482 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004483 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004484
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004485 // Capability analysis attributes.
4486 case AttributeList::AT_Capability:
4487 case AttributeList::AT_Lockable:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004488 handleCapabilityAttr(S, D, Attr);
4489 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004490 case AttributeList::AT_RequiresCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004491 handleRequiresCapabilityAttr(S, D, Attr);
4492 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004493
4494 case AttributeList::AT_AssertCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004495 handleAssertCapabilityAttr(S, D, Attr);
4496 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004497 case AttributeList::AT_AcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004498 handleAcquireCapabilityAttr(S, D, Attr);
4499 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004500 case AttributeList::AT_ReleaseCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004501 handleReleaseCapabilityAttr(S, D, Attr);
4502 break;
Aaron Ballman9e9d1842014-02-21 21:05:14 +00004503 case AttributeList::AT_TryAcquireCapability:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004504 handleTryAcquireCapabilityAttr(S, D, Attr);
4505 break;
Aaron Ballmanefe348e2014-02-18 17:36:50 +00004506
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004507 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004508 case AttributeList::AT_Consumable:
4509 handleConsumableAttr(S, D, Attr);
4510 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004511 case AttributeList::AT_ConsumableAutoCast:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004512 handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr);
4513 break;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00004514 case AttributeList::AT_ConsumableSetOnRead:
Aaron Ballman8abdd0e2014-03-06 14:02:27 +00004515 handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr);
4516 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004517 case AttributeList::AT_CallableWhen:
4518 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004519 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004520 case AttributeList::AT_ParamTypestate:
4521 handleParamTypestateAttr(S, D, Attr);
4522 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004523 case AttributeList::AT_ReturnTypestate:
4524 handleReturnTypestateAttr(S, D, Attr);
4525 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004526 case AttributeList::AT_SetTypestate:
4527 handleSetTypestateAttr(S, D, Attr);
4528 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004529 case AttributeList::AT_TestTypestate:
4530 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004531 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004532
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004533 // Type safety attributes.
4534 case AttributeList::AT_ArgumentWithTypeTag:
4535 handleArgumentWithTypeTagAttr(S, D, Attr);
4536 break;
4537 case AttributeList::AT_TypeTagForDatatype:
4538 handleTypeTagForDatatypeAttr(S, D, Attr);
4539 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004540 }
4541}
4542
4543/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4544/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004545void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004546 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004547 bool IncludeCXX11Attributes) {
4548 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004549 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004550
Joey Gouly2cd9db12013-12-13 16:15:28 +00004551 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004552 // GCC accepts
4553 // static int a9 __attribute__((weakref));
4554 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004555 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Aaron Ballman9f6fec42014-01-02 23:02:01 +00004556 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias)
4557 << cast<NamedDecl>(D);
Rafael Espindolab3069002013-01-16 23:49:06 +00004558 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004559 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004560 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004561
4562 if (!D->hasAttr<OpenCLKernelAttr>()) {
4563 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004564 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
4565 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004566 D->setInvalidDecl();
4567 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004568 if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
4569 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004570 D->setInvalidDecl();
4571 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004572 if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
4573 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004574 D->setInvalidDecl();
4575 }
4576 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004577}
4578
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004579// Annotation attributes are the only attributes allowed after an access
4580// specifier.
4581bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4582 const AttributeList *AttrList) {
4583 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004584 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004585 handleAnnotateAttr(*this, ASDecl, *l);
4586 } else {
4587 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4588 return true;
4589 }
4590 }
4591
4592 return false;
4593}
4594
John McCall42856de2011-10-01 05:17:03 +00004595/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4596/// contains any decl attributes that we should warn about.
4597static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4598 for ( ; A; A = A->getNext()) {
4599 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004600 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004601 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4602
4603 if (A->getKind() == AttributeList::UnknownAttribute) {
4604 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4605 << A->getName() << A->getRange();
4606 } else {
4607 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4608 << A->getName() << A->getRange();
4609 }
4610 }
4611}
4612
4613/// checkUnusedDeclAttributes - Given a declarator which is not being
4614/// used to build a declaration, complain about any decl attributes
4615/// which might be lying around on it.
4616void Sema::checkUnusedDeclAttributes(Declarator &D) {
4617 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4618 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4619 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4620 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4621}
4622
Ryan Flynn7d470f32009-07-30 03:15:39 +00004623/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004624/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004625NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4626 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004627 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Craig Topperc3ec1492014-05-26 06:22:03 +00004628 NamedDecl *NewD = nullptr;
Ryan Flynn7d470f32009-07-30 03:15:39 +00004629 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004630 FunctionDecl *NewFD;
4631 // FIXME: Missing call to CheckFunctionDeclaration().
4632 // FIXME: Mangling?
4633 // FIXME: Is the qualifier info correct?
4634 // FIXME: Is the DeclContext correct?
4635 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4636 Loc, Loc, DeclarationName(II),
4637 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004638 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004639 FD->hasPrototype(),
4640 false/*isConstexprSpecified*/);
4641 NewD = NewFD;
4642
4643 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004644 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004645
4646 // Fake up parameter variables; they are declared as if this were
4647 // a typedef.
4648 QualType FDTy = FD->getType();
4649 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4650 SmallVector<ParmVarDecl*, 16> Params;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00004651 for (const auto &AI : FT->param_types()) {
4652 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
Eli Friedmance3e2c82011-09-07 04:05:06 +00004653 Param->setScopeInfo(0, Params.size());
4654 Params.push_back(Param);
4655 }
David Blaikie9c70e042011-09-21 18:16:56 +00004656 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004657 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004658 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4659 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004660 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004661 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004662 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004663 if (VD->getQualifier()) {
4664 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004665 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004666 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004667 }
4668 return NewD;
4669}
4670
James Dennett634962f2012-06-14 21:40:34 +00004671/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004672/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004673void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004674 if (W.getUsed()) return; // only do this once
4675 W.setUsed(true);
4676 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4677 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004678 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Aaron Ballman36a53502014-01-16 13:03:14 +00004679 NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(),
4680 W.getLocation()));
4681 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Chris Lattnere6eab982009-09-08 18:10:11 +00004682 WeakTopLevelDecl.push_back(NewD);
4683 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4684 // to insert Decl at TU scope, sorry.
4685 DeclContext *SavedContext = CurContext;
4686 CurContext = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0098a4b2014-03-09 05:15:28 +00004687 NewD->setDeclContext(CurContext);
4688 NewD->setLexicalDeclContext(CurContext);
Chris Lattnere6eab982009-09-08 18:10:11 +00004689 PushOnScopeChains(NewD, S);
4690 CurContext = SavedContext;
4691 } else { // just add weak to existing
Aaron Ballman36a53502014-01-16 13:03:14 +00004692 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004693 }
4694}
4695
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004696void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4697 // It's valid to "forward-declare" #pragma weak, in which case we
4698 // have to do this.
4699 LoadExternalWeakUndeclaredIdentifiers();
4700 if (!WeakUndeclaredIdentifiers.empty()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00004701 NamedDecl *ND = nullptr;
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004702 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4703 if (VD->isExternC())
4704 ND = VD;
4705 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4706 if (FD->isExternC())
4707 ND = FD;
4708 if (ND) {
4709 if (IdentifierInfo *Id = ND->getIdentifier()) {
4710 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4711 = WeakUndeclaredIdentifiers.find(Id);
4712 if (I != WeakUndeclaredIdentifiers.end()) {
4713 WeakInfo W = I->second;
4714 DeclApplyPragmaWeak(S, ND, W);
4715 WeakUndeclaredIdentifiers[Id] = W;
4716 }
4717 }
4718 }
4719 }
4720}
4721
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004722/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4723/// it, apply them to D. This is a bit tricky because PD can have attributes
4724/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004725void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004726 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004727 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004728 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004729
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004730 // Walk the declarator structure, applying decl attributes that were in a type
4731 // position to the decl itself. This handles cases like:
4732 // int *__attr__(x)** D;
4733 // when X is a decl attribute.
4734 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4735 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004736 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004737
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004738 // Finally, apply any attributes on the decl itself.
4739 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004740 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004741}
John McCall28a6aea2009-11-04 02:18:39 +00004742
John McCall31168b02011-06-15 23:02:42 +00004743/// Is the given declaration allowed to use a forbidden type?
4744static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4745 // Private ivars are always okay. Unfortunately, people don't
4746 // always properly make their ivars private, even in system headers.
4747 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004748 // Function declarations in sys headers will be marked unavailable.
4749 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4750 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004751 return false;
4752
4753 // Require it to be declared in a system header.
4754 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4755}
4756
4757/// Handle a delayed forbidden-type diagnostic.
4758static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4759 Decl *decl) {
4760 if (decl && isForbiddenTypeAllowed(S, decl)) {
Aaron Ballman36a53502014-01-16 13:03:14 +00004761 decl->addAttr(UnavailableAttr::CreateImplicit(S.Context,
4762 "this system declaration uses an unsupported type",
4763 diag.Loc));
John McCall31168b02011-06-15 23:02:42 +00004764 return;
4765 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004766 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004767 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004768 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004769 // kind of forbidden type messages on unavailable functions.
4770 if (FD->hasAttr<UnavailableAttr>() &&
4771 diag.getForbiddenTypeDiagnostic() ==
4772 diag::err_arc_array_param_no_ownership) {
4773 diag.Triggered = true;
4774 return;
4775 }
4776 }
John McCall31168b02011-06-15 23:02:42 +00004777
4778 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4779 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4780 diag.Triggered = true;
4781}
4782
John McCall2ec85372012-05-07 06:16:41 +00004783void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4784 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004785 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004786 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004787
John McCall2ec85372012-05-07 06:16:41 +00004788 // When delaying diagnostics to run in the context of a parsed
4789 // declaration, we only want to actually emit anything if parsing
4790 // succeeds.
4791 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004792
John McCall2ec85372012-05-07 06:16:41 +00004793 // We emit all the active diagnostics in this pool or any of its
4794 // parents. In general, we'll get one pool for the decl spec
4795 // and a child pool for each declarator; in a decl group like:
4796 // deprecated_typedef foo, *bar, baz();
4797 // only the declarator pops will be passed decls. This is correct;
4798 // we really do need to consider delayed diagnostics from the decl spec
4799 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004800 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004801 do {
John McCall6347b682012-05-07 06:16:58 +00004802 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004803 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4804 // This const_cast is a bit lame. Really, Triggered should be mutable.
4805 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004806 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004807 continue;
4808
John McCallc1465822011-02-14 07:13:47 +00004809 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004810 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00004811 case DelayedDiagnostic::Unavailable:
4812 // Don't bother giving deprecation/unavailable diagnostics if
4813 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00004814 if (!decl->isInvalidDecl())
Ted Kremenekb79ee572013-12-18 23:30:06 +00004815 HandleDelayedAvailabilityCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004816 break;
4817
4818 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004819 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004820 break;
John McCall31168b02011-06-15 23:02:42 +00004821
4822 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004823 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004824 break;
John McCall86121512010-01-27 03:50:35 +00004825 }
4826 }
John McCall2ec85372012-05-07 06:16:41 +00004827 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004828}
4829
John McCall6347b682012-05-07 06:16:58 +00004830/// Given a set of delayed diagnostics, re-emit them as if they had
4831/// been delayed in the current context instead of in the given pool.
4832/// Essentially, this just moves them to the current pool.
4833void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4834 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4835 assert(curPool && "re-emitting in undelayed context not supported");
4836 curPool->steal(pool);
4837}
4838
John McCall28a6aea2009-11-04 02:18:39 +00004839static bool isDeclDeprecated(Decl *D) {
4840 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004841 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004842 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004843 // A category implicitly has the availability of the interface.
4844 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4845 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004846 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4847 return false;
4848}
4849
Ted Kremenekb79ee572013-12-18 23:30:06 +00004850static bool isDeclUnavailable(Decl *D) {
4851 do {
4852 if (D->isUnavailable())
4853 return true;
4854 // A category implicitly has the availability of the interface.
4855 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4856 return CatD->getClassInterface()->isUnavailable();
4857 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4858 return false;
4859}
4860
Eli Friedman971bfa12012-08-08 21:52:41 +00004861static void
Ted Kremenekb79ee572013-12-18 23:30:06 +00004862DoEmitAvailabilityWarning(Sema &S,
4863 DelayedDiagnostic::DDKind K,
4864 Decl *Ctx,
4865 const NamedDecl *D,
4866 StringRef Message,
4867 SourceLocation Loc,
4868 const ObjCInterfaceDecl *UnknownObjCClass,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004869 const ObjCPropertyDecl *ObjCProperty,
4870 bool ObjCPropertyAccess) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004871
4872 // Diagnostics for deprecated or unavailable.
4873 unsigned diag, diag_message, diag_fwdclass_message;
4874
4875 // Matches 'diag::note_property_attribute' options.
4876 unsigned property_note_select;
4877
4878 // Matches diag::note_availability_specified_here.
4879 unsigned available_here_select_kind;
4880
4881 // Don't warn if our current context is deprecated or unavailable.
4882 switch (K) {
4883 case DelayedDiagnostic::Deprecation:
4884 if (isDeclDeprecated(Ctx))
4885 return;
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004886 diag = !ObjCPropertyAccess ? diag::warn_deprecated
4887 : diag::warn_property_method_deprecated;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004888 diag_message = diag::warn_deprecated_message;
4889 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
4890 property_note_select = /* deprecated */ 0;
4891 available_here_select_kind = /* deprecated */ 2;
4892 break;
4893
4894 case DelayedDiagnostic::Unavailable:
4895 if (isDeclUnavailable(Ctx))
4896 return;
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004897 diag = !ObjCPropertyAccess ? diag::err_unavailable
4898 : diag::err_property_method_unavailable;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004899 diag_message = diag::err_unavailable_message;
4900 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
4901 property_note_select = /* unavailable */ 1;
4902 available_here_select_kind = /* unavailable */ 0;
4903 break;
4904
4905 default:
4906 llvm_unreachable("Neither a deprecation or unavailable kind");
4907 }
4908
Eli Friedman971bfa12012-08-08 21:52:41 +00004909 DeclarationName Name = D->getDeclName();
4910 if (!Message.empty()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004911 S.Diag(Loc, diag_message) << Name << Message;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004912 if (ObjCProperty)
4913 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4914 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004915 } else if (!UnknownObjCClass) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004916 S.Diag(Loc, diag) << Name;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004917 if (ObjCProperty)
4918 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4919 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004920 } else {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004921 S.Diag(Loc, diag_fwdclass_message) << Name;
Eli Friedman971bfa12012-08-08 21:52:41 +00004922 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4923 }
Ted Kremenekb79ee572013-12-18 23:30:06 +00004924
4925 S.Diag(D->getLocation(), diag::note_availability_specified_here)
4926 << D << available_here_select_kind;
Eli Friedman971bfa12012-08-08 21:52:41 +00004927}
4928
Ted Kremenekb79ee572013-12-18 23:30:06 +00004929void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD,
4930 Decl *Ctx) {
John McCall86121512010-01-27 03:50:35 +00004931 DD.Triggered = true;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004932 DoEmitAvailabilityWarning(*this,
4933 (DelayedDiagnostic::DDKind) DD.Kind,
4934 Ctx,
4935 DD.getDeprecationDecl(),
4936 DD.getDeprecationMessage(),
4937 DD.Loc,
4938 DD.getUnknownObjCClass(),
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004939 DD.getObjCProperty(), false);
John McCall28a6aea2009-11-04 02:18:39 +00004940}
4941
Ted Kremenekb79ee572013-12-18 23:30:06 +00004942void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
4943 NamedDecl *D, StringRef Message,
4944 SourceLocation Loc,
4945 const ObjCInterfaceDecl *UnknownObjCClass,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004946 const ObjCPropertyDecl *ObjCProperty,
4947 bool ObjCPropertyAccess) {
John McCall28a6aea2009-11-04 02:18:39 +00004948 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004949 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004950 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D,
4951 UnknownObjCClass,
4952 ObjCProperty,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004953 Message,
4954 ObjCPropertyAccess));
John McCall28a6aea2009-11-04 02:18:39 +00004955 return;
4956 }
4957
Ted Kremenekb79ee572013-12-18 23:30:06 +00004958 Decl *Ctx = cast<Decl>(getCurLexicalContext());
4959 DelayedDiagnostic::DDKind K;
4960 switch (AD) {
4961 case AD_Deprecation:
4962 K = DelayedDiagnostic::Deprecation;
4963 break;
4964 case AD_Unavailable:
4965 K = DelayedDiagnostic::Unavailable;
4966 break;
4967 }
4968
4969 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
Fariborz Jahanian89ea9612014-06-16 17:25:41 +00004970 UnknownObjCClass, ObjCProperty, ObjCPropertyAccess);
John McCall28a6aea2009-11-04 02:18:39 +00004971}