blob: 6d7bc0346580fbcc4aff8696160c04e2560012ed [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>();
424 if (RT->getDecl()->getAttr<LockableAttr>())
425 return true;
426 return false;
427}
428
429
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000430/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000431/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000432static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
433 QualType Ty) {
434 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000435
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000436 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000437 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000438 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000439 << Attr.getName() << Ty.getAsString();
440 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000441 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000442
Michael Hana9171bc2012-08-03 17:40:43 +0000443 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000444 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000445 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000446
447 // Allow smart pointers to be used as lockable objects.
448 // FIXME -- Check the type that the smart pointer points to.
449 if (threadSafetyCheckIsSmartPointer(S, RT))
450 return;
451
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000452 // Check if the type is lockable.
453 RecordDecl *RD = RT->getDecl();
454 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000455 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000456
457 // Else check if any base classes are lockable.
458 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
459 CXXBasePaths BPaths(false, false);
460 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
461 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000462 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000463
464 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
465 << Attr.getName() << Ty.getAsString();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000466}
467
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000468/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000469/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000470/// \param Sidx The attribute argument index to start checking with.
471/// \param ParamIdxOk Whether an argument can be indexing into a function
472/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000473static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000474 const AttributeList &Attr,
475 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000476 int Sidx = 0,
477 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000478 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000479 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000480
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000481 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000482 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000483 Args.push_back(ArgExp);
484 continue;
485 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000486
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000487 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000488 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000489 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000490 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000491 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000492 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000493 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000494 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000495
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000496 // We allow constant strings to be used as a placeholder for expressions
497 // that are not valid C++ syntax, but warn that they are ignored.
498 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
499 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000500 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000501 continue;
502 }
503
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000504 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000505
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000506 // A pointer to member expression of the form &MyClass::mu is treated
507 // specially -- we need to look at the type of the member.
508 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
509 if (UOp->getOpcode() == UO_AddrOf)
510 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
511 if (DRE->getDecl()->isCXXInstanceMember())
512 ArgTy = DRE->getDecl()->getType();
513
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000514 // First see if we can just cast to record type, or point to record type.
515 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000516
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000517 // Now check if we index into a record type function param.
518 if(!RT && ParamIdxOk) {
519 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000520 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
521 if(FD && IL) {
522 unsigned int NumParams = FD->getNumParams();
523 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000524 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
525 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
526 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000527 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
528 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000529 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000530 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000531 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000532 }
533 }
534
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000535 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000536
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000537 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000538 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000539}
540
Chris Lattner58418ff2008-06-29 00:16:31 +0000541//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000542// Attribute Implementations
543//===----------------------------------------------------------------------===//
544
Daniel Dunbar032db472008-07-31 22:40:48 +0000545// FIXME: All this manual attribute parsing code is gross. At the
546// least add some helper functions to check most argument patterns (#
547// and types of args).
548
Michael Hana9171bc2012-08-03 17:40:43 +0000549static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000550 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000551 if (!threadSafetyCheckIsPointer(S, D, Attr))
552 return;
553
Michael Han99315932013-01-24 16:46:58 +0000554 D->addAttr(::new (S.Context)
555 PtGuardedVarAttr(Attr.getRange(), S.Context,
556 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000557}
558
Michael Hana9171bc2012-08-03 17:40:43 +0000559static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
560 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000561 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000562 SmallVector<Expr*, 1> Args;
563 // check that all arguments are lockable objects
564 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
565 unsigned Size = Args.size();
566 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000567 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000568
Michael Han3be3b442012-07-23 18:48:41 +0000569 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000570
Michael Han3be3b442012-07-23 18:48:41 +0000571 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000572}
573
Michael Han3be3b442012-07-23 18:48:41 +0000574static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
575 Expr *Arg = 0;
576 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
577 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000578
Michael Han3be3b442012-07-23 18:48:41 +0000579 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
580}
581
Michael Hana9171bc2012-08-03 17:40:43 +0000582static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000583 const AttributeList &Attr) {
584 Expr *Arg = 0;
585 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
586 return;
587
588 if (!threadSafetyCheckIsPointer(S, D, Attr))
589 return;
590
591 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
592 S.Context, Arg));
593}
594
Michael Hana9171bc2012-08-03 17:40:43 +0000595static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
596 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000597 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000598 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000599 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000600
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000601 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000602 QualType QT = cast<ValueDecl>(D)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000603 if (!QT->isDependentType()) {
604 const RecordType *RT = getRecordType(QT);
605 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000606 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000607 << Attr.getName();
608 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000609 }
610 }
611
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000612 // Check that all arguments are lockable objects.
613 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000614 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000615 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000616
Michael Han3be3b442012-07-23 18:48:41 +0000617 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000618}
619
Michael Hana9171bc2012-08-03 17:40:43 +0000620static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000621 const AttributeList &Attr) {
622 SmallVector<Expr*, 1> Args;
623 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
624 return;
625
626 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000627 D->addAttr(::new (S.Context)
628 AcquiredAfterAttr(Attr.getRange(), S.Context,
629 StartArg, Args.size(),
630 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000631}
632
Michael Hana9171bc2012-08-03 17:40:43 +0000633static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000634 const AttributeList &Attr) {
635 SmallVector<Expr*, 1> Args;
636 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
637 return;
638
639 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000640 D->addAttr(::new (S.Context)
641 AcquiredBeforeAttr(Attr.getRange(), S.Context,
642 StartArg, Args.size(),
643 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000644}
645
Michael Hana9171bc2012-08-03 17:40:43 +0000646static bool checkLockFunAttrCommon(Sema &S, Decl *D,
647 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000648 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000649 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000650 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000651 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000652
Michael Han3be3b442012-07-23 18:48:41 +0000653 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000654}
655
Michael Hana9171bc2012-08-03 17:40:43 +0000656static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000657 const AttributeList &Attr) {
658 SmallVector<Expr*, 1> Args;
659 if (!checkLockFunAttrCommon(S, D, Attr, Args))
660 return;
661
662 unsigned Size = Args.size();
663 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000664 D->addAttr(::new (S.Context)
665 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
666 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000667}
668
Michael Hana9171bc2012-08-03 17:40:43 +0000669static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000670 const AttributeList &Attr) {
671 SmallVector<Expr*, 1> Args;
672 if (!checkLockFunAttrCommon(S, D, Attr, Args))
673 return;
674
675 unsigned Size = Args.size();
676 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000677 D->addAttr(::new (S.Context)
678 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
679 StartArg, Size,
680 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000681}
682
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000683static void handleAssertSharedLockAttr(Sema &S, Decl *D,
684 const AttributeList &Attr) {
685 SmallVector<Expr*, 1> Args;
686 if (!checkLockFunAttrCommon(S, D, Attr, Args))
687 return;
688
689 unsigned Size = Args.size();
690 Expr **StartArg = Size == 0 ? 0 : &Args[0];
691 D->addAttr(::new (S.Context)
692 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
693 Attr.getAttributeSpellingListIndex()));
694}
695
696static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
697 const AttributeList &Attr) {
698 SmallVector<Expr*, 1> Args;
699 if (!checkLockFunAttrCommon(S, D, Attr, Args))
700 return;
701
702 unsigned Size = Args.size();
703 Expr **StartArg = Size == 0 ? 0 : &Args[0];
704 D->addAttr(::new (S.Context)
705 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
706 StartArg, Size,
707 Attr.getAttributeSpellingListIndex()));
708}
709
710
Michael Hana9171bc2012-08-03 17:40:43 +0000711static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
712 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000713 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000714 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000715 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000716
Aaron Ballman00e99962013-08-31 01:11:41 +0000717 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000718 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000719 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000720 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000721 }
722
723 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000724 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000725
Michael Han3be3b442012-07-23 18:48:41 +0000726 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000727}
728
Michael Hana9171bc2012-08-03 17:40:43 +0000729static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000730 const AttributeList &Attr) {
731 SmallVector<Expr*, 2> Args;
732 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
733 return;
734
Michael Han99315932013-01-24 16:46:58 +0000735 D->addAttr(::new (S.Context)
736 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000737 Attr.getArgAsExpr(0),
738 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000739 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000740}
741
Michael Hana9171bc2012-08-03 17:40:43 +0000742static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000743 const AttributeList &Attr) {
744 SmallVector<Expr*, 2> Args;
745 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
746 return;
747
Michael Han99315932013-01-24 16:46:58 +0000748 D->addAttr(::new (S.Context)
749 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000750 Attr.getArgAsExpr(0),
751 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000752 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000753}
754
Michael Hana9171bc2012-08-03 17:40:43 +0000755static bool checkLocksRequiredCommon(Sema &S, Decl *D,
756 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000757 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000758 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000759 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000760
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000761 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000762 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000763 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000764 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000765
Michael Han3be3b442012-07-23 18:48:41 +0000766 return true;
767}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000768
Michael Hana9171bc2012-08-03 17:40:43 +0000769static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000770 const AttributeList &Attr) {
771 SmallVector<Expr*, 1> Args;
772 if (!checkLocksRequiredCommon(S, D, Attr, Args))
773 return;
774
775 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000776 D->addAttr(::new (S.Context)
777 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
778 StartArg, Args.size(),
779 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000780}
781
Michael Hana9171bc2012-08-03 17:40:43 +0000782static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000783 const AttributeList &Attr) {
784 SmallVector<Expr*, 1> Args;
785 if (!checkLocksRequiredCommon(S, D, Attr, Args))
786 return;
787
788 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000789 D->addAttr(::new (S.Context)
790 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
791 StartArg, Args.size(),
792 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000793}
794
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000795static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000796 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000797 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000798 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000799 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000800 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000801 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000802 Expr **StartArg = Size == 0 ? 0 : &Args[0];
803
Michael Han99315932013-01-24 16:46:58 +0000804 D->addAttr(::new (S.Context)
805 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
806 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000807}
808
809static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000810 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000811 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000812 SmallVector<Expr*, 1> Args;
813 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
814 unsigned Size = Args.size();
815 if (Size == 0)
816 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000817
Michael Han99315932013-01-24 16:46:58 +0000818 D->addAttr(::new (S.Context)
819 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
820 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000821}
822
823static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000824 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000825 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000826 return;
827
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000828 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000829 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000830 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000831 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000832 if (Size == 0)
833 return;
834 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000835
Michael Han99315932013-01-24 16:46:58 +0000836 D->addAttr(::new (S.Context)
837 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
838 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000839}
840
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000841static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000842 ConsumableAttr::ConsumedState DefaultState;
843
844 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000845 IdentifierLoc *IL = Attr.getArgAsIdent(0);
846 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
847 DefaultState)) {
848 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
849 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000850 return;
851 }
David Blaikie16f76d22013-09-06 01:28:43 +0000852 } else {
853 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
854 << Attr.getName() << AANT_ArgumentIdentifier;
855 return;
856 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000857
858 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000859 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000860 Attr.getAttributeSpellingListIndex()));
861}
862
863static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
864 const AttributeList &Attr) {
865 ASTContext &CurrContext = S.getASTContext();
866 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
867
868 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
869 if (!RD->hasAttr<ConsumableAttr>()) {
870 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
871 RD->getNameAsString();
872
873 return false;
874 }
875 }
876
877 return true;
878}
879
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000880
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000881static void handleCallableWhenAttr(Sema &S, Decl *D,
882 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000883 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
884 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000885
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000886 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
887 return;
888
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000889 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
890 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
891 CallableWhenAttr::ConsumedState CallableState;
892
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000893 StringRef StateString;
894 SourceLocation Loc;
895 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
896 return;
897
898 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000899 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000900 S.Diag(Loc, diag::warn_attribute_type_not_supported)
901 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000902 return;
903 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000904
905 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000906 }
907
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000908 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000909 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
910 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000911}
912
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000913
DeLesley Hutchins69391772013-10-17 23:23:53 +0000914static void handleParamTypestateAttr(Sema &S, Decl *D,
915 const AttributeList &Attr) {
916 if (!checkAttributeNumArgs(S, Attr, 1)) return;
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000917
DeLesley Hutchins69391772013-10-17 23:23:53 +0000918 ParamTypestateAttr::ConsumedState ParamState;
919
920 if (Attr.isArgIdent(0)) {
921 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
922 StringRef StateString = Ident->Ident->getName();
923
924 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
925 ParamState)) {
926 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
927 << Attr.getName() << StateString;
928 return;
929 }
930 } else {
931 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
932 Attr.getName() << AANT_ArgumentIdentifier;
933 return;
934 }
935
936 // FIXME: This check is currently being done in the analysis. It can be
937 // enabled here only after the parser propagates attributes at
938 // template specialization definition, not declaration.
939 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
940 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
941 //
942 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
943 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
944 // ReturnType.getAsString();
945 // return;
946 //}
947
948 D->addAttr(::new (S.Context)
949 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
950 Attr.getAttributeSpellingListIndex()));
951}
952
953
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000954static void handleReturnTypestateAttr(Sema &S, Decl *D,
955 const AttributeList &Attr) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000956 if (!checkAttributeNumArgs(S, Attr, 1)) return;
957
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000958 ReturnTypestateAttr::ConsumedState ReturnState;
959
960 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000961 IdentifierLoc *IL = Attr.getArgAsIdent(0);
962 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
963 ReturnState)) {
964 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
965 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000966 return;
967 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000968 } else {
969 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
970 Attr.getName() << AANT_ArgumentIdentifier;
971 return;
972 }
973
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000974 // FIXME: This check is currently being done in the analysis. It can be
975 // enabled here only after the parser propagates attributes at
976 // template specialization definition, not declaration.
977 //QualType ReturnType;
978 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000979 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
980 // ReturnType = Param->getType();
981 //
982 //} else if (const CXXConstructorDecl *Constructor =
983 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000984 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
985 //
986 //} else {
987 //
988 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
989 //}
990 //
991 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
992 //
993 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
994 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
995 // ReturnType.getAsString();
996 // return;
997 //}
998
999 D->addAttr(::new (S.Context)
1000 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1001 Attr.getAttributeSpellingListIndex()));
1002}
1003
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001004
1005static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001006 if (!checkAttributeNumArgs(S, Attr, 1))
1007 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001008
1009 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1010 return;
1011
1012 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001013 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001014 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1015 StringRef Param = Ident->Ident->getName();
1016 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1017 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1018 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001019 return;
1020 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001021 } else {
1022 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1023 Attr.getName() << AANT_ArgumentIdentifier;
1024 return;
1025 }
1026
1027 D->addAttr(::new (S.Context)
1028 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1029 Attr.getAttributeSpellingListIndex()));
1030}
1031
Chris Wailes9385f9f2013-10-29 20:28:41 +00001032static void handleTestTypestateAttr(Sema &S, Decl *D,
1033 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001034 if (!checkAttributeNumArgs(S, Attr, 1))
1035 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001036
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001037 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1038 return;
1039
Chris Wailes9385f9f2013-10-29 20:28:41 +00001040 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001041 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001042 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1043 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001044 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001045 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1046 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001047 return;
1048 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001049 } else {
1050 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1051 Attr.getName() << AANT_ArgumentIdentifier;
1052 return;
1053 }
1054
1055 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001056 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001057 Attr.getAttributeSpellingListIndex()));
1058}
1059
Chandler Carruthedc2c642011-07-02 00:01:44 +00001060static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1061 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001062 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001063 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001064}
1065
Chandler Carruthedc2c642011-07-02 00:01:44 +00001066static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001067 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001068 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001069 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001070 // If the alignment is less than or equal to 8 bits, the packed attribute
1071 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001072 if (!FD->getType()->isDependentType() &&
1073 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001074 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001075 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001076 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001077 else
Michael Han99315932013-01-24 16:46:58 +00001078 FD->addAttr(::new (S.Context)
1079 PackedAttr(Attr.getRange(), S.Context,
1080 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001081 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001082 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001083}
1084
Ted Kremenek7fd17232011-09-29 07:02:25 +00001085static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1086 // The IBOutlet/IBOutletCollection attributes only apply to instance
1087 // variables or properties of Objective-C classes. The outlet must also
1088 // have an object reference type.
1089 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1090 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001091 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001092 << Attr.getName() << VD->getType() << 0;
1093 return false;
1094 }
1095 }
1096 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1097 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001098 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001099 << Attr.getName() << PD->getType() << 1;
1100 return false;
1101 }
1102 }
1103 else {
1104 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1105 return false;
1106 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001107
Ted Kremenek7fd17232011-09-29 07:02:25 +00001108 return true;
1109}
1110
Chandler Carruthedc2c642011-07-02 00:01:44 +00001111static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001112 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001113 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001114
Michael Han99315932013-01-24 16:46:58 +00001115 D->addAttr(::new (S.Context)
1116 IBOutletAttr(Attr.getRange(), S.Context,
1117 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001118}
1119
Chandler Carruthedc2c642011-07-02 00:01:44 +00001120static void handleIBOutletCollection(Sema &S, Decl *D,
1121 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001122
1123 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001124 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001125 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1126 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001127 return;
1128 }
1129
Ted Kremenek7fd17232011-09-29 07:02:25 +00001130 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001131 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001132
Richard Smithb1f9a282013-10-31 01:56:18 +00001133 ParsedType PT;
1134
1135 if (Attr.hasParsedType())
1136 PT = Attr.getTypeArg();
1137 else {
1138 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1139 S.getScopeForContext(D->getDeclContext()->getParent()));
1140 if (!PT) {
1141 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1142 return;
1143 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001144 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001145
Richard Smithb87c4652013-10-31 21:23:20 +00001146 TypeSourceInfo *QTLoc = 0;
1147 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1148 if (!QTLoc)
1149 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001150
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001151 // Diagnose use of non-object type in iboutletcollection attribute.
1152 // FIXME. Gnu attribute extension ignores use of builtin types in
1153 // attributes. So, __attribute__((iboutletcollection(char))) will be
1154 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001155 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001156 S.Diag(Attr.getLoc(),
1157 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1158 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001159 return;
1160 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001161
Michael Han99315932013-01-24 16:46:58 +00001162 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001163 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001164 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001165}
1166
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001167static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001168 if (const RecordType *UT = T->getAsUnionType())
1169 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1170 RecordDecl *UD = UT->getDecl();
1171 for (RecordDecl::field_iterator it = UD->field_begin(),
1172 itend = UD->field_end(); it != itend; ++it) {
1173 QualType QT = it->getType();
1174 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1175 T = QT;
1176 return;
1177 }
1178 }
1179 }
1180}
1181
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001182static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopese881ce22012-06-18 16:39:04 +00001183 if (!isFunctionOrMethod(D)) {
1184 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001185 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopese881ce22012-06-18 16:39:04 +00001186 return;
1187 }
1188
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001189 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1190 return;
1191
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001192 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001193 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001194 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001195 uint64_t Idx;
1196 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1197 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001198 return;
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001199
1200 // check if the function argument is of an integer type
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001201 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001202 if (!T->isIntegerType()) {
Aaron Ballman9d695092013-07-30 14:10:17 +00001203 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1204 << Attr.getName() << AANT_ArgumentIntegerConstant
1205 << Ex->getSourceRange();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001206 return;
1207 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001208 SizeArgs.push_back(Idx);
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001209 }
1210
1211 // check if the function returns a pointer
1212 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1213 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001214 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001215 }
1216
Michael Han99315932013-01-24 16:46:58 +00001217 D->addAttr(::new (S.Context)
1218 AllocSizeAttr(Attr.getRange(), S.Context,
1219 SizeArgs.data(), SizeArgs.size(),
1220 Attr.getAttributeSpellingListIndex()));
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001221}
1222
Chandler Carruthedc2c642011-07-02 00:01:44 +00001223static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001224 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1225 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001226 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001227 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001228 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001229 return;
1230 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001231
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001232 SmallVector<unsigned, 8> NonNullArgs;
1233 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001234 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001235 uint64_t Idx;
1236 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1237 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001238 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001239
1240 // Is the function argument a pointer type?
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001241 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001242 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001243
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001244 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001245 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +00001246 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +00001247 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001248 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001249 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001250
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001251 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001252 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001253
1254 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1255 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001256 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001257 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1258 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001259 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001260 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001261 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001262 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001263
Ted Kremenek22813f42010-10-21 18:49:36 +00001264 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001265 if (NonNullArgs.empty()) {
1266 // Warn the trivial case only if attribute is not coming from a
1267 // macro instantiation.
1268 if (Attr.getLoc().isFileID())
1269 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001270 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001271 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001272 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001273
Nick Lewyckye1121512013-01-24 01:12:16 +00001274 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001275 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001276 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001277 D->addAttr(::new (S.Context)
1278 NonNullAttr(Attr.getRange(), S.Context, start, size,
1279 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001280}
1281
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001282static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1283 switch (K) {
1284 case OwnershipAttr::Holds: return "'ownership_holds'";
1285 case OwnershipAttr::Takes: return "'ownership_takes'";
1286 case OwnershipAttr::Returns: return "'ownership_returns'";
1287 }
1288 llvm_unreachable("unknown ownership");
1289}
1290
Chandler Carruthedc2c642011-07-02 00:01:44 +00001291static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001292 // This attribute must be applied to a function declaration. The first
1293 // argument to the attribute must be an identifier, the name of the resource,
1294 // for example: malloc. The following arguments must be argument indexes, the
1295 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001296 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001297 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001298 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001299
Aaron Ballman00e99962013-08-31 01:11:41 +00001300 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001301 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001302 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001303 return;
1304 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001305
Richard Smith852e9ce2013-11-27 01:46:48 +00001306 // Figure out our Kind.
1307 OwnershipAttr::OwnershipKind K =
1308 OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0,
1309 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001310
Richard Smith852e9ce2013-11-27 01:46:48 +00001311 // Check arguments.
1312 switch (K) {
1313 case OwnershipAttr::Takes:
1314 case OwnershipAttr::Holds:
1315 if (AL.getNumArgs() < 2) {
1316 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
1317 return;
1318 }
1319 break;
1320 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001321 if (AL.getNumArgs() > 2) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001322 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001323 return;
1324 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001325 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001326 }
1327
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001328 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001329 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1330 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001331 return;
1332 }
1333
Richard Smith852e9ce2013-11-27 01:46:48 +00001334 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001335
1336 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001337 StringRef ModuleName = Module->getName();
1338 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1339 ModuleName.size() > 4) {
1340 ModuleName = ModuleName.drop_front(2).drop_back(2);
1341 Module = &S.PP.getIdentifierTable().get(ModuleName);
1342 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001343
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001344 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001345 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1346 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001347 uint64_t Idx;
1348 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
Aaron Ballman00e99962013-08-31 01:11:41 +00001349 AL.getLoc(), i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001350 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001351
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001352 // Is the function argument a pointer type?
1353 QualType T = getFunctionOrMethodArgType(D, Idx);
1354 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001355 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001356 case OwnershipAttr::Takes:
1357 case OwnershipAttr::Holds:
1358 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1359 Err = 0;
1360 break;
1361 case OwnershipAttr::Returns:
1362 if (!T->isIntegerType())
1363 Err = 1;
1364 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001365 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001366 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001367 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001368 << Ex->getSourceRange();
1369 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001370 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001371
1372 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001373 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001374 i = D->specific_attr_begin<OwnershipAttr>(),
1375 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001376 // FIXME: A returns attribute should conflict with any returns attribute
1377 // with a different index too.
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001378 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1379 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1380 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1381 << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind());
1382 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001383 }
1384 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001385 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001386 }
1387
1388 unsigned* start = OwnershipArgs.data();
1389 unsigned size = OwnershipArgs.size();
1390 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001391
Michael Han99315932013-01-24 16:46:58 +00001392 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001393 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001394 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001395}
1396
Chandler Carruthedc2c642011-07-02 00:01:44 +00001397static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001398 // Check the attribute arguments.
1399 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001400 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1401 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001402 return;
1403 }
1404
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001405 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001406
Rafael Espindolac18086a2010-02-23 22:00:30 +00001407 // gcc rejects
1408 // class c {
1409 // static int a __attribute__((weakref ("v2")));
1410 // static int b() __attribute__((weakref ("f3")));
1411 // };
1412 // and ignores the attributes of
1413 // void f(void) {
1414 // static int a __attribute__((weakref ("v2")));
1415 // }
1416 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001417 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001418 if (!Ctx->isFileContext()) {
1419 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +00001420 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +00001421 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001422 }
1423
1424 // The GCC manual says
1425 //
1426 // At present, a declaration to which `weakref' is attached can only
1427 // be `static'.
1428 //
1429 // It also says
1430 //
1431 // Without a TARGET,
1432 // given as an argument to `weakref' or to `alias', `weakref' is
1433 // equivalent to `weak'.
1434 //
1435 // gcc 4.4.1 will accept
1436 // int a7 __attribute__((weakref));
1437 // as
1438 // int a7 __attribute__((weak));
1439 // This looks like a bug in gcc. We reject that for now. We should revisit
1440 // it if this behaviour is actually used.
1441
Rafael Espindolac18086a2010-02-23 22:00:30 +00001442 // GCC rejects
1443 // static ((alias ("y"), weakref)).
1444 // Should we? How to check that weakref is before or after alias?
1445
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001446 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1447 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1448 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001449 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001450 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001451 // GCC will accept anything as the argument of weakref. Should we
1452 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001453 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1454 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001455
Michael Han99315932013-01-24 16:46:58 +00001456 D->addAttr(::new (S.Context)
1457 WeakRefAttr(Attr.getRange(), S.Context,
1458 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001459}
1460
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001461static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1462 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001463 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001464 return;
1465
Douglas Gregore8bbc122011-09-02 00:18:52 +00001466 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001467 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1468 return;
1469 }
1470
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001471 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001472
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001473 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001474 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001475}
1476
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001477static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanfb763042013-12-02 18:05:46 +00001478 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr, "hot"))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001479 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001480
Michael Han99315932013-01-24 16:46:58 +00001481 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1482 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001483}
1484
1485static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanfb763042013-12-02 18:05:46 +00001486 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr, "cold"))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001487 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001488
Michael Han99315932013-01-24 16:46:58 +00001489 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1490 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001491}
1492
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001493static void handleTLSModelAttr(Sema &S, Decl *D,
1494 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001495 StringRef Model;
1496 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001497 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001498 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001499 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001500
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001501 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001502 if (Model != "global-dynamic" && Model != "local-dynamic"
1503 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001504 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001505 return;
1506 }
1507
Michael Han99315932013-01-24 16:46:58 +00001508 D->addAttr(::new (S.Context)
1509 TLSModelAttr(Attr.getRange(), S.Context, Model,
1510 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001511}
1512
Chandler Carruthedc2c642011-07-02 00:01:44 +00001513static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001514 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001515 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001516 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001517 D->addAttr(::new (S.Context)
1518 MallocAttr(Attr.getRange(), S.Context,
1519 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001520 return;
1521 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001522 }
1523
Ted Kremenek08479ae2009-08-15 00:51:46 +00001524 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001525}
1526
Chandler Carruthedc2c642011-07-02 00:01:44 +00001527static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001528 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001529 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1530 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001531 return;
1532 }
1533
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001534 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1535 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001536}
1537
Chandler Carruthedc2c642011-07-02 00:01:44 +00001538static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001539 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001540
1541 if (S.CheckNoReturnAttr(attr)) return;
1542
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001543 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001544 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001545 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001546 return;
1547 }
1548
Michael Han99315932013-01-24 16:46:58 +00001549 D->addAttr(::new (S.Context)
1550 NoReturnAttr(attr.getRange(), S.Context,
1551 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001552}
1553
1554bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001555 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001556 attr.setInvalid();
1557 return true;
1558 }
1559
1560 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001561}
1562
Chandler Carruthedc2c642011-07-02 00:01:44 +00001563static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1564 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001565
1566 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1567 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001568 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1569 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001570 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1571 && !VD->getType()->isFunctionPointerType())) {
1572 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001573 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001574 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001575 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001576 return;
1577 }
1578 }
1579
Michael Han99315932013-01-24 16:46:58 +00001580 D->addAttr(::new (S.Context)
1581 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1582 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001583}
1584
John Thompsoncdb847ba2010-08-09 21:53:52 +00001585// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001586static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001587/*
1588 Returning a Vector Class in Registers
1589
Eric Christopherbc638a82010-12-01 22:13:54 +00001590 According to the PPU ABI specifications, a class with a single member of
1591 vector type is returned in memory when used as the return value of a function.
1592 This results in inefficient code when implementing vector classes. To return
1593 the value in a single vector register, add the vecreturn attribute to the
1594 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001595
1596 Example:
1597
1598 struct Vector
1599 {
1600 __vector float xyzw;
1601 } __attribute__((vecreturn));
1602
1603 Vector Add(Vector lhs, Vector rhs)
1604 {
1605 Vector result;
1606 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1607 return result; // This will be returned in a register
1608 }
1609*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001610 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001611 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1612 return;
1613 }
1614
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001615 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001616 int count = 0;
1617
1618 if (!isa<CXXRecordDecl>(record)) {
1619 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1620 return;
1621 }
1622
1623 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1624 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1625 return;
1626 }
1627
Eric Christopherbc638a82010-12-01 22:13:54 +00001628 for (RecordDecl::field_iterator iter = record->field_begin();
1629 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001630 if ((count == 1) || !iter->getType()->isVectorType()) {
1631 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1632 return;
1633 }
1634 count++;
1635 }
1636
Michael Han99315932013-01-24 16:46:58 +00001637 D->addAttr(::new (S.Context)
1638 VecReturnAttr(Attr.getRange(), S.Context,
1639 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001640}
1641
Richard Smithe233fbf2013-01-28 22:42:45 +00001642static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1643 const AttributeList &Attr) {
1644 if (isa<ParmVarDecl>(D)) {
1645 // [[carries_dependency]] can only be applied to a parameter if it is a
1646 // parameter of a function declaration or lambda.
1647 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1648 S.Diag(Attr.getLoc(),
1649 diag::err_carries_dependency_param_not_function_decl);
1650 return;
1651 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001652 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001653
1654 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1655 Attr.getRange(), S.Context,
1656 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001657}
1658
Chandler Carruthedc2c642011-07-02 00:01:44 +00001659static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001660 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00001661 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001662 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001663 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001664 return;
1665 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001666
Michael Han99315932013-01-24 16:46:58 +00001667 D->addAttr(::new (S.Context)
1668 UnusedAttr(Attr.getRange(), S.Context,
1669 Attr.getAttributeSpellingListIndex()));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001670}
1671
Chandler Carruthedc2c642011-07-02 00:01:44 +00001672static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001673 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001674 if (VD->hasLocalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001675 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1676 return;
1677 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001678 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001679 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001680 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001681 return;
1682 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001683
Michael Han99315932013-01-24 16:46:58 +00001684 D->addAttr(::new (S.Context)
1685 UsedAttr(Attr.getRange(), S.Context,
1686 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001687}
1688
Chandler Carruthedc2c642011-07-02 00:01:44 +00001689static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001690 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001691 if (Attr.getNumArgs() > 1) {
1692 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001693 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001694 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001695
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001696 uint32_t priority = ConstructorAttr::DefaultPriority;
1697 if (Attr.getNumArgs() > 0 &&
1698 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1699 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001700
Michael Han99315932013-01-24 16:46:58 +00001701 D->addAttr(::new (S.Context)
1702 ConstructorAttr(Attr.getRange(), S.Context, priority,
1703 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001704}
1705
Chandler Carruthedc2c642011-07-02 00:01:44 +00001706static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001707 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001708 if (Attr.getNumArgs() > 1) {
1709 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001710 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001711 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001712
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001713 uint32_t priority = ConstructorAttr::DefaultPriority;
1714 if (Attr.getNumArgs() > 0 &&
1715 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1716 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001717
Michael Han99315932013-01-24 16:46:58 +00001718 D->addAttr(::new (S.Context)
1719 DestructorAttr(Attr.getRange(), S.Context, priority,
1720 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001721}
1722
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001723template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001724static void handleAttrWithMessage(Sema &S, Decl *D,
1725 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001726 unsigned NumArgs = Attr.getNumArgs();
1727 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001728 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001729 return;
1730 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001731
1732 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001733 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001734 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001735 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001736
Michael Han99315932013-01-24 16:46:58 +00001737 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1738 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001739}
1740
Ted Kremenek28eace62013-11-23 01:01:34 +00001741static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
1742 const AttributeList &Attr) {
Ted Kremenek7559b472013-11-23 22:29:11 +00001743 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek28eace62013-11-23 01:01:34 +00001744
1745 if (!Parm) {
1746 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 1;
1747 return;
1748 }
1749
1750 D->addAttr(::new (S.Context)
1751 ObjCSuppressProtocolAttr(Attr.getRange(), S.Context, Parm->Ident,
1752 Attr.getAttributeSpellingListIndex()));
1753}
1754
Jordy Rose740b0c22012-05-08 03:27:22 +00001755static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1756 IdentifierInfo *Platform,
1757 VersionTuple Introduced,
1758 VersionTuple Deprecated,
1759 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001760 StringRef PlatformName
1761 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1762 if (PlatformName.empty())
1763 PlatformName = Platform->getName();
1764
1765 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1766 // of these steps are needed).
1767 if (!Introduced.empty() && !Deprecated.empty() &&
1768 !(Introduced <= Deprecated)) {
1769 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1770 << 1 << PlatformName << Deprecated.getAsString()
1771 << 0 << Introduced.getAsString();
1772 return true;
1773 }
1774
1775 if (!Introduced.empty() && !Obsoleted.empty() &&
1776 !(Introduced <= Obsoleted)) {
1777 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1778 << 2 << PlatformName << Obsoleted.getAsString()
1779 << 0 << Introduced.getAsString();
1780 return true;
1781 }
1782
1783 if (!Deprecated.empty() && !Obsoleted.empty() &&
1784 !(Deprecated <= Obsoleted)) {
1785 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1786 << 2 << PlatformName << Obsoleted.getAsString()
1787 << 1 << Deprecated.getAsString();
1788 return true;
1789 }
1790
1791 return false;
1792}
1793
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001794/// \brief Check whether the two versions match.
1795///
1796/// If either version tuple is empty, then they are assumed to match. If
1797/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1798static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1799 bool BeforeIsOkay) {
1800 if (X.empty() || Y.empty())
1801 return true;
1802
1803 if (X == Y)
1804 return true;
1805
1806 if (BeforeIsOkay && X < Y)
1807 return true;
1808
1809 return false;
1810}
1811
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001812AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001813 IdentifierInfo *Platform,
1814 VersionTuple Introduced,
1815 VersionTuple Deprecated,
1816 VersionTuple Obsoleted,
1817 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001818 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001819 bool Override,
1820 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001821 VersionTuple MergedIntroduced = Introduced;
1822 VersionTuple MergedDeprecated = Deprecated;
1823 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001824 bool FoundAny = false;
1825
Rafael Espindolac67f2232012-05-10 02:50:16 +00001826 if (D->hasAttrs()) {
1827 AttrVec &Attrs = D->getAttrs();
1828 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1829 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1830 if (!OldAA) {
1831 ++i;
1832 continue;
1833 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001834
Rafael Espindolac67f2232012-05-10 02:50:16 +00001835 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1836 if (OldPlatform != Platform) {
1837 ++i;
1838 continue;
1839 }
1840
1841 FoundAny = true;
1842 VersionTuple OldIntroduced = OldAA->getIntroduced();
1843 VersionTuple OldDeprecated = OldAA->getDeprecated();
1844 VersionTuple OldObsoleted = OldAA->getObsoleted();
1845 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001846
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001847 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1848 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1849 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1850 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001851 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001852 if (Override) {
1853 int Which = -1;
1854 VersionTuple FirstVersion;
1855 VersionTuple SecondVersion;
1856 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1857 Which = 0;
1858 FirstVersion = OldIntroduced;
1859 SecondVersion = Introduced;
1860 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1861 Which = 1;
1862 FirstVersion = Deprecated;
1863 SecondVersion = OldDeprecated;
1864 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1865 Which = 2;
1866 FirstVersion = Obsoleted;
1867 SecondVersion = OldObsoleted;
1868 }
1869
1870 if (Which == -1) {
1871 Diag(OldAA->getLocation(),
1872 diag::warn_mismatched_availability_override_unavail)
1873 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1874 } else {
1875 Diag(OldAA->getLocation(),
1876 diag::warn_mismatched_availability_override)
1877 << Which
1878 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1879 << FirstVersion.getAsString() << SecondVersion.getAsString();
1880 }
1881 Diag(Range.getBegin(), diag::note_overridden_method);
1882 } else {
1883 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1884 Diag(Range.getBegin(), diag::note_previous_attribute);
1885 }
1886
Rafael Espindolac67f2232012-05-10 02:50:16 +00001887 Attrs.erase(Attrs.begin() + i);
1888 --e;
1889 continue;
1890 }
1891
1892 VersionTuple MergedIntroduced2 = MergedIntroduced;
1893 VersionTuple MergedDeprecated2 = MergedDeprecated;
1894 VersionTuple MergedObsoleted2 = MergedObsoleted;
1895
1896 if (MergedIntroduced2.empty())
1897 MergedIntroduced2 = OldIntroduced;
1898 if (MergedDeprecated2.empty())
1899 MergedDeprecated2 = OldDeprecated;
1900 if (MergedObsoleted2.empty())
1901 MergedObsoleted2 = OldObsoleted;
1902
1903 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1904 MergedIntroduced2, MergedDeprecated2,
1905 MergedObsoleted2)) {
1906 Attrs.erase(Attrs.begin() + i);
1907 --e;
1908 continue;
1909 }
1910
1911 MergedIntroduced = MergedIntroduced2;
1912 MergedDeprecated = MergedDeprecated2;
1913 MergedObsoleted = MergedObsoleted2;
1914 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001915 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001916 }
1917
1918 if (FoundAny &&
1919 MergedIntroduced == Introduced &&
1920 MergedDeprecated == Deprecated &&
1921 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001922 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001923
Ted Kremenekb5445722013-04-06 00:34:27 +00001924 // Only create a new attribute if !Override, but we want to do
1925 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001926 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001927 MergedDeprecated, MergedObsoleted) &&
1928 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001929 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1930 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001931 Obsoleted, IsUnavailable, Message,
1932 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001933 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001934 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001935}
1936
Chandler Carruthedc2c642011-07-02 00:01:44 +00001937static void handleAvailabilityAttr(Sema &S, Decl *D,
1938 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001939 if (!checkAttributeNumArgs(S, Attr, 1))
1940 return;
1941 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001942 unsigned Index = Attr.getAttributeSpellingListIndex();
1943
Aaron Ballman00e99962013-08-31 01:11:41 +00001944 IdentifierInfo *II = Platform->Ident;
1945 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1946 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1947 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001948
Rafael Espindolac231fab2013-01-08 21:30:32 +00001949 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1950 if (!ND) {
1951 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1952 return;
1953 }
1954
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001955 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1956 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1957 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001958 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001959 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001960 if (const StringLiteral *SE =
1961 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001962 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001963
Aaron Ballman00e99962013-08-31 01:11:41 +00001964 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001965 Introduced.Version,
1966 Deprecated.Version,
1967 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001968 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001969 /*Override=*/false,
1970 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001971 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001972 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001973}
1974
John McCalld041a9b2013-02-20 01:54:26 +00001975template <class T>
1976static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1977 typename T::VisibilityType value,
1978 unsigned attrSpellingListIndex) {
1979 T *existingAttr = D->getAttr<T>();
1980 if (existingAttr) {
1981 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1982 if (existingValue == value)
1983 return NULL;
1984 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1985 S.Diag(range.getBegin(), diag::note_previous_attribute);
1986 D->dropAttr<T>();
1987 }
1988 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1989}
1990
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001991VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001992 VisibilityAttr::VisibilityType Vis,
1993 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001994 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1995 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001996}
1997
John McCalld041a9b2013-02-20 01:54:26 +00001998TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1999 TypeVisibilityAttr::VisibilityType Vis,
2000 unsigned AttrSpellingListIndex) {
2001 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2002 AttrSpellingListIndex);
2003}
2004
2005static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2006 bool isTypeVisibility) {
2007 // Visibility attributes don't mean anything on a typedef.
2008 if (isa<TypedefNameDecl>(D)) {
2009 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2010 << Attr.getName();
2011 return;
2012 }
2013
2014 // 'type_visibility' can only go on a type or namespace.
2015 if (isTypeVisibility &&
2016 !(isa<TagDecl>(D) ||
2017 isa<ObjCInterfaceDecl>(D) ||
2018 isa<NamespaceDecl>(D))) {
2019 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2020 << Attr.getName() << ExpectedTypeOrNamespace;
2021 return;
2022 }
2023
Benjamin Kramer70370212013-09-09 15:08:57 +00002024 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002025 StringRef TypeStr;
2026 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002027 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002028 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002029
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002030 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002031 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002032 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002033 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002034 return;
2035 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002036
2037 // Complain about attempts to use protected visibility on targets
2038 // (like Darwin) that don't support it.
2039 if (type == VisibilityAttr::Protected &&
2040 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2041 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2042 type = VisibilityAttr::Default;
2043 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002044
Michael Han99315932013-01-24 16:46:58 +00002045 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002046 clang::Attr *newAttr;
2047 if (isTypeVisibility) {
2048 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2049 (TypeVisibilityAttr::VisibilityType) type,
2050 Index);
2051 } else {
2052 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2053 }
2054 if (newAttr)
2055 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002056}
2057
Chandler Carruthedc2c642011-07-02 00:01:44 +00002058static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2059 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002060 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002061 if (!Attr.isArgIdent(0)) {
2062 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2063 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002064 return;
2065 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002066
Aaron Ballman682ee422013-09-11 19:47:58 +00002067 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2068 ObjCMethodFamilyAttr::FamilyKind F;
2069 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2070 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2071 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002072 return;
2073 }
2074
Aaron Ballman682ee422013-09-11 19:47:58 +00002075 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCall31168b02011-06-15 23:02:42 +00002076 !method->getResultType()->isObjCObjectPointerType()) {
2077 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2078 << method->getResultType();
2079 // Ignore the attribute.
2080 return;
2081 }
2082
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002083 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman682ee422013-09-11 19:47:58 +00002084 S.Context, F));
John McCall86bc21f2011-03-02 11:33:24 +00002085}
2086
Chandler Carruthedc2c642011-07-02 00:01:44 +00002087static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002088 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002089 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002090 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002091 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2092 return;
2093 }
2094 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002095 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2096 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002097 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002098 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2099 return;
2100 }
2101 }
2102 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002103 // It is okay to include this attribute on properties, e.g.:
2104 //
2105 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2106 //
2107 // In this case it follows tradition and suppresses an error in the above
2108 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002109 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002110 }
Michael Han99315932013-01-24 16:46:58 +00002111 D->addAttr(::new (S.Context)
2112 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2113 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002114}
2115
Chandler Carruthedc2c642011-07-02 00:01:44 +00002116static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002117 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002118 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002119 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002120 return;
2121 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002122
Aaron Ballman00e99962013-08-31 01:11:41 +00002123 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002124 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002125 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2126 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2127 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002128 return;
2129 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002130
Michael Han99315932013-01-24 16:46:58 +00002131 D->addAttr(::new (S.Context)
2132 BlocksAttr(Attr.getRange(), S.Context, type,
2133 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002134}
2135
Chandler Carruthedc2c642011-07-02 00:01:44 +00002136static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002137 // check the attribute arguments.
2138 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002139 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002140 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002141 }
2142
Aaron Ballman18a78382013-11-21 00:28:23 +00002143 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002144 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002145 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002146 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002147 if (E->isTypeDependent() || E->isValueDependent() ||
2148 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002149 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002150 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002151 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002152 return;
2153 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002154
John McCallb46f2872011-09-09 07:56:05 +00002155 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002156 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2157 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002158 return;
2159 }
John McCallb46f2872011-09-09 07:56:05 +00002160
2161 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002162 }
2163
Aaron Ballman18a78382013-11-21 00:28:23 +00002164 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002165 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002166 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002167 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002168 if (E->isTypeDependent() || E->isValueDependent() ||
2169 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002170 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002171 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002172 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002173 return;
2174 }
2175 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002176
John McCallb46f2872011-09-09 07:56:05 +00002177 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002178 // FIXME: This error message could be improved, it would be nice
2179 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002180 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2181 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002182 return;
2183 }
2184 }
2185
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002186 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002187 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002188 if (isa<FunctionNoProtoType>(FT)) {
2189 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2190 return;
2191 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002192
Chris Lattner9363e312009-03-17 23:03:47 +00002193 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002194 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002195 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002196 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002197 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002198 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002199 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002200 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002201 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002202 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2203 if (!BD->isVariadic()) {
2204 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2205 return;
2206 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002207 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002208 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002209 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002210 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002211 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002212 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002213 int m = Ty->isFunctionPointerType() ? 0 : 1;
2214 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002215 return;
2216 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002217 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002218 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002219 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002220 return;
2221 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002222 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002223 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002224 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002225 return;
2226 }
Michael Han99315932013-01-24 16:46:58 +00002227 D->addAttr(::new (S.Context)
2228 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2229 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002230}
2231
Chandler Carruthedc2c642011-07-02 00:01:44 +00002232static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002233 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002234 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002235 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002236 return;
2237 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002238
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002239 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2240 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2241 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002242 return;
2243 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002244 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2245 if (MD->getResultType()->isVoidType()) {
2246 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2247 << Attr.getName() << 1;
2248 return;
2249 }
2250
Michael Han99315932013-01-24 16:46:58 +00002251 D->addAttr(::new (S.Context)
2252 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2253 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002254}
2255
Chandler Carruthedc2c642011-07-02 00:01:44 +00002256static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002257 // weak_import only applies to variable & function declarations.
2258 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002259 if (!D->canBeWeakImported(isDef)) {
2260 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002261 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2262 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002263 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002264 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002265 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002266 // Nothing to warn about here.
2267 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002268 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002269 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002270
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002271 return;
2272 }
2273
Michael Han99315932013-01-24 16:46:58 +00002274 D->addAttr(::new (S.Context)
2275 WeakImportAttr(Attr.getRange(), S.Context,
2276 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002277}
2278
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002279// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002280template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002281static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002282 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002283 uint32_t WGSize[3];
2284 for (unsigned i = 0; i < 3; ++i)
2285 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002286 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002287
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002288 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2289 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2290 Existing->getYDim() == WGSize[1] &&
2291 Existing->getZDim() == WGSize[2]))
2292 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002293
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002294 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2295 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002296 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002297}
2298
Joey Goulyaba589c2013-03-08 09:42:32 +00002299static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002300 if (!Attr.hasParsedType()) {
2301 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2302 << Attr.getName() << 1;
2303 return;
2304 }
2305
Richard Smithb87c4652013-10-31 21:23:20 +00002306 TypeSourceInfo *ParmTSI = 0;
2307 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2308 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002309
2310 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2311 (ParmType->isBooleanType() ||
2312 !ParmType->isIntegralType(S.getASTContext()))) {
2313 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2314 << ParmType;
2315 return;
2316 }
2317
Aaron Ballmana9e05402013-12-02 22:16:55 +00002318 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002319 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002320 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2321 return;
2322 }
2323 }
2324
2325 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Richard Smithb87c4652013-10-31 21:23:20 +00002326 ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002327}
2328
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002329SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002330 StringRef Name,
2331 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002332 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2333 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002334 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002335 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2336 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002337 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002338 }
Michael Han99315932013-01-24 16:46:58 +00002339 return ::new (Context) SectionAttr(Range, Context, Name,
2340 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002341}
2342
Chandler Carruthedc2c642011-07-02 00:01:44 +00002343static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002344 // Make sure that there is a string literal as the sections's single
2345 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002346 StringRef Str;
2347 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002348 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002349 return;
Mike Stump11289f42009-09-09 15:08:12 +00002350
Chris Lattner30ba6742009-08-10 19:03:04 +00002351 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002352 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002353 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002354 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002355 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002356 return;
2357 }
Mike Stump11289f42009-09-09 15:08:12 +00002358
Chris Lattner20aee9b2010-01-12 20:58:53 +00002359 // This attribute cannot be applied to local variables.
2360 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002361 S.Diag(LiteralLoc, diag::err_attribute_section_local_variable);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002362 return;
2363 }
Michael Han99315932013-01-24 16:46:58 +00002364
2365 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002366 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002367 if (NewAttr)
2368 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002369}
2370
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002371
Chandler Carruthedc2c642011-07-02 00:01:44 +00002372static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002373 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002374 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002375 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002376 } else {
Michael Han99315932013-01-24 16:46:58 +00002377 D->addAttr(::new (S.Context)
2378 NoThrowAttr(Attr.getRange(), S.Context,
2379 Attr.getAttributeSpellingListIndex()));
Douglas Gregor88336832011-06-15 05:45:11 +00002380 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002381}
2382
Chandler Carruthedc2c642011-07-02 00:01:44 +00002383static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002384 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002385 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002386 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002387 } else {
Michael Han99315932013-01-24 16:46:58 +00002388 D->addAttr(::new (S.Context)
2389 ConstAttr(Attr.getRange(), S.Context,
2390 Attr.getAttributeSpellingListIndex() ));
Douglas Gregor88336832011-06-15 05:45:11 +00002391 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002392}
2393
Chandler Carruthedc2c642011-07-02 00:01:44 +00002394static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002395 VarDecl *VD = cast<VarDecl>(D);
2396 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002397 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002398 return;
2399 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002400
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002401 Expr *E = Attr.getArgAsExpr(0);
2402 SourceLocation Loc = E->getExprLoc();
2403 FunctionDecl *FD = 0;
2404 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002405
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002406 // gcc only allows for simple identifiers. Since we support more than gcc, we
2407 // will warn the user.
2408 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2409 if (DRE->hasQualifier())
2410 S.Diag(Loc, diag::warn_cleanup_ext);
2411 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2412 NI = DRE->getNameInfo();
2413 if (!FD) {
2414 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2415 << NI.getName();
2416 return;
2417 }
2418 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2419 if (ULE->hasExplicitTemplateArgs())
2420 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002421 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2422 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002423 if (!FD) {
2424 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2425 << NI.getName();
2426 if (ULE->getType() == S.Context.OverloadTy)
2427 S.NoteAllOverloadCandidates(ULE);
2428 return;
2429 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002430 } else {
2431 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002432 return;
2433 }
2434
Anders Carlssond277d792009-01-31 01:16:18 +00002435 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002436 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2437 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002438 return;
2439 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002440
Anders Carlsson723f55d2009-02-07 23:16:50 +00002441 // We're currently more strict than GCC about what function types we accept.
2442 // If this ever proves to be a problem it should be easy to fix.
2443 QualType Ty = S.Context.getPointerType(VD->getType());
2444 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002445 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2446 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002447 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2448 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002449 return;
2450 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002451
Michael Han99315932013-01-24 16:46:58 +00002452 D->addAttr(::new (S.Context)
2453 CleanupAttr(Attr.getRange(), S.Context, FD,
2454 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002455}
2456
Mike Stumpd3bb5572009-07-24 19:02:52 +00002457/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002458/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002459static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002460 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002461 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002462 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002463 return;
2464 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002465
Aaron Ballman00e99962013-08-31 01:11:41 +00002466 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002467 uint64_t ArgIdx;
2468 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2469 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002470 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002471
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002472 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002473 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002474
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002475 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2476 if (not_nsstring_type &&
2477 !isCFStringType(Ty, S.Context) &&
2478 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002479 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002480 // FIXME: Should highlight the actual expression that has the wrong type.
2481 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002482 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002483 << IdxExpr->getSourceRange();
2484 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002485 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002486 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002487 if (!isNSStringType(Ty, S.Context) &&
2488 !isCFStringType(Ty, S.Context) &&
2489 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002490 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002491 // FIXME: Should highlight the actual expression that has the wrong type.
2492 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002493 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002494 << IdxExpr->getSourceRange();
2495 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002496 }
2497
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002498 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2499 // because that has corrected for the implicit this parameter, and is zero-
2500 // based. The attribute expects what the user wrote explicitly.
2501 llvm::APSInt Val;
2502 IdxExpr->EvaluateAsInt(Val, S.Context);
2503
Michael Han99315932013-01-24 16:46:58 +00002504 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002505 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002506 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002507}
2508
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002509enum FormatAttrKind {
2510 CFStringFormat,
2511 NSStringFormat,
2512 StrftimeFormat,
2513 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002514 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002515 InvalidFormat
2516};
2517
2518/// getFormatAttrKind - Map from format attribute names to supported format
2519/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002520static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002521 return llvm::StringSwitch<FormatAttrKind>(Format)
2522 // Check for formats that get handled specially.
2523 .Case("NSString", NSStringFormat)
2524 .Case("CFString", CFStringFormat)
2525 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002526
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002527 // Otherwise, check for supported formats.
2528 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2529 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2530 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002531
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002532 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2533 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002534}
2535
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002536/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002537/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002538static void handleInitPriorityAttr(Sema &S, Decl *D,
2539 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002540 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002541 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2542 return;
2543 }
2544
Aaron Ballman4a611152013-11-27 16:34:09 +00002545 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002546 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2547 Attr.setInvalid();
2548 return;
2549 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002550 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002551 if (S.Context.getAsArrayType(T))
2552 T = S.Context.getBaseElementType(T);
2553 if (!T->getAs<RecordType>()) {
2554 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2555 Attr.setInvalid();
2556 return;
2557 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002558
2559 Expr *E = Attr.getArgAsExpr(0);
2560 uint32_t prioritynum;
2561 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002562 Attr.setInvalid();
2563 return;
2564 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002565
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002566 if (prioritynum < 101 || prioritynum > 65535) {
2567 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002568 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002569 Attr.setInvalid();
2570 return;
2571 }
Michael Han99315932013-01-24 16:46:58 +00002572 D->addAttr(::new (S.Context)
2573 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2574 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002575}
2576
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002577FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2578 IdentifierInfo *Format, int FormatIdx,
2579 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002580 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002581 // Check whether we already have an equivalent format attribute.
2582 for (specific_attr_iterator<FormatAttr>
2583 i = D->specific_attr_begin<FormatAttr>(),
2584 e = D->specific_attr_end<FormatAttr>();
2585 i != e ; ++i) {
2586 FormatAttr *f = *i;
2587 if (f->getType() == Format &&
2588 f->getFormatIdx() == FormatIdx &&
2589 f->getFirstArg() == FirstArg) {
2590 // If we don't have a valid location for this attribute, adopt the
2591 // location.
2592 if (f->getLocation().isInvalid())
2593 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002594 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002595 }
2596 }
2597
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002598 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2599 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002600}
2601
Mike Stumpd3bb5572009-07-24 19:02:52 +00002602/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002603/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002604static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002605 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002606 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002607 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002608 return;
2609 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002610
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002611 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002612 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002613 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002614 return;
2615 }
2616
Chandler Carruth743682b2010-11-16 08:35:43 +00002617 // In C++ the implicit 'this' function parameter also counts, and they are
2618 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002619 bool HasImplicitThisParam = isInstanceMethod(D);
2620 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002621
Aaron Ballman00e99962013-08-31 01:11:41 +00002622 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2623 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002624
2625 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002626 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002627 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002628 // If we've modified the string name, we need a new identifier for it.
2629 II = &S.Context.Idents.get(Format);
2630 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002631
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002632 // Check for supported formats.
2633 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002634
2635 if (Kind == IgnoredFormat)
2636 return;
2637
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002638 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002639 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman00e99962013-08-31 01:11:41 +00002640 << "format" << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002641 return;
2642 }
2643
2644 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002645 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002646 uint32_t Idx;
2647 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002648 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002649
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002650 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002651 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002652 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002653 return;
2654 }
2655
2656 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002657 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002658
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002659 if (HasImplicitThisParam) {
2660 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002661 S.Diag(Attr.getLoc(),
2662 diag::err_format_attribute_implicit_this_format_string)
2663 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002664 return;
2665 }
2666 ArgIdx--;
2667 }
Mike Stump11289f42009-09-09 15:08:12 +00002668
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002669 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002670 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002671
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002672 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002673 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002674 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2675 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002676 return;
2677 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002678 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002679 // FIXME: do we need to check if the type is NSString*? What are the
2680 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002681 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002682 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002683 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2684 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002685 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002686 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002687 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002688 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002689 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002690 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2691 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002692 return;
2693 }
2694
2695 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002696 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002697 uint32_t FirstArg;
2698 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002699 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002700
2701 // check if the function is variadic if the 3rd argument non-zero
2702 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002703 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002704 ++NumArgs; // +1 for ...
2705 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002706 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002707 return;
2708 }
2709 }
2710
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002711 // strftime requires FirstArg to be 0 because it doesn't read from any
2712 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002713 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002714 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002715 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2716 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002717 return;
2718 }
2719 // if 0 it disables parameter checking (to use with e.g. va_list)
2720 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002721 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002722 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002723 return;
2724 }
2725
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002726 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002727 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002728 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002729 if (NewAttr)
2730 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002731}
2732
Chandler Carruthedc2c642011-07-02 00:01:44 +00002733static void handleTransparentUnionAttr(Sema &S, Decl *D,
2734 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002735 // Try to find the underlying union declaration.
2736 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002737 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002738 if (TD && TD->getUnderlyingType()->isUnionType())
2739 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2740 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002741 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002742
2743 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002744 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002745 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002746 return;
2747 }
2748
John McCallf937c022011-10-07 06:10:15 +00002749 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002750 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002751 diag::warn_transparent_union_attribute_not_definition);
2752 return;
2753 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002754
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002755 RecordDecl::field_iterator Field = RD->field_begin(),
2756 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002757 if (Field == FieldEnd) {
2758 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2759 return;
2760 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002761
David Blaikie40ed2972012-06-06 20:45:41 +00002762 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002763 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002764 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002765 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002766 diag::warn_transparent_union_attribute_floating)
2767 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002768 return;
2769 }
2770
2771 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2772 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2773 for (; Field != FieldEnd; ++Field) {
2774 QualType FieldType = Field->getType();
2775 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2776 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2777 // Warn if we drop the attribute.
2778 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002779 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002780 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002781 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002782 diag::warn_transparent_union_attribute_field_size_align)
2783 << isSize << Field->getDeclName() << FieldBits;
2784 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002785 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002786 diag::note_transparent_union_first_field_size_align)
2787 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002788 return;
2789 }
2790 }
2791
Michael Han99315932013-01-24 16:46:58 +00002792 RD->addAttr(::new (S.Context)
2793 TransparentUnionAttr(Attr.getRange(), S.Context,
2794 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002795}
2796
Chandler Carruthedc2c642011-07-02 00:01:44 +00002797static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002798 // Make sure that there is a string literal as the annotation's single
2799 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002800 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002801 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002802 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002803
2804 // Don't duplicate annotations that are already set.
2805 for (specific_attr_iterator<AnnotateAttr>
2806 i = D->specific_attr_begin<AnnotateAttr>(),
2807 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002808 if ((*i)->getAnnotation() == Str)
2809 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002810 }
Michael Han99315932013-01-24 16:46:58 +00002811
2812 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002813 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002814 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002815}
2816
Chandler Carruthedc2c642011-07-02 00:01:44 +00002817static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002818 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002819 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002820 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2821 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002822 return;
2823 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002824
Richard Smith848e1f12013-02-01 08:12:08 +00002825 if (Attr.getNumArgs() == 0) {
2826 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2827 true, 0, Attr.getAttributeSpellingListIndex()));
2828 return;
2829 }
2830
Aaron Ballman00e99962013-08-31 01:11:41 +00002831 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002832 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2833 S.Diag(Attr.getEllipsisLoc(),
2834 diag::err_pack_expansion_without_parameter_packs);
2835 return;
2836 }
2837
2838 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2839 return;
2840
2841 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2842 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002843}
2844
2845void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002846 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002847 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2848 SourceLocation AttrLoc = AttrRange.getBegin();
2849
Richard Smith1dba27c2013-01-29 09:02:09 +00002850 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002851 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002852 // C++11 [dcl.align]p1:
2853 // An alignment-specifier may be applied to a variable or to a class
2854 // data member, but it shall not be applied to a bit-field, a function
2855 // parameter, the formal parameter of a catch clause, or a variable
2856 // declared with the register storage class specifier. An
2857 // alignment-specifier may also be applied to the declaration of a class
2858 // or enumeration type.
2859 // C11 6.7.5/2:
2860 // An alignment attribute shall not be specified in a declaration of
2861 // a typedef, or a bit-field, or a function, or a parameter, or an
2862 // object declared with the register storage-class specifier.
2863 int DiagKind = -1;
2864 if (isa<ParmVarDecl>(D)) {
2865 DiagKind = 0;
2866 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2867 if (VD->getStorageClass() == SC_Register)
2868 DiagKind = 1;
2869 if (VD->isExceptionVariable())
2870 DiagKind = 2;
2871 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2872 if (FD->isBitField())
2873 DiagKind = 3;
2874 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00002875 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
2876 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00002877 << (TmpAttr.isC11() ? ExpectedVariableOrField
2878 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002879 return;
2880 }
2881 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002882 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smithbc8caaf2013-02-22 04:55:39 +00002883 << TmpAttr.isC11() << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002884 return;
2885 }
2886 }
2887
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002888 if (E->isTypeDependent() || E->isValueDependent()) {
2889 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002890 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2891 AA->setPackExpansion(IsPackExpansion);
2892 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002893 return;
2894 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002895
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002896 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002897 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002898 ExprResult ICE
2899 = VerifyIntegerConstantExpression(E, &Alignment,
2900 diag::err_aligned_attribute_argument_not_int,
2901 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002902 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002903 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002904
2905 // C++11 [dcl.align]p2:
2906 // -- if the constant expression evaluates to zero, the alignment
2907 // specifier shall have no effect
2908 // C11 6.7.5p6:
2909 // An alignment specification of zero has no effect.
2910 if (!(TmpAttr.isAlignas() && !Alignment) &&
2911 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002912 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2913 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002914 return;
2915 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002916
Richard Smith848e1f12013-02-01 08:12:08 +00002917 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00002918 // We've already verified it's a power of 2, now let's make sure it's
2919 // 8192 or less.
2920 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00002921 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00002922 << E->getSourceRange();
2923 return;
2924 }
2925 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002926
Richard Smith44c247f2013-02-22 08:32:16 +00002927 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
2928 ICE.take(), SpellingListIndex);
2929 AA->setPackExpansion(IsPackExpansion);
2930 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002931}
2932
Michael Hanaf02bbe2013-02-01 01:19:17 +00002933void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002934 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002935 // FIXME: Cache the number on the Attr object if non-dependent?
2936 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002937 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2938 SpellingListIndex);
2939 AA->setPackExpansion(IsPackExpansion);
2940 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002941}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002942
Richard Smith848e1f12013-02-01 08:12:08 +00002943void Sema::CheckAlignasUnderalignment(Decl *D) {
2944 assert(D->hasAttrs() && "no attributes on decl");
2945
2946 QualType Ty;
2947 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2948 Ty = VD->getType();
2949 else
2950 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002951 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002952 return;
2953
2954 // C++11 [dcl.align]p5, C11 6.7.5/4:
2955 // The combined effect of all alignment attributes in a declaration shall
2956 // not specify an alignment that is less strict than the alignment that
2957 // would otherwise be required for the entity being declared.
2958 AlignedAttr *AlignasAttr = 0;
2959 unsigned Align = 0;
2960 for (specific_attr_iterator<AlignedAttr>
2961 I = D->specific_attr_begin<AlignedAttr>(),
2962 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
2963 if (I->isAlignmentDependent())
2964 return;
2965 if (I->isAlignas())
2966 AlignasAttr = *I;
2967 Align = std::max(Align, I->getAlignment(Context));
2968 }
2969
2970 if (AlignasAttr && Align) {
2971 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2972 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2973 if (NaturalAlign > RequestedAlign)
2974 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2975 << Ty << (unsigned)NaturalAlign.getQuantity();
2976 }
2977}
2978
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002979/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002980/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002981///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002982/// Despite what would be logical, the mode attribute is a decl attribute, not a
2983/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2984/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002985static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002986 // This attribute isn't documented, but glibc uses it. It changes
2987 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00002988 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00002989 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
2990 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002991 return;
2992 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002993
Aaron Ballman00e99962013-08-31 01:11:41 +00002994 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
2995 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002996
2997 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002998 if (Str.startswith("__") && Str.endswith("__"))
2999 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003000
3001 unsigned DestWidth = 0;
3002 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003003 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003004 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003005 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003006 switch (Str[0]) {
3007 case 'Q': DestWidth = 8; break;
3008 case 'H': DestWidth = 16; break;
3009 case 'S': DestWidth = 32; break;
3010 case 'D': DestWidth = 64; break;
3011 case 'X': DestWidth = 96; break;
3012 case 'T': DestWidth = 128; break;
3013 }
3014 if (Str[1] == 'F') {
3015 IntegerMode = false;
3016 } else if (Str[1] == 'C') {
3017 IntegerMode = false;
3018 ComplexMode = true;
3019 } else if (Str[1] != 'I') {
3020 DestWidth = 0;
3021 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003022 break;
3023 case 4:
3024 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3025 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003026 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003027 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003028 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003029 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003030 break;
3031 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003032 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003033 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003034 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003035 case 11:
3036 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003037 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003038 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003039 }
3040
3041 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003042 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003043 OldTy = TD->getUnderlyingType();
3044 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3045 OldTy = VD->getType();
3046 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003047 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003048 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003049 return;
3050 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003051
John McCall9dd450b2009-09-21 23:43:11 +00003052 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003053 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3054 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003055 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003056 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3057 } else if (ComplexMode) {
3058 if (!OldTy->isComplexType())
3059 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3060 } else {
3061 if (!OldTy->isFloatingType())
3062 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3063 }
3064
Mike Stump87c57ac2009-05-16 07:39:55 +00003065 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3066 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003067 // FIXME: Make sure floating-point mappings are accurate
3068 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003069 if (!DestWidth) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003070 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003071 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003072 }
3073
3074 QualType NewTy;
3075
3076 if (IntegerMode)
3077 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3078 OldTy->isSignedIntegerType());
3079 else
3080 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3081
3082 if (NewTy.isNull()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003083 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003084 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003085 }
3086
Eli Friedman4735374e2009-03-03 06:41:03 +00003087 if (ComplexMode) {
3088 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003089 }
3090
3091 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003092 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3093 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3094 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003095 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003096
3097 D->addAttr(::new (S.Context)
3098 ModeAttr(Attr.getRange(), S.Context, Name,
3099 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003100}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003101
Chandler Carruthedc2c642011-07-02 00:01:44 +00003102static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003103 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3104 if (!VD->hasGlobalStorage())
3105 S.Diag(Attr.getLoc(),
3106 diag::warn_attribute_requires_functions_or_static_globals)
3107 << Attr.getName();
3108 } else if (!isFunctionOrMethod(D)) {
3109 S.Diag(Attr.getLoc(),
3110 diag::warn_attribute_requires_functions_or_static_globals)
3111 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003112 return;
3113 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003114
Michael Han99315932013-01-24 16:46:58 +00003115 D->addAttr(::new (S.Context)
3116 NoDebugAttr(Attr.getRange(), S.Context,
3117 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003118}
3119
Chandler Carruthedc2c642011-07-02 00:01:44 +00003120static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003121 FunctionDecl *FD = cast<FunctionDecl>(D);
3122 if (!FD->getResultType()->isVoidType()) {
3123 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
3124 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
3125 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3126 << FD->getType()
3127 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
3128 "void");
3129 } else {
3130 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3131 << FD->getType();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003132 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003133 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003134 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003135
Aaron Ballman3aff6332013-12-02 19:30:36 +00003136 D->addAttr(::new (S.Context)
3137 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00003138 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003139}
3140
Chandler Carruthedc2c642011-07-02 00:01:44 +00003141static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003142 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003143 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003144 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003145 return;
3146 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003147
Michael Han99315932013-01-24 16:46:58 +00003148 D->addAttr(::new (S.Context)
3149 GNUInlineAttr(Attr.getRange(), S.Context,
3150 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003151}
3152
Chandler Carruthedc2c642011-07-02 00:01:44 +00003153static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003154 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003155
Aaron Ballman02df2e02012-12-09 17:45:41 +00003156 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003157 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003158 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3159 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003160 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003161 return;
3162
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003163 if (!isa<ObjCMethodDecl>(D)) {
3164 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3165 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003166 return;
3167 }
3168
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003169 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003170 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003171 D->addAttr(::new (S.Context)
3172 FastCallAttr(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_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003176 D->addAttr(::new (S.Context)
3177 StdCallAttr(Attr.getRange(), S.Context,
3178 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003179 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003180 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003181 D->addAttr(::new (S.Context)
3182 ThisCallAttr(Attr.getRange(), S.Context,
3183 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003184 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003185 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003186 D->addAttr(::new (S.Context)
3187 CDeclAttr(Attr.getRange(), S.Context,
3188 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003189 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003190 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003191 D->addAttr(::new (S.Context)
3192 PascalAttr(Attr.getRange(), S.Context,
3193 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003194 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003195 case AttributeList::AT_MSABI:
3196 D->addAttr(::new (S.Context)
3197 MSABIAttr(Attr.getRange(), S.Context,
3198 Attr.getAttributeSpellingListIndex()));
3199 return;
3200 case AttributeList::AT_SysVABI:
3201 D->addAttr(::new (S.Context)
3202 SysVABIAttr(Attr.getRange(), S.Context,
3203 Attr.getAttributeSpellingListIndex()));
3204 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003205 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003206 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003207 switch (CC) {
3208 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003209 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003210 break;
3211 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003212 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003213 break;
3214 default:
3215 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003216 }
3217
Michael Han99315932013-01-24 16:46:58 +00003218 D->addAttr(::new (S.Context)
3219 PcsAttr(Attr.getRange(), S.Context, PCS,
3220 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003221 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003222 }
Derek Schuffa2020962012-10-16 22:30:41 +00003223 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003224 D->addAttr(::new (S.Context)
3225 PnaclCallAttr(Attr.getRange(), S.Context,
3226 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003227 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003228 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003229 D->addAttr(::new (S.Context)
3230 IntelOclBiccAttr(Attr.getRange(), S.Context,
3231 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003232 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003233
Abramo Bagnara50099372010-04-30 13:10:51 +00003234 default:
3235 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003236 }
3237}
3238
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003239static void handleOpenCLImageAccessAttr(Sema &S, Decl *D,
3240 const AttributeList &Attr) {
3241 uint32_t ArgNum;
3242 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), ArgNum))
Guy Benyeifb36ede2013-03-24 13:58:12 +00003243 return;
Guy Benyeifb36ede2013-03-24 13:58:12 +00003244
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003245 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(Attr.getRange(),
3246 S.Context, ArgNum));
Guy Benyeifb36ede2013-03-24 13:58:12 +00003247}
3248
Aaron Ballman02df2e02012-12-09 17:45:41 +00003249bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3250 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003251 if (attr.isInvalid())
3252 return true;
3253
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003254 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003255 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003256 attr.setInvalid();
3257 return true;
3258 }
3259
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003260 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3261 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003262 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003263 case AttributeList::AT_CDecl: CC = CC_C; break;
3264 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3265 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3266 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3267 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003268 case AttributeList::AT_MSABI:
3269 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3270 CC_X86_64Win64;
3271 break;
3272 case AttributeList::AT_SysVABI:
3273 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3274 CC_C;
3275 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003276 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003277 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003278 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003279 attr.setInvalid();
3280 return true;
3281 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003282 if (StrRef == "aapcs") {
3283 CC = CC_AAPCS;
3284 break;
3285 } else if (StrRef == "aapcs-vfp") {
3286 CC = CC_AAPCS_VFP;
3287 break;
3288 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003289
3290 attr.setInvalid();
3291 Diag(attr.getLoc(), diag::err_invalid_pcs);
3292 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003293 }
Derek Schuffa2020962012-10-16 22:30:41 +00003294 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003295 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003296 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003297 }
3298
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003299 const TargetInfo &TI = Context.getTargetInfo();
3300 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3301 if (A == TargetInfo::CCCR_Warning) {
3302 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003303
3304 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3305 if (FD)
3306 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3307 TargetInfo::CCMT_NonMember;
3308 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003309 }
3310
John McCall3882ace2011-01-05 12:14:39 +00003311 return false;
3312}
3313
Chandler Carruthedc2c642011-07-02 00:01:44 +00003314static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003315 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003316
3317 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003318 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003319 return;
3320
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003321 if (!isa<ObjCMethodDecl>(D)) {
3322 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3323 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003324 return;
3325 }
Eli Friedman7044b762009-03-27 21:06:47 +00003326
Michael Han99315932013-01-24 16:46:58 +00003327 D->addAttr(::new (S.Context)
3328 RegparmAttr(Attr.getRange(), S.Context, numParams,
3329 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00003330}
3331
3332/// Checks a regparm attribute, returning true if it is ill-formed and
3333/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003334bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3335 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003336 return true;
3337
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003338 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003339 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003340 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003341 }
Eli Friedman7044b762009-03-27 21:06:47 +00003342
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003343 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003344 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003345 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
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
Douglas Gregore8bbc122011-09-02 00:18:52 +00003350 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003351 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003352 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003353 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003354 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003355 }
3356
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003357 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003358 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003359 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003360 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003361 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003362 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003363 }
3364
John McCall3882ace2011-01-05 12:14:39 +00003365 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003366}
3367
Chandler Carruthedc2c642011-07-02 00:01:44 +00003368static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Aaron Ballman3aff6332013-12-02 19:30:36 +00003369 // check the attribute arguments.
3370 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
3371 // FIXME: 0 is not okay.
3372 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
3373 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003374 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003375
3376 if (!isFunctionOrMethod(D)) {
3377 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3378 << Attr.getName() << ExpectedFunctionOrMethod;
3379 return;
3380 }
3381
3382 uint32_t MaxThreads, MinBlocks;
3383 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1) ||
3384 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(1), MinBlocks, 2))
3385 return;
3386
3387 D->addAttr(::new (S.Context)
3388 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3389 MaxThreads, MinBlocks,
3390 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003391}
3392
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003393static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3394 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003395 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003396 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003397 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003398 return;
3399 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003400
3401 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003402 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003403
Aaron Ballman00e99962013-08-31 01:11:41 +00003404 StringRef AttrName = Attr.getName()->getName();
3405 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003406
3407 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3408 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3409 << Attr.getName() << ExpectedFunctionOrMethod;
3410 return;
3411 }
3412
3413 uint64_t ArgumentIdx;
3414 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3415 Attr.getLoc(), 2,
Aaron Ballman00e99962013-08-31 01:11:41 +00003416 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003417 return;
3418
3419 uint64_t TypeTagIdx;
3420 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3421 Attr.getLoc(), 3,
Aaron Ballman00e99962013-08-31 01:11:41 +00003422 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003423 return;
3424
3425 bool IsPointer = (AttrName == "pointer_with_type_tag");
3426 if (IsPointer) {
3427 // Ensure that buffer has a pointer type.
3428 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3429 if (!BufferTy->isPointerType()) {
3430 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003431 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003432 }
3433 }
3434
Michael Han99315932013-01-24 16:46:58 +00003435 D->addAttr(::new (S.Context)
3436 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3437 ArgumentIdx, TypeTagIdx, IsPointer,
3438 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003439}
3440
3441static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3442 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003443 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003444 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003445 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003446 return;
3447 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003448
3449 if (!checkAttributeNumArgs(S, Attr, 1))
3450 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003451
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003452 if (!isa<VarDecl>(D)) {
3453 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3454 << Attr.getName() << ExpectedVariable;
3455 return;
3456 }
3457
Aaron Ballman00e99962013-08-31 01:11:41 +00003458 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003459 TypeSourceInfo *MatchingCTypeLoc = 0;
3460 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3461 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003462
Michael Han99315932013-01-24 16:46:58 +00003463 D->addAttr(::new (S.Context)
3464 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003465 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003466 Attr.getLayoutCompatible(),
3467 Attr.getMustBeNull(),
3468 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003469}
3470
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003471//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003472// Checker-specific attribute handlers.
3473//===----------------------------------------------------------------------===//
3474
John McCalled433932011-01-25 03:31:58 +00003475static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003476 return type->isDependentType() ||
3477 type->isObjCObjectPointerType() ||
3478 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003479}
3480static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003481 return type->isDependentType() ||
3482 type->isPointerType() ||
3483 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003484}
3485
Chandler Carruthedc2c642011-07-02 00:01:44 +00003486static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003487 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003488 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003489
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003490 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003491 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3492 cf = false;
3493 } else {
3494 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3495 cf = true;
3496 }
3497
3498 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003499 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003500 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003501 return;
3502 }
3503
3504 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003505 param->addAttr(::new (S.Context)
3506 CFConsumedAttr(Attr.getRange(), S.Context,
3507 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003508 else
Michael Han99315932013-01-24 16:46:58 +00003509 param->addAttr(::new (S.Context)
3510 NSConsumedAttr(Attr.getRange(), S.Context,
3511 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003512}
3513
Chandler Carruthedc2c642011-07-02 00:01:44 +00003514static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3515 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003516
John McCalled433932011-01-25 03:31:58 +00003517 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003518
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003519 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003520 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003521 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003522 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003523 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003524 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3525 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003526 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003527 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003528 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003529 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003530 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003531 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003532 return;
3533 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003534
John McCalled433932011-01-25 03:31:58 +00003535 bool typeOK;
3536 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003537 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003538 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003539 case AttributeList::AT_NSReturnsAutoreleased:
3540 case AttributeList::AT_NSReturnsRetained:
3541 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003542 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3543 cf = false;
3544 break;
3545
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003546 case AttributeList::AT_CFReturnsRetained:
3547 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003548 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3549 cf = true;
3550 break;
3551 }
3552
3553 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003554 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003555 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003556 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003557 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003558
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003559 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003560 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003561 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003562 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003563 D->addAttr(::new (S.Context)
3564 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3565 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003566 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003567 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003568 D->addAttr(::new (S.Context)
3569 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3570 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003571 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003572 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003573 D->addAttr(::new (S.Context)
3574 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3575 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003576 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003577 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003578 D->addAttr(::new (S.Context)
3579 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3580 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003581 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003582 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003583 D->addAttr(::new (S.Context)
3584 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3585 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003586 return;
3587 };
3588}
3589
John McCallcf166702011-07-22 08:53:00 +00003590static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3591 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003592 const int EP_ObjCMethod = 1;
3593 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003594
John McCallcf166702011-07-22 08:53:00 +00003595 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003596 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003597 if (isa<ObjCMethodDecl>(D))
3598 resultType = cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003599 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003600 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003601
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003602 if (!resultType->isReferenceType() &&
3603 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003604 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003605 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003606 << attr.getName()
3607 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003608 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003609
3610 // Drop the attribute.
3611 return;
3612 }
3613
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003614 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003615 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3616 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003617}
3618
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003619static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3620 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003621 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003622
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003623 DeclContext *DC = method->getDeclContext();
3624 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3625 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3626 << attr.getName() << 0;
3627 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3628 return;
3629 }
3630 if (method->getMethodFamily() == OMF_dealloc) {
3631 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3632 << attr.getName() << 1;
3633 return;
3634 }
3635
Michael Han99315932013-01-24 16:46:58 +00003636 method->addAttr(::new (S.Context)
3637 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3638 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003639}
3640
Aaron Ballmanfb763042013-12-02 18:05:46 +00003641static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3642 const AttributeList &Attr) {
3643 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr,
3644 "cf_unknown_transfer"))
John McCall32f5fe12011-09-30 05:12:12 +00003645 return;
John McCall32f5fe12011-09-30 05:12:12 +00003646
Aaron Ballmanfb763042013-12-02 18:05:46 +00003647 D->addAttr(::new (S.Context)
3648 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3649 Attr.getAttributeSpellingListIndex()));
3650}
3651
3652static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3653 const AttributeList &Attr) {
3654 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr,
3655 "cf_audited_transfer"))
3656 return;
3657
3658 D->addAttr(::new (S.Context)
3659 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3660 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003661}
3662
John McCallf1e8b342011-09-29 07:17:38 +00003663static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
3664 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003665 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallf1e8b342011-09-29 07:17:38 +00003666
3667 // In Objective-C, verify that the type names an Objective-C type.
3668 // We don't want to check this outside of ObjC because people sometimes
3669 // do crazy C declarations of Objective-C types.
Aaron Ballman00e99962013-08-31 01:11:41 +00003670 if (Parm && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00003671 // Check for an existing type with this name.
Aaron Ballman00e99962013-08-31 01:11:41 +00003672 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallf1e8b342011-09-29 07:17:38 +00003673 Sema::LookupOrdinaryName);
3674 if (S.LookupName(R, Sc)) {
3675 NamedDecl *Target = R.getFoundDecl();
3676 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
3677 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
3678 S.Diag(Target->getLocStart(), diag::note_declared_at);
3679 }
3680 }
3681 }
3682
Michael Han99315932013-01-24 16:46:58 +00003683 D->addAttr(::new (S.Context)
Aaron Ballman00e99962013-08-31 01:11:41 +00003684 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han99315932013-01-24 16:46:58 +00003685 Attr.getAttributeSpellingListIndex()));
John McCallf1e8b342011-09-29 07:17:38 +00003686}
3687
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003688static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3689 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003690 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003691
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003692 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003693 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003694 return;
3695 }
3696
3697 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003698 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003699 Attr.getAttributeSpellingListIndex()));
3700}
3701
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003702static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3703 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003704 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003705
3706 if (!Parm) {
3707 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3708 return;
3709 }
3710
3711 D->addAttr(::new (S.Context)
3712 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3713 Attr.getAttributeSpellingListIndex()));
3714}
3715
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003716static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3717 const AttributeList &Attr) {
3718 IdentifierInfo *RelatedClass =
3719 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0;
3720 if (!RelatedClass) {
3721 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3722 return;
3723 }
3724 IdentifierInfo *ClassMethod =
3725 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0;
3726 IdentifierInfo *InstanceMethod =
3727 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0;
3728 D->addAttr(::new (S.Context)
3729 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3730 ClassMethod, InstanceMethod,
3731 Attr.getAttributeSpellingListIndex()));
3732}
3733
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003734static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3735 const AttributeList &Attr) {
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003736 ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003737 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003738 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003739 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3740 Attr.getAttributeSpellingListIndex()));
3741}
3742
Chandler Carruthedc2c642011-07-02 00:01:44 +00003743static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3744 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003745 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003746
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003747 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003748 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003749}
3750
Chandler Carruthedc2c642011-07-02 00:01:44 +00003751static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3752 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003753 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003754 QualType type = vd->getType();
3755
3756 if (!type->isDependentType() &&
3757 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003758 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003759 << type;
3760 return;
3761 }
3762
3763 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3764
3765 // If we have no lifetime yet, check the lifetime we're presumably
3766 // going to infer.
3767 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3768 lifetime = type->getObjCARCImplicitLifetime();
3769
3770 switch (lifetime) {
3771 case Qualifiers::OCL_None:
3772 assert(type->isDependentType() &&
3773 "didn't infer lifetime for non-dependent type?");
3774 break;
3775
3776 case Qualifiers::OCL_Weak: // meaningful
3777 case Qualifiers::OCL_Strong: // meaningful
3778 break;
3779
3780 case Qualifiers::OCL_ExplicitNone:
3781 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003782 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003783 << (lifetime == Qualifiers::OCL_Autoreleasing);
3784 break;
3785 }
3786
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003787 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003788 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3789 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003790}
3791
Francois Picheta83957a2010-12-19 06:50:37 +00003792//===----------------------------------------------------------------------===//
3793// Microsoft specific attribute handlers.
3794//===----------------------------------------------------------------------===//
3795
Chandler Carruthedc2c642011-07-02 00:01:44 +00003796static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003797 if (!S.LangOpts.CPlusPlus) {
3798 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3799 << Attr.getName() << AttributeLangSupport::C;
3800 return;
3801 }
3802
Aaron Ballman60e705e2013-11-24 20:58:02 +00003803 if (!isa<CXXRecordDecl>(D)) {
3804 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3805 << Attr.getName() << ExpectedClass;
3806 return;
3807 }
3808
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003809 StringRef StrRef;
3810 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003811 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003812 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003813
David Majnemer89085342013-08-09 08:56:20 +00003814 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3815 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003816 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3817 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003818
Reid Kleckner140c4a72013-05-17 14:04:52 +00003819 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003820 if (StrRef.size() != 36) {
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;
3823 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003824
David Majnemer89085342013-08-09 08:56:20 +00003825 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003826 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003827 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003828 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003829 return;
3830 }
David Majnemer89085342013-08-09 08:56:20 +00003831 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003832 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003833 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003834 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003835 }
Francois Picheta83957a2010-12-19 06:50:37 +00003836
David Majnemer89085342013-08-09 08:56:20 +00003837 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3838 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003839}
3840
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003841/// Handles semantic checking for features that are common to all attributes,
3842/// such as checking whether a parameter was properly specified, or the correct
3843/// number of arguments were passed, etc.
3844static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
3845 const AttributeList &Attr) {
3846 // Several attributes carry different semantics than the parsing requires, so
3847 // those are opted out of the common handling.
3848 //
3849 // We also bail on unknown and ignored attributes because those are handled
3850 // as part of the target-specific handling logic.
3851 if (Attr.hasCustomParsing() ||
3852 Attr.getKind() == AttributeList::UnknownAttribute ||
3853 Attr.getKind() == AttributeList::IgnoredAttribute)
3854 return false;
3855
Aaron Ballman3aff6332013-12-02 19:30:36 +00003856 // Check whether the attribute requires specific language extensions to be
3857 // enabled.
3858 if (!Attr.diagnoseLangOpts(S))
3859 return true;
3860
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003861 // If there are no optional arguments, then checking for the argument count
3862 // is trivial.
3863 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
3864 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
3865 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003866
3867 // Check whether the attribute appertains to the given subject.
3868 if (!Attr.diagnoseAppertainsTo(S, D))
3869 return true;
3870
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003871 return false;
3872}
3873
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003874//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003875// Top Level Sema Entry Points
3876//===----------------------------------------------------------------------===//
3877
Richard Smithf8a75c32013-08-29 00:47:48 +00003878/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3879/// the attribute applies to decls. If the attribute is a type attribute, just
3880/// silently ignore it if a GNU attribute.
3881static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3882 const AttributeList &Attr,
3883 bool IncludeCXX11Attributes) {
3884 if (Attr.isInvalid())
3885 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003886
Richard Smithf8a75c32013-08-29 00:47:48 +00003887 // Ignore C++11 attributes on declarator chunks: they appertain to the type
3888 // instead.
3889 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
3890 return;
3891
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003892 if (handleCommonAttributeFeatures(S, scope, D, Attr))
3893 return;
3894
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003895 switch (Attr.getKind()) {
Aaron Ballman9beb5172013-12-02 15:13:14 +00003896 case AttributeList::AT_IBAction:
3897 handleSimpleAttribute<IBActionAttr>(S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003898 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
3899 case AttributeList::AT_IBOutletCollection:
3900 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003901 case AttributeList::AT_AddressSpace:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003902 case AttributeList::AT_ObjCGC:
3903 case AttributeList::AT_VectorSize:
3904 case AttributeList::AT_NeonVectorType:
3905 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballman317a77f2013-05-22 23:25:32 +00003906 case AttributeList::AT_Ptr32:
3907 case AttributeList::AT_Ptr64:
3908 case AttributeList::AT_SPtr:
3909 case AttributeList::AT_UPtr:
Mike Stumpd3bb5572009-07-24 19:02:52 +00003910 // Ignore these, these are type attributes, handled by
3911 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003912 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003913 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
3914 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
3915 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
3916 case AttributeList::AT_AlwaysInline:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003917 handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003918 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003919 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00003920 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003921 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
3922 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
3923 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00003924 handleDependencyAttr(S, scope, D, Attr);
3925 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003926 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003927 case AttributeList::AT_CUDAConstant:
3928 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003929 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003930 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman3a8e2d92013-11-27 18:53:58 +00003931 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003932 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00003933 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00003934 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003935 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
3936 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003937 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003938 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00003939 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003940 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00003941 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003942 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
3943 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
3944 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00003945 case AttributeList::AT_CUDADevice:
3946 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003947 case AttributeList::AT_CUDAHost:
3948 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003949 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
3950 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003951 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00003952 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003953 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003954 case AttributeList::AT_MayAlias:
3955 handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00003956 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003957 case AttributeList::AT_NoCommon:
3958 handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003959 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003960 case AttributeList::AT_Overloadable:
3961 handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break;
Richard Smith852e9ce2013-11-27 01:46:48 +00003962 case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003963 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
3964 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003965 case AttributeList::AT_Naked:
3966 handleSimpleAttribute<NakedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003967 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
3968 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003969 case AttributeList::AT_CUDAShared:
3970 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003971 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003972
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003973 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003974 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003975 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003976 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003977
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003978 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00003979 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3980
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003981 case AttributeList::AT_ObjCRequiresSuper:
3982 handleObjCRequiresSuperAttr(S, D, Attr); break;
3983
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003984 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00003985 handleNSBridgedAttr(S, scope, D, Attr); break;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003986
3987 case AttributeList::AT_ObjCBridge:
3988 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003989
3990 case AttributeList::AT_ObjCBridgeMutable:
3991 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003992
3993 case AttributeList::AT_ObjCBridgeRelated:
3994 handleObjCBridgeRelatedAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00003995
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003996 case AttributeList::AT_ObjCDesignatedInitializer:
3997 handleObjCDesignatedInitializer(S, D, Attr); break;
3998
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003999 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00004000 handleCFAuditedTransferAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004001 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00004002 handleCFUnknownTransferAttr(S, D, Attr); break;
John McCall32f5fe12011-09-30 05:12:12 +00004003
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004004 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004005 case AttributeList::AT_CFConsumed:
4006 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4007 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004008 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004009
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004010 case AttributeList::AT_NSReturnsAutoreleased:
4011 case AttributeList::AT_NSReturnsNotRetained:
4012 case AttributeList::AT_CFReturnsNotRetained:
4013 case AttributeList::AT_NSReturnsRetained:
4014 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004015 handleNSReturnsRetainedAttr(S, D, Attr); break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004016 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00004017 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004018 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00004019 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004020 case AttributeList::AT_VecTypeHint:
4021 handleVecTypeHint(S, D, Attr); break;
4022
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004023 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004024 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004025
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004026 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4027 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4028 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004029 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004030 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004031 case AttributeList::AT_ArcWeakrefUnavailable:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004032 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004033 case AttributeList::AT_ObjCRootClass:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004034 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break;
Ted Kremenek28eace62013-11-23 01:01:34 +00004035 case AttributeList::AT_ObjCSuppressProtocol:
4036 handleObjCSuppresProtocolAttr(S, D, Attr);
4037 break;
4038 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004039 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004040 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4041 case AttributeList::AT_ReturnsTwice:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004042 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004043 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004044 case AttributeList::AT_Visibility:
4045 handleVisibilityAttr(S, D, Attr, false);
4046 break;
4047 case AttributeList::AT_TypeVisibility:
4048 handleVisibilityAttr(S, D, Attr, true);
4049 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004050 case AttributeList::AT_WarnUnused:
Aaron Ballman6a42b5a2013-11-27 16:59:17 +00004051 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004052 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004053 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004054 case AttributeList::AT_Weak:
4055 handleSimpleAttribute<WeakAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004056 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4057 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4058 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004059 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004060 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004061 case AttributeList::AT_ObjCException:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004062 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004063 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004064 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004065 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004066 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4067 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4068 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4069 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004070 case AttributeList::AT_Pure:
4071 handleSimpleAttribute<PureAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004072 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4073 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004074 case AttributeList::AT_NoInline:
4075 handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004076 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004077 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004078 // Just ignore
4079 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004080 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004081 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004082 case AttributeList::AT_StdCall:
4083 case AttributeList::AT_CDecl:
4084 case AttributeList::AT_FastCall:
4085 case AttributeList::AT_ThisCall:
4086 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004087 case AttributeList::AT_MSABI:
4088 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004089 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004090 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004091 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004092 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004093 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004094 case AttributeList::AT_OpenCLKernel:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004095 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004096 case AttributeList::AT_OpenCLImageAccess:
4097 handleOpenCLImageAccessAttr(S, D, Attr);
4098 break;
John McCall8d32c052012-05-22 21:28:12 +00004099
4100 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004101 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004102 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004103 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004104 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004105 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004106 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004107 case AttributeList::AT_SingleInheritance:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004108 handleSimpleAttribute<SingleInheritanceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004109 case AttributeList::AT_MultipleInheritance:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004110 handleSimpleAttribute<MultipleInheritanceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004111 case AttributeList::AT_VirtualInheritance:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004112 handleSimpleAttribute<VirtualInheritanceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004113 case AttributeList::AT_ForceInline:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004114 handleSimpleAttribute<ForceInlineAttr>(S, D, Attr); break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004115 case AttributeList::AT_SelectAny:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004116 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004117
4118 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004119 case AttributeList::AT_AssertExclusiveLock:
4120 handleAssertExclusiveLockAttr(S, D, Attr);
4121 break;
4122 case AttributeList::AT_AssertSharedLock:
4123 handleAssertSharedLockAttr(S, D, Attr);
4124 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004125 case AttributeList::AT_GuardedVar:
Aaron Ballmane61b8b82013-12-02 15:02:49 +00004126 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004127 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004128 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004129 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004130 case AttributeList::AT_ScopedLockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004131 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004132 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004133 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004134 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004135 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004136 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004137 break;
4138 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004139 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004140 break;
4141 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004142 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004143 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004144 case AttributeList::AT_Lockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004145 handleSimpleAttribute<LockableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004146 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004147 handleGuardedByAttr(S, D, Attr);
4148 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004149 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004150 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004151 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004152 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004153 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004154 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004155 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004156 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004157 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004158 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004159 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004160 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004161 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004162 handleLockReturnedAttr(S, D, Attr);
4163 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004164 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004165 handleLocksExcludedAttr(S, D, Attr);
4166 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004167 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004168 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004169 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004170 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004171 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004172 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004173 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004174 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004175 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004176 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004177 handleUnlockFunAttr(S, D, Attr);
4178 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004179 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004180 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004181 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004182 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004183 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004184 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004185
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004186 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004187 case AttributeList::AT_Consumable:
4188 handleConsumableAttr(S, D, Attr);
4189 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004190 case AttributeList::AT_CallableWhen:
4191 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004192 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004193 case AttributeList::AT_ParamTypestate:
4194 handleParamTypestateAttr(S, D, Attr);
4195 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004196 case AttributeList::AT_ReturnTypestate:
4197 handleReturnTypestateAttr(S, D, Attr);
4198 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004199 case AttributeList::AT_SetTypestate:
4200 handleSetTypestateAttr(S, D, Attr);
4201 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004202 case AttributeList::AT_TestTypestate:
4203 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004204 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004205
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004206 // Type safety attributes.
4207 case AttributeList::AT_ArgumentWithTypeTag:
4208 handleArgumentWithTypeTagAttr(S, D, Attr);
4209 break;
4210 case AttributeList::AT_TypeTagForDatatype:
4211 handleTypeTagForDatatypeAttr(S, D, Attr);
4212 break;
4213
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004214 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004215 // Ask target about the attribute.
4216 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4217 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004218 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4219 diag::warn_unhandled_ms_attribute_ignored :
4220 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004221 break;
4222 }
4223}
4224
4225/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4226/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004227void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004228 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004229 bool IncludeCXX11Attributes) {
4230 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004231 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004232
4233 // GCC accepts
4234 // static int a9 __attribute__((weakref));
4235 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004236 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004237 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00004238 cast<NamedDecl>(D)->getNameAsString();
4239 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004240 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004241 }
4242}
4243
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004244// Annotation attributes are the only attributes allowed after an access
4245// specifier.
4246bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4247 const AttributeList *AttrList) {
4248 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004249 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004250 handleAnnotateAttr(*this, ASDecl, *l);
4251 } else {
4252 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4253 return true;
4254 }
4255 }
4256
4257 return false;
4258}
4259
John McCall42856de2011-10-01 05:17:03 +00004260/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4261/// contains any decl attributes that we should warn about.
4262static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4263 for ( ; A; A = A->getNext()) {
4264 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004265 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004266 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4267
4268 if (A->getKind() == AttributeList::UnknownAttribute) {
4269 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4270 << A->getName() << A->getRange();
4271 } else {
4272 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4273 << A->getName() << A->getRange();
4274 }
4275 }
4276}
4277
4278/// checkUnusedDeclAttributes - Given a declarator which is not being
4279/// used to build a declaration, complain about any decl attributes
4280/// which might be lying around on it.
4281void Sema::checkUnusedDeclAttributes(Declarator &D) {
4282 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4283 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4284 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4285 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4286}
4287
Ryan Flynn7d470f32009-07-30 03:15:39 +00004288/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004289/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004290NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4291 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004292 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004293 NamedDecl *NewD = 0;
4294 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004295 FunctionDecl *NewFD;
4296 // FIXME: Missing call to CheckFunctionDeclaration().
4297 // FIXME: Mangling?
4298 // FIXME: Is the qualifier info correct?
4299 // FIXME: Is the DeclContext correct?
4300 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4301 Loc, Loc, DeclarationName(II),
4302 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004303 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004304 FD->hasPrototype(),
4305 false/*isConstexprSpecified*/);
4306 NewD = NewFD;
4307
4308 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004309 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004310
4311 // Fake up parameter variables; they are declared as if this were
4312 // a typedef.
4313 QualType FDTy = FD->getType();
4314 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4315 SmallVector<ParmVarDecl*, 16> Params;
4316 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4317 AE = FT->arg_type_end(); AI != AE; ++AI) {
4318 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4319 Param->setScopeInfo(0, Params.size());
4320 Params.push_back(Param);
4321 }
David Blaikie9c70e042011-09-21 18:16:56 +00004322 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004323 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004324 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4325 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004326 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004327 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004328 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004329 if (VD->getQualifier()) {
4330 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004331 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004332 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004333 }
4334 return NewD;
4335}
4336
James Dennett634962f2012-06-14 21:40:34 +00004337/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004338/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004339void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004340 if (W.getUsed()) return; // only do this once
4341 W.setUsed(true);
4342 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4343 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004344 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004345 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4346 NDId->getName()));
4347 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00004348 WeakTopLevelDecl.push_back(NewD);
4349 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4350 // to insert Decl at TU scope, sorry.
4351 DeclContext *SavedContext = CurContext;
4352 CurContext = Context.getTranslationUnitDecl();
4353 PushOnScopeChains(NewD, S);
4354 CurContext = SavedContext;
4355 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004356 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004357 }
4358}
4359
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004360void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4361 // It's valid to "forward-declare" #pragma weak, in which case we
4362 // have to do this.
4363 LoadExternalWeakUndeclaredIdentifiers();
4364 if (!WeakUndeclaredIdentifiers.empty()) {
4365 NamedDecl *ND = NULL;
4366 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4367 if (VD->isExternC())
4368 ND = VD;
4369 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4370 if (FD->isExternC())
4371 ND = FD;
4372 if (ND) {
4373 if (IdentifierInfo *Id = ND->getIdentifier()) {
4374 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4375 = WeakUndeclaredIdentifiers.find(Id);
4376 if (I != WeakUndeclaredIdentifiers.end()) {
4377 WeakInfo W = I->second;
4378 DeclApplyPragmaWeak(S, ND, W);
4379 WeakUndeclaredIdentifiers[Id] = W;
4380 }
4381 }
4382 }
4383 }
4384}
4385
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004386/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4387/// it, apply them to D. This is a bit tricky because PD can have attributes
4388/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004389void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004390 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004391 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004392 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004393
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004394 // Walk the declarator structure, applying decl attributes that were in a type
4395 // position to the decl itself. This handles cases like:
4396 // int *__attr__(x)** D;
4397 // when X is a decl attribute.
4398 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4399 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004400 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004401
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004402 // Finally, apply any attributes on the decl itself.
4403 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004404 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004405}
John McCall28a6aea2009-11-04 02:18:39 +00004406
John McCall31168b02011-06-15 23:02:42 +00004407/// Is the given declaration allowed to use a forbidden type?
4408static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4409 // Private ivars are always okay. Unfortunately, people don't
4410 // always properly make their ivars private, even in system headers.
4411 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004412 // Function declarations in sys headers will be marked unavailable.
4413 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4414 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004415 return false;
4416
4417 // Require it to be declared in a system header.
4418 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4419}
4420
4421/// Handle a delayed forbidden-type diagnostic.
4422static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4423 Decl *decl) {
4424 if (decl && isForbiddenTypeAllowed(S, decl)) {
4425 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4426 "this system declaration uses an unsupported type"));
4427 return;
4428 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004429 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004430 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004431 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004432 // kind of forbidden type messages on unavailable functions.
4433 if (FD->hasAttr<UnavailableAttr>() &&
4434 diag.getForbiddenTypeDiagnostic() ==
4435 diag::err_arc_array_param_no_ownership) {
4436 diag.Triggered = true;
4437 return;
4438 }
4439 }
John McCall31168b02011-06-15 23:02:42 +00004440
4441 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4442 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4443 diag.Triggered = true;
4444}
4445
John McCall2ec85372012-05-07 06:16:41 +00004446void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4447 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004448 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004449 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004450
John McCall2ec85372012-05-07 06:16:41 +00004451 // When delaying diagnostics to run in the context of a parsed
4452 // declaration, we only want to actually emit anything if parsing
4453 // succeeds.
4454 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004455
John McCall2ec85372012-05-07 06:16:41 +00004456 // We emit all the active diagnostics in this pool or any of its
4457 // parents. In general, we'll get one pool for the decl spec
4458 // and a child pool for each declarator; in a decl group like:
4459 // deprecated_typedef foo, *bar, baz();
4460 // only the declarator pops will be passed decls. This is correct;
4461 // we really do need to consider delayed diagnostics from the decl spec
4462 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004463 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004464 do {
John McCall6347b682012-05-07 06:16:58 +00004465 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004466 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4467 // This const_cast is a bit lame. Really, Triggered should be mutable.
4468 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004469 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004470 continue;
4471
John McCallc1465822011-02-14 07:13:47 +00004472 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004473 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00004474 // Don't bother giving deprecation diagnostics if the decl is invalid.
4475 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00004476 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004477 break;
4478
4479 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004480 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004481 break;
John McCall31168b02011-06-15 23:02:42 +00004482
4483 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004484 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004485 break;
John McCall86121512010-01-27 03:50:35 +00004486 }
4487 }
John McCall2ec85372012-05-07 06:16:41 +00004488 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004489}
4490
John McCall6347b682012-05-07 06:16:58 +00004491/// Given a set of delayed diagnostics, re-emit them as if they had
4492/// been delayed in the current context instead of in the given pool.
4493/// Essentially, this just moves them to the current pool.
4494void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4495 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4496 assert(curPool && "re-emitting in undelayed context not supported");
4497 curPool->steal(pool);
4498}
4499
John McCall28a6aea2009-11-04 02:18:39 +00004500static bool isDeclDeprecated(Decl *D) {
4501 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004502 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004503 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004504 // A category implicitly has the availability of the interface.
4505 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4506 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004507 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4508 return false;
4509}
4510
Eli Friedman971bfa12012-08-08 21:52:41 +00004511static void
4512DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
4513 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004514 const ObjCInterfaceDecl *UnknownObjCClass,
4515 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedman971bfa12012-08-08 21:52:41 +00004516 DeclarationName Name = D->getDeclName();
4517 if (!Message.empty()) {
4518 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
4519 S.Diag(D->getLocation(),
4520 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4521 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004522 if (ObjCPropery)
4523 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4524 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00004525 } else if (!UnknownObjCClass) {
4526 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
4527 S.Diag(D->getLocation(),
4528 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4529 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004530 if (ObjCPropery)
4531 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4532 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00004533 } else {
4534 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
4535 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4536 }
4537}
4538
John McCallb45a1e72010-08-26 02:13:20 +00004539void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00004540 Decl *Ctx) {
4541 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00004542 return;
4543
John McCall86121512010-01-27 03:50:35 +00004544 DD.Triggered = true;
Eli Friedman971bfa12012-08-08 21:52:41 +00004545 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
4546 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004547 DD.getUnknownObjCClass(),
4548 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004549}
4550
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004551void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004552 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004553 const ObjCInterfaceDecl *UnknownObjCClass,
4554 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004555 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004556 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004557 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4558 UnknownObjCClass,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004559 ObjCProperty,
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004560 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004561 return;
4562 }
4563
4564 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00004565 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00004566 return;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004567 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004568}