blob: ccbf9ac13567eb016c7865766f2a54afaff0cf69 [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"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +000017#include "clang/AST/CXXInheritance.h"
John McCall28a0cf72010-08-25 07:42:41 +000018#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000019#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000021#include "clang/AST/Expr.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
Chandler Carruthff4c4f02011-07-01 23:49:12 +000047static const FunctionType *getFunctionType(const Decl *D,
Ted Kremenek527042b2009-08-14 20:49:40 +000048 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000049 QualType Ty;
Chandler Carruthff4c4f02011-07-01 23:49:12 +000050 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000051 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000052 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000053 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000054 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000055 Ty = decl->getUnderlyingType();
56 else
57 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000058
Chris Lattner2c6fcf52008-06-26 18:38:35 +000059 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000060 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000061 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000062 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000063
John McCall9dd450b2009-09-21 23:43:11 +000064 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000065}
66
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000067// FIXME: We should provide an abstraction around a method or function
68// to provide the following bits of information.
69
Nuno Lopes518e3702009-12-20 23:11:08 +000070/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000071/// type (function or function-typed variable).
Chandler Carruthff4c4f02011-07-01 23:49:12 +000072static bool isFunction(const Decl *D) {
73 return getFunctionType(D, false) != NULL;
Ted Kremenek527042b2009-08-14 20:49:40 +000074}
75
76/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000077/// type (function or function-typed variable) or an Objective-C
78/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000079static bool isFunctionOrMethod(const Decl *D) {
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +000080 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000081}
82
Fariborz Jahanian4447e172009-05-15 23:15:03 +000083/// isFunctionOrMethodOrBlock - Return true if the given decl has function
84/// type (function or function-typed variable) or an Objective-C
85/// method or a block.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000086static bool isFunctionOrMethodOrBlock(const Decl *D) {
87 if (isFunctionOrMethod(D))
Fariborz Jahanian4447e172009-05-15 23:15:03 +000088 return true;
89 // check for block is more involved.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000090 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000091 QualType Ty = V->getType();
92 return Ty->isBlockPointerType();
93 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +000094 return isa<BlockDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000095}
96
John McCall3882ace2011-01-05 12:14:39 +000097/// Return true if the given decl has a declarator that should have
98/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000099static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +0000100 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000101 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
102 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +0000103}
104
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000105/// hasFunctionProto - Return true if the given decl has a argument
106/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000107/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000108static bool hasFunctionProto(const Decl *D) {
109 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000110 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000111 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000112 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000113 return true;
114 }
115}
116
117/// getFunctionOrMethodNumArgs - Return number of function or method
118/// arguments. It is an error to call this on a K&R function (use
119/// hasFunctionProto first).
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000120static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
121 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000122 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000123 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000124 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000125 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000126}
127
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000128static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
129 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000130 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000131 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000132 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000133
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000134 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000135}
136
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000137static QualType getFunctionOrMethodResultType(const Decl *D) {
138 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000139 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000140 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000141}
142
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000143static bool isFunctionOrMethodVariadic(const Decl *D) {
144 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000145 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000146 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000147 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000148 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000149 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000150 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000151 }
152}
153
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000154static bool isInstanceMethod(const Decl *D) {
155 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000156 return MethodDecl->isInstance();
157 return false;
158}
159
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000160static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000161 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000162 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000163 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000164
John McCall96fa4842010-05-17 21:00:27 +0000165 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
166 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000167 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000168
John McCall96fa4842010-05-17 21:00:27 +0000169 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000170
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000171 // FIXME: Should we walk the chain of classes?
172 return ClsName == &Ctx.Idents.get("NSString") ||
173 ClsName == &Ctx.Idents.get("NSMutableString");
174}
175
Daniel Dunbar980c6692008-09-26 03:32:58 +0000176static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000177 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000178 if (!PT)
179 return false;
180
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000181 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000182 if (!RT)
183 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000184
Daniel Dunbar980c6692008-09-26 03:32:58 +0000185 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000186 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000187 return false;
188
189 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
190}
191
Richard Smithb87c4652013-10-31 21:23:20 +0000192static unsigned getNumAttributeArgs(const AttributeList &Attr) {
193 // FIXME: Include the type in the argument list.
194 return Attr.getNumArgs() + Attr.hasParsedType();
195}
196
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000197/// \brief Check if the attribute has exactly as many args as Num. May
198/// output an error.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000199static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000200 unsigned Num) {
201 if (getNumAttributeArgs(Attr) != Num) {
Aaron Ballmanb7243382013-07-23 19:30:11 +0000202 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
203 << Attr.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000204 return false;
205 }
206
207 return true;
208}
209
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000210
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000211/// \brief Check if the attribute has at least as many args as Num. May
212/// output an error.
213static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000214 unsigned Num) {
215 if (getNumAttributeArgs(Attr) < Num) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000216 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
217 return false;
218 }
219
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000220 return true;
221}
222
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000223/// \brief If Expr is a valid integer constant, get the value of the integer
224/// expression and return success or failure. May output an error.
225static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
226 const Expr *Expr, uint32_t &Val,
227 unsigned Idx = UINT_MAX) {
228 llvm::APSInt I(32);
229 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
230 !Expr->isIntegerConstantExpr(I, S.Context)) {
231 if (Idx != UINT_MAX)
232 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
233 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
234 << Expr->getSourceRange();
235 else
236 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
237 << Attr.getName() << AANT_ArgumentIntegerConstant
238 << Expr->getSourceRange();
239 return false;
240 }
241 Val = (uint32_t)I.getZExtValue();
242 return true;
243}
244
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000245/// \brief Check if IdxExpr is a valid argument index for a function or
246/// instance method D. May output an error.
247///
248/// \returns true if IdxExpr is a valid index.
249static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
250 StringRef AttrName,
251 SourceLocation AttrLoc,
252 unsigned AttrArgNum,
253 const Expr *IdxExpr,
254 uint64_t &Idx)
255{
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000256 assert(isFunctionOrMethod(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000257
258 // In C++ the implicit 'this' function parameter also counts.
259 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000260 bool HP = hasFunctionProto(D);
261 bool HasImplicitThisParam = isInstanceMethod(D);
262 bool IV = HP && isFunctionOrMethodVariadic(D);
263 unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) +
264 HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000265
266 llvm::APSInt IdxInt;
267 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
268 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +0000269 std::string Name = std::string("'") + AttrName.str() + std::string("'");
270 S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str()
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000271 << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000272 return false;
273 }
274
275 Idx = IdxInt.getLimitedValue();
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000276 if (Idx < 1 || (!IV && Idx > NumArgs)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000277 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
278 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
279 return false;
280 }
281 Idx--; // Convert to zero-based.
282 if (HasImplicitThisParam) {
283 if (Idx == 0) {
284 S.Diag(AttrLoc,
285 diag::err_attribute_invalid_implicit_this_argument)
286 << AttrName << IdxExpr->getSourceRange();
287 return false;
288 }
289 --Idx;
290 }
291
292 return true;
293}
294
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000295/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
296/// If not emit an error and return false. If the argument is an identifier it
297/// will emit an error with a fixit hint and treat it as if it was a string
298/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000299bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
300 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000301 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000302 // Look for identifiers. If we have one emit a hint to fix it to a literal.
303 if (Attr.isArgIdent(ArgNum)) {
304 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000305 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000306 << Attr.getName() << AANT_ArgumentString
307 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000308 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000309 Str = Loc->Ident->getName();
310 if (ArgLocation)
311 *ArgLocation = Loc->Loc;
312 return true;
313 }
314
315 // Now check for an actual string literal.
316 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
317 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
318 if (ArgLocation)
319 *ArgLocation = ArgExpr->getLocStart();
320
321 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000322 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000323 << Attr.getName() << AANT_ArgumentString;
324 return false;
325 }
326
327 Str = Literal->getString();
328 return true;
329}
330
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000331/// \brief Applies the given attribute to the Decl without performing any
332/// additional semantic checking.
333template <typename AttrType>
334static void handleSimpleAttribute(Sema &S, Decl *D,
335 const AttributeList &Attr) {
336 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
337 Attr.getAttributeSpellingListIndex()));
338}
339
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000340///
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000341/// \brief Check if passed in Decl is a field or potentially shared global var
342/// \return true if the Decl is a field or potentially shared global variable
343///
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000344static bool mayBeSharedVariable(const Decl *D) {
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000345 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Richard Smithfd3834f2013-04-13 02:43:54 +0000346 return vd->hasGlobalStorage() && !vd->getTLSKind();
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000347
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000348 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000349}
350
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000351/// \brief Check if the passed-in expression is of type int or bool.
352static bool isIntOrBool(Expr *Exp) {
353 QualType QT = Exp->getType();
354 return QT->isBooleanType() || QT->isIntegerType();
355}
356
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000357
358// Check to see if the type is a smart pointer of some kind. We assume
359// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000360static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
361 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
362 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000363 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000364 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000365
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000366 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
367 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000368 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000369 return false;
370
371 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000372}
373
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000374/// \brief Check if passed in Decl is a pointer type.
375/// Note that this function may produce an error message.
376/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000377static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
378 const AttributeList &Attr) {
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000379 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000380 QualType QT = vd->getType();
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000381 if (QT->isAnyPointerType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000382 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000383
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000384 if (const RecordType *RT = QT->getAs<RecordType>()) {
385 // If it's an incomplete type, it could be a smart pointer; skip it.
386 // (We don't want to force template instantiation if we can avoid it,
387 // since that would alter the order in which templates are instantiated.)
388 if (RT->isIncompleteType())
389 return true;
390
391 if (threadSafetyCheckIsSmartPointer(S, RT))
392 return true;
393 }
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000394
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000395 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000396 << Attr.getName()->getName() << QT;
397 } else {
398 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
399 << Attr.getName();
400 }
401 return false;
402}
403
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000404/// \brief Checks that the passed in QualType either is of RecordType or points
405/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000406static const RecordType *getRecordType(QualType QT) {
407 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000408 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000409
410 // Now check if we point to record type.
411 if (const PointerType *PT = QT->getAs<PointerType>())
412 return PT->getPointeeType()->getAs<RecordType>();
413
414 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000415}
416
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000417
Jordy Rose740b0c22012-05-08 03:27:22 +0000418static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
419 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000420 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
421 if (RT->getDecl()->getAttr<LockableAttr>())
422 return true;
423 return false;
424}
425
426
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000427/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000428/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000429static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
430 QualType Ty) {
431 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000432
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000433 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000434 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000435 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000436 << Attr.getName() << Ty.getAsString();
437 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000438 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000439
Michael Hana9171bc2012-08-03 17:40:43 +0000440 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000441 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000442 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000443
444 // Allow smart pointers to be used as lockable objects.
445 // FIXME -- Check the type that the smart pointer points to.
446 if (threadSafetyCheckIsSmartPointer(S, RT))
447 return;
448
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000449 // Check if the type is lockable.
450 RecordDecl *RD = RT->getDecl();
451 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000452 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000453
454 // Else check if any base classes are lockable.
455 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
456 CXXBasePaths BPaths(false, false);
457 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
458 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000459 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000460
461 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
462 << Attr.getName() << Ty.getAsString();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000463}
464
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000465/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000466/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000467/// \param Sidx The attribute argument index to start checking with.
468/// \param ParamIdxOk Whether an argument can be indexing into a function
469/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000470static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000471 const AttributeList &Attr,
472 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000473 int Sidx = 0,
474 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000475 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000476 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000477
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000478 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000479 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000480 Args.push_back(ArgExp);
481 continue;
482 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000483
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000484 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000485 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000486 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000487 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000488 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000489 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000490 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000491 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000492
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000493 // We allow constant strings to be used as a placeholder for expressions
494 // that are not valid C++ syntax, but warn that they are ignored.
495 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
496 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000497 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000498 continue;
499 }
500
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000501 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000502
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000503 // A pointer to member expression of the form &MyClass::mu is treated
504 // specially -- we need to look at the type of the member.
505 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
506 if (UOp->getOpcode() == UO_AddrOf)
507 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
508 if (DRE->getDecl()->isCXXInstanceMember())
509 ArgTy = DRE->getDecl()->getType();
510
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000511 // First see if we can just cast to record type, or point to record type.
512 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000513
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000514 // Now check if we index into a record type function param.
515 if(!RT && ParamIdxOk) {
516 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000517 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
518 if(FD && IL) {
519 unsigned int NumParams = FD->getNumParams();
520 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000521 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
522 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
523 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000524 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
525 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000526 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000527 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000528 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000529 }
530 }
531
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000532 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000533
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000534 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000535 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000536}
537
Chris Lattner58418ff2008-06-29 00:16:31 +0000538//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000539// Attribute Implementations
540//===----------------------------------------------------------------------===//
541
Daniel Dunbar032db472008-07-31 22:40:48 +0000542// FIXME: All this manual attribute parsing code is gross. At the
543// least add some helper functions to check most argument patterns (#
544// and types of args).
545
Michael Hana9171bc2012-08-03 17:40:43 +0000546static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000547 const AttributeList &Attr) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000548 // D must be either a member field or global (potentially shared) variable.
549 if (!mayBeSharedVariable(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000550 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
551 << Attr.getName() << ExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000552 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000553 }
554
Michael Han3be3b442012-07-23 18:48:41 +0000555 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000556}
557
Michael Han3be3b442012-07-23 18:48:41 +0000558static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
559 if (!checkGuardedVarAttrCommon(S, D, Attr))
560 return;
Michael Hana9171bc2012-08-03 17:40:43 +0000561
Michael Han99315932013-01-24 16:46:58 +0000562 D->addAttr(::new (S.Context)
563 GuardedVarAttr(Attr.getRange(), S.Context,
564 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000565}
566
Michael Hana9171bc2012-08-03 17:40:43 +0000567static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000568 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000569 if (!checkGuardedVarAttrCommon(S, D, Attr))
570 return;
571
572 if (!threadSafetyCheckIsPointer(S, D, Attr))
573 return;
574
Michael Han99315932013-01-24 16:46:58 +0000575 D->addAttr(::new (S.Context)
576 PtGuardedVarAttr(Attr.getRange(), S.Context,
577 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000578}
579
Michael Hana9171bc2012-08-03 17:40:43 +0000580static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
581 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000582 Expr* &Arg) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000583 // D must be either a member field or global (potentially shared) variable.
584 if (!mayBeSharedVariable(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000585 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
586 << Attr.getName() << ExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000587 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000588 }
589
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000590 SmallVector<Expr*, 1> Args;
591 // check that all arguments are lockable objects
592 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
593 unsigned Size = Args.size();
594 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000595 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000596
Michael Han3be3b442012-07-23 18:48:41 +0000597 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000598
Michael Han3be3b442012-07-23 18:48:41 +0000599 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000600}
601
Michael Han3be3b442012-07-23 18:48:41 +0000602static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
603 Expr *Arg = 0;
604 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
605 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000606
Michael Han3be3b442012-07-23 18:48:41 +0000607 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
608}
609
Michael Hana9171bc2012-08-03 17:40:43 +0000610static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000611 const AttributeList &Attr) {
612 Expr *Arg = 0;
613 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
614 return;
615
616 if (!threadSafetyCheckIsPointer(S, D, Attr))
617 return;
618
619 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
620 S.Context, Arg));
621}
622
Michael Hana9171bc2012-08-03 17:40:43 +0000623static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000624 const AttributeList &Attr) {
Caitlin Sadowski086fb952011-09-16 00:35:54 +0000625 // FIXME: Lockable structs for C code.
David Blaikie021221d2013-07-29 18:24:03 +0000626 if (!isa<RecordDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000627 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
628 << Attr.getName() << ExpectedStructOrUnionOrClass;
Michael Han3be3b442012-07-23 18:48:41 +0000629 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000630 }
631
Michael Han3be3b442012-07-23 18:48:41 +0000632 return true;
633}
634
635static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
636 if (!checkLockableAttrCommon(S, D, Attr))
637 return;
638
639 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
640}
641
Michael Hana9171bc2012-08-03 17:40:43 +0000642static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000643 const AttributeList &Attr) {
644 if (!checkLockableAttrCommon(S, D, Attr))
645 return;
646
Michael Han99315932013-01-24 16:46:58 +0000647 D->addAttr(::new (S.Context)
648 ScopedLockableAttr(Attr.getRange(), S.Context,
649 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000650}
651
Michael Hana9171bc2012-08-03 17:40:43 +0000652static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
653 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000654 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000655 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000656 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000657
658 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000659 ValueDecl *VD = dyn_cast<ValueDecl>(D);
660 if (!VD || !mayBeSharedVariable(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000661 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
662 << Attr.getName() << ExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000663 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000664 }
665
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000666 // Check that this attribute only applies to lockable types.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000667 QualType QT = VD->getType();
668 if (!QT->isDependentType()) {
669 const RecordType *RT = getRecordType(QT);
670 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000671 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000672 << Attr.getName();
673 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000674 }
675 }
676
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000677 // Check that all arguments are lockable objects.
678 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000679 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000680 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000681
Michael Han3be3b442012-07-23 18:48:41 +0000682 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000683}
684
Michael Hana9171bc2012-08-03 17:40:43 +0000685static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000686 const AttributeList &Attr) {
687 SmallVector<Expr*, 1> Args;
688 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
689 return;
690
691 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000692 D->addAttr(::new (S.Context)
693 AcquiredAfterAttr(Attr.getRange(), S.Context,
694 StartArg, Args.size(),
695 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000696}
697
Michael Hana9171bc2012-08-03 17:40:43 +0000698static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000699 const AttributeList &Attr) {
700 SmallVector<Expr*, 1> Args;
701 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
702 return;
703
704 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000705 D->addAttr(::new (S.Context)
706 AcquiredBeforeAttr(Attr.getRange(), S.Context,
707 StartArg, Args.size(),
708 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000709}
710
Michael Hana9171bc2012-08-03 17:40:43 +0000711static bool checkLockFunAttrCommon(Sema &S, Decl *D,
712 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000713 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000714 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000715 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000716 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000717
Michael Han3be3b442012-07-23 18:48:41 +0000718 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000719}
720
Michael Hana9171bc2012-08-03 17:40:43 +0000721static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000722 const AttributeList &Attr) {
723 SmallVector<Expr*, 1> Args;
724 if (!checkLockFunAttrCommon(S, D, Attr, Args))
725 return;
726
727 unsigned Size = Args.size();
728 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000729 D->addAttr(::new (S.Context)
730 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
731 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000732}
733
Michael Hana9171bc2012-08-03 17:40:43 +0000734static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000735 const AttributeList &Attr) {
736 SmallVector<Expr*, 1> Args;
737 if (!checkLockFunAttrCommon(S, D, Attr, Args))
738 return;
739
740 unsigned Size = Args.size();
741 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000742 D->addAttr(::new (S.Context)
743 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
744 StartArg, Size,
745 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000746}
747
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000748static void handleAssertSharedLockAttr(Sema &S, Decl *D,
749 const AttributeList &Attr) {
750 SmallVector<Expr*, 1> Args;
751 if (!checkLockFunAttrCommon(S, D, Attr, Args))
752 return;
753
754 unsigned Size = Args.size();
755 Expr **StartArg = Size == 0 ? 0 : &Args[0];
756 D->addAttr(::new (S.Context)
757 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
758 Attr.getAttributeSpellingListIndex()));
759}
760
761static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
762 const AttributeList &Attr) {
763 SmallVector<Expr*, 1> Args;
764 if (!checkLockFunAttrCommon(S, D, Attr, Args))
765 return;
766
767 unsigned Size = Args.size();
768 Expr **StartArg = Size == 0 ? 0 : &Args[0];
769 D->addAttr(::new (S.Context)
770 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
771 StartArg, Size,
772 Attr.getAttributeSpellingListIndex()));
773}
774
775
Michael Hana9171bc2012-08-03 17:40:43 +0000776static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
777 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000778 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000779 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000780 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000781
Aaron Ballman00e99962013-08-31 01:11:41 +0000782 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000783 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000784 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000785 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000786 }
787
788 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000789 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000790
Michael Han3be3b442012-07-23 18:48:41 +0000791 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000792}
793
Michael Hana9171bc2012-08-03 17:40:43 +0000794static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000795 const AttributeList &Attr) {
796 SmallVector<Expr*, 2> Args;
797 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
798 return;
799
Michael Han99315932013-01-24 16:46:58 +0000800 D->addAttr(::new (S.Context)
801 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000802 Attr.getArgAsExpr(0),
803 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000804 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000805}
806
Michael Hana9171bc2012-08-03 17:40:43 +0000807static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000808 const AttributeList &Attr) {
809 SmallVector<Expr*, 2> Args;
810 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
811 return;
812
Michael Han99315932013-01-24 16:46:58 +0000813 D->addAttr(::new (S.Context)
814 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000815 Attr.getArgAsExpr(0),
816 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000817 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000818}
819
Michael Hana9171bc2012-08-03 17:40:43 +0000820static bool checkLocksRequiredCommon(Sema &S, Decl *D,
821 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000822 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000823 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000824 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000825
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000826 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000827 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000828 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000829 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000830
Michael Han3be3b442012-07-23 18:48:41 +0000831 return true;
832}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000833
Michael Hana9171bc2012-08-03 17:40:43 +0000834static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000835 const AttributeList &Attr) {
836 SmallVector<Expr*, 1> Args;
837 if (!checkLocksRequiredCommon(S, D, Attr, Args))
838 return;
839
840 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000841 D->addAttr(::new (S.Context)
842 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
843 StartArg, Args.size(),
844 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000845}
846
Michael Hana9171bc2012-08-03 17:40:43 +0000847static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000848 const AttributeList &Attr) {
849 SmallVector<Expr*, 1> Args;
850 if (!checkLocksRequiredCommon(S, D, Attr, Args))
851 return;
852
853 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000854 D->addAttr(::new (S.Context)
855 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
856 StartArg, Args.size(),
857 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000858}
859
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000860static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000861 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000862 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000863 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000864 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000865 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000866 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000867 Expr **StartArg = Size == 0 ? 0 : &Args[0];
868
Michael Han99315932013-01-24 16:46:58 +0000869 D->addAttr(::new (S.Context)
870 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
871 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000872}
873
874static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000875 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000876 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000877 SmallVector<Expr*, 1> Args;
878 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
879 unsigned Size = Args.size();
880 if (Size == 0)
881 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000882
Michael Han99315932013-01-24 16:46:58 +0000883 D->addAttr(::new (S.Context)
884 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
885 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000886}
887
888static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000889 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000890 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000891 return;
892
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000893 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000894 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000895 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000896 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000897 if (Size == 0)
898 return;
899 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000900
Michael Han99315932013-01-24 16:46:58 +0000901 D->addAttr(::new (S.Context)
902 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
903 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000904}
905
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000906static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000907 ConsumableAttr::ConsumedState DefaultState;
908
909 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000910 IdentifierLoc *IL = Attr.getArgAsIdent(0);
911 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
912 DefaultState)) {
913 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
914 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000915 return;
916 }
David Blaikie16f76d22013-09-06 01:28:43 +0000917 } else {
918 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
919 << Attr.getName() << AANT_ArgumentIdentifier;
920 return;
921 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000922
923 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000924 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000925 Attr.getAttributeSpellingListIndex()));
926}
927
928static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
929 const AttributeList &Attr) {
930 ASTContext &CurrContext = S.getASTContext();
931 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
932
933 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
934 if (!RD->hasAttr<ConsumableAttr>()) {
935 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
936 RD->getNameAsString();
937
938 return false;
939 }
940 }
941
942 return true;
943}
944
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000945
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000946static void handleCallableWhenAttr(Sema &S, Decl *D,
947 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000948 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
949 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000950
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000951 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
952 return;
953
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000954 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
955 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
956 CallableWhenAttr::ConsumedState CallableState;
957
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000958 StringRef StateString;
959 SourceLocation Loc;
960 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
961 return;
962
963 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000964 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000965 S.Diag(Loc, diag::warn_attribute_type_not_supported)
966 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000967 return;
968 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000969
970 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000971 }
972
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000973 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000974 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
975 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000976}
977
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000978
DeLesley Hutchins69391772013-10-17 23:23:53 +0000979static void handleParamTypestateAttr(Sema &S, Decl *D,
980 const AttributeList &Attr) {
981 if (!checkAttributeNumArgs(S, Attr, 1)) return;
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000982
DeLesley Hutchins69391772013-10-17 23:23:53 +0000983 ParamTypestateAttr::ConsumedState ParamState;
984
985 if (Attr.isArgIdent(0)) {
986 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
987 StringRef StateString = Ident->Ident->getName();
988
989 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
990 ParamState)) {
991 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
992 << Attr.getName() << StateString;
993 return;
994 }
995 } else {
996 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
997 Attr.getName() << AANT_ArgumentIdentifier;
998 return;
999 }
1000
1001 // FIXME: This check is currently being done in the analysis. It can be
1002 // enabled here only after the parser propagates attributes at
1003 // template specialization definition, not declaration.
1004 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
1005 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1006 //
1007 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1008 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1009 // ReturnType.getAsString();
1010 // return;
1011 //}
1012
1013 D->addAttr(::new (S.Context)
1014 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
1015 Attr.getAttributeSpellingListIndex()));
1016}
1017
1018
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001019static void handleReturnTypestateAttr(Sema &S, Decl *D,
1020 const AttributeList &Attr) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001021 if (!checkAttributeNumArgs(S, Attr, 1)) return;
1022
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001023 ReturnTypestateAttr::ConsumedState ReturnState;
1024
1025 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +00001026 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1027 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1028 ReturnState)) {
1029 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
1030 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001031 return;
1032 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001033 } else {
1034 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1035 Attr.getName() << AANT_ArgumentIdentifier;
1036 return;
1037 }
1038
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001039 // FIXME: This check is currently being done in the analysis. It can be
1040 // enabled here only after the parser propagates attributes at
1041 // template specialization definition, not declaration.
1042 //QualType ReturnType;
1043 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001044 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1045 // ReturnType = Param->getType();
1046 //
1047 //} else if (const CXXConstructorDecl *Constructor =
1048 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001049 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1050 //
1051 //} else {
1052 //
1053 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1054 //}
1055 //
1056 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1057 //
1058 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1059 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1060 // ReturnType.getAsString();
1061 // return;
1062 //}
1063
1064 D->addAttr(::new (S.Context)
1065 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1066 Attr.getAttributeSpellingListIndex()));
1067}
1068
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001069
1070static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001071 if (!checkAttributeNumArgs(S, Attr, 1))
1072 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001073
1074 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1075 return;
1076
1077 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001078 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001079 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1080 StringRef Param = Ident->Ident->getName();
1081 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1082 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1083 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001084 return;
1085 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001086 } else {
1087 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1088 Attr.getName() << AANT_ArgumentIdentifier;
1089 return;
1090 }
1091
1092 D->addAttr(::new (S.Context)
1093 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1094 Attr.getAttributeSpellingListIndex()));
1095}
1096
Chris Wailes9385f9f2013-10-29 20:28:41 +00001097static void handleTestTypestateAttr(Sema &S, Decl *D,
1098 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001099 if (!checkAttributeNumArgs(S, Attr, 1))
1100 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001101
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001102 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1103 return;
1104
Chris Wailes9385f9f2013-10-29 20:28:41 +00001105 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001106 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001107 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1108 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001109 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001110 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1111 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001112 return;
1113 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001114 } else {
1115 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1116 Attr.getName() << AANT_ArgumentIdentifier;
1117 return;
1118 }
1119
1120 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001121 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001122 Attr.getAttributeSpellingListIndex()));
1123}
1124
Chandler Carruthedc2c642011-07-02 00:01:44 +00001125static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1126 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001127 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001128 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001129}
1130
Chandler Carruthedc2c642011-07-02 00:01:44 +00001131static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001132 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001133 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001134 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001135 // If the alignment is less than or equal to 8 bits, the packed attribute
1136 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001137 if (!FD->getType()->isDependentType() &&
1138 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001139 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001140 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001141 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001142 else
Michael Han99315932013-01-24 16:46:58 +00001143 FD->addAttr(::new (S.Context)
1144 PackedAttr(Attr.getRange(), S.Context,
1145 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001146 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001147 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001148}
1149
Chandler Carruthedc2c642011-07-02 00:01:44 +00001150static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek1f672822010-02-18 03:08:58 +00001151 // The IBAction attributes only apply to instance methods.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001152 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek1f672822010-02-18 03:08:58 +00001153 if (MD->isInstanceMethod()) {
Michael Han99315932013-01-24 16:46:58 +00001154 D->addAttr(::new (S.Context)
1155 IBActionAttr(Attr.getRange(), S.Context,
1156 Attr.getAttributeSpellingListIndex()));
Ted Kremenek1f672822010-02-18 03:08:58 +00001157 return;
1158 }
1159
Ted Kremenekd68ec812011-02-04 06:54:16 +00001160 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek1f672822010-02-18 03:08:58 +00001161}
1162
Ted Kremenek7fd17232011-09-29 07:02:25 +00001163static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1164 // The IBOutlet/IBOutletCollection attributes only apply to instance
1165 // variables or properties of Objective-C classes. The outlet must also
1166 // have an object reference type.
1167 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1168 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001169 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001170 << Attr.getName() << VD->getType() << 0;
1171 return false;
1172 }
1173 }
1174 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1175 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001176 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001177 << Attr.getName() << PD->getType() << 1;
1178 return false;
1179 }
1180 }
1181 else {
1182 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1183 return false;
1184 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001185
Ted Kremenek7fd17232011-09-29 07:02:25 +00001186 return true;
1187}
1188
Chandler Carruthedc2c642011-07-02 00:01:44 +00001189static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001190 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001191 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001192
Michael Han99315932013-01-24 16:46:58 +00001193 D->addAttr(::new (S.Context)
1194 IBOutletAttr(Attr.getRange(), S.Context,
1195 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001196}
1197
Chandler Carruthedc2c642011-07-02 00:01:44 +00001198static void handleIBOutletCollection(Sema &S, Decl *D,
1199 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001200
1201 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001202 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001203 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1204 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001205 return;
1206 }
1207
Ted Kremenek7fd17232011-09-29 07:02:25 +00001208 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001209 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001210
Richard Smithb1f9a282013-10-31 01:56:18 +00001211 ParsedType PT;
1212
1213 if (Attr.hasParsedType())
1214 PT = Attr.getTypeArg();
1215 else {
1216 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1217 S.getScopeForContext(D->getDeclContext()->getParent()));
1218 if (!PT) {
1219 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1220 return;
1221 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001222 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001223
Richard Smithb87c4652013-10-31 21:23:20 +00001224 TypeSourceInfo *QTLoc = 0;
1225 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1226 if (!QTLoc)
1227 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001228
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001229 // Diagnose use of non-object type in iboutletcollection attribute.
1230 // FIXME. Gnu attribute extension ignores use of builtin types in
1231 // attributes. So, __attribute__((iboutletcollection(char))) will be
1232 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001233 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001234 S.Diag(Attr.getLoc(),
1235 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1236 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001237 return;
1238 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001239
Michael Han99315932013-01-24 16:46:58 +00001240 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001241 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001242 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001243}
1244
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001245static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001246 if (const RecordType *UT = T->getAsUnionType())
1247 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1248 RecordDecl *UD = UT->getDecl();
1249 for (RecordDecl::field_iterator it = UD->field_begin(),
1250 itend = UD->field_end(); it != itend; ++it) {
1251 QualType QT = it->getType();
1252 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1253 T = QT;
1254 return;
1255 }
1256 }
1257 }
1258}
1259
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001260static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopese881ce22012-06-18 16:39:04 +00001261 if (!isFunctionOrMethod(D)) {
1262 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001263 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopese881ce22012-06-18 16:39:04 +00001264 return;
1265 }
1266
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001267 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1268 return;
1269
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001270 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001271 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001272 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001273 uint64_t Idx;
1274 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1275 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001276 return;
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001277
1278 // check if the function argument is of an integer type
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001279 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001280 if (!T->isIntegerType()) {
Aaron Ballman9d695092013-07-30 14:10:17 +00001281 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1282 << Attr.getName() << AANT_ArgumentIntegerConstant
1283 << Ex->getSourceRange();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001284 return;
1285 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001286 SizeArgs.push_back(Idx);
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001287 }
1288
1289 // check if the function returns a pointer
1290 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1291 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001292 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001293 }
1294
Michael Han99315932013-01-24 16:46:58 +00001295 D->addAttr(::new (S.Context)
1296 AllocSizeAttr(Attr.getRange(), S.Context,
1297 SizeArgs.data(), SizeArgs.size(),
1298 Attr.getAttributeSpellingListIndex()));
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001299}
1300
Chandler Carruthedc2c642011-07-02 00:01:44 +00001301static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001302 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1303 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001304 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001305 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001306 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001307 return;
1308 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001309
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001310 SmallVector<unsigned, 8> NonNullArgs;
1311 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001312 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001313 uint64_t Idx;
1314 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1315 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001316 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001317
1318 // Is the function argument a pointer type?
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001319 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001320 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001321
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001322 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001323 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +00001324 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +00001325 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001326 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001327 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001328
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001329 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001330 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001331
1332 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1333 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001334 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001335 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1336 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001337 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001338 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001339 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001340 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001341
Ted Kremenek22813f42010-10-21 18:49:36 +00001342 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001343 if (NonNullArgs.empty()) {
1344 // Warn the trivial case only if attribute is not coming from a
1345 // macro instantiation.
1346 if (Attr.getLoc().isFileID())
1347 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001348 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001349 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001350 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001351
Nick Lewyckye1121512013-01-24 01:12:16 +00001352 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001353 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001354 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001355 D->addAttr(::new (S.Context)
1356 NonNullAttr(Attr.getRange(), S.Context, start, size,
1357 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001358}
1359
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001360static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1361 switch (K) {
1362 case OwnershipAttr::Holds: return "'ownership_holds'";
1363 case OwnershipAttr::Takes: return "'ownership_takes'";
1364 case OwnershipAttr::Returns: return "'ownership_returns'";
1365 }
1366 llvm_unreachable("unknown ownership");
1367}
1368
Chandler Carruthedc2c642011-07-02 00:01:44 +00001369static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001370 // This attribute must be applied to a function declaration. The first
1371 // argument to the attribute must be an identifier, the name of the resource,
1372 // for example: malloc. The following arguments must be argument indexes, the
1373 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001374 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001375 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001376 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001377
Aaron Ballman00e99962013-08-31 01:11:41 +00001378 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001379 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001380 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001381 return;
1382 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001383
Richard Smith852e9ce2013-11-27 01:46:48 +00001384 // Figure out our Kind.
1385 OwnershipAttr::OwnershipKind K =
1386 OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0,
1387 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001388
Richard Smith852e9ce2013-11-27 01:46:48 +00001389 // Check arguments.
1390 switch (K) {
1391 case OwnershipAttr::Takes:
1392 case OwnershipAttr::Holds:
1393 if (AL.getNumArgs() < 2) {
1394 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
1395 return;
1396 }
1397 break;
1398 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001399 if (AL.getNumArgs() > 2) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001400 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001401 return;
1402 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001403 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001404 }
1405
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001406 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001407 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1408 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001409 return;
1410 }
1411
Richard Smith852e9ce2013-11-27 01:46:48 +00001412 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001413
1414 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001415 StringRef ModuleName = Module->getName();
1416 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1417 ModuleName.size() > 4) {
1418 ModuleName = ModuleName.drop_front(2).drop_back(2);
1419 Module = &S.PP.getIdentifierTable().get(ModuleName);
1420 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001421
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001422 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001423 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1424 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001425 uint64_t Idx;
1426 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
Aaron Ballman00e99962013-08-31 01:11:41 +00001427 AL.getLoc(), i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001428 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001429
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001430 // Is the function argument a pointer type?
1431 QualType T = getFunctionOrMethodArgType(D, Idx);
1432 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001433 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001434 case OwnershipAttr::Takes:
1435 case OwnershipAttr::Holds:
1436 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1437 Err = 0;
1438 break;
1439 case OwnershipAttr::Returns:
1440 if (!T->isIntegerType())
1441 Err = 1;
1442 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001443 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001444 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001445 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001446 << Ex->getSourceRange();
1447 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001448 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001449
1450 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001451 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001452 i = D->specific_attr_begin<OwnershipAttr>(),
1453 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001454 // FIXME: A returns attribute should conflict with any returns attribute
1455 // with a different index too.
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001456 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1457 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1458 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1459 << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind());
1460 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001461 }
1462 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001463 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001464 }
1465
1466 unsigned* start = OwnershipArgs.data();
1467 unsigned size = OwnershipArgs.size();
1468 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001469
Michael Han99315932013-01-24 16:46:58 +00001470 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001471 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001472 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001473}
1474
Chandler Carruthedc2c642011-07-02 00:01:44 +00001475static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001476 // Check the attribute arguments.
1477 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001478 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1479 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001480 return;
1481 }
1482
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001483 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001484
Rafael Espindolac18086a2010-02-23 22:00:30 +00001485 // gcc rejects
1486 // class c {
1487 // static int a __attribute__((weakref ("v2")));
1488 // static int b() __attribute__((weakref ("f3")));
1489 // };
1490 // and ignores the attributes of
1491 // void f(void) {
1492 // static int a __attribute__((weakref ("v2")));
1493 // }
1494 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001495 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001496 if (!Ctx->isFileContext()) {
1497 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +00001498 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +00001499 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001500 }
1501
1502 // The GCC manual says
1503 //
1504 // At present, a declaration to which `weakref' is attached can only
1505 // be `static'.
1506 //
1507 // It also says
1508 //
1509 // Without a TARGET,
1510 // given as an argument to `weakref' or to `alias', `weakref' is
1511 // equivalent to `weak'.
1512 //
1513 // gcc 4.4.1 will accept
1514 // int a7 __attribute__((weakref));
1515 // as
1516 // int a7 __attribute__((weak));
1517 // This looks like a bug in gcc. We reject that for now. We should revisit
1518 // it if this behaviour is actually used.
1519
Rafael Espindolac18086a2010-02-23 22:00:30 +00001520 // GCC rejects
1521 // static ((alias ("y"), weakref)).
1522 // Should we? How to check that weakref is before or after alias?
1523
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001524 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1525 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1526 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001527 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001528 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001529 // GCC will accept anything as the argument of weakref. Should we
1530 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001531 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1532 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001533
Michael Han99315932013-01-24 16:46:58 +00001534 D->addAttr(::new (S.Context)
1535 WeakRefAttr(Attr.getRange(), S.Context,
1536 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001537}
1538
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001539static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1540 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001541 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001542 return;
1543
Douglas Gregore8bbc122011-09-02 00:18:52 +00001544 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001545 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1546 return;
1547 }
1548
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001549 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001550
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001551 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001552 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001553}
1554
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001555static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001556 if (D->hasAttr<HotAttr>()) {
1557 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1558 << Attr.getName() << "hot";
1559 return;
1560 }
1561
Michael Han99315932013-01-24 16:46:58 +00001562 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1563 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001564}
1565
1566static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001567 if (D->hasAttr<ColdAttr>()) {
1568 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1569 << Attr.getName() << "cold";
1570 return;
1571 }
1572
Michael Han99315932013-01-24 16:46:58 +00001573 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1574 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001575}
1576
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001577static void handleTLSModelAttr(Sema &S, Decl *D,
1578 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001579 StringRef Model;
1580 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001581 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001582 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001583 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001584
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001585 if (!cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001586 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1587 << Attr.getName() << ExpectedTLSVar;
1588 return;
1589 }
1590
1591 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001592 if (Model != "global-dynamic" && Model != "local-dynamic"
1593 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001594 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001595 return;
1596 }
1597
Michael Han99315932013-01-24 16:46:58 +00001598 D->addAttr(::new (S.Context)
1599 TLSModelAttr(Attr.getRange(), S.Context, Model,
1600 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001601}
1602
Chandler Carruthedc2c642011-07-02 00:01:44 +00001603static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001604 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001605 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001606 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001607 D->addAttr(::new (S.Context)
1608 MallocAttr(Attr.getRange(), S.Context,
1609 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001610 return;
1611 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001612 }
1613
Ted Kremenek08479ae2009-08-15 00:51:46 +00001614 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001615}
1616
Chandler Carruthedc2c642011-07-02 00:01:44 +00001617static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001618 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001619 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1620 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001621 return;
1622 }
1623
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001624 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1625 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001626}
1627
Chandler Carruthedc2c642011-07-02 00:01:44 +00001628static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001629 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001630
1631 if (S.CheckNoReturnAttr(attr)) return;
1632
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001633 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001634 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001635 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001636 return;
1637 }
1638
Michael Han99315932013-01-24 16:46:58 +00001639 D->addAttr(::new (S.Context)
1640 NoReturnAttr(attr.getRange(), S.Context,
1641 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001642}
1643
1644bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001645 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001646 attr.setInvalid();
1647 return true;
1648 }
1649
1650 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001651}
1652
Chandler Carruthedc2c642011-07-02 00:01:44 +00001653static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1654 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001655
1656 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1657 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001658 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1659 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001660 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1661 && !VD->getType()->isFunctionPointerType())) {
1662 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001663 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001664 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001665 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001666 return;
1667 }
1668 }
1669
Michael Han99315932013-01-24 16:46:58 +00001670 D->addAttr(::new (S.Context)
1671 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1672 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001673}
1674
John Thompsoncdb847ba2010-08-09 21:53:52 +00001675// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001676static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001677/*
1678 Returning a Vector Class in Registers
1679
Eric Christopherbc638a82010-12-01 22:13:54 +00001680 According to the PPU ABI specifications, a class with a single member of
1681 vector type is returned in memory when used as the return value of a function.
1682 This results in inefficient code when implementing vector classes. To return
1683 the value in a single vector register, add the vecreturn attribute to the
1684 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001685
1686 Example:
1687
1688 struct Vector
1689 {
1690 __vector float xyzw;
1691 } __attribute__((vecreturn));
1692
1693 Vector Add(Vector lhs, Vector rhs)
1694 {
1695 Vector result;
1696 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1697 return result; // This will be returned in a register
1698 }
1699*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001700 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001701 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1702 return;
1703 }
1704
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001705 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001706 int count = 0;
1707
1708 if (!isa<CXXRecordDecl>(record)) {
1709 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1710 return;
1711 }
1712
1713 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1714 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1715 return;
1716 }
1717
Eric Christopherbc638a82010-12-01 22:13:54 +00001718 for (RecordDecl::field_iterator iter = record->field_begin();
1719 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001720 if ((count == 1) || !iter->getType()->isVectorType()) {
1721 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1722 return;
1723 }
1724 count++;
1725 }
1726
Michael Han99315932013-01-24 16:46:58 +00001727 D->addAttr(::new (S.Context)
1728 VecReturnAttr(Attr.getRange(), S.Context,
1729 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001730}
1731
Richard Smithe233fbf2013-01-28 22:42:45 +00001732static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1733 const AttributeList &Attr) {
1734 if (isa<ParmVarDecl>(D)) {
1735 // [[carries_dependency]] can only be applied to a parameter if it is a
1736 // parameter of a function declaration or lambda.
1737 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1738 S.Diag(Attr.getLoc(),
1739 diag::err_carries_dependency_param_not_function_decl);
1740 return;
1741 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001742 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001743
1744 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1745 Attr.getRange(), S.Context,
1746 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001747}
1748
Chandler Carruthedc2c642011-07-02 00:01:44 +00001749static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001750 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00001751 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001752 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001753 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001754 return;
1755 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001756
Michael Han99315932013-01-24 16:46:58 +00001757 D->addAttr(::new (S.Context)
1758 UnusedAttr(Attr.getRange(), S.Context,
1759 Attr.getAttributeSpellingListIndex()));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001760}
1761
Chandler Carruthedc2c642011-07-02 00:01:44 +00001762static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001763 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001764 if (VD->hasLocalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001765 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1766 return;
1767 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001768 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001769 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001770 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001771 return;
1772 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001773
Michael Han99315932013-01-24 16:46:58 +00001774 D->addAttr(::new (S.Context)
1775 UsedAttr(Attr.getRange(), S.Context,
1776 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001777}
1778
Chandler Carruthedc2c642011-07-02 00:01:44 +00001779static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001780 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001781 if (Attr.getNumArgs() > 1) {
1782 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001783 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001784 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001785
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001786 uint32_t priority = ConstructorAttr::DefaultPriority;
1787 if (Attr.getNumArgs() > 0 &&
1788 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1789 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001790
Michael Han99315932013-01-24 16:46:58 +00001791 D->addAttr(::new (S.Context)
1792 ConstructorAttr(Attr.getRange(), S.Context, priority,
1793 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001794}
1795
Chandler Carruthedc2c642011-07-02 00:01:44 +00001796static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001797 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001798 if (Attr.getNumArgs() > 1) {
1799 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001800 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001801 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001802
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001803 uint32_t priority = ConstructorAttr::DefaultPriority;
1804 if (Attr.getNumArgs() > 0 &&
1805 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1806 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001807
Michael Han99315932013-01-24 16:46:58 +00001808 D->addAttr(::new (S.Context)
1809 DestructorAttr(Attr.getRange(), S.Context, priority,
1810 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001811}
1812
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001813template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001814static void handleAttrWithMessage(Sema &S, Decl *D,
1815 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001816 unsigned NumArgs = Attr.getNumArgs();
1817 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001818 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001819 return;
1820 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001821
1822 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001823 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001824 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001825 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001826
Michael Han99315932013-01-24 16:46:58 +00001827 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1828 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001829}
1830
Ted Kremenek28eace62013-11-23 01:01:34 +00001831static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
1832 const AttributeList &Attr) {
Ted Kremenek7559b472013-11-23 22:29:11 +00001833 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek28eace62013-11-23 01:01:34 +00001834
1835 if (!Parm) {
1836 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 1;
1837 return;
1838 }
1839
1840 D->addAttr(::new (S.Context)
1841 ObjCSuppressProtocolAttr(Attr.getRange(), S.Context, Parm->Ident,
1842 Attr.getAttributeSpellingListIndex()));
1843}
1844
Jordy Rose740b0c22012-05-08 03:27:22 +00001845static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1846 IdentifierInfo *Platform,
1847 VersionTuple Introduced,
1848 VersionTuple Deprecated,
1849 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001850 StringRef PlatformName
1851 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1852 if (PlatformName.empty())
1853 PlatformName = Platform->getName();
1854
1855 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1856 // of these steps are needed).
1857 if (!Introduced.empty() && !Deprecated.empty() &&
1858 !(Introduced <= Deprecated)) {
1859 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1860 << 1 << PlatformName << Deprecated.getAsString()
1861 << 0 << Introduced.getAsString();
1862 return true;
1863 }
1864
1865 if (!Introduced.empty() && !Obsoleted.empty() &&
1866 !(Introduced <= Obsoleted)) {
1867 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1868 << 2 << PlatformName << Obsoleted.getAsString()
1869 << 0 << Introduced.getAsString();
1870 return true;
1871 }
1872
1873 if (!Deprecated.empty() && !Obsoleted.empty() &&
1874 !(Deprecated <= Obsoleted)) {
1875 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1876 << 2 << PlatformName << Obsoleted.getAsString()
1877 << 1 << Deprecated.getAsString();
1878 return true;
1879 }
1880
1881 return false;
1882}
1883
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001884/// \brief Check whether the two versions match.
1885///
1886/// If either version tuple is empty, then they are assumed to match. If
1887/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1888static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1889 bool BeforeIsOkay) {
1890 if (X.empty() || Y.empty())
1891 return true;
1892
1893 if (X == Y)
1894 return true;
1895
1896 if (BeforeIsOkay && X < Y)
1897 return true;
1898
1899 return false;
1900}
1901
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001902AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001903 IdentifierInfo *Platform,
1904 VersionTuple Introduced,
1905 VersionTuple Deprecated,
1906 VersionTuple Obsoleted,
1907 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001908 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001909 bool Override,
1910 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001911 VersionTuple MergedIntroduced = Introduced;
1912 VersionTuple MergedDeprecated = Deprecated;
1913 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001914 bool FoundAny = false;
1915
Rafael Espindolac67f2232012-05-10 02:50:16 +00001916 if (D->hasAttrs()) {
1917 AttrVec &Attrs = D->getAttrs();
1918 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1919 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1920 if (!OldAA) {
1921 ++i;
1922 continue;
1923 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001924
Rafael Espindolac67f2232012-05-10 02:50:16 +00001925 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1926 if (OldPlatform != Platform) {
1927 ++i;
1928 continue;
1929 }
1930
1931 FoundAny = true;
1932 VersionTuple OldIntroduced = OldAA->getIntroduced();
1933 VersionTuple OldDeprecated = OldAA->getDeprecated();
1934 VersionTuple OldObsoleted = OldAA->getObsoleted();
1935 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001936
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001937 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1938 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1939 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1940 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001941 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001942 if (Override) {
1943 int Which = -1;
1944 VersionTuple FirstVersion;
1945 VersionTuple SecondVersion;
1946 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1947 Which = 0;
1948 FirstVersion = OldIntroduced;
1949 SecondVersion = Introduced;
1950 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1951 Which = 1;
1952 FirstVersion = Deprecated;
1953 SecondVersion = OldDeprecated;
1954 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1955 Which = 2;
1956 FirstVersion = Obsoleted;
1957 SecondVersion = OldObsoleted;
1958 }
1959
1960 if (Which == -1) {
1961 Diag(OldAA->getLocation(),
1962 diag::warn_mismatched_availability_override_unavail)
1963 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1964 } else {
1965 Diag(OldAA->getLocation(),
1966 diag::warn_mismatched_availability_override)
1967 << Which
1968 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1969 << FirstVersion.getAsString() << SecondVersion.getAsString();
1970 }
1971 Diag(Range.getBegin(), diag::note_overridden_method);
1972 } else {
1973 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1974 Diag(Range.getBegin(), diag::note_previous_attribute);
1975 }
1976
Rafael Espindolac67f2232012-05-10 02:50:16 +00001977 Attrs.erase(Attrs.begin() + i);
1978 --e;
1979 continue;
1980 }
1981
1982 VersionTuple MergedIntroduced2 = MergedIntroduced;
1983 VersionTuple MergedDeprecated2 = MergedDeprecated;
1984 VersionTuple MergedObsoleted2 = MergedObsoleted;
1985
1986 if (MergedIntroduced2.empty())
1987 MergedIntroduced2 = OldIntroduced;
1988 if (MergedDeprecated2.empty())
1989 MergedDeprecated2 = OldDeprecated;
1990 if (MergedObsoleted2.empty())
1991 MergedObsoleted2 = OldObsoleted;
1992
1993 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1994 MergedIntroduced2, MergedDeprecated2,
1995 MergedObsoleted2)) {
1996 Attrs.erase(Attrs.begin() + i);
1997 --e;
1998 continue;
1999 }
2000
2001 MergedIntroduced = MergedIntroduced2;
2002 MergedDeprecated = MergedDeprecated2;
2003 MergedObsoleted = MergedObsoleted2;
2004 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002005 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002006 }
2007
2008 if (FoundAny &&
2009 MergedIntroduced == Introduced &&
2010 MergedDeprecated == Deprecated &&
2011 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002012 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002013
Ted Kremenekb5445722013-04-06 00:34:27 +00002014 // Only create a new attribute if !Override, but we want to do
2015 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002016 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002017 MergedDeprecated, MergedObsoleted) &&
2018 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002019 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2020 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002021 Obsoleted, IsUnavailable, Message,
2022 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002023 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002024 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002025}
2026
Chandler Carruthedc2c642011-07-02 00:01:44 +00002027static void handleAvailabilityAttr(Sema &S, Decl *D,
2028 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002029 if (!checkAttributeNumArgs(S, Attr, 1))
2030 return;
2031 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00002032 unsigned Index = Attr.getAttributeSpellingListIndex();
2033
Aaron Ballman00e99962013-08-31 01:11:41 +00002034 IdentifierInfo *II = Platform->Ident;
2035 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2036 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2037 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002038
Rafael Espindolac231fab2013-01-08 21:30:32 +00002039 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2040 if (!ND) {
2041 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2042 return;
2043 }
2044
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002045 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2046 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2047 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002048 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002049 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00002050 if (const StringLiteral *SE =
2051 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002052 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002053
Aaron Ballman00e99962013-08-31 01:11:41 +00002054 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002055 Introduced.Version,
2056 Deprecated.Version,
2057 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002058 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00002059 /*Override=*/false,
2060 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002061 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002062 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002063}
2064
John McCalld041a9b2013-02-20 01:54:26 +00002065template <class T>
2066static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2067 typename T::VisibilityType value,
2068 unsigned attrSpellingListIndex) {
2069 T *existingAttr = D->getAttr<T>();
2070 if (existingAttr) {
2071 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2072 if (existingValue == value)
2073 return NULL;
2074 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2075 S.Diag(range.getBegin(), diag::note_previous_attribute);
2076 D->dropAttr<T>();
2077 }
2078 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2079}
2080
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002081VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002082 VisibilityAttr::VisibilityType Vis,
2083 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002084 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2085 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002086}
2087
John McCalld041a9b2013-02-20 01:54:26 +00002088TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2089 TypeVisibilityAttr::VisibilityType Vis,
2090 unsigned AttrSpellingListIndex) {
2091 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2092 AttrSpellingListIndex);
2093}
2094
2095static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2096 bool isTypeVisibility) {
2097 // Visibility attributes don't mean anything on a typedef.
2098 if (isa<TypedefNameDecl>(D)) {
2099 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2100 << Attr.getName();
2101 return;
2102 }
2103
2104 // 'type_visibility' can only go on a type or namespace.
2105 if (isTypeVisibility &&
2106 !(isa<TagDecl>(D) ||
2107 isa<ObjCInterfaceDecl>(D) ||
2108 isa<NamespaceDecl>(D))) {
2109 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2110 << Attr.getName() << ExpectedTypeOrNamespace;
2111 return;
2112 }
2113
Benjamin Kramer70370212013-09-09 15:08:57 +00002114 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002115 StringRef TypeStr;
2116 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002117 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002118 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002119
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002120 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002121 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002122 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002123 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002124 return;
2125 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002126
2127 // Complain about attempts to use protected visibility on targets
2128 // (like Darwin) that don't support it.
2129 if (type == VisibilityAttr::Protected &&
2130 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2131 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2132 type = VisibilityAttr::Default;
2133 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002134
Michael Han99315932013-01-24 16:46:58 +00002135 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002136 clang::Attr *newAttr;
2137 if (isTypeVisibility) {
2138 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2139 (TypeVisibilityAttr::VisibilityType) type,
2140 Index);
2141 } else {
2142 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2143 }
2144 if (newAttr)
2145 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002146}
2147
Chandler Carruthedc2c642011-07-02 00:01:44 +00002148static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2149 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002150 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002151 if (!Attr.isArgIdent(0)) {
2152 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2153 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002154 return;
2155 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002156
Aaron Ballman682ee422013-09-11 19:47:58 +00002157 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2158 ObjCMethodFamilyAttr::FamilyKind F;
2159 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2160 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2161 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002162 return;
2163 }
2164
Aaron Ballman682ee422013-09-11 19:47:58 +00002165 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCall31168b02011-06-15 23:02:42 +00002166 !method->getResultType()->isObjCObjectPointerType()) {
2167 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2168 << method->getResultType();
2169 // Ignore the attribute.
2170 return;
2171 }
2172
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002173 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman682ee422013-09-11 19:47:58 +00002174 S.Context, F));
John McCall86bc21f2011-03-02 11:33:24 +00002175}
2176
Chandler Carruthedc2c642011-07-02 00:01:44 +00002177static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002178 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002179 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002180 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002181 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2182 return;
2183 }
2184 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002185 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2186 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002187 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002188 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2189 return;
2190 }
2191 }
2192 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002193 // It is okay to include this attribute on properties, e.g.:
2194 //
2195 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2196 //
2197 // In this case it follows tradition and suppresses an error in the above
2198 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002199 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002200 }
Michael Han99315932013-01-24 16:46:58 +00002201 D->addAttr(::new (S.Context)
2202 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2203 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002204}
2205
Chandler Carruthedc2c642011-07-02 00:01:44 +00002206static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002207 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002208 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002209 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002210 return;
2211 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002212
Aaron Ballman00e99962013-08-31 01:11:41 +00002213 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002214 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002215 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2216 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2217 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002218 return;
2219 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002220
Michael Han99315932013-01-24 16:46:58 +00002221 D->addAttr(::new (S.Context)
2222 BlocksAttr(Attr.getRange(), S.Context, type,
2223 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002224}
2225
Chandler Carruthedc2c642011-07-02 00:01:44 +00002226static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002227 // check the attribute arguments.
2228 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002229 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002230 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002231 }
2232
Aaron Ballman18a78382013-11-21 00:28:23 +00002233 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002234 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002235 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002236 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002237 if (E->isTypeDependent() || E->isValueDependent() ||
2238 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002239 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002240 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002241 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002242 return;
2243 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002244
John McCallb46f2872011-09-09 07:56:05 +00002245 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002246 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2247 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002248 return;
2249 }
John McCallb46f2872011-09-09 07:56:05 +00002250
2251 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002252 }
2253
Aaron Ballman18a78382013-11-21 00:28:23 +00002254 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002255 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002256 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002257 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002258 if (E->isTypeDependent() || E->isValueDependent() ||
2259 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002260 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002261 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002262 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002263 return;
2264 }
2265 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002266
John McCallb46f2872011-09-09 07:56:05 +00002267 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002268 // FIXME: This error message could be improved, it would be nice
2269 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002270 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2271 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002272 return;
2273 }
2274 }
2275
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002276 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002277 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002278 if (isa<FunctionNoProtoType>(FT)) {
2279 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2280 return;
2281 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002282
Chris Lattner9363e312009-03-17 23:03:47 +00002283 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002284 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002285 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002286 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002287 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002288 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002289 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002290 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002291 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002292 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2293 if (!BD->isVariadic()) {
2294 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2295 return;
2296 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002297 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002298 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002299 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002300 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002301 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002302 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002303 int m = Ty->isFunctionPointerType() ? 0 : 1;
2304 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002305 return;
2306 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002307 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002308 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002309 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002310 return;
2311 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002312 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002313 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002314 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002315 return;
2316 }
Michael Han99315932013-01-24 16:46:58 +00002317 D->addAttr(::new (S.Context)
2318 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2319 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002320}
2321
Chandler Carruthedc2c642011-07-02 00:01:44 +00002322static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002323 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002324 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002325 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002326 return;
2327 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002328
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002329 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2330 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2331 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002332 return;
2333 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002334 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2335 if (MD->getResultType()->isVoidType()) {
2336 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2337 << Attr.getName() << 1;
2338 return;
2339 }
2340
Michael Han99315932013-01-24 16:46:58 +00002341 D->addAttr(::new (S.Context)
2342 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2343 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002344}
2345
Chandler Carruthedc2c642011-07-02 00:01:44 +00002346static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002347 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002348 if (isa<CXXRecordDecl>(D)) {
2349 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2350 return;
2351 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002352 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2353 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002354 return;
2355 }
2356
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002357 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002358
Michael Han99315932013-01-24 16:46:58 +00002359 nd->addAttr(::new (S.Context)
2360 WeakAttr(Attr.getRange(), S.Context,
2361 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002362}
2363
Chandler Carruthedc2c642011-07-02 00:01:44 +00002364static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002365 // weak_import only applies to variable & function declarations.
2366 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002367 if (!D->canBeWeakImported(isDef)) {
2368 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002369 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2370 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002371 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002372 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002373 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002374 // Nothing to warn about here.
2375 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002376 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002377 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002378
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002379 return;
2380 }
2381
Michael Han99315932013-01-24 16:46:58 +00002382 D->addAttr(::new (S.Context)
2383 WeakImportAttr(Attr.getRange(), S.Context,
2384 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002385}
2386
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002387// Handles reqd_work_group_size and work_group_size_hint.
2388static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002389 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002390 uint32_t WGSize[3];
2391 for (unsigned i = 0; i < 3; ++i)
2392 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002393 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002394
2395 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2396 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2397 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2398 if (!(A->getXDim() == WGSize[0] &&
2399 A->getYDim() == WGSize[1] &&
2400 A->getZDim() == WGSize[2])) {
2401 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2402 Attr.getName();
2403 }
2404 }
2405
2406 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2407 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2408 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2409 if (!(A->getXDim() == WGSize[0] &&
2410 A->getYDim() == WGSize[1] &&
2411 A->getZDim() == WGSize[2])) {
2412 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2413 Attr.getName();
2414 }
2415 }
2416
2417 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2418 D->addAttr(::new (S.Context)
2419 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002420 WGSize[0], WGSize[1], WGSize[2],
2421 Attr.getAttributeSpellingListIndex()));
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002422 else
2423 D->addAttr(::new (S.Context)
2424 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002425 WGSize[0], WGSize[1], WGSize[2],
2426 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002427}
2428
Joey Goulyaba589c2013-03-08 09:42:32 +00002429static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2430 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2431
Aaron Ballman00e99962013-08-31 01:11:41 +00002432 if (!Attr.hasParsedType()) {
2433 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2434 << Attr.getName() << 1;
2435 return;
2436 }
2437
Richard Smithb87c4652013-10-31 21:23:20 +00002438 TypeSourceInfo *ParmTSI = 0;
2439 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2440 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002441
2442 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2443 (ParmType->isBooleanType() ||
2444 !ParmType->isIntegralType(S.getASTContext()))) {
2445 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2446 << ParmType;
2447 return;
2448 }
2449
2450 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2451 D->hasAttr<VecTypeHintAttr>()) {
2452 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
Richard Smithb87c4652013-10-31 21:23:20 +00002453 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002454 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2455 return;
2456 }
2457 }
2458
2459 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Richard Smithb87c4652013-10-31 21:23:20 +00002460 ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002461}
2462
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002463SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002464 StringRef Name,
2465 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002466 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2467 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002468 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002469 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2470 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002471 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002472 }
Michael Han99315932013-01-24 16:46:58 +00002473 return ::new (Context) SectionAttr(Range, Context, Name,
2474 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002475}
2476
Chandler Carruthedc2c642011-07-02 00:01:44 +00002477static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002478 // Make sure that there is a string literal as the sections's single
2479 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002480 StringRef Str;
2481 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002482 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002483 return;
Mike Stump11289f42009-09-09 15:08:12 +00002484
Chris Lattner30ba6742009-08-10 19:03:04 +00002485 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002486 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002487 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002488 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002489 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002490 return;
2491 }
Mike Stump11289f42009-09-09 15:08:12 +00002492
Chris Lattner20aee9b2010-01-12 20:58:53 +00002493 // This attribute cannot be applied to local variables.
2494 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002495 S.Diag(LiteralLoc, diag::err_attribute_section_local_variable);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002496 return;
2497 }
Michael Han99315932013-01-24 16:46:58 +00002498
2499 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002500 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002501 if (NewAttr)
2502 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002503}
2504
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002505
Chandler Carruthedc2c642011-07-02 00:01:44 +00002506static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002507 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002508 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002509 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002510 } else {
Michael Han99315932013-01-24 16:46:58 +00002511 D->addAttr(::new (S.Context)
2512 NoThrowAttr(Attr.getRange(), S.Context,
2513 Attr.getAttributeSpellingListIndex()));
Douglas Gregor88336832011-06-15 05:45:11 +00002514 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002515}
2516
Chandler Carruthedc2c642011-07-02 00:01:44 +00002517static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002518 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002519 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002520 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002521 } else {
Michael Han99315932013-01-24 16:46:58 +00002522 D->addAttr(::new (S.Context)
2523 ConstAttr(Attr.getRange(), S.Context,
2524 Attr.getAttributeSpellingListIndex() ));
Douglas Gregor88336832011-06-15 05:45:11 +00002525 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002526}
2527
Chandler Carruthedc2c642011-07-02 00:01:44 +00002528static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002529 VarDecl *VD = cast<VarDecl>(D);
2530 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002531 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002532 return;
2533 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002534
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002535 Expr *E = Attr.getArgAsExpr(0);
2536 SourceLocation Loc = E->getExprLoc();
2537 FunctionDecl *FD = 0;
2538 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002539
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002540 // gcc only allows for simple identifiers. Since we support more than gcc, we
2541 // will warn the user.
2542 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2543 if (DRE->hasQualifier())
2544 S.Diag(Loc, diag::warn_cleanup_ext);
2545 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2546 NI = DRE->getNameInfo();
2547 if (!FD) {
2548 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2549 << NI.getName();
2550 return;
2551 }
2552 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2553 if (ULE->hasExplicitTemplateArgs())
2554 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002555 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2556 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002557 if (!FD) {
2558 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2559 << NI.getName();
2560 if (ULE->getType() == S.Context.OverloadTy)
2561 S.NoteAllOverloadCandidates(ULE);
2562 return;
2563 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002564 } else {
2565 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002566 return;
2567 }
2568
Anders Carlssond277d792009-01-31 01:16:18 +00002569 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002570 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2571 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002572 return;
2573 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002574
Anders Carlsson723f55d2009-02-07 23:16:50 +00002575 // We're currently more strict than GCC about what function types we accept.
2576 // If this ever proves to be a problem it should be easy to fix.
2577 QualType Ty = S.Context.getPointerType(VD->getType());
2578 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002579 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2580 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002581 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2582 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002583 return;
2584 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002585
Michael Han99315932013-01-24 16:46:58 +00002586 D->addAttr(::new (S.Context)
2587 CleanupAttr(Attr.getRange(), S.Context, FD,
2588 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002589}
2590
Mike Stumpd3bb5572009-07-24 19:02:52 +00002591/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002592/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002593static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002594 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002595 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002596 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002597 return;
2598 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002599
Aaron Ballman00e99962013-08-31 01:11:41 +00002600 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002601 uint64_t ArgIdx;
2602 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2603 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002604 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002605
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002606 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002607 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002608
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002609 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2610 if (not_nsstring_type &&
2611 !isCFStringType(Ty, S.Context) &&
2612 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002613 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002614 // FIXME: Should highlight the actual expression that has the wrong type.
2615 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002616 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002617 << IdxExpr->getSourceRange();
2618 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002619 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002620 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002621 if (!isNSStringType(Ty, S.Context) &&
2622 !isCFStringType(Ty, S.Context) &&
2623 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002624 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002625 // FIXME: Should highlight the actual expression that has the wrong type.
2626 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002627 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002628 << IdxExpr->getSourceRange();
2629 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002630 }
2631
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002632 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2633 // because that has corrected for the implicit this parameter, and is zero-
2634 // based. The attribute expects what the user wrote explicitly.
2635 llvm::APSInt Val;
2636 IdxExpr->EvaluateAsInt(Val, S.Context);
2637
Michael Han99315932013-01-24 16:46:58 +00002638 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002639 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002640 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002641}
2642
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002643enum FormatAttrKind {
2644 CFStringFormat,
2645 NSStringFormat,
2646 StrftimeFormat,
2647 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002648 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002649 InvalidFormat
2650};
2651
2652/// getFormatAttrKind - Map from format attribute names to supported format
2653/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002654static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002655 return llvm::StringSwitch<FormatAttrKind>(Format)
2656 // Check for formats that get handled specially.
2657 .Case("NSString", NSStringFormat)
2658 .Case("CFString", CFStringFormat)
2659 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002660
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002661 // Otherwise, check for supported formats.
2662 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2663 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2664 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002665
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002666 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2667 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002668}
2669
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002670/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002671/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002672static void handleInitPriorityAttr(Sema &S, Decl *D,
2673 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002674 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002675 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2676 return;
2677 }
2678
Aaron Ballman4a611152013-11-27 16:34:09 +00002679 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002680 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2681 Attr.setInvalid();
2682 return;
2683 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002684 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002685 if (S.Context.getAsArrayType(T))
2686 T = S.Context.getBaseElementType(T);
2687 if (!T->getAs<RecordType>()) {
2688 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2689 Attr.setInvalid();
2690 return;
2691 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002692
2693 Expr *E = Attr.getArgAsExpr(0);
2694 uint32_t prioritynum;
2695 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002696 Attr.setInvalid();
2697 return;
2698 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002699
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002700 if (prioritynum < 101 || prioritynum > 65535) {
2701 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002702 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002703 Attr.setInvalid();
2704 return;
2705 }
Michael Han99315932013-01-24 16:46:58 +00002706 D->addAttr(::new (S.Context)
2707 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2708 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002709}
2710
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002711FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2712 IdentifierInfo *Format, int FormatIdx,
2713 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002714 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002715 // Check whether we already have an equivalent format attribute.
2716 for (specific_attr_iterator<FormatAttr>
2717 i = D->specific_attr_begin<FormatAttr>(),
2718 e = D->specific_attr_end<FormatAttr>();
2719 i != e ; ++i) {
2720 FormatAttr *f = *i;
2721 if (f->getType() == Format &&
2722 f->getFormatIdx() == FormatIdx &&
2723 f->getFirstArg() == FirstArg) {
2724 // If we don't have a valid location for this attribute, adopt the
2725 // location.
2726 if (f->getLocation().isInvalid())
2727 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002728 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002729 }
2730 }
2731
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002732 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2733 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002734}
2735
Mike Stumpd3bb5572009-07-24 19:02:52 +00002736/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002737/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002738static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002739 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002740 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002741 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002742 return;
2743 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002744
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002745 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002746 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002747 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002748 return;
2749 }
2750
Chandler Carruth743682b2010-11-16 08:35:43 +00002751 // In C++ the implicit 'this' function parameter also counts, and they are
2752 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002753 bool HasImplicitThisParam = isInstanceMethod(D);
2754 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002755
Aaron Ballman00e99962013-08-31 01:11:41 +00002756 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2757 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002758
2759 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002760 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002761 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002762 // If we've modified the string name, we need a new identifier for it.
2763 II = &S.Context.Idents.get(Format);
2764 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002765
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002766 // Check for supported formats.
2767 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002768
2769 if (Kind == IgnoredFormat)
2770 return;
2771
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002772 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002773 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman00e99962013-08-31 01:11:41 +00002774 << "format" << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002775 return;
2776 }
2777
2778 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002779 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002780 uint32_t Idx;
2781 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002782 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002783
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002784 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002785 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002786 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002787 return;
2788 }
2789
2790 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002791 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002792
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002793 if (HasImplicitThisParam) {
2794 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002795 S.Diag(Attr.getLoc(),
2796 diag::err_format_attribute_implicit_this_format_string)
2797 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002798 return;
2799 }
2800 ArgIdx--;
2801 }
Mike Stump11289f42009-09-09 15:08:12 +00002802
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002803 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002804 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002805
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002806 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002807 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002808 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2809 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002810 return;
2811 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002812 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002813 // FIXME: do we need to check if the type is NSString*? What are the
2814 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002815 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002816 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002817 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2818 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002819 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002820 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002821 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002822 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002823 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002824 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2825 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002826 return;
2827 }
2828
2829 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002830 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002831 uint32_t FirstArg;
2832 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002833 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002834
2835 // check if the function is variadic if the 3rd argument non-zero
2836 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002837 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002838 ++NumArgs; // +1 for ...
2839 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002840 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002841 return;
2842 }
2843 }
2844
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002845 // strftime requires FirstArg to be 0 because it doesn't read from any
2846 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002847 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002848 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002849 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2850 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002851 return;
2852 }
2853 // if 0 it disables parameter checking (to use with e.g. va_list)
2854 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002855 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002856 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002857 return;
2858 }
2859
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002860 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002861 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002862 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002863 if (NewAttr)
2864 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002865}
2866
Chandler Carruthedc2c642011-07-02 00:01:44 +00002867static void handleTransparentUnionAttr(Sema &S, Decl *D,
2868 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002869 // Try to find the underlying union declaration.
2870 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002871 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002872 if (TD && TD->getUnderlyingType()->isUnionType())
2873 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2874 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002875 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002876
2877 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002878 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002879 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002880 return;
2881 }
2882
John McCallf937c022011-10-07 06:10:15 +00002883 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002884 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002885 diag::warn_transparent_union_attribute_not_definition);
2886 return;
2887 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002888
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002889 RecordDecl::field_iterator Field = RD->field_begin(),
2890 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002891 if (Field == FieldEnd) {
2892 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2893 return;
2894 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002895
David Blaikie40ed2972012-06-06 20:45:41 +00002896 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002897 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002898 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002899 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002900 diag::warn_transparent_union_attribute_floating)
2901 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002902 return;
2903 }
2904
2905 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2906 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2907 for (; Field != FieldEnd; ++Field) {
2908 QualType FieldType = Field->getType();
2909 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2910 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2911 // Warn if we drop the attribute.
2912 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002913 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002914 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002915 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002916 diag::warn_transparent_union_attribute_field_size_align)
2917 << isSize << Field->getDeclName() << FieldBits;
2918 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002919 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002920 diag::note_transparent_union_first_field_size_align)
2921 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002922 return;
2923 }
2924 }
2925
Michael Han99315932013-01-24 16:46:58 +00002926 RD->addAttr(::new (S.Context)
2927 TransparentUnionAttr(Attr.getRange(), S.Context,
2928 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002929}
2930
Chandler Carruthedc2c642011-07-02 00:01:44 +00002931static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002932 // Make sure that there is a string literal as the annotation's single
2933 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002934 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002935 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002936 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002937
2938 // Don't duplicate annotations that are already set.
2939 for (specific_attr_iterator<AnnotateAttr>
2940 i = D->specific_attr_begin<AnnotateAttr>(),
2941 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002942 if ((*i)->getAnnotation() == Str)
2943 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002944 }
Michael Han99315932013-01-24 16:46:58 +00002945
2946 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002947 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002948 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002949}
2950
Chandler Carruthedc2c642011-07-02 00:01:44 +00002951static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002952 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002953 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002954 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2955 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002956 return;
2957 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002958
Richard Smith848e1f12013-02-01 08:12:08 +00002959 if (Attr.getNumArgs() == 0) {
2960 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2961 true, 0, Attr.getAttributeSpellingListIndex()));
2962 return;
2963 }
2964
Aaron Ballman00e99962013-08-31 01:11:41 +00002965 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002966 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2967 S.Diag(Attr.getEllipsisLoc(),
2968 diag::err_pack_expansion_without_parameter_packs);
2969 return;
2970 }
2971
2972 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2973 return;
2974
2975 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2976 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002977}
2978
2979void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002980 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002981 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2982 SourceLocation AttrLoc = AttrRange.getBegin();
2983
Richard Smith1dba27c2013-01-29 09:02:09 +00002984 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002985 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002986 // C++11 [dcl.align]p1:
2987 // An alignment-specifier may be applied to a variable or to a class
2988 // data member, but it shall not be applied to a bit-field, a function
2989 // parameter, the formal parameter of a catch clause, or a variable
2990 // declared with the register storage class specifier. An
2991 // alignment-specifier may also be applied to the declaration of a class
2992 // or enumeration type.
2993 // C11 6.7.5/2:
2994 // An alignment attribute shall not be specified in a declaration of
2995 // a typedef, or a bit-field, or a function, or a parameter, or an
2996 // object declared with the register storage-class specifier.
2997 int DiagKind = -1;
2998 if (isa<ParmVarDecl>(D)) {
2999 DiagKind = 0;
3000 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3001 if (VD->getStorageClass() == SC_Register)
3002 DiagKind = 1;
3003 if (VD->isExceptionVariable())
3004 DiagKind = 2;
3005 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3006 if (FD->isBitField())
3007 DiagKind = 3;
3008 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00003009 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3010 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00003011 << (TmpAttr.isC11() ? ExpectedVariableOrField
3012 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003013 return;
3014 }
3015 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003016 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smithbc8caaf2013-02-22 04:55:39 +00003017 << TmpAttr.isC11() << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003018 return;
3019 }
3020 }
3021
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003022 if (E->isTypeDependent() || E->isValueDependent()) {
3023 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003024 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3025 AA->setPackExpansion(IsPackExpansion);
3026 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003027 return;
3028 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003029
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003030 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00003031 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00003032 ExprResult ICE
3033 = VerifyIntegerConstantExpression(E, &Alignment,
3034 diag::err_aligned_attribute_argument_not_int,
3035 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003036 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003037 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003038
3039 // C++11 [dcl.align]p2:
3040 // -- if the constant expression evaluates to zero, the alignment
3041 // specifier shall have no effect
3042 // C11 6.7.5p6:
3043 // An alignment specification of zero has no effect.
3044 if (!(TmpAttr.isAlignas() && !Alignment) &&
3045 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003046 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3047 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003048 return;
3049 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003050
Richard Smith848e1f12013-02-01 08:12:08 +00003051 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00003052 // We've already verified it's a power of 2, now let's make sure it's
3053 // 8192 or less.
3054 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00003055 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00003056 << E->getSourceRange();
3057 return;
3058 }
3059 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003060
Richard Smith44c247f2013-02-22 08:32:16 +00003061 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3062 ICE.take(), SpellingListIndex);
3063 AA->setPackExpansion(IsPackExpansion);
3064 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003065}
3066
Michael Hanaf02bbe2013-02-01 01:19:17 +00003067void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003068 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003069 // FIXME: Cache the number on the Attr object if non-dependent?
3070 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003071 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3072 SpellingListIndex);
3073 AA->setPackExpansion(IsPackExpansion);
3074 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003075}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003076
Richard Smith848e1f12013-02-01 08:12:08 +00003077void Sema::CheckAlignasUnderalignment(Decl *D) {
3078 assert(D->hasAttrs() && "no attributes on decl");
3079
3080 QualType Ty;
3081 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3082 Ty = VD->getType();
3083 else
3084 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00003085 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003086 return;
3087
3088 // C++11 [dcl.align]p5, C11 6.7.5/4:
3089 // The combined effect of all alignment attributes in a declaration shall
3090 // not specify an alignment that is less strict than the alignment that
3091 // would otherwise be required for the entity being declared.
3092 AlignedAttr *AlignasAttr = 0;
3093 unsigned Align = 0;
3094 for (specific_attr_iterator<AlignedAttr>
3095 I = D->specific_attr_begin<AlignedAttr>(),
3096 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3097 if (I->isAlignmentDependent())
3098 return;
3099 if (I->isAlignas())
3100 AlignasAttr = *I;
3101 Align = std::max(Align, I->getAlignment(Context));
3102 }
3103
3104 if (AlignasAttr && Align) {
3105 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3106 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3107 if (NaturalAlign > RequestedAlign)
3108 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3109 << Ty << (unsigned)NaturalAlign.getQuantity();
3110 }
3111}
3112
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003113/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003114/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003115///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003116/// Despite what would be logical, the mode attribute is a decl attribute, not a
3117/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3118/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003119static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003120 // This attribute isn't documented, but glibc uses it. It changes
3121 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00003122 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00003123 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3124 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003125 return;
3126 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003127
Aaron Ballman00e99962013-08-31 01:11:41 +00003128 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3129 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003130
3131 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003132 if (Str.startswith("__") && Str.endswith("__"))
3133 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003134
3135 unsigned DestWidth = 0;
3136 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003137 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003138 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003139 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003140 switch (Str[0]) {
3141 case 'Q': DestWidth = 8; break;
3142 case 'H': DestWidth = 16; break;
3143 case 'S': DestWidth = 32; break;
3144 case 'D': DestWidth = 64; break;
3145 case 'X': DestWidth = 96; break;
3146 case 'T': DestWidth = 128; break;
3147 }
3148 if (Str[1] == 'F') {
3149 IntegerMode = false;
3150 } else if (Str[1] == 'C') {
3151 IntegerMode = false;
3152 ComplexMode = true;
3153 } else if (Str[1] != 'I') {
3154 DestWidth = 0;
3155 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003156 break;
3157 case 4:
3158 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3159 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003160 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003161 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003162 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003163 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003164 break;
3165 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003166 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003167 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003168 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003169 case 11:
3170 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003171 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003172 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003173 }
3174
3175 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003176 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003177 OldTy = TD->getUnderlyingType();
3178 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3179 OldTy = VD->getType();
3180 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003181 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003182 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003183 return;
3184 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003185
John McCall9dd450b2009-09-21 23:43:11 +00003186 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003187 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3188 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003189 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003190 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3191 } else if (ComplexMode) {
3192 if (!OldTy->isComplexType())
3193 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3194 } else {
3195 if (!OldTy->isFloatingType())
3196 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3197 }
3198
Mike Stump87c57ac2009-05-16 07:39:55 +00003199 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3200 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003201 // FIXME: Make sure floating-point mappings are accurate
3202 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003203 if (!DestWidth) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003204 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003205 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003206 }
3207
3208 QualType NewTy;
3209
3210 if (IntegerMode)
3211 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3212 OldTy->isSignedIntegerType());
3213 else
3214 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3215
3216 if (NewTy.isNull()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003217 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003218 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003219 }
3220
Eli Friedman4735374e2009-03-03 06:41:03 +00003221 if (ComplexMode) {
3222 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003223 }
3224
3225 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003226 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3227 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3228 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003229 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003230
3231 D->addAttr(::new (S.Context)
3232 ModeAttr(Attr.getRange(), S.Context, Name,
3233 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003234}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003235
Chandler Carruthedc2c642011-07-02 00:01:44 +00003236static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003237 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3238 if (!VD->hasGlobalStorage())
3239 S.Diag(Attr.getLoc(),
3240 diag::warn_attribute_requires_functions_or_static_globals)
3241 << Attr.getName();
3242 } else if (!isFunctionOrMethod(D)) {
3243 S.Diag(Attr.getLoc(),
3244 diag::warn_attribute_requires_functions_or_static_globals)
3245 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003246 return;
3247 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003248
Michael Han99315932013-01-24 16:46:58 +00003249 D->addAttr(::new (S.Context)
3250 NoDebugAttr(Attr.getRange(), S.Context,
3251 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003252}
3253
Chandler Carruthedc2c642011-07-02 00:01:44 +00003254static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003255 if (S.LangOpts.CUDA) {
Michael Han99315932013-01-24 16:46:58 +00003256 D->addAttr(::new (S.Context)
3257 CUDAConstantAttr(Attr.getRange(), S.Context,
3258 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003259 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003260 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003261 }
3262}
3263
Chandler Carruthedc2c642011-07-02 00:01:44 +00003264static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003265 if (S.LangOpts.CUDA) {
3266 // check the attribute arguments.
3267 if (Attr.getNumArgs() != 0) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003268 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3269 << Attr.getName() << 0;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003270 return;
3271 }
3272
Michael Han99315932013-01-24 16:46:58 +00003273 D->addAttr(::new (S.Context)
3274 CUDADeviceAttr(Attr.getRange(), S.Context,
3275 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003276 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003277 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003278 }
3279}
3280
Chandler Carruthedc2c642011-07-02 00:01:44 +00003281static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003282 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003283 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003284 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00003285 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie6adc78e2013-02-18 22:06:02 +00003286 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003287 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3288 << FD->getType()
David Blaikie6adc78e2013-02-18 22:06:02 +00003289 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003290 "void");
3291 } else {
3292 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3293 << FD->getType();
3294 }
3295 return;
3296 }
3297
Michael Han99315932013-01-24 16:46:58 +00003298 D->addAttr(::new (S.Context)
3299 CUDAGlobalAttr(Attr.getRange(), S.Context,
3300 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003301 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003302 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003303 }
3304}
3305
Chandler Carruthedc2c642011-07-02 00:01:44 +00003306static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003307 if (S.LangOpts.CUDA) {
Michael Han99315932013-01-24 16:46:58 +00003308 D->addAttr(::new (S.Context)
3309 CUDAHostAttr(Attr.getRange(), S.Context,
3310 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003311 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003312 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003313 }
3314}
3315
Chandler Carruthedc2c642011-07-02 00:01:44 +00003316static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003317 if (S.LangOpts.CUDA) {
Michael Han99315932013-01-24 16:46:58 +00003318 D->addAttr(::new (S.Context)
3319 CUDASharedAttr(Attr.getRange(), S.Context,
3320 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003321 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003322 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003323 }
3324}
3325
Chandler Carruthedc2c642011-07-02 00:01:44 +00003326static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003327 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003328 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003329 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003330 return;
3331 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003332
Michael Han99315932013-01-24 16:46:58 +00003333 D->addAttr(::new (S.Context)
3334 GNUInlineAttr(Attr.getRange(), S.Context,
3335 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003336}
3337
Chandler Carruthedc2c642011-07-02 00:01:44 +00003338static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003339 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003340
Aaron Ballman02df2e02012-12-09 17:45:41 +00003341 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003342 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003343 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3344 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003345 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003346 return;
3347
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003348 if (!isa<ObjCMethodDecl>(D)) {
3349 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3350 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003351 return;
3352 }
3353
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003354 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003355 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003356 D->addAttr(::new (S.Context)
3357 FastCallAttr(Attr.getRange(), S.Context,
3358 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003359 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003360 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003361 D->addAttr(::new (S.Context)
3362 StdCallAttr(Attr.getRange(), S.Context,
3363 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003364 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003365 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003366 D->addAttr(::new (S.Context)
3367 ThisCallAttr(Attr.getRange(), S.Context,
3368 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003369 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003370 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003371 D->addAttr(::new (S.Context)
3372 CDeclAttr(Attr.getRange(), S.Context,
3373 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003374 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003375 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003376 D->addAttr(::new (S.Context)
3377 PascalAttr(Attr.getRange(), S.Context,
3378 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003379 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003380 case AttributeList::AT_MSABI:
3381 D->addAttr(::new (S.Context)
3382 MSABIAttr(Attr.getRange(), S.Context,
3383 Attr.getAttributeSpellingListIndex()));
3384 return;
3385 case AttributeList::AT_SysVABI:
3386 D->addAttr(::new (S.Context)
3387 SysVABIAttr(Attr.getRange(), S.Context,
3388 Attr.getAttributeSpellingListIndex()));
3389 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003390 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003391 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003392 switch (CC) {
3393 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003394 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003395 break;
3396 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003397 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003398 break;
3399 default:
3400 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003401 }
3402
Michael Han99315932013-01-24 16:46:58 +00003403 D->addAttr(::new (S.Context)
3404 PcsAttr(Attr.getRange(), S.Context, PCS,
3405 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003406 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003407 }
Derek Schuffa2020962012-10-16 22:30:41 +00003408 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003409 D->addAttr(::new (S.Context)
3410 PnaclCallAttr(Attr.getRange(), S.Context,
3411 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003412 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003413 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003414 D->addAttr(::new (S.Context)
3415 IntelOclBiccAttr(Attr.getRange(), S.Context,
3416 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003417 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003418
Abramo Bagnara50099372010-04-30 13:10:51 +00003419 default:
3420 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003421 }
3422}
3423
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003424static void handleOpenCLImageAccessAttr(Sema &S, Decl *D,
3425 const AttributeList &Attr) {
3426 uint32_t ArgNum;
3427 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), ArgNum))
Guy Benyeifb36ede2013-03-24 13:58:12 +00003428 return;
Guy Benyeifb36ede2013-03-24 13:58:12 +00003429
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003430 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(Attr.getRange(),
3431 S.Context, ArgNum));
Guy Benyeifb36ede2013-03-24 13:58:12 +00003432}
3433
Aaron Ballman02df2e02012-12-09 17:45:41 +00003434bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3435 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003436 if (attr.isInvalid())
3437 return true;
3438
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003439 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003440 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003441 attr.setInvalid();
3442 return true;
3443 }
3444
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003445 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3446 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003447 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003448 case AttributeList::AT_CDecl: CC = CC_C; break;
3449 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3450 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3451 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3452 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003453 case AttributeList::AT_MSABI:
3454 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3455 CC_X86_64Win64;
3456 break;
3457 case AttributeList::AT_SysVABI:
3458 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3459 CC_C;
3460 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003461 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003462 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003463 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003464 attr.setInvalid();
3465 return true;
3466 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003467 if (StrRef == "aapcs") {
3468 CC = CC_AAPCS;
3469 break;
3470 } else if (StrRef == "aapcs-vfp") {
3471 CC = CC_AAPCS_VFP;
3472 break;
3473 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003474
3475 attr.setInvalid();
3476 Diag(attr.getLoc(), diag::err_invalid_pcs);
3477 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003478 }
Derek Schuffa2020962012-10-16 22:30:41 +00003479 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003480 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003481 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003482 }
3483
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003484 const TargetInfo &TI = Context.getTargetInfo();
3485 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3486 if (A == TargetInfo::CCCR_Warning) {
3487 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003488
3489 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3490 if (FD)
3491 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3492 TargetInfo::CCMT_NonMember;
3493 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003494 }
3495
John McCall3882ace2011-01-05 12:14:39 +00003496 return false;
3497}
3498
Chandler Carruthedc2c642011-07-02 00:01:44 +00003499static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003500 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003501
3502 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003503 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003504 return;
3505
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003506 if (!isa<ObjCMethodDecl>(D)) {
3507 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3508 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003509 return;
3510 }
Eli Friedman7044b762009-03-27 21:06:47 +00003511
Michael Han99315932013-01-24 16:46:58 +00003512 D->addAttr(::new (S.Context)
3513 RegparmAttr(Attr.getRange(), S.Context, numParams,
3514 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00003515}
3516
3517/// Checks a regparm attribute, returning true if it is ill-formed and
3518/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003519bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3520 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003521 return true;
3522
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003523 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003524 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003525 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003526 }
Eli Friedman7044b762009-03-27 21:06:47 +00003527
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003528 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003529 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003530 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003531 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003532 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003533 }
3534
Douglas Gregore8bbc122011-09-02 00:18:52 +00003535 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003536 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003537 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003538 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003539 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003540 }
3541
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003542 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003543 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003544 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003545 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003546 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003547 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003548 }
3549
John McCall3882ace2011-01-05 12:14:39 +00003550 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003551}
3552
Chandler Carruthedc2c642011-07-02 00:01:44 +00003553static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00003554 if (S.LangOpts.CUDA) {
3555 // check the attribute arguments.
3556 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00003557 // FIXME: 0 is not okay.
3558 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003559 return;
3560 }
3561
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003562 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00003563 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003564 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003565 return;
3566 }
3567
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003568 uint32_t MaxThreads, MinBlocks;
3569 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1) ||
3570 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(1), MinBlocks, 2))
Peter Collingbourne827301e2010-12-12 23:03:07 +00003571 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003572
Michael Han99315932013-01-24 16:46:58 +00003573 D->addAttr(::new (S.Context)
3574 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003575 MaxThreads, MinBlocks,
Michael Han99315932013-01-24 16:46:58 +00003576 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003577 } else {
3578 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3579 }
3580}
3581
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003582static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3583 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003584 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003585 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003586 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003587 return;
3588 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003589
3590 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003591 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003592
Aaron Ballman00e99962013-08-31 01:11:41 +00003593 StringRef AttrName = Attr.getName()->getName();
3594 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003595
3596 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3597 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3598 << Attr.getName() << ExpectedFunctionOrMethod;
3599 return;
3600 }
3601
3602 uint64_t ArgumentIdx;
3603 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3604 Attr.getLoc(), 2,
Aaron Ballman00e99962013-08-31 01:11:41 +00003605 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003606 return;
3607
3608 uint64_t TypeTagIdx;
3609 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3610 Attr.getLoc(), 3,
Aaron Ballman00e99962013-08-31 01:11:41 +00003611 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003612 return;
3613
3614 bool IsPointer = (AttrName == "pointer_with_type_tag");
3615 if (IsPointer) {
3616 // Ensure that buffer has a pointer type.
3617 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3618 if (!BufferTy->isPointerType()) {
3619 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003620 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003621 }
3622 }
3623
Michael Han99315932013-01-24 16:46:58 +00003624 D->addAttr(::new (S.Context)
3625 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3626 ArgumentIdx, TypeTagIdx, IsPointer,
3627 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003628}
3629
3630static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3631 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003632 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003633 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003634 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003635 return;
3636 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003637
3638 if (!checkAttributeNumArgs(S, Attr, 1))
3639 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003640
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003641 if (!isa<VarDecl>(D)) {
3642 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3643 << Attr.getName() << ExpectedVariable;
3644 return;
3645 }
3646
Aaron Ballman00e99962013-08-31 01:11:41 +00003647 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003648 TypeSourceInfo *MatchingCTypeLoc = 0;
3649 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3650 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003651
Michael Han99315932013-01-24 16:46:58 +00003652 D->addAttr(::new (S.Context)
3653 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003654 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003655 Attr.getLayoutCompatible(),
3656 Attr.getMustBeNull(),
3657 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003658}
3659
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003660//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003661// Checker-specific attribute handlers.
3662//===----------------------------------------------------------------------===//
3663
John McCalled433932011-01-25 03:31:58 +00003664static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003665 return type->isDependentType() ||
3666 type->isObjCObjectPointerType() ||
3667 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003668}
3669static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003670 return type->isDependentType() ||
3671 type->isPointerType() ||
3672 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003673}
3674
Chandler Carruthedc2c642011-07-02 00:01:44 +00003675static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003676 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003677 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003678
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003679 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003680 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3681 cf = false;
3682 } else {
3683 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3684 cf = true;
3685 }
3686
3687 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003688 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003689 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003690 return;
3691 }
3692
3693 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003694 param->addAttr(::new (S.Context)
3695 CFConsumedAttr(Attr.getRange(), S.Context,
3696 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003697 else
Michael Han99315932013-01-24 16:46:58 +00003698 param->addAttr(::new (S.Context)
3699 NSConsumedAttr(Attr.getRange(), S.Context,
3700 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003701}
3702
Chandler Carruthedc2c642011-07-02 00:01:44 +00003703static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3704 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003705
John McCalled433932011-01-25 03:31:58 +00003706 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003707
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003708 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003709 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003710 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003711 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003712 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003713 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3714 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003715 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003716 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003717 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003718 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003719 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003720 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003721 return;
3722 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003723
John McCalled433932011-01-25 03:31:58 +00003724 bool typeOK;
3725 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003726 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003727 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003728 case AttributeList::AT_NSReturnsAutoreleased:
3729 case AttributeList::AT_NSReturnsRetained:
3730 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003731 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3732 cf = false;
3733 break;
3734
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003735 case AttributeList::AT_CFReturnsRetained:
3736 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003737 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3738 cf = true;
3739 break;
3740 }
3741
3742 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003743 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003744 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003745 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003746 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003747
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003748 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003749 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003750 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003751 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003752 D->addAttr(::new (S.Context)
3753 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3754 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003755 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003756 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003757 D->addAttr(::new (S.Context)
3758 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3759 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003760 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003761 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003762 D->addAttr(::new (S.Context)
3763 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3764 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003765 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003766 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003767 D->addAttr(::new (S.Context)
3768 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3769 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003770 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003771 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003772 D->addAttr(::new (S.Context)
3773 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3774 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003775 return;
3776 };
3777}
3778
John McCallcf166702011-07-22 08:53:00 +00003779static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3780 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003781 const int EP_ObjCMethod = 1;
3782 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003783
John McCallcf166702011-07-22 08:53:00 +00003784 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003785 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003786 if (isa<ObjCMethodDecl>(D))
3787 resultType = cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003788 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003789 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003790
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003791 if (!resultType->isReferenceType() &&
3792 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003793 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003794 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003795 << attr.getName()
3796 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003797 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003798
3799 // Drop the attribute.
3800 return;
3801 }
3802
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003803 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003804 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3805 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003806}
3807
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003808static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3809 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003810 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003811
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003812 DeclContext *DC = method->getDeclContext();
3813 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3814 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3815 << attr.getName() << 0;
3816 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3817 return;
3818 }
3819 if (method->getMethodFamily() == OMF_dealloc) {
3820 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3821 << attr.getName() << 1;
3822 return;
3823 }
3824
Michael Han99315932013-01-24 16:46:58 +00003825 method->addAttr(::new (S.Context)
3826 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3827 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003828}
3829
John McCall32f5fe12011-09-30 05:12:12 +00003830/// Handle cf_audited_transfer and cf_unknown_transfer.
3831static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003832 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall32f5fe12011-09-30 05:12:12 +00003833
3834 // Check whether there's a conflicting attribute already present.
3835 Attr *Existing;
3836 if (IsAudited) {
3837 Existing = D->getAttr<CFUnknownTransferAttr>();
3838 } else {
3839 Existing = D->getAttr<CFAuditedTransferAttr>();
3840 }
3841 if (Existing) {
3842 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
3843 << A.getName()
3844 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
3845 << A.getRange() << Existing->getRange();
3846 return;
3847 }
3848
3849 // All clear; add the attribute.
3850 if (IsAudited) {
Michael Han99315932013-01-24 16:46:58 +00003851 D->addAttr(::new (S.Context)
3852 CFAuditedTransferAttr(A.getRange(), S.Context,
3853 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003854 } else {
Michael Han99315932013-01-24 16:46:58 +00003855 D->addAttr(::new (S.Context)
3856 CFUnknownTransferAttr(A.getRange(), S.Context,
3857 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003858 }
3859}
3860
John McCallf1e8b342011-09-29 07:17:38 +00003861static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
3862 const AttributeList &Attr) {
3863 RecordDecl *RD = dyn_cast<RecordDecl>(D);
3864 if (!RD || RD->isUnion()) {
3865 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003866 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00003867 }
3868
Aaron Ballman00e99962013-08-31 01:11:41 +00003869 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallf1e8b342011-09-29 07:17:38 +00003870
3871 // In Objective-C, verify that the type names an Objective-C type.
3872 // We don't want to check this outside of ObjC because people sometimes
3873 // do crazy C declarations of Objective-C types.
Aaron Ballman00e99962013-08-31 01:11:41 +00003874 if (Parm && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00003875 // Check for an existing type with this name.
Aaron Ballman00e99962013-08-31 01:11:41 +00003876 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallf1e8b342011-09-29 07:17:38 +00003877 Sema::LookupOrdinaryName);
3878 if (S.LookupName(R, Sc)) {
3879 NamedDecl *Target = R.getFoundDecl();
3880 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
3881 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
3882 S.Diag(Target->getLocStart(), diag::note_declared_at);
3883 }
3884 }
3885 }
3886
Michael Han99315932013-01-24 16:46:58 +00003887 D->addAttr(::new (S.Context)
Aaron Ballman00e99962013-08-31 01:11:41 +00003888 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han99315932013-01-24 16:46:58 +00003889 Attr.getAttributeSpellingListIndex()));
John McCallf1e8b342011-09-29 07:17:38 +00003890}
3891
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003892static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3893 const AttributeList &Attr) {
Fariborz Jahaniandb3d8552013-11-19 00:09:48 +00003894 if (!isa<RecordDecl>(D)) {
Fariborz Jahanianf720f862013-11-19 17:42:25 +00003895 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3896 << Attr.getName()
3897 << (S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass
3898 : ExpectedStructOrUnion);
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003899 return;
3900 }
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003901
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003902 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003903
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003904 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003905 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003906 return;
3907 }
3908
3909 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003910 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003911 Attr.getAttributeSpellingListIndex()));
3912}
3913
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003914static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3915 const AttributeList &Attr) {
3916 if (!isa<RecordDecl>(D)) {
3917 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3918 << Attr.getName()
3919 << (S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass
3920 : ExpectedStructOrUnion);
3921 return;
3922 }
3923
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003924 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003925
3926 if (!Parm) {
3927 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3928 return;
3929 }
3930
3931 D->addAttr(::new (S.Context)
3932 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3933 Attr.getAttributeSpellingListIndex()));
3934}
3935
Chandler Carruthedc2c642011-07-02 00:01:44 +00003936static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3937 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003938 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003939
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003940 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003941 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003942}
3943
Chandler Carruthedc2c642011-07-02 00:01:44 +00003944static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3945 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003946 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003947 QualType type = vd->getType();
3948
3949 if (!type->isDependentType() &&
3950 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003951 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003952 << type;
3953 return;
3954 }
3955
3956 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3957
3958 // If we have no lifetime yet, check the lifetime we're presumably
3959 // going to infer.
3960 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3961 lifetime = type->getObjCARCImplicitLifetime();
3962
3963 switch (lifetime) {
3964 case Qualifiers::OCL_None:
3965 assert(type->isDependentType() &&
3966 "didn't infer lifetime for non-dependent type?");
3967 break;
3968
3969 case Qualifiers::OCL_Weak: // meaningful
3970 case Qualifiers::OCL_Strong: // meaningful
3971 break;
3972
3973 case Qualifiers::OCL_ExplicitNone:
3974 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003975 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003976 << (lifetime == Qualifiers::OCL_Autoreleasing);
3977 break;
3978 }
3979
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003980 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003981 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3982 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003983}
3984
Francois Picheta83957a2010-12-19 06:50:37 +00003985//===----------------------------------------------------------------------===//
3986// Microsoft specific attribute handlers.
3987//===----------------------------------------------------------------------===//
3988
Reid Kleckner140c4a72013-05-17 14:04:52 +00003989// Check if MS extensions or some other language extensions are enabled. If
3990// not, issue a diagnostic that the given attribute is unused.
3991static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
3992 bool OtherExtension = false) {
3993 if (S.LangOpts.MicrosoftExt || OtherExtension)
3994 return true;
3995 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3996 return false;
3997}
3998
Chandler Carruthedc2c642011-07-02 00:01:44 +00003999static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00004000 if (!S.LangOpts.CPlusPlus) {
4001 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4002 << Attr.getName() << AttributeLangSupport::C;
4003 return;
4004 }
4005
Reid Kleckner140c4a72013-05-17 14:04:52 +00004006 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4007 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004008
Aaron Ballman60e705e2013-11-24 20:58:02 +00004009 if (!isa<CXXRecordDecl>(D)) {
4010 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4011 << Attr.getName() << ExpectedClass;
4012 return;
4013 }
4014
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004015 StringRef StrRef;
4016 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00004017 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00004018 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004019
David Majnemer89085342013-08-09 08:56:20 +00004020 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4021 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00004022 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4023 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00004024
Reid Kleckner140c4a72013-05-17 14:04:52 +00004025 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00004026 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004027 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004028 return;
4029 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004030
David Majnemer89085342013-08-09 08:56:20 +00004031 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004032 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00004033 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004034 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00004035 return;
4036 }
David Majnemer89085342013-08-09 08:56:20 +00004037 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004038 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004039 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004040 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004041 }
Francois Picheta83957a2010-12-19 06:50:37 +00004042
David Majnemer89085342013-08-09 08:56:20 +00004043 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4044 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004045}
4046
John McCall8d32c052012-05-22 21:28:12 +00004047static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004048 if (!checkMicrosoftExt(S, Attr))
Nico Weberefa45b22012-11-07 21:31:36 +00004049 return;
Nico Weberefa45b22012-11-07 21:31:36 +00004050
4051 AttributeList::Kind Kind = Attr.getKind();
4052 if (Kind == AttributeList::AT_SingleInheritance)
4053 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004054 ::new (S.Context)
4055 SingleInheritanceAttr(Attr.getRange(), S.Context,
4056 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004057 else if (Kind == AttributeList::AT_MultipleInheritance)
4058 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004059 ::new (S.Context)
4060 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4061 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004062 else if (Kind == AttributeList::AT_VirtualInheritance)
4063 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004064 ::new (S.Context)
4065 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4066 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004067}
4068
4069static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004070 if (!checkMicrosoftExt(S, Attr))
4071 return;
4072
4073 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmancc14f3a2013-11-22 21:49:04 +00004074 if (Kind == AttributeList::AT_Win64)
4075 D->addAttr(
4076 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4077 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004078}
4079
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004080static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004081 if (!checkMicrosoftExt(S, Attr))
4082 return;
4083 D->addAttr(::new (S.Context)
4084 ForceInlineAttr(Attr.getRange(), S.Context,
4085 Attr.getAttributeSpellingListIndex()));
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004086}
4087
Reid Klecknerb144d362013-05-20 14:02:37 +00004088static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4089 if (!checkMicrosoftExt(S, Attr))
4090 return;
4091 // Check linkage after possibly merging declaratinos. See
4092 // checkAttributesAfterMerging().
4093 D->addAttr(::new (S.Context)
4094 SelectAnyAttr(Attr.getRange(), S.Context,
4095 Attr.getAttributeSpellingListIndex()));
4096}
4097
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004098/// Handles semantic checking for features that are common to all attributes,
4099/// such as checking whether a parameter was properly specified, or the correct
4100/// number of arguments were passed, etc.
4101static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4102 const AttributeList &Attr) {
4103 // Several attributes carry different semantics than the parsing requires, so
4104 // those are opted out of the common handling.
4105 //
4106 // We also bail on unknown and ignored attributes because those are handled
4107 // as part of the target-specific handling logic.
4108 if (Attr.hasCustomParsing() ||
4109 Attr.getKind() == AttributeList::UnknownAttribute ||
4110 Attr.getKind() == AttributeList::IgnoredAttribute)
4111 return false;
4112
4113 // If there are no optional arguments, then checking for the argument count
4114 // is trivial.
4115 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
4116 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4117 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004118
4119 // Check whether the attribute appertains to the given subject.
4120 if (!Attr.diagnoseAppertainsTo(S, D))
4121 return true;
4122
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004123 return false;
4124}
4125
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004126//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004127// Top Level Sema Entry Points
4128//===----------------------------------------------------------------------===//
4129
Richard Smithf8a75c32013-08-29 00:47:48 +00004130/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4131/// the attribute applies to decls. If the attribute is a type attribute, just
4132/// silently ignore it if a GNU attribute.
4133static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4134 const AttributeList &Attr,
4135 bool IncludeCXX11Attributes) {
4136 if (Attr.isInvalid())
4137 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004138
Richard Smithf8a75c32013-08-29 00:47:48 +00004139 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4140 // instead.
4141 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4142 return;
4143
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004144 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4145 return;
4146
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004147 switch (Attr.getKind()) {
Richard Smith10876ef2013-01-17 01:30:42 +00004148 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4149 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4150 case AttributeList::AT_IBOutletCollection:
4151 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004152 case AttributeList::AT_AddressSpace:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004153 case AttributeList::AT_ObjCGC:
4154 case AttributeList::AT_VectorSize:
4155 case AttributeList::AT_NeonVectorType:
4156 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004157 case AttributeList::AT_Ptr32:
4158 case AttributeList::AT_Ptr64:
4159 case AttributeList::AT_SPtr:
4160 case AttributeList::AT_UPtr:
Mike Stumpd3bb5572009-07-24 19:02:52 +00004161 // Ignore these, these are type attributes, handled by
4162 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004163 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004164 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4165 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4166 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4167 case AttributeList::AT_AlwaysInline:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004168 handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004169 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004170 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00004171 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004172 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4173 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4174 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004175 handleDependencyAttr(S, scope, D, Attr);
4176 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004177 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4178 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4179 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004180 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman3a8e2d92013-11-27 18:53:58 +00004181 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004182 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004183 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004184 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004185 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4186 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004187 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004188 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004189 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004190 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00004191 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004192 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4193 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4194 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman9a226212013-08-28 23:13:26 +00004195 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4196 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004197 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4198 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004199 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004200 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004201 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004202 case AttributeList::AT_MayAlias:
4203 handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00004204 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004205 case AttributeList::AT_NoCommon:
4206 handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004207 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004208 case AttributeList::AT_Overloadable:
4209 handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break;
Richard Smith852e9ce2013-11-27 01:46:48 +00004210 case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004211 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4212 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004213 case AttributeList::AT_Naked:
4214 handleSimpleAttribute<NakedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004215 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4216 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4217 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4218 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004219
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004220 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004221 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004222 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004223 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004224
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004225 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004226 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4227
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004228 case AttributeList::AT_ObjCRequiresSuper:
4229 handleObjCRequiresSuperAttr(S, D, Attr); break;
4230
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004231 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00004232 handleNSBridgedAttr(S, scope, D, Attr); break;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004233
4234 case AttributeList::AT_ObjCBridge:
4235 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004236
4237 case AttributeList::AT_ObjCBridgeMutable:
4238 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00004239
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004240 case AttributeList::AT_CFAuditedTransfer:
4241 case AttributeList::AT_CFUnknownTransfer:
John McCall32f5fe12011-09-30 05:12:12 +00004242 handleCFTransferAttr(S, D, Attr); break;
4243
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004244 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004245 case AttributeList::AT_CFConsumed:
4246 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4247 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004248 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004249
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004250 case AttributeList::AT_NSReturnsAutoreleased:
4251 case AttributeList::AT_NSReturnsNotRetained:
4252 case AttributeList::AT_CFReturnsNotRetained:
4253 case AttributeList::AT_NSReturnsRetained:
4254 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004255 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004256
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004257 case AttributeList::AT_WorkGroupSizeHint:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004258 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004259 handleWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00004260
Joey Goulyaba589c2013-03-08 09:42:32 +00004261 case AttributeList::AT_VecTypeHint:
4262 handleVecTypeHint(S, D, Attr); break;
4263
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004264 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004265 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004266
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004267 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4268 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4269 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004270 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004271 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004272 case AttributeList::AT_ArcWeakrefUnavailable:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004273 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004274 case AttributeList::AT_ObjCRootClass:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004275 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break;
Ted Kremenek28eace62013-11-23 01:01:34 +00004276 case AttributeList::AT_ObjCSuppressProtocol:
4277 handleObjCSuppresProtocolAttr(S, D, Attr);
4278 break;
4279 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004280 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004281 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4282 case AttributeList::AT_ReturnsTwice:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004283 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004284 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004285 case AttributeList::AT_Visibility:
4286 handleVisibilityAttr(S, D, Attr, false);
4287 break;
4288 case AttributeList::AT_TypeVisibility:
4289 handleVisibilityAttr(S, D, Attr, true);
4290 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004291 case AttributeList::AT_WarnUnused:
Aaron Ballman6a42b5a2013-11-27 16:59:17 +00004292 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004293 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004294 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004295 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4296 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4297 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4298 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004299 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004300 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004301 case AttributeList::AT_ObjCException:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004302 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004303 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004304 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004305 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004306 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4307 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4308 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4309 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004310 case AttributeList::AT_Pure:
4311 handleSimpleAttribute<PureAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004312 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4313 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004314 case AttributeList::AT_NoInline:
4315 handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004316 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004317 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004318 // Just ignore
4319 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004320 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004321 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004322 case AttributeList::AT_StdCall:
4323 case AttributeList::AT_CDecl:
4324 case AttributeList::AT_FastCall:
4325 case AttributeList::AT_ThisCall:
4326 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004327 case AttributeList::AT_MSABI:
4328 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004329 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004330 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004331 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004332 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004333 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004334 case AttributeList::AT_OpenCLKernel:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004335 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004336 case AttributeList::AT_OpenCLImageAccess:
4337 handleOpenCLImageAccessAttr(S, D, Attr);
4338 break;
John McCall8d32c052012-05-22 21:28:12 +00004339
4340 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004341 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004342 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004343 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004344 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004345 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004346 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004347 case AttributeList::AT_SingleInheritance:
4348 case AttributeList::AT_MultipleInheritance:
4349 case AttributeList::AT_VirtualInheritance:
John McCall8d32c052012-05-22 21:28:12 +00004350 handleInheritanceAttr(S, D, Attr);
4351 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004352 case AttributeList::AT_Win64:
John McCall8d32c052012-05-22 21:28:12 +00004353 handlePortabilityAttr(S, D, Attr);
4354 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004355 case AttributeList::AT_ForceInline:
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004356 handleForceInlineAttr(S, D, Attr);
4357 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004358 case AttributeList::AT_SelectAny:
4359 handleSelectAnyAttr(S, D, Attr);
4360 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004361
4362 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004363 case AttributeList::AT_AssertExclusiveLock:
4364 handleAssertExclusiveLockAttr(S, D, Attr);
4365 break;
4366 case AttributeList::AT_AssertSharedLock:
4367 handleAssertSharedLockAttr(S, D, Attr);
4368 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004369 case AttributeList::AT_GuardedVar:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004370 handleGuardedVarAttr(S, D, Attr);
4371 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004372 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004373 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004374 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004375 case AttributeList::AT_ScopedLockable:
Michael Han3be3b442012-07-23 18:48:41 +00004376 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004377 break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004378 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004379 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004380 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004381 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004382 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004383 break;
4384 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004385 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004386 break;
4387 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004388 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004389 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004390 case AttributeList::AT_Lockable:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004391 handleLockableAttr(S, D, Attr);
4392 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004393 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004394 handleGuardedByAttr(S, D, Attr);
4395 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004396 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004397 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004398 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004399 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004400 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004401 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004402 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004403 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004404 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004405 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004406 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004407 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004408 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004409 handleLockReturnedAttr(S, D, Attr);
4410 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004411 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004412 handleLocksExcludedAttr(S, D, Attr);
4413 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004414 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004415 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004416 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004417 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004418 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004419 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004420 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004421 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004422 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004423 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004424 handleUnlockFunAttr(S, D, Attr);
4425 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004426 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004427 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004428 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004429 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004430 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004431 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004432
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004433 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004434 case AttributeList::AT_Consumable:
4435 handleConsumableAttr(S, D, Attr);
4436 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004437 case AttributeList::AT_CallableWhen:
4438 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004439 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004440 case AttributeList::AT_ParamTypestate:
4441 handleParamTypestateAttr(S, D, Attr);
4442 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004443 case AttributeList::AT_ReturnTypestate:
4444 handleReturnTypestateAttr(S, D, Attr);
4445 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004446 case AttributeList::AT_SetTypestate:
4447 handleSetTypestateAttr(S, D, Attr);
4448 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004449 case AttributeList::AT_TestTypestate:
4450 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004451 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004452
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004453 // Type safety attributes.
4454 case AttributeList::AT_ArgumentWithTypeTag:
4455 handleArgumentWithTypeTagAttr(S, D, Attr);
4456 break;
4457 case AttributeList::AT_TypeTagForDatatype:
4458 handleTypeTagForDatatypeAttr(S, D, Attr);
4459 break;
4460
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004461 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004462 // Ask target about the attribute.
4463 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4464 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004465 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4466 diag::warn_unhandled_ms_attribute_ignored :
4467 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004468 break;
4469 }
4470}
4471
4472/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4473/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004474void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004475 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004476 bool IncludeCXX11Attributes) {
4477 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004478 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004479
4480 // GCC accepts
4481 // static int a9 __attribute__((weakref));
4482 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004483 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004484 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00004485 cast<NamedDecl>(D)->getNameAsString();
4486 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004487 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004488 }
4489}
4490
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004491// Annotation attributes are the only attributes allowed after an access
4492// specifier.
4493bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4494 const AttributeList *AttrList) {
4495 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004496 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004497 handleAnnotateAttr(*this, ASDecl, *l);
4498 } else {
4499 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4500 return true;
4501 }
4502 }
4503
4504 return false;
4505}
4506
John McCall42856de2011-10-01 05:17:03 +00004507/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4508/// contains any decl attributes that we should warn about.
4509static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4510 for ( ; A; A = A->getNext()) {
4511 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004512 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004513 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4514
4515 if (A->getKind() == AttributeList::UnknownAttribute) {
4516 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4517 << A->getName() << A->getRange();
4518 } else {
4519 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4520 << A->getName() << A->getRange();
4521 }
4522 }
4523}
4524
4525/// checkUnusedDeclAttributes - Given a declarator which is not being
4526/// used to build a declaration, complain about any decl attributes
4527/// which might be lying around on it.
4528void Sema::checkUnusedDeclAttributes(Declarator &D) {
4529 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4530 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4531 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4532 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4533}
4534
Ryan Flynn7d470f32009-07-30 03:15:39 +00004535/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004536/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004537NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4538 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004539 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004540 NamedDecl *NewD = 0;
4541 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004542 FunctionDecl *NewFD;
4543 // FIXME: Missing call to CheckFunctionDeclaration().
4544 // FIXME: Mangling?
4545 // FIXME: Is the qualifier info correct?
4546 // FIXME: Is the DeclContext correct?
4547 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4548 Loc, Loc, DeclarationName(II),
4549 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004550 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004551 FD->hasPrototype(),
4552 false/*isConstexprSpecified*/);
4553 NewD = NewFD;
4554
4555 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004556 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004557
4558 // Fake up parameter variables; they are declared as if this were
4559 // a typedef.
4560 QualType FDTy = FD->getType();
4561 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4562 SmallVector<ParmVarDecl*, 16> Params;
4563 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4564 AE = FT->arg_type_end(); AI != AE; ++AI) {
4565 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4566 Param->setScopeInfo(0, Params.size());
4567 Params.push_back(Param);
4568 }
David Blaikie9c70e042011-09-21 18:16:56 +00004569 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004570 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004571 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4572 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004573 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004574 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004575 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004576 if (VD->getQualifier()) {
4577 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004578 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004579 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004580 }
4581 return NewD;
4582}
4583
James Dennett634962f2012-06-14 21:40:34 +00004584/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004585/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004586void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004587 if (W.getUsed()) return; // only do this once
4588 W.setUsed(true);
4589 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4590 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004591 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004592 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4593 NDId->getName()));
4594 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00004595 WeakTopLevelDecl.push_back(NewD);
4596 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4597 // to insert Decl at TU scope, sorry.
4598 DeclContext *SavedContext = CurContext;
4599 CurContext = Context.getTranslationUnitDecl();
4600 PushOnScopeChains(NewD, S);
4601 CurContext = SavedContext;
4602 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004603 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004604 }
4605}
4606
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004607void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4608 // It's valid to "forward-declare" #pragma weak, in which case we
4609 // have to do this.
4610 LoadExternalWeakUndeclaredIdentifiers();
4611 if (!WeakUndeclaredIdentifiers.empty()) {
4612 NamedDecl *ND = NULL;
4613 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4614 if (VD->isExternC())
4615 ND = VD;
4616 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4617 if (FD->isExternC())
4618 ND = FD;
4619 if (ND) {
4620 if (IdentifierInfo *Id = ND->getIdentifier()) {
4621 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4622 = WeakUndeclaredIdentifiers.find(Id);
4623 if (I != WeakUndeclaredIdentifiers.end()) {
4624 WeakInfo W = I->second;
4625 DeclApplyPragmaWeak(S, ND, W);
4626 WeakUndeclaredIdentifiers[Id] = W;
4627 }
4628 }
4629 }
4630 }
4631}
4632
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004633/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4634/// it, apply them to D. This is a bit tricky because PD can have attributes
4635/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004636void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004637 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004638 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004639 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004640
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004641 // Walk the declarator structure, applying decl attributes that were in a type
4642 // position to the decl itself. This handles cases like:
4643 // int *__attr__(x)** D;
4644 // when X is a decl attribute.
4645 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4646 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004647 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004648
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004649 // Finally, apply any attributes on the decl itself.
4650 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004651 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004652}
John McCall28a6aea2009-11-04 02:18:39 +00004653
John McCall31168b02011-06-15 23:02:42 +00004654/// Is the given declaration allowed to use a forbidden type?
4655static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4656 // Private ivars are always okay. Unfortunately, people don't
4657 // always properly make their ivars private, even in system headers.
4658 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004659 // Function declarations in sys headers will be marked unavailable.
4660 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4661 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004662 return false;
4663
4664 // Require it to be declared in a system header.
4665 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4666}
4667
4668/// Handle a delayed forbidden-type diagnostic.
4669static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4670 Decl *decl) {
4671 if (decl && isForbiddenTypeAllowed(S, decl)) {
4672 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4673 "this system declaration uses an unsupported type"));
4674 return;
4675 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004676 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004677 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004678 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004679 // kind of forbidden type messages on unavailable functions.
4680 if (FD->hasAttr<UnavailableAttr>() &&
4681 diag.getForbiddenTypeDiagnostic() ==
4682 diag::err_arc_array_param_no_ownership) {
4683 diag.Triggered = true;
4684 return;
4685 }
4686 }
John McCall31168b02011-06-15 23:02:42 +00004687
4688 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4689 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4690 diag.Triggered = true;
4691}
4692
John McCall2ec85372012-05-07 06:16:41 +00004693void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4694 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004695 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004696 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004697
John McCall2ec85372012-05-07 06:16:41 +00004698 // When delaying diagnostics to run in the context of a parsed
4699 // declaration, we only want to actually emit anything if parsing
4700 // succeeds.
4701 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004702
John McCall2ec85372012-05-07 06:16:41 +00004703 // We emit all the active diagnostics in this pool or any of its
4704 // parents. In general, we'll get one pool for the decl spec
4705 // and a child pool for each declarator; in a decl group like:
4706 // deprecated_typedef foo, *bar, baz();
4707 // only the declarator pops will be passed decls. This is correct;
4708 // we really do need to consider delayed diagnostics from the decl spec
4709 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004710 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004711 do {
John McCall6347b682012-05-07 06:16:58 +00004712 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004713 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4714 // This const_cast is a bit lame. Really, Triggered should be mutable.
4715 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004716 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004717 continue;
4718
John McCallc1465822011-02-14 07:13:47 +00004719 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004720 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00004721 // Don't bother giving deprecation diagnostics if the decl is invalid.
4722 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00004723 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004724 break;
4725
4726 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004727 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004728 break;
John McCall31168b02011-06-15 23:02:42 +00004729
4730 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004731 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004732 break;
John McCall86121512010-01-27 03:50:35 +00004733 }
4734 }
John McCall2ec85372012-05-07 06:16:41 +00004735 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004736}
4737
John McCall6347b682012-05-07 06:16:58 +00004738/// Given a set of delayed diagnostics, re-emit them as if they had
4739/// been delayed in the current context instead of in the given pool.
4740/// Essentially, this just moves them to the current pool.
4741void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4742 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4743 assert(curPool && "re-emitting in undelayed context not supported");
4744 curPool->steal(pool);
4745}
4746
John McCall28a6aea2009-11-04 02:18:39 +00004747static bool isDeclDeprecated(Decl *D) {
4748 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004749 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004750 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004751 // A category implicitly has the availability of the interface.
4752 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4753 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004754 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4755 return false;
4756}
4757
Eli Friedman971bfa12012-08-08 21:52:41 +00004758static void
4759DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
4760 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004761 const ObjCInterfaceDecl *UnknownObjCClass,
4762 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedman971bfa12012-08-08 21:52:41 +00004763 DeclarationName Name = D->getDeclName();
4764 if (!Message.empty()) {
4765 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
4766 S.Diag(D->getLocation(),
4767 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4768 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004769 if (ObjCPropery)
4770 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4771 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00004772 } else if (!UnknownObjCClass) {
4773 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
4774 S.Diag(D->getLocation(),
4775 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4776 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004777 if (ObjCPropery)
4778 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4779 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00004780 } else {
4781 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
4782 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4783 }
4784}
4785
John McCallb45a1e72010-08-26 02:13:20 +00004786void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00004787 Decl *Ctx) {
4788 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00004789 return;
4790
John McCall86121512010-01-27 03:50:35 +00004791 DD.Triggered = true;
Eli Friedman971bfa12012-08-08 21:52:41 +00004792 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
4793 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004794 DD.getUnknownObjCClass(),
4795 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004796}
4797
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004798void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004799 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004800 const ObjCInterfaceDecl *UnknownObjCClass,
4801 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004802 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004803 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004804 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4805 UnknownObjCClass,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004806 ObjCProperty,
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004807 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004808 return;
4809 }
4810
4811 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00004812 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00004813 return;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004814 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004815}