blob: a6240368bf803a38bba42e4e2922069f264b9c24 [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
Aaron Ballmanfb763042013-12-02 18:05:46 +0000243/// \brief Diagnose mutually exclusive attributes when present on a given
244/// declaration. Returns true if diagnosed.
245template <typename AttrTy>
246static bool checkAttrMutualExclusion(Sema &S, Decl *D,
247 const AttributeList &Attr,
248 const char *OtherName) {
249 // FIXME: it would be nice if OtherName did not have to be passed in, but was
250 // instead determined based on the AttrTy template parameter.
251 if (D->hasAttr<AttrTy>()) {
252 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
253 << Attr.getName() << OtherName;
254 return true;
255 }
256 return false;
257}
258
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000259/// \brief Check if IdxExpr is a valid argument index for a function or
260/// instance method D. May output an error.
261///
262/// \returns true if IdxExpr is a valid index.
263static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
264 StringRef AttrName,
265 SourceLocation AttrLoc,
266 unsigned AttrArgNum,
267 const Expr *IdxExpr,
268 uint64_t &Idx)
269{
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000270 assert(isFunctionOrMethod(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000271
272 // In C++ the implicit 'this' function parameter also counts.
273 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000274 bool HP = hasFunctionProto(D);
275 bool HasImplicitThisParam = isInstanceMethod(D);
276 bool IV = HP && isFunctionOrMethodVariadic(D);
277 unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) +
278 HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000279
280 llvm::APSInt IdxInt;
281 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
282 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +0000283 std::string Name = std::string("'") + AttrName.str() + std::string("'");
284 S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str()
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000285 << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000286 return false;
287 }
288
289 Idx = IdxInt.getLimitedValue();
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000290 if (Idx < 1 || (!IV && Idx > NumArgs)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000291 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
292 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
293 return false;
294 }
295 Idx--; // Convert to zero-based.
296 if (HasImplicitThisParam) {
297 if (Idx == 0) {
298 S.Diag(AttrLoc,
299 diag::err_attribute_invalid_implicit_this_argument)
300 << AttrName << IdxExpr->getSourceRange();
301 return false;
302 }
303 --Idx;
304 }
305
306 return true;
307}
308
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000309/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
310/// If not emit an error and return false. If the argument is an identifier it
311/// will emit an error with a fixit hint and treat it as if it was a string
312/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000313bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
314 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000315 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000316 // Look for identifiers. If we have one emit a hint to fix it to a literal.
317 if (Attr.isArgIdent(ArgNum)) {
318 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000319 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000320 << Attr.getName() << AANT_ArgumentString
321 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000322 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000323 Str = Loc->Ident->getName();
324 if (ArgLocation)
325 *ArgLocation = Loc->Loc;
326 return true;
327 }
328
329 // Now check for an actual string literal.
330 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
331 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
332 if (ArgLocation)
333 *ArgLocation = ArgExpr->getLocStart();
334
335 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000336 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000337 << Attr.getName() << AANT_ArgumentString;
338 return false;
339 }
340
341 Str = Literal->getString();
342 return true;
343}
344
Aaron Ballman6f9165a2013-11-27 15:24:06 +0000345/// \brief Applies the given attribute to the Decl without performing any
346/// additional semantic checking.
347template <typename AttrType>
348static void handleSimpleAttribute(Sema &S, Decl *D,
349 const AttributeList &Attr) {
350 D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context,
351 Attr.getAttributeSpellingListIndex()));
352}
353
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000354/// \brief Check if the passed-in expression is of type int or bool.
355static bool isIntOrBool(Expr *Exp) {
356 QualType QT = Exp->getType();
357 return QT->isBooleanType() || QT->isIntegerType();
358}
359
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000360
361// Check to see if the type is a smart pointer of some kind. We assume
362// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000363static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
364 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
365 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000366 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000367 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000368
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000369 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
370 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000371 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000372 return false;
373
374 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000375}
376
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000377/// \brief Check if passed in Decl is a pointer type.
378/// Note that this function may produce an error message.
379/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000380static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
381 const AttributeList &Attr) {
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000382 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000383 QualType QT = vd->getType();
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000384 if (QT->isAnyPointerType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000385 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000386
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000387 if (const RecordType *RT = QT->getAs<RecordType>()) {
388 // If it's an incomplete type, it could be a smart pointer; skip it.
389 // (We don't want to force template instantiation if we can avoid it,
390 // since that would alter the order in which templates are instantiated.)
391 if (RT->isIncompleteType())
392 return true;
393
394 if (threadSafetyCheckIsSmartPointer(S, RT))
395 return true;
396 }
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000397
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000398 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000399 << Attr.getName()->getName() << QT;
400 } else {
401 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
402 << Attr.getName();
403 }
404 return false;
405}
406
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000407/// \brief Checks that the passed in QualType either is of RecordType or points
408/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000409static const RecordType *getRecordType(QualType QT) {
410 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000411 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000412
413 // Now check if we point to record type.
414 if (const PointerType *PT = QT->getAs<PointerType>())
415 return PT->getPointeeType()->getAs<RecordType>();
416
417 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000418}
419
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000420
Jordy Rose740b0c22012-05-08 03:27:22 +0000421static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
422 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000423 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
Aaron Ballman9ead1242013-12-19 02:39:40 +0000424 return RT->getDecl()->hasAttr<LockableAttr>();
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000425}
426
427
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000428/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000429/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000430static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
431 QualType Ty) {
432 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000433
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000434 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000435 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000436 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000437 << Attr.getName() << Ty.getAsString();
438 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000439 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000440
Michael Hana9171bc2012-08-03 17:40:43 +0000441 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000442 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000443 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000444
445 // Allow smart pointers to be used as lockable objects.
446 // FIXME -- Check the type that the smart pointer points to.
447 if (threadSafetyCheckIsSmartPointer(S, RT))
448 return;
449
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000450 // Check if the type is lockable.
451 RecordDecl *RD = RT->getDecl();
Aaron Ballman9ead1242013-12-19 02:39:40 +0000452 if (RD->hasAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000453 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000454
455 // Else check if any base classes are lockable.
456 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
457 CXXBasePaths BPaths(false, false);
458 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
459 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000460 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000461
462 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
463 << Attr.getName() << Ty.getAsString();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000464}
465
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000466/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000467/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000468/// \param Sidx The attribute argument index to start checking with.
469/// \param ParamIdxOk Whether an argument can be indexing into a function
470/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000471static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000472 const AttributeList &Attr,
473 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000474 int Sidx = 0,
475 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000476 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000477 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000478
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000479 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000480 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000481 Args.push_back(ArgExp);
482 continue;
483 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000484
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000485 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000486 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000487 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000488 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000489 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000490 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000491 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000492 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000493
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000494 // We allow constant strings to be used as a placeholder for expressions
495 // that are not valid C++ syntax, but warn that they are ignored.
496 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
497 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000498 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000499 continue;
500 }
501
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000502 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000503
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000504 // A pointer to member expression of the form &MyClass::mu is treated
505 // specially -- we need to look at the type of the member.
506 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
507 if (UOp->getOpcode() == UO_AddrOf)
508 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
509 if (DRE->getDecl()->isCXXInstanceMember())
510 ArgTy = DRE->getDecl()->getType();
511
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000512 // First see if we can just cast to record type, or point to record type.
513 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000514
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000515 // Now check if we index into a record type function param.
516 if(!RT && ParamIdxOk) {
517 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000518 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
519 if(FD && IL) {
520 unsigned int NumParams = FD->getNumParams();
521 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000522 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
523 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
524 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000525 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
526 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000527 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000528 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000529 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000530 }
531 }
532
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000533 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000534
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000535 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000536 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000537}
538
Chris Lattner58418ff2008-06-29 00:16:31 +0000539//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000540// Attribute Implementations
541//===----------------------------------------------------------------------===//
542
Daniel Dunbar032db472008-07-31 22:40:48 +0000543// FIXME: All this manual attribute parsing code is gross. At the
544// least add some helper functions to check most argument patterns (#
545// and types of args).
546
Michael Hana9171bc2012-08-03 17:40:43 +0000547static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000548 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000549 if (!threadSafetyCheckIsPointer(S, D, Attr))
550 return;
551
Michael Han99315932013-01-24 16:46:58 +0000552 D->addAttr(::new (S.Context)
553 PtGuardedVarAttr(Attr.getRange(), S.Context,
554 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000555}
556
Michael Hana9171bc2012-08-03 17:40:43 +0000557static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
558 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000559 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000560 SmallVector<Expr*, 1> Args;
561 // check that all arguments are lockable objects
562 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
563 unsigned Size = Args.size();
564 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000565 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000566
Michael Han3be3b442012-07-23 18:48:41 +0000567 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000568
Michael Han3be3b442012-07-23 18:48:41 +0000569 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000570}
571
Michael Han3be3b442012-07-23 18:48:41 +0000572static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
573 Expr *Arg = 0;
574 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
575 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000576
Michael Han3be3b442012-07-23 18:48:41 +0000577 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
578}
579
Michael Hana9171bc2012-08-03 17:40:43 +0000580static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000581 const AttributeList &Attr) {
582 Expr *Arg = 0;
583 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
584 return;
585
586 if (!threadSafetyCheckIsPointer(S, D, Attr))
587 return;
588
589 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
590 S.Context, Arg));
591}
592
Michael Hana9171bc2012-08-03 17:40:43 +0000593static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
594 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000595 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000596 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000597 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000598
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000599 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000600 QualType QT = cast<ValueDecl>(D)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000601 if (!QT->isDependentType()) {
602 const RecordType *RT = getRecordType(QT);
Aaron Ballman9ead1242013-12-19 02:39:40 +0000603 if (!RT || !RT->getDecl()->hasAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000604 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000605 << Attr.getName();
606 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000607 }
608 }
609
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000610 // Check that all arguments are lockable objects.
611 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000612 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000613 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000614
Michael Han3be3b442012-07-23 18:48:41 +0000615 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000616}
617
Michael Hana9171bc2012-08-03 17:40:43 +0000618static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000619 const AttributeList &Attr) {
620 SmallVector<Expr*, 1> Args;
621 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
622 return;
623
624 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000625 D->addAttr(::new (S.Context)
626 AcquiredAfterAttr(Attr.getRange(), S.Context,
627 StartArg, Args.size(),
628 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000629}
630
Michael Hana9171bc2012-08-03 17:40:43 +0000631static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000632 const AttributeList &Attr) {
633 SmallVector<Expr*, 1> Args;
634 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
635 return;
636
637 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000638 D->addAttr(::new (S.Context)
639 AcquiredBeforeAttr(Attr.getRange(), S.Context,
640 StartArg, Args.size(),
641 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000642}
643
Michael Hana9171bc2012-08-03 17:40:43 +0000644static bool checkLockFunAttrCommon(Sema &S, Decl *D,
645 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000646 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000647 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000648 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000649 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000650
Michael Han3be3b442012-07-23 18:48:41 +0000651 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000652}
653
Michael Hana9171bc2012-08-03 17:40:43 +0000654static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000655 const AttributeList &Attr) {
656 SmallVector<Expr*, 1> Args;
657 if (!checkLockFunAttrCommon(S, D, Attr, Args))
658 return;
659
660 unsigned Size = Args.size();
661 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000662 D->addAttr(::new (S.Context)
663 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
664 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000665}
666
Michael Hana9171bc2012-08-03 17:40:43 +0000667static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000668 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];
Michael Han99315932013-01-24 16:46:58 +0000675 D->addAttr(::new (S.Context)
676 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
677 StartArg, Size,
678 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000679}
680
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000681static void handleAssertSharedLockAttr(Sema &S, Decl *D,
682 const AttributeList &Attr) {
683 SmallVector<Expr*, 1> Args;
684 if (!checkLockFunAttrCommon(S, D, Attr, Args))
685 return;
686
687 unsigned Size = Args.size();
688 Expr **StartArg = Size == 0 ? 0 : &Args[0];
689 D->addAttr(::new (S.Context)
690 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
691 Attr.getAttributeSpellingListIndex()));
692}
693
694static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
695 const AttributeList &Attr) {
696 SmallVector<Expr*, 1> Args;
697 if (!checkLockFunAttrCommon(S, D, Attr, Args))
698 return;
699
700 unsigned Size = Args.size();
701 Expr **StartArg = Size == 0 ? 0 : &Args[0];
702 D->addAttr(::new (S.Context)
703 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
704 StartArg, Size,
705 Attr.getAttributeSpellingListIndex()));
706}
707
708
Michael Hana9171bc2012-08-03 17:40:43 +0000709static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
710 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000711 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000712 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000713 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000714
Aaron Ballman00e99962013-08-31 01:11:41 +0000715 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000716 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000717 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000718 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000719 }
720
721 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000722 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000723
Michael Han3be3b442012-07-23 18:48:41 +0000724 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000725}
726
Michael Hana9171bc2012-08-03 17:40:43 +0000727static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000728 const AttributeList &Attr) {
729 SmallVector<Expr*, 2> Args;
730 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
731 return;
732
Michael Han99315932013-01-24 16:46:58 +0000733 D->addAttr(::new (S.Context)
734 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000735 Attr.getArgAsExpr(0),
736 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000737 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000738}
739
Michael Hana9171bc2012-08-03 17:40:43 +0000740static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000741 const AttributeList &Attr) {
742 SmallVector<Expr*, 2> Args;
743 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
744 return;
745
Michael Han99315932013-01-24 16:46:58 +0000746 D->addAttr(::new (S.Context)
747 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000748 Attr.getArgAsExpr(0),
749 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000750 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000751}
752
Michael Hana9171bc2012-08-03 17:40:43 +0000753static bool checkLocksRequiredCommon(Sema &S, Decl *D,
754 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000755 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000756 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000757 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000758
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000759 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000760 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000761 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000762 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000763
Michael Han3be3b442012-07-23 18:48:41 +0000764 return true;
765}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000766
Michael Hana9171bc2012-08-03 17:40:43 +0000767static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000768 const AttributeList &Attr) {
769 SmallVector<Expr*, 1> Args;
770 if (!checkLocksRequiredCommon(S, D, Attr, Args))
771 return;
772
773 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000774 D->addAttr(::new (S.Context)
775 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
776 StartArg, Args.size(),
777 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000778}
779
Michael Hana9171bc2012-08-03 17:40:43 +0000780static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000781 const AttributeList &Attr) {
782 SmallVector<Expr*, 1> Args;
783 if (!checkLocksRequiredCommon(S, D, Attr, Args))
784 return;
785
786 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000787 D->addAttr(::new (S.Context)
788 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
789 StartArg, Args.size(),
790 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000791}
792
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000793static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000794 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000795 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000796 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000797 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000798 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000799 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000800 Expr **StartArg = Size == 0 ? 0 : &Args[0];
801
Michael Han99315932013-01-24 16:46:58 +0000802 D->addAttr(::new (S.Context)
803 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
804 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000805}
806
807static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000808 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000809 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000810 SmallVector<Expr*, 1> Args;
811 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
812 unsigned Size = Args.size();
813 if (Size == 0)
814 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000815
Michael Han99315932013-01-24 16:46:58 +0000816 D->addAttr(::new (S.Context)
817 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
818 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000819}
820
821static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000822 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000823 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000824 return;
825
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000826 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000827 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000828 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000829 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000830 if (Size == 0)
831 return;
832 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000833
Michael Han99315932013-01-24 16:46:58 +0000834 D->addAttr(::new (S.Context)
835 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
836 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000837}
838
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000839static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000840 ConsumableAttr::ConsumedState DefaultState;
841
842 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000843 IdentifierLoc *IL = Attr.getArgAsIdent(0);
844 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
845 DefaultState)) {
846 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
847 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000848 return;
849 }
David Blaikie16f76d22013-09-06 01:28:43 +0000850 } else {
851 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
852 << Attr.getName() << AANT_ArgumentIdentifier;
853 return;
854 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000855
856 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000857 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000858 Attr.getAttributeSpellingListIndex()));
859}
860
861static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
862 const AttributeList &Attr) {
863 ASTContext &CurrContext = S.getASTContext();
864 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
865
866 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
867 if (!RD->hasAttr<ConsumableAttr>()) {
868 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
869 RD->getNameAsString();
870
871 return false;
872 }
873 }
874
875 return true;
876}
877
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000878
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000879static void handleCallableWhenAttr(Sema &S, Decl *D,
880 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000881 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
882 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000883
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000884 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
885 return;
886
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000887 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
888 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
889 CallableWhenAttr::ConsumedState CallableState;
890
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000891 StringRef StateString;
892 SourceLocation Loc;
893 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
894 return;
895
896 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000897 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000898 S.Diag(Loc, diag::warn_attribute_type_not_supported)
899 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000900 return;
901 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000902
903 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000904 }
905
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000906 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000907 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
908 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000909}
910
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000911
DeLesley Hutchins69391772013-10-17 23:23:53 +0000912static void handleParamTypestateAttr(Sema &S, Decl *D,
913 const AttributeList &Attr) {
914 if (!checkAttributeNumArgs(S, Attr, 1)) return;
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000915
DeLesley Hutchins69391772013-10-17 23:23:53 +0000916 ParamTypestateAttr::ConsumedState ParamState;
917
918 if (Attr.isArgIdent(0)) {
919 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
920 StringRef StateString = Ident->Ident->getName();
921
922 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
923 ParamState)) {
924 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
925 << Attr.getName() << StateString;
926 return;
927 }
928 } else {
929 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
930 Attr.getName() << AANT_ArgumentIdentifier;
931 return;
932 }
933
934 // FIXME: This check is currently being done in the analysis. It can be
935 // enabled here only after the parser propagates attributes at
936 // template specialization definition, not declaration.
937 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
938 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
939 //
940 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
941 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
942 // ReturnType.getAsString();
943 // return;
944 //}
945
946 D->addAttr(::new (S.Context)
947 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
948 Attr.getAttributeSpellingListIndex()));
949}
950
951
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000952static void handleReturnTypestateAttr(Sema &S, Decl *D,
953 const AttributeList &Attr) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000954 if (!checkAttributeNumArgs(S, Attr, 1)) return;
955
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000956 ReturnTypestateAttr::ConsumedState ReturnState;
957
958 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000959 IdentifierLoc *IL = Attr.getArgAsIdent(0);
960 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
961 ReturnState)) {
962 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
963 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000964 return;
965 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000966 } else {
967 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
968 Attr.getName() << AANT_ArgumentIdentifier;
969 return;
970 }
971
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000972 // FIXME: This check is currently being done in the analysis. It can be
973 // enabled here only after the parser propagates attributes at
974 // template specialization definition, not declaration.
975 //QualType ReturnType;
976 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000977 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
978 // ReturnType = Param->getType();
979 //
980 //} else if (const CXXConstructorDecl *Constructor =
981 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000982 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
983 //
984 //} else {
985 //
986 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
987 //}
988 //
989 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
990 //
991 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
992 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
993 // ReturnType.getAsString();
994 // return;
995 //}
996
997 D->addAttr(::new (S.Context)
998 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
999 Attr.getAttributeSpellingListIndex()));
1000}
1001
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001002
1003static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001004 if (!checkAttributeNumArgs(S, Attr, 1))
1005 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001006
1007 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1008 return;
1009
1010 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001011 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001012 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1013 StringRef Param = Ident->Ident->getName();
1014 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1015 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1016 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001017 return;
1018 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001019 } else {
1020 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1021 Attr.getName() << AANT_ArgumentIdentifier;
1022 return;
1023 }
1024
1025 D->addAttr(::new (S.Context)
1026 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1027 Attr.getAttributeSpellingListIndex()));
1028}
1029
Chris Wailes9385f9f2013-10-29 20:28:41 +00001030static void handleTestTypestateAttr(Sema &S, Decl *D,
1031 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001032 if (!checkAttributeNumArgs(S, Attr, 1))
1033 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001034
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001035 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1036 return;
1037
Chris Wailes9385f9f2013-10-29 20:28:41 +00001038 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001039 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001040 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1041 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001042 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001043 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1044 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001045 return;
1046 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001047 } else {
1048 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1049 Attr.getName() << AANT_ArgumentIdentifier;
1050 return;
1051 }
1052
1053 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001054 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001055 Attr.getAttributeSpellingListIndex()));
1056}
1057
Chandler Carruthedc2c642011-07-02 00:01:44 +00001058static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1059 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001060 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001061 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001062}
1063
Chandler Carruthedc2c642011-07-02 00:01:44 +00001064static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001065 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001066 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001067 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001068 // If the alignment is less than or equal to 8 bits, the packed attribute
1069 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001070 if (!FD->getType()->isDependentType() &&
1071 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001072 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001073 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001074 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001075 else
Michael Han99315932013-01-24 16:46:58 +00001076 FD->addAttr(::new (S.Context)
1077 PackedAttr(Attr.getRange(), S.Context,
1078 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001079 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001080 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001081}
1082
Ted Kremenek7fd17232011-09-29 07:02:25 +00001083static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1084 // The IBOutlet/IBOutletCollection attributes only apply to instance
1085 // variables or properties of Objective-C classes. The outlet must also
1086 // have an object reference type.
1087 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1088 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001089 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001090 << Attr.getName() << VD->getType() << 0;
1091 return false;
1092 }
1093 }
1094 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1095 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001096 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001097 << Attr.getName() << PD->getType() << 1;
1098 return false;
1099 }
1100 }
1101 else {
1102 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1103 return false;
1104 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001105
Ted Kremenek7fd17232011-09-29 07:02:25 +00001106 return true;
1107}
1108
Chandler Carruthedc2c642011-07-02 00:01:44 +00001109static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001110 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001111 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001112
Michael Han99315932013-01-24 16:46:58 +00001113 D->addAttr(::new (S.Context)
1114 IBOutletAttr(Attr.getRange(), S.Context,
1115 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001116}
1117
Chandler Carruthedc2c642011-07-02 00:01:44 +00001118static void handleIBOutletCollection(Sema &S, Decl *D,
1119 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001120
1121 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001122 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001123 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1124 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001125 return;
1126 }
1127
Ted Kremenek7fd17232011-09-29 07:02:25 +00001128 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001129 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001130
Richard Smithb1f9a282013-10-31 01:56:18 +00001131 ParsedType PT;
1132
1133 if (Attr.hasParsedType())
1134 PT = Attr.getTypeArg();
1135 else {
1136 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1137 S.getScopeForContext(D->getDeclContext()->getParent()));
1138 if (!PT) {
1139 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1140 return;
1141 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001142 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001143
Richard Smithb87c4652013-10-31 21:23:20 +00001144 TypeSourceInfo *QTLoc = 0;
1145 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1146 if (!QTLoc)
1147 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001148
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001149 // Diagnose use of non-object type in iboutletcollection attribute.
1150 // FIXME. Gnu attribute extension ignores use of builtin types in
1151 // attributes. So, __attribute__((iboutletcollection(char))) will be
1152 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001153 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001154 S.Diag(Attr.getLoc(),
1155 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1156 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001157 return;
1158 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001159
Michael Han99315932013-01-24 16:46:58 +00001160 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001161 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001162 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001163}
1164
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001165static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001166 if (const RecordType *UT = T->getAsUnionType())
1167 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1168 RecordDecl *UD = UT->getDecl();
1169 for (RecordDecl::field_iterator it = UD->field_begin(),
1170 itend = UD->field_end(); it != itend; ++it) {
1171 QualType QT = it->getType();
1172 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1173 T = QT;
1174 return;
1175 }
1176 }
1177 }
1178}
1179
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001180static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopese881ce22012-06-18 16:39:04 +00001181 if (!isFunctionOrMethod(D)) {
1182 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001183 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopese881ce22012-06-18 16:39:04 +00001184 return;
1185 }
1186
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001187 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1188 return;
1189
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001190 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001191 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001192 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001193 uint64_t Idx;
1194 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1195 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001196 return;
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001197
1198 // check if the function argument is of an integer type
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001199 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001200 if (!T->isIntegerType()) {
Aaron Ballman9d695092013-07-30 14:10:17 +00001201 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1202 << Attr.getName() << AANT_ArgumentIntegerConstant
1203 << Ex->getSourceRange();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001204 return;
1205 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001206 SizeArgs.push_back(Idx);
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001207 }
1208
1209 // check if the function returns a pointer
1210 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1211 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001212 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001213 }
1214
Michael Han99315932013-01-24 16:46:58 +00001215 D->addAttr(::new (S.Context)
1216 AllocSizeAttr(Attr.getRange(), S.Context,
1217 SizeArgs.data(), SizeArgs.size(),
1218 Attr.getAttributeSpellingListIndex()));
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001219}
1220
Chandler Carruthedc2c642011-07-02 00:01:44 +00001221static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001222 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1223 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001224 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001225 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001226 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001227 return;
1228 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001229
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001230 SmallVector<unsigned, 8> NonNullArgs;
1231 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001232 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001233 uint64_t Idx;
1234 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1235 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001236 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001237
1238 // Is the function argument a pointer type?
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001239 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001240 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001241
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001242 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001243 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +00001244 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +00001245 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001246 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001247 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001248
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001249 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001250 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001251
1252 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1253 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001254 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001255 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1256 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001257 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001258 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001259 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001260 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001261
Ted Kremenek22813f42010-10-21 18:49:36 +00001262 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001263 if (NonNullArgs.empty()) {
1264 // Warn the trivial case only if attribute is not coming from a
1265 // macro instantiation.
1266 if (Attr.getLoc().isFileID())
1267 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001268 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001269 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001270 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001271
Nick Lewyckye1121512013-01-24 01:12:16 +00001272 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001273 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001274 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001275 D->addAttr(::new (S.Context)
1276 NonNullAttr(Attr.getRange(), S.Context, start, size,
1277 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001278}
1279
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001280static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1281 switch (K) {
1282 case OwnershipAttr::Holds: return "'ownership_holds'";
1283 case OwnershipAttr::Takes: return "'ownership_takes'";
1284 case OwnershipAttr::Returns: return "'ownership_returns'";
1285 }
1286 llvm_unreachable("unknown ownership");
1287}
1288
Chandler Carruthedc2c642011-07-02 00:01:44 +00001289static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001290 // This attribute must be applied to a function declaration. The first
1291 // argument to the attribute must be an identifier, the name of the resource,
1292 // for example: malloc. The following arguments must be argument indexes, the
1293 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001294 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001295 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001296 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001297
Aaron Ballman00e99962013-08-31 01:11:41 +00001298 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001299 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001300 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001301 return;
1302 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001303
Richard Smith852e9ce2013-11-27 01:46:48 +00001304 // Figure out our Kind.
1305 OwnershipAttr::OwnershipKind K =
1306 OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0,
1307 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001308
Richard Smith852e9ce2013-11-27 01:46:48 +00001309 // Check arguments.
1310 switch (K) {
1311 case OwnershipAttr::Takes:
1312 case OwnershipAttr::Holds:
1313 if (AL.getNumArgs() < 2) {
1314 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
1315 return;
1316 }
1317 break;
1318 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001319 if (AL.getNumArgs() > 2) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001320 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001321 return;
1322 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001323 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001324 }
1325
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001326 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001327 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1328 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001329 return;
1330 }
1331
Richard Smith852e9ce2013-11-27 01:46:48 +00001332 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001333
1334 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001335 StringRef ModuleName = Module->getName();
1336 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1337 ModuleName.size() > 4) {
1338 ModuleName = ModuleName.drop_front(2).drop_back(2);
1339 Module = &S.PP.getIdentifierTable().get(ModuleName);
1340 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001341
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001342 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001343 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1344 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001345 uint64_t Idx;
1346 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
Aaron Ballman00e99962013-08-31 01:11:41 +00001347 AL.getLoc(), i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001348 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001349
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001350 // Is the function argument a pointer type?
1351 QualType T = getFunctionOrMethodArgType(D, Idx);
1352 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001353 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001354 case OwnershipAttr::Takes:
1355 case OwnershipAttr::Holds:
1356 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1357 Err = 0;
1358 break;
1359 case OwnershipAttr::Returns:
1360 if (!T->isIntegerType())
1361 Err = 1;
1362 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001363 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001364 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001365 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001366 << Ex->getSourceRange();
1367 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001368 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001369
1370 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001371 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001372 i = D->specific_attr_begin<OwnershipAttr>(),
1373 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001374 // FIXME: A returns attribute should conflict with any returns attribute
1375 // with a different index too.
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001376 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1377 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1378 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1379 << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind());
1380 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001381 }
1382 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001383 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001384 }
1385
1386 unsigned* start = OwnershipArgs.data();
1387 unsigned size = OwnershipArgs.size();
1388 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001389
Michael Han99315932013-01-24 16:46:58 +00001390 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001391 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001392 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001393}
1394
Chandler Carruthedc2c642011-07-02 00:01:44 +00001395static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001396 // Check the attribute arguments.
1397 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001398 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1399 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001400 return;
1401 }
1402
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001403 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001404
Rafael Espindolac18086a2010-02-23 22:00:30 +00001405 // gcc rejects
1406 // class c {
1407 // static int a __attribute__((weakref ("v2")));
1408 // static int b() __attribute__((weakref ("f3")));
1409 // };
1410 // and ignores the attributes of
1411 // void f(void) {
1412 // static int a __attribute__((weakref ("v2")));
1413 // }
1414 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001415 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001416 if (!Ctx->isFileContext()) {
1417 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +00001418 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +00001419 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001420 }
1421
1422 // The GCC manual says
1423 //
1424 // At present, a declaration to which `weakref' is attached can only
1425 // be `static'.
1426 //
1427 // It also says
1428 //
1429 // Without a TARGET,
1430 // given as an argument to `weakref' or to `alias', `weakref' is
1431 // equivalent to `weak'.
1432 //
1433 // gcc 4.4.1 will accept
1434 // int a7 __attribute__((weakref));
1435 // as
1436 // int a7 __attribute__((weak));
1437 // This looks like a bug in gcc. We reject that for now. We should revisit
1438 // it if this behaviour is actually used.
1439
Rafael Espindolac18086a2010-02-23 22:00:30 +00001440 // GCC rejects
1441 // static ((alias ("y"), weakref)).
1442 // Should we? How to check that weakref is before or after alias?
1443
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001444 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1445 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1446 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001447 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001448 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001449 // GCC will accept anything as the argument of weakref. Should we
1450 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001451 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1452 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001453
Michael Han99315932013-01-24 16:46:58 +00001454 D->addAttr(::new (S.Context)
1455 WeakRefAttr(Attr.getRange(), S.Context,
1456 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001457}
1458
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001459static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1460 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001461 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001462 return;
1463
Douglas Gregore8bbc122011-09-02 00:18:52 +00001464 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001465 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1466 return;
1467 }
1468
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001469 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001470
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001471 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001472 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001473}
1474
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001475static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanfb763042013-12-02 18:05:46 +00001476 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr, "hot"))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001477 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001478
Michael Han99315932013-01-24 16:46:58 +00001479 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1480 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001481}
1482
1483static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanfb763042013-12-02 18:05:46 +00001484 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr, "cold"))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001485 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001486
Michael Han99315932013-01-24 16:46:58 +00001487 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1488 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001489}
1490
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001491static void handleTLSModelAttr(Sema &S, Decl *D,
1492 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001493 StringRef Model;
1494 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001495 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001496 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001497 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001498
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001499 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001500 if (Model != "global-dynamic" && Model != "local-dynamic"
1501 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001502 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001503 return;
1504 }
1505
Michael Han99315932013-01-24 16:46:58 +00001506 D->addAttr(::new (S.Context)
1507 TLSModelAttr(Attr.getRange(), S.Context, Model,
1508 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001509}
1510
Chandler Carruthedc2c642011-07-02 00:01:44 +00001511static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001512 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001513 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001514 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001515 D->addAttr(::new (S.Context)
1516 MallocAttr(Attr.getRange(), S.Context,
1517 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001518 return;
1519 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001520 }
1521
Ted Kremenek08479ae2009-08-15 00:51:46 +00001522 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001523}
1524
Chandler Carruthedc2c642011-07-02 00:01:44 +00001525static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001526 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001527 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1528 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001529 return;
1530 }
1531
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001532 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1533 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001534}
1535
Chandler Carruthedc2c642011-07-02 00:01:44 +00001536static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001537 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001538
1539 if (S.CheckNoReturnAttr(attr)) return;
1540
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001541 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001542 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001543 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001544 return;
1545 }
1546
Michael Han99315932013-01-24 16:46:58 +00001547 D->addAttr(::new (S.Context)
1548 NoReturnAttr(attr.getRange(), S.Context,
1549 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001550}
1551
1552bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001553 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001554 attr.setInvalid();
1555 return true;
1556 }
1557
1558 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001559}
1560
Chandler Carruthedc2c642011-07-02 00:01:44 +00001561static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1562 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001563
1564 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1565 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001566 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1567 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001568 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1569 && !VD->getType()->isFunctionPointerType())) {
1570 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001571 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001572 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001573 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001574 return;
1575 }
1576 }
1577
Michael Han99315932013-01-24 16:46:58 +00001578 D->addAttr(::new (S.Context)
1579 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1580 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001581}
1582
John Thompsoncdb847ba2010-08-09 21:53:52 +00001583// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001584static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001585/*
1586 Returning a Vector Class in Registers
1587
Eric Christopherbc638a82010-12-01 22:13:54 +00001588 According to the PPU ABI specifications, a class with a single member of
1589 vector type is returned in memory when used as the return value of a function.
1590 This results in inefficient code when implementing vector classes. To return
1591 the value in a single vector register, add the vecreturn attribute to the
1592 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001593
1594 Example:
1595
1596 struct Vector
1597 {
1598 __vector float xyzw;
1599 } __attribute__((vecreturn));
1600
1601 Vector Add(Vector lhs, Vector rhs)
1602 {
1603 Vector result;
1604 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1605 return result; // This will be returned in a register
1606 }
1607*/
Aaron Ballman9ead1242013-12-19 02:39:40 +00001608 if (D->hasAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001609 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1610 return;
1611 }
1612
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001613 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001614 int count = 0;
1615
1616 if (!isa<CXXRecordDecl>(record)) {
1617 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1618 return;
1619 }
1620
1621 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1622 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1623 return;
1624 }
1625
Eric Christopherbc638a82010-12-01 22:13:54 +00001626 for (RecordDecl::field_iterator iter = record->field_begin();
1627 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001628 if ((count == 1) || !iter->getType()->isVectorType()) {
1629 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1630 return;
1631 }
1632 count++;
1633 }
1634
Michael Han99315932013-01-24 16:46:58 +00001635 D->addAttr(::new (S.Context)
1636 VecReturnAttr(Attr.getRange(), S.Context,
1637 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001638}
1639
Richard Smithe233fbf2013-01-28 22:42:45 +00001640static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1641 const AttributeList &Attr) {
1642 if (isa<ParmVarDecl>(D)) {
1643 // [[carries_dependency]] can only be applied to a parameter if it is a
1644 // parameter of a function declaration or lambda.
1645 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1646 S.Diag(Attr.getLoc(),
1647 diag::err_carries_dependency_param_not_function_decl);
1648 return;
1649 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001650 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001651
1652 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1653 Attr.getRange(), S.Context,
1654 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001655}
1656
Chandler Carruthedc2c642011-07-02 00:01:44 +00001657static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001658 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00001659 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001660 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001661 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001662 return;
1663 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001664
Michael Han99315932013-01-24 16:46:58 +00001665 D->addAttr(::new (S.Context)
1666 UnusedAttr(Attr.getRange(), S.Context,
1667 Attr.getAttributeSpellingListIndex()));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001668}
1669
Chandler Carruthedc2c642011-07-02 00:01:44 +00001670static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001671 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001672 if (VD->hasLocalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001673 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1674 return;
1675 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001676 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001677 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001678 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001679 return;
1680 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001681
Michael Han99315932013-01-24 16:46:58 +00001682 D->addAttr(::new (S.Context)
1683 UsedAttr(Attr.getRange(), S.Context,
1684 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001685}
1686
Chandler Carruthedc2c642011-07-02 00:01:44 +00001687static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001688 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001689 if (Attr.getNumArgs() > 1) {
1690 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001691 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001692 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001693
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001694 uint32_t priority = ConstructorAttr::DefaultPriority;
1695 if (Attr.getNumArgs() > 0 &&
1696 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1697 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001698
Michael Han99315932013-01-24 16:46:58 +00001699 D->addAttr(::new (S.Context)
1700 ConstructorAttr(Attr.getRange(), S.Context, priority,
1701 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001702}
1703
Chandler Carruthedc2c642011-07-02 00:01:44 +00001704static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001705 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001706 if (Attr.getNumArgs() > 1) {
1707 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001708 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001709 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001710
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001711 uint32_t priority = ConstructorAttr::DefaultPriority;
1712 if (Attr.getNumArgs() > 0 &&
1713 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1714 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001715
Michael Han99315932013-01-24 16:46:58 +00001716 D->addAttr(::new (S.Context)
1717 DestructorAttr(Attr.getRange(), S.Context, priority,
1718 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001719}
1720
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001721template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001722static void handleAttrWithMessage(Sema &S, Decl *D,
1723 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001724 unsigned NumArgs = Attr.getNumArgs();
1725 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001726 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001727 return;
1728 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001729
1730 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001731 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001732 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001733 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001734
Michael Han99315932013-01-24 16:46:58 +00001735 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1736 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001737}
1738
Ted Kremenek28eace62013-11-23 01:01:34 +00001739static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
1740 const AttributeList &Attr) {
Ted Kremenek28eace62013-11-23 01:01:34 +00001741 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001742 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1743 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001744}
1745
Jordy Rose740b0c22012-05-08 03:27:22 +00001746static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1747 IdentifierInfo *Platform,
1748 VersionTuple Introduced,
1749 VersionTuple Deprecated,
1750 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001751 StringRef PlatformName
1752 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1753 if (PlatformName.empty())
1754 PlatformName = Platform->getName();
1755
1756 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1757 // of these steps are needed).
1758 if (!Introduced.empty() && !Deprecated.empty() &&
1759 !(Introduced <= Deprecated)) {
1760 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1761 << 1 << PlatformName << Deprecated.getAsString()
1762 << 0 << Introduced.getAsString();
1763 return true;
1764 }
1765
1766 if (!Introduced.empty() && !Obsoleted.empty() &&
1767 !(Introduced <= Obsoleted)) {
1768 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1769 << 2 << PlatformName << Obsoleted.getAsString()
1770 << 0 << Introduced.getAsString();
1771 return true;
1772 }
1773
1774 if (!Deprecated.empty() && !Obsoleted.empty() &&
1775 !(Deprecated <= Obsoleted)) {
1776 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1777 << 2 << PlatformName << Obsoleted.getAsString()
1778 << 1 << Deprecated.getAsString();
1779 return true;
1780 }
1781
1782 return false;
1783}
1784
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001785/// \brief Check whether the two versions match.
1786///
1787/// If either version tuple is empty, then they are assumed to match. If
1788/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1789static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1790 bool BeforeIsOkay) {
1791 if (X.empty() || Y.empty())
1792 return true;
1793
1794 if (X == Y)
1795 return true;
1796
1797 if (BeforeIsOkay && X < Y)
1798 return true;
1799
1800 return false;
1801}
1802
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001803AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001804 IdentifierInfo *Platform,
1805 VersionTuple Introduced,
1806 VersionTuple Deprecated,
1807 VersionTuple Obsoleted,
1808 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001809 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001810 bool Override,
1811 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001812 VersionTuple MergedIntroduced = Introduced;
1813 VersionTuple MergedDeprecated = Deprecated;
1814 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001815 bool FoundAny = false;
1816
Rafael Espindolac67f2232012-05-10 02:50:16 +00001817 if (D->hasAttrs()) {
1818 AttrVec &Attrs = D->getAttrs();
1819 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1820 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1821 if (!OldAA) {
1822 ++i;
1823 continue;
1824 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001825
Rafael Espindolac67f2232012-05-10 02:50:16 +00001826 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1827 if (OldPlatform != Platform) {
1828 ++i;
1829 continue;
1830 }
1831
1832 FoundAny = true;
1833 VersionTuple OldIntroduced = OldAA->getIntroduced();
1834 VersionTuple OldDeprecated = OldAA->getDeprecated();
1835 VersionTuple OldObsoleted = OldAA->getObsoleted();
1836 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001837
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001838 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1839 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1840 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1841 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001842 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001843 if (Override) {
1844 int Which = -1;
1845 VersionTuple FirstVersion;
1846 VersionTuple SecondVersion;
1847 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1848 Which = 0;
1849 FirstVersion = OldIntroduced;
1850 SecondVersion = Introduced;
1851 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1852 Which = 1;
1853 FirstVersion = Deprecated;
1854 SecondVersion = OldDeprecated;
1855 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1856 Which = 2;
1857 FirstVersion = Obsoleted;
1858 SecondVersion = OldObsoleted;
1859 }
1860
1861 if (Which == -1) {
1862 Diag(OldAA->getLocation(),
1863 diag::warn_mismatched_availability_override_unavail)
1864 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1865 } else {
1866 Diag(OldAA->getLocation(),
1867 diag::warn_mismatched_availability_override)
1868 << Which
1869 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1870 << FirstVersion.getAsString() << SecondVersion.getAsString();
1871 }
1872 Diag(Range.getBegin(), diag::note_overridden_method);
1873 } else {
1874 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1875 Diag(Range.getBegin(), diag::note_previous_attribute);
1876 }
1877
Rafael Espindolac67f2232012-05-10 02:50:16 +00001878 Attrs.erase(Attrs.begin() + i);
1879 --e;
1880 continue;
1881 }
1882
1883 VersionTuple MergedIntroduced2 = MergedIntroduced;
1884 VersionTuple MergedDeprecated2 = MergedDeprecated;
1885 VersionTuple MergedObsoleted2 = MergedObsoleted;
1886
1887 if (MergedIntroduced2.empty())
1888 MergedIntroduced2 = OldIntroduced;
1889 if (MergedDeprecated2.empty())
1890 MergedDeprecated2 = OldDeprecated;
1891 if (MergedObsoleted2.empty())
1892 MergedObsoleted2 = OldObsoleted;
1893
1894 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1895 MergedIntroduced2, MergedDeprecated2,
1896 MergedObsoleted2)) {
1897 Attrs.erase(Attrs.begin() + i);
1898 --e;
1899 continue;
1900 }
1901
1902 MergedIntroduced = MergedIntroduced2;
1903 MergedDeprecated = MergedDeprecated2;
1904 MergedObsoleted = MergedObsoleted2;
1905 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001906 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001907 }
1908
1909 if (FoundAny &&
1910 MergedIntroduced == Introduced &&
1911 MergedDeprecated == Deprecated &&
1912 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001913 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001914
Ted Kremenekb5445722013-04-06 00:34:27 +00001915 // Only create a new attribute if !Override, but we want to do
1916 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001917 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001918 MergedDeprecated, MergedObsoleted) &&
1919 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001920 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1921 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001922 Obsoleted, IsUnavailable, Message,
1923 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001924 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001925 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001926}
1927
Chandler Carruthedc2c642011-07-02 00:01:44 +00001928static void handleAvailabilityAttr(Sema &S, Decl *D,
1929 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001930 if (!checkAttributeNumArgs(S, Attr, 1))
1931 return;
1932 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001933 unsigned Index = Attr.getAttributeSpellingListIndex();
1934
Aaron Ballman00e99962013-08-31 01:11:41 +00001935 IdentifierInfo *II = Platform->Ident;
1936 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1937 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1938 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001939
Rafael Espindolac231fab2013-01-08 21:30:32 +00001940 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1941 if (!ND) {
1942 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1943 return;
1944 }
1945
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001946 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1947 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1948 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001949 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001950 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001951 if (const StringLiteral *SE =
1952 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001953 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001954
Aaron Ballman00e99962013-08-31 01:11:41 +00001955 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001956 Introduced.Version,
1957 Deprecated.Version,
1958 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001959 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001960 /*Override=*/false,
1961 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001962 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001963 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001964}
1965
John McCalld041a9b2013-02-20 01:54:26 +00001966template <class T>
1967static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1968 typename T::VisibilityType value,
1969 unsigned attrSpellingListIndex) {
1970 T *existingAttr = D->getAttr<T>();
1971 if (existingAttr) {
1972 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1973 if (existingValue == value)
1974 return NULL;
1975 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1976 S.Diag(range.getBegin(), diag::note_previous_attribute);
1977 D->dropAttr<T>();
1978 }
1979 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1980}
1981
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001982VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001983 VisibilityAttr::VisibilityType Vis,
1984 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001985 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1986 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001987}
1988
John McCalld041a9b2013-02-20 01:54:26 +00001989TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1990 TypeVisibilityAttr::VisibilityType Vis,
1991 unsigned AttrSpellingListIndex) {
1992 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1993 AttrSpellingListIndex);
1994}
1995
1996static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1997 bool isTypeVisibility) {
1998 // Visibility attributes don't mean anything on a typedef.
1999 if (isa<TypedefNameDecl>(D)) {
2000 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2001 << Attr.getName();
2002 return;
2003 }
2004
2005 // 'type_visibility' can only go on a type or namespace.
2006 if (isTypeVisibility &&
2007 !(isa<TagDecl>(D) ||
2008 isa<ObjCInterfaceDecl>(D) ||
2009 isa<NamespaceDecl>(D))) {
2010 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2011 << Attr.getName() << ExpectedTypeOrNamespace;
2012 return;
2013 }
2014
Benjamin Kramer70370212013-09-09 15:08:57 +00002015 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002016 StringRef TypeStr;
2017 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002018 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002019 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002020
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002021 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002022 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002023 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002024 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002025 return;
2026 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002027
2028 // Complain about attempts to use protected visibility on targets
2029 // (like Darwin) that don't support it.
2030 if (type == VisibilityAttr::Protected &&
2031 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2032 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2033 type = VisibilityAttr::Default;
2034 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002035
Michael Han99315932013-01-24 16:46:58 +00002036 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002037 clang::Attr *newAttr;
2038 if (isTypeVisibility) {
2039 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2040 (TypeVisibilityAttr::VisibilityType) type,
2041 Index);
2042 } else {
2043 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2044 }
2045 if (newAttr)
2046 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002047}
2048
Chandler Carruthedc2c642011-07-02 00:01:44 +00002049static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2050 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002051 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002052 if (!Attr.isArgIdent(0)) {
2053 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2054 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002055 return;
2056 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002057
Aaron Ballman682ee422013-09-11 19:47:58 +00002058 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2059 ObjCMethodFamilyAttr::FamilyKind F;
2060 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2061 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2062 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002063 return;
2064 }
2065
Aaron Ballman682ee422013-09-11 19:47:58 +00002066 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCall31168b02011-06-15 23:02:42 +00002067 !method->getResultType()->isObjCObjectPointerType()) {
2068 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2069 << method->getResultType();
2070 // Ignore the attribute.
2071 return;
2072 }
2073
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002074 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman682ee422013-09-11 19:47:58 +00002075 S.Context, F));
John McCall86bc21f2011-03-02 11:33:24 +00002076}
2077
Chandler Carruthedc2c642011-07-02 00:01:44 +00002078static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002079 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002080 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002081 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002082 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2083 return;
2084 }
2085 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002086 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2087 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002088 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002089 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2090 return;
2091 }
2092 }
2093 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002094 // It is okay to include this attribute on properties, e.g.:
2095 //
2096 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2097 //
2098 // In this case it follows tradition and suppresses an error in the above
2099 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002100 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002101 }
Michael Han99315932013-01-24 16:46:58 +00002102 D->addAttr(::new (S.Context)
2103 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2104 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002105}
2106
Chandler Carruthedc2c642011-07-02 00:01:44 +00002107static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002108 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002109 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002110 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002111 return;
2112 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002113
Aaron Ballman00e99962013-08-31 01:11:41 +00002114 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002115 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002116 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2117 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2118 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002119 return;
2120 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002121
Michael Han99315932013-01-24 16:46:58 +00002122 D->addAttr(::new (S.Context)
2123 BlocksAttr(Attr.getRange(), S.Context, type,
2124 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002125}
2126
Chandler Carruthedc2c642011-07-02 00:01:44 +00002127static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002128 // check the attribute arguments.
2129 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002130 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002131 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002132 }
2133
Aaron Ballman18a78382013-11-21 00:28:23 +00002134 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002135 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002136 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002137 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002138 if (E->isTypeDependent() || E->isValueDependent() ||
2139 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002140 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002141 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002142 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002143 return;
2144 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002145
John McCallb46f2872011-09-09 07:56:05 +00002146 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002147 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2148 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002149 return;
2150 }
John McCallb46f2872011-09-09 07:56:05 +00002151
2152 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002153 }
2154
Aaron Ballman18a78382013-11-21 00:28:23 +00002155 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002156 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002157 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002158 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002159 if (E->isTypeDependent() || E->isValueDependent() ||
2160 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002161 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002162 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002163 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002164 return;
2165 }
2166 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002167
John McCallb46f2872011-09-09 07:56:05 +00002168 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002169 // FIXME: This error message could be improved, it would be nice
2170 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002171 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2172 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002173 return;
2174 }
2175 }
2176
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002177 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002178 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002179 if (isa<FunctionNoProtoType>(FT)) {
2180 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2181 return;
2182 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002183
Chris Lattner9363e312009-03-17 23:03:47 +00002184 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002185 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002186 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002187 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002188 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002189 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002190 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002191 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002192 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002193 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2194 if (!BD->isVariadic()) {
2195 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2196 return;
2197 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002198 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002199 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002200 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002201 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002202 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002203 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002204 int m = Ty->isFunctionPointerType() ? 0 : 1;
2205 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002206 return;
2207 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002208 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002209 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002210 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002211 return;
2212 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002213 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002214 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002215 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002216 return;
2217 }
Michael Han99315932013-01-24 16:46:58 +00002218 D->addAttr(::new (S.Context)
2219 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2220 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002221}
2222
Chandler Carruthedc2c642011-07-02 00:01:44 +00002223static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002224 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002225 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002226 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002227 return;
2228 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002229
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002230 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2231 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2232 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002233 return;
2234 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002235 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2236 if (MD->getResultType()->isVoidType()) {
2237 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2238 << Attr.getName() << 1;
2239 return;
2240 }
2241
Michael Han99315932013-01-24 16:46:58 +00002242 D->addAttr(::new (S.Context)
2243 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2244 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002245}
2246
Chandler Carruthedc2c642011-07-02 00:01:44 +00002247static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002248 // weak_import only applies to variable & function declarations.
2249 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002250 if (!D->canBeWeakImported(isDef)) {
2251 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002252 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2253 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002254 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002255 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002256 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002257 // Nothing to warn about here.
2258 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002259 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002260 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002261
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002262 return;
2263 }
2264
Michael Han99315932013-01-24 16:46:58 +00002265 D->addAttr(::new (S.Context)
2266 WeakImportAttr(Attr.getRange(), S.Context,
2267 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002268}
2269
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002270// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002271template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002272static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002273 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002274 uint32_t WGSize[3];
2275 for (unsigned i = 0; i < 3; ++i)
2276 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002277 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002278
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002279 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2280 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2281 Existing->getYDim() == WGSize[1] &&
2282 Existing->getZDim() == WGSize[2]))
2283 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002284
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002285 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2286 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002287 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002288}
2289
Joey Goulyaba589c2013-03-08 09:42:32 +00002290static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002291 if (!Attr.hasParsedType()) {
2292 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2293 << Attr.getName() << 1;
2294 return;
2295 }
2296
Richard Smithb87c4652013-10-31 21:23:20 +00002297 TypeSourceInfo *ParmTSI = 0;
2298 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2299 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002300
2301 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2302 (ParmType->isBooleanType() ||
2303 !ParmType->isIntegralType(S.getASTContext()))) {
2304 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2305 << ParmType;
2306 return;
2307 }
2308
Aaron Ballmana9e05402013-12-02 22:16:55 +00002309 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002310 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002311 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2312 return;
2313 }
2314 }
2315
2316 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Richard Smithb87c4652013-10-31 21:23:20 +00002317 ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002318}
2319
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002320SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002321 StringRef Name,
2322 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002323 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2324 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002325 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002326 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2327 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002328 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002329 }
Michael Han99315932013-01-24 16:46:58 +00002330 return ::new (Context) SectionAttr(Range, Context, Name,
2331 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002332}
2333
Chandler Carruthedc2c642011-07-02 00:01:44 +00002334static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002335 // Make sure that there is a string literal as the sections's single
2336 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002337 StringRef Str;
2338 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002339 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002340 return;
Mike Stump11289f42009-09-09 15:08:12 +00002341
Chris Lattner30ba6742009-08-10 19:03:04 +00002342 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002343 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002344 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002345 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002346 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002347 return;
2348 }
Mike Stump11289f42009-09-09 15:08:12 +00002349
Michael Han99315932013-01-24 16:46:58 +00002350 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002351 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002352 if (NewAttr)
2353 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002354}
2355
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002356
Chandler Carruthedc2c642011-07-02 00:01:44 +00002357static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002358 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002359 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002360 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002361 } else {
Michael Han99315932013-01-24 16:46:58 +00002362 D->addAttr(::new (S.Context)
2363 NoThrowAttr(Attr.getRange(), S.Context,
2364 Attr.getAttributeSpellingListIndex()));
Douglas Gregor88336832011-06-15 05:45:11 +00002365 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002366}
2367
Chandler Carruthedc2c642011-07-02 00:01:44 +00002368static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002369 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002370 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002371 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002372 } else {
Michael Han99315932013-01-24 16:46:58 +00002373 D->addAttr(::new (S.Context)
2374 ConstAttr(Attr.getRange(), S.Context,
2375 Attr.getAttributeSpellingListIndex() ));
Douglas Gregor88336832011-06-15 05:45:11 +00002376 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002377}
2378
Chandler Carruthedc2c642011-07-02 00:01:44 +00002379static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002380 VarDecl *VD = cast<VarDecl>(D);
2381 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002382 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002383 return;
2384 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002385
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002386 Expr *E = Attr.getArgAsExpr(0);
2387 SourceLocation Loc = E->getExprLoc();
2388 FunctionDecl *FD = 0;
2389 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002390
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002391 // gcc only allows for simple identifiers. Since we support more than gcc, we
2392 // will warn the user.
2393 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2394 if (DRE->hasQualifier())
2395 S.Diag(Loc, diag::warn_cleanup_ext);
2396 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2397 NI = DRE->getNameInfo();
2398 if (!FD) {
2399 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2400 << NI.getName();
2401 return;
2402 }
2403 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2404 if (ULE->hasExplicitTemplateArgs())
2405 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002406 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2407 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002408 if (!FD) {
2409 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2410 << NI.getName();
2411 if (ULE->getType() == S.Context.OverloadTy)
2412 S.NoteAllOverloadCandidates(ULE);
2413 return;
2414 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002415 } else {
2416 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002417 return;
2418 }
2419
Anders Carlssond277d792009-01-31 01:16:18 +00002420 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002421 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2422 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002423 return;
2424 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002425
Anders Carlsson723f55d2009-02-07 23:16:50 +00002426 // We're currently more strict than GCC about what function types we accept.
2427 // If this ever proves to be a problem it should be easy to fix.
2428 QualType Ty = S.Context.getPointerType(VD->getType());
2429 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002430 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2431 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002432 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2433 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002434 return;
2435 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002436
Michael Han99315932013-01-24 16:46:58 +00002437 D->addAttr(::new (S.Context)
2438 CleanupAttr(Attr.getRange(), S.Context, FD,
2439 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002440}
2441
Mike Stumpd3bb5572009-07-24 19:02:52 +00002442/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002443/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002444static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002445 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002446 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002447 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002448 return;
2449 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002450
Aaron Ballman00e99962013-08-31 01:11:41 +00002451 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002452 uint64_t ArgIdx;
2453 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2454 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002455 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002456
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002457 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002458 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002459
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002460 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2461 if (not_nsstring_type &&
2462 !isCFStringType(Ty, S.Context) &&
2463 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002464 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002465 // FIXME: Should highlight the actual expression that has the wrong type.
2466 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002467 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002468 << IdxExpr->getSourceRange();
2469 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002470 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002471 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002472 if (!isNSStringType(Ty, S.Context) &&
2473 !isCFStringType(Ty, S.Context) &&
2474 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002475 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002476 // FIXME: Should highlight the actual expression that has the wrong type.
2477 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002478 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002479 << IdxExpr->getSourceRange();
2480 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002481 }
2482
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002483 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2484 // because that has corrected for the implicit this parameter, and is zero-
2485 // based. The attribute expects what the user wrote explicitly.
2486 llvm::APSInt Val;
2487 IdxExpr->EvaluateAsInt(Val, S.Context);
2488
Michael Han99315932013-01-24 16:46:58 +00002489 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002490 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002491 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002492}
2493
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002494enum FormatAttrKind {
2495 CFStringFormat,
2496 NSStringFormat,
2497 StrftimeFormat,
2498 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002499 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002500 InvalidFormat
2501};
2502
2503/// getFormatAttrKind - Map from format attribute names to supported format
2504/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002505static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002506 return llvm::StringSwitch<FormatAttrKind>(Format)
2507 // Check for formats that get handled specially.
2508 .Case("NSString", NSStringFormat)
2509 .Case("CFString", CFStringFormat)
2510 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002511
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002512 // Otherwise, check for supported formats.
2513 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2514 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2515 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002516
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002517 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2518 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002519}
2520
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002521/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002522/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002523static void handleInitPriorityAttr(Sema &S, Decl *D,
2524 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002525 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002526 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2527 return;
2528 }
2529
Aaron Ballman4a611152013-11-27 16:34:09 +00002530 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002531 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2532 Attr.setInvalid();
2533 return;
2534 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002535 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002536 if (S.Context.getAsArrayType(T))
2537 T = S.Context.getBaseElementType(T);
2538 if (!T->getAs<RecordType>()) {
2539 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2540 Attr.setInvalid();
2541 return;
2542 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002543
2544 Expr *E = Attr.getArgAsExpr(0);
2545 uint32_t prioritynum;
2546 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002547 Attr.setInvalid();
2548 return;
2549 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002550
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002551 if (prioritynum < 101 || prioritynum > 65535) {
2552 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002553 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002554 Attr.setInvalid();
2555 return;
2556 }
Michael Han99315932013-01-24 16:46:58 +00002557 D->addAttr(::new (S.Context)
2558 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2559 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002560}
2561
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002562FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2563 IdentifierInfo *Format, int FormatIdx,
2564 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002565 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002566 // Check whether we already have an equivalent format attribute.
2567 for (specific_attr_iterator<FormatAttr>
2568 i = D->specific_attr_begin<FormatAttr>(),
2569 e = D->specific_attr_end<FormatAttr>();
2570 i != e ; ++i) {
2571 FormatAttr *f = *i;
2572 if (f->getType() == Format &&
2573 f->getFormatIdx() == FormatIdx &&
2574 f->getFirstArg() == FirstArg) {
2575 // If we don't have a valid location for this attribute, adopt the
2576 // location.
2577 if (f->getLocation().isInvalid())
2578 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002579 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002580 }
2581 }
2582
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002583 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2584 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002585}
2586
Mike Stumpd3bb5572009-07-24 19:02:52 +00002587/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002588/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002589static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002590 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002591 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002592 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002593 return;
2594 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002595
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002596 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002597 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002598 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002599 return;
2600 }
2601
Chandler Carruth743682b2010-11-16 08:35:43 +00002602 // In C++ the implicit 'this' function parameter also counts, and they are
2603 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002604 bool HasImplicitThisParam = isInstanceMethod(D);
2605 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002606
Aaron Ballman00e99962013-08-31 01:11:41 +00002607 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2608 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002609
2610 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002611 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002612 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002613 // If we've modified the string name, we need a new identifier for it.
2614 II = &S.Context.Idents.get(Format);
2615 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002616
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002617 // Check for supported formats.
2618 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002619
2620 if (Kind == IgnoredFormat)
2621 return;
2622
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002623 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002624 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman00e99962013-08-31 01:11:41 +00002625 << "format" << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002626 return;
2627 }
2628
2629 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002630 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002631 uint32_t Idx;
2632 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002633 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002634
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002635 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002636 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002637 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002638 return;
2639 }
2640
2641 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002642 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002643
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002644 if (HasImplicitThisParam) {
2645 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002646 S.Diag(Attr.getLoc(),
2647 diag::err_format_attribute_implicit_this_format_string)
2648 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002649 return;
2650 }
2651 ArgIdx--;
2652 }
Mike Stump11289f42009-09-09 15:08:12 +00002653
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002654 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002655 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002656
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002657 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002658 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002659 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2660 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002661 return;
2662 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002663 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002664 // FIXME: do we need to check if the type is NSString*? What are the
2665 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002666 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002667 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002668 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2669 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002670 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002671 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002672 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002673 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002674 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002675 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2676 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002677 return;
2678 }
2679
2680 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002681 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002682 uint32_t FirstArg;
2683 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002684 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002685
2686 // check if the function is variadic if the 3rd argument non-zero
2687 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002688 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002689 ++NumArgs; // +1 for ...
2690 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002691 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002692 return;
2693 }
2694 }
2695
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002696 // strftime requires FirstArg to be 0 because it doesn't read from any
2697 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002698 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002699 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002700 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2701 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002702 return;
2703 }
2704 // if 0 it disables parameter checking (to use with e.g. va_list)
2705 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002706 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002707 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002708 return;
2709 }
2710
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002711 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002712 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002713 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002714 if (NewAttr)
2715 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002716}
2717
Chandler Carruthedc2c642011-07-02 00:01:44 +00002718static void handleTransparentUnionAttr(Sema &S, Decl *D,
2719 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002720 // Try to find the underlying union declaration.
2721 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002722 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002723 if (TD && TD->getUnderlyingType()->isUnionType())
2724 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2725 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002726 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002727
2728 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002729 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002730 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002731 return;
2732 }
2733
John McCallf937c022011-10-07 06:10:15 +00002734 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002735 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002736 diag::warn_transparent_union_attribute_not_definition);
2737 return;
2738 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002739
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002740 RecordDecl::field_iterator Field = RD->field_begin(),
2741 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002742 if (Field == FieldEnd) {
2743 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2744 return;
2745 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002746
David Blaikie40ed2972012-06-06 20:45:41 +00002747 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002748 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002749 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002750 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002751 diag::warn_transparent_union_attribute_floating)
2752 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002753 return;
2754 }
2755
2756 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2757 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2758 for (; Field != FieldEnd; ++Field) {
2759 QualType FieldType = Field->getType();
2760 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2761 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2762 // Warn if we drop the attribute.
2763 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002764 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002765 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002766 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002767 diag::warn_transparent_union_attribute_field_size_align)
2768 << isSize << Field->getDeclName() << FieldBits;
2769 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002770 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002771 diag::note_transparent_union_first_field_size_align)
2772 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002773 return;
2774 }
2775 }
2776
Michael Han99315932013-01-24 16:46:58 +00002777 RD->addAttr(::new (S.Context)
2778 TransparentUnionAttr(Attr.getRange(), S.Context,
2779 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002780}
2781
Chandler Carruthedc2c642011-07-02 00:01:44 +00002782static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002783 // Make sure that there is a string literal as the annotation's single
2784 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002785 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002786 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002787 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002788
2789 // Don't duplicate annotations that are already set.
2790 for (specific_attr_iterator<AnnotateAttr>
2791 i = D->specific_attr_begin<AnnotateAttr>(),
2792 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002793 if ((*i)->getAnnotation() == Str)
2794 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002795 }
Michael Han99315932013-01-24 16:46:58 +00002796
2797 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002798 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002799 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002800}
2801
Chandler Carruthedc2c642011-07-02 00:01:44 +00002802static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002803 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002804 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002805 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2806 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002807 return;
2808 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002809
Richard Smith848e1f12013-02-01 08:12:08 +00002810 if (Attr.getNumArgs() == 0) {
2811 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2812 true, 0, Attr.getAttributeSpellingListIndex()));
2813 return;
2814 }
2815
Aaron Ballman00e99962013-08-31 01:11:41 +00002816 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002817 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2818 S.Diag(Attr.getEllipsisLoc(),
2819 diag::err_pack_expansion_without_parameter_packs);
2820 return;
2821 }
2822
2823 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2824 return;
2825
2826 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2827 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002828}
2829
2830void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002831 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002832 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2833 SourceLocation AttrLoc = AttrRange.getBegin();
2834
Richard Smith1dba27c2013-01-29 09:02:09 +00002835 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002836 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002837 // C++11 [dcl.align]p1:
2838 // An alignment-specifier may be applied to a variable or to a class
2839 // data member, but it shall not be applied to a bit-field, a function
2840 // parameter, the formal parameter of a catch clause, or a variable
2841 // declared with the register storage class specifier. An
2842 // alignment-specifier may also be applied to the declaration of a class
2843 // or enumeration type.
2844 // C11 6.7.5/2:
2845 // An alignment attribute shall not be specified in a declaration of
2846 // a typedef, or a bit-field, or a function, or a parameter, or an
2847 // object declared with the register storage-class specifier.
2848 int DiagKind = -1;
2849 if (isa<ParmVarDecl>(D)) {
2850 DiagKind = 0;
2851 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2852 if (VD->getStorageClass() == SC_Register)
2853 DiagKind = 1;
2854 if (VD->isExceptionVariable())
2855 DiagKind = 2;
2856 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2857 if (FD->isBitField())
2858 DiagKind = 3;
2859 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00002860 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
2861 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00002862 << (TmpAttr.isC11() ? ExpectedVariableOrField
2863 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002864 return;
2865 }
2866 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002867 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smithbc8caaf2013-02-22 04:55:39 +00002868 << TmpAttr.isC11() << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002869 return;
2870 }
2871 }
2872
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002873 if (E->isTypeDependent() || E->isValueDependent()) {
2874 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002875 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2876 AA->setPackExpansion(IsPackExpansion);
2877 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002878 return;
2879 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002880
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002881 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002882 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002883 ExprResult ICE
2884 = VerifyIntegerConstantExpression(E, &Alignment,
2885 diag::err_aligned_attribute_argument_not_int,
2886 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002887 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002888 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002889
2890 // C++11 [dcl.align]p2:
2891 // -- if the constant expression evaluates to zero, the alignment
2892 // specifier shall have no effect
2893 // C11 6.7.5p6:
2894 // An alignment specification of zero has no effect.
2895 if (!(TmpAttr.isAlignas() && !Alignment) &&
2896 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002897 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2898 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002899 return;
2900 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002901
Richard Smith848e1f12013-02-01 08:12:08 +00002902 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00002903 // We've already verified it's a power of 2, now let's make sure it's
2904 // 8192 or less.
2905 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00002906 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00002907 << E->getSourceRange();
2908 return;
2909 }
2910 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002911
Richard Smith44c247f2013-02-22 08:32:16 +00002912 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
2913 ICE.take(), SpellingListIndex);
2914 AA->setPackExpansion(IsPackExpansion);
2915 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002916}
2917
Michael Hanaf02bbe2013-02-01 01:19:17 +00002918void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002919 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002920 // FIXME: Cache the number on the Attr object if non-dependent?
2921 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002922 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2923 SpellingListIndex);
2924 AA->setPackExpansion(IsPackExpansion);
2925 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002926}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002927
Richard Smith848e1f12013-02-01 08:12:08 +00002928void Sema::CheckAlignasUnderalignment(Decl *D) {
2929 assert(D->hasAttrs() && "no attributes on decl");
2930
2931 QualType Ty;
2932 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2933 Ty = VD->getType();
2934 else
2935 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002936 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002937 return;
2938
2939 // C++11 [dcl.align]p5, C11 6.7.5/4:
2940 // The combined effect of all alignment attributes in a declaration shall
2941 // not specify an alignment that is less strict than the alignment that
2942 // would otherwise be required for the entity being declared.
2943 AlignedAttr *AlignasAttr = 0;
2944 unsigned Align = 0;
2945 for (specific_attr_iterator<AlignedAttr>
2946 I = D->specific_attr_begin<AlignedAttr>(),
2947 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
2948 if (I->isAlignmentDependent())
2949 return;
2950 if (I->isAlignas())
2951 AlignasAttr = *I;
2952 Align = std::max(Align, I->getAlignment(Context));
2953 }
2954
2955 if (AlignasAttr && Align) {
2956 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2957 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2958 if (NaturalAlign > RequestedAlign)
2959 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2960 << Ty << (unsigned)NaturalAlign.getQuantity();
2961 }
2962}
2963
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002964/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002965/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002966///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002967/// Despite what would be logical, the mode attribute is a decl attribute, not a
2968/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2969/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002970static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002971 // This attribute isn't documented, but glibc uses it. It changes
2972 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00002973 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00002974 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
2975 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002976 return;
2977 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002978
Aaron Ballman00e99962013-08-31 01:11:41 +00002979 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
2980 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002981
2982 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002983 if (Str.startswith("__") && Str.endswith("__"))
2984 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002985
2986 unsigned DestWidth = 0;
2987 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002988 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002989 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002990 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002991 switch (Str[0]) {
2992 case 'Q': DestWidth = 8; break;
2993 case 'H': DestWidth = 16; break;
2994 case 'S': DestWidth = 32; break;
2995 case 'D': DestWidth = 64; break;
2996 case 'X': DestWidth = 96; break;
2997 case 'T': DestWidth = 128; break;
2998 }
2999 if (Str[1] == 'F') {
3000 IntegerMode = false;
3001 } else if (Str[1] == 'C') {
3002 IntegerMode = false;
3003 ComplexMode = true;
3004 } else if (Str[1] != 'I') {
3005 DestWidth = 0;
3006 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003007 break;
3008 case 4:
3009 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3010 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003011 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003012 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003013 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003014 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003015 break;
3016 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003017 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003018 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003019 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003020 case 11:
3021 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003022 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003023 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003024 }
3025
3026 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003027 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003028 OldTy = TD->getUnderlyingType();
3029 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3030 OldTy = VD->getType();
3031 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003032 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003033 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003034 return;
3035 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003036
John McCall9dd450b2009-09-21 23:43:11 +00003037 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003038 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3039 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003040 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003041 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3042 } else if (ComplexMode) {
3043 if (!OldTy->isComplexType())
3044 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3045 } else {
3046 if (!OldTy->isFloatingType())
3047 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3048 }
3049
Mike Stump87c57ac2009-05-16 07:39:55 +00003050 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3051 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003052 // FIXME: Make sure floating-point mappings are accurate
3053 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003054 if (!DestWidth) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003055 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003056 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003057 }
3058
3059 QualType NewTy;
3060
3061 if (IntegerMode)
3062 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3063 OldTy->isSignedIntegerType());
3064 else
3065 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3066
3067 if (NewTy.isNull()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003068 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003069 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003070 }
3071
Eli Friedman4735374e2009-03-03 06:41:03 +00003072 if (ComplexMode) {
3073 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003074 }
3075
3076 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003077 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3078 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3079 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003080 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003081
3082 D->addAttr(::new (S.Context)
3083 ModeAttr(Attr.getRange(), S.Context, Name,
3084 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003085}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003086
Chandler Carruthedc2c642011-07-02 00:01:44 +00003087static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003088 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3089 if (!VD->hasGlobalStorage())
3090 S.Diag(Attr.getLoc(),
3091 diag::warn_attribute_requires_functions_or_static_globals)
3092 << Attr.getName();
3093 } else if (!isFunctionOrMethod(D)) {
3094 S.Diag(Attr.getLoc(),
3095 diag::warn_attribute_requires_functions_or_static_globals)
3096 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003097 return;
3098 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003099
Michael Han99315932013-01-24 16:46:58 +00003100 D->addAttr(::new (S.Context)
3101 NoDebugAttr(Attr.getRange(), S.Context,
3102 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003103}
3104
Chandler Carruthedc2c642011-07-02 00:01:44 +00003105static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003106 FunctionDecl *FD = cast<FunctionDecl>(D);
3107 if (!FD->getResultType()->isVoidType()) {
3108 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
3109 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
3110 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3111 << FD->getType()
3112 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
3113 "void");
3114 } else {
3115 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3116 << FD->getType();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003117 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003118 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003119 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003120
Aaron Ballman3aff6332013-12-02 19:30:36 +00003121 D->addAttr(::new (S.Context)
3122 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00003123 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003124}
3125
Chandler Carruthedc2c642011-07-02 00:01:44 +00003126static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003127 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003128 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003129 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003130 return;
3131 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003132
Michael Han99315932013-01-24 16:46:58 +00003133 D->addAttr(::new (S.Context)
3134 GNUInlineAttr(Attr.getRange(), S.Context,
3135 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003136}
3137
Chandler Carruthedc2c642011-07-02 00:01:44 +00003138static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003139 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003140
Aaron Ballman02df2e02012-12-09 17:45:41 +00003141 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003142 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003143 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3144 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003145 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003146 return;
3147
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003148 if (!isa<ObjCMethodDecl>(D)) {
3149 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3150 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003151 return;
3152 }
3153
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003154 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003155 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003156 D->addAttr(::new (S.Context)
3157 FastCallAttr(Attr.getRange(), S.Context,
3158 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003159 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003160 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003161 D->addAttr(::new (S.Context)
3162 StdCallAttr(Attr.getRange(), S.Context,
3163 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003164 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003165 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003166 D->addAttr(::new (S.Context)
3167 ThisCallAttr(Attr.getRange(), S.Context,
3168 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003169 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003170 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003171 D->addAttr(::new (S.Context)
3172 CDeclAttr(Attr.getRange(), S.Context,
3173 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003174 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003175 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003176 D->addAttr(::new (S.Context)
3177 PascalAttr(Attr.getRange(), S.Context,
3178 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003179 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003180 case AttributeList::AT_MSABI:
3181 D->addAttr(::new (S.Context)
3182 MSABIAttr(Attr.getRange(), S.Context,
3183 Attr.getAttributeSpellingListIndex()));
3184 return;
3185 case AttributeList::AT_SysVABI:
3186 D->addAttr(::new (S.Context)
3187 SysVABIAttr(Attr.getRange(), S.Context,
3188 Attr.getAttributeSpellingListIndex()));
3189 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003190 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003191 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003192 switch (CC) {
3193 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003194 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003195 break;
3196 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003197 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003198 break;
3199 default:
3200 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003201 }
3202
Michael Han99315932013-01-24 16:46:58 +00003203 D->addAttr(::new (S.Context)
3204 PcsAttr(Attr.getRange(), S.Context, PCS,
3205 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003206 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003207 }
Derek Schuffa2020962012-10-16 22:30:41 +00003208 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003209 D->addAttr(::new (S.Context)
3210 PnaclCallAttr(Attr.getRange(), S.Context,
3211 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003212 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003213 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003214 D->addAttr(::new (S.Context)
3215 IntelOclBiccAttr(Attr.getRange(), S.Context,
3216 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003217 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003218
Abramo Bagnara50099372010-04-30 13:10:51 +00003219 default:
3220 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003221 }
3222}
3223
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003224static void handleOpenCLImageAccessAttr(Sema &S, Decl *D,
3225 const AttributeList &Attr) {
3226 uint32_t ArgNum;
3227 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), ArgNum))
Guy Benyeifb36ede2013-03-24 13:58:12 +00003228 return;
Guy Benyeifb36ede2013-03-24 13:58:12 +00003229
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003230 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(Attr.getRange(),
3231 S.Context, ArgNum));
Guy Benyeifb36ede2013-03-24 13:58:12 +00003232}
3233
Aaron Ballman02df2e02012-12-09 17:45:41 +00003234bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3235 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003236 if (attr.isInvalid())
3237 return true;
3238
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003239 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003240 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003241 attr.setInvalid();
3242 return true;
3243 }
3244
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003245 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3246 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003247 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003248 case AttributeList::AT_CDecl: CC = CC_C; break;
3249 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3250 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3251 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3252 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003253 case AttributeList::AT_MSABI:
3254 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3255 CC_X86_64Win64;
3256 break;
3257 case AttributeList::AT_SysVABI:
3258 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3259 CC_C;
3260 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003261 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003262 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003263 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003264 attr.setInvalid();
3265 return true;
3266 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003267 if (StrRef == "aapcs") {
3268 CC = CC_AAPCS;
3269 break;
3270 } else if (StrRef == "aapcs-vfp") {
3271 CC = CC_AAPCS_VFP;
3272 break;
3273 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003274
3275 attr.setInvalid();
3276 Diag(attr.getLoc(), diag::err_invalid_pcs);
3277 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003278 }
Derek Schuffa2020962012-10-16 22:30:41 +00003279 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003280 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003281 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003282 }
3283
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003284 const TargetInfo &TI = Context.getTargetInfo();
3285 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3286 if (A == TargetInfo::CCCR_Warning) {
3287 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003288
3289 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3290 if (FD)
3291 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3292 TargetInfo::CCMT_NonMember;
3293 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003294 }
3295
John McCall3882ace2011-01-05 12:14:39 +00003296 return false;
3297}
3298
Chandler Carruthedc2c642011-07-02 00:01:44 +00003299static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003300 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003301
3302 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003303 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003304 return;
3305
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003306 if (!isa<ObjCMethodDecl>(D)) {
3307 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3308 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003309 return;
3310 }
Eli Friedman7044b762009-03-27 21:06:47 +00003311
Michael Han99315932013-01-24 16:46:58 +00003312 D->addAttr(::new (S.Context)
3313 RegparmAttr(Attr.getRange(), S.Context, numParams,
3314 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00003315}
3316
3317/// Checks a regparm attribute, returning true if it is ill-formed and
3318/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003319bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3320 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003321 return true;
3322
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003323 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003324 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003325 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003326 }
Eli Friedman7044b762009-03-27 21:06:47 +00003327
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003328 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003329 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003330 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003331 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003332 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003333 }
3334
Douglas Gregore8bbc122011-09-02 00:18:52 +00003335 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003336 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003337 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003338 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003339 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003340 }
3341
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003342 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003343 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003344 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003345 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003346 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003347 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003348 }
3349
John McCall3882ace2011-01-05 12:14:39 +00003350 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003351}
3352
Aaron Ballman66039932013-12-19 00:41:31 +00003353static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3354 const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003355 // check the attribute arguments.
3356 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
3357 // FIXME: 0 is not okay.
3358 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
3359 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003360 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003361
3362 if (!isFunctionOrMethod(D)) {
3363 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3364 << Attr.getName() << ExpectedFunctionOrMethod;
3365 return;
3366 }
3367
Aaron Ballman66039932013-12-19 00:41:31 +00003368 uint32_t MaxThreads, MinBlocks = 0;
3369 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1))
3370 return;
3371 if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr,
3372 Attr.getArgAsExpr(1),
3373 MinBlocks, 2))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003374 return;
3375
3376 D->addAttr(::new (S.Context)
3377 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3378 MaxThreads, MinBlocks,
3379 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003380}
3381
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003382static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3383 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003384 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003385 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003386 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003387 return;
3388 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003389
3390 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003391 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003392
Aaron Ballman00e99962013-08-31 01:11:41 +00003393 StringRef AttrName = Attr.getName()->getName();
3394 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003395
3396 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3397 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3398 << Attr.getName() << ExpectedFunctionOrMethod;
3399 return;
3400 }
3401
3402 uint64_t ArgumentIdx;
3403 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3404 Attr.getLoc(), 2,
Aaron Ballman00e99962013-08-31 01:11:41 +00003405 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003406 return;
3407
3408 uint64_t TypeTagIdx;
3409 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3410 Attr.getLoc(), 3,
Aaron Ballman00e99962013-08-31 01:11:41 +00003411 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003412 return;
3413
3414 bool IsPointer = (AttrName == "pointer_with_type_tag");
3415 if (IsPointer) {
3416 // Ensure that buffer has a pointer type.
3417 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3418 if (!BufferTy->isPointerType()) {
3419 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003420 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003421 }
3422 }
3423
Michael Han99315932013-01-24 16:46:58 +00003424 D->addAttr(::new (S.Context)
3425 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3426 ArgumentIdx, TypeTagIdx, IsPointer,
3427 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003428}
3429
3430static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3431 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003432 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003433 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003434 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003435 return;
3436 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003437
3438 if (!checkAttributeNumArgs(S, Attr, 1))
3439 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003440
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003441 if (!isa<VarDecl>(D)) {
3442 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3443 << Attr.getName() << ExpectedVariable;
3444 return;
3445 }
3446
Aaron Ballman00e99962013-08-31 01:11:41 +00003447 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003448 TypeSourceInfo *MatchingCTypeLoc = 0;
3449 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3450 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003451
Michael Han99315932013-01-24 16:46:58 +00003452 D->addAttr(::new (S.Context)
3453 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003454 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003455 Attr.getLayoutCompatible(),
3456 Attr.getMustBeNull(),
3457 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003458}
3459
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003460//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003461// Checker-specific attribute handlers.
3462//===----------------------------------------------------------------------===//
3463
John McCalled433932011-01-25 03:31:58 +00003464static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003465 return type->isDependentType() ||
3466 type->isObjCObjectPointerType() ||
3467 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003468}
3469static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003470 return type->isDependentType() ||
3471 type->isPointerType() ||
3472 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003473}
3474
Chandler Carruthedc2c642011-07-02 00:01:44 +00003475static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003476 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003477 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003478
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003479 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003480 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3481 cf = false;
3482 } else {
3483 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3484 cf = true;
3485 }
3486
3487 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003488 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003489 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003490 return;
3491 }
3492
3493 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003494 param->addAttr(::new (S.Context)
3495 CFConsumedAttr(Attr.getRange(), S.Context,
3496 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003497 else
Michael Han99315932013-01-24 16:46:58 +00003498 param->addAttr(::new (S.Context)
3499 NSConsumedAttr(Attr.getRange(), S.Context,
3500 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003501}
3502
Chandler Carruthedc2c642011-07-02 00:01:44 +00003503static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3504 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003505
John McCalled433932011-01-25 03:31:58 +00003506 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003507
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003508 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003509 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003510 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003511 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003512 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003513 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3514 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003515 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003516 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003517 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003518 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003519 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003520 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003521 return;
3522 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003523
John McCalled433932011-01-25 03:31:58 +00003524 bool typeOK;
3525 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003526 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003527 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003528 case AttributeList::AT_NSReturnsAutoreleased:
3529 case AttributeList::AT_NSReturnsRetained:
3530 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003531 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3532 cf = false;
3533 break;
3534
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003535 case AttributeList::AT_CFReturnsRetained:
3536 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003537 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3538 cf = true;
3539 break;
3540 }
3541
3542 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003543 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003544 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003545 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003546 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003547
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003548 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003549 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003550 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003551 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003552 D->addAttr(::new (S.Context)
3553 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3554 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003555 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003556 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003557 D->addAttr(::new (S.Context)
3558 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3559 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003560 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003561 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003562 D->addAttr(::new (S.Context)
3563 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3564 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003565 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003566 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003567 D->addAttr(::new (S.Context)
3568 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3569 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003570 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003571 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003572 D->addAttr(::new (S.Context)
3573 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3574 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003575 return;
3576 };
3577}
3578
John McCallcf166702011-07-22 08:53:00 +00003579static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3580 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003581 const int EP_ObjCMethod = 1;
3582 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003583
John McCallcf166702011-07-22 08:53:00 +00003584 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003585 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003586 if (isa<ObjCMethodDecl>(D))
3587 resultType = cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003588 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003589 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003590
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003591 if (!resultType->isReferenceType() &&
3592 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003593 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003594 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003595 << attr.getName()
3596 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003597 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003598
3599 // Drop the attribute.
3600 return;
3601 }
3602
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003603 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003604 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3605 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003606}
3607
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003608static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3609 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003610 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003611
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003612 DeclContext *DC = method->getDeclContext();
3613 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3614 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3615 << attr.getName() << 0;
3616 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3617 return;
3618 }
3619 if (method->getMethodFamily() == OMF_dealloc) {
3620 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3621 << attr.getName() << 1;
3622 return;
3623 }
3624
Michael Han99315932013-01-24 16:46:58 +00003625 method->addAttr(::new (S.Context)
3626 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3627 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003628}
3629
Aaron Ballmanfb763042013-12-02 18:05:46 +00003630static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3631 const AttributeList &Attr) {
3632 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr,
3633 "cf_unknown_transfer"))
John McCall32f5fe12011-09-30 05:12:12 +00003634 return;
John McCall32f5fe12011-09-30 05:12:12 +00003635
Aaron Ballmanfb763042013-12-02 18:05:46 +00003636 D->addAttr(::new (S.Context)
3637 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3638 Attr.getAttributeSpellingListIndex()));
3639}
3640
3641static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3642 const AttributeList &Attr) {
3643 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr,
3644 "cf_audited_transfer"))
3645 return;
3646
3647 D->addAttr(::new (S.Context)
3648 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3649 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003650}
3651
John McCallf1e8b342011-09-29 07:17:38 +00003652static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
3653 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003654 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallf1e8b342011-09-29 07:17:38 +00003655
3656 // In Objective-C, verify that the type names an Objective-C type.
3657 // We don't want to check this outside of ObjC because people sometimes
3658 // do crazy C declarations of Objective-C types.
Aaron Ballman00e99962013-08-31 01:11:41 +00003659 if (Parm && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00003660 // Check for an existing type with this name.
Aaron Ballman00e99962013-08-31 01:11:41 +00003661 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallf1e8b342011-09-29 07:17:38 +00003662 Sema::LookupOrdinaryName);
3663 if (S.LookupName(R, Sc)) {
3664 NamedDecl *Target = R.getFoundDecl();
3665 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
3666 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
3667 S.Diag(Target->getLocStart(), diag::note_declared_at);
3668 }
3669 }
3670 }
3671
Michael Han99315932013-01-24 16:46:58 +00003672 D->addAttr(::new (S.Context)
Aaron Ballman00e99962013-08-31 01:11:41 +00003673 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han99315932013-01-24 16:46:58 +00003674 Attr.getAttributeSpellingListIndex()));
John McCallf1e8b342011-09-29 07:17:38 +00003675}
3676
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003677static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3678 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003679 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003680
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003681 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003682 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003683 return;
3684 }
3685
3686 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003687 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003688 Attr.getAttributeSpellingListIndex()));
3689}
3690
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003691static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3692 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003693 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003694
3695 if (!Parm) {
3696 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3697 return;
3698 }
3699
3700 D->addAttr(::new (S.Context)
3701 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3702 Attr.getAttributeSpellingListIndex()));
3703}
3704
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003705static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3706 const AttributeList &Attr) {
3707 IdentifierInfo *RelatedClass =
3708 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0;
3709 if (!RelatedClass) {
3710 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3711 return;
3712 }
3713 IdentifierInfo *ClassMethod =
3714 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0;
3715 IdentifierInfo *InstanceMethod =
3716 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0;
3717 D->addAttr(::new (S.Context)
3718 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3719 ClassMethod, InstanceMethod,
3720 Attr.getAttributeSpellingListIndex()));
3721}
3722
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003723static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3724 const AttributeList &Attr) {
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003725 ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003726 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003727 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003728 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3729 Attr.getAttributeSpellingListIndex()));
3730}
3731
Chandler Carruthedc2c642011-07-02 00:01:44 +00003732static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3733 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003734 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003735
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003736 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003737 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003738}
3739
Chandler Carruthedc2c642011-07-02 00:01:44 +00003740static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3741 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003742 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003743 QualType type = vd->getType();
3744
3745 if (!type->isDependentType() &&
3746 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003747 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003748 << type;
3749 return;
3750 }
3751
3752 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3753
3754 // If we have no lifetime yet, check the lifetime we're presumably
3755 // going to infer.
3756 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3757 lifetime = type->getObjCARCImplicitLifetime();
3758
3759 switch (lifetime) {
3760 case Qualifiers::OCL_None:
3761 assert(type->isDependentType() &&
3762 "didn't infer lifetime for non-dependent type?");
3763 break;
3764
3765 case Qualifiers::OCL_Weak: // meaningful
3766 case Qualifiers::OCL_Strong: // meaningful
3767 break;
3768
3769 case Qualifiers::OCL_ExplicitNone:
3770 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003771 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003772 << (lifetime == Qualifiers::OCL_Autoreleasing);
3773 break;
3774 }
3775
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003776 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003777 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3778 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003779}
3780
Francois Picheta83957a2010-12-19 06:50:37 +00003781//===----------------------------------------------------------------------===//
3782// Microsoft specific attribute handlers.
3783//===----------------------------------------------------------------------===//
3784
Chandler Carruthedc2c642011-07-02 00:01:44 +00003785static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003786 if (!S.LangOpts.CPlusPlus) {
3787 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3788 << Attr.getName() << AttributeLangSupport::C;
3789 return;
3790 }
3791
Aaron Ballman60e705e2013-11-24 20:58:02 +00003792 if (!isa<CXXRecordDecl>(D)) {
3793 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3794 << Attr.getName() << ExpectedClass;
3795 return;
3796 }
3797
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003798 StringRef StrRef;
3799 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003800 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003801 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003802
David Majnemer89085342013-08-09 08:56:20 +00003803 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3804 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003805 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3806 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003807
Reid Kleckner140c4a72013-05-17 14:04:52 +00003808 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003809 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003810 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003811 return;
3812 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003813
David Majnemer89085342013-08-09 08:56:20 +00003814 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003815 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003816 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003817 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003818 return;
3819 }
David Majnemer89085342013-08-09 08:56:20 +00003820 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003821 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003822 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003823 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003824 }
Francois Picheta83957a2010-12-19 06:50:37 +00003825
David Majnemer89085342013-08-09 08:56:20 +00003826 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3827 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003828}
3829
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003830/// Handles semantic checking for features that are common to all attributes,
3831/// such as checking whether a parameter was properly specified, or the correct
3832/// number of arguments were passed, etc.
3833static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
3834 const AttributeList &Attr) {
3835 // Several attributes carry different semantics than the parsing requires, so
3836 // those are opted out of the common handling.
3837 //
3838 // We also bail on unknown and ignored attributes because those are handled
3839 // as part of the target-specific handling logic.
3840 if (Attr.hasCustomParsing() ||
3841 Attr.getKind() == AttributeList::UnknownAttribute ||
3842 Attr.getKind() == AttributeList::IgnoredAttribute)
3843 return false;
3844
Aaron Ballman3aff6332013-12-02 19:30:36 +00003845 // Check whether the attribute requires specific language extensions to be
3846 // enabled.
3847 if (!Attr.diagnoseLangOpts(S))
3848 return true;
3849
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003850 // If there are no optional arguments, then checking for the argument count
3851 // is trivial.
3852 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
3853 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
3854 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003855
3856 // Check whether the attribute appertains to the given subject.
3857 if (!Attr.diagnoseAppertainsTo(S, D))
3858 return true;
3859
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003860 return false;
3861}
3862
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003863//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003864// Top Level Sema Entry Points
3865//===----------------------------------------------------------------------===//
3866
Richard Smithf8a75c32013-08-29 00:47:48 +00003867/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3868/// the attribute applies to decls. If the attribute is a type attribute, just
3869/// silently ignore it if a GNU attribute.
3870static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3871 const AttributeList &Attr,
3872 bool IncludeCXX11Attributes) {
3873 if (Attr.isInvalid())
3874 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003875
Richard Smithf8a75c32013-08-29 00:47:48 +00003876 // Ignore C++11 attributes on declarator chunks: they appertain to the type
3877 // instead.
3878 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
3879 return;
3880
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003881 if (handleCommonAttributeFeatures(S, scope, D, Attr))
3882 return;
3883
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003884 switch (Attr.getKind()) {
Aaron Ballman9beb5172013-12-02 15:13:14 +00003885 case AttributeList::AT_IBAction:
3886 handleSimpleAttribute<IBActionAttr>(S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003887 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
3888 case AttributeList::AT_IBOutletCollection:
3889 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003890 case AttributeList::AT_AddressSpace:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003891 case AttributeList::AT_ObjCGC:
3892 case AttributeList::AT_VectorSize:
3893 case AttributeList::AT_NeonVectorType:
3894 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballman317a77f2013-05-22 23:25:32 +00003895 case AttributeList::AT_Ptr32:
3896 case AttributeList::AT_Ptr64:
3897 case AttributeList::AT_SPtr:
3898 case AttributeList::AT_UPtr:
Mike Stumpd3bb5572009-07-24 19:02:52 +00003899 // Ignore these, these are type attributes, handled by
3900 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003901 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003902 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
3903 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
3904 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
3905 case AttributeList::AT_AlwaysInline:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003906 handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003907 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003908 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00003909 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003910 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
3911 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
3912 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00003913 handleDependencyAttr(S, scope, D, Attr);
3914 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003915 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003916 case AttributeList::AT_CUDAConstant:
3917 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003918 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003919 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman3a8e2d92013-11-27 18:53:58 +00003920 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003921 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00003922 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00003923 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003924 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
3925 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003926 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003927 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00003928 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003929 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00003930 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003931 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
3932 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
3933 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00003934 case AttributeList::AT_CUDADevice:
3935 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003936 case AttributeList::AT_CUDAHost:
3937 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003938 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
3939 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003940 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00003941 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003942 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003943 case AttributeList::AT_MayAlias:
3944 handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00003945 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003946 case AttributeList::AT_NoCommon:
3947 handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003948 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003949 case AttributeList::AT_Overloadable:
3950 handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break;
Richard Smith852e9ce2013-11-27 01:46:48 +00003951 case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003952 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
3953 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003954 case AttributeList::AT_Naked:
3955 handleSimpleAttribute<NakedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003956 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
3957 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003958 case AttributeList::AT_CUDAShared:
3959 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003960 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003961
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003962 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003963 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003964 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003965 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003966
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003967 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00003968 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3969
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003970 case AttributeList::AT_ObjCRequiresSuper:
3971 handleObjCRequiresSuperAttr(S, D, Attr); break;
3972
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003973 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00003974 handleNSBridgedAttr(S, scope, D, Attr); break;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003975
3976 case AttributeList::AT_ObjCBridge:
3977 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003978
3979 case AttributeList::AT_ObjCBridgeMutable:
3980 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003981
3982 case AttributeList::AT_ObjCBridgeRelated:
3983 handleObjCBridgeRelatedAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00003984
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003985 case AttributeList::AT_ObjCDesignatedInitializer:
3986 handleObjCDesignatedInitializer(S, D, Attr); break;
3987
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003988 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00003989 handleCFAuditedTransferAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003990 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00003991 handleCFUnknownTransferAttr(S, D, Attr); break;
John McCall32f5fe12011-09-30 05:12:12 +00003992
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003993 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003994 case AttributeList::AT_CFConsumed:
3995 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
3996 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003997 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00003998
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003999 case AttributeList::AT_NSReturnsAutoreleased:
4000 case AttributeList::AT_NSReturnsNotRetained:
4001 case AttributeList::AT_CFReturnsNotRetained:
4002 case AttributeList::AT_NSReturnsRetained:
4003 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004004 handleNSReturnsRetainedAttr(S, D, Attr); break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004005 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00004006 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004007 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00004008 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004009 case AttributeList::AT_VecTypeHint:
4010 handleVecTypeHint(S, D, Attr); break;
4011
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004012 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004013 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004014
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004015 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4016 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4017 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004018 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004019 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004020 case AttributeList::AT_ArcWeakrefUnavailable:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004021 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004022 case AttributeList::AT_ObjCRootClass:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004023 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00004024 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek28eace62013-11-23 01:01:34 +00004025 handleObjCSuppresProtocolAttr(S, D, Attr);
4026 break;
4027 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004028 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004029 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4030 case AttributeList::AT_ReturnsTwice:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004031 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004032 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004033 case AttributeList::AT_Visibility:
4034 handleVisibilityAttr(S, D, Attr, false);
4035 break;
4036 case AttributeList::AT_TypeVisibility:
4037 handleVisibilityAttr(S, D, Attr, true);
4038 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004039 case AttributeList::AT_WarnUnused:
Aaron Ballman6a42b5a2013-11-27 16:59:17 +00004040 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004041 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004042 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004043 case AttributeList::AT_Weak:
4044 handleSimpleAttribute<WeakAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004045 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4046 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4047 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004048 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004049 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004050 case AttributeList::AT_ObjCException:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004051 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004052 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004053 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004054 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004055 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4056 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4057 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4058 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004059 case AttributeList::AT_Pure:
4060 handleSimpleAttribute<PureAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004061 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4062 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004063 case AttributeList::AT_NoInline:
4064 handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004065 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004066 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004067 // Just ignore
4068 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004069 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004070 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004071 case AttributeList::AT_StdCall:
4072 case AttributeList::AT_CDecl:
4073 case AttributeList::AT_FastCall:
4074 case AttributeList::AT_ThisCall:
4075 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004076 case AttributeList::AT_MSABI:
4077 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004078 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004079 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004080 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004081 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004082 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004083 case AttributeList::AT_OpenCLKernel:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004084 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004085 case AttributeList::AT_OpenCLImageAccess:
4086 handleOpenCLImageAccessAttr(S, D, Attr);
4087 break;
John McCall8d32c052012-05-22 21:28:12 +00004088
4089 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004090 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004091 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004092 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004093 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004094 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004095 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004096 case AttributeList::AT_MSInheritance:
4097 handleSimpleAttribute<MSInheritanceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004098 case AttributeList::AT_ForceInline:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004099 handleSimpleAttribute<ForceInlineAttr>(S, D, Attr); break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004100 case AttributeList::AT_SelectAny:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004101 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004102
4103 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004104 case AttributeList::AT_AssertExclusiveLock:
4105 handleAssertExclusiveLockAttr(S, D, Attr);
4106 break;
4107 case AttributeList::AT_AssertSharedLock:
4108 handleAssertSharedLockAttr(S, D, Attr);
4109 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004110 case AttributeList::AT_GuardedVar:
Aaron Ballmane61b8b82013-12-02 15:02:49 +00004111 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004112 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004113 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004114 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004115 case AttributeList::AT_ScopedLockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004116 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004117 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004118 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004119 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004120 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004121 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004122 break;
4123 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004124 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004125 break;
4126 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004127 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004128 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004129 case AttributeList::AT_Lockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004130 handleSimpleAttribute<LockableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004131 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004132 handleGuardedByAttr(S, D, Attr);
4133 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004134 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004135 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004136 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004137 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004138 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004139 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004140 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004141 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004142 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004143 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004144 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004145 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004146 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004147 handleLockReturnedAttr(S, D, Attr);
4148 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004149 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004150 handleLocksExcludedAttr(S, D, Attr);
4151 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004152 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004153 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004154 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004155 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004156 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004157 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004158 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004159 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004160 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004161 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004162 handleUnlockFunAttr(S, D, Attr);
4163 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004164 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004165 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004166 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004167 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004168 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004169 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004170
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004171 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004172 case AttributeList::AT_Consumable:
4173 handleConsumableAttr(S, D, Attr);
4174 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004175 case AttributeList::AT_CallableWhen:
4176 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004177 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004178 case AttributeList::AT_ParamTypestate:
4179 handleParamTypestateAttr(S, D, Attr);
4180 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004181 case AttributeList::AT_ReturnTypestate:
4182 handleReturnTypestateAttr(S, D, Attr);
4183 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004184 case AttributeList::AT_SetTypestate:
4185 handleSetTypestateAttr(S, D, Attr);
4186 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004187 case AttributeList::AT_TestTypestate:
4188 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004189 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004190
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004191 // Type safety attributes.
4192 case AttributeList::AT_ArgumentWithTypeTag:
4193 handleArgumentWithTypeTagAttr(S, D, Attr);
4194 break;
4195 case AttributeList::AT_TypeTagForDatatype:
4196 handleTypeTagForDatatypeAttr(S, D, Attr);
4197 break;
4198
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004199 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004200 // Ask target about the attribute.
4201 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4202 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004203 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4204 diag::warn_unhandled_ms_attribute_ignored :
4205 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004206 break;
4207 }
4208}
4209
4210/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4211/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004212void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004213 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004214 bool IncludeCXX11Attributes) {
4215 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004216 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004217
Joey Gouly2cd9db12013-12-13 16:15:28 +00004218 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004219 // GCC accepts
4220 // static int a9 __attribute__((weakref));
4221 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004222 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004223 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00004224 cast<NamedDecl>(D)->getNameAsString();
4225 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004226 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004227 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004228
4229 if (!D->hasAttr<OpenCLKernelAttr>()) {
4230 // These attributes cannot be applied to a non-kernel function.
4231 if (D->hasAttr<ReqdWorkGroupSizeAttr>()) {
4232 Diag(D->getLocation(), diag::err_opencl_kernel_attr)
4233 << "reqd_work_group_size";
4234 D->setInvalidDecl();
4235 }
4236 if (D->hasAttr<WorkGroupSizeHintAttr>()) {
4237 Diag(D->getLocation(), diag::err_opencl_kernel_attr)
4238 << "work_group_size_hint";
4239 D->setInvalidDecl();
4240 }
4241 if (D->hasAttr<VecTypeHintAttr>()) {
4242 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << "vec_type_hint";
4243 D->setInvalidDecl();
4244 }
4245 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004246}
4247
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004248// Annotation attributes are the only attributes allowed after an access
4249// specifier.
4250bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4251 const AttributeList *AttrList) {
4252 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004253 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004254 handleAnnotateAttr(*this, ASDecl, *l);
4255 } else {
4256 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4257 return true;
4258 }
4259 }
4260
4261 return false;
4262}
4263
John McCall42856de2011-10-01 05:17:03 +00004264/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4265/// contains any decl attributes that we should warn about.
4266static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4267 for ( ; A; A = A->getNext()) {
4268 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004269 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004270 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4271
4272 if (A->getKind() == AttributeList::UnknownAttribute) {
4273 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4274 << A->getName() << A->getRange();
4275 } else {
4276 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4277 << A->getName() << A->getRange();
4278 }
4279 }
4280}
4281
4282/// checkUnusedDeclAttributes - Given a declarator which is not being
4283/// used to build a declaration, complain about any decl attributes
4284/// which might be lying around on it.
4285void Sema::checkUnusedDeclAttributes(Declarator &D) {
4286 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4287 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4288 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4289 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4290}
4291
Ryan Flynn7d470f32009-07-30 03:15:39 +00004292/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004293/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004294NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4295 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004296 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004297 NamedDecl *NewD = 0;
4298 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004299 FunctionDecl *NewFD;
4300 // FIXME: Missing call to CheckFunctionDeclaration().
4301 // FIXME: Mangling?
4302 // FIXME: Is the qualifier info correct?
4303 // FIXME: Is the DeclContext correct?
4304 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4305 Loc, Loc, DeclarationName(II),
4306 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004307 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004308 FD->hasPrototype(),
4309 false/*isConstexprSpecified*/);
4310 NewD = NewFD;
4311
4312 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004313 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004314
4315 // Fake up parameter variables; they are declared as if this were
4316 // a typedef.
4317 QualType FDTy = FD->getType();
4318 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4319 SmallVector<ParmVarDecl*, 16> Params;
4320 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4321 AE = FT->arg_type_end(); AI != AE; ++AI) {
4322 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4323 Param->setScopeInfo(0, Params.size());
4324 Params.push_back(Param);
4325 }
David Blaikie9c70e042011-09-21 18:16:56 +00004326 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004327 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004328 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4329 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004330 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004331 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004332 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004333 if (VD->getQualifier()) {
4334 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004335 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004336 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004337 }
4338 return NewD;
4339}
4340
James Dennett634962f2012-06-14 21:40:34 +00004341/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004342/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004343void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004344 if (W.getUsed()) return; // only do this once
4345 W.setUsed(true);
4346 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4347 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004348 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004349 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4350 NDId->getName()));
4351 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00004352 WeakTopLevelDecl.push_back(NewD);
4353 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4354 // to insert Decl at TU scope, sorry.
4355 DeclContext *SavedContext = CurContext;
4356 CurContext = Context.getTranslationUnitDecl();
4357 PushOnScopeChains(NewD, S);
4358 CurContext = SavedContext;
4359 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004360 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004361 }
4362}
4363
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004364void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4365 // It's valid to "forward-declare" #pragma weak, in which case we
4366 // have to do this.
4367 LoadExternalWeakUndeclaredIdentifiers();
4368 if (!WeakUndeclaredIdentifiers.empty()) {
4369 NamedDecl *ND = NULL;
4370 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4371 if (VD->isExternC())
4372 ND = VD;
4373 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4374 if (FD->isExternC())
4375 ND = FD;
4376 if (ND) {
4377 if (IdentifierInfo *Id = ND->getIdentifier()) {
4378 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4379 = WeakUndeclaredIdentifiers.find(Id);
4380 if (I != WeakUndeclaredIdentifiers.end()) {
4381 WeakInfo W = I->second;
4382 DeclApplyPragmaWeak(S, ND, W);
4383 WeakUndeclaredIdentifiers[Id] = W;
4384 }
4385 }
4386 }
4387 }
4388}
4389
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004390/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4391/// it, apply them to D. This is a bit tricky because PD can have attributes
4392/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004393void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004394 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004395 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004396 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004397
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004398 // Walk the declarator structure, applying decl attributes that were in a type
4399 // position to the decl itself. This handles cases like:
4400 // int *__attr__(x)** D;
4401 // when X is a decl attribute.
4402 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4403 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004404 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004405
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004406 // Finally, apply any attributes on the decl itself.
4407 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004408 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004409}
John McCall28a6aea2009-11-04 02:18:39 +00004410
John McCall31168b02011-06-15 23:02:42 +00004411/// Is the given declaration allowed to use a forbidden type?
4412static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4413 // Private ivars are always okay. Unfortunately, people don't
4414 // always properly make their ivars private, even in system headers.
4415 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004416 // Function declarations in sys headers will be marked unavailable.
4417 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4418 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004419 return false;
4420
4421 // Require it to be declared in a system header.
4422 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4423}
4424
4425/// Handle a delayed forbidden-type diagnostic.
4426static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4427 Decl *decl) {
4428 if (decl && isForbiddenTypeAllowed(S, decl)) {
4429 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4430 "this system declaration uses an unsupported type"));
4431 return;
4432 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004433 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004434 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004435 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004436 // kind of forbidden type messages on unavailable functions.
4437 if (FD->hasAttr<UnavailableAttr>() &&
4438 diag.getForbiddenTypeDiagnostic() ==
4439 diag::err_arc_array_param_no_ownership) {
4440 diag.Triggered = true;
4441 return;
4442 }
4443 }
John McCall31168b02011-06-15 23:02:42 +00004444
4445 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4446 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4447 diag.Triggered = true;
4448}
4449
John McCall2ec85372012-05-07 06:16:41 +00004450void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4451 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004452 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004453 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004454
John McCall2ec85372012-05-07 06:16:41 +00004455 // When delaying diagnostics to run in the context of a parsed
4456 // declaration, we only want to actually emit anything if parsing
4457 // succeeds.
4458 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004459
John McCall2ec85372012-05-07 06:16:41 +00004460 // We emit all the active diagnostics in this pool or any of its
4461 // parents. In general, we'll get one pool for the decl spec
4462 // and a child pool for each declarator; in a decl group like:
4463 // deprecated_typedef foo, *bar, baz();
4464 // only the declarator pops will be passed decls. This is correct;
4465 // we really do need to consider delayed diagnostics from the decl spec
4466 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004467 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004468 do {
John McCall6347b682012-05-07 06:16:58 +00004469 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004470 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4471 // This const_cast is a bit lame. Really, Triggered should be mutable.
4472 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004473 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004474 continue;
4475
John McCallc1465822011-02-14 07:13:47 +00004476 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004477 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00004478 case DelayedDiagnostic::Unavailable:
4479 // Don't bother giving deprecation/unavailable diagnostics if
4480 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00004481 if (!decl->isInvalidDecl())
Ted Kremenekb79ee572013-12-18 23:30:06 +00004482 HandleDelayedAvailabilityCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004483 break;
4484
4485 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004486 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004487 break;
John McCall31168b02011-06-15 23:02:42 +00004488
4489 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004490 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004491 break;
John McCall86121512010-01-27 03:50:35 +00004492 }
4493 }
John McCall2ec85372012-05-07 06:16:41 +00004494 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004495}
4496
John McCall6347b682012-05-07 06:16:58 +00004497/// Given a set of delayed diagnostics, re-emit them as if they had
4498/// been delayed in the current context instead of in the given pool.
4499/// Essentially, this just moves them to the current pool.
4500void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4501 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4502 assert(curPool && "re-emitting in undelayed context not supported");
4503 curPool->steal(pool);
4504}
4505
John McCall28a6aea2009-11-04 02:18:39 +00004506static bool isDeclDeprecated(Decl *D) {
4507 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004508 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004509 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004510 // A category implicitly has the availability of the interface.
4511 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4512 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004513 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4514 return false;
4515}
4516
Ted Kremenekb79ee572013-12-18 23:30:06 +00004517static bool isDeclUnavailable(Decl *D) {
4518 do {
4519 if (D->isUnavailable())
4520 return true;
4521 // A category implicitly has the availability of the interface.
4522 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4523 return CatD->getClassInterface()->isUnavailable();
4524 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4525 return false;
4526}
4527
Eli Friedman971bfa12012-08-08 21:52:41 +00004528static void
Ted Kremenekb79ee572013-12-18 23:30:06 +00004529DoEmitAvailabilityWarning(Sema &S,
4530 DelayedDiagnostic::DDKind K,
4531 Decl *Ctx,
4532 const NamedDecl *D,
4533 StringRef Message,
4534 SourceLocation Loc,
4535 const ObjCInterfaceDecl *UnknownObjCClass,
4536 const ObjCPropertyDecl *ObjCProperty) {
4537
4538 // Diagnostics for deprecated or unavailable.
4539 unsigned diag, diag_message, diag_fwdclass_message;
4540
4541 // Matches 'diag::note_property_attribute' options.
4542 unsigned property_note_select;
4543
4544 // Matches diag::note_availability_specified_here.
4545 unsigned available_here_select_kind;
4546
4547 // Don't warn if our current context is deprecated or unavailable.
4548 switch (K) {
4549 case DelayedDiagnostic::Deprecation:
4550 if (isDeclDeprecated(Ctx))
4551 return;
4552 diag = diag::warn_deprecated;
4553 diag_message = diag::warn_deprecated_message;
4554 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
4555 property_note_select = /* deprecated */ 0;
4556 available_here_select_kind = /* deprecated */ 2;
4557 break;
4558
4559 case DelayedDiagnostic::Unavailable:
4560 if (isDeclUnavailable(Ctx))
4561 return;
4562 diag = diag::err_unavailable;
4563 diag_message = diag::err_unavailable_message;
4564 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
4565 property_note_select = /* unavailable */ 1;
4566 available_here_select_kind = /* unavailable */ 0;
4567 break;
4568
4569 default:
4570 llvm_unreachable("Neither a deprecation or unavailable kind");
4571 }
4572
Eli Friedman971bfa12012-08-08 21:52:41 +00004573 DeclarationName Name = D->getDeclName();
4574 if (!Message.empty()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004575 S.Diag(Loc, diag_message) << Name << Message;
4576// S.Diag(D->getLocation(), diag::note_availability_specified_here)
4577// << D << available_here_select_kind;
4578 if (ObjCProperty)
4579 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4580 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004581 } else if (!UnknownObjCClass) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004582 S.Diag(Loc, diag) << Name;
4583// S.Diag(D->getLocation(), diag::note_availability_specified_here)
4584// << D << available_here_select_kind;
4585 if (ObjCProperty)
4586 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4587 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004588 } else {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004589 S.Diag(Loc, diag_fwdclass_message) << Name;
Eli Friedman971bfa12012-08-08 21:52:41 +00004590 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4591 }
Ted Kremenekb79ee572013-12-18 23:30:06 +00004592
4593 S.Diag(D->getLocation(), diag::note_availability_specified_here)
4594 << D << available_here_select_kind;
Eli Friedman971bfa12012-08-08 21:52:41 +00004595}
4596
Ted Kremenekb79ee572013-12-18 23:30:06 +00004597void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD,
4598 Decl *Ctx) {
John McCall86121512010-01-27 03:50:35 +00004599 DD.Triggered = true;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004600 DoEmitAvailabilityWarning(*this,
4601 (DelayedDiagnostic::DDKind) DD.Kind,
4602 Ctx,
4603 DD.getDeprecationDecl(),
4604 DD.getDeprecationMessage(),
4605 DD.Loc,
4606 DD.getUnknownObjCClass(),
4607 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004608}
4609
Ted Kremenekb79ee572013-12-18 23:30:06 +00004610void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
4611 NamedDecl *D, StringRef Message,
4612 SourceLocation Loc,
4613 const ObjCInterfaceDecl *UnknownObjCClass,
4614 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004615 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004616 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004617 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D,
4618 UnknownObjCClass,
4619 ObjCProperty,
4620 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004621 return;
4622 }
4623
Ted Kremenekb79ee572013-12-18 23:30:06 +00004624 Decl *Ctx = cast<Decl>(getCurLexicalContext());
4625 DelayedDiagnostic::DDKind K;
4626 switch (AD) {
4627 case AD_Deprecation:
4628 K = DelayedDiagnostic::Deprecation;
4629 break;
4630 case AD_Unavailable:
4631 K = DelayedDiagnostic::Unavailable;
4632 break;
4633 }
4634
4635 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
4636 UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004637}