blob: 046054df51783fd9e386bcb7b26f44aa9f1c3bd6 [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
Lubos Lunakedc13882013-07-20 15:05:36 +00002339static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Lubos Lunakedc13882013-07-20 15:05:36 +00002340 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2341 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2342 else
2343 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2344}
2345
Chandler Carruthedc2c642011-07-02 00:01:44 +00002346static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002347 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002348 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002349 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002350 return;
2351 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002352
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002353 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2354 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2355 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002356 return;
2357 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002358 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2359 if (MD->getResultType()->isVoidType()) {
2360 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2361 << Attr.getName() << 1;
2362 return;
2363 }
2364
Michael Han99315932013-01-24 16:46:58 +00002365 D->addAttr(::new (S.Context)
2366 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2367 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002368}
2369
Chandler Carruthedc2c642011-07-02 00:01:44 +00002370static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002371 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002372 if (isa<CXXRecordDecl>(D)) {
2373 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2374 return;
2375 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002376 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2377 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002378 return;
2379 }
2380
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002381 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002382
Michael Han99315932013-01-24 16:46:58 +00002383 nd->addAttr(::new (S.Context)
2384 WeakAttr(Attr.getRange(), S.Context,
2385 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002386}
2387
Chandler Carruthedc2c642011-07-02 00:01:44 +00002388static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002389 // weak_import only applies to variable & function declarations.
2390 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002391 if (!D->canBeWeakImported(isDef)) {
2392 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002393 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2394 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002395 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002396 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002397 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002398 // Nothing to warn about here.
2399 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002400 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002401 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002402
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002403 return;
2404 }
2405
Michael Han99315932013-01-24 16:46:58 +00002406 D->addAttr(::new (S.Context)
2407 WeakImportAttr(Attr.getRange(), S.Context,
2408 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002409}
2410
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002411// Handles reqd_work_group_size and work_group_size_hint.
2412static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002413 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002414 uint32_t WGSize[3];
2415 for (unsigned i = 0; i < 3; ++i)
2416 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002417 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002418
2419 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2420 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2421 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2422 if (!(A->getXDim() == WGSize[0] &&
2423 A->getYDim() == WGSize[1] &&
2424 A->getZDim() == WGSize[2])) {
2425 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2426 Attr.getName();
2427 }
2428 }
2429
2430 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2431 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2432 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2433 if (!(A->getXDim() == WGSize[0] &&
2434 A->getYDim() == WGSize[1] &&
2435 A->getZDim() == WGSize[2])) {
2436 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2437 Attr.getName();
2438 }
2439 }
2440
2441 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2442 D->addAttr(::new (S.Context)
2443 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002444 WGSize[0], WGSize[1], WGSize[2],
2445 Attr.getAttributeSpellingListIndex()));
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002446 else
2447 D->addAttr(::new (S.Context)
2448 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002449 WGSize[0], WGSize[1], WGSize[2],
2450 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002451}
2452
Joey Goulyaba589c2013-03-08 09:42:32 +00002453static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2454 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2455
Aaron Ballman00e99962013-08-31 01:11:41 +00002456 if (!Attr.hasParsedType()) {
2457 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2458 << Attr.getName() << 1;
2459 return;
2460 }
2461
Richard Smithb87c4652013-10-31 21:23:20 +00002462 TypeSourceInfo *ParmTSI = 0;
2463 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2464 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002465
2466 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2467 (ParmType->isBooleanType() ||
2468 !ParmType->isIntegralType(S.getASTContext()))) {
2469 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2470 << ParmType;
2471 return;
2472 }
2473
2474 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2475 D->hasAttr<VecTypeHintAttr>()) {
2476 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
Richard Smithb87c4652013-10-31 21:23:20 +00002477 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002478 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2479 return;
2480 }
2481 }
2482
2483 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Richard Smithb87c4652013-10-31 21:23:20 +00002484 ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002485}
2486
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002487SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002488 StringRef Name,
2489 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002490 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2491 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002492 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002493 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2494 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002495 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002496 }
Michael Han99315932013-01-24 16:46:58 +00002497 return ::new (Context) SectionAttr(Range, Context, Name,
2498 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002499}
2500
Chandler Carruthedc2c642011-07-02 00:01:44 +00002501static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002502 // Make sure that there is a string literal as the sections's single
2503 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002504 StringRef Str;
2505 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002506 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002507 return;
Mike Stump11289f42009-09-09 15:08:12 +00002508
Chris Lattner30ba6742009-08-10 19:03:04 +00002509 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002510 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002511 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002512 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002513 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002514 return;
2515 }
Mike Stump11289f42009-09-09 15:08:12 +00002516
Chris Lattner20aee9b2010-01-12 20:58:53 +00002517 // This attribute cannot be applied to local variables.
2518 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002519 S.Diag(LiteralLoc, diag::err_attribute_section_local_variable);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002520 return;
2521 }
Michael Han99315932013-01-24 16:46:58 +00002522
2523 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002524 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002525 if (NewAttr)
2526 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002527}
2528
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002529
Chandler Carruthedc2c642011-07-02 00:01:44 +00002530static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002531 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002532 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002533 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002534 } else {
Michael Han99315932013-01-24 16:46:58 +00002535 D->addAttr(::new (S.Context)
2536 NoThrowAttr(Attr.getRange(), S.Context,
2537 Attr.getAttributeSpellingListIndex()));
Douglas Gregor88336832011-06-15 05:45:11 +00002538 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002539}
2540
Chandler Carruthedc2c642011-07-02 00:01:44 +00002541static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002542 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002543 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002544 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002545 } else {
Michael Han99315932013-01-24 16:46:58 +00002546 D->addAttr(::new (S.Context)
2547 ConstAttr(Attr.getRange(), S.Context,
2548 Attr.getAttributeSpellingListIndex() ));
Douglas Gregor88336832011-06-15 05:45:11 +00002549 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002550}
2551
Chandler Carruthedc2c642011-07-02 00:01:44 +00002552static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002553 VarDecl *VD = cast<VarDecl>(D);
2554 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002555 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002556 return;
2557 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002558
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002559 Expr *E = Attr.getArgAsExpr(0);
2560 SourceLocation Loc = E->getExprLoc();
2561 FunctionDecl *FD = 0;
2562 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002563
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002564 // gcc only allows for simple identifiers. Since we support more than gcc, we
2565 // will warn the user.
2566 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2567 if (DRE->hasQualifier())
2568 S.Diag(Loc, diag::warn_cleanup_ext);
2569 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2570 NI = DRE->getNameInfo();
2571 if (!FD) {
2572 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2573 << NI.getName();
2574 return;
2575 }
2576 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2577 if (ULE->hasExplicitTemplateArgs())
2578 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002579 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2580 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002581 if (!FD) {
2582 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2583 << NI.getName();
2584 if (ULE->getType() == S.Context.OverloadTy)
2585 S.NoteAllOverloadCandidates(ULE);
2586 return;
2587 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002588 } else {
2589 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002590 return;
2591 }
2592
Anders Carlssond277d792009-01-31 01:16:18 +00002593 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002594 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2595 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002596 return;
2597 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002598
Anders Carlsson723f55d2009-02-07 23:16:50 +00002599 // We're currently more strict than GCC about what function types we accept.
2600 // If this ever proves to be a problem it should be easy to fix.
2601 QualType Ty = S.Context.getPointerType(VD->getType());
2602 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002603 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2604 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002605 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2606 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002607 return;
2608 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002609
Michael Han99315932013-01-24 16:46:58 +00002610 D->addAttr(::new (S.Context)
2611 CleanupAttr(Attr.getRange(), S.Context, FD,
2612 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002613}
2614
Mike Stumpd3bb5572009-07-24 19:02:52 +00002615/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002616/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002617static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002618 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002619 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002620 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002621 return;
2622 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002623
Aaron Ballman00e99962013-08-31 01:11:41 +00002624 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002625 uint64_t ArgIdx;
2626 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2627 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002628 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002629
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002630 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002631 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002632
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002633 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2634 if (not_nsstring_type &&
2635 !isCFStringType(Ty, S.Context) &&
2636 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002637 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002638 // FIXME: Should highlight the actual expression that has the wrong type.
2639 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002640 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002641 << IdxExpr->getSourceRange();
2642 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002643 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002644 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002645 if (!isNSStringType(Ty, S.Context) &&
2646 !isCFStringType(Ty, S.Context) &&
2647 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002648 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002649 // FIXME: Should highlight the actual expression that has the wrong type.
2650 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002651 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002652 << IdxExpr->getSourceRange();
2653 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002654 }
2655
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002656 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2657 // because that has corrected for the implicit this parameter, and is zero-
2658 // based. The attribute expects what the user wrote explicitly.
2659 llvm::APSInt Val;
2660 IdxExpr->EvaluateAsInt(Val, S.Context);
2661
Michael Han99315932013-01-24 16:46:58 +00002662 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002663 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002664 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002665}
2666
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002667enum FormatAttrKind {
2668 CFStringFormat,
2669 NSStringFormat,
2670 StrftimeFormat,
2671 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002672 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002673 InvalidFormat
2674};
2675
2676/// getFormatAttrKind - Map from format attribute names to supported format
2677/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002678static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002679 return llvm::StringSwitch<FormatAttrKind>(Format)
2680 // Check for formats that get handled specially.
2681 .Case("NSString", NSStringFormat)
2682 .Case("CFString", CFStringFormat)
2683 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002684
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002685 // Otherwise, check for supported formats.
2686 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2687 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2688 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002689
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002690 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2691 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002692}
2693
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002694/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002695/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002696static void handleInitPriorityAttr(Sema &S, Decl *D,
2697 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002698 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002699 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2700 return;
2701 }
2702
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002703 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002704 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2705 Attr.setInvalid();
2706 return;
2707 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002708 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002709 if (S.Context.getAsArrayType(T))
2710 T = S.Context.getBaseElementType(T);
2711 if (!T->getAs<RecordType>()) {
2712 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2713 Attr.setInvalid();
2714 return;
2715 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002716
2717 Expr *E = Attr.getArgAsExpr(0);
2718 uint32_t prioritynum;
2719 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002720 Attr.setInvalid();
2721 return;
2722 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002723
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002724 if (prioritynum < 101 || prioritynum > 65535) {
2725 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002726 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002727 Attr.setInvalid();
2728 return;
2729 }
Michael Han99315932013-01-24 16:46:58 +00002730 D->addAttr(::new (S.Context)
2731 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2732 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002733}
2734
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002735FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2736 IdentifierInfo *Format, int FormatIdx,
2737 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002738 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002739 // Check whether we already have an equivalent format attribute.
2740 for (specific_attr_iterator<FormatAttr>
2741 i = D->specific_attr_begin<FormatAttr>(),
2742 e = D->specific_attr_end<FormatAttr>();
2743 i != e ; ++i) {
2744 FormatAttr *f = *i;
2745 if (f->getType() == Format &&
2746 f->getFormatIdx() == FormatIdx &&
2747 f->getFirstArg() == FirstArg) {
2748 // If we don't have a valid location for this attribute, adopt the
2749 // location.
2750 if (f->getLocation().isInvalid())
2751 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002752 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002753 }
2754 }
2755
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002756 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2757 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002758}
2759
Mike Stumpd3bb5572009-07-24 19:02:52 +00002760/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002761/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002762static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002763 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002764 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002765 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002766 return;
2767 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002768
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002769 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002770 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002771 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002772 return;
2773 }
2774
Chandler Carruth743682b2010-11-16 08:35:43 +00002775 // In C++ the implicit 'this' function parameter also counts, and they are
2776 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002777 bool HasImplicitThisParam = isInstanceMethod(D);
2778 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002779
Aaron Ballman00e99962013-08-31 01:11:41 +00002780 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2781 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002782
2783 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002784 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002785 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002786 // If we've modified the string name, we need a new identifier for it.
2787 II = &S.Context.Idents.get(Format);
2788 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002789
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002790 // Check for supported formats.
2791 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002792
2793 if (Kind == IgnoredFormat)
2794 return;
2795
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002796 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002797 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman00e99962013-08-31 01:11:41 +00002798 << "format" << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002799 return;
2800 }
2801
2802 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002803 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002804 uint32_t Idx;
2805 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002806 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002807
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002808 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002809 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002810 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002811 return;
2812 }
2813
2814 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002815 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002816
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002817 if (HasImplicitThisParam) {
2818 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002819 S.Diag(Attr.getLoc(),
2820 diag::err_format_attribute_implicit_this_format_string)
2821 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002822 return;
2823 }
2824 ArgIdx--;
2825 }
Mike Stump11289f42009-09-09 15:08:12 +00002826
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002827 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002828 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002829
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002830 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002831 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002832 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2833 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002834 return;
2835 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002836 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002837 // FIXME: do we need to check if the type is NSString*? What are the
2838 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002839 if (!isNSStringType(Ty, S.Context)) {
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 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002843 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002844 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002845 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002846 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002847 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002848 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2849 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002850 return;
2851 }
2852
2853 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002854 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002855 uint32_t FirstArg;
2856 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002857 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002858
2859 // check if the function is variadic if the 3rd argument non-zero
2860 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002861 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002862 ++NumArgs; // +1 for ...
2863 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002864 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002865 return;
2866 }
2867 }
2868
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002869 // strftime requires FirstArg to be 0 because it doesn't read from any
2870 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002871 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002872 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002873 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2874 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002875 return;
2876 }
2877 // if 0 it disables parameter checking (to use with e.g. va_list)
2878 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002879 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002880 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002881 return;
2882 }
2883
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002884 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002885 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002886 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002887 if (NewAttr)
2888 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002889}
2890
Chandler Carruthedc2c642011-07-02 00:01:44 +00002891static void handleTransparentUnionAttr(Sema &S, Decl *D,
2892 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002893 // Try to find the underlying union declaration.
2894 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002895 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002896 if (TD && TD->getUnderlyingType()->isUnionType())
2897 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2898 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002899 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002900
2901 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002902 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002903 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002904 return;
2905 }
2906
John McCallf937c022011-10-07 06:10:15 +00002907 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002908 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002909 diag::warn_transparent_union_attribute_not_definition);
2910 return;
2911 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002912
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002913 RecordDecl::field_iterator Field = RD->field_begin(),
2914 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002915 if (Field == FieldEnd) {
2916 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2917 return;
2918 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002919
David Blaikie40ed2972012-06-06 20:45:41 +00002920 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002921 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002922 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002923 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002924 diag::warn_transparent_union_attribute_floating)
2925 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002926 return;
2927 }
2928
2929 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2930 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2931 for (; Field != FieldEnd; ++Field) {
2932 QualType FieldType = Field->getType();
2933 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2934 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2935 // Warn if we drop the attribute.
2936 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002937 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002938 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002939 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002940 diag::warn_transparent_union_attribute_field_size_align)
2941 << isSize << Field->getDeclName() << FieldBits;
2942 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002943 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002944 diag::note_transparent_union_first_field_size_align)
2945 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002946 return;
2947 }
2948 }
2949
Michael Han99315932013-01-24 16:46:58 +00002950 RD->addAttr(::new (S.Context)
2951 TransparentUnionAttr(Attr.getRange(), S.Context,
2952 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002953}
2954
Chandler Carruthedc2c642011-07-02 00:01:44 +00002955static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002956 // Make sure that there is a string literal as the annotation's single
2957 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002958 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002959 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002960 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002961
2962 // Don't duplicate annotations that are already set.
2963 for (specific_attr_iterator<AnnotateAttr>
2964 i = D->specific_attr_begin<AnnotateAttr>(),
2965 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002966 if ((*i)->getAnnotation() == Str)
2967 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002968 }
Michael Han99315932013-01-24 16:46:58 +00002969
2970 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002971 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002972 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002973}
2974
Chandler Carruthedc2c642011-07-02 00:01:44 +00002975static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002976 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002977 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002978 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2979 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002980 return;
2981 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002982
Richard Smith848e1f12013-02-01 08:12:08 +00002983 if (Attr.getNumArgs() == 0) {
2984 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2985 true, 0, Attr.getAttributeSpellingListIndex()));
2986 return;
2987 }
2988
Aaron Ballman00e99962013-08-31 01:11:41 +00002989 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002990 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2991 S.Diag(Attr.getEllipsisLoc(),
2992 diag::err_pack_expansion_without_parameter_packs);
2993 return;
2994 }
2995
2996 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2997 return;
2998
2999 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3000 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003001}
3002
3003void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003004 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003005 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3006 SourceLocation AttrLoc = AttrRange.getBegin();
3007
Richard Smith1dba27c2013-01-29 09:02:09 +00003008 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003009 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003010 // C++11 [dcl.align]p1:
3011 // An alignment-specifier may be applied to a variable or to a class
3012 // data member, but it shall not be applied to a bit-field, a function
3013 // parameter, the formal parameter of a catch clause, or a variable
3014 // declared with the register storage class specifier. An
3015 // alignment-specifier may also be applied to the declaration of a class
3016 // or enumeration type.
3017 // C11 6.7.5/2:
3018 // An alignment attribute shall not be specified in a declaration of
3019 // a typedef, or a bit-field, or a function, or a parameter, or an
3020 // object declared with the register storage-class specifier.
3021 int DiagKind = -1;
3022 if (isa<ParmVarDecl>(D)) {
3023 DiagKind = 0;
3024 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3025 if (VD->getStorageClass() == SC_Register)
3026 DiagKind = 1;
3027 if (VD->isExceptionVariable())
3028 DiagKind = 2;
3029 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3030 if (FD->isBitField())
3031 DiagKind = 3;
3032 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00003033 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3034 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00003035 << (TmpAttr.isC11() ? ExpectedVariableOrField
3036 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003037 return;
3038 }
3039 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003040 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smithbc8caaf2013-02-22 04:55:39 +00003041 << TmpAttr.isC11() << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003042 return;
3043 }
3044 }
3045
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003046 if (E->isTypeDependent() || E->isValueDependent()) {
3047 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003048 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3049 AA->setPackExpansion(IsPackExpansion);
3050 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003051 return;
3052 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003053
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003054 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00003055 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00003056 ExprResult ICE
3057 = VerifyIntegerConstantExpression(E, &Alignment,
3058 diag::err_aligned_attribute_argument_not_int,
3059 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003060 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003061 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003062
3063 // C++11 [dcl.align]p2:
3064 // -- if the constant expression evaluates to zero, the alignment
3065 // specifier shall have no effect
3066 // C11 6.7.5p6:
3067 // An alignment specification of zero has no effect.
3068 if (!(TmpAttr.isAlignas() && !Alignment) &&
3069 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003070 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3071 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003072 return;
3073 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003074
Richard Smith848e1f12013-02-01 08:12:08 +00003075 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00003076 // We've already verified it's a power of 2, now let's make sure it's
3077 // 8192 or less.
3078 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00003079 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00003080 << E->getSourceRange();
3081 return;
3082 }
3083 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003084
Richard Smith44c247f2013-02-22 08:32:16 +00003085 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3086 ICE.take(), SpellingListIndex);
3087 AA->setPackExpansion(IsPackExpansion);
3088 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003089}
3090
Michael Hanaf02bbe2013-02-01 01:19:17 +00003091void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003092 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003093 // FIXME: Cache the number on the Attr object if non-dependent?
3094 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003095 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3096 SpellingListIndex);
3097 AA->setPackExpansion(IsPackExpansion);
3098 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003099}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003100
Richard Smith848e1f12013-02-01 08:12:08 +00003101void Sema::CheckAlignasUnderalignment(Decl *D) {
3102 assert(D->hasAttrs() && "no attributes on decl");
3103
3104 QualType Ty;
3105 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3106 Ty = VD->getType();
3107 else
3108 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00003109 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003110 return;
3111
3112 // C++11 [dcl.align]p5, C11 6.7.5/4:
3113 // The combined effect of all alignment attributes in a declaration shall
3114 // not specify an alignment that is less strict than the alignment that
3115 // would otherwise be required for the entity being declared.
3116 AlignedAttr *AlignasAttr = 0;
3117 unsigned Align = 0;
3118 for (specific_attr_iterator<AlignedAttr>
3119 I = D->specific_attr_begin<AlignedAttr>(),
3120 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3121 if (I->isAlignmentDependent())
3122 return;
3123 if (I->isAlignas())
3124 AlignasAttr = *I;
3125 Align = std::max(Align, I->getAlignment(Context));
3126 }
3127
3128 if (AlignasAttr && Align) {
3129 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3130 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3131 if (NaturalAlign > RequestedAlign)
3132 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3133 << Ty << (unsigned)NaturalAlign.getQuantity();
3134 }
3135}
3136
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003137/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003138/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003139///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003140/// Despite what would be logical, the mode attribute is a decl attribute, not a
3141/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3142/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003143static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003144 // This attribute isn't documented, but glibc uses it. It changes
3145 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00003146 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00003147 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3148 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003149 return;
3150 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003151
Aaron Ballman00e99962013-08-31 01:11:41 +00003152 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3153 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003154
3155 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003156 if (Str.startswith("__") && Str.endswith("__"))
3157 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003158
3159 unsigned DestWidth = 0;
3160 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003161 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003162 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003163 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003164 switch (Str[0]) {
3165 case 'Q': DestWidth = 8; break;
3166 case 'H': DestWidth = 16; break;
3167 case 'S': DestWidth = 32; break;
3168 case 'D': DestWidth = 64; break;
3169 case 'X': DestWidth = 96; break;
3170 case 'T': DestWidth = 128; break;
3171 }
3172 if (Str[1] == 'F') {
3173 IntegerMode = false;
3174 } else if (Str[1] == 'C') {
3175 IntegerMode = false;
3176 ComplexMode = true;
3177 } else if (Str[1] != 'I') {
3178 DestWidth = 0;
3179 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003180 break;
3181 case 4:
3182 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3183 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003184 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003185 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003186 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003187 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003188 break;
3189 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003190 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003191 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003192 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003193 case 11:
3194 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003195 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003196 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003197 }
3198
3199 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003200 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003201 OldTy = TD->getUnderlyingType();
3202 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3203 OldTy = VD->getType();
3204 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003205 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003206 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003207 return;
3208 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003209
John McCall9dd450b2009-09-21 23:43:11 +00003210 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003211 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3212 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003213 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003214 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3215 } else if (ComplexMode) {
3216 if (!OldTy->isComplexType())
3217 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3218 } else {
3219 if (!OldTy->isFloatingType())
3220 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3221 }
3222
Mike Stump87c57ac2009-05-16 07:39:55 +00003223 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3224 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003225 // FIXME: Make sure floating-point mappings are accurate
3226 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003227 if (!DestWidth) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003228 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003229 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003230 }
3231
3232 QualType NewTy;
3233
3234 if (IntegerMode)
3235 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3236 OldTy->isSignedIntegerType());
3237 else
3238 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3239
3240 if (NewTy.isNull()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003241 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003242 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003243 }
3244
Eli Friedman4735374e2009-03-03 06:41:03 +00003245 if (ComplexMode) {
3246 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003247 }
3248
3249 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003250 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3251 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3252 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003253 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003254
3255 D->addAttr(::new (S.Context)
3256 ModeAttr(Attr.getRange(), S.Context, Name,
3257 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003258}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003259
Chandler Carruthedc2c642011-07-02 00:01:44 +00003260static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003261 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3262 if (!VD->hasGlobalStorage())
3263 S.Diag(Attr.getLoc(),
3264 diag::warn_attribute_requires_functions_or_static_globals)
3265 << Attr.getName();
3266 } else if (!isFunctionOrMethod(D)) {
3267 S.Diag(Attr.getLoc(),
3268 diag::warn_attribute_requires_functions_or_static_globals)
3269 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003270 return;
3271 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003272
Michael Han99315932013-01-24 16:46:58 +00003273 D->addAttr(::new (S.Context)
3274 NoDebugAttr(Attr.getRange(), S.Context,
3275 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003276}
3277
Chandler Carruthedc2c642011-07-02 00:01:44 +00003278static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003279 if (S.LangOpts.CUDA) {
Michael Han99315932013-01-24 16:46:58 +00003280 D->addAttr(::new (S.Context)
3281 CUDAConstantAttr(Attr.getRange(), S.Context,
3282 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003283 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003284 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003285 }
3286}
3287
Chandler Carruthedc2c642011-07-02 00:01:44 +00003288static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003289 if (S.LangOpts.CUDA) {
3290 // check the attribute arguments.
3291 if (Attr.getNumArgs() != 0) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003292 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3293 << Attr.getName() << 0;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003294 return;
3295 }
3296
Michael Han99315932013-01-24 16:46:58 +00003297 D->addAttr(::new (S.Context)
3298 CUDADeviceAttr(Attr.getRange(), S.Context,
3299 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003300 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003301 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003302 }
3303}
3304
Chandler Carruthedc2c642011-07-02 00:01:44 +00003305static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003306 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003307 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003308 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00003309 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie6adc78e2013-02-18 22:06:02 +00003310 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003311 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3312 << FD->getType()
David Blaikie6adc78e2013-02-18 22:06:02 +00003313 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003314 "void");
3315 } else {
3316 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3317 << FD->getType();
3318 }
3319 return;
3320 }
3321
Michael Han99315932013-01-24 16:46:58 +00003322 D->addAttr(::new (S.Context)
3323 CUDAGlobalAttr(Attr.getRange(), S.Context,
3324 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003325 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003326 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003327 }
3328}
3329
Chandler Carruthedc2c642011-07-02 00:01:44 +00003330static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003331 if (S.LangOpts.CUDA) {
Michael Han99315932013-01-24 16:46:58 +00003332 D->addAttr(::new (S.Context)
3333 CUDAHostAttr(Attr.getRange(), S.Context,
3334 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003335 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003336 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003337 }
3338}
3339
Chandler Carruthedc2c642011-07-02 00:01:44 +00003340static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003341 if (S.LangOpts.CUDA) {
Michael Han99315932013-01-24 16:46:58 +00003342 D->addAttr(::new (S.Context)
3343 CUDASharedAttr(Attr.getRange(), S.Context,
3344 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003345 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003346 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003347 }
3348}
3349
Chandler Carruthedc2c642011-07-02 00:01:44 +00003350static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003351 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003352 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003353 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003354 return;
3355 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003356
Michael Han99315932013-01-24 16:46:58 +00003357 D->addAttr(::new (S.Context)
3358 GNUInlineAttr(Attr.getRange(), S.Context,
3359 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003360}
3361
Chandler Carruthedc2c642011-07-02 00:01:44 +00003362static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003363 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003364
Aaron Ballman02df2e02012-12-09 17:45:41 +00003365 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003366 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003367 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3368 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003369 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003370 return;
3371
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003372 if (!isa<ObjCMethodDecl>(D)) {
3373 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3374 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003375 return;
3376 }
3377
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003378 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003379 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003380 D->addAttr(::new (S.Context)
3381 FastCallAttr(Attr.getRange(), S.Context,
3382 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003383 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003384 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003385 D->addAttr(::new (S.Context)
3386 StdCallAttr(Attr.getRange(), S.Context,
3387 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003388 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003389 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003390 D->addAttr(::new (S.Context)
3391 ThisCallAttr(Attr.getRange(), S.Context,
3392 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003393 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003394 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003395 D->addAttr(::new (S.Context)
3396 CDeclAttr(Attr.getRange(), S.Context,
3397 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003398 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003399 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003400 D->addAttr(::new (S.Context)
3401 PascalAttr(Attr.getRange(), S.Context,
3402 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003403 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003404 case AttributeList::AT_MSABI:
3405 D->addAttr(::new (S.Context)
3406 MSABIAttr(Attr.getRange(), S.Context,
3407 Attr.getAttributeSpellingListIndex()));
3408 return;
3409 case AttributeList::AT_SysVABI:
3410 D->addAttr(::new (S.Context)
3411 SysVABIAttr(Attr.getRange(), S.Context,
3412 Attr.getAttributeSpellingListIndex()));
3413 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003414 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003415 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003416 switch (CC) {
3417 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003418 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003419 break;
3420 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003421 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003422 break;
3423 default:
3424 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003425 }
3426
Michael Han99315932013-01-24 16:46:58 +00003427 D->addAttr(::new (S.Context)
3428 PcsAttr(Attr.getRange(), S.Context, PCS,
3429 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003430 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003431 }
Derek Schuffa2020962012-10-16 22:30:41 +00003432 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003433 D->addAttr(::new (S.Context)
3434 PnaclCallAttr(Attr.getRange(), S.Context,
3435 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003436 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003437 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003438 D->addAttr(::new (S.Context)
3439 IntelOclBiccAttr(Attr.getRange(), S.Context,
3440 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003441 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003442
Abramo Bagnara50099372010-04-30 13:10:51 +00003443 default:
3444 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003445 }
3446}
3447
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003448static void handleOpenCLImageAccessAttr(Sema &S, Decl *D,
3449 const AttributeList &Attr) {
3450 uint32_t ArgNum;
3451 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), ArgNum))
Guy Benyeifb36ede2013-03-24 13:58:12 +00003452 return;
Guy Benyeifb36ede2013-03-24 13:58:12 +00003453
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003454 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(Attr.getRange(),
3455 S.Context, ArgNum));
Guy Benyeifb36ede2013-03-24 13:58:12 +00003456}
3457
Aaron Ballman02df2e02012-12-09 17:45:41 +00003458bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3459 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003460 if (attr.isInvalid())
3461 return true;
3462
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003463 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003464 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003465 attr.setInvalid();
3466 return true;
3467 }
3468
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003469 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3470 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003471 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003472 case AttributeList::AT_CDecl: CC = CC_C; break;
3473 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3474 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3475 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3476 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003477 case AttributeList::AT_MSABI:
3478 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3479 CC_X86_64Win64;
3480 break;
3481 case AttributeList::AT_SysVABI:
3482 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3483 CC_C;
3484 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003485 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003486 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003487 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003488 attr.setInvalid();
3489 return true;
3490 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003491 if (StrRef == "aapcs") {
3492 CC = CC_AAPCS;
3493 break;
3494 } else if (StrRef == "aapcs-vfp") {
3495 CC = CC_AAPCS_VFP;
3496 break;
3497 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003498
3499 attr.setInvalid();
3500 Diag(attr.getLoc(), diag::err_invalid_pcs);
3501 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003502 }
Derek Schuffa2020962012-10-16 22:30:41 +00003503 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003504 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003505 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003506 }
3507
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003508 const TargetInfo &TI = Context.getTargetInfo();
3509 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3510 if (A == TargetInfo::CCCR_Warning) {
3511 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003512
3513 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3514 if (FD)
3515 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3516 TargetInfo::CCMT_NonMember;
3517 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003518 }
3519
John McCall3882ace2011-01-05 12:14:39 +00003520 return false;
3521}
3522
Chandler Carruthedc2c642011-07-02 00:01:44 +00003523static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003524 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003525
3526 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003527 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003528 return;
3529
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003530 if (!isa<ObjCMethodDecl>(D)) {
3531 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3532 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003533 return;
3534 }
Eli Friedman7044b762009-03-27 21:06:47 +00003535
Michael Han99315932013-01-24 16:46:58 +00003536 D->addAttr(::new (S.Context)
3537 RegparmAttr(Attr.getRange(), S.Context, numParams,
3538 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00003539}
3540
3541/// Checks a regparm attribute, returning true if it is ill-formed and
3542/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003543bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3544 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003545 return true;
3546
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003547 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003548 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003549 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003550 }
Eli Friedman7044b762009-03-27 21:06:47 +00003551
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003552 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003553 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003554 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
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
Douglas Gregore8bbc122011-09-02 00:18:52 +00003559 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003560 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003561 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003562 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003563 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003564 }
3565
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003566 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003567 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003568 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003569 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003570 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003571 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003572 }
3573
John McCall3882ace2011-01-05 12:14:39 +00003574 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003575}
3576
Chandler Carruthedc2c642011-07-02 00:01:44 +00003577static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00003578 if (S.LangOpts.CUDA) {
3579 // check the attribute arguments.
3580 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00003581 // FIXME: 0 is not okay.
3582 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003583 return;
3584 }
3585
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003586 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00003587 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003588 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003589 return;
3590 }
3591
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003592 uint32_t MaxThreads, MinBlocks;
3593 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1) ||
3594 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(1), MinBlocks, 2))
Peter Collingbourne827301e2010-12-12 23:03:07 +00003595 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003596
Michael Han99315932013-01-24 16:46:58 +00003597 D->addAttr(::new (S.Context)
3598 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003599 MaxThreads, MinBlocks,
Michael Han99315932013-01-24 16:46:58 +00003600 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003601 } else {
3602 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3603 }
3604}
3605
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003606static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3607 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003608 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003609 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003610 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003611 return;
3612 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003613
3614 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003615 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003616
Aaron Ballman00e99962013-08-31 01:11:41 +00003617 StringRef AttrName = Attr.getName()->getName();
3618 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003619
3620 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3621 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3622 << Attr.getName() << ExpectedFunctionOrMethod;
3623 return;
3624 }
3625
3626 uint64_t ArgumentIdx;
3627 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3628 Attr.getLoc(), 2,
Aaron Ballman00e99962013-08-31 01:11:41 +00003629 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003630 return;
3631
3632 uint64_t TypeTagIdx;
3633 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3634 Attr.getLoc(), 3,
Aaron Ballman00e99962013-08-31 01:11:41 +00003635 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003636 return;
3637
3638 bool IsPointer = (AttrName == "pointer_with_type_tag");
3639 if (IsPointer) {
3640 // Ensure that buffer has a pointer type.
3641 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3642 if (!BufferTy->isPointerType()) {
3643 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003644 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003645 }
3646 }
3647
Michael Han99315932013-01-24 16:46:58 +00003648 D->addAttr(::new (S.Context)
3649 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3650 ArgumentIdx, TypeTagIdx, IsPointer,
3651 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003652}
3653
3654static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3655 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003656 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003657 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003658 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003659 return;
3660 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003661
3662 if (!checkAttributeNumArgs(S, Attr, 1))
3663 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003664
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003665 if (!isa<VarDecl>(D)) {
3666 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3667 << Attr.getName() << ExpectedVariable;
3668 return;
3669 }
3670
Aaron Ballman00e99962013-08-31 01:11:41 +00003671 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003672 TypeSourceInfo *MatchingCTypeLoc = 0;
3673 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3674 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003675
Michael Han99315932013-01-24 16:46:58 +00003676 D->addAttr(::new (S.Context)
3677 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003678 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003679 Attr.getLayoutCompatible(),
3680 Attr.getMustBeNull(),
3681 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003682}
3683
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003684//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003685// Checker-specific attribute handlers.
3686//===----------------------------------------------------------------------===//
3687
John McCalled433932011-01-25 03:31:58 +00003688static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003689 return type->isDependentType() ||
3690 type->isObjCObjectPointerType() ||
3691 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003692}
3693static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003694 return type->isDependentType() ||
3695 type->isPointerType() ||
3696 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003697}
3698
Chandler Carruthedc2c642011-07-02 00:01:44 +00003699static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003700 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003701 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003702
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003703 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003704 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3705 cf = false;
3706 } else {
3707 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3708 cf = true;
3709 }
3710
3711 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003712 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003713 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003714 return;
3715 }
3716
3717 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003718 param->addAttr(::new (S.Context)
3719 CFConsumedAttr(Attr.getRange(), S.Context,
3720 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003721 else
Michael Han99315932013-01-24 16:46:58 +00003722 param->addAttr(::new (S.Context)
3723 NSConsumedAttr(Attr.getRange(), S.Context,
3724 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003725}
3726
Chandler Carruthedc2c642011-07-02 00:01:44 +00003727static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3728 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003729
John McCalled433932011-01-25 03:31:58 +00003730 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003731
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003732 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003733 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003734 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003735 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003736 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003737 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3738 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003739 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003740 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003741 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003742 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003743 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003744 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003745 return;
3746 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003747
John McCalled433932011-01-25 03:31:58 +00003748 bool typeOK;
3749 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003750 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003751 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003752 case AttributeList::AT_NSReturnsAutoreleased:
3753 case AttributeList::AT_NSReturnsRetained:
3754 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003755 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3756 cf = false;
3757 break;
3758
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003759 case AttributeList::AT_CFReturnsRetained:
3760 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003761 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3762 cf = true;
3763 break;
3764 }
3765
3766 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003767 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003768 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003769 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003770 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003771
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003772 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003773 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003774 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003775 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003776 D->addAttr(::new (S.Context)
3777 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3778 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003779 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003780 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003781 D->addAttr(::new (S.Context)
3782 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3783 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003784 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003785 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003786 D->addAttr(::new (S.Context)
3787 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3788 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003789 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003790 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003791 D->addAttr(::new (S.Context)
3792 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3793 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003794 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003795 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003796 D->addAttr(::new (S.Context)
3797 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3798 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003799 return;
3800 };
3801}
3802
John McCallcf166702011-07-22 08:53:00 +00003803static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3804 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003805 const int EP_ObjCMethod = 1;
3806 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003807
John McCallcf166702011-07-22 08:53:00 +00003808 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003809 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003810 if (isa<ObjCMethodDecl>(D))
3811 resultType = cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003812 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003813 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003814
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003815 if (!resultType->isReferenceType() &&
3816 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003817 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003818 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003819 << attr.getName()
3820 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003821 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003822
3823 // Drop the attribute.
3824 return;
3825 }
3826
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003827 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003828 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3829 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003830}
3831
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003832static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3833 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003834 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003835
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003836 DeclContext *DC = method->getDeclContext();
3837 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3838 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3839 << attr.getName() << 0;
3840 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3841 return;
3842 }
3843 if (method->getMethodFamily() == OMF_dealloc) {
3844 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3845 << attr.getName() << 1;
3846 return;
3847 }
3848
Michael Han99315932013-01-24 16:46:58 +00003849 method->addAttr(::new (S.Context)
3850 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3851 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003852}
3853
John McCall32f5fe12011-09-30 05:12:12 +00003854/// Handle cf_audited_transfer and cf_unknown_transfer.
3855static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003856 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall32f5fe12011-09-30 05:12:12 +00003857
3858 // Check whether there's a conflicting attribute already present.
3859 Attr *Existing;
3860 if (IsAudited) {
3861 Existing = D->getAttr<CFUnknownTransferAttr>();
3862 } else {
3863 Existing = D->getAttr<CFAuditedTransferAttr>();
3864 }
3865 if (Existing) {
3866 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
3867 << A.getName()
3868 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
3869 << A.getRange() << Existing->getRange();
3870 return;
3871 }
3872
3873 // All clear; add the attribute.
3874 if (IsAudited) {
Michael Han99315932013-01-24 16:46:58 +00003875 D->addAttr(::new (S.Context)
3876 CFAuditedTransferAttr(A.getRange(), S.Context,
3877 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003878 } else {
Michael Han99315932013-01-24 16:46:58 +00003879 D->addAttr(::new (S.Context)
3880 CFUnknownTransferAttr(A.getRange(), S.Context,
3881 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003882 }
3883}
3884
John McCallf1e8b342011-09-29 07:17:38 +00003885static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
3886 const AttributeList &Attr) {
3887 RecordDecl *RD = dyn_cast<RecordDecl>(D);
3888 if (!RD || RD->isUnion()) {
3889 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003890 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00003891 }
3892
Aaron Ballman00e99962013-08-31 01:11:41 +00003893 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallf1e8b342011-09-29 07:17:38 +00003894
3895 // In Objective-C, verify that the type names an Objective-C type.
3896 // We don't want to check this outside of ObjC because people sometimes
3897 // do crazy C declarations of Objective-C types.
Aaron Ballman00e99962013-08-31 01:11:41 +00003898 if (Parm && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00003899 // Check for an existing type with this name.
Aaron Ballman00e99962013-08-31 01:11:41 +00003900 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallf1e8b342011-09-29 07:17:38 +00003901 Sema::LookupOrdinaryName);
3902 if (S.LookupName(R, Sc)) {
3903 NamedDecl *Target = R.getFoundDecl();
3904 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
3905 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
3906 S.Diag(Target->getLocStart(), diag::note_declared_at);
3907 }
3908 }
3909 }
3910
Michael Han99315932013-01-24 16:46:58 +00003911 D->addAttr(::new (S.Context)
Aaron Ballman00e99962013-08-31 01:11:41 +00003912 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han99315932013-01-24 16:46:58 +00003913 Attr.getAttributeSpellingListIndex()));
John McCallf1e8b342011-09-29 07:17:38 +00003914}
3915
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003916static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3917 const AttributeList &Attr) {
Fariborz Jahaniandb3d8552013-11-19 00:09:48 +00003918 if (!isa<RecordDecl>(D)) {
Fariborz Jahanianf720f862013-11-19 17:42:25 +00003919 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3920 << Attr.getName()
3921 << (S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass
3922 : ExpectedStructOrUnion);
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003923 return;
3924 }
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003925
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003926 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003927
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003928 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003929 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003930 return;
3931 }
3932
3933 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003934 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003935 Attr.getAttributeSpellingListIndex()));
3936}
3937
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003938static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3939 const AttributeList &Attr) {
3940 if (!isa<RecordDecl>(D)) {
3941 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3942 << Attr.getName()
3943 << (S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass
3944 : ExpectedStructOrUnion);
3945 return;
3946 }
3947
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003948 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003949
3950 if (!Parm) {
3951 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3952 return;
3953 }
3954
3955 D->addAttr(::new (S.Context)
3956 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3957 Attr.getAttributeSpellingListIndex()));
3958}
3959
Chandler Carruthedc2c642011-07-02 00:01:44 +00003960static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3961 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003962 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003963
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003964 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003965 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003966}
3967
Chandler Carruthedc2c642011-07-02 00:01:44 +00003968static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3969 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003970 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003971 QualType type = vd->getType();
3972
3973 if (!type->isDependentType() &&
3974 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003975 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003976 << type;
3977 return;
3978 }
3979
3980 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3981
3982 // If we have no lifetime yet, check the lifetime we're presumably
3983 // going to infer.
3984 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3985 lifetime = type->getObjCARCImplicitLifetime();
3986
3987 switch (lifetime) {
3988 case Qualifiers::OCL_None:
3989 assert(type->isDependentType() &&
3990 "didn't infer lifetime for non-dependent type?");
3991 break;
3992
3993 case Qualifiers::OCL_Weak: // meaningful
3994 case Qualifiers::OCL_Strong: // meaningful
3995 break;
3996
3997 case Qualifiers::OCL_ExplicitNone:
3998 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003999 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004000 << (lifetime == Qualifiers::OCL_Autoreleasing);
4001 break;
4002 }
4003
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004004 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004005 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4006 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004007}
4008
Francois Picheta83957a2010-12-19 06:50:37 +00004009//===----------------------------------------------------------------------===//
4010// Microsoft specific attribute handlers.
4011//===----------------------------------------------------------------------===//
4012
Reid Kleckner140c4a72013-05-17 14:04:52 +00004013// Check if MS extensions or some other language extensions are enabled. If
4014// not, issue a diagnostic that the given attribute is unused.
4015static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4016 bool OtherExtension = false) {
4017 if (S.LangOpts.MicrosoftExt || OtherExtension)
4018 return true;
4019 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4020 return false;
4021}
4022
Chandler Carruthedc2c642011-07-02 00:01:44 +00004023static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00004024 if (!S.LangOpts.CPlusPlus) {
4025 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4026 << Attr.getName() << AttributeLangSupport::C;
4027 return;
4028 }
4029
Reid Kleckner140c4a72013-05-17 14:04:52 +00004030 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4031 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004032
Aaron Ballman60e705e2013-11-24 20:58:02 +00004033 if (!isa<CXXRecordDecl>(D)) {
4034 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4035 << Attr.getName() << ExpectedClass;
4036 return;
4037 }
4038
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004039 StringRef StrRef;
4040 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00004041 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00004042 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004043
David Majnemer89085342013-08-09 08:56:20 +00004044 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4045 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00004046 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4047 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00004048
Reid Kleckner140c4a72013-05-17 14:04:52 +00004049 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00004050 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004051 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004052 return;
4053 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004054
David Majnemer89085342013-08-09 08:56:20 +00004055 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004056 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00004057 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004058 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00004059 return;
4060 }
David Majnemer89085342013-08-09 08:56:20 +00004061 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004062 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004063 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004064 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004065 }
Francois Picheta83957a2010-12-19 06:50:37 +00004066
David Majnemer89085342013-08-09 08:56:20 +00004067 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4068 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004069}
4070
John McCall8d32c052012-05-22 21:28:12 +00004071static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004072 if (!checkMicrosoftExt(S, Attr))
Nico Weberefa45b22012-11-07 21:31:36 +00004073 return;
Nico Weberefa45b22012-11-07 21:31:36 +00004074
4075 AttributeList::Kind Kind = Attr.getKind();
4076 if (Kind == AttributeList::AT_SingleInheritance)
4077 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004078 ::new (S.Context)
4079 SingleInheritanceAttr(Attr.getRange(), S.Context,
4080 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004081 else if (Kind == AttributeList::AT_MultipleInheritance)
4082 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004083 ::new (S.Context)
4084 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4085 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004086 else if (Kind == AttributeList::AT_VirtualInheritance)
4087 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004088 ::new (S.Context)
4089 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4090 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004091}
4092
4093static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004094 if (!checkMicrosoftExt(S, Attr))
4095 return;
4096
4097 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmancc14f3a2013-11-22 21:49:04 +00004098 if (Kind == AttributeList::AT_Win64)
4099 D->addAttr(
4100 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4101 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004102}
4103
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004104static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004105 if (!checkMicrosoftExt(S, Attr))
4106 return;
4107 D->addAttr(::new (S.Context)
4108 ForceInlineAttr(Attr.getRange(), S.Context,
4109 Attr.getAttributeSpellingListIndex()));
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004110}
4111
Reid Klecknerb144d362013-05-20 14:02:37 +00004112static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4113 if (!checkMicrosoftExt(S, Attr))
4114 return;
4115 // Check linkage after possibly merging declaratinos. See
4116 // checkAttributesAfterMerging().
4117 D->addAttr(::new (S.Context)
4118 SelectAnyAttr(Attr.getRange(), S.Context,
4119 Attr.getAttributeSpellingListIndex()));
4120}
4121
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004122/// Handles semantic checking for features that are common to all attributes,
4123/// such as checking whether a parameter was properly specified, or the correct
4124/// number of arguments were passed, etc.
4125static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4126 const AttributeList &Attr) {
4127 // Several attributes carry different semantics than the parsing requires, so
4128 // those are opted out of the common handling.
4129 //
4130 // We also bail on unknown and ignored attributes because those are handled
4131 // as part of the target-specific handling logic.
4132 if (Attr.hasCustomParsing() ||
4133 Attr.getKind() == AttributeList::UnknownAttribute ||
4134 Attr.getKind() == AttributeList::IgnoredAttribute)
4135 return false;
4136
4137 // If there are no optional arguments, then checking for the argument count
4138 // is trivial.
4139 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
4140 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4141 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00004142
4143 // Check whether the attribute appertains to the given subject.
4144 if (!Attr.diagnoseAppertainsTo(S, D))
4145 return true;
4146
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004147 return false;
4148}
4149
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004150//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004151// Top Level Sema Entry Points
4152//===----------------------------------------------------------------------===//
4153
Richard Smithf8a75c32013-08-29 00:47:48 +00004154/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4155/// the attribute applies to decls. If the attribute is a type attribute, just
4156/// silently ignore it if a GNU attribute.
4157static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4158 const AttributeList &Attr,
4159 bool IncludeCXX11Attributes) {
4160 if (Attr.isInvalid())
4161 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004162
Richard Smithf8a75c32013-08-29 00:47:48 +00004163 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4164 // instead.
4165 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4166 return;
4167
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004168 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4169 return;
4170
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004171 switch (Attr.getKind()) {
Richard Smith10876ef2013-01-17 01:30:42 +00004172 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4173 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4174 case AttributeList::AT_IBOutletCollection:
4175 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004176 case AttributeList::AT_AddressSpace:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004177 case AttributeList::AT_ObjCGC:
4178 case AttributeList::AT_VectorSize:
4179 case AttributeList::AT_NeonVectorType:
4180 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004181 case AttributeList::AT_Ptr32:
4182 case AttributeList::AT_Ptr64:
4183 case AttributeList::AT_SPtr:
4184 case AttributeList::AT_UPtr:
Mike Stumpd3bb5572009-07-24 19:02:52 +00004185 // Ignore these, these are type attributes, handled by
4186 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004187 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004188 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4189 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4190 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4191 case AttributeList::AT_AlwaysInline:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004192 handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004193 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004194 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00004195 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004196 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4197 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4198 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004199 handleDependencyAttr(S, scope, D, Attr);
4200 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004201 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4202 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4203 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004204 case AttributeList::AT_CXX11NoReturn:
4205 handleCXX11NoReturnAttr(S, D, Attr);
4206 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004207 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004208 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004209 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004210 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4211 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004212 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004213 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004214 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004215 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00004216 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004217 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4218 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4219 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman9a226212013-08-28 23:13:26 +00004220 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4221 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004222 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4223 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004224 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004225 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004226 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004227 case AttributeList::AT_MayAlias:
4228 handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00004229 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004230 case AttributeList::AT_NoCommon:
4231 handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004232 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004233 case AttributeList::AT_Overloadable:
4234 handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break;
Richard Smith852e9ce2013-11-27 01:46:48 +00004235 case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004236 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4237 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004238 case AttributeList::AT_Naked:
4239 handleSimpleAttribute<NakedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004240 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4241 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4242 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4243 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004244
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004245 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004246 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004247 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004248 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004249
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004250 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004251 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4252
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004253 case AttributeList::AT_ObjCRequiresSuper:
4254 handleObjCRequiresSuperAttr(S, D, Attr); break;
4255
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004256 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00004257 handleNSBridgedAttr(S, scope, D, Attr); break;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004258
4259 case AttributeList::AT_ObjCBridge:
4260 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004261
4262 case AttributeList::AT_ObjCBridgeMutable:
4263 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00004264
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004265 case AttributeList::AT_CFAuditedTransfer:
4266 case AttributeList::AT_CFUnknownTransfer:
John McCall32f5fe12011-09-30 05:12:12 +00004267 handleCFTransferAttr(S, D, Attr); break;
4268
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004269 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004270 case AttributeList::AT_CFConsumed:
4271 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4272 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004273 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004274
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004275 case AttributeList::AT_NSReturnsAutoreleased:
4276 case AttributeList::AT_NSReturnsNotRetained:
4277 case AttributeList::AT_CFReturnsNotRetained:
4278 case AttributeList::AT_NSReturnsRetained:
4279 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004280 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004281
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004282 case AttributeList::AT_WorkGroupSizeHint:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004283 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004284 handleWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00004285
Joey Goulyaba589c2013-03-08 09:42:32 +00004286 case AttributeList::AT_VecTypeHint:
4287 handleVecTypeHint(S, D, Attr); break;
4288
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004289 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004290 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004291
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004292 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4293 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4294 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004295 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004296 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004297 case AttributeList::AT_ArcWeakrefUnavailable:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004298 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004299 case AttributeList::AT_ObjCRootClass:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004300 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break;
Ted Kremenek28eace62013-11-23 01:01:34 +00004301 case AttributeList::AT_ObjCSuppressProtocol:
4302 handleObjCSuppresProtocolAttr(S, D, Attr);
4303 break;
4304 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004305 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004306 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4307 case AttributeList::AT_ReturnsTwice:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004308 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004309 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004310 case AttributeList::AT_Visibility:
4311 handleVisibilityAttr(S, D, Attr, false);
4312 break;
4313 case AttributeList::AT_TypeVisibility:
4314 handleVisibilityAttr(S, D, Attr, true);
4315 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004316 case AttributeList::AT_WarnUnused:
4317 handleWarnUnusedAttr(S, D, Attr);
4318 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004319 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004320 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004321 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4322 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4323 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4324 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004325 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004326 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004327 case AttributeList::AT_ObjCException:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004328 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004329 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004330 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004331 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004332 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4333 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4334 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4335 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004336 case AttributeList::AT_Pure:
4337 handleSimpleAttribute<PureAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004338 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4339 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004340 case AttributeList::AT_NoInline:
4341 handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004342 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004343 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004344 // Just ignore
4345 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004346 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004347 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004348 case AttributeList::AT_StdCall:
4349 case AttributeList::AT_CDecl:
4350 case AttributeList::AT_FastCall:
4351 case AttributeList::AT_ThisCall:
4352 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004353 case AttributeList::AT_MSABI:
4354 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004355 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004356 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004357 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004358 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004359 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004360 case AttributeList::AT_OpenCLKernel:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004361 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004362 case AttributeList::AT_OpenCLImageAccess:
4363 handleOpenCLImageAccessAttr(S, D, Attr);
4364 break;
John McCall8d32c052012-05-22 21:28:12 +00004365
4366 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004367 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004368 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004369 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004370 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004371 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004372 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004373 case AttributeList::AT_SingleInheritance:
4374 case AttributeList::AT_MultipleInheritance:
4375 case AttributeList::AT_VirtualInheritance:
John McCall8d32c052012-05-22 21:28:12 +00004376 handleInheritanceAttr(S, D, Attr);
4377 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004378 case AttributeList::AT_Win64:
John McCall8d32c052012-05-22 21:28:12 +00004379 handlePortabilityAttr(S, D, Attr);
4380 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004381 case AttributeList::AT_ForceInline:
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004382 handleForceInlineAttr(S, D, Attr);
4383 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004384 case AttributeList::AT_SelectAny:
4385 handleSelectAnyAttr(S, D, Attr);
4386 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004387
4388 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004389 case AttributeList::AT_AssertExclusiveLock:
4390 handleAssertExclusiveLockAttr(S, D, Attr);
4391 break;
4392 case AttributeList::AT_AssertSharedLock:
4393 handleAssertSharedLockAttr(S, D, Attr);
4394 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004395 case AttributeList::AT_GuardedVar:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004396 handleGuardedVarAttr(S, D, Attr);
4397 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004398 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004399 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004400 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004401 case AttributeList::AT_ScopedLockable:
Michael Han3be3b442012-07-23 18:48:41 +00004402 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004403 break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004404 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004405 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004406 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004407 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004408 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004409 break;
4410 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004411 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004412 break;
4413 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004414 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004415 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004416 case AttributeList::AT_Lockable:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004417 handleLockableAttr(S, D, Attr);
4418 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004419 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004420 handleGuardedByAttr(S, D, Attr);
4421 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004422 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004423 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004424 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004425 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004426 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004427 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004428 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004429 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004430 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004431 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004432 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004433 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004434 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004435 handleLockReturnedAttr(S, D, Attr);
4436 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004437 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004438 handleLocksExcludedAttr(S, D, Attr);
4439 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004440 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004441 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004442 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004443 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004444 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004445 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004446 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004447 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004448 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004449 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004450 handleUnlockFunAttr(S, D, Attr);
4451 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004452 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004453 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004454 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004455 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004456 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004457 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004458
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004459 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004460 case AttributeList::AT_Consumable:
4461 handleConsumableAttr(S, D, Attr);
4462 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004463 case AttributeList::AT_CallableWhen:
4464 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004465 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004466 case AttributeList::AT_ParamTypestate:
4467 handleParamTypestateAttr(S, D, Attr);
4468 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004469 case AttributeList::AT_ReturnTypestate:
4470 handleReturnTypestateAttr(S, D, Attr);
4471 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004472 case AttributeList::AT_SetTypestate:
4473 handleSetTypestateAttr(S, D, Attr);
4474 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004475 case AttributeList::AT_TestTypestate:
4476 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004477 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004478
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004479 // Type safety attributes.
4480 case AttributeList::AT_ArgumentWithTypeTag:
4481 handleArgumentWithTypeTagAttr(S, D, Attr);
4482 break;
4483 case AttributeList::AT_TypeTagForDatatype:
4484 handleTypeTagForDatatypeAttr(S, D, Attr);
4485 break;
4486
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004487 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004488 // Ask target about the attribute.
4489 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4490 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004491 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4492 diag::warn_unhandled_ms_attribute_ignored :
4493 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004494 break;
4495 }
4496}
4497
4498/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4499/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004500void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004501 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004502 bool IncludeCXX11Attributes) {
4503 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004504 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004505
4506 // GCC accepts
4507 // static int a9 __attribute__((weakref));
4508 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004509 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004510 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00004511 cast<NamedDecl>(D)->getNameAsString();
4512 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004513 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004514 }
4515}
4516
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004517// Annotation attributes are the only attributes allowed after an access
4518// specifier.
4519bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4520 const AttributeList *AttrList) {
4521 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004522 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004523 handleAnnotateAttr(*this, ASDecl, *l);
4524 } else {
4525 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4526 return true;
4527 }
4528 }
4529
4530 return false;
4531}
4532
John McCall42856de2011-10-01 05:17:03 +00004533/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4534/// contains any decl attributes that we should warn about.
4535static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4536 for ( ; A; A = A->getNext()) {
4537 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004538 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004539 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4540
4541 if (A->getKind() == AttributeList::UnknownAttribute) {
4542 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4543 << A->getName() << A->getRange();
4544 } else {
4545 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4546 << A->getName() << A->getRange();
4547 }
4548 }
4549}
4550
4551/// checkUnusedDeclAttributes - Given a declarator which is not being
4552/// used to build a declaration, complain about any decl attributes
4553/// which might be lying around on it.
4554void Sema::checkUnusedDeclAttributes(Declarator &D) {
4555 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4556 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4557 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4558 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4559}
4560
Ryan Flynn7d470f32009-07-30 03:15:39 +00004561/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004562/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004563NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4564 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004565 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004566 NamedDecl *NewD = 0;
4567 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004568 FunctionDecl *NewFD;
4569 // FIXME: Missing call to CheckFunctionDeclaration().
4570 // FIXME: Mangling?
4571 // FIXME: Is the qualifier info correct?
4572 // FIXME: Is the DeclContext correct?
4573 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4574 Loc, Loc, DeclarationName(II),
4575 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004576 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004577 FD->hasPrototype(),
4578 false/*isConstexprSpecified*/);
4579 NewD = NewFD;
4580
4581 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004582 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004583
4584 // Fake up parameter variables; they are declared as if this were
4585 // a typedef.
4586 QualType FDTy = FD->getType();
4587 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4588 SmallVector<ParmVarDecl*, 16> Params;
4589 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4590 AE = FT->arg_type_end(); AI != AE; ++AI) {
4591 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4592 Param->setScopeInfo(0, Params.size());
4593 Params.push_back(Param);
4594 }
David Blaikie9c70e042011-09-21 18:16:56 +00004595 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004596 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004597 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4598 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004599 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004600 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004601 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004602 if (VD->getQualifier()) {
4603 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004604 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004605 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004606 }
4607 return NewD;
4608}
4609
James Dennett634962f2012-06-14 21:40:34 +00004610/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004611/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004612void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004613 if (W.getUsed()) return; // only do this once
4614 W.setUsed(true);
4615 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4616 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004617 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004618 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4619 NDId->getName()));
4620 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00004621 WeakTopLevelDecl.push_back(NewD);
4622 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4623 // to insert Decl at TU scope, sorry.
4624 DeclContext *SavedContext = CurContext;
4625 CurContext = Context.getTranslationUnitDecl();
4626 PushOnScopeChains(NewD, S);
4627 CurContext = SavedContext;
4628 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004629 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004630 }
4631}
4632
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004633void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4634 // It's valid to "forward-declare" #pragma weak, in which case we
4635 // have to do this.
4636 LoadExternalWeakUndeclaredIdentifiers();
4637 if (!WeakUndeclaredIdentifiers.empty()) {
4638 NamedDecl *ND = NULL;
4639 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4640 if (VD->isExternC())
4641 ND = VD;
4642 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4643 if (FD->isExternC())
4644 ND = FD;
4645 if (ND) {
4646 if (IdentifierInfo *Id = ND->getIdentifier()) {
4647 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4648 = WeakUndeclaredIdentifiers.find(Id);
4649 if (I != WeakUndeclaredIdentifiers.end()) {
4650 WeakInfo W = I->second;
4651 DeclApplyPragmaWeak(S, ND, W);
4652 WeakUndeclaredIdentifiers[Id] = W;
4653 }
4654 }
4655 }
4656 }
4657}
4658
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004659/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4660/// it, apply them to D. This is a bit tricky because PD can have attributes
4661/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004662void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004663 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004664 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004665 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004666
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004667 // Walk the declarator structure, applying decl attributes that were in a type
4668 // position to the decl itself. This handles cases like:
4669 // int *__attr__(x)** D;
4670 // when X is a decl attribute.
4671 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4672 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004673 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004674
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004675 // Finally, apply any attributes on the decl itself.
4676 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004677 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004678}
John McCall28a6aea2009-11-04 02:18:39 +00004679
John McCall31168b02011-06-15 23:02:42 +00004680/// Is the given declaration allowed to use a forbidden type?
4681static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4682 // Private ivars are always okay. Unfortunately, people don't
4683 // always properly make their ivars private, even in system headers.
4684 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004685 // Function declarations in sys headers will be marked unavailable.
4686 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4687 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004688 return false;
4689
4690 // Require it to be declared in a system header.
4691 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4692}
4693
4694/// Handle a delayed forbidden-type diagnostic.
4695static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4696 Decl *decl) {
4697 if (decl && isForbiddenTypeAllowed(S, decl)) {
4698 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4699 "this system declaration uses an unsupported type"));
4700 return;
4701 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004702 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004703 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004704 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004705 // kind of forbidden type messages on unavailable functions.
4706 if (FD->hasAttr<UnavailableAttr>() &&
4707 diag.getForbiddenTypeDiagnostic() ==
4708 diag::err_arc_array_param_no_ownership) {
4709 diag.Triggered = true;
4710 return;
4711 }
4712 }
John McCall31168b02011-06-15 23:02:42 +00004713
4714 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4715 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4716 diag.Triggered = true;
4717}
4718
John McCall2ec85372012-05-07 06:16:41 +00004719void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4720 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004721 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004722 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004723
John McCall2ec85372012-05-07 06:16:41 +00004724 // When delaying diagnostics to run in the context of a parsed
4725 // declaration, we only want to actually emit anything if parsing
4726 // succeeds.
4727 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004728
John McCall2ec85372012-05-07 06:16:41 +00004729 // We emit all the active diagnostics in this pool or any of its
4730 // parents. In general, we'll get one pool for the decl spec
4731 // and a child pool for each declarator; in a decl group like:
4732 // deprecated_typedef foo, *bar, baz();
4733 // only the declarator pops will be passed decls. This is correct;
4734 // we really do need to consider delayed diagnostics from the decl spec
4735 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004736 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004737 do {
John McCall6347b682012-05-07 06:16:58 +00004738 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004739 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4740 // This const_cast is a bit lame. Really, Triggered should be mutable.
4741 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004742 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004743 continue;
4744
John McCallc1465822011-02-14 07:13:47 +00004745 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004746 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00004747 // Don't bother giving deprecation diagnostics if the decl is invalid.
4748 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00004749 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004750 break;
4751
4752 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004753 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004754 break;
John McCall31168b02011-06-15 23:02:42 +00004755
4756 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004757 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004758 break;
John McCall86121512010-01-27 03:50:35 +00004759 }
4760 }
John McCall2ec85372012-05-07 06:16:41 +00004761 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004762}
4763
John McCall6347b682012-05-07 06:16:58 +00004764/// Given a set of delayed diagnostics, re-emit them as if they had
4765/// been delayed in the current context instead of in the given pool.
4766/// Essentially, this just moves them to the current pool.
4767void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4768 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4769 assert(curPool && "re-emitting in undelayed context not supported");
4770 curPool->steal(pool);
4771}
4772
John McCall28a6aea2009-11-04 02:18:39 +00004773static bool isDeclDeprecated(Decl *D) {
4774 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004775 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004776 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004777 // A category implicitly has the availability of the interface.
4778 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4779 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004780 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4781 return false;
4782}
4783
Eli Friedman971bfa12012-08-08 21:52:41 +00004784static void
4785DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
4786 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004787 const ObjCInterfaceDecl *UnknownObjCClass,
4788 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedman971bfa12012-08-08 21:52:41 +00004789 DeclarationName Name = D->getDeclName();
4790 if (!Message.empty()) {
4791 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
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 if (!UnknownObjCClass) {
4799 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
4800 S.Diag(D->getLocation(),
4801 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4802 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004803 if (ObjCPropery)
4804 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4805 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00004806 } else {
4807 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
4808 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4809 }
4810}
4811
John McCallb45a1e72010-08-26 02:13:20 +00004812void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00004813 Decl *Ctx) {
4814 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00004815 return;
4816
John McCall86121512010-01-27 03:50:35 +00004817 DD.Triggered = true;
Eli Friedman971bfa12012-08-08 21:52:41 +00004818 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
4819 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004820 DD.getUnknownObjCClass(),
4821 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004822}
4823
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004824void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004825 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004826 const ObjCInterfaceDecl *UnknownObjCClass,
4827 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004828 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004829 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004830 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4831 UnknownObjCClass,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004832 ObjCProperty,
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004833 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004834 return;
4835 }
4836
4837 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00004838 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00004839 return;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004840 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004841}