blob: a8d336e2293cc13dadc5f7c737cf43e50a3615bb [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
Richard Smith10876ef2013-01-17 01:30:42 +00001675static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1676 const AttributeList &Attr) {
1677 // C++11 [dcl.attr.noreturn]p1:
1678 // The attribute may be applied to the declarator-id in a function
1679 // declaration.
1680 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1681 if (!FD) {
1682 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1683 << Attr.getName() << ExpectedFunctionOrMethod;
1684 return;
1685 }
1686
Michael Han99315932013-01-24 16:46:58 +00001687 D->addAttr(::new (S.Context)
1688 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1689 Attr.getAttributeSpellingListIndex()));
Richard Smith10876ef2013-01-17 01:30:42 +00001690}
1691
John Thompsoncdb847ba2010-08-09 21:53:52 +00001692// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001693static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001694/*
1695 Returning a Vector Class in Registers
1696
Eric Christopherbc638a82010-12-01 22:13:54 +00001697 According to the PPU ABI specifications, a class with a single member of
1698 vector type is returned in memory when used as the return value of a function.
1699 This results in inefficient code when implementing vector classes. To return
1700 the value in a single vector register, add the vecreturn attribute to the
1701 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001702
1703 Example:
1704
1705 struct Vector
1706 {
1707 __vector float xyzw;
1708 } __attribute__((vecreturn));
1709
1710 Vector Add(Vector lhs, Vector rhs)
1711 {
1712 Vector result;
1713 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1714 return result; // This will be returned in a register
1715 }
1716*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001717 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001718 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1719 return;
1720 }
1721
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001722 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001723 int count = 0;
1724
1725 if (!isa<CXXRecordDecl>(record)) {
1726 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1727 return;
1728 }
1729
1730 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1731 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1732 return;
1733 }
1734
Eric Christopherbc638a82010-12-01 22:13:54 +00001735 for (RecordDecl::field_iterator iter = record->field_begin();
1736 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001737 if ((count == 1) || !iter->getType()->isVectorType()) {
1738 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1739 return;
1740 }
1741 count++;
1742 }
1743
Michael Han99315932013-01-24 16:46:58 +00001744 D->addAttr(::new (S.Context)
1745 VecReturnAttr(Attr.getRange(), S.Context,
1746 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001747}
1748
Richard Smithe233fbf2013-01-28 22:42:45 +00001749static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1750 const AttributeList &Attr) {
1751 if (isa<ParmVarDecl>(D)) {
1752 // [[carries_dependency]] can only be applied to a parameter if it is a
1753 // parameter of a function declaration or lambda.
1754 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1755 S.Diag(Attr.getLoc(),
1756 diag::err_carries_dependency_param_not_function_decl);
1757 return;
1758 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001759 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001760
1761 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1762 Attr.getRange(), S.Context,
1763 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001764}
1765
Chandler Carruthedc2c642011-07-02 00:01:44 +00001766static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001767 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00001768 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001769 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001770 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +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 UnusedAttr(Attr.getRange(), S.Context,
1776 Attr.getAttributeSpellingListIndex()));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001777}
1778
Chandler Carruthedc2c642011-07-02 00:01:44 +00001779static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001780 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001781 if (VD->hasLocalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001782 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1783 return;
1784 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001785 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001786 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001787 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001788 return;
1789 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001790
Michael Han99315932013-01-24 16:46:58 +00001791 D->addAttr(::new (S.Context)
1792 UsedAttr(Attr.getRange(), S.Context,
1793 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001794}
1795
Chandler Carruthedc2c642011-07-02 00:01:44 +00001796static void handleConstructorAttr(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 ConstructorAttr(Attr.getRange(), S.Context, priority,
1810 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001811}
1812
Chandler Carruthedc2c642011-07-02 00:01:44 +00001813static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001814 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001815 if (Attr.getNumArgs() > 1) {
1816 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001817 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001818 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001819
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001820 uint32_t priority = ConstructorAttr::DefaultPriority;
1821 if (Attr.getNumArgs() > 0 &&
1822 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1823 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001824
Michael Han99315932013-01-24 16:46:58 +00001825 D->addAttr(::new (S.Context)
1826 DestructorAttr(Attr.getRange(), S.Context, priority,
1827 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001828}
1829
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001830template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001831static void handleAttrWithMessage(Sema &S, Decl *D,
1832 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001833 unsigned NumArgs = Attr.getNumArgs();
1834 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001835 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001836 return;
1837 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001838
1839 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001840 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001841 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001842 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001843
Michael Han99315932013-01-24 16:46:58 +00001844 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1845 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001846}
1847
Ted Kremenek28eace62013-11-23 01:01:34 +00001848static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
1849 const AttributeList &Attr) {
Ted Kremenek7559b472013-11-23 22:29:11 +00001850 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek28eace62013-11-23 01:01:34 +00001851
1852 if (!Parm) {
1853 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 1;
1854 return;
1855 }
1856
1857 D->addAttr(::new (S.Context)
1858 ObjCSuppressProtocolAttr(Attr.getRange(), S.Context, Parm->Ident,
1859 Attr.getAttributeSpellingListIndex()));
1860}
1861
Jordy Rose740b0c22012-05-08 03:27:22 +00001862static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1863 IdentifierInfo *Platform,
1864 VersionTuple Introduced,
1865 VersionTuple Deprecated,
1866 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001867 StringRef PlatformName
1868 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1869 if (PlatformName.empty())
1870 PlatformName = Platform->getName();
1871
1872 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1873 // of these steps are needed).
1874 if (!Introduced.empty() && !Deprecated.empty() &&
1875 !(Introduced <= Deprecated)) {
1876 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1877 << 1 << PlatformName << Deprecated.getAsString()
1878 << 0 << Introduced.getAsString();
1879 return true;
1880 }
1881
1882 if (!Introduced.empty() && !Obsoleted.empty() &&
1883 !(Introduced <= Obsoleted)) {
1884 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1885 << 2 << PlatformName << Obsoleted.getAsString()
1886 << 0 << Introduced.getAsString();
1887 return true;
1888 }
1889
1890 if (!Deprecated.empty() && !Obsoleted.empty() &&
1891 !(Deprecated <= Obsoleted)) {
1892 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1893 << 2 << PlatformName << Obsoleted.getAsString()
1894 << 1 << Deprecated.getAsString();
1895 return true;
1896 }
1897
1898 return false;
1899}
1900
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001901/// \brief Check whether the two versions match.
1902///
1903/// If either version tuple is empty, then they are assumed to match. If
1904/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1905static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1906 bool BeforeIsOkay) {
1907 if (X.empty() || Y.empty())
1908 return true;
1909
1910 if (X == Y)
1911 return true;
1912
1913 if (BeforeIsOkay && X < Y)
1914 return true;
1915
1916 return false;
1917}
1918
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001919AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001920 IdentifierInfo *Platform,
1921 VersionTuple Introduced,
1922 VersionTuple Deprecated,
1923 VersionTuple Obsoleted,
1924 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001925 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001926 bool Override,
1927 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001928 VersionTuple MergedIntroduced = Introduced;
1929 VersionTuple MergedDeprecated = Deprecated;
1930 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001931 bool FoundAny = false;
1932
Rafael Espindolac67f2232012-05-10 02:50:16 +00001933 if (D->hasAttrs()) {
1934 AttrVec &Attrs = D->getAttrs();
1935 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1936 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1937 if (!OldAA) {
1938 ++i;
1939 continue;
1940 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001941
Rafael Espindolac67f2232012-05-10 02:50:16 +00001942 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1943 if (OldPlatform != Platform) {
1944 ++i;
1945 continue;
1946 }
1947
1948 FoundAny = true;
1949 VersionTuple OldIntroduced = OldAA->getIntroduced();
1950 VersionTuple OldDeprecated = OldAA->getDeprecated();
1951 VersionTuple OldObsoleted = OldAA->getObsoleted();
1952 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001953
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001954 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1955 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1956 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1957 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001958 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001959 if (Override) {
1960 int Which = -1;
1961 VersionTuple FirstVersion;
1962 VersionTuple SecondVersion;
1963 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1964 Which = 0;
1965 FirstVersion = OldIntroduced;
1966 SecondVersion = Introduced;
1967 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1968 Which = 1;
1969 FirstVersion = Deprecated;
1970 SecondVersion = OldDeprecated;
1971 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1972 Which = 2;
1973 FirstVersion = Obsoleted;
1974 SecondVersion = OldObsoleted;
1975 }
1976
1977 if (Which == -1) {
1978 Diag(OldAA->getLocation(),
1979 diag::warn_mismatched_availability_override_unavail)
1980 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1981 } else {
1982 Diag(OldAA->getLocation(),
1983 diag::warn_mismatched_availability_override)
1984 << Which
1985 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1986 << FirstVersion.getAsString() << SecondVersion.getAsString();
1987 }
1988 Diag(Range.getBegin(), diag::note_overridden_method);
1989 } else {
1990 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1991 Diag(Range.getBegin(), diag::note_previous_attribute);
1992 }
1993
Rafael Espindolac67f2232012-05-10 02:50:16 +00001994 Attrs.erase(Attrs.begin() + i);
1995 --e;
1996 continue;
1997 }
1998
1999 VersionTuple MergedIntroduced2 = MergedIntroduced;
2000 VersionTuple MergedDeprecated2 = MergedDeprecated;
2001 VersionTuple MergedObsoleted2 = MergedObsoleted;
2002
2003 if (MergedIntroduced2.empty())
2004 MergedIntroduced2 = OldIntroduced;
2005 if (MergedDeprecated2.empty())
2006 MergedDeprecated2 = OldDeprecated;
2007 if (MergedObsoleted2.empty())
2008 MergedObsoleted2 = OldObsoleted;
2009
2010 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2011 MergedIntroduced2, MergedDeprecated2,
2012 MergedObsoleted2)) {
2013 Attrs.erase(Attrs.begin() + i);
2014 --e;
2015 continue;
2016 }
2017
2018 MergedIntroduced = MergedIntroduced2;
2019 MergedDeprecated = MergedDeprecated2;
2020 MergedObsoleted = MergedObsoleted2;
2021 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002022 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002023 }
2024
2025 if (FoundAny &&
2026 MergedIntroduced == Introduced &&
2027 MergedDeprecated == Deprecated &&
2028 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002029 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002030
Ted Kremenekb5445722013-04-06 00:34:27 +00002031 // Only create a new attribute if !Override, but we want to do
2032 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002033 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002034 MergedDeprecated, MergedObsoleted) &&
2035 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002036 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2037 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002038 Obsoleted, IsUnavailable, Message,
2039 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002040 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002041 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002042}
2043
Chandler Carruthedc2c642011-07-02 00:01:44 +00002044static void handleAvailabilityAttr(Sema &S, Decl *D,
2045 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002046 if (!checkAttributeNumArgs(S, Attr, 1))
2047 return;
2048 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00002049 unsigned Index = Attr.getAttributeSpellingListIndex();
2050
Aaron Ballman00e99962013-08-31 01:11:41 +00002051 IdentifierInfo *II = Platform->Ident;
2052 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2053 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2054 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002055
Rafael Espindolac231fab2013-01-08 21:30:32 +00002056 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2057 if (!ND) {
2058 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2059 return;
2060 }
2061
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002062 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2063 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2064 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002065 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002066 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00002067 if (const StringLiteral *SE =
2068 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002069 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002070
Aaron Ballman00e99962013-08-31 01:11:41 +00002071 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002072 Introduced.Version,
2073 Deprecated.Version,
2074 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002075 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00002076 /*Override=*/false,
2077 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002078 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002079 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002080}
2081
John McCalld041a9b2013-02-20 01:54:26 +00002082template <class T>
2083static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2084 typename T::VisibilityType value,
2085 unsigned attrSpellingListIndex) {
2086 T *existingAttr = D->getAttr<T>();
2087 if (existingAttr) {
2088 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2089 if (existingValue == value)
2090 return NULL;
2091 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2092 S.Diag(range.getBegin(), diag::note_previous_attribute);
2093 D->dropAttr<T>();
2094 }
2095 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2096}
2097
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002098VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002099 VisibilityAttr::VisibilityType Vis,
2100 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002101 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2102 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002103}
2104
John McCalld041a9b2013-02-20 01:54:26 +00002105TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2106 TypeVisibilityAttr::VisibilityType Vis,
2107 unsigned AttrSpellingListIndex) {
2108 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2109 AttrSpellingListIndex);
2110}
2111
2112static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2113 bool isTypeVisibility) {
2114 // Visibility attributes don't mean anything on a typedef.
2115 if (isa<TypedefNameDecl>(D)) {
2116 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2117 << Attr.getName();
2118 return;
2119 }
2120
2121 // 'type_visibility' can only go on a type or namespace.
2122 if (isTypeVisibility &&
2123 !(isa<TagDecl>(D) ||
2124 isa<ObjCInterfaceDecl>(D) ||
2125 isa<NamespaceDecl>(D))) {
2126 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2127 << Attr.getName() << ExpectedTypeOrNamespace;
2128 return;
2129 }
2130
Benjamin Kramer70370212013-09-09 15:08:57 +00002131 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002132 StringRef TypeStr;
2133 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002134 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002135 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002136
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002137 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002138 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002139 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002140 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002141 return;
2142 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002143
2144 // Complain about attempts to use protected visibility on targets
2145 // (like Darwin) that don't support it.
2146 if (type == VisibilityAttr::Protected &&
2147 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2148 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2149 type = VisibilityAttr::Default;
2150 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002151
Michael Han99315932013-01-24 16:46:58 +00002152 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002153 clang::Attr *newAttr;
2154 if (isTypeVisibility) {
2155 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2156 (TypeVisibilityAttr::VisibilityType) type,
2157 Index);
2158 } else {
2159 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2160 }
2161 if (newAttr)
2162 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002163}
2164
Chandler Carruthedc2c642011-07-02 00:01:44 +00002165static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2166 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002167 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002168 if (!Attr.isArgIdent(0)) {
2169 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2170 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002171 return;
2172 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002173
Aaron Ballman682ee422013-09-11 19:47:58 +00002174 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2175 ObjCMethodFamilyAttr::FamilyKind F;
2176 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2177 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2178 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002179 return;
2180 }
2181
Aaron Ballman682ee422013-09-11 19:47:58 +00002182 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCall31168b02011-06-15 23:02:42 +00002183 !method->getResultType()->isObjCObjectPointerType()) {
2184 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2185 << method->getResultType();
2186 // Ignore the attribute.
2187 return;
2188 }
2189
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002190 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman682ee422013-09-11 19:47:58 +00002191 S.Context, F));
John McCall86bc21f2011-03-02 11:33:24 +00002192}
2193
Chandler Carruthedc2c642011-07-02 00:01:44 +00002194static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002195 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002196 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002197 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002198 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2199 return;
2200 }
2201 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002202 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2203 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002204 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002205 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2206 return;
2207 }
2208 }
2209 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002210 // It is okay to include this attribute on properties, e.g.:
2211 //
2212 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2213 //
2214 // In this case it follows tradition and suppresses an error in the above
2215 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002216 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002217 }
Michael Han99315932013-01-24 16:46:58 +00002218 D->addAttr(::new (S.Context)
2219 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2220 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002221}
2222
Chandler Carruthedc2c642011-07-02 00:01:44 +00002223static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002224 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002225 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002226 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002227 return;
2228 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002229
Aaron Ballman00e99962013-08-31 01:11:41 +00002230 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002231 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002232 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2233 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2234 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002235 return;
2236 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002237
Michael Han99315932013-01-24 16:46:58 +00002238 D->addAttr(::new (S.Context)
2239 BlocksAttr(Attr.getRange(), S.Context, type,
2240 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002241}
2242
Chandler Carruthedc2c642011-07-02 00:01:44 +00002243static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002244 // check the attribute arguments.
2245 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002246 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002247 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002248 }
2249
Aaron Ballman18a78382013-11-21 00:28:23 +00002250 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002251 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002252 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002253 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002254 if (E->isTypeDependent() || E->isValueDependent() ||
2255 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002256 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002257 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002258 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002259 return;
2260 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002261
John McCallb46f2872011-09-09 07:56:05 +00002262 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002263 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2264 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002265 return;
2266 }
John McCallb46f2872011-09-09 07:56:05 +00002267
2268 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002269 }
2270
Aaron Ballman18a78382013-11-21 00:28:23 +00002271 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002272 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002273 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002274 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002275 if (E->isTypeDependent() || E->isValueDependent() ||
2276 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002277 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002278 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002279 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002280 return;
2281 }
2282 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002283
John McCallb46f2872011-09-09 07:56:05 +00002284 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002285 // FIXME: This error message could be improved, it would be nice
2286 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002287 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2288 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002289 return;
2290 }
2291 }
2292
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002293 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002294 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002295 if (isa<FunctionNoProtoType>(FT)) {
2296 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2297 return;
2298 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002299
Chris Lattner9363e312009-03-17 23:03:47 +00002300 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002301 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002302 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002303 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002304 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002305 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002306 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002307 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002308 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002309 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2310 if (!BD->isVariadic()) {
2311 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2312 return;
2313 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002314 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002315 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002316 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002317 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002318 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002319 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002320 int m = Ty->isFunctionPointerType() ? 0 : 1;
2321 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002322 return;
2323 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002324 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002325 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002326 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002327 return;
2328 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002329 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002330 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002331 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002332 return;
2333 }
Michael Han99315932013-01-24 16:46:58 +00002334 D->addAttr(::new (S.Context)
2335 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2336 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002337}
2338
Chandler Carruthedc2c642011-07-02 00:01:44 +00002339static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002340 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002341 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002342 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002343 return;
2344 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002345
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002346 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2347 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2348 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002349 return;
2350 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002351 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2352 if (MD->getResultType()->isVoidType()) {
2353 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2354 << Attr.getName() << 1;
2355 return;
2356 }
2357
Michael Han99315932013-01-24 16:46:58 +00002358 D->addAttr(::new (S.Context)
2359 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2360 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002361}
2362
Chandler Carruthedc2c642011-07-02 00:01:44 +00002363static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002364 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002365 if (isa<CXXRecordDecl>(D)) {
2366 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2367 return;
2368 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002369 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2370 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002371 return;
2372 }
2373
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002374 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002375
Michael Han99315932013-01-24 16:46:58 +00002376 nd->addAttr(::new (S.Context)
2377 WeakAttr(Attr.getRange(), S.Context,
2378 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002379}
2380
Chandler Carruthedc2c642011-07-02 00:01:44 +00002381static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002382 // weak_import only applies to variable & function declarations.
2383 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002384 if (!D->canBeWeakImported(isDef)) {
2385 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002386 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2387 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002388 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002389 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002390 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002391 // Nothing to warn about here.
2392 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002393 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002394 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002395
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002396 return;
2397 }
2398
Michael Han99315932013-01-24 16:46:58 +00002399 D->addAttr(::new (S.Context)
2400 WeakImportAttr(Attr.getRange(), S.Context,
2401 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002402}
2403
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002404// Handles reqd_work_group_size and work_group_size_hint.
2405static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002406 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002407 uint32_t WGSize[3];
2408 for (unsigned i = 0; i < 3; ++i)
2409 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002410 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002411
2412 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2413 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2414 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2415 if (!(A->getXDim() == WGSize[0] &&
2416 A->getYDim() == WGSize[1] &&
2417 A->getZDim() == WGSize[2])) {
2418 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2419 Attr.getName();
2420 }
2421 }
2422
2423 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2424 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2425 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2426 if (!(A->getXDim() == WGSize[0] &&
2427 A->getYDim() == WGSize[1] &&
2428 A->getZDim() == WGSize[2])) {
2429 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2430 Attr.getName();
2431 }
2432 }
2433
2434 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2435 D->addAttr(::new (S.Context)
2436 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002437 WGSize[0], WGSize[1], WGSize[2],
2438 Attr.getAttributeSpellingListIndex()));
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002439 else
2440 D->addAttr(::new (S.Context)
2441 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002442 WGSize[0], WGSize[1], WGSize[2],
2443 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002444}
2445
Joey Goulyaba589c2013-03-08 09:42:32 +00002446static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2447 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2448
Aaron Ballman00e99962013-08-31 01:11:41 +00002449 if (!Attr.hasParsedType()) {
2450 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2451 << Attr.getName() << 1;
2452 return;
2453 }
2454
Richard Smithb87c4652013-10-31 21:23:20 +00002455 TypeSourceInfo *ParmTSI = 0;
2456 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2457 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002458
2459 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2460 (ParmType->isBooleanType() ||
2461 !ParmType->isIntegralType(S.getASTContext()))) {
2462 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2463 << ParmType;
2464 return;
2465 }
2466
2467 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2468 D->hasAttr<VecTypeHintAttr>()) {
2469 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
Richard Smithb87c4652013-10-31 21:23:20 +00002470 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002471 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2472 return;
2473 }
2474 }
2475
2476 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Richard Smithb87c4652013-10-31 21:23:20 +00002477 ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002478}
2479
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002480SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002481 StringRef Name,
2482 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002483 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2484 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002485 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002486 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2487 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002488 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002489 }
Michael Han99315932013-01-24 16:46:58 +00002490 return ::new (Context) SectionAttr(Range, Context, Name,
2491 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002492}
2493
Chandler Carruthedc2c642011-07-02 00:01:44 +00002494static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002495 // Make sure that there is a string literal as the sections's single
2496 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002497 StringRef Str;
2498 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002499 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002500 return;
Mike Stump11289f42009-09-09 15:08:12 +00002501
Chris Lattner30ba6742009-08-10 19:03:04 +00002502 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002503 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002504 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002505 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002506 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002507 return;
2508 }
Mike Stump11289f42009-09-09 15:08:12 +00002509
Chris Lattner20aee9b2010-01-12 20:58:53 +00002510 // This attribute cannot be applied to local variables.
2511 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002512 S.Diag(LiteralLoc, diag::err_attribute_section_local_variable);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002513 return;
2514 }
Michael Han99315932013-01-24 16:46:58 +00002515
2516 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002517 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002518 if (NewAttr)
2519 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002520}
2521
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002522
Chandler Carruthedc2c642011-07-02 00:01:44 +00002523static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002524 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002525 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002526 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002527 } else {
Michael Han99315932013-01-24 16:46:58 +00002528 D->addAttr(::new (S.Context)
2529 NoThrowAttr(Attr.getRange(), S.Context,
2530 Attr.getAttributeSpellingListIndex()));
Douglas Gregor88336832011-06-15 05:45:11 +00002531 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002532}
2533
Chandler Carruthedc2c642011-07-02 00:01:44 +00002534static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002535 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002536 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002537 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002538 } else {
Michael Han99315932013-01-24 16:46:58 +00002539 D->addAttr(::new (S.Context)
2540 ConstAttr(Attr.getRange(), S.Context,
2541 Attr.getAttributeSpellingListIndex() ));
Douglas Gregor88336832011-06-15 05:45:11 +00002542 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002543}
2544
Chandler Carruthedc2c642011-07-02 00:01:44 +00002545static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002546 VarDecl *VD = cast<VarDecl>(D);
2547 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002548 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002549 return;
2550 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002551
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002552 Expr *E = Attr.getArgAsExpr(0);
2553 SourceLocation Loc = E->getExprLoc();
2554 FunctionDecl *FD = 0;
2555 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002556
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002557 // gcc only allows for simple identifiers. Since we support more than gcc, we
2558 // will warn the user.
2559 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2560 if (DRE->hasQualifier())
2561 S.Diag(Loc, diag::warn_cleanup_ext);
2562 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2563 NI = DRE->getNameInfo();
2564 if (!FD) {
2565 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2566 << NI.getName();
2567 return;
2568 }
2569 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2570 if (ULE->hasExplicitTemplateArgs())
2571 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002572 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2573 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002574 if (!FD) {
2575 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2576 << NI.getName();
2577 if (ULE->getType() == S.Context.OverloadTy)
2578 S.NoteAllOverloadCandidates(ULE);
2579 return;
2580 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002581 } else {
2582 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002583 return;
2584 }
2585
Anders Carlssond277d792009-01-31 01:16:18 +00002586 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002587 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2588 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002589 return;
2590 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002591
Anders Carlsson723f55d2009-02-07 23:16:50 +00002592 // We're currently more strict than GCC about what function types we accept.
2593 // If this ever proves to be a problem it should be easy to fix.
2594 QualType Ty = S.Context.getPointerType(VD->getType());
2595 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002596 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2597 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002598 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2599 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002600 return;
2601 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002602
Michael Han99315932013-01-24 16:46:58 +00002603 D->addAttr(::new (S.Context)
2604 CleanupAttr(Attr.getRange(), S.Context, FD,
2605 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002606}
2607
Mike Stumpd3bb5572009-07-24 19:02:52 +00002608/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002609/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002610static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002611 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002612 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002613 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002614 return;
2615 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002616
Aaron Ballman00e99962013-08-31 01:11:41 +00002617 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002618 uint64_t ArgIdx;
2619 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2620 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002621 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002622
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002623 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002624 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002625
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002626 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2627 if (not_nsstring_type &&
2628 !isCFStringType(Ty, S.Context) &&
2629 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002630 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002631 // FIXME: Should highlight the actual expression that has the wrong type.
2632 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002633 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002634 << IdxExpr->getSourceRange();
2635 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002636 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002637 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002638 if (!isNSStringType(Ty, S.Context) &&
2639 !isCFStringType(Ty, S.Context) &&
2640 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002641 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002642 // FIXME: Should highlight the actual expression that has the wrong type.
2643 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002644 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002645 << IdxExpr->getSourceRange();
2646 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002647 }
2648
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002649 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2650 // because that has corrected for the implicit this parameter, and is zero-
2651 // based. The attribute expects what the user wrote explicitly.
2652 llvm::APSInt Val;
2653 IdxExpr->EvaluateAsInt(Val, S.Context);
2654
Michael Han99315932013-01-24 16:46:58 +00002655 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002656 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002657 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002658}
2659
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002660enum FormatAttrKind {
2661 CFStringFormat,
2662 NSStringFormat,
2663 StrftimeFormat,
2664 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002665 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002666 InvalidFormat
2667};
2668
2669/// getFormatAttrKind - Map from format attribute names to supported format
2670/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002671static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002672 return llvm::StringSwitch<FormatAttrKind>(Format)
2673 // Check for formats that get handled specially.
2674 .Case("NSString", NSStringFormat)
2675 .Case("CFString", CFStringFormat)
2676 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002677
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002678 // Otherwise, check for supported formats.
2679 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2680 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2681 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002682
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002683 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2684 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002685}
2686
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002687/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002688/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002689static void handleInitPriorityAttr(Sema &S, Decl *D,
2690 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002691 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002692 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2693 return;
2694 }
2695
Aaron Ballman4a611152013-11-27 16:34:09 +00002696 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002697 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2698 Attr.setInvalid();
2699 return;
2700 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002701 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002702 if (S.Context.getAsArrayType(T))
2703 T = S.Context.getBaseElementType(T);
2704 if (!T->getAs<RecordType>()) {
2705 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2706 Attr.setInvalid();
2707 return;
2708 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002709
2710 Expr *E = Attr.getArgAsExpr(0);
2711 uint32_t prioritynum;
2712 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002713 Attr.setInvalid();
2714 return;
2715 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002716
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002717 if (prioritynum < 101 || prioritynum > 65535) {
2718 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002719 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002720 Attr.setInvalid();
2721 return;
2722 }
Michael Han99315932013-01-24 16:46:58 +00002723 D->addAttr(::new (S.Context)
2724 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2725 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002726}
2727
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002728FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2729 IdentifierInfo *Format, int FormatIdx,
2730 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002731 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002732 // Check whether we already have an equivalent format attribute.
2733 for (specific_attr_iterator<FormatAttr>
2734 i = D->specific_attr_begin<FormatAttr>(),
2735 e = D->specific_attr_end<FormatAttr>();
2736 i != e ; ++i) {
2737 FormatAttr *f = *i;
2738 if (f->getType() == Format &&
2739 f->getFormatIdx() == FormatIdx &&
2740 f->getFirstArg() == FirstArg) {
2741 // If we don't have a valid location for this attribute, adopt the
2742 // location.
2743 if (f->getLocation().isInvalid())
2744 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002745 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002746 }
2747 }
2748
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002749 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2750 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002751}
2752
Mike Stumpd3bb5572009-07-24 19:02:52 +00002753/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002754/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002755static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002756 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002757 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002758 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002759 return;
2760 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002761
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002762 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002763 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002764 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002765 return;
2766 }
2767
Chandler Carruth743682b2010-11-16 08:35:43 +00002768 // In C++ the implicit 'this' function parameter also counts, and they are
2769 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002770 bool HasImplicitThisParam = isInstanceMethod(D);
2771 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002772
Aaron Ballman00e99962013-08-31 01:11:41 +00002773 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2774 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002775
2776 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002777 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002778 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002779 // If we've modified the string name, we need a new identifier for it.
2780 II = &S.Context.Idents.get(Format);
2781 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002782
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002783 // Check for supported formats.
2784 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002785
2786 if (Kind == IgnoredFormat)
2787 return;
2788
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002789 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002790 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman00e99962013-08-31 01:11:41 +00002791 << "format" << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002792 return;
2793 }
2794
2795 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002796 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002797 uint32_t Idx;
2798 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002799 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002800
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002801 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002802 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002803 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002804 return;
2805 }
2806
2807 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002808 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002809
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002810 if (HasImplicitThisParam) {
2811 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002812 S.Diag(Attr.getLoc(),
2813 diag::err_format_attribute_implicit_this_format_string)
2814 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002815 return;
2816 }
2817 ArgIdx--;
2818 }
Mike Stump11289f42009-09-09 15:08:12 +00002819
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002820 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002821 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002822
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002823 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002824 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002825 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2826 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002827 return;
2828 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002829 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002830 // FIXME: do we need to check if the type is NSString*? What are the
2831 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002832 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002833 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002834 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2835 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002836 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002837 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002838 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002839 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002840 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002841 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2842 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002843 return;
2844 }
2845
2846 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002847 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002848 uint32_t FirstArg;
2849 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002850 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002851
2852 // check if the function is variadic if the 3rd argument non-zero
2853 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002854 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002855 ++NumArgs; // +1 for ...
2856 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002857 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002858 return;
2859 }
2860 }
2861
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002862 // strftime requires FirstArg to be 0 because it doesn't read from any
2863 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002864 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002865 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002866 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2867 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002868 return;
2869 }
2870 // if 0 it disables parameter checking (to use with e.g. va_list)
2871 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002872 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002873 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002874 return;
2875 }
2876
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002877 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002878 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002879 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002880 if (NewAttr)
2881 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002882}
2883
Chandler Carruthedc2c642011-07-02 00:01:44 +00002884static void handleTransparentUnionAttr(Sema &S, Decl *D,
2885 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002886 // Try to find the underlying union declaration.
2887 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002888 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002889 if (TD && TD->getUnderlyingType()->isUnionType())
2890 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2891 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002892 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002893
2894 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002895 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002896 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002897 return;
2898 }
2899
John McCallf937c022011-10-07 06:10:15 +00002900 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002901 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002902 diag::warn_transparent_union_attribute_not_definition);
2903 return;
2904 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002905
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002906 RecordDecl::field_iterator Field = RD->field_begin(),
2907 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002908 if (Field == FieldEnd) {
2909 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2910 return;
2911 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002912
David Blaikie40ed2972012-06-06 20:45:41 +00002913 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002914 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002915 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002916 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002917 diag::warn_transparent_union_attribute_floating)
2918 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002919 return;
2920 }
2921
2922 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2923 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2924 for (; Field != FieldEnd; ++Field) {
2925 QualType FieldType = Field->getType();
2926 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2927 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2928 // Warn if we drop the attribute.
2929 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002930 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002931 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002932 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002933 diag::warn_transparent_union_attribute_field_size_align)
2934 << isSize << Field->getDeclName() << FieldBits;
2935 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002936 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002937 diag::note_transparent_union_first_field_size_align)
2938 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002939 return;
2940 }
2941 }
2942
Michael Han99315932013-01-24 16:46:58 +00002943 RD->addAttr(::new (S.Context)
2944 TransparentUnionAttr(Attr.getRange(), S.Context,
2945 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002946}
2947
Chandler Carruthedc2c642011-07-02 00:01:44 +00002948static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002949 // Make sure that there is a string literal as the annotation's single
2950 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002951 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002952 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002953 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002954
2955 // Don't duplicate annotations that are already set.
2956 for (specific_attr_iterator<AnnotateAttr>
2957 i = D->specific_attr_begin<AnnotateAttr>(),
2958 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002959 if ((*i)->getAnnotation() == Str)
2960 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002961 }
Michael Han99315932013-01-24 16:46:58 +00002962
2963 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002964 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002965 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002966}
2967
Chandler Carruthedc2c642011-07-02 00:01:44 +00002968static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002969 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002970 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002971 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2972 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002973 return;
2974 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002975
Richard Smith848e1f12013-02-01 08:12:08 +00002976 if (Attr.getNumArgs() == 0) {
2977 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2978 true, 0, Attr.getAttributeSpellingListIndex()));
2979 return;
2980 }
2981
Aaron Ballman00e99962013-08-31 01:11:41 +00002982 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002983 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2984 S.Diag(Attr.getEllipsisLoc(),
2985 diag::err_pack_expansion_without_parameter_packs);
2986 return;
2987 }
2988
2989 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2990 return;
2991
2992 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2993 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002994}
2995
2996void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002997 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002998 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2999 SourceLocation AttrLoc = AttrRange.getBegin();
3000
Richard Smith1dba27c2013-01-29 09:02:09 +00003001 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003002 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003003 // C++11 [dcl.align]p1:
3004 // An alignment-specifier may be applied to a variable or to a class
3005 // data member, but it shall not be applied to a bit-field, a function
3006 // parameter, the formal parameter of a catch clause, or a variable
3007 // declared with the register storage class specifier. An
3008 // alignment-specifier may also be applied to the declaration of a class
3009 // or enumeration type.
3010 // C11 6.7.5/2:
3011 // An alignment attribute shall not be specified in a declaration of
3012 // a typedef, or a bit-field, or a function, or a parameter, or an
3013 // object declared with the register storage-class specifier.
3014 int DiagKind = -1;
3015 if (isa<ParmVarDecl>(D)) {
3016 DiagKind = 0;
3017 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3018 if (VD->getStorageClass() == SC_Register)
3019 DiagKind = 1;
3020 if (VD->isExceptionVariable())
3021 DiagKind = 2;
3022 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3023 if (FD->isBitField())
3024 DiagKind = 3;
3025 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00003026 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3027 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00003028 << (TmpAttr.isC11() ? ExpectedVariableOrField
3029 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003030 return;
3031 }
3032 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003033 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smithbc8caaf2013-02-22 04:55:39 +00003034 << TmpAttr.isC11() << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003035 return;
3036 }
3037 }
3038
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003039 if (E->isTypeDependent() || E->isValueDependent()) {
3040 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003041 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3042 AA->setPackExpansion(IsPackExpansion);
3043 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003044 return;
3045 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003046
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003047 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00003048 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00003049 ExprResult ICE
3050 = VerifyIntegerConstantExpression(E, &Alignment,
3051 diag::err_aligned_attribute_argument_not_int,
3052 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003053 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003054 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003055
3056 // C++11 [dcl.align]p2:
3057 // -- if the constant expression evaluates to zero, the alignment
3058 // specifier shall have no effect
3059 // C11 6.7.5p6:
3060 // An alignment specification of zero has no effect.
3061 if (!(TmpAttr.isAlignas() && !Alignment) &&
3062 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003063 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3064 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003065 return;
3066 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003067
Richard Smith848e1f12013-02-01 08:12:08 +00003068 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00003069 // We've already verified it's a power of 2, now let's make sure it's
3070 // 8192 or less.
3071 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00003072 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00003073 << E->getSourceRange();
3074 return;
3075 }
3076 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003077
Richard Smith44c247f2013-02-22 08:32:16 +00003078 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3079 ICE.take(), SpellingListIndex);
3080 AA->setPackExpansion(IsPackExpansion);
3081 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003082}
3083
Michael Hanaf02bbe2013-02-01 01:19:17 +00003084void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003085 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003086 // FIXME: Cache the number on the Attr object if non-dependent?
3087 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003088 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3089 SpellingListIndex);
3090 AA->setPackExpansion(IsPackExpansion);
3091 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003092}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003093
Richard Smith848e1f12013-02-01 08:12:08 +00003094void Sema::CheckAlignasUnderalignment(Decl *D) {
3095 assert(D->hasAttrs() && "no attributes on decl");
3096
3097 QualType Ty;
3098 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3099 Ty = VD->getType();
3100 else
3101 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00003102 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003103 return;
3104
3105 // C++11 [dcl.align]p5, C11 6.7.5/4:
3106 // The combined effect of all alignment attributes in a declaration shall
3107 // not specify an alignment that is less strict than the alignment that
3108 // would otherwise be required for the entity being declared.
3109 AlignedAttr *AlignasAttr = 0;
3110 unsigned Align = 0;
3111 for (specific_attr_iterator<AlignedAttr>
3112 I = D->specific_attr_begin<AlignedAttr>(),
3113 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3114 if (I->isAlignmentDependent())
3115 return;
3116 if (I->isAlignas())
3117 AlignasAttr = *I;
3118 Align = std::max(Align, I->getAlignment(Context));
3119 }
3120
3121 if (AlignasAttr && Align) {
3122 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3123 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3124 if (NaturalAlign > RequestedAlign)
3125 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3126 << Ty << (unsigned)NaturalAlign.getQuantity();
3127 }
3128}
3129
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003130/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003131/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003132///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003133/// Despite what would be logical, the mode attribute is a decl attribute, not a
3134/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3135/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003136static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003137 // This attribute isn't documented, but glibc uses it. It changes
3138 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00003139 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00003140 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3141 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003142 return;
3143 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003144
Aaron Ballman00e99962013-08-31 01:11:41 +00003145 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3146 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003147
3148 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003149 if (Str.startswith("__") && Str.endswith("__"))
3150 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003151
3152 unsigned DestWidth = 0;
3153 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003154 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003155 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003156 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003157 switch (Str[0]) {
3158 case 'Q': DestWidth = 8; break;
3159 case 'H': DestWidth = 16; break;
3160 case 'S': DestWidth = 32; break;
3161 case 'D': DestWidth = 64; break;
3162 case 'X': DestWidth = 96; break;
3163 case 'T': DestWidth = 128; break;
3164 }
3165 if (Str[1] == 'F') {
3166 IntegerMode = false;
3167 } else if (Str[1] == 'C') {
3168 IntegerMode = false;
3169 ComplexMode = true;
3170 } else if (Str[1] != 'I') {
3171 DestWidth = 0;
3172 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003173 break;
3174 case 4:
3175 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3176 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003177 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003178 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003179 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003180 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003181 break;
3182 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003183 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003184 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003185 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003186 case 11:
3187 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003188 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003189 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003190 }
3191
3192 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003193 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003194 OldTy = TD->getUnderlyingType();
3195 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3196 OldTy = VD->getType();
3197 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003198 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003199 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003200 return;
3201 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003202
John McCall9dd450b2009-09-21 23:43:11 +00003203 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003204 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3205 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003206 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003207 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3208 } else if (ComplexMode) {
3209 if (!OldTy->isComplexType())
3210 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3211 } else {
3212 if (!OldTy->isFloatingType())
3213 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3214 }
3215
Mike Stump87c57ac2009-05-16 07:39:55 +00003216 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3217 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003218 // FIXME: Make sure floating-point mappings are accurate
3219 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003220 if (!DestWidth) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003221 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003222 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003223 }
3224
3225 QualType NewTy;
3226
3227 if (IntegerMode)
3228 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3229 OldTy->isSignedIntegerType());
3230 else
3231 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3232
3233 if (NewTy.isNull()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003234 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003235 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003236 }
3237
Eli Friedman4735374e2009-03-03 06:41:03 +00003238 if (ComplexMode) {
3239 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003240 }
3241
3242 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003243 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3244 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3245 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003246 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003247
3248 D->addAttr(::new (S.Context)
3249 ModeAttr(Attr.getRange(), S.Context, Name,
3250 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003251}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003252
Chandler Carruthedc2c642011-07-02 00:01:44 +00003253static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003254 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3255 if (!VD->hasGlobalStorage())
3256 S.Diag(Attr.getLoc(),
3257 diag::warn_attribute_requires_functions_or_static_globals)
3258 << Attr.getName();
3259 } else if (!isFunctionOrMethod(D)) {
3260 S.Diag(Attr.getLoc(),
3261 diag::warn_attribute_requires_functions_or_static_globals)
3262 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003263 return;
3264 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003265
Michael Han99315932013-01-24 16:46:58 +00003266 D->addAttr(::new (S.Context)
3267 NoDebugAttr(Attr.getRange(), S.Context,
3268 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003269}
3270
Chandler Carruthedc2c642011-07-02 00:01:44 +00003271static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003272 if (S.LangOpts.CUDA) {
Michael Han99315932013-01-24 16:46:58 +00003273 D->addAttr(::new (S.Context)
3274 CUDAConstantAttr(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 handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003282 if (S.LangOpts.CUDA) {
3283 // check the attribute arguments.
3284 if (Attr.getNumArgs() != 0) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003285 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3286 << Attr.getName() << 0;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003287 return;
3288 }
3289
Michael Han99315932013-01-24 16:46:58 +00003290 D->addAttr(::new (S.Context)
3291 CUDADeviceAttr(Attr.getRange(), S.Context,
3292 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003293 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003294 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003295 }
3296}
3297
Chandler Carruthedc2c642011-07-02 00:01:44 +00003298static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003299 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003300 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003301 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00003302 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie6adc78e2013-02-18 22:06:02 +00003303 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003304 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3305 << FD->getType()
David Blaikie6adc78e2013-02-18 22:06:02 +00003306 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003307 "void");
3308 } else {
3309 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3310 << FD->getType();
3311 }
3312 return;
3313 }
3314
Michael Han99315932013-01-24 16:46:58 +00003315 D->addAttr(::new (S.Context)
3316 CUDAGlobalAttr(Attr.getRange(), S.Context,
3317 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003318 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003319 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003320 }
3321}
3322
Chandler Carruthedc2c642011-07-02 00:01:44 +00003323static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003324 if (S.LangOpts.CUDA) {
Michael Han99315932013-01-24 16:46:58 +00003325 D->addAttr(::new (S.Context)
3326 CUDAHostAttr(Attr.getRange(), S.Context,
3327 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003328 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003329 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003330 }
3331}
3332
Chandler Carruthedc2c642011-07-02 00:01:44 +00003333static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003334 if (S.LangOpts.CUDA) {
Michael Han99315932013-01-24 16:46:58 +00003335 D->addAttr(::new (S.Context)
3336 CUDASharedAttr(Attr.getRange(), S.Context,
3337 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003338 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003339 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003340 }
3341}
3342
Chandler Carruthedc2c642011-07-02 00:01:44 +00003343static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003344 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003345 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003346 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003347 return;
3348 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003349
Michael Han99315932013-01-24 16:46:58 +00003350 D->addAttr(::new (S.Context)
3351 GNUInlineAttr(Attr.getRange(), S.Context,
3352 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003353}
3354
Chandler Carruthedc2c642011-07-02 00:01:44 +00003355static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003356 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003357
Aaron Ballman02df2e02012-12-09 17:45:41 +00003358 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003359 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003360 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3361 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003362 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003363 return;
3364
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003365 if (!isa<ObjCMethodDecl>(D)) {
3366 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3367 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003368 return;
3369 }
3370
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003371 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003372 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003373 D->addAttr(::new (S.Context)
3374 FastCallAttr(Attr.getRange(), S.Context,
3375 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003376 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003377 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003378 D->addAttr(::new (S.Context)
3379 StdCallAttr(Attr.getRange(), S.Context,
3380 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003381 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003382 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003383 D->addAttr(::new (S.Context)
3384 ThisCallAttr(Attr.getRange(), S.Context,
3385 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003386 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003387 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003388 D->addAttr(::new (S.Context)
3389 CDeclAttr(Attr.getRange(), S.Context,
3390 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003391 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003392 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003393 D->addAttr(::new (S.Context)
3394 PascalAttr(Attr.getRange(), S.Context,
3395 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003396 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003397 case AttributeList::AT_MSABI:
3398 D->addAttr(::new (S.Context)
3399 MSABIAttr(Attr.getRange(), S.Context,
3400 Attr.getAttributeSpellingListIndex()));
3401 return;
3402 case AttributeList::AT_SysVABI:
3403 D->addAttr(::new (S.Context)
3404 SysVABIAttr(Attr.getRange(), S.Context,
3405 Attr.getAttributeSpellingListIndex()));
3406 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003407 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003408 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003409 switch (CC) {
3410 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003411 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003412 break;
3413 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003414 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003415 break;
3416 default:
3417 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003418 }
3419
Michael Han99315932013-01-24 16:46:58 +00003420 D->addAttr(::new (S.Context)
3421 PcsAttr(Attr.getRange(), S.Context, PCS,
3422 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003423 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003424 }
Derek Schuffa2020962012-10-16 22:30:41 +00003425 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003426 D->addAttr(::new (S.Context)
3427 PnaclCallAttr(Attr.getRange(), S.Context,
3428 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003429 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003430 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003431 D->addAttr(::new (S.Context)
3432 IntelOclBiccAttr(Attr.getRange(), S.Context,
3433 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003434 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003435
Abramo Bagnara50099372010-04-30 13:10:51 +00003436 default:
3437 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003438 }
3439}
3440
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003441static void handleOpenCLImageAccessAttr(Sema &S, Decl *D,
3442 const AttributeList &Attr) {
3443 uint32_t ArgNum;
3444 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), ArgNum))
Guy Benyeifb36ede2013-03-24 13:58:12 +00003445 return;
Guy Benyeifb36ede2013-03-24 13:58:12 +00003446
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003447 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(Attr.getRange(),
3448 S.Context, ArgNum));
Guy Benyeifb36ede2013-03-24 13:58:12 +00003449}
3450
Aaron Ballman02df2e02012-12-09 17:45:41 +00003451bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3452 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003453 if (attr.isInvalid())
3454 return true;
3455
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003456 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003457 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003458 attr.setInvalid();
3459 return true;
3460 }
3461
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003462 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3463 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003464 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003465 case AttributeList::AT_CDecl: CC = CC_C; break;
3466 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3467 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3468 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3469 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003470 case AttributeList::AT_MSABI:
3471 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3472 CC_X86_64Win64;
3473 break;
3474 case AttributeList::AT_SysVABI:
3475 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3476 CC_C;
3477 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003478 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003479 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003480 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003481 attr.setInvalid();
3482 return true;
3483 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003484 if (StrRef == "aapcs") {
3485 CC = CC_AAPCS;
3486 break;
3487 } else if (StrRef == "aapcs-vfp") {
3488 CC = CC_AAPCS_VFP;
3489 break;
3490 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003491
3492 attr.setInvalid();
3493 Diag(attr.getLoc(), diag::err_invalid_pcs);
3494 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003495 }
Derek Schuffa2020962012-10-16 22:30:41 +00003496 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003497 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003498 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003499 }
3500
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003501 const TargetInfo &TI = Context.getTargetInfo();
3502 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3503 if (A == TargetInfo::CCCR_Warning) {
3504 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003505
3506 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3507 if (FD)
3508 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3509 TargetInfo::CCMT_NonMember;
3510 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003511 }
3512
John McCall3882ace2011-01-05 12:14:39 +00003513 return false;
3514}
3515
Chandler Carruthedc2c642011-07-02 00:01:44 +00003516static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003517 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003518
3519 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003520 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003521 return;
3522
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003523 if (!isa<ObjCMethodDecl>(D)) {
3524 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3525 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003526 return;
3527 }
Eli Friedman7044b762009-03-27 21:06:47 +00003528
Michael Han99315932013-01-24 16:46:58 +00003529 D->addAttr(::new (S.Context)
3530 RegparmAttr(Attr.getRange(), S.Context, numParams,
3531 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00003532}
3533
3534/// Checks a regparm attribute, returning true if it is ill-formed and
3535/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003536bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3537 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003538 return true;
3539
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003540 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003541 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003542 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003543 }
Eli Friedman7044b762009-03-27 21:06:47 +00003544
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003545 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003546 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003547 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003548 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003549 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003550 }
3551
Douglas Gregore8bbc122011-09-02 00:18:52 +00003552 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003553 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003554 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003555 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003556 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003557 }
3558
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003559 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003560 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003561 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003562 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003563 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003564 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003565 }
3566
John McCall3882ace2011-01-05 12:14:39 +00003567 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003568}
3569
Chandler Carruthedc2c642011-07-02 00:01:44 +00003570static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00003571 if (S.LangOpts.CUDA) {
3572 // check the attribute arguments.
3573 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00003574 // FIXME: 0 is not okay.
3575 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003576 return;
3577 }
3578
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003579 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00003580 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003581 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003582 return;
3583 }
3584
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003585 uint32_t MaxThreads, MinBlocks;
3586 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1) ||
3587 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(1), MinBlocks, 2))
Peter Collingbourne827301e2010-12-12 23:03:07 +00003588 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003589
Michael Han99315932013-01-24 16:46:58 +00003590 D->addAttr(::new (S.Context)
3591 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003592 MaxThreads, MinBlocks,
Michael Han99315932013-01-24 16:46:58 +00003593 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003594 } else {
3595 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3596 }
3597}
3598
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003599static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3600 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003601 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003602 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003603 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003604 return;
3605 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003606
3607 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003608 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003609
Aaron Ballman00e99962013-08-31 01:11:41 +00003610 StringRef AttrName = Attr.getName()->getName();
3611 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003612
3613 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3614 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3615 << Attr.getName() << ExpectedFunctionOrMethod;
3616 return;
3617 }
3618
3619 uint64_t ArgumentIdx;
3620 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3621 Attr.getLoc(), 2,
Aaron Ballman00e99962013-08-31 01:11:41 +00003622 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003623 return;
3624
3625 uint64_t TypeTagIdx;
3626 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3627 Attr.getLoc(), 3,
Aaron Ballman00e99962013-08-31 01:11:41 +00003628 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003629 return;
3630
3631 bool IsPointer = (AttrName == "pointer_with_type_tag");
3632 if (IsPointer) {
3633 // Ensure that buffer has a pointer type.
3634 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3635 if (!BufferTy->isPointerType()) {
3636 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003637 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003638 }
3639 }
3640
Michael Han99315932013-01-24 16:46:58 +00003641 D->addAttr(::new (S.Context)
3642 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3643 ArgumentIdx, TypeTagIdx, IsPointer,
3644 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003645}
3646
3647static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3648 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003649 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003650 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003651 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003652 return;
3653 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003654
3655 if (!checkAttributeNumArgs(S, Attr, 1))
3656 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003657
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003658 if (!isa<VarDecl>(D)) {
3659 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3660 << Attr.getName() << ExpectedVariable;
3661 return;
3662 }
3663
Aaron Ballman00e99962013-08-31 01:11:41 +00003664 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003665 TypeSourceInfo *MatchingCTypeLoc = 0;
3666 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3667 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003668
Michael Han99315932013-01-24 16:46:58 +00003669 D->addAttr(::new (S.Context)
3670 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003671 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003672 Attr.getLayoutCompatible(),
3673 Attr.getMustBeNull(),
3674 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003675}
3676
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003677//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003678// Checker-specific attribute handlers.
3679//===----------------------------------------------------------------------===//
3680
John McCalled433932011-01-25 03:31:58 +00003681static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003682 return type->isDependentType() ||
3683 type->isObjCObjectPointerType() ||
3684 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003685}
3686static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003687 return type->isDependentType() ||
3688 type->isPointerType() ||
3689 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003690}
3691
Chandler Carruthedc2c642011-07-02 00:01:44 +00003692static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003693 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003694 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003695
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003696 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003697 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3698 cf = false;
3699 } else {
3700 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3701 cf = true;
3702 }
3703
3704 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003705 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003706 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003707 return;
3708 }
3709
3710 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003711 param->addAttr(::new (S.Context)
3712 CFConsumedAttr(Attr.getRange(), S.Context,
3713 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003714 else
Michael Han99315932013-01-24 16:46:58 +00003715 param->addAttr(::new (S.Context)
3716 NSConsumedAttr(Attr.getRange(), S.Context,
3717 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003718}
3719
Chandler Carruthedc2c642011-07-02 00:01:44 +00003720static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3721 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003722
John McCalled433932011-01-25 03:31:58 +00003723 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003724
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003725 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003726 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003727 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003728 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003729 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003730 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3731 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003732 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003733 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003734 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003735 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003736 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003737 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003738 return;
3739 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003740
John McCalled433932011-01-25 03:31:58 +00003741 bool typeOK;
3742 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003743 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003744 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003745 case AttributeList::AT_NSReturnsAutoreleased:
3746 case AttributeList::AT_NSReturnsRetained:
3747 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003748 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3749 cf = false;
3750 break;
3751
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003752 case AttributeList::AT_CFReturnsRetained:
3753 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003754 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3755 cf = true;
3756 break;
3757 }
3758
3759 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003760 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003761 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003762 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003763 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003764
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003765 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003766 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003767 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003768 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003769 D->addAttr(::new (S.Context)
3770 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3771 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003772 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003773 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003774 D->addAttr(::new (S.Context)
3775 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3776 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003777 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003778 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003779 D->addAttr(::new (S.Context)
3780 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3781 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003782 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003783 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003784 D->addAttr(::new (S.Context)
3785 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3786 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003787 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003788 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003789 D->addAttr(::new (S.Context)
3790 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3791 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003792 return;
3793 };
3794}
3795
John McCallcf166702011-07-22 08:53:00 +00003796static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3797 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003798 const int EP_ObjCMethod = 1;
3799 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003800
John McCallcf166702011-07-22 08:53:00 +00003801 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003802 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003803 if (isa<ObjCMethodDecl>(D))
3804 resultType = cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003805 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003806 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003807
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003808 if (!resultType->isReferenceType() &&
3809 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003810 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003811 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003812 << attr.getName()
3813 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003814 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003815
3816 // Drop the attribute.
3817 return;
3818 }
3819
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003820 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003821 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3822 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003823}
3824
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003825static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3826 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003827 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003828
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003829 DeclContext *DC = method->getDeclContext();
3830 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3831 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3832 << attr.getName() << 0;
3833 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3834 return;
3835 }
3836 if (method->getMethodFamily() == OMF_dealloc) {
3837 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3838 << attr.getName() << 1;
3839 return;
3840 }
3841
Michael Han99315932013-01-24 16:46:58 +00003842 method->addAttr(::new (S.Context)
3843 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3844 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003845}
3846
John McCall32f5fe12011-09-30 05:12:12 +00003847/// Handle cf_audited_transfer and cf_unknown_transfer.
3848static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003849 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall32f5fe12011-09-30 05:12:12 +00003850
3851 // Check whether there's a conflicting attribute already present.
3852 Attr *Existing;
3853 if (IsAudited) {
3854 Existing = D->getAttr<CFUnknownTransferAttr>();
3855 } else {
3856 Existing = D->getAttr<CFAuditedTransferAttr>();
3857 }
3858 if (Existing) {
3859 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
3860 << A.getName()
3861 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
3862 << A.getRange() << Existing->getRange();
3863 return;
3864 }
3865
3866 // All clear; add the attribute.
3867 if (IsAudited) {
Michael Han99315932013-01-24 16:46:58 +00003868 D->addAttr(::new (S.Context)
3869 CFAuditedTransferAttr(A.getRange(), S.Context,
3870 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003871 } else {
Michael Han99315932013-01-24 16:46:58 +00003872 D->addAttr(::new (S.Context)
3873 CFUnknownTransferAttr(A.getRange(), S.Context,
3874 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003875 }
3876}
3877
John McCallf1e8b342011-09-29 07:17:38 +00003878static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
3879 const AttributeList &Attr) {
3880 RecordDecl *RD = dyn_cast<RecordDecl>(D);
3881 if (!RD || RD->isUnion()) {
3882 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003883 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00003884 }
3885
Aaron Ballman00e99962013-08-31 01:11:41 +00003886 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallf1e8b342011-09-29 07:17:38 +00003887
3888 // In Objective-C, verify that the type names an Objective-C type.
3889 // We don't want to check this outside of ObjC because people sometimes
3890 // do crazy C declarations of Objective-C types.
Aaron Ballman00e99962013-08-31 01:11:41 +00003891 if (Parm && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00003892 // Check for an existing type with this name.
Aaron Ballman00e99962013-08-31 01:11:41 +00003893 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallf1e8b342011-09-29 07:17:38 +00003894 Sema::LookupOrdinaryName);
3895 if (S.LookupName(R, Sc)) {
3896 NamedDecl *Target = R.getFoundDecl();
3897 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
3898 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
3899 S.Diag(Target->getLocStart(), diag::note_declared_at);
3900 }
3901 }
3902 }
3903
Michael Han99315932013-01-24 16:46:58 +00003904 D->addAttr(::new (S.Context)
Aaron Ballman00e99962013-08-31 01:11:41 +00003905 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han99315932013-01-24 16:46:58 +00003906 Attr.getAttributeSpellingListIndex()));
John McCallf1e8b342011-09-29 07:17:38 +00003907}
3908
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003909static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3910 const AttributeList &Attr) {
Fariborz Jahaniandb3d8552013-11-19 00:09:48 +00003911 if (!isa<RecordDecl>(D)) {
Fariborz Jahanianf720f862013-11-19 17:42:25 +00003912 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3913 << Attr.getName()
3914 << (S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass
3915 : ExpectedStructOrUnion);
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003916 return;
3917 }
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003918
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003919 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003920
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003921 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003922 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003923 return;
3924 }
3925
3926 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003927 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003928 Attr.getAttributeSpellingListIndex()));
3929}
3930
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003931static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3932 const AttributeList &Attr) {
3933 if (!isa<RecordDecl>(D)) {
3934 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3935 << Attr.getName()
3936 << (S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass
3937 : ExpectedStructOrUnion);
3938 return;
3939 }
3940
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003941 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003942
3943 if (!Parm) {
3944 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3945 return;
3946 }
3947
3948 D->addAttr(::new (S.Context)
3949 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3950 Attr.getAttributeSpellingListIndex()));
3951}
3952
Chandler Carruthedc2c642011-07-02 00:01:44 +00003953static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3954 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003955 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003956
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003957 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003958 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003959}
3960
Chandler Carruthedc2c642011-07-02 00:01:44 +00003961static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3962 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003963 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003964 QualType type = vd->getType();
3965
3966 if (!type->isDependentType() &&
3967 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003968 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003969 << type;
3970 return;
3971 }
3972
3973 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3974
3975 // If we have no lifetime yet, check the lifetime we're presumably
3976 // going to infer.
3977 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3978 lifetime = type->getObjCARCImplicitLifetime();
3979
3980 switch (lifetime) {
3981 case Qualifiers::OCL_None:
3982 assert(type->isDependentType() &&
3983 "didn't infer lifetime for non-dependent type?");
3984 break;
3985
3986 case Qualifiers::OCL_Weak: // meaningful
3987 case Qualifiers::OCL_Strong: // meaningful
3988 break;
3989
3990 case Qualifiers::OCL_ExplicitNone:
3991 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003992 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003993 << (lifetime == Qualifiers::OCL_Autoreleasing);
3994 break;
3995 }
3996
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003997 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003998 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3999 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004000}
4001
Francois Picheta83957a2010-12-19 06:50:37 +00004002//===----------------------------------------------------------------------===//
4003// Microsoft specific attribute handlers.
4004//===----------------------------------------------------------------------===//
4005
Reid Kleckner140c4a72013-05-17 14:04:52 +00004006// Check if MS extensions or some other language extensions are enabled. If
4007// not, issue a diagnostic that the given attribute is unused.
4008static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4009 bool OtherExtension = false) {
4010 if (S.LangOpts.MicrosoftExt || OtherExtension)
4011 return true;
4012 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4013 return false;
4014}
4015
Chandler Carruthedc2c642011-07-02 00:01:44 +00004016static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00004017 if (!S.LangOpts.CPlusPlus) {
4018 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4019 << Attr.getName() << AttributeLangSupport::C;
4020 return;
4021 }
4022
Reid Kleckner140c4a72013-05-17 14:04:52 +00004023 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4024 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004025
Aaron Ballman60e705e2013-11-24 20:58:02 +00004026 if (!isa<CXXRecordDecl>(D)) {
4027 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4028 << Attr.getName() << ExpectedClass;
4029 return;
4030 }
4031
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004032 StringRef StrRef;
4033 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00004034 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00004035 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004036
David Majnemer89085342013-08-09 08:56:20 +00004037 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4038 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00004039 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4040 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00004041
Reid Kleckner140c4a72013-05-17 14:04:52 +00004042 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00004043 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004044 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004045 return;
4046 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004047
David Majnemer89085342013-08-09 08:56:20 +00004048 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004049 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00004050 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004051 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00004052 return;
4053 }
David Majnemer89085342013-08-09 08:56:20 +00004054 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004055 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004056 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004057 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004058 }
Francois Picheta83957a2010-12-19 06:50:37 +00004059
David Majnemer89085342013-08-09 08:56:20 +00004060 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4061 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004062}
4063
John McCall8d32c052012-05-22 21:28:12 +00004064static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004065 if (!checkMicrosoftExt(S, Attr))
Nico Weberefa45b22012-11-07 21:31:36 +00004066 return;
Nico Weberefa45b22012-11-07 21:31:36 +00004067
4068 AttributeList::Kind Kind = Attr.getKind();
4069 if (Kind == AttributeList::AT_SingleInheritance)
4070 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004071 ::new (S.Context)
4072 SingleInheritanceAttr(Attr.getRange(), S.Context,
4073 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004074 else if (Kind == AttributeList::AT_MultipleInheritance)
4075 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004076 ::new (S.Context)
4077 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4078 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004079 else if (Kind == AttributeList::AT_VirtualInheritance)
4080 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004081 ::new (S.Context)
4082 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4083 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004084}
4085
4086static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004087 if (!checkMicrosoftExt(S, Attr))
4088 return;
4089
4090 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmancc14f3a2013-11-22 21:49:04 +00004091 if (Kind == AttributeList::AT_Win64)
4092 D->addAttr(
4093 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4094 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004095}
4096
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004097static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004098 if (!checkMicrosoftExt(S, Attr))
4099 return;
4100 D->addAttr(::new (S.Context)
4101 ForceInlineAttr(Attr.getRange(), S.Context,
4102 Attr.getAttributeSpellingListIndex()));
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004103}
4104
Reid Klecknerb144d362013-05-20 14:02:37 +00004105static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4106 if (!checkMicrosoftExt(S, Attr))
4107 return;
4108 // Check linkage after possibly merging declaratinos. See
4109 // checkAttributesAfterMerging().
4110 D->addAttr(::new (S.Context)
4111 SelectAnyAttr(Attr.getRange(), S.Context,
4112 Attr.getAttributeSpellingListIndex()));
4113}
4114
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004115/// Handles semantic checking for features that are common to all attributes,
4116/// such as checking whether a parameter was properly specified, or the correct
4117/// number of arguments were passed, etc.
4118static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4119 const AttributeList &Attr) {
4120 // Several attributes carry different semantics than the parsing requires, so
4121 // those are opted out of the common handling.
4122 //
4123 // We also bail on unknown and ignored attributes because those are handled
4124 // as part of the target-specific handling logic.
4125 if (Attr.hasCustomParsing() ||
4126 Attr.getKind() == AttributeList::UnknownAttribute ||
4127 Attr.getKind() == AttributeList::IgnoredAttribute)
4128 return false;
4129
4130 // If there are no optional arguments, then checking for the argument count
4131 // is trivial.
4132 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
4133 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4134 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004135
4136 // Check whether the attribute appertains to the given subject.
4137 if (!Attr.diagnoseAppertainsTo(S, D))
4138 return true;
4139
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004140 return false;
4141}
4142
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004143//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004144// Top Level Sema Entry Points
4145//===----------------------------------------------------------------------===//
4146
Richard Smithf8a75c32013-08-29 00:47:48 +00004147/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4148/// the attribute applies to decls. If the attribute is a type attribute, just
4149/// silently ignore it if a GNU attribute.
4150static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4151 const AttributeList &Attr,
4152 bool IncludeCXX11Attributes) {
4153 if (Attr.isInvalid())
4154 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004155
Richard Smithf8a75c32013-08-29 00:47:48 +00004156 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4157 // instead.
4158 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4159 return;
4160
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004161 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4162 return;
4163
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004164 switch (Attr.getKind()) {
Richard Smith10876ef2013-01-17 01:30:42 +00004165 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4166 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4167 case AttributeList::AT_IBOutletCollection:
4168 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004169 case AttributeList::AT_AddressSpace:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004170 case AttributeList::AT_ObjCGC:
4171 case AttributeList::AT_VectorSize:
4172 case AttributeList::AT_NeonVectorType:
4173 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004174 case AttributeList::AT_Ptr32:
4175 case AttributeList::AT_Ptr64:
4176 case AttributeList::AT_SPtr:
4177 case AttributeList::AT_UPtr:
Mike Stumpd3bb5572009-07-24 19:02:52 +00004178 // Ignore these, these are type attributes, handled by
4179 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004180 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004181 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4182 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4183 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4184 case AttributeList::AT_AlwaysInline:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004185 handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004186 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004187 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00004188 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004189 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4190 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4191 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004192 handleDependencyAttr(S, scope, D, Attr);
4193 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004194 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4195 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4196 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004197 case AttributeList::AT_CXX11NoReturn:
4198 handleCXX11NoReturnAttr(S, D, Attr);
4199 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004200 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004201 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004202 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004203 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4204 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004205 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004206 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004207 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004208 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00004209 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004210 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4211 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4212 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman9a226212013-08-28 23:13:26 +00004213 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4214 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004215 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4216 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004217 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004218 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004219 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004220 case AttributeList::AT_MayAlias:
4221 handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00004222 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004223 case AttributeList::AT_NoCommon:
4224 handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004225 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004226 case AttributeList::AT_Overloadable:
4227 handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break;
Richard Smith852e9ce2013-11-27 01:46:48 +00004228 case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004229 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4230 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004231 case AttributeList::AT_Naked:
4232 handleSimpleAttribute<NakedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004233 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4234 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4235 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4236 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004237
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004238 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004239 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004240 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004241 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004242
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004243 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004244 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4245
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004246 case AttributeList::AT_ObjCRequiresSuper:
4247 handleObjCRequiresSuperAttr(S, D, Attr); break;
4248
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004249 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00004250 handleNSBridgedAttr(S, scope, D, Attr); break;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004251
4252 case AttributeList::AT_ObjCBridge:
4253 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004254
4255 case AttributeList::AT_ObjCBridgeMutable:
4256 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00004257
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004258 case AttributeList::AT_CFAuditedTransfer:
4259 case AttributeList::AT_CFUnknownTransfer:
John McCall32f5fe12011-09-30 05:12:12 +00004260 handleCFTransferAttr(S, D, Attr); break;
4261
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004262 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004263 case AttributeList::AT_CFConsumed:
4264 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4265 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004266 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004267
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004268 case AttributeList::AT_NSReturnsAutoreleased:
4269 case AttributeList::AT_NSReturnsNotRetained:
4270 case AttributeList::AT_CFReturnsNotRetained:
4271 case AttributeList::AT_NSReturnsRetained:
4272 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004273 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004274
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004275 case AttributeList::AT_WorkGroupSizeHint:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004276 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004277 handleWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00004278
Joey Goulyaba589c2013-03-08 09:42:32 +00004279 case AttributeList::AT_VecTypeHint:
4280 handleVecTypeHint(S, D, Attr); break;
4281
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004282 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004283 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004284
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004285 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4286 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4287 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004288 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004289 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004290 case AttributeList::AT_ArcWeakrefUnavailable:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004291 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004292 case AttributeList::AT_ObjCRootClass:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004293 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break;
Ted Kremenek28eace62013-11-23 01:01:34 +00004294 case AttributeList::AT_ObjCSuppressProtocol:
4295 handleObjCSuppresProtocolAttr(S, D, Attr);
4296 break;
4297 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004298 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004299 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4300 case AttributeList::AT_ReturnsTwice:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004301 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004302 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004303 case AttributeList::AT_Visibility:
4304 handleVisibilityAttr(S, D, Attr, false);
4305 break;
4306 case AttributeList::AT_TypeVisibility:
4307 handleVisibilityAttr(S, D, Attr, true);
4308 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004309 case AttributeList::AT_WarnUnused:
Aaron Ballman6a42b5a2013-11-27 16:59:17 +00004310 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004311 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004312 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004313 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4314 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4315 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4316 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004317 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004318 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004319 case AttributeList::AT_ObjCException:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004320 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004321 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004322 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004323 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004324 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4325 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4326 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4327 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004328 case AttributeList::AT_Pure:
4329 handleSimpleAttribute<PureAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004330 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4331 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004332 case AttributeList::AT_NoInline:
4333 handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004334 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004335 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004336 // Just ignore
4337 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004338 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004339 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004340 case AttributeList::AT_StdCall:
4341 case AttributeList::AT_CDecl:
4342 case AttributeList::AT_FastCall:
4343 case AttributeList::AT_ThisCall:
4344 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004345 case AttributeList::AT_MSABI:
4346 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004347 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004348 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004349 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004350 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004351 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004352 case AttributeList::AT_OpenCLKernel:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004353 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004354 case AttributeList::AT_OpenCLImageAccess:
4355 handleOpenCLImageAccessAttr(S, D, Attr);
4356 break;
John McCall8d32c052012-05-22 21:28:12 +00004357
4358 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004359 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004360 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004361 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004362 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004363 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004364 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004365 case AttributeList::AT_SingleInheritance:
4366 case AttributeList::AT_MultipleInheritance:
4367 case AttributeList::AT_VirtualInheritance:
John McCall8d32c052012-05-22 21:28:12 +00004368 handleInheritanceAttr(S, D, Attr);
4369 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004370 case AttributeList::AT_Win64:
John McCall8d32c052012-05-22 21:28:12 +00004371 handlePortabilityAttr(S, D, Attr);
4372 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004373 case AttributeList::AT_ForceInline:
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004374 handleForceInlineAttr(S, D, Attr);
4375 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004376 case AttributeList::AT_SelectAny:
4377 handleSelectAnyAttr(S, D, Attr);
4378 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004379
4380 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004381 case AttributeList::AT_AssertExclusiveLock:
4382 handleAssertExclusiveLockAttr(S, D, Attr);
4383 break;
4384 case AttributeList::AT_AssertSharedLock:
4385 handleAssertSharedLockAttr(S, D, Attr);
4386 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004387 case AttributeList::AT_GuardedVar:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004388 handleGuardedVarAttr(S, D, Attr);
4389 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004390 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004391 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004392 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004393 case AttributeList::AT_ScopedLockable:
Michael Han3be3b442012-07-23 18:48:41 +00004394 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004395 break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004396 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004397 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004398 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004399 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004400 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004401 break;
4402 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004403 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004404 break;
4405 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004406 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004407 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004408 case AttributeList::AT_Lockable:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004409 handleLockableAttr(S, D, Attr);
4410 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004411 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004412 handleGuardedByAttr(S, D, Attr);
4413 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004414 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004415 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004416 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004417 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004418 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004419 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004420 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004421 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004422 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004423 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004424 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004425 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004426 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004427 handleLockReturnedAttr(S, D, Attr);
4428 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004429 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004430 handleLocksExcludedAttr(S, D, Attr);
4431 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004432 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004433 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004434 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004435 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004436 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004437 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004438 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004439 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004440 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004441 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004442 handleUnlockFunAttr(S, D, Attr);
4443 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004444 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004445 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004446 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004447 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004448 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004449 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004450
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004451 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004452 case AttributeList::AT_Consumable:
4453 handleConsumableAttr(S, D, Attr);
4454 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004455 case AttributeList::AT_CallableWhen:
4456 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004457 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004458 case AttributeList::AT_ParamTypestate:
4459 handleParamTypestateAttr(S, D, Attr);
4460 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004461 case AttributeList::AT_ReturnTypestate:
4462 handleReturnTypestateAttr(S, D, Attr);
4463 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004464 case AttributeList::AT_SetTypestate:
4465 handleSetTypestateAttr(S, D, Attr);
4466 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004467 case AttributeList::AT_TestTypestate:
4468 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004469 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004470
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004471 // Type safety attributes.
4472 case AttributeList::AT_ArgumentWithTypeTag:
4473 handleArgumentWithTypeTagAttr(S, D, Attr);
4474 break;
4475 case AttributeList::AT_TypeTagForDatatype:
4476 handleTypeTagForDatatypeAttr(S, D, Attr);
4477 break;
4478
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004479 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004480 // Ask target about the attribute.
4481 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4482 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004483 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4484 diag::warn_unhandled_ms_attribute_ignored :
4485 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004486 break;
4487 }
4488}
4489
4490/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4491/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004492void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004493 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004494 bool IncludeCXX11Attributes) {
4495 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004496 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004497
4498 // GCC accepts
4499 // static int a9 __attribute__((weakref));
4500 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004501 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004502 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00004503 cast<NamedDecl>(D)->getNameAsString();
4504 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004505 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004506 }
4507}
4508
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004509// Annotation attributes are the only attributes allowed after an access
4510// specifier.
4511bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4512 const AttributeList *AttrList) {
4513 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004514 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004515 handleAnnotateAttr(*this, ASDecl, *l);
4516 } else {
4517 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4518 return true;
4519 }
4520 }
4521
4522 return false;
4523}
4524
John McCall42856de2011-10-01 05:17:03 +00004525/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4526/// contains any decl attributes that we should warn about.
4527static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4528 for ( ; A; A = A->getNext()) {
4529 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004530 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004531 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4532
4533 if (A->getKind() == AttributeList::UnknownAttribute) {
4534 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4535 << A->getName() << A->getRange();
4536 } else {
4537 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4538 << A->getName() << A->getRange();
4539 }
4540 }
4541}
4542
4543/// checkUnusedDeclAttributes - Given a declarator which is not being
4544/// used to build a declaration, complain about any decl attributes
4545/// which might be lying around on it.
4546void Sema::checkUnusedDeclAttributes(Declarator &D) {
4547 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4548 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4549 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4550 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4551}
4552
Ryan Flynn7d470f32009-07-30 03:15:39 +00004553/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004554/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004555NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4556 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004557 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004558 NamedDecl *NewD = 0;
4559 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004560 FunctionDecl *NewFD;
4561 // FIXME: Missing call to CheckFunctionDeclaration().
4562 // FIXME: Mangling?
4563 // FIXME: Is the qualifier info correct?
4564 // FIXME: Is the DeclContext correct?
4565 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4566 Loc, Loc, DeclarationName(II),
4567 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004568 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004569 FD->hasPrototype(),
4570 false/*isConstexprSpecified*/);
4571 NewD = NewFD;
4572
4573 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004574 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004575
4576 // Fake up parameter variables; they are declared as if this were
4577 // a typedef.
4578 QualType FDTy = FD->getType();
4579 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4580 SmallVector<ParmVarDecl*, 16> Params;
4581 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4582 AE = FT->arg_type_end(); AI != AE; ++AI) {
4583 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4584 Param->setScopeInfo(0, Params.size());
4585 Params.push_back(Param);
4586 }
David Blaikie9c70e042011-09-21 18:16:56 +00004587 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004588 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004589 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4590 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004591 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004592 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004593 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004594 if (VD->getQualifier()) {
4595 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004596 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004597 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004598 }
4599 return NewD;
4600}
4601
James Dennett634962f2012-06-14 21:40:34 +00004602/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004603/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004604void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004605 if (W.getUsed()) return; // only do this once
4606 W.setUsed(true);
4607 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4608 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004609 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004610 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4611 NDId->getName()));
4612 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00004613 WeakTopLevelDecl.push_back(NewD);
4614 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4615 // to insert Decl at TU scope, sorry.
4616 DeclContext *SavedContext = CurContext;
4617 CurContext = Context.getTranslationUnitDecl();
4618 PushOnScopeChains(NewD, S);
4619 CurContext = SavedContext;
4620 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004621 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004622 }
4623}
4624
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004625void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4626 // It's valid to "forward-declare" #pragma weak, in which case we
4627 // have to do this.
4628 LoadExternalWeakUndeclaredIdentifiers();
4629 if (!WeakUndeclaredIdentifiers.empty()) {
4630 NamedDecl *ND = NULL;
4631 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4632 if (VD->isExternC())
4633 ND = VD;
4634 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4635 if (FD->isExternC())
4636 ND = FD;
4637 if (ND) {
4638 if (IdentifierInfo *Id = ND->getIdentifier()) {
4639 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4640 = WeakUndeclaredIdentifiers.find(Id);
4641 if (I != WeakUndeclaredIdentifiers.end()) {
4642 WeakInfo W = I->second;
4643 DeclApplyPragmaWeak(S, ND, W);
4644 WeakUndeclaredIdentifiers[Id] = W;
4645 }
4646 }
4647 }
4648 }
4649}
4650
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004651/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4652/// it, apply them to D. This is a bit tricky because PD can have attributes
4653/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004654void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004655 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004656 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004657 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004658
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004659 // Walk the declarator structure, applying decl attributes that were in a type
4660 // position to the decl itself. This handles cases like:
4661 // int *__attr__(x)** D;
4662 // when X is a decl attribute.
4663 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4664 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004665 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004666
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004667 // Finally, apply any attributes on the decl itself.
4668 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004669 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004670}
John McCall28a6aea2009-11-04 02:18:39 +00004671
John McCall31168b02011-06-15 23:02:42 +00004672/// Is the given declaration allowed to use a forbidden type?
4673static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4674 // Private ivars are always okay. Unfortunately, people don't
4675 // always properly make their ivars private, even in system headers.
4676 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004677 // Function declarations in sys headers will be marked unavailable.
4678 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4679 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004680 return false;
4681
4682 // Require it to be declared in a system header.
4683 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4684}
4685
4686/// Handle a delayed forbidden-type diagnostic.
4687static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4688 Decl *decl) {
4689 if (decl && isForbiddenTypeAllowed(S, decl)) {
4690 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4691 "this system declaration uses an unsupported type"));
4692 return;
4693 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004694 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004695 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004696 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004697 // kind of forbidden type messages on unavailable functions.
4698 if (FD->hasAttr<UnavailableAttr>() &&
4699 diag.getForbiddenTypeDiagnostic() ==
4700 diag::err_arc_array_param_no_ownership) {
4701 diag.Triggered = true;
4702 return;
4703 }
4704 }
John McCall31168b02011-06-15 23:02:42 +00004705
4706 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4707 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4708 diag.Triggered = true;
4709}
4710
John McCall2ec85372012-05-07 06:16:41 +00004711void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4712 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004713 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004714 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004715
John McCall2ec85372012-05-07 06:16:41 +00004716 // When delaying diagnostics to run in the context of a parsed
4717 // declaration, we only want to actually emit anything if parsing
4718 // succeeds.
4719 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004720
John McCall2ec85372012-05-07 06:16:41 +00004721 // We emit all the active diagnostics in this pool or any of its
4722 // parents. In general, we'll get one pool for the decl spec
4723 // and a child pool for each declarator; in a decl group like:
4724 // deprecated_typedef foo, *bar, baz();
4725 // only the declarator pops will be passed decls. This is correct;
4726 // we really do need to consider delayed diagnostics from the decl spec
4727 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004728 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004729 do {
John McCall6347b682012-05-07 06:16:58 +00004730 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004731 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4732 // This const_cast is a bit lame. Really, Triggered should be mutable.
4733 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004734 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004735 continue;
4736
John McCallc1465822011-02-14 07:13:47 +00004737 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004738 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00004739 // Don't bother giving deprecation diagnostics if the decl is invalid.
4740 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00004741 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004742 break;
4743
4744 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004745 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004746 break;
John McCall31168b02011-06-15 23:02:42 +00004747
4748 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004749 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004750 break;
John McCall86121512010-01-27 03:50:35 +00004751 }
4752 }
John McCall2ec85372012-05-07 06:16:41 +00004753 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004754}
4755
John McCall6347b682012-05-07 06:16:58 +00004756/// Given a set of delayed diagnostics, re-emit them as if they had
4757/// been delayed in the current context instead of in the given pool.
4758/// Essentially, this just moves them to the current pool.
4759void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4760 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4761 assert(curPool && "re-emitting in undelayed context not supported");
4762 curPool->steal(pool);
4763}
4764
John McCall28a6aea2009-11-04 02:18:39 +00004765static bool isDeclDeprecated(Decl *D) {
4766 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004767 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004768 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004769 // A category implicitly has the availability of the interface.
4770 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4771 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004772 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4773 return false;
4774}
4775
Eli Friedman971bfa12012-08-08 21:52:41 +00004776static void
4777DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
4778 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004779 const ObjCInterfaceDecl *UnknownObjCClass,
4780 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedman971bfa12012-08-08 21:52:41 +00004781 DeclarationName Name = D->getDeclName();
4782 if (!Message.empty()) {
4783 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
4784 S.Diag(D->getLocation(),
4785 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4786 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004787 if (ObjCPropery)
4788 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4789 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00004790 } else if (!UnknownObjCClass) {
4791 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
4792 S.Diag(D->getLocation(),
4793 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4794 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004795 if (ObjCPropery)
4796 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4797 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00004798 } else {
4799 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
4800 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4801 }
4802}
4803
John McCallb45a1e72010-08-26 02:13:20 +00004804void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00004805 Decl *Ctx) {
4806 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00004807 return;
4808
John McCall86121512010-01-27 03:50:35 +00004809 DD.Triggered = true;
Eli Friedman971bfa12012-08-08 21:52:41 +00004810 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
4811 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004812 DD.getUnknownObjCClass(),
4813 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004814}
4815
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004816void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004817 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004818 const ObjCInterfaceDecl *UnknownObjCClass,
4819 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004820 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004821 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004822 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4823 UnknownObjCClass,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004824 ObjCProperty,
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004825 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004826 return;
4827 }
4828
4829 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00004830 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00004831 return;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004832 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004833}