blob: 1ae62ef3af007724b5cfd209ace6bee0d4f135e2 [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 TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000053 Ty = decl->getUnderlyingType();
54 else
55 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000056
Chris Lattner2c6fcf52008-06-26 18:38:35 +000057 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000058 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000059 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000060 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000061
John McCall9dd450b2009-09-21 23:43:11 +000062 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000063}
64
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000065// FIXME: We should provide an abstraction around a method or function
66// to provide the following bits of information.
67
Nuno Lopes518e3702009-12-20 23:11:08 +000068/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000069/// type (function or function-typed variable).
Chandler Carruthff4c4f02011-07-01 23:49:12 +000070static bool isFunction(const Decl *D) {
71 return getFunctionType(D, false) != NULL;
Ted Kremenek527042b2009-08-14 20:49:40 +000072}
73
74/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000075/// type (function or function-typed variable) or an Objective-C
76/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000077static bool isFunctionOrMethod(const Decl *D) {
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +000078 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000079}
80
Fariborz Jahanian4447e172009-05-15 23:15:03 +000081/// isFunctionOrMethodOrBlock - Return true if the given decl has function
82/// type (function or function-typed variable) or an Objective-C
83/// method or a block.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000084static bool isFunctionOrMethodOrBlock(const Decl *D) {
85 if (isFunctionOrMethod(D))
Fariborz Jahanian4447e172009-05-15 23:15:03 +000086 return true;
87 // check for block is more involved.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000088 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000089 QualType Ty = V->getType();
90 return Ty->isBlockPointerType();
91 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +000092 return isa<BlockDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000093}
94
John McCall3882ace2011-01-05 12:14:39 +000095/// Return true if the given decl has a declarator that should have
96/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000097static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +000098 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000099 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
100 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +0000101}
102
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000103/// hasFunctionProto - Return true if the given decl has a argument
104/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000105/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000106static bool hasFunctionProto(const Decl *D) {
107 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000108 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000109 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000110 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000111 return true;
112 }
113}
114
115/// getFunctionOrMethodNumArgs - Return number of function or method
116/// arguments. It is an error to call this on a K&R function (use
117/// hasFunctionProto first).
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000118static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
119 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000120 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000121 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000122 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000123 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000124}
125
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000126static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
127 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000128 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000129 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000130 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000131
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000132 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000133}
134
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000135static QualType getFunctionOrMethodResultType(const Decl *D) {
136 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000137 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000138 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000139}
140
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000141static bool isFunctionOrMethodVariadic(const Decl *D) {
142 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000143 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000144 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000145 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000146 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000147 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000148 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000149 }
150}
151
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000152static bool isInstanceMethod(const Decl *D) {
153 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000154 return MethodDecl->isInstance();
155 return false;
156}
157
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000158static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000159 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000160 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000161 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000162
John McCall96fa4842010-05-17 21:00:27 +0000163 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
164 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000165 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000166
John McCall96fa4842010-05-17 21:00:27 +0000167 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000168
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000169 // FIXME: Should we walk the chain of classes?
170 return ClsName == &Ctx.Idents.get("NSString") ||
171 ClsName == &Ctx.Idents.get("NSMutableString");
172}
173
Daniel Dunbar980c6692008-09-26 03:32:58 +0000174static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000175 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000176 if (!PT)
177 return false;
178
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000179 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000180 if (!RT)
181 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000182
Daniel Dunbar980c6692008-09-26 03:32:58 +0000183 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000184 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000185 return false;
186
187 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
188}
189
Richard Smithb87c4652013-10-31 21:23:20 +0000190static unsigned getNumAttributeArgs(const AttributeList &Attr) {
191 // FIXME: Include the type in the argument list.
192 return Attr.getNumArgs() + Attr.hasParsedType();
193}
194
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000195/// \brief Check if the attribute has exactly as many args as Num. May
196/// output an error.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000197static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000198 unsigned Num) {
199 if (getNumAttributeArgs(Attr) != Num) {
Aaron Ballmanb7243382013-07-23 19:30:11 +0000200 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
201 << Attr.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000202 return false;
203 }
204
205 return true;
206}
207
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000208
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000209/// \brief Check if the attribute has at least as many args as Num. May
210/// output an error.
211static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000212 unsigned Num) {
213 if (getNumAttributeArgs(Attr) < Num) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000214 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
215 return false;
216 }
217
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000218 return true;
219}
220
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000221/// \brief If Expr is a valid integer constant, get the value of the integer
222/// expression and return success or failure. May output an error.
223static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
224 const Expr *Expr, uint32_t &Val,
225 unsigned Idx = UINT_MAX) {
226 llvm::APSInt I(32);
227 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
228 !Expr->isIntegerConstantExpr(I, S.Context)) {
229 if (Idx != UINT_MAX)
230 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
231 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
232 << Expr->getSourceRange();
233 else
234 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
235 << Attr.getName() << AANT_ArgumentIntegerConstant
236 << Expr->getSourceRange();
237 return false;
238 }
239 Val = (uint32_t)I.getZExtValue();
240 return true;
241}
242
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000243/// \brief Check if IdxExpr is a valid argument index for a function or
244/// instance method D. May output an error.
245///
246/// \returns true if IdxExpr is a valid index.
247static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
248 StringRef AttrName,
249 SourceLocation AttrLoc,
250 unsigned AttrArgNum,
251 const Expr *IdxExpr,
252 uint64_t &Idx)
253{
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000254 assert(isFunctionOrMethod(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000255
256 // In C++ the implicit 'this' function parameter also counts.
257 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000258 bool HP = hasFunctionProto(D);
259 bool HasImplicitThisParam = isInstanceMethod(D);
260 bool IV = HP && isFunctionOrMethodVariadic(D);
261 unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) +
262 HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000263
264 llvm::APSInt IdxInt;
265 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
266 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +0000267 std::string Name = std::string("'") + AttrName.str() + std::string("'");
268 S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str()
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000269 << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000270 return false;
271 }
272
273 Idx = IdxInt.getLimitedValue();
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000274 if (Idx < 1 || (!IV && Idx > NumArgs)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000275 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
276 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
277 return false;
278 }
279 Idx--; // Convert to zero-based.
280 if (HasImplicitThisParam) {
281 if (Idx == 0) {
282 S.Diag(AttrLoc,
283 diag::err_attribute_invalid_implicit_this_argument)
284 << AttrName << IdxExpr->getSourceRange();
285 return false;
286 }
287 --Idx;
288 }
289
290 return true;
291}
292
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000293/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
294/// If not emit an error and return false. If the argument is an identifier it
295/// will emit an error with a fixit hint and treat it as if it was a string
296/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000297bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
298 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000299 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000300 // Look for identifiers. If we have one emit a hint to fix it to a literal.
301 if (Attr.isArgIdent(ArgNum)) {
302 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000303 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000304 << Attr.getName() << AANT_ArgumentString
305 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000306 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000307 Str = Loc->Ident->getName();
308 if (ArgLocation)
309 *ArgLocation = Loc->Loc;
310 return true;
311 }
312
313 // Now check for an actual string literal.
314 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
315 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
316 if (ArgLocation)
317 *ArgLocation = ArgExpr->getLocStart();
318
319 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000320 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000321 << Attr.getName() << AANT_ArgumentString;
322 return false;
323 }
324
325 Str = Literal->getString();
326 return true;
327}
328
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000329/// \brief Applies the given attribute to the Decl without performing any
330/// additional semantic checking.
331template <typename AttrType>
332static void handleSimpleAttribute(Sema &S, Decl *D,
333 const AttributeList &Attr) {
334 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
335 Attr.getAttributeSpellingListIndex()));
336}
337
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000338/// \brief Check if the passed-in expression is of type int or bool.
339static bool isIntOrBool(Expr *Exp) {
340 QualType QT = Exp->getType();
341 return QT->isBooleanType() || QT->isIntegerType();
342}
343
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000344
345// Check to see if the type is a smart pointer of some kind. We assume
346// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000347static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
348 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
349 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000350 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000351 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000352
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000353 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
354 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000355 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000356 return false;
357
358 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000359}
360
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000361/// \brief Check if passed in Decl is a pointer type.
362/// Note that this function may produce an error message.
363/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000364static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
365 const AttributeList &Attr) {
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000366 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000367 QualType QT = vd->getType();
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000368 if (QT->isAnyPointerType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000369 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000370
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000371 if (const RecordType *RT = QT->getAs<RecordType>()) {
372 // If it's an incomplete type, it could be a smart pointer; skip it.
373 // (We don't want to force template instantiation if we can avoid it,
374 // since that would alter the order in which templates are instantiated.)
375 if (RT->isIncompleteType())
376 return true;
377
378 if (threadSafetyCheckIsSmartPointer(S, RT))
379 return true;
380 }
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000381
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000382 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000383 << Attr.getName()->getName() << QT;
384 } else {
385 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
386 << Attr.getName();
387 }
388 return false;
389}
390
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000391/// \brief Checks that the passed in QualType either is of RecordType or points
392/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000393static const RecordType *getRecordType(QualType QT) {
394 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000395 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000396
397 // Now check if we point to record type.
398 if (const PointerType *PT = QT->getAs<PointerType>())
399 return PT->getPointeeType()->getAs<RecordType>();
400
401 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000402}
403
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000404
Jordy Rose740b0c22012-05-08 03:27:22 +0000405static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
406 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000407 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
408 if (RT->getDecl()->getAttr<LockableAttr>())
409 return true;
410 return false;
411}
412
413
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000414/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000415/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000416static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
417 QualType Ty) {
418 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000419
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000420 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000421 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000422 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000423 << Attr.getName() << Ty.getAsString();
424 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000425 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000426
Michael Hana9171bc2012-08-03 17:40:43 +0000427 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000428 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000429 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000430
431 // Allow smart pointers to be used as lockable objects.
432 // FIXME -- Check the type that the smart pointer points to.
433 if (threadSafetyCheckIsSmartPointer(S, RT))
434 return;
435
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000436 // Check if the type is lockable.
437 RecordDecl *RD = RT->getDecl();
438 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000439 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000440
441 // Else check if any base classes are lockable.
442 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
443 CXXBasePaths BPaths(false, false);
444 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
445 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000446 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000447
448 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
449 << Attr.getName() << Ty.getAsString();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000450}
451
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000452/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000453/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000454/// \param Sidx The attribute argument index to start checking with.
455/// \param ParamIdxOk Whether an argument can be indexing into a function
456/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000457static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000458 const AttributeList &Attr,
459 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000460 int Sidx = 0,
461 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000462 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000463 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000464
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000465 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000466 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000467 Args.push_back(ArgExp);
468 continue;
469 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000470
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000471 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000472 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000473 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000474 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000475 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000476 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000477 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000478 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000479
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000480 // We allow constant strings to be used as a placeholder for expressions
481 // that are not valid C++ syntax, but warn that they are ignored.
482 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
483 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000484 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000485 continue;
486 }
487
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000488 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000489
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000490 // A pointer to member expression of the form &MyClass::mu is treated
491 // specially -- we need to look at the type of the member.
492 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
493 if (UOp->getOpcode() == UO_AddrOf)
494 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
495 if (DRE->getDecl()->isCXXInstanceMember())
496 ArgTy = DRE->getDecl()->getType();
497
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000498 // First see if we can just cast to record type, or point to record type.
499 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000500
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000501 // Now check if we index into a record type function param.
502 if(!RT && ParamIdxOk) {
503 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000504 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
505 if(FD && IL) {
506 unsigned int NumParams = FD->getNumParams();
507 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000508 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
509 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
510 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000511 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
512 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000513 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000514 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000515 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000516 }
517 }
518
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000519 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000520
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000521 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000522 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000523}
524
Chris Lattner58418ff2008-06-29 00:16:31 +0000525//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000526// Attribute Implementations
527//===----------------------------------------------------------------------===//
528
Daniel Dunbar032db472008-07-31 22:40:48 +0000529// FIXME: All this manual attribute parsing code is gross. At the
530// least add some helper functions to check most argument patterns (#
531// and types of args).
532
Michael Hana9171bc2012-08-03 17:40:43 +0000533static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000534 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000535 if (!threadSafetyCheckIsPointer(S, D, Attr))
536 return;
537
Michael Han99315932013-01-24 16:46:58 +0000538 D->addAttr(::new (S.Context)
539 PtGuardedVarAttr(Attr.getRange(), S.Context,
540 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000541}
542
Michael Hana9171bc2012-08-03 17:40:43 +0000543static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
544 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000545 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000546 SmallVector<Expr*, 1> Args;
547 // check that all arguments are lockable objects
548 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
549 unsigned Size = Args.size();
550 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000551 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000552
Michael Han3be3b442012-07-23 18:48:41 +0000553 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000554
Michael Han3be3b442012-07-23 18:48:41 +0000555 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000556}
557
Michael Han3be3b442012-07-23 18:48:41 +0000558static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
559 Expr *Arg = 0;
560 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
561 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000562
Michael Han3be3b442012-07-23 18:48:41 +0000563 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
564}
565
Michael Hana9171bc2012-08-03 17:40:43 +0000566static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000567 const AttributeList &Attr) {
568 Expr *Arg = 0;
569 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
570 return;
571
572 if (!threadSafetyCheckIsPointer(S, D, Attr))
573 return;
574
575 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
576 S.Context, Arg));
577}
578
Michael Hana9171bc2012-08-03 17:40:43 +0000579static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
580 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000581 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000582 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000583 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000584
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000585 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000586 QualType QT = cast<ValueDecl>(D)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000587 if (!QT->isDependentType()) {
588 const RecordType *RT = getRecordType(QT);
589 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000590 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000591 << Attr.getName();
592 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000593 }
594 }
595
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000596 // Check that all arguments are lockable objects.
597 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000598 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000599 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000600
Michael Han3be3b442012-07-23 18:48:41 +0000601 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000602}
603
Michael Hana9171bc2012-08-03 17:40:43 +0000604static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000605 const AttributeList &Attr) {
606 SmallVector<Expr*, 1> Args;
607 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
608 return;
609
610 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000611 D->addAttr(::new (S.Context)
612 AcquiredAfterAttr(Attr.getRange(), S.Context,
613 StartArg, Args.size(),
614 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000615}
616
Michael Hana9171bc2012-08-03 17:40:43 +0000617static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000618 const AttributeList &Attr) {
619 SmallVector<Expr*, 1> Args;
620 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
621 return;
622
623 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000624 D->addAttr(::new (S.Context)
625 AcquiredBeforeAttr(Attr.getRange(), S.Context,
626 StartArg, Args.size(),
627 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000628}
629
Michael Hana9171bc2012-08-03 17:40:43 +0000630static bool checkLockFunAttrCommon(Sema &S, Decl *D,
631 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000632 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000633 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000634 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000635 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000636
Michael Han3be3b442012-07-23 18:48:41 +0000637 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000638}
639
Michael Hana9171bc2012-08-03 17:40:43 +0000640static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000641 const AttributeList &Attr) {
642 SmallVector<Expr*, 1> Args;
643 if (!checkLockFunAttrCommon(S, D, Attr, Args))
644 return;
645
646 unsigned Size = Args.size();
647 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000648 D->addAttr(::new (S.Context)
649 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
650 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000651}
652
Michael Hana9171bc2012-08-03 17:40:43 +0000653static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000654 const AttributeList &Attr) {
655 SmallVector<Expr*, 1> Args;
656 if (!checkLockFunAttrCommon(S, D, Attr, Args))
657 return;
658
659 unsigned Size = Args.size();
660 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000661 D->addAttr(::new (S.Context)
662 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
663 StartArg, Size,
664 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000665}
666
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000667static void handleAssertSharedLockAttr(Sema &S, Decl *D,
668 const AttributeList &Attr) {
669 SmallVector<Expr*, 1> Args;
670 if (!checkLockFunAttrCommon(S, D, Attr, Args))
671 return;
672
673 unsigned Size = Args.size();
674 Expr **StartArg = Size == 0 ? 0 : &Args[0];
675 D->addAttr(::new (S.Context)
676 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
677 Attr.getAttributeSpellingListIndex()));
678}
679
680static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
681 const AttributeList &Attr) {
682 SmallVector<Expr*, 1> Args;
683 if (!checkLockFunAttrCommon(S, D, Attr, Args))
684 return;
685
686 unsigned Size = Args.size();
687 Expr **StartArg = Size == 0 ? 0 : &Args[0];
688 D->addAttr(::new (S.Context)
689 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
690 StartArg, Size,
691 Attr.getAttributeSpellingListIndex()));
692}
693
694
Michael Hana9171bc2012-08-03 17:40:43 +0000695static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
696 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000697 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000698 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000699 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000700
Aaron Ballman00e99962013-08-31 01:11:41 +0000701 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000702 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000703 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000704 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000705 }
706
707 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000708 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000709
Michael Han3be3b442012-07-23 18:48:41 +0000710 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000711}
712
Michael Hana9171bc2012-08-03 17:40:43 +0000713static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000714 const AttributeList &Attr) {
715 SmallVector<Expr*, 2> Args;
716 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
717 return;
718
Michael Han99315932013-01-24 16:46:58 +0000719 D->addAttr(::new (S.Context)
720 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000721 Attr.getArgAsExpr(0),
722 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000723 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000724}
725
Michael Hana9171bc2012-08-03 17:40:43 +0000726static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000727 const AttributeList &Attr) {
728 SmallVector<Expr*, 2> Args;
729 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
730 return;
731
Michael Han99315932013-01-24 16:46:58 +0000732 D->addAttr(::new (S.Context)
733 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000734 Attr.getArgAsExpr(0),
735 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000736 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000737}
738
Michael Hana9171bc2012-08-03 17:40:43 +0000739static bool checkLocksRequiredCommon(Sema &S, Decl *D,
740 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000741 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000742 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000743 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000744
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000745 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000746 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000747 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000748 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000749
Michael Han3be3b442012-07-23 18:48:41 +0000750 return true;
751}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000752
Michael Hana9171bc2012-08-03 17:40:43 +0000753static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000754 const AttributeList &Attr) {
755 SmallVector<Expr*, 1> Args;
756 if (!checkLocksRequiredCommon(S, D, Attr, Args))
757 return;
758
759 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000760 D->addAttr(::new (S.Context)
761 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
762 StartArg, Args.size(),
763 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000764}
765
Michael Hana9171bc2012-08-03 17:40:43 +0000766static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000767 const AttributeList &Attr) {
768 SmallVector<Expr*, 1> Args;
769 if (!checkLocksRequiredCommon(S, D, Attr, Args))
770 return;
771
772 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000773 D->addAttr(::new (S.Context)
774 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
775 StartArg, Args.size(),
776 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000777}
778
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000779static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000780 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000781 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000782 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000783 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000784 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000785 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000786 Expr **StartArg = Size == 0 ? 0 : &Args[0];
787
Michael Han99315932013-01-24 16:46:58 +0000788 D->addAttr(::new (S.Context)
789 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
790 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000791}
792
793static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000794 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000795 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000796 SmallVector<Expr*, 1> Args;
797 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
798 unsigned Size = Args.size();
799 if (Size == 0)
800 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000801
Michael Han99315932013-01-24 16:46:58 +0000802 D->addAttr(::new (S.Context)
803 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
804 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000805}
806
807static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000808 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000809 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000810 return;
811
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000812 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000813 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000814 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000815 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000816 if (Size == 0)
817 return;
818 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000819
Michael Han99315932013-01-24 16:46:58 +0000820 D->addAttr(::new (S.Context)
821 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
822 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000823}
824
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000825static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000826 ConsumableAttr::ConsumedState DefaultState;
827
828 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000829 IdentifierLoc *IL = Attr.getArgAsIdent(0);
830 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
831 DefaultState)) {
832 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
833 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000834 return;
835 }
David Blaikie16f76d22013-09-06 01:28:43 +0000836 } else {
837 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
838 << Attr.getName() << AANT_ArgumentIdentifier;
839 return;
840 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000841
842 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000843 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000844 Attr.getAttributeSpellingListIndex()));
845}
846
847static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
848 const AttributeList &Attr) {
849 ASTContext &CurrContext = S.getASTContext();
850 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
851
852 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
853 if (!RD->hasAttr<ConsumableAttr>()) {
854 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
855 RD->getNameAsString();
856
857 return false;
858 }
859 }
860
861 return true;
862}
863
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000864
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000865static void handleCallableWhenAttr(Sema &S, Decl *D,
866 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000867 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
868 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000869
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000870 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
871 return;
872
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000873 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
874 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
875 CallableWhenAttr::ConsumedState CallableState;
876
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000877 StringRef StateString;
878 SourceLocation Loc;
879 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
880 return;
881
882 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000883 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000884 S.Diag(Loc, diag::warn_attribute_type_not_supported)
885 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000886 return;
887 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000888
889 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000890 }
891
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000892 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000893 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
894 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000895}
896
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000897
DeLesley Hutchins69391772013-10-17 23:23:53 +0000898static void handleParamTypestateAttr(Sema &S, Decl *D,
899 const AttributeList &Attr) {
900 if (!checkAttributeNumArgs(S, Attr, 1)) return;
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000901
DeLesley Hutchins69391772013-10-17 23:23:53 +0000902 ParamTypestateAttr::ConsumedState ParamState;
903
904 if (Attr.isArgIdent(0)) {
905 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
906 StringRef StateString = Ident->Ident->getName();
907
908 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
909 ParamState)) {
910 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
911 << Attr.getName() << StateString;
912 return;
913 }
914 } else {
915 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
916 Attr.getName() << AANT_ArgumentIdentifier;
917 return;
918 }
919
920 // FIXME: This check is currently being done in the analysis. It can be
921 // enabled here only after the parser propagates attributes at
922 // template specialization definition, not declaration.
923 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
924 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
925 //
926 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
927 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
928 // ReturnType.getAsString();
929 // return;
930 //}
931
932 D->addAttr(::new (S.Context)
933 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
934 Attr.getAttributeSpellingListIndex()));
935}
936
937
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000938static void handleReturnTypestateAttr(Sema &S, Decl *D,
939 const AttributeList &Attr) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000940 if (!checkAttributeNumArgs(S, Attr, 1)) return;
941
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000942 ReturnTypestateAttr::ConsumedState ReturnState;
943
944 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000945 IdentifierLoc *IL = Attr.getArgAsIdent(0);
946 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
947 ReturnState)) {
948 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
949 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000950 return;
951 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000952 } else {
953 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
954 Attr.getName() << AANT_ArgumentIdentifier;
955 return;
956 }
957
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000958 // FIXME: This check is currently being done in the analysis. It can be
959 // enabled here only after the parser propagates attributes at
960 // template specialization definition, not declaration.
961 //QualType ReturnType;
962 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000963 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
964 // ReturnType = Param->getType();
965 //
966 //} else if (const CXXConstructorDecl *Constructor =
967 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000968 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
969 //
970 //} else {
971 //
972 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
973 //}
974 //
975 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
976 //
977 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
978 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
979 // ReturnType.getAsString();
980 // return;
981 //}
982
983 D->addAttr(::new (S.Context)
984 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
985 Attr.getAttributeSpellingListIndex()));
986}
987
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000988
989static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000990 if (!checkAttributeNumArgs(S, Attr, 1))
991 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000992
993 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
994 return;
995
996 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000997 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +0000998 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
999 StringRef Param = Ident->Ident->getName();
1000 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1001 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1002 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001003 return;
1004 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001005 } else {
1006 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1007 Attr.getName() << AANT_ArgumentIdentifier;
1008 return;
1009 }
1010
1011 D->addAttr(::new (S.Context)
1012 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1013 Attr.getAttributeSpellingListIndex()));
1014}
1015
Chris Wailes9385f9f2013-10-29 20:28:41 +00001016static void handleTestTypestateAttr(Sema &S, Decl *D,
1017 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001018 if (!checkAttributeNumArgs(S, Attr, 1))
1019 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001020
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001021 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1022 return;
1023
Chris Wailes9385f9f2013-10-29 20:28:41 +00001024 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001025 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001026 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1027 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001028 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001029 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1030 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001031 return;
1032 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001033 } else {
1034 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1035 Attr.getName() << AANT_ArgumentIdentifier;
1036 return;
1037 }
1038
1039 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001040 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001041 Attr.getAttributeSpellingListIndex()));
1042}
1043
Chandler Carruthedc2c642011-07-02 00:01:44 +00001044static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1045 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001046 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001047 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001048}
1049
Chandler Carruthedc2c642011-07-02 00:01:44 +00001050static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001051 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001052 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001053 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001054 // If the alignment is less than or equal to 8 bits, the packed attribute
1055 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001056 if (!FD->getType()->isDependentType() &&
1057 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001058 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001059 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001060 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001061 else
Michael Han99315932013-01-24 16:46:58 +00001062 FD->addAttr(::new (S.Context)
1063 PackedAttr(Attr.getRange(), S.Context,
1064 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001065 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001066 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001067}
1068
Ted Kremenek7fd17232011-09-29 07:02:25 +00001069static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1070 // The IBOutlet/IBOutletCollection attributes only apply to instance
1071 // variables or properties of Objective-C classes. The outlet must also
1072 // have an object reference type.
1073 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1074 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001075 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001076 << Attr.getName() << VD->getType() << 0;
1077 return false;
1078 }
1079 }
1080 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1081 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001082 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001083 << Attr.getName() << PD->getType() << 1;
1084 return false;
1085 }
1086 }
1087 else {
1088 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1089 return false;
1090 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001091
Ted Kremenek7fd17232011-09-29 07:02:25 +00001092 return true;
1093}
1094
Chandler Carruthedc2c642011-07-02 00:01:44 +00001095static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001096 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001097 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001098
Michael Han99315932013-01-24 16:46:58 +00001099 D->addAttr(::new (S.Context)
1100 IBOutletAttr(Attr.getRange(), S.Context,
1101 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001102}
1103
Chandler Carruthedc2c642011-07-02 00:01:44 +00001104static void handleIBOutletCollection(Sema &S, Decl *D,
1105 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001106
1107 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001108 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001109 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1110 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001111 return;
1112 }
1113
Ted Kremenek7fd17232011-09-29 07:02:25 +00001114 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001115 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001116
Richard Smithb1f9a282013-10-31 01:56:18 +00001117 ParsedType PT;
1118
1119 if (Attr.hasParsedType())
1120 PT = Attr.getTypeArg();
1121 else {
1122 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1123 S.getScopeForContext(D->getDeclContext()->getParent()));
1124 if (!PT) {
1125 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1126 return;
1127 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001128 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001129
Richard Smithb87c4652013-10-31 21:23:20 +00001130 TypeSourceInfo *QTLoc = 0;
1131 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1132 if (!QTLoc)
1133 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001134
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001135 // Diagnose use of non-object type in iboutletcollection attribute.
1136 // FIXME. Gnu attribute extension ignores use of builtin types in
1137 // attributes. So, __attribute__((iboutletcollection(char))) will be
1138 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001139 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001140 S.Diag(Attr.getLoc(),
1141 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1142 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001143 return;
1144 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001145
Michael Han99315932013-01-24 16:46:58 +00001146 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001147 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001148 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001149}
1150
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001151static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001152 if (const RecordType *UT = T->getAsUnionType())
1153 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1154 RecordDecl *UD = UT->getDecl();
1155 for (RecordDecl::field_iterator it = UD->field_begin(),
1156 itend = UD->field_end(); it != itend; ++it) {
1157 QualType QT = it->getType();
1158 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1159 T = QT;
1160 return;
1161 }
1162 }
1163 }
1164}
1165
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001166static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopese881ce22012-06-18 16:39:04 +00001167 if (!isFunctionOrMethod(D)) {
1168 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001169 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopese881ce22012-06-18 16:39:04 +00001170 return;
1171 }
1172
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001173 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1174 return;
1175
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001176 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001177 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001178 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001179 uint64_t Idx;
1180 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1181 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001182 return;
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001183
1184 // check if the function argument is of an integer type
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001185 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001186 if (!T->isIntegerType()) {
Aaron Ballman9d695092013-07-30 14:10:17 +00001187 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1188 << Attr.getName() << AANT_ArgumentIntegerConstant
1189 << Ex->getSourceRange();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001190 return;
1191 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001192 SizeArgs.push_back(Idx);
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001193 }
1194
1195 // check if the function returns a pointer
1196 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1197 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001198 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001199 }
1200
Michael Han99315932013-01-24 16:46:58 +00001201 D->addAttr(::new (S.Context)
1202 AllocSizeAttr(Attr.getRange(), S.Context,
1203 SizeArgs.data(), SizeArgs.size(),
1204 Attr.getAttributeSpellingListIndex()));
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001205}
1206
Chandler Carruthedc2c642011-07-02 00:01:44 +00001207static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001208 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1209 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001210 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001211 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001212 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001213 return;
1214 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001215
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001216 SmallVector<unsigned, 8> NonNullArgs;
1217 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001218 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001219 uint64_t Idx;
1220 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1221 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001222 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001223
1224 // Is the function argument a pointer type?
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001225 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001226 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001227
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001228 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001229 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +00001230 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +00001231 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001232 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001233 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001234
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001235 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001236 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001237
1238 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1239 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001240 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001241 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1242 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001243 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001244 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001245 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001246 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001247
Ted Kremenek22813f42010-10-21 18:49:36 +00001248 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001249 if (NonNullArgs.empty()) {
1250 // Warn the trivial case only if attribute is not coming from a
1251 // macro instantiation.
1252 if (Attr.getLoc().isFileID())
1253 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001254 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001255 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001256 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001257
Nick Lewyckye1121512013-01-24 01:12:16 +00001258 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001259 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001260 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001261 D->addAttr(::new (S.Context)
1262 NonNullAttr(Attr.getRange(), S.Context, start, size,
1263 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001264}
1265
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001266static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1267 switch (K) {
1268 case OwnershipAttr::Holds: return "'ownership_holds'";
1269 case OwnershipAttr::Takes: return "'ownership_takes'";
1270 case OwnershipAttr::Returns: return "'ownership_returns'";
1271 }
1272 llvm_unreachable("unknown ownership");
1273}
1274
Chandler Carruthedc2c642011-07-02 00:01:44 +00001275static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001276 // This attribute must be applied to a function declaration. The first
1277 // argument to the attribute must be an identifier, the name of the resource,
1278 // for example: malloc. The following arguments must be argument indexes, the
1279 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001280 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001281 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001282 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001283
Aaron Ballman00e99962013-08-31 01:11:41 +00001284 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001285 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001286 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001287 return;
1288 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001289
Richard Smith852e9ce2013-11-27 01:46:48 +00001290 // Figure out our Kind.
1291 OwnershipAttr::OwnershipKind K =
1292 OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0,
1293 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001294
Richard Smith852e9ce2013-11-27 01:46:48 +00001295 // Check arguments.
1296 switch (K) {
1297 case OwnershipAttr::Takes:
1298 case OwnershipAttr::Holds:
1299 if (AL.getNumArgs() < 2) {
1300 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
1301 return;
1302 }
1303 break;
1304 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001305 if (AL.getNumArgs() > 2) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001306 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001307 return;
1308 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001309 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001310 }
1311
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001312 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001313 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1314 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001315 return;
1316 }
1317
Richard Smith852e9ce2013-11-27 01:46:48 +00001318 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001319
1320 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001321 StringRef ModuleName = Module->getName();
1322 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1323 ModuleName.size() > 4) {
1324 ModuleName = ModuleName.drop_front(2).drop_back(2);
1325 Module = &S.PP.getIdentifierTable().get(ModuleName);
1326 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001327
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001328 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001329 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1330 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001331 uint64_t Idx;
1332 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
Aaron Ballman00e99962013-08-31 01:11:41 +00001333 AL.getLoc(), i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001334 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001335
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001336 // Is the function argument a pointer type?
1337 QualType T = getFunctionOrMethodArgType(D, Idx);
1338 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001339 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001340 case OwnershipAttr::Takes:
1341 case OwnershipAttr::Holds:
1342 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1343 Err = 0;
1344 break;
1345 case OwnershipAttr::Returns:
1346 if (!T->isIntegerType())
1347 Err = 1;
1348 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001349 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001350 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001351 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001352 << Ex->getSourceRange();
1353 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001354 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001355
1356 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001357 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001358 i = D->specific_attr_begin<OwnershipAttr>(),
1359 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001360 // FIXME: A returns attribute should conflict with any returns attribute
1361 // with a different index too.
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001362 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1363 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1364 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1365 << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind());
1366 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001367 }
1368 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001369 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001370 }
1371
1372 unsigned* start = OwnershipArgs.data();
1373 unsigned size = OwnershipArgs.size();
1374 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001375
Michael Han99315932013-01-24 16:46:58 +00001376 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001377 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001378 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001379}
1380
Chandler Carruthedc2c642011-07-02 00:01:44 +00001381static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001382 // Check the attribute arguments.
1383 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001384 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1385 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001386 return;
1387 }
1388
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001389 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001390
Rafael Espindolac18086a2010-02-23 22:00:30 +00001391 // gcc rejects
1392 // class c {
1393 // static int a __attribute__((weakref ("v2")));
1394 // static int b() __attribute__((weakref ("f3")));
1395 // };
1396 // and ignores the attributes of
1397 // void f(void) {
1398 // static int a __attribute__((weakref ("v2")));
1399 // }
1400 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001401 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001402 if (!Ctx->isFileContext()) {
1403 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +00001404 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +00001405 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001406 }
1407
1408 // The GCC manual says
1409 //
1410 // At present, a declaration to which `weakref' is attached can only
1411 // be `static'.
1412 //
1413 // It also says
1414 //
1415 // Without a TARGET,
1416 // given as an argument to `weakref' or to `alias', `weakref' is
1417 // equivalent to `weak'.
1418 //
1419 // gcc 4.4.1 will accept
1420 // int a7 __attribute__((weakref));
1421 // as
1422 // int a7 __attribute__((weak));
1423 // This looks like a bug in gcc. We reject that for now. We should revisit
1424 // it if this behaviour is actually used.
1425
Rafael Espindolac18086a2010-02-23 22:00:30 +00001426 // GCC rejects
1427 // static ((alias ("y"), weakref)).
1428 // Should we? How to check that weakref is before or after alias?
1429
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001430 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1431 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1432 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001433 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001434 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001435 // GCC will accept anything as the argument of weakref. Should we
1436 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001437 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1438 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001439
Michael Han99315932013-01-24 16:46:58 +00001440 D->addAttr(::new (S.Context)
1441 WeakRefAttr(Attr.getRange(), S.Context,
1442 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001443}
1444
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001445static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1446 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001447 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001448 return;
1449
Douglas Gregore8bbc122011-09-02 00:18:52 +00001450 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001451 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1452 return;
1453 }
1454
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001455 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001456
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001457 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001458 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001459}
1460
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001461static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001462 if (D->hasAttr<HotAttr>()) {
1463 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1464 << Attr.getName() << "hot";
1465 return;
1466 }
1467
Michael Han99315932013-01-24 16:46:58 +00001468 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1469 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001470}
1471
1472static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001473 if (D->hasAttr<ColdAttr>()) {
1474 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1475 << Attr.getName() << "cold";
1476 return;
1477 }
1478
Michael Han99315932013-01-24 16:46:58 +00001479 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1480 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001481}
1482
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001483static void handleTLSModelAttr(Sema &S, Decl *D,
1484 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001485 StringRef Model;
1486 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001487 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001488 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001489 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001490
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001491 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001492 if (Model != "global-dynamic" && Model != "local-dynamic"
1493 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001494 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001495 return;
1496 }
1497
Michael Han99315932013-01-24 16:46:58 +00001498 D->addAttr(::new (S.Context)
1499 TLSModelAttr(Attr.getRange(), S.Context, Model,
1500 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001501}
1502
Chandler Carruthedc2c642011-07-02 00:01:44 +00001503static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001504 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001505 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001506 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001507 D->addAttr(::new (S.Context)
1508 MallocAttr(Attr.getRange(), S.Context,
1509 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001510 return;
1511 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001512 }
1513
Ted Kremenek08479ae2009-08-15 00:51:46 +00001514 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001515}
1516
Chandler Carruthedc2c642011-07-02 00:01:44 +00001517static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001518 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001519 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1520 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001521 return;
1522 }
1523
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001524 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1525 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001526}
1527
Chandler Carruthedc2c642011-07-02 00:01:44 +00001528static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001529 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001530
1531 if (S.CheckNoReturnAttr(attr)) return;
1532
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001533 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001534 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001535 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001536 return;
1537 }
1538
Michael Han99315932013-01-24 16:46:58 +00001539 D->addAttr(::new (S.Context)
1540 NoReturnAttr(attr.getRange(), S.Context,
1541 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001542}
1543
1544bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001545 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001546 attr.setInvalid();
1547 return true;
1548 }
1549
1550 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001551}
1552
Chandler Carruthedc2c642011-07-02 00:01:44 +00001553static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1554 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001555
1556 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1557 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001558 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1559 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001560 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1561 && !VD->getType()->isFunctionPointerType())) {
1562 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001563 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001564 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001565 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001566 return;
1567 }
1568 }
1569
Michael Han99315932013-01-24 16:46:58 +00001570 D->addAttr(::new (S.Context)
1571 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1572 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001573}
1574
John Thompsoncdb847ba2010-08-09 21:53:52 +00001575// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001576static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001577/*
1578 Returning a Vector Class in Registers
1579
Eric Christopherbc638a82010-12-01 22:13:54 +00001580 According to the PPU ABI specifications, a class with a single member of
1581 vector type is returned in memory when used as the return value of a function.
1582 This results in inefficient code when implementing vector classes. To return
1583 the value in a single vector register, add the vecreturn attribute to the
1584 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001585
1586 Example:
1587
1588 struct Vector
1589 {
1590 __vector float xyzw;
1591 } __attribute__((vecreturn));
1592
1593 Vector Add(Vector lhs, Vector rhs)
1594 {
1595 Vector result;
1596 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1597 return result; // This will be returned in a register
1598 }
1599*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001600 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001601 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1602 return;
1603 }
1604
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001605 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001606 int count = 0;
1607
1608 if (!isa<CXXRecordDecl>(record)) {
1609 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1610 return;
1611 }
1612
1613 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1614 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1615 return;
1616 }
1617
Eric Christopherbc638a82010-12-01 22:13:54 +00001618 for (RecordDecl::field_iterator iter = record->field_begin();
1619 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001620 if ((count == 1) || !iter->getType()->isVectorType()) {
1621 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1622 return;
1623 }
1624 count++;
1625 }
1626
Michael Han99315932013-01-24 16:46:58 +00001627 D->addAttr(::new (S.Context)
1628 VecReturnAttr(Attr.getRange(), S.Context,
1629 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001630}
1631
Richard Smithe233fbf2013-01-28 22:42:45 +00001632static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1633 const AttributeList &Attr) {
1634 if (isa<ParmVarDecl>(D)) {
1635 // [[carries_dependency]] can only be applied to a parameter if it is a
1636 // parameter of a function declaration or lambda.
1637 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1638 S.Diag(Attr.getLoc(),
1639 diag::err_carries_dependency_param_not_function_decl);
1640 return;
1641 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001642 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001643
1644 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1645 Attr.getRange(), S.Context,
1646 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001647}
1648
Chandler Carruthedc2c642011-07-02 00:01:44 +00001649static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001650 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00001651 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001652 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001653 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001654 return;
1655 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001656
Michael Han99315932013-01-24 16:46:58 +00001657 D->addAttr(::new (S.Context)
1658 UnusedAttr(Attr.getRange(), S.Context,
1659 Attr.getAttributeSpellingListIndex()));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001660}
1661
Chandler Carruthedc2c642011-07-02 00:01:44 +00001662static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001663 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001664 if (VD->hasLocalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001665 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1666 return;
1667 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001668 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001669 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001670 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001671 return;
1672 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001673
Michael Han99315932013-01-24 16:46:58 +00001674 D->addAttr(::new (S.Context)
1675 UsedAttr(Attr.getRange(), S.Context,
1676 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001677}
1678
Chandler Carruthedc2c642011-07-02 00:01:44 +00001679static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001680 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001681 if (Attr.getNumArgs() > 1) {
1682 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001683 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001684 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001685
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001686 uint32_t priority = ConstructorAttr::DefaultPriority;
1687 if (Attr.getNumArgs() > 0 &&
1688 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1689 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001690
Michael Han99315932013-01-24 16:46:58 +00001691 D->addAttr(::new (S.Context)
1692 ConstructorAttr(Attr.getRange(), S.Context, priority,
1693 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001694}
1695
Chandler Carruthedc2c642011-07-02 00:01:44 +00001696static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001697 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001698 if (Attr.getNumArgs() > 1) {
1699 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001700 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001701 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001702
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001703 uint32_t priority = ConstructorAttr::DefaultPriority;
1704 if (Attr.getNumArgs() > 0 &&
1705 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1706 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001707
Michael Han99315932013-01-24 16:46:58 +00001708 D->addAttr(::new (S.Context)
1709 DestructorAttr(Attr.getRange(), S.Context, priority,
1710 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001711}
1712
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001713template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001714static void handleAttrWithMessage(Sema &S, Decl *D,
1715 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001716 unsigned NumArgs = Attr.getNumArgs();
1717 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001718 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001719 return;
1720 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001721
1722 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001723 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001724 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001725 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001726
Michael Han99315932013-01-24 16:46:58 +00001727 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1728 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001729}
1730
Ted Kremenek28eace62013-11-23 01:01:34 +00001731static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
1732 const AttributeList &Attr) {
Ted Kremenek7559b472013-11-23 22:29:11 +00001733 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek28eace62013-11-23 01:01:34 +00001734
1735 if (!Parm) {
1736 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 1;
1737 return;
1738 }
1739
1740 D->addAttr(::new (S.Context)
1741 ObjCSuppressProtocolAttr(Attr.getRange(), S.Context, Parm->Ident,
1742 Attr.getAttributeSpellingListIndex()));
1743}
1744
Jordy Rose740b0c22012-05-08 03:27:22 +00001745static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1746 IdentifierInfo *Platform,
1747 VersionTuple Introduced,
1748 VersionTuple Deprecated,
1749 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001750 StringRef PlatformName
1751 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1752 if (PlatformName.empty())
1753 PlatformName = Platform->getName();
1754
1755 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1756 // of these steps are needed).
1757 if (!Introduced.empty() && !Deprecated.empty() &&
1758 !(Introduced <= Deprecated)) {
1759 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1760 << 1 << PlatformName << Deprecated.getAsString()
1761 << 0 << Introduced.getAsString();
1762 return true;
1763 }
1764
1765 if (!Introduced.empty() && !Obsoleted.empty() &&
1766 !(Introduced <= Obsoleted)) {
1767 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1768 << 2 << PlatformName << Obsoleted.getAsString()
1769 << 0 << Introduced.getAsString();
1770 return true;
1771 }
1772
1773 if (!Deprecated.empty() && !Obsoleted.empty() &&
1774 !(Deprecated <= Obsoleted)) {
1775 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1776 << 2 << PlatformName << Obsoleted.getAsString()
1777 << 1 << Deprecated.getAsString();
1778 return true;
1779 }
1780
1781 return false;
1782}
1783
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001784/// \brief Check whether the two versions match.
1785///
1786/// If either version tuple is empty, then they are assumed to match. If
1787/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1788static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1789 bool BeforeIsOkay) {
1790 if (X.empty() || Y.empty())
1791 return true;
1792
1793 if (X == Y)
1794 return true;
1795
1796 if (BeforeIsOkay && X < Y)
1797 return true;
1798
1799 return false;
1800}
1801
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001802AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001803 IdentifierInfo *Platform,
1804 VersionTuple Introduced,
1805 VersionTuple Deprecated,
1806 VersionTuple Obsoleted,
1807 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001808 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001809 bool Override,
1810 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001811 VersionTuple MergedIntroduced = Introduced;
1812 VersionTuple MergedDeprecated = Deprecated;
1813 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001814 bool FoundAny = false;
1815
Rafael Espindolac67f2232012-05-10 02:50:16 +00001816 if (D->hasAttrs()) {
1817 AttrVec &Attrs = D->getAttrs();
1818 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1819 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1820 if (!OldAA) {
1821 ++i;
1822 continue;
1823 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001824
Rafael Espindolac67f2232012-05-10 02:50:16 +00001825 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1826 if (OldPlatform != Platform) {
1827 ++i;
1828 continue;
1829 }
1830
1831 FoundAny = true;
1832 VersionTuple OldIntroduced = OldAA->getIntroduced();
1833 VersionTuple OldDeprecated = OldAA->getDeprecated();
1834 VersionTuple OldObsoleted = OldAA->getObsoleted();
1835 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001836
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001837 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1838 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1839 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1840 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001841 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001842 if (Override) {
1843 int Which = -1;
1844 VersionTuple FirstVersion;
1845 VersionTuple SecondVersion;
1846 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1847 Which = 0;
1848 FirstVersion = OldIntroduced;
1849 SecondVersion = Introduced;
1850 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1851 Which = 1;
1852 FirstVersion = Deprecated;
1853 SecondVersion = OldDeprecated;
1854 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1855 Which = 2;
1856 FirstVersion = Obsoleted;
1857 SecondVersion = OldObsoleted;
1858 }
1859
1860 if (Which == -1) {
1861 Diag(OldAA->getLocation(),
1862 diag::warn_mismatched_availability_override_unavail)
1863 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1864 } else {
1865 Diag(OldAA->getLocation(),
1866 diag::warn_mismatched_availability_override)
1867 << Which
1868 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1869 << FirstVersion.getAsString() << SecondVersion.getAsString();
1870 }
1871 Diag(Range.getBegin(), diag::note_overridden_method);
1872 } else {
1873 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1874 Diag(Range.getBegin(), diag::note_previous_attribute);
1875 }
1876
Rafael Espindolac67f2232012-05-10 02:50:16 +00001877 Attrs.erase(Attrs.begin() + i);
1878 --e;
1879 continue;
1880 }
1881
1882 VersionTuple MergedIntroduced2 = MergedIntroduced;
1883 VersionTuple MergedDeprecated2 = MergedDeprecated;
1884 VersionTuple MergedObsoleted2 = MergedObsoleted;
1885
1886 if (MergedIntroduced2.empty())
1887 MergedIntroduced2 = OldIntroduced;
1888 if (MergedDeprecated2.empty())
1889 MergedDeprecated2 = OldDeprecated;
1890 if (MergedObsoleted2.empty())
1891 MergedObsoleted2 = OldObsoleted;
1892
1893 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1894 MergedIntroduced2, MergedDeprecated2,
1895 MergedObsoleted2)) {
1896 Attrs.erase(Attrs.begin() + i);
1897 --e;
1898 continue;
1899 }
1900
1901 MergedIntroduced = MergedIntroduced2;
1902 MergedDeprecated = MergedDeprecated2;
1903 MergedObsoleted = MergedObsoleted2;
1904 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001905 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001906 }
1907
1908 if (FoundAny &&
1909 MergedIntroduced == Introduced &&
1910 MergedDeprecated == Deprecated &&
1911 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001912 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001913
Ted Kremenekb5445722013-04-06 00:34:27 +00001914 // Only create a new attribute if !Override, but we want to do
1915 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001916 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001917 MergedDeprecated, MergedObsoleted) &&
1918 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001919 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1920 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001921 Obsoleted, IsUnavailable, Message,
1922 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001923 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001924 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001925}
1926
Chandler Carruthedc2c642011-07-02 00:01:44 +00001927static void handleAvailabilityAttr(Sema &S, Decl *D,
1928 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001929 if (!checkAttributeNumArgs(S, Attr, 1))
1930 return;
1931 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001932 unsigned Index = Attr.getAttributeSpellingListIndex();
1933
Aaron Ballman00e99962013-08-31 01:11:41 +00001934 IdentifierInfo *II = Platform->Ident;
1935 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1936 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1937 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001938
Rafael Espindolac231fab2013-01-08 21:30:32 +00001939 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1940 if (!ND) {
1941 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1942 return;
1943 }
1944
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001945 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1946 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1947 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001948 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001949 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001950 if (const StringLiteral *SE =
1951 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001952 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001953
Aaron Ballman00e99962013-08-31 01:11:41 +00001954 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001955 Introduced.Version,
1956 Deprecated.Version,
1957 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001958 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001959 /*Override=*/false,
1960 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001961 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001962 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001963}
1964
John McCalld041a9b2013-02-20 01:54:26 +00001965template <class T>
1966static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1967 typename T::VisibilityType value,
1968 unsigned attrSpellingListIndex) {
1969 T *existingAttr = D->getAttr<T>();
1970 if (existingAttr) {
1971 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1972 if (existingValue == value)
1973 return NULL;
1974 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1975 S.Diag(range.getBegin(), diag::note_previous_attribute);
1976 D->dropAttr<T>();
1977 }
1978 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1979}
1980
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001981VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001982 VisibilityAttr::VisibilityType Vis,
1983 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001984 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1985 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001986}
1987
John McCalld041a9b2013-02-20 01:54:26 +00001988TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1989 TypeVisibilityAttr::VisibilityType Vis,
1990 unsigned AttrSpellingListIndex) {
1991 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1992 AttrSpellingListIndex);
1993}
1994
1995static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1996 bool isTypeVisibility) {
1997 // Visibility attributes don't mean anything on a typedef.
1998 if (isa<TypedefNameDecl>(D)) {
1999 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2000 << Attr.getName();
2001 return;
2002 }
2003
2004 // 'type_visibility' can only go on a type or namespace.
2005 if (isTypeVisibility &&
2006 !(isa<TagDecl>(D) ||
2007 isa<ObjCInterfaceDecl>(D) ||
2008 isa<NamespaceDecl>(D))) {
2009 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2010 << Attr.getName() << ExpectedTypeOrNamespace;
2011 return;
2012 }
2013
Benjamin Kramer70370212013-09-09 15:08:57 +00002014 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002015 StringRef TypeStr;
2016 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002017 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002018 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002019
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002020 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002021 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002022 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002023 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002024 return;
2025 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002026
2027 // Complain about attempts to use protected visibility on targets
2028 // (like Darwin) that don't support it.
2029 if (type == VisibilityAttr::Protected &&
2030 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2031 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2032 type = VisibilityAttr::Default;
2033 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002034
Michael Han99315932013-01-24 16:46:58 +00002035 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002036 clang::Attr *newAttr;
2037 if (isTypeVisibility) {
2038 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2039 (TypeVisibilityAttr::VisibilityType) type,
2040 Index);
2041 } else {
2042 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2043 }
2044 if (newAttr)
2045 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002046}
2047
Chandler Carruthedc2c642011-07-02 00:01:44 +00002048static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2049 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002050 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002051 if (!Attr.isArgIdent(0)) {
2052 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2053 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002054 return;
2055 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002056
Aaron Ballman682ee422013-09-11 19:47:58 +00002057 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2058 ObjCMethodFamilyAttr::FamilyKind F;
2059 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2060 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2061 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002062 return;
2063 }
2064
Aaron Ballman682ee422013-09-11 19:47:58 +00002065 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCall31168b02011-06-15 23:02:42 +00002066 !method->getResultType()->isObjCObjectPointerType()) {
2067 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2068 << method->getResultType();
2069 // Ignore the attribute.
2070 return;
2071 }
2072
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002073 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman682ee422013-09-11 19:47:58 +00002074 S.Context, F));
John McCall86bc21f2011-03-02 11:33:24 +00002075}
2076
Chandler Carruthedc2c642011-07-02 00:01:44 +00002077static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002078 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002079 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002080 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002081 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2082 return;
2083 }
2084 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002085 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2086 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002087 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002088 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2089 return;
2090 }
2091 }
2092 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002093 // It is okay to include this attribute on properties, e.g.:
2094 //
2095 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2096 //
2097 // In this case it follows tradition and suppresses an error in the above
2098 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002099 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002100 }
Michael Han99315932013-01-24 16:46:58 +00002101 D->addAttr(::new (S.Context)
2102 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2103 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002104}
2105
Chandler Carruthedc2c642011-07-02 00:01:44 +00002106static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002107 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002108 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002109 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002110 return;
2111 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002112
Aaron Ballman00e99962013-08-31 01:11:41 +00002113 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002114 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002115 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2116 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2117 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002118 return;
2119 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002120
Michael Han99315932013-01-24 16:46:58 +00002121 D->addAttr(::new (S.Context)
2122 BlocksAttr(Attr.getRange(), S.Context, type,
2123 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002124}
2125
Chandler Carruthedc2c642011-07-02 00:01:44 +00002126static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002127 // check the attribute arguments.
2128 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002129 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002130 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002131 }
2132
Aaron Ballman18a78382013-11-21 00:28:23 +00002133 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002134 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002135 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002136 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002137 if (E->isTypeDependent() || E->isValueDependent() ||
2138 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002139 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002140 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002141 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002142 return;
2143 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002144
John McCallb46f2872011-09-09 07:56:05 +00002145 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002146 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2147 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002148 return;
2149 }
John McCallb46f2872011-09-09 07:56:05 +00002150
2151 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002152 }
2153
Aaron Ballman18a78382013-11-21 00:28:23 +00002154 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002155 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002156 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002157 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002158 if (E->isTypeDependent() || E->isValueDependent() ||
2159 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002160 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002161 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002162 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002163 return;
2164 }
2165 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002166
John McCallb46f2872011-09-09 07:56:05 +00002167 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002168 // FIXME: This error message could be improved, it would be nice
2169 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002170 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2171 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002172 return;
2173 }
2174 }
2175
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002176 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002177 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002178 if (isa<FunctionNoProtoType>(FT)) {
2179 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2180 return;
2181 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002182
Chris Lattner9363e312009-03-17 23:03:47 +00002183 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002184 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002185 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002186 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002187 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002188 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002189 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002190 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002191 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002192 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2193 if (!BD->isVariadic()) {
2194 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2195 return;
2196 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002197 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002198 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002199 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002200 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002201 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002202 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002203 int m = Ty->isFunctionPointerType() ? 0 : 1;
2204 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002205 return;
2206 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002207 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002208 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002209 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002210 return;
2211 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002212 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002213 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002214 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002215 return;
2216 }
Michael Han99315932013-01-24 16:46:58 +00002217 D->addAttr(::new (S.Context)
2218 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2219 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002220}
2221
Chandler Carruthedc2c642011-07-02 00:01:44 +00002222static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002223 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002224 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002225 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002226 return;
2227 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002228
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002229 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2230 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2231 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002232 return;
2233 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002234 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2235 if (MD->getResultType()->isVoidType()) {
2236 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2237 << Attr.getName() << 1;
2238 return;
2239 }
2240
Michael Han99315932013-01-24 16:46:58 +00002241 D->addAttr(::new (S.Context)
2242 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2243 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002244}
2245
Chandler Carruthedc2c642011-07-02 00:01:44 +00002246static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002247 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002248 if (isa<CXXRecordDecl>(D)) {
2249 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2250 return;
2251 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002252 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2253 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002254 return;
2255 }
2256
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002257 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002258
Michael Han99315932013-01-24 16:46:58 +00002259 nd->addAttr(::new (S.Context)
2260 WeakAttr(Attr.getRange(), S.Context,
2261 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002262}
2263
Chandler Carruthedc2c642011-07-02 00:01:44 +00002264static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002265 // weak_import only applies to variable & function declarations.
2266 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002267 if (!D->canBeWeakImported(isDef)) {
2268 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002269 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2270 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002271 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002272 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002273 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002274 // Nothing to warn about here.
2275 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002276 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002277 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002278
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002279 return;
2280 }
2281
Michael Han99315932013-01-24 16:46:58 +00002282 D->addAttr(::new (S.Context)
2283 WeakImportAttr(Attr.getRange(), S.Context,
2284 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002285}
2286
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002287// Handles reqd_work_group_size and work_group_size_hint.
2288static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002289 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002290 uint32_t WGSize[3];
2291 for (unsigned i = 0; i < 3; ++i)
2292 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002293 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002294
2295 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2296 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2297 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2298 if (!(A->getXDim() == WGSize[0] &&
2299 A->getYDim() == WGSize[1] &&
2300 A->getZDim() == WGSize[2])) {
2301 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2302 Attr.getName();
2303 }
2304 }
2305
2306 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2307 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2308 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2309 if (!(A->getXDim() == WGSize[0] &&
2310 A->getYDim() == WGSize[1] &&
2311 A->getZDim() == WGSize[2])) {
2312 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2313 Attr.getName();
2314 }
2315 }
2316
2317 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2318 D->addAttr(::new (S.Context)
2319 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002320 WGSize[0], WGSize[1], WGSize[2],
2321 Attr.getAttributeSpellingListIndex()));
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002322 else
2323 D->addAttr(::new (S.Context)
2324 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002325 WGSize[0], WGSize[1], WGSize[2],
2326 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002327}
2328
Joey Goulyaba589c2013-03-08 09:42:32 +00002329static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2330 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2331
Aaron Ballman00e99962013-08-31 01:11:41 +00002332 if (!Attr.hasParsedType()) {
2333 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2334 << Attr.getName() << 1;
2335 return;
2336 }
2337
Richard Smithb87c4652013-10-31 21:23:20 +00002338 TypeSourceInfo *ParmTSI = 0;
2339 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2340 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002341
2342 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2343 (ParmType->isBooleanType() ||
2344 !ParmType->isIntegralType(S.getASTContext()))) {
2345 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2346 << ParmType;
2347 return;
2348 }
2349
2350 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2351 D->hasAttr<VecTypeHintAttr>()) {
2352 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
Richard Smithb87c4652013-10-31 21:23:20 +00002353 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002354 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2355 return;
2356 }
2357 }
2358
2359 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Richard Smithb87c4652013-10-31 21:23:20 +00002360 ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002361}
2362
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002363SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002364 StringRef Name,
2365 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002366 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2367 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002368 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002369 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2370 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002371 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002372 }
Michael Han99315932013-01-24 16:46:58 +00002373 return ::new (Context) SectionAttr(Range, Context, Name,
2374 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002375}
2376
Chandler Carruthedc2c642011-07-02 00:01:44 +00002377static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002378 // Make sure that there is a string literal as the sections's single
2379 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002380 StringRef Str;
2381 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002382 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002383 return;
Mike Stump11289f42009-09-09 15:08:12 +00002384
Chris Lattner30ba6742009-08-10 19:03:04 +00002385 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002386 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002387 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002388 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002389 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002390 return;
2391 }
Mike Stump11289f42009-09-09 15:08:12 +00002392
Chris Lattner20aee9b2010-01-12 20:58:53 +00002393 // This attribute cannot be applied to local variables.
2394 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002395 S.Diag(LiteralLoc, diag::err_attribute_section_local_variable);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002396 return;
2397 }
Michael Han99315932013-01-24 16:46:58 +00002398
2399 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002400 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002401 if (NewAttr)
2402 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002403}
2404
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002405
Chandler Carruthedc2c642011-07-02 00:01:44 +00002406static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002407 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002408 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002409 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002410 } else {
Michael Han99315932013-01-24 16:46:58 +00002411 D->addAttr(::new (S.Context)
2412 NoThrowAttr(Attr.getRange(), S.Context,
2413 Attr.getAttributeSpellingListIndex()));
Douglas Gregor88336832011-06-15 05:45:11 +00002414 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002415}
2416
Chandler Carruthedc2c642011-07-02 00:01:44 +00002417static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002418 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002419 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002420 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002421 } else {
Michael Han99315932013-01-24 16:46:58 +00002422 D->addAttr(::new (S.Context)
2423 ConstAttr(Attr.getRange(), S.Context,
2424 Attr.getAttributeSpellingListIndex() ));
Douglas Gregor88336832011-06-15 05:45:11 +00002425 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002426}
2427
Chandler Carruthedc2c642011-07-02 00:01:44 +00002428static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002429 VarDecl *VD = cast<VarDecl>(D);
2430 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002431 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002432 return;
2433 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002434
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002435 Expr *E = Attr.getArgAsExpr(0);
2436 SourceLocation Loc = E->getExprLoc();
2437 FunctionDecl *FD = 0;
2438 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002439
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002440 // gcc only allows for simple identifiers. Since we support more than gcc, we
2441 // will warn the user.
2442 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2443 if (DRE->hasQualifier())
2444 S.Diag(Loc, diag::warn_cleanup_ext);
2445 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2446 NI = DRE->getNameInfo();
2447 if (!FD) {
2448 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2449 << NI.getName();
2450 return;
2451 }
2452 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2453 if (ULE->hasExplicitTemplateArgs())
2454 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002455 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2456 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002457 if (!FD) {
2458 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2459 << NI.getName();
2460 if (ULE->getType() == S.Context.OverloadTy)
2461 S.NoteAllOverloadCandidates(ULE);
2462 return;
2463 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002464 } else {
2465 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002466 return;
2467 }
2468
Anders Carlssond277d792009-01-31 01:16:18 +00002469 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002470 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2471 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002472 return;
2473 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002474
Anders Carlsson723f55d2009-02-07 23:16:50 +00002475 // We're currently more strict than GCC about what function types we accept.
2476 // If this ever proves to be a problem it should be easy to fix.
2477 QualType Ty = S.Context.getPointerType(VD->getType());
2478 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002479 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2480 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002481 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2482 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002483 return;
2484 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002485
Michael Han99315932013-01-24 16:46:58 +00002486 D->addAttr(::new (S.Context)
2487 CleanupAttr(Attr.getRange(), S.Context, FD,
2488 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002489}
2490
Mike Stumpd3bb5572009-07-24 19:02:52 +00002491/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002492/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002493static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002494 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002495 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002496 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002497 return;
2498 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002499
Aaron Ballman00e99962013-08-31 01:11:41 +00002500 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002501 uint64_t ArgIdx;
2502 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2503 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002504 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002505
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002506 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002507 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002508
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002509 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2510 if (not_nsstring_type &&
2511 !isCFStringType(Ty, S.Context) &&
2512 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002513 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002514 // FIXME: Should highlight the actual expression that has the wrong type.
2515 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002516 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002517 << IdxExpr->getSourceRange();
2518 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002519 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002520 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002521 if (!isNSStringType(Ty, S.Context) &&
2522 !isCFStringType(Ty, S.Context) &&
2523 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002524 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002525 // FIXME: Should highlight the actual expression that has the wrong type.
2526 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002527 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002528 << IdxExpr->getSourceRange();
2529 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002530 }
2531
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002532 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2533 // because that has corrected for the implicit this parameter, and is zero-
2534 // based. The attribute expects what the user wrote explicitly.
2535 llvm::APSInt Val;
2536 IdxExpr->EvaluateAsInt(Val, S.Context);
2537
Michael Han99315932013-01-24 16:46:58 +00002538 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002539 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002540 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002541}
2542
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002543enum FormatAttrKind {
2544 CFStringFormat,
2545 NSStringFormat,
2546 StrftimeFormat,
2547 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002548 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002549 InvalidFormat
2550};
2551
2552/// getFormatAttrKind - Map from format attribute names to supported format
2553/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002554static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002555 return llvm::StringSwitch<FormatAttrKind>(Format)
2556 // Check for formats that get handled specially.
2557 .Case("NSString", NSStringFormat)
2558 .Case("CFString", CFStringFormat)
2559 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002560
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002561 // Otherwise, check for supported formats.
2562 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2563 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2564 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002565
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002566 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2567 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002568}
2569
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002570/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002571/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002572static void handleInitPriorityAttr(Sema &S, Decl *D,
2573 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002574 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002575 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2576 return;
2577 }
2578
Aaron Ballman4a611152013-11-27 16:34:09 +00002579 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002580 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2581 Attr.setInvalid();
2582 return;
2583 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002584 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002585 if (S.Context.getAsArrayType(T))
2586 T = S.Context.getBaseElementType(T);
2587 if (!T->getAs<RecordType>()) {
2588 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2589 Attr.setInvalid();
2590 return;
2591 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002592
2593 Expr *E = Attr.getArgAsExpr(0);
2594 uint32_t prioritynum;
2595 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002596 Attr.setInvalid();
2597 return;
2598 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002599
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002600 if (prioritynum < 101 || prioritynum > 65535) {
2601 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002602 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002603 Attr.setInvalid();
2604 return;
2605 }
Michael Han99315932013-01-24 16:46:58 +00002606 D->addAttr(::new (S.Context)
2607 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2608 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002609}
2610
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002611FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2612 IdentifierInfo *Format, int FormatIdx,
2613 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002614 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002615 // Check whether we already have an equivalent format attribute.
2616 for (specific_attr_iterator<FormatAttr>
2617 i = D->specific_attr_begin<FormatAttr>(),
2618 e = D->specific_attr_end<FormatAttr>();
2619 i != e ; ++i) {
2620 FormatAttr *f = *i;
2621 if (f->getType() == Format &&
2622 f->getFormatIdx() == FormatIdx &&
2623 f->getFirstArg() == FirstArg) {
2624 // If we don't have a valid location for this attribute, adopt the
2625 // location.
2626 if (f->getLocation().isInvalid())
2627 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002628 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002629 }
2630 }
2631
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002632 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2633 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002634}
2635
Mike Stumpd3bb5572009-07-24 19:02:52 +00002636/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002637/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002638static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002639 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002640 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002641 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002642 return;
2643 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002644
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002645 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002646 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002647 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002648 return;
2649 }
2650
Chandler Carruth743682b2010-11-16 08:35:43 +00002651 // In C++ the implicit 'this' function parameter also counts, and they are
2652 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002653 bool HasImplicitThisParam = isInstanceMethod(D);
2654 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002655
Aaron Ballman00e99962013-08-31 01:11:41 +00002656 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2657 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002658
2659 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002660 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002661 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002662 // If we've modified the string name, we need a new identifier for it.
2663 II = &S.Context.Idents.get(Format);
2664 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002665
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002666 // Check for supported formats.
2667 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002668
2669 if (Kind == IgnoredFormat)
2670 return;
2671
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002672 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002673 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman00e99962013-08-31 01:11:41 +00002674 << "format" << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002675 return;
2676 }
2677
2678 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002679 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002680 uint32_t Idx;
2681 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002682 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002683
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002684 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002685 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002686 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002687 return;
2688 }
2689
2690 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002691 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002692
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002693 if (HasImplicitThisParam) {
2694 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002695 S.Diag(Attr.getLoc(),
2696 diag::err_format_attribute_implicit_this_format_string)
2697 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002698 return;
2699 }
2700 ArgIdx--;
2701 }
Mike Stump11289f42009-09-09 15:08:12 +00002702
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002703 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002704 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002705
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002706 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002707 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002708 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2709 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002710 return;
2711 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002712 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002713 // FIXME: do we need to check if the type is NSString*? What are the
2714 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002715 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002716 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002717 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2718 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002719 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002720 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002721 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002722 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002723 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002724 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2725 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002726 return;
2727 }
2728
2729 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002730 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002731 uint32_t FirstArg;
2732 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002733 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002734
2735 // check if the function is variadic if the 3rd argument non-zero
2736 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002737 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002738 ++NumArgs; // +1 for ...
2739 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002740 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002741 return;
2742 }
2743 }
2744
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002745 // strftime requires FirstArg to be 0 because it doesn't read from any
2746 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002747 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002748 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002749 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2750 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002751 return;
2752 }
2753 // if 0 it disables parameter checking (to use with e.g. va_list)
2754 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002755 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002756 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002757 return;
2758 }
2759
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002760 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002761 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002762 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002763 if (NewAttr)
2764 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002765}
2766
Chandler Carruthedc2c642011-07-02 00:01:44 +00002767static void handleTransparentUnionAttr(Sema &S, Decl *D,
2768 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002769 // Try to find the underlying union declaration.
2770 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002771 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002772 if (TD && TD->getUnderlyingType()->isUnionType())
2773 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2774 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002775 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002776
2777 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002778 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002779 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002780 return;
2781 }
2782
John McCallf937c022011-10-07 06:10:15 +00002783 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002784 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002785 diag::warn_transparent_union_attribute_not_definition);
2786 return;
2787 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002788
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002789 RecordDecl::field_iterator Field = RD->field_begin(),
2790 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002791 if (Field == FieldEnd) {
2792 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2793 return;
2794 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002795
David Blaikie40ed2972012-06-06 20:45:41 +00002796 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002797 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002798 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002799 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002800 diag::warn_transparent_union_attribute_floating)
2801 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002802 return;
2803 }
2804
2805 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2806 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2807 for (; Field != FieldEnd; ++Field) {
2808 QualType FieldType = Field->getType();
2809 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2810 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2811 // Warn if we drop the attribute.
2812 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002813 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002814 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002815 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002816 diag::warn_transparent_union_attribute_field_size_align)
2817 << isSize << Field->getDeclName() << FieldBits;
2818 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002819 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002820 diag::note_transparent_union_first_field_size_align)
2821 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002822 return;
2823 }
2824 }
2825
Michael Han99315932013-01-24 16:46:58 +00002826 RD->addAttr(::new (S.Context)
2827 TransparentUnionAttr(Attr.getRange(), S.Context,
2828 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002829}
2830
Chandler Carruthedc2c642011-07-02 00:01:44 +00002831static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002832 // Make sure that there is a string literal as the annotation's single
2833 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002834 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002835 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002836 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002837
2838 // Don't duplicate annotations that are already set.
2839 for (specific_attr_iterator<AnnotateAttr>
2840 i = D->specific_attr_begin<AnnotateAttr>(),
2841 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002842 if ((*i)->getAnnotation() == Str)
2843 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002844 }
Michael Han99315932013-01-24 16:46:58 +00002845
2846 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002847 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002848 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002849}
2850
Chandler Carruthedc2c642011-07-02 00:01:44 +00002851static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002852 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002853 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002854 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2855 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002856 return;
2857 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002858
Richard Smith848e1f12013-02-01 08:12:08 +00002859 if (Attr.getNumArgs() == 0) {
2860 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2861 true, 0, Attr.getAttributeSpellingListIndex()));
2862 return;
2863 }
2864
Aaron Ballman00e99962013-08-31 01:11:41 +00002865 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002866 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2867 S.Diag(Attr.getEllipsisLoc(),
2868 diag::err_pack_expansion_without_parameter_packs);
2869 return;
2870 }
2871
2872 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2873 return;
2874
2875 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2876 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002877}
2878
2879void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002880 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002881 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2882 SourceLocation AttrLoc = AttrRange.getBegin();
2883
Richard Smith1dba27c2013-01-29 09:02:09 +00002884 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002885 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002886 // C++11 [dcl.align]p1:
2887 // An alignment-specifier may be applied to a variable or to a class
2888 // data member, but it shall not be applied to a bit-field, a function
2889 // parameter, the formal parameter of a catch clause, or a variable
2890 // declared with the register storage class specifier. An
2891 // alignment-specifier may also be applied to the declaration of a class
2892 // or enumeration type.
2893 // C11 6.7.5/2:
2894 // An alignment attribute shall not be specified in a declaration of
2895 // a typedef, or a bit-field, or a function, or a parameter, or an
2896 // object declared with the register storage-class specifier.
2897 int DiagKind = -1;
2898 if (isa<ParmVarDecl>(D)) {
2899 DiagKind = 0;
2900 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2901 if (VD->getStorageClass() == SC_Register)
2902 DiagKind = 1;
2903 if (VD->isExceptionVariable())
2904 DiagKind = 2;
2905 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2906 if (FD->isBitField())
2907 DiagKind = 3;
2908 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00002909 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
2910 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00002911 << (TmpAttr.isC11() ? ExpectedVariableOrField
2912 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002913 return;
2914 }
2915 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002916 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smithbc8caaf2013-02-22 04:55:39 +00002917 << TmpAttr.isC11() << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002918 return;
2919 }
2920 }
2921
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002922 if (E->isTypeDependent() || E->isValueDependent()) {
2923 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002924 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2925 AA->setPackExpansion(IsPackExpansion);
2926 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002927 return;
2928 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002929
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002930 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002931 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002932 ExprResult ICE
2933 = VerifyIntegerConstantExpression(E, &Alignment,
2934 diag::err_aligned_attribute_argument_not_int,
2935 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002936 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002937 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002938
2939 // C++11 [dcl.align]p2:
2940 // -- if the constant expression evaluates to zero, the alignment
2941 // specifier shall have no effect
2942 // C11 6.7.5p6:
2943 // An alignment specification of zero has no effect.
2944 if (!(TmpAttr.isAlignas() && !Alignment) &&
2945 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002946 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2947 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002948 return;
2949 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002950
Richard Smith848e1f12013-02-01 08:12:08 +00002951 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00002952 // We've already verified it's a power of 2, now let's make sure it's
2953 // 8192 or less.
2954 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00002955 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00002956 << E->getSourceRange();
2957 return;
2958 }
2959 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002960
Richard Smith44c247f2013-02-22 08:32:16 +00002961 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
2962 ICE.take(), SpellingListIndex);
2963 AA->setPackExpansion(IsPackExpansion);
2964 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002965}
2966
Michael Hanaf02bbe2013-02-01 01:19:17 +00002967void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002968 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002969 // FIXME: Cache the number on the Attr object if non-dependent?
2970 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002971 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2972 SpellingListIndex);
2973 AA->setPackExpansion(IsPackExpansion);
2974 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002975}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002976
Richard Smith848e1f12013-02-01 08:12:08 +00002977void Sema::CheckAlignasUnderalignment(Decl *D) {
2978 assert(D->hasAttrs() && "no attributes on decl");
2979
2980 QualType Ty;
2981 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2982 Ty = VD->getType();
2983 else
2984 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002985 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002986 return;
2987
2988 // C++11 [dcl.align]p5, C11 6.7.5/4:
2989 // The combined effect of all alignment attributes in a declaration shall
2990 // not specify an alignment that is less strict than the alignment that
2991 // would otherwise be required for the entity being declared.
2992 AlignedAttr *AlignasAttr = 0;
2993 unsigned Align = 0;
2994 for (specific_attr_iterator<AlignedAttr>
2995 I = D->specific_attr_begin<AlignedAttr>(),
2996 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
2997 if (I->isAlignmentDependent())
2998 return;
2999 if (I->isAlignas())
3000 AlignasAttr = *I;
3001 Align = std::max(Align, I->getAlignment(Context));
3002 }
3003
3004 if (AlignasAttr && Align) {
3005 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3006 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3007 if (NaturalAlign > RequestedAlign)
3008 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3009 << Ty << (unsigned)NaturalAlign.getQuantity();
3010 }
3011}
3012
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003013/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003014/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003015///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003016/// Despite what would be logical, the mode attribute is a decl attribute, not a
3017/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3018/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003019static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003020 // This attribute isn't documented, but glibc uses it. It changes
3021 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00003022 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00003023 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3024 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003025 return;
3026 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003027
Aaron Ballman00e99962013-08-31 01:11:41 +00003028 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3029 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003030
3031 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003032 if (Str.startswith("__") && Str.endswith("__"))
3033 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003034
3035 unsigned DestWidth = 0;
3036 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003037 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003038 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003039 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003040 switch (Str[0]) {
3041 case 'Q': DestWidth = 8; break;
3042 case 'H': DestWidth = 16; break;
3043 case 'S': DestWidth = 32; break;
3044 case 'D': DestWidth = 64; break;
3045 case 'X': DestWidth = 96; break;
3046 case 'T': DestWidth = 128; break;
3047 }
3048 if (Str[1] == 'F') {
3049 IntegerMode = false;
3050 } else if (Str[1] == 'C') {
3051 IntegerMode = false;
3052 ComplexMode = true;
3053 } else if (Str[1] != 'I') {
3054 DestWidth = 0;
3055 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003056 break;
3057 case 4:
3058 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3059 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003060 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003061 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003062 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003063 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003064 break;
3065 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003066 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003067 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003068 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003069 case 11:
3070 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003071 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003072 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003073 }
3074
3075 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003076 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003077 OldTy = TD->getUnderlyingType();
3078 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3079 OldTy = VD->getType();
3080 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003081 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003082 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003083 return;
3084 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003085
John McCall9dd450b2009-09-21 23:43:11 +00003086 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003087 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3088 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003089 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003090 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3091 } else if (ComplexMode) {
3092 if (!OldTy->isComplexType())
3093 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3094 } else {
3095 if (!OldTy->isFloatingType())
3096 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3097 }
3098
Mike Stump87c57ac2009-05-16 07:39:55 +00003099 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3100 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003101 // FIXME: Make sure floating-point mappings are accurate
3102 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003103 if (!DestWidth) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003104 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003105 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003106 }
3107
3108 QualType NewTy;
3109
3110 if (IntegerMode)
3111 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3112 OldTy->isSignedIntegerType());
3113 else
3114 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3115
3116 if (NewTy.isNull()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003117 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003118 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003119 }
3120
Eli Friedman4735374e2009-03-03 06:41:03 +00003121 if (ComplexMode) {
3122 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003123 }
3124
3125 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003126 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3127 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3128 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003129 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003130
3131 D->addAttr(::new (S.Context)
3132 ModeAttr(Attr.getRange(), S.Context, Name,
3133 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003134}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003135
Chandler Carruthedc2c642011-07-02 00:01:44 +00003136static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003137 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3138 if (!VD->hasGlobalStorage())
3139 S.Diag(Attr.getLoc(),
3140 diag::warn_attribute_requires_functions_or_static_globals)
3141 << Attr.getName();
3142 } else if (!isFunctionOrMethod(D)) {
3143 S.Diag(Attr.getLoc(),
3144 diag::warn_attribute_requires_functions_or_static_globals)
3145 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003146 return;
3147 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003148
Michael Han99315932013-01-24 16:46:58 +00003149 D->addAttr(::new (S.Context)
3150 NoDebugAttr(Attr.getRange(), S.Context,
3151 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003152}
3153
Chandler Carruthedc2c642011-07-02 00:01:44 +00003154static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003155 if (S.LangOpts.CUDA) {
Michael Han99315932013-01-24 16:46:58 +00003156 D->addAttr(::new (S.Context)
3157 CUDAConstantAttr(Attr.getRange(), S.Context,
3158 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003159 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003160 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003161 }
3162}
3163
Chandler Carruthedc2c642011-07-02 00:01:44 +00003164static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003165 if (S.LangOpts.CUDA) {
3166 // check the attribute arguments.
3167 if (Attr.getNumArgs() != 0) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003168 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3169 << Attr.getName() << 0;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003170 return;
3171 }
3172
Michael Han99315932013-01-24 16:46:58 +00003173 D->addAttr(::new (S.Context)
3174 CUDADeviceAttr(Attr.getRange(), S.Context,
3175 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003176 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003177 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003178 }
3179}
3180
Chandler Carruthedc2c642011-07-02 00:01:44 +00003181static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003182 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003183 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003184 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00003185 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie6adc78e2013-02-18 22:06:02 +00003186 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003187 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3188 << FD->getType()
David Blaikie6adc78e2013-02-18 22:06:02 +00003189 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003190 "void");
3191 } else {
3192 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3193 << FD->getType();
3194 }
3195 return;
3196 }
3197
Michael Han99315932013-01-24 16:46:58 +00003198 D->addAttr(::new (S.Context)
3199 CUDAGlobalAttr(Attr.getRange(), S.Context,
3200 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003201 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003202 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003203 }
3204}
3205
Chandler Carruthedc2c642011-07-02 00:01:44 +00003206static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003207 if (S.LangOpts.CUDA) {
Michael Han99315932013-01-24 16:46:58 +00003208 D->addAttr(::new (S.Context)
3209 CUDAHostAttr(Attr.getRange(), S.Context,
3210 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003211 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003212 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003213 }
3214}
3215
Chandler Carruthedc2c642011-07-02 00:01:44 +00003216static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003217 if (S.LangOpts.CUDA) {
Michael Han99315932013-01-24 16:46:58 +00003218 D->addAttr(::new (S.Context)
3219 CUDASharedAttr(Attr.getRange(), S.Context,
3220 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003221 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003222 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003223 }
3224}
3225
Chandler Carruthedc2c642011-07-02 00:01:44 +00003226static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003227 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003228 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003229 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003230 return;
3231 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003232
Michael Han99315932013-01-24 16:46:58 +00003233 D->addAttr(::new (S.Context)
3234 GNUInlineAttr(Attr.getRange(), S.Context,
3235 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003236}
3237
Chandler Carruthedc2c642011-07-02 00:01:44 +00003238static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003239 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003240
Aaron Ballman02df2e02012-12-09 17:45:41 +00003241 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003242 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003243 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3244 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003245 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003246 return;
3247
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003248 if (!isa<ObjCMethodDecl>(D)) {
3249 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3250 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003251 return;
3252 }
3253
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003254 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003255 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003256 D->addAttr(::new (S.Context)
3257 FastCallAttr(Attr.getRange(), S.Context,
3258 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003259 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003260 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003261 D->addAttr(::new (S.Context)
3262 StdCallAttr(Attr.getRange(), S.Context,
3263 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003264 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003265 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003266 D->addAttr(::new (S.Context)
3267 ThisCallAttr(Attr.getRange(), S.Context,
3268 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003269 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003270 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003271 D->addAttr(::new (S.Context)
3272 CDeclAttr(Attr.getRange(), S.Context,
3273 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003274 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003275 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003276 D->addAttr(::new (S.Context)
3277 PascalAttr(Attr.getRange(), S.Context,
3278 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003279 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003280 case AttributeList::AT_MSABI:
3281 D->addAttr(::new (S.Context)
3282 MSABIAttr(Attr.getRange(), S.Context,
3283 Attr.getAttributeSpellingListIndex()));
3284 return;
3285 case AttributeList::AT_SysVABI:
3286 D->addAttr(::new (S.Context)
3287 SysVABIAttr(Attr.getRange(), S.Context,
3288 Attr.getAttributeSpellingListIndex()));
3289 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003290 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003291 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003292 switch (CC) {
3293 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003294 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003295 break;
3296 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003297 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003298 break;
3299 default:
3300 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003301 }
3302
Michael Han99315932013-01-24 16:46:58 +00003303 D->addAttr(::new (S.Context)
3304 PcsAttr(Attr.getRange(), S.Context, PCS,
3305 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003306 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003307 }
Derek Schuffa2020962012-10-16 22:30:41 +00003308 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003309 D->addAttr(::new (S.Context)
3310 PnaclCallAttr(Attr.getRange(), S.Context,
3311 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003312 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003313 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003314 D->addAttr(::new (S.Context)
3315 IntelOclBiccAttr(Attr.getRange(), S.Context,
3316 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003317 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003318
Abramo Bagnara50099372010-04-30 13:10:51 +00003319 default:
3320 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003321 }
3322}
3323
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003324static void handleOpenCLImageAccessAttr(Sema &S, Decl *D,
3325 const AttributeList &Attr) {
3326 uint32_t ArgNum;
3327 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), ArgNum))
Guy Benyeifb36ede2013-03-24 13:58:12 +00003328 return;
Guy Benyeifb36ede2013-03-24 13:58:12 +00003329
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003330 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(Attr.getRange(),
3331 S.Context, ArgNum));
Guy Benyeifb36ede2013-03-24 13:58:12 +00003332}
3333
Aaron Ballman02df2e02012-12-09 17:45:41 +00003334bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3335 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003336 if (attr.isInvalid())
3337 return true;
3338
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003339 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003340 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003341 attr.setInvalid();
3342 return true;
3343 }
3344
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003345 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3346 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003347 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003348 case AttributeList::AT_CDecl: CC = CC_C; break;
3349 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3350 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3351 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3352 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003353 case AttributeList::AT_MSABI:
3354 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3355 CC_X86_64Win64;
3356 break;
3357 case AttributeList::AT_SysVABI:
3358 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3359 CC_C;
3360 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003361 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003362 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003363 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003364 attr.setInvalid();
3365 return true;
3366 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003367 if (StrRef == "aapcs") {
3368 CC = CC_AAPCS;
3369 break;
3370 } else if (StrRef == "aapcs-vfp") {
3371 CC = CC_AAPCS_VFP;
3372 break;
3373 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003374
3375 attr.setInvalid();
3376 Diag(attr.getLoc(), diag::err_invalid_pcs);
3377 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003378 }
Derek Schuffa2020962012-10-16 22:30:41 +00003379 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003380 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003381 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003382 }
3383
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003384 const TargetInfo &TI = Context.getTargetInfo();
3385 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3386 if (A == TargetInfo::CCCR_Warning) {
3387 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003388
3389 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3390 if (FD)
3391 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3392 TargetInfo::CCMT_NonMember;
3393 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003394 }
3395
John McCall3882ace2011-01-05 12:14:39 +00003396 return false;
3397}
3398
Chandler Carruthedc2c642011-07-02 00:01:44 +00003399static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003400 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003401
3402 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003403 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003404 return;
3405
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003406 if (!isa<ObjCMethodDecl>(D)) {
3407 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3408 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003409 return;
3410 }
Eli Friedman7044b762009-03-27 21:06:47 +00003411
Michael Han99315932013-01-24 16:46:58 +00003412 D->addAttr(::new (S.Context)
3413 RegparmAttr(Attr.getRange(), S.Context, numParams,
3414 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00003415}
3416
3417/// Checks a regparm attribute, returning true if it is ill-formed and
3418/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003419bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3420 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003421 return true;
3422
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003423 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003424 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003425 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003426 }
Eli Friedman7044b762009-03-27 21:06:47 +00003427
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003428 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003429 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003430 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003431 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003432 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003433 }
3434
Douglas Gregore8bbc122011-09-02 00:18:52 +00003435 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003436 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003437 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003438 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003439 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003440 }
3441
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003442 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003443 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003444 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003445 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003446 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003447 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003448 }
3449
John McCall3882ace2011-01-05 12:14:39 +00003450 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003451}
3452
Chandler Carruthedc2c642011-07-02 00:01:44 +00003453static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00003454 if (S.LangOpts.CUDA) {
3455 // check the attribute arguments.
3456 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00003457 // FIXME: 0 is not okay.
3458 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003459 return;
3460 }
3461
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003462 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00003463 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003464 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003465 return;
3466 }
3467
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003468 uint32_t MaxThreads, MinBlocks;
3469 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1) ||
3470 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(1), MinBlocks, 2))
Peter Collingbourne827301e2010-12-12 23:03:07 +00003471 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003472
Michael Han99315932013-01-24 16:46:58 +00003473 D->addAttr(::new (S.Context)
3474 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003475 MaxThreads, MinBlocks,
Michael Han99315932013-01-24 16:46:58 +00003476 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003477 } else {
3478 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3479 }
3480}
3481
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003482static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3483 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003484 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003485 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003486 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003487 return;
3488 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003489
3490 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003491 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003492
Aaron Ballman00e99962013-08-31 01:11:41 +00003493 StringRef AttrName = Attr.getName()->getName();
3494 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003495
3496 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3497 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3498 << Attr.getName() << ExpectedFunctionOrMethod;
3499 return;
3500 }
3501
3502 uint64_t ArgumentIdx;
3503 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3504 Attr.getLoc(), 2,
Aaron Ballman00e99962013-08-31 01:11:41 +00003505 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003506 return;
3507
3508 uint64_t TypeTagIdx;
3509 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3510 Attr.getLoc(), 3,
Aaron Ballman00e99962013-08-31 01:11:41 +00003511 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003512 return;
3513
3514 bool IsPointer = (AttrName == "pointer_with_type_tag");
3515 if (IsPointer) {
3516 // Ensure that buffer has a pointer type.
3517 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3518 if (!BufferTy->isPointerType()) {
3519 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003520 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003521 }
3522 }
3523
Michael Han99315932013-01-24 16:46:58 +00003524 D->addAttr(::new (S.Context)
3525 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3526 ArgumentIdx, TypeTagIdx, IsPointer,
3527 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003528}
3529
3530static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3531 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003532 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003533 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003534 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003535 return;
3536 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003537
3538 if (!checkAttributeNumArgs(S, Attr, 1))
3539 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003540
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003541 if (!isa<VarDecl>(D)) {
3542 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3543 << Attr.getName() << ExpectedVariable;
3544 return;
3545 }
3546
Aaron Ballman00e99962013-08-31 01:11:41 +00003547 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003548 TypeSourceInfo *MatchingCTypeLoc = 0;
3549 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3550 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003551
Michael Han99315932013-01-24 16:46:58 +00003552 D->addAttr(::new (S.Context)
3553 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003554 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003555 Attr.getLayoutCompatible(),
3556 Attr.getMustBeNull(),
3557 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003558}
3559
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003560//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003561// Checker-specific attribute handlers.
3562//===----------------------------------------------------------------------===//
3563
John McCalled433932011-01-25 03:31:58 +00003564static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003565 return type->isDependentType() ||
3566 type->isObjCObjectPointerType() ||
3567 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003568}
3569static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003570 return type->isDependentType() ||
3571 type->isPointerType() ||
3572 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003573}
3574
Chandler Carruthedc2c642011-07-02 00:01:44 +00003575static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003576 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003577 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003578
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003579 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003580 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3581 cf = false;
3582 } else {
3583 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3584 cf = true;
3585 }
3586
3587 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003588 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003589 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003590 return;
3591 }
3592
3593 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003594 param->addAttr(::new (S.Context)
3595 CFConsumedAttr(Attr.getRange(), S.Context,
3596 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003597 else
Michael Han99315932013-01-24 16:46:58 +00003598 param->addAttr(::new (S.Context)
3599 NSConsumedAttr(Attr.getRange(), S.Context,
3600 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003601}
3602
Chandler Carruthedc2c642011-07-02 00:01:44 +00003603static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3604 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003605
John McCalled433932011-01-25 03:31:58 +00003606 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003607
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003608 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003609 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003610 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003611 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003612 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003613 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3614 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003615 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003616 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003617 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003618 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003619 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003620 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003621 return;
3622 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003623
John McCalled433932011-01-25 03:31:58 +00003624 bool typeOK;
3625 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003626 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003627 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003628 case AttributeList::AT_NSReturnsAutoreleased:
3629 case AttributeList::AT_NSReturnsRetained:
3630 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003631 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3632 cf = false;
3633 break;
3634
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003635 case AttributeList::AT_CFReturnsRetained:
3636 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003637 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3638 cf = true;
3639 break;
3640 }
3641
3642 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003643 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003644 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003645 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003646 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003647
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003648 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003649 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003650 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003651 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003652 D->addAttr(::new (S.Context)
3653 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3654 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003655 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003656 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003657 D->addAttr(::new (S.Context)
3658 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3659 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003660 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003661 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003662 D->addAttr(::new (S.Context)
3663 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3664 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003665 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003666 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003667 D->addAttr(::new (S.Context)
3668 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3669 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003670 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003671 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003672 D->addAttr(::new (S.Context)
3673 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3674 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003675 return;
3676 };
3677}
3678
John McCallcf166702011-07-22 08:53:00 +00003679static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3680 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003681 const int EP_ObjCMethod = 1;
3682 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003683
John McCallcf166702011-07-22 08:53:00 +00003684 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003685 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003686 if (isa<ObjCMethodDecl>(D))
3687 resultType = cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003688 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003689 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003690
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003691 if (!resultType->isReferenceType() &&
3692 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003693 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003694 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003695 << attr.getName()
3696 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003697 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003698
3699 // Drop the attribute.
3700 return;
3701 }
3702
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003703 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003704 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3705 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003706}
3707
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003708static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3709 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003710 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003711
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003712 DeclContext *DC = method->getDeclContext();
3713 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3714 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3715 << attr.getName() << 0;
3716 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3717 return;
3718 }
3719 if (method->getMethodFamily() == OMF_dealloc) {
3720 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3721 << attr.getName() << 1;
3722 return;
3723 }
3724
Michael Han99315932013-01-24 16:46:58 +00003725 method->addAttr(::new (S.Context)
3726 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3727 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003728}
3729
John McCall32f5fe12011-09-30 05:12:12 +00003730/// Handle cf_audited_transfer and cf_unknown_transfer.
3731static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003732 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall32f5fe12011-09-30 05:12:12 +00003733
3734 // Check whether there's a conflicting attribute already present.
3735 Attr *Existing;
3736 if (IsAudited) {
3737 Existing = D->getAttr<CFUnknownTransferAttr>();
3738 } else {
3739 Existing = D->getAttr<CFAuditedTransferAttr>();
3740 }
3741 if (Existing) {
3742 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
3743 << A.getName()
3744 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
3745 << A.getRange() << Existing->getRange();
3746 return;
3747 }
3748
3749 // All clear; add the attribute.
3750 if (IsAudited) {
Michael Han99315932013-01-24 16:46:58 +00003751 D->addAttr(::new (S.Context)
3752 CFAuditedTransferAttr(A.getRange(), S.Context,
3753 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003754 } else {
Michael Han99315932013-01-24 16:46:58 +00003755 D->addAttr(::new (S.Context)
3756 CFUnknownTransferAttr(A.getRange(), S.Context,
3757 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003758 }
3759}
3760
John McCallf1e8b342011-09-29 07:17:38 +00003761static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
3762 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003763 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallf1e8b342011-09-29 07:17:38 +00003764
3765 // In Objective-C, verify that the type names an Objective-C type.
3766 // We don't want to check this outside of ObjC because people sometimes
3767 // do crazy C declarations of Objective-C types.
Aaron Ballman00e99962013-08-31 01:11:41 +00003768 if (Parm && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00003769 // Check for an existing type with this name.
Aaron Ballman00e99962013-08-31 01:11:41 +00003770 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallf1e8b342011-09-29 07:17:38 +00003771 Sema::LookupOrdinaryName);
3772 if (S.LookupName(R, Sc)) {
3773 NamedDecl *Target = R.getFoundDecl();
3774 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
3775 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
3776 S.Diag(Target->getLocStart(), diag::note_declared_at);
3777 }
3778 }
3779 }
3780
Michael Han99315932013-01-24 16:46:58 +00003781 D->addAttr(::new (S.Context)
Aaron Ballman00e99962013-08-31 01:11:41 +00003782 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han99315932013-01-24 16:46:58 +00003783 Attr.getAttributeSpellingListIndex()));
John McCallf1e8b342011-09-29 07:17:38 +00003784}
3785
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003786static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3787 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003788 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003789
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003790 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003791 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003792 return;
3793 }
3794
3795 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003796 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003797 Attr.getAttributeSpellingListIndex()));
3798}
3799
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003800static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3801 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003802 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003803
3804 if (!Parm) {
3805 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3806 return;
3807 }
3808
3809 D->addAttr(::new (S.Context)
3810 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3811 Attr.getAttributeSpellingListIndex()));
3812}
3813
Chandler Carruthedc2c642011-07-02 00:01:44 +00003814static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3815 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003816 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003817
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003818 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003819 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003820}
3821
Chandler Carruthedc2c642011-07-02 00:01:44 +00003822static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3823 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003824 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003825 QualType type = vd->getType();
3826
3827 if (!type->isDependentType() &&
3828 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003829 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003830 << type;
3831 return;
3832 }
3833
3834 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3835
3836 // If we have no lifetime yet, check the lifetime we're presumably
3837 // going to infer.
3838 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3839 lifetime = type->getObjCARCImplicitLifetime();
3840
3841 switch (lifetime) {
3842 case Qualifiers::OCL_None:
3843 assert(type->isDependentType() &&
3844 "didn't infer lifetime for non-dependent type?");
3845 break;
3846
3847 case Qualifiers::OCL_Weak: // meaningful
3848 case Qualifiers::OCL_Strong: // meaningful
3849 break;
3850
3851 case Qualifiers::OCL_ExplicitNone:
3852 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003853 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003854 << (lifetime == Qualifiers::OCL_Autoreleasing);
3855 break;
3856 }
3857
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003858 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003859 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3860 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003861}
3862
Francois Picheta83957a2010-12-19 06:50:37 +00003863//===----------------------------------------------------------------------===//
3864// Microsoft specific attribute handlers.
3865//===----------------------------------------------------------------------===//
3866
Reid Kleckner140c4a72013-05-17 14:04:52 +00003867// Check if MS extensions or some other language extensions are enabled. If
3868// not, issue a diagnostic that the given attribute is unused.
3869static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
3870 bool OtherExtension = false) {
3871 if (S.LangOpts.MicrosoftExt || OtherExtension)
3872 return true;
3873 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3874 return false;
3875}
3876
Chandler Carruthedc2c642011-07-02 00:01:44 +00003877static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003878 if (!S.LangOpts.CPlusPlus) {
3879 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3880 << Attr.getName() << AttributeLangSupport::C;
3881 return;
3882 }
3883
Reid Kleckner140c4a72013-05-17 14:04:52 +00003884 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
3885 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003886
Aaron Ballman60e705e2013-11-24 20:58:02 +00003887 if (!isa<CXXRecordDecl>(D)) {
3888 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3889 << Attr.getName() << ExpectedClass;
3890 return;
3891 }
3892
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003893 StringRef StrRef;
3894 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003895 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003896 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003897
David Majnemer89085342013-08-09 08:56:20 +00003898 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3899 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003900 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3901 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003902
Reid Kleckner140c4a72013-05-17 14:04:52 +00003903 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003904 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003905 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003906 return;
3907 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003908
David Majnemer89085342013-08-09 08:56:20 +00003909 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003910 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003911 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003912 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003913 return;
3914 }
David Majnemer89085342013-08-09 08:56:20 +00003915 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003916 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003917 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003918 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003919 }
Francois Picheta83957a2010-12-19 06:50:37 +00003920
David Majnemer89085342013-08-09 08:56:20 +00003921 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3922 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003923}
3924
John McCall8d32c052012-05-22 21:28:12 +00003925static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003926 if (!checkMicrosoftExt(S, Attr))
Nico Weberefa45b22012-11-07 21:31:36 +00003927 return;
Nico Weberefa45b22012-11-07 21:31:36 +00003928
3929 AttributeList::Kind Kind = Attr.getKind();
3930 if (Kind == AttributeList::AT_SingleInheritance)
3931 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00003932 ::new (S.Context)
3933 SingleInheritanceAttr(Attr.getRange(), S.Context,
3934 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00003935 else if (Kind == AttributeList::AT_MultipleInheritance)
3936 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00003937 ::new (S.Context)
3938 MultipleInheritanceAttr(Attr.getRange(), S.Context,
3939 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00003940 else if (Kind == AttributeList::AT_VirtualInheritance)
3941 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00003942 ::new (S.Context)
3943 VirtualInheritanceAttr(Attr.getRange(), S.Context,
3944 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00003945}
3946
Aaron Ballman5010a762013-12-02 16:17:55 +00003947static void handleWin64Attr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003948 if (!checkMicrosoftExt(S, Attr))
3949 return;
3950
Aaron Ballman5010a762013-12-02 16:17:55 +00003951 D->addAttr(::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
3952 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00003953}
3954
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00003955static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003956 if (!checkMicrosoftExt(S, Attr))
3957 return;
3958 D->addAttr(::new (S.Context)
3959 ForceInlineAttr(Attr.getRange(), S.Context,
3960 Attr.getAttributeSpellingListIndex()));
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00003961}
3962
Reid Klecknerb144d362013-05-20 14:02:37 +00003963static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3964 if (!checkMicrosoftExt(S, Attr))
3965 return;
3966 // Check linkage after possibly merging declaratinos. See
3967 // checkAttributesAfterMerging().
3968 D->addAttr(::new (S.Context)
3969 SelectAnyAttr(Attr.getRange(), S.Context,
3970 Attr.getAttributeSpellingListIndex()));
3971}
3972
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003973/// Handles semantic checking for features that are common to all attributes,
3974/// such as checking whether a parameter was properly specified, or the correct
3975/// number of arguments were passed, etc.
3976static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
3977 const AttributeList &Attr) {
3978 // Several attributes carry different semantics than the parsing requires, so
3979 // those are opted out of the common handling.
3980 //
3981 // We also bail on unknown and ignored attributes because those are handled
3982 // as part of the target-specific handling logic.
3983 if (Attr.hasCustomParsing() ||
3984 Attr.getKind() == AttributeList::UnknownAttribute ||
3985 Attr.getKind() == AttributeList::IgnoredAttribute)
3986 return false;
3987
3988 // If there are no optional arguments, then checking for the argument count
3989 // is trivial.
3990 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
3991 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
3992 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003993
3994 // Check whether the attribute appertains to the given subject.
3995 if (!Attr.diagnoseAppertainsTo(S, D))
3996 return true;
3997
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003998 return false;
3999}
4000
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004001//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004002// Top Level Sema Entry Points
4003//===----------------------------------------------------------------------===//
4004
Richard Smithf8a75c32013-08-29 00:47:48 +00004005/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4006/// the attribute applies to decls. If the attribute is a type attribute, just
4007/// silently ignore it if a GNU attribute.
4008static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4009 const AttributeList &Attr,
4010 bool IncludeCXX11Attributes) {
4011 if (Attr.isInvalid())
4012 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004013
Richard Smithf8a75c32013-08-29 00:47:48 +00004014 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4015 // instead.
4016 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4017 return;
4018
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004019 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4020 return;
4021
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004022 switch (Attr.getKind()) {
Aaron Ballman9beb5172013-12-02 15:13:14 +00004023 case AttributeList::AT_IBAction:
4024 handleSimpleAttribute<IBActionAttr>(S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004025 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4026 case AttributeList::AT_IBOutletCollection:
4027 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004028 case AttributeList::AT_AddressSpace:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004029 case AttributeList::AT_ObjCGC:
4030 case AttributeList::AT_VectorSize:
4031 case AttributeList::AT_NeonVectorType:
4032 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004033 case AttributeList::AT_Ptr32:
4034 case AttributeList::AT_Ptr64:
4035 case AttributeList::AT_SPtr:
4036 case AttributeList::AT_UPtr:
Mike Stumpd3bb5572009-07-24 19:02:52 +00004037 // Ignore these, these are type attributes, handled by
4038 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004039 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004040 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4041 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4042 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4043 case AttributeList::AT_AlwaysInline:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004044 handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004045 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004046 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00004047 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004048 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4049 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4050 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004051 handleDependencyAttr(S, scope, D, Attr);
4052 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004053 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4054 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4055 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004056 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman3a8e2d92013-11-27 18:53:58 +00004057 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004058 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004059 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004060 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004061 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4062 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004063 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004064 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004065 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004066 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00004067 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004068 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4069 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4070 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman9a226212013-08-28 23:13:26 +00004071 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4072 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004073 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4074 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004075 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004076 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004077 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004078 case AttributeList::AT_MayAlias:
4079 handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00004080 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004081 case AttributeList::AT_NoCommon:
4082 handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004083 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004084 case AttributeList::AT_Overloadable:
4085 handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break;
Richard Smith852e9ce2013-11-27 01:46:48 +00004086 case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004087 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4088 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004089 case AttributeList::AT_Naked:
4090 handleSimpleAttribute<NakedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004091 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4092 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4093 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4094 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004095
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004096 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004097 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004098 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004099 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004100
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004101 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004102 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4103
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004104 case AttributeList::AT_ObjCRequiresSuper:
4105 handleObjCRequiresSuperAttr(S, D, Attr); break;
4106
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004107 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00004108 handleNSBridgedAttr(S, scope, D, Attr); break;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004109
4110 case AttributeList::AT_ObjCBridge:
4111 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004112
4113 case AttributeList::AT_ObjCBridgeMutable:
4114 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00004115
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004116 case AttributeList::AT_CFAuditedTransfer:
4117 case AttributeList::AT_CFUnknownTransfer:
John McCall32f5fe12011-09-30 05:12:12 +00004118 handleCFTransferAttr(S, D, Attr); break;
4119
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004120 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004121 case AttributeList::AT_CFConsumed:
4122 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4123 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004124 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004125
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004126 case AttributeList::AT_NSReturnsAutoreleased:
4127 case AttributeList::AT_NSReturnsNotRetained:
4128 case AttributeList::AT_CFReturnsNotRetained:
4129 case AttributeList::AT_NSReturnsRetained:
4130 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004131 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004132
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004133 case AttributeList::AT_WorkGroupSizeHint:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004134 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004135 handleWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00004136
Joey Goulyaba589c2013-03-08 09:42:32 +00004137 case AttributeList::AT_VecTypeHint:
4138 handleVecTypeHint(S, D, Attr); break;
4139
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004140 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004141 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004142
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004143 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4144 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4145 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004146 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004147 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004148 case AttributeList::AT_ArcWeakrefUnavailable:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004149 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004150 case AttributeList::AT_ObjCRootClass:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004151 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break;
Ted Kremenek28eace62013-11-23 01:01:34 +00004152 case AttributeList::AT_ObjCSuppressProtocol:
4153 handleObjCSuppresProtocolAttr(S, D, Attr);
4154 break;
4155 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004156 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004157 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4158 case AttributeList::AT_ReturnsTwice:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004159 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004160 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004161 case AttributeList::AT_Visibility:
4162 handleVisibilityAttr(S, D, Attr, false);
4163 break;
4164 case AttributeList::AT_TypeVisibility:
4165 handleVisibilityAttr(S, D, Attr, true);
4166 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004167 case AttributeList::AT_WarnUnused:
Aaron Ballman6a42b5a2013-11-27 16:59:17 +00004168 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004169 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004170 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004171 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4172 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4173 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4174 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004175 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004176 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004177 case AttributeList::AT_ObjCException:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004178 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004179 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004180 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004181 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004182 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4183 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4184 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4185 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004186 case AttributeList::AT_Pure:
4187 handleSimpleAttribute<PureAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004188 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4189 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004190 case AttributeList::AT_NoInline:
4191 handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004192 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004193 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004194 // Just ignore
4195 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004196 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004197 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004198 case AttributeList::AT_StdCall:
4199 case AttributeList::AT_CDecl:
4200 case AttributeList::AT_FastCall:
4201 case AttributeList::AT_ThisCall:
4202 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004203 case AttributeList::AT_MSABI:
4204 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004205 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004206 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004207 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004208 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004209 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004210 case AttributeList::AT_OpenCLKernel:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004211 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004212 case AttributeList::AT_OpenCLImageAccess:
4213 handleOpenCLImageAccessAttr(S, D, Attr);
4214 break;
John McCall8d32c052012-05-22 21:28:12 +00004215
4216 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004217 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004218 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004219 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004220 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004221 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004222 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004223 case AttributeList::AT_SingleInheritance:
4224 case AttributeList::AT_MultipleInheritance:
4225 case AttributeList::AT_VirtualInheritance:
John McCall8d32c052012-05-22 21:28:12 +00004226 handleInheritanceAttr(S, D, Attr);
4227 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004228 case AttributeList::AT_Win64:
Aaron Ballman5010a762013-12-02 16:17:55 +00004229 handleWin64Attr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004230 case AttributeList::AT_ForceInline:
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004231 handleForceInlineAttr(S, D, Attr);
4232 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004233 case AttributeList::AT_SelectAny:
4234 handleSelectAnyAttr(S, D, Attr);
4235 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004236
4237 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004238 case AttributeList::AT_AssertExclusiveLock:
4239 handleAssertExclusiveLockAttr(S, D, Attr);
4240 break;
4241 case AttributeList::AT_AssertSharedLock:
4242 handleAssertSharedLockAttr(S, D, Attr);
4243 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004244 case AttributeList::AT_GuardedVar:
Aaron Ballmane61b8b82013-12-02 15:02:49 +00004245 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004246 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004247 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004248 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004249 case AttributeList::AT_ScopedLockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004250 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004251 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004252 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004253 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004254 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004255 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004256 break;
4257 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004258 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004259 break;
4260 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004261 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004262 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004263 case AttributeList::AT_Lockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004264 handleSimpleAttribute<LockableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004265 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004266 handleGuardedByAttr(S, D, Attr);
4267 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004268 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004269 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004270 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004271 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004272 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004273 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004274 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004275 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004276 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004277 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004278 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004279 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004280 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004281 handleLockReturnedAttr(S, D, Attr);
4282 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004283 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004284 handleLocksExcludedAttr(S, D, Attr);
4285 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004286 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004287 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004288 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004289 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004290 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004291 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004292 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004293 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004294 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004295 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004296 handleUnlockFunAttr(S, D, Attr);
4297 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004298 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004299 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004300 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004301 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004302 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004303 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004304
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004305 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004306 case AttributeList::AT_Consumable:
4307 handleConsumableAttr(S, D, Attr);
4308 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004309 case AttributeList::AT_CallableWhen:
4310 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004311 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004312 case AttributeList::AT_ParamTypestate:
4313 handleParamTypestateAttr(S, D, Attr);
4314 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004315 case AttributeList::AT_ReturnTypestate:
4316 handleReturnTypestateAttr(S, D, Attr);
4317 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004318 case AttributeList::AT_SetTypestate:
4319 handleSetTypestateAttr(S, D, Attr);
4320 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004321 case AttributeList::AT_TestTypestate:
4322 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004323 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004324
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004325 // Type safety attributes.
4326 case AttributeList::AT_ArgumentWithTypeTag:
4327 handleArgumentWithTypeTagAttr(S, D, Attr);
4328 break;
4329 case AttributeList::AT_TypeTagForDatatype:
4330 handleTypeTagForDatatypeAttr(S, D, Attr);
4331 break;
4332
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004333 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004334 // Ask target about the attribute.
4335 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4336 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004337 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4338 diag::warn_unhandled_ms_attribute_ignored :
4339 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004340 break;
4341 }
4342}
4343
4344/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4345/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004346void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004347 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004348 bool IncludeCXX11Attributes) {
4349 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004350 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004351
4352 // GCC accepts
4353 // static int a9 __attribute__((weakref));
4354 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004355 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004356 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00004357 cast<NamedDecl>(D)->getNameAsString();
4358 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004359 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004360 }
4361}
4362
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004363// Annotation attributes are the only attributes allowed after an access
4364// specifier.
4365bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4366 const AttributeList *AttrList) {
4367 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004368 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004369 handleAnnotateAttr(*this, ASDecl, *l);
4370 } else {
4371 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4372 return true;
4373 }
4374 }
4375
4376 return false;
4377}
4378
John McCall42856de2011-10-01 05:17:03 +00004379/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4380/// contains any decl attributes that we should warn about.
4381static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4382 for ( ; A; A = A->getNext()) {
4383 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004384 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004385 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4386
4387 if (A->getKind() == AttributeList::UnknownAttribute) {
4388 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4389 << A->getName() << A->getRange();
4390 } else {
4391 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4392 << A->getName() << A->getRange();
4393 }
4394 }
4395}
4396
4397/// checkUnusedDeclAttributes - Given a declarator which is not being
4398/// used to build a declaration, complain about any decl attributes
4399/// which might be lying around on it.
4400void Sema::checkUnusedDeclAttributes(Declarator &D) {
4401 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4402 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4403 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4404 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4405}
4406
Ryan Flynn7d470f32009-07-30 03:15:39 +00004407/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004408/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004409NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4410 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004411 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004412 NamedDecl *NewD = 0;
4413 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004414 FunctionDecl *NewFD;
4415 // FIXME: Missing call to CheckFunctionDeclaration().
4416 // FIXME: Mangling?
4417 // FIXME: Is the qualifier info correct?
4418 // FIXME: Is the DeclContext correct?
4419 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4420 Loc, Loc, DeclarationName(II),
4421 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004422 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004423 FD->hasPrototype(),
4424 false/*isConstexprSpecified*/);
4425 NewD = NewFD;
4426
4427 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004428 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004429
4430 // Fake up parameter variables; they are declared as if this were
4431 // a typedef.
4432 QualType FDTy = FD->getType();
4433 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4434 SmallVector<ParmVarDecl*, 16> Params;
4435 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4436 AE = FT->arg_type_end(); AI != AE; ++AI) {
4437 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4438 Param->setScopeInfo(0, Params.size());
4439 Params.push_back(Param);
4440 }
David Blaikie9c70e042011-09-21 18:16:56 +00004441 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004442 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004443 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4444 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004445 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004446 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004447 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004448 if (VD->getQualifier()) {
4449 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004450 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004451 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004452 }
4453 return NewD;
4454}
4455
James Dennett634962f2012-06-14 21:40:34 +00004456/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004457/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004458void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004459 if (W.getUsed()) return; // only do this once
4460 W.setUsed(true);
4461 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4462 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004463 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004464 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4465 NDId->getName()));
4466 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00004467 WeakTopLevelDecl.push_back(NewD);
4468 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4469 // to insert Decl at TU scope, sorry.
4470 DeclContext *SavedContext = CurContext;
4471 CurContext = Context.getTranslationUnitDecl();
4472 PushOnScopeChains(NewD, S);
4473 CurContext = SavedContext;
4474 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004475 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004476 }
4477}
4478
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004479void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4480 // It's valid to "forward-declare" #pragma weak, in which case we
4481 // have to do this.
4482 LoadExternalWeakUndeclaredIdentifiers();
4483 if (!WeakUndeclaredIdentifiers.empty()) {
4484 NamedDecl *ND = NULL;
4485 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4486 if (VD->isExternC())
4487 ND = VD;
4488 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4489 if (FD->isExternC())
4490 ND = FD;
4491 if (ND) {
4492 if (IdentifierInfo *Id = ND->getIdentifier()) {
4493 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4494 = WeakUndeclaredIdentifiers.find(Id);
4495 if (I != WeakUndeclaredIdentifiers.end()) {
4496 WeakInfo W = I->second;
4497 DeclApplyPragmaWeak(S, ND, W);
4498 WeakUndeclaredIdentifiers[Id] = W;
4499 }
4500 }
4501 }
4502 }
4503}
4504
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004505/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4506/// it, apply them to D. This is a bit tricky because PD can have attributes
4507/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004508void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004509 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004510 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004511 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004512
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004513 // Walk the declarator structure, applying decl attributes that were in a type
4514 // position to the decl itself. This handles cases like:
4515 // int *__attr__(x)** D;
4516 // when X is a decl attribute.
4517 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4518 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004519 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004520
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004521 // Finally, apply any attributes on the decl itself.
4522 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004523 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004524}
John McCall28a6aea2009-11-04 02:18:39 +00004525
John McCall31168b02011-06-15 23:02:42 +00004526/// Is the given declaration allowed to use a forbidden type?
4527static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4528 // Private ivars are always okay. Unfortunately, people don't
4529 // always properly make their ivars private, even in system headers.
4530 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004531 // Function declarations in sys headers will be marked unavailable.
4532 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4533 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004534 return false;
4535
4536 // Require it to be declared in a system header.
4537 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4538}
4539
4540/// Handle a delayed forbidden-type diagnostic.
4541static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4542 Decl *decl) {
4543 if (decl && isForbiddenTypeAllowed(S, decl)) {
4544 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4545 "this system declaration uses an unsupported type"));
4546 return;
4547 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004548 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004549 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004550 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004551 // kind of forbidden type messages on unavailable functions.
4552 if (FD->hasAttr<UnavailableAttr>() &&
4553 diag.getForbiddenTypeDiagnostic() ==
4554 diag::err_arc_array_param_no_ownership) {
4555 diag.Triggered = true;
4556 return;
4557 }
4558 }
John McCall31168b02011-06-15 23:02:42 +00004559
4560 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4561 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4562 diag.Triggered = true;
4563}
4564
John McCall2ec85372012-05-07 06:16:41 +00004565void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4566 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004567 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004568 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004569
John McCall2ec85372012-05-07 06:16:41 +00004570 // When delaying diagnostics to run in the context of a parsed
4571 // declaration, we only want to actually emit anything if parsing
4572 // succeeds.
4573 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004574
John McCall2ec85372012-05-07 06:16:41 +00004575 // We emit all the active diagnostics in this pool or any of its
4576 // parents. In general, we'll get one pool for the decl spec
4577 // and a child pool for each declarator; in a decl group like:
4578 // deprecated_typedef foo, *bar, baz();
4579 // only the declarator pops will be passed decls. This is correct;
4580 // we really do need to consider delayed diagnostics from the decl spec
4581 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004582 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004583 do {
John McCall6347b682012-05-07 06:16:58 +00004584 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004585 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4586 // This const_cast is a bit lame. Really, Triggered should be mutable.
4587 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004588 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004589 continue;
4590
John McCallc1465822011-02-14 07:13:47 +00004591 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004592 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00004593 // Don't bother giving deprecation diagnostics if the decl is invalid.
4594 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00004595 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004596 break;
4597
4598 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004599 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004600 break;
John McCall31168b02011-06-15 23:02:42 +00004601
4602 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004603 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004604 break;
John McCall86121512010-01-27 03:50:35 +00004605 }
4606 }
John McCall2ec85372012-05-07 06:16:41 +00004607 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004608}
4609
John McCall6347b682012-05-07 06:16:58 +00004610/// Given a set of delayed diagnostics, re-emit them as if they had
4611/// been delayed in the current context instead of in the given pool.
4612/// Essentially, this just moves them to the current pool.
4613void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4614 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4615 assert(curPool && "re-emitting in undelayed context not supported");
4616 curPool->steal(pool);
4617}
4618
John McCall28a6aea2009-11-04 02:18:39 +00004619static bool isDeclDeprecated(Decl *D) {
4620 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004621 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004622 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004623 // A category implicitly has the availability of the interface.
4624 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4625 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004626 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4627 return false;
4628}
4629
Eli Friedman971bfa12012-08-08 21:52:41 +00004630static void
4631DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
4632 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004633 const ObjCInterfaceDecl *UnknownObjCClass,
4634 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedman971bfa12012-08-08 21:52:41 +00004635 DeclarationName Name = D->getDeclName();
4636 if (!Message.empty()) {
4637 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
4638 S.Diag(D->getLocation(),
4639 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4640 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004641 if (ObjCPropery)
4642 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4643 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00004644 } else if (!UnknownObjCClass) {
4645 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
4646 S.Diag(D->getLocation(),
4647 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4648 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004649 if (ObjCPropery)
4650 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4651 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00004652 } else {
4653 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
4654 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4655 }
4656}
4657
John McCallb45a1e72010-08-26 02:13:20 +00004658void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00004659 Decl *Ctx) {
4660 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00004661 return;
4662
John McCall86121512010-01-27 03:50:35 +00004663 DD.Triggered = true;
Eli Friedman971bfa12012-08-08 21:52:41 +00004664 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
4665 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004666 DD.getUnknownObjCClass(),
4667 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004668}
4669
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004670void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004671 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004672 const ObjCInterfaceDecl *UnknownObjCClass,
4673 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004674 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004675 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004676 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4677 UnknownObjCClass,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004678 ObjCProperty,
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004679 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004680 return;
4681 }
4682
4683 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00004684 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00004685 return;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004686 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004687}