blob: 687f8a014e46e52725cd15087e584864e5084071 [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.
2280static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002281 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002282 uint32_t WGSize[3];
2283 for (unsigned i = 0; i < 3; ++i)
2284 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002285 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002286
2287 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2288 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2289 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2290 if (!(A->getXDim() == WGSize[0] &&
2291 A->getYDim() == WGSize[1] &&
2292 A->getZDim() == WGSize[2])) {
2293 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2294 Attr.getName();
2295 }
2296 }
2297
2298 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2299 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2300 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2301 if (!(A->getXDim() == WGSize[0] &&
2302 A->getYDim() == WGSize[1] &&
2303 A->getZDim() == WGSize[2])) {
2304 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2305 Attr.getName();
2306 }
2307 }
2308
2309 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2310 D->addAttr(::new (S.Context)
2311 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002312 WGSize[0], WGSize[1], WGSize[2],
2313 Attr.getAttributeSpellingListIndex()));
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002314 else
2315 D->addAttr(::new (S.Context)
2316 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002317 WGSize[0], WGSize[1], WGSize[2],
2318 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002319}
2320
Joey Goulyaba589c2013-03-08 09:42:32 +00002321static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2322 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2323
Aaron Ballman00e99962013-08-31 01:11:41 +00002324 if (!Attr.hasParsedType()) {
2325 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2326 << Attr.getName() << 1;
2327 return;
2328 }
2329
Richard Smithb87c4652013-10-31 21:23:20 +00002330 TypeSourceInfo *ParmTSI = 0;
2331 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2332 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002333
2334 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2335 (ParmType->isBooleanType() ||
2336 !ParmType->isIntegralType(S.getASTContext()))) {
2337 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2338 << ParmType;
2339 return;
2340 }
2341
2342 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2343 D->hasAttr<VecTypeHintAttr>()) {
2344 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
Richard Smithb87c4652013-10-31 21:23:20 +00002345 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002346 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2347 return;
2348 }
2349 }
2350
2351 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Richard Smithb87c4652013-10-31 21:23:20 +00002352 ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002353}
2354
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002355SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002356 StringRef Name,
2357 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002358 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2359 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002360 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002361 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2362 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002363 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002364 }
Michael Han99315932013-01-24 16:46:58 +00002365 return ::new (Context) SectionAttr(Range, Context, Name,
2366 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002367}
2368
Chandler Carruthedc2c642011-07-02 00:01:44 +00002369static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002370 // Make sure that there is a string literal as the sections's single
2371 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002372 StringRef Str;
2373 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002374 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002375 return;
Mike Stump11289f42009-09-09 15:08:12 +00002376
Chris Lattner30ba6742009-08-10 19:03:04 +00002377 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002378 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002379 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002380 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002381 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002382 return;
2383 }
Mike Stump11289f42009-09-09 15:08:12 +00002384
Chris Lattner20aee9b2010-01-12 20:58:53 +00002385 // This attribute cannot be applied to local variables.
2386 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002387 S.Diag(LiteralLoc, diag::err_attribute_section_local_variable);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002388 return;
2389 }
Michael Han99315932013-01-24 16:46:58 +00002390
2391 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002392 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002393 if (NewAttr)
2394 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002395}
2396
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002397
Chandler Carruthedc2c642011-07-02 00:01:44 +00002398static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002399 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002400 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002401 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002402 } else {
Michael Han99315932013-01-24 16:46:58 +00002403 D->addAttr(::new (S.Context)
2404 NoThrowAttr(Attr.getRange(), S.Context,
2405 Attr.getAttributeSpellingListIndex()));
Douglas Gregor88336832011-06-15 05:45:11 +00002406 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002407}
2408
Chandler Carruthedc2c642011-07-02 00:01:44 +00002409static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002410 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002411 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002412 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002413 } else {
Michael Han99315932013-01-24 16:46:58 +00002414 D->addAttr(::new (S.Context)
2415 ConstAttr(Attr.getRange(), S.Context,
2416 Attr.getAttributeSpellingListIndex() ));
Douglas Gregor88336832011-06-15 05:45:11 +00002417 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002418}
2419
Chandler Carruthedc2c642011-07-02 00:01:44 +00002420static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002421 VarDecl *VD = cast<VarDecl>(D);
2422 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002423 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002424 return;
2425 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002426
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002427 Expr *E = Attr.getArgAsExpr(0);
2428 SourceLocation Loc = E->getExprLoc();
2429 FunctionDecl *FD = 0;
2430 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002431
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002432 // gcc only allows for simple identifiers. Since we support more than gcc, we
2433 // will warn the user.
2434 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2435 if (DRE->hasQualifier())
2436 S.Diag(Loc, diag::warn_cleanup_ext);
2437 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2438 NI = DRE->getNameInfo();
2439 if (!FD) {
2440 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2441 << NI.getName();
2442 return;
2443 }
2444 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2445 if (ULE->hasExplicitTemplateArgs())
2446 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002447 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2448 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002449 if (!FD) {
2450 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2451 << NI.getName();
2452 if (ULE->getType() == S.Context.OverloadTy)
2453 S.NoteAllOverloadCandidates(ULE);
2454 return;
2455 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002456 } else {
2457 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002458 return;
2459 }
2460
Anders Carlssond277d792009-01-31 01:16:18 +00002461 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002462 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2463 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002464 return;
2465 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002466
Anders Carlsson723f55d2009-02-07 23:16:50 +00002467 // We're currently more strict than GCC about what function types we accept.
2468 // If this ever proves to be a problem it should be easy to fix.
2469 QualType Ty = S.Context.getPointerType(VD->getType());
2470 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002471 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2472 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002473 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2474 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002475 return;
2476 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002477
Michael Han99315932013-01-24 16:46:58 +00002478 D->addAttr(::new (S.Context)
2479 CleanupAttr(Attr.getRange(), S.Context, FD,
2480 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002481}
2482
Mike Stumpd3bb5572009-07-24 19:02:52 +00002483/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002484/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002485static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002486 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002487 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002488 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002489 return;
2490 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002491
Aaron Ballman00e99962013-08-31 01:11:41 +00002492 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002493 uint64_t ArgIdx;
2494 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2495 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002496 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002497
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002498 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002499 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002500
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002501 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2502 if (not_nsstring_type &&
2503 !isCFStringType(Ty, S.Context) &&
2504 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002505 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002506 // FIXME: Should highlight the actual expression that has the wrong type.
2507 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002508 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002509 << IdxExpr->getSourceRange();
2510 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002511 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002512 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002513 if (!isNSStringType(Ty, S.Context) &&
2514 !isCFStringType(Ty, S.Context) &&
2515 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002516 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002517 // FIXME: Should highlight the actual expression that has the wrong type.
2518 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002519 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002520 << IdxExpr->getSourceRange();
2521 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002522 }
2523
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002524 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2525 // because that has corrected for the implicit this parameter, and is zero-
2526 // based. The attribute expects what the user wrote explicitly.
2527 llvm::APSInt Val;
2528 IdxExpr->EvaluateAsInt(Val, S.Context);
2529
Michael Han99315932013-01-24 16:46:58 +00002530 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002531 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002532 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002533}
2534
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002535enum FormatAttrKind {
2536 CFStringFormat,
2537 NSStringFormat,
2538 StrftimeFormat,
2539 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002540 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002541 InvalidFormat
2542};
2543
2544/// getFormatAttrKind - Map from format attribute names to supported format
2545/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002546static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002547 return llvm::StringSwitch<FormatAttrKind>(Format)
2548 // Check for formats that get handled specially.
2549 .Case("NSString", NSStringFormat)
2550 .Case("CFString", CFStringFormat)
2551 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002552
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002553 // Otherwise, check for supported formats.
2554 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2555 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2556 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002557
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002558 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2559 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002560}
2561
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002562/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002563/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002564static void handleInitPriorityAttr(Sema &S, Decl *D,
2565 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002566 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002567 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2568 return;
2569 }
2570
Aaron Ballman4a611152013-11-27 16:34:09 +00002571 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002572 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2573 Attr.setInvalid();
2574 return;
2575 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002576 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002577 if (S.Context.getAsArrayType(T))
2578 T = S.Context.getBaseElementType(T);
2579 if (!T->getAs<RecordType>()) {
2580 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2581 Attr.setInvalid();
2582 return;
2583 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002584
2585 Expr *E = Attr.getArgAsExpr(0);
2586 uint32_t prioritynum;
2587 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002588 Attr.setInvalid();
2589 return;
2590 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002591
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002592 if (prioritynum < 101 || prioritynum > 65535) {
2593 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002594 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002595 Attr.setInvalid();
2596 return;
2597 }
Michael Han99315932013-01-24 16:46:58 +00002598 D->addAttr(::new (S.Context)
2599 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2600 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002601}
2602
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002603FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2604 IdentifierInfo *Format, int FormatIdx,
2605 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002606 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002607 // Check whether we already have an equivalent format attribute.
2608 for (specific_attr_iterator<FormatAttr>
2609 i = D->specific_attr_begin<FormatAttr>(),
2610 e = D->specific_attr_end<FormatAttr>();
2611 i != e ; ++i) {
2612 FormatAttr *f = *i;
2613 if (f->getType() == Format &&
2614 f->getFormatIdx() == FormatIdx &&
2615 f->getFirstArg() == FirstArg) {
2616 // If we don't have a valid location for this attribute, adopt the
2617 // location.
2618 if (f->getLocation().isInvalid())
2619 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002620 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002621 }
2622 }
2623
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002624 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2625 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002626}
2627
Mike Stumpd3bb5572009-07-24 19:02:52 +00002628/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002629/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002630static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002631 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002632 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002633 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002634 return;
2635 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002636
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002637 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002638 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002639 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002640 return;
2641 }
2642
Chandler Carruth743682b2010-11-16 08:35:43 +00002643 // In C++ the implicit 'this' function parameter also counts, and they are
2644 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002645 bool HasImplicitThisParam = isInstanceMethod(D);
2646 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002647
Aaron Ballman00e99962013-08-31 01:11:41 +00002648 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2649 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002650
2651 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002652 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002653 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002654 // If we've modified the string name, we need a new identifier for it.
2655 II = &S.Context.Idents.get(Format);
2656 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002657
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002658 // Check for supported formats.
2659 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002660
2661 if (Kind == IgnoredFormat)
2662 return;
2663
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002664 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002665 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman00e99962013-08-31 01:11:41 +00002666 << "format" << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002667 return;
2668 }
2669
2670 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002671 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002672 uint32_t Idx;
2673 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002674 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002675
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002676 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002677 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002678 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002679 return;
2680 }
2681
2682 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002683 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002684
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002685 if (HasImplicitThisParam) {
2686 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002687 S.Diag(Attr.getLoc(),
2688 diag::err_format_attribute_implicit_this_format_string)
2689 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002690 return;
2691 }
2692 ArgIdx--;
2693 }
Mike Stump11289f42009-09-09 15:08:12 +00002694
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002695 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002696 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002697
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002698 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002699 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002700 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2701 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002702 return;
2703 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002704 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002705 // FIXME: do we need to check if the type is NSString*? What are the
2706 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002707 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002708 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002709 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2710 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002711 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002712 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002713 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002714 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002715 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002716 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2717 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002718 return;
2719 }
2720
2721 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002722 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002723 uint32_t FirstArg;
2724 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002725 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002726
2727 // check if the function is variadic if the 3rd argument non-zero
2728 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002729 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002730 ++NumArgs; // +1 for ...
2731 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002732 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002733 return;
2734 }
2735 }
2736
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002737 // strftime requires FirstArg to be 0 because it doesn't read from any
2738 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002739 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002740 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002741 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2742 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002743 return;
2744 }
2745 // if 0 it disables parameter checking (to use with e.g. va_list)
2746 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002747 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002748 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002749 return;
2750 }
2751
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002752 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002753 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002754 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002755 if (NewAttr)
2756 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002757}
2758
Chandler Carruthedc2c642011-07-02 00:01:44 +00002759static void handleTransparentUnionAttr(Sema &S, Decl *D,
2760 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002761 // Try to find the underlying union declaration.
2762 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002763 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002764 if (TD && TD->getUnderlyingType()->isUnionType())
2765 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2766 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002767 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002768
2769 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002770 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002771 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002772 return;
2773 }
2774
John McCallf937c022011-10-07 06:10:15 +00002775 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002776 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002777 diag::warn_transparent_union_attribute_not_definition);
2778 return;
2779 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002780
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002781 RecordDecl::field_iterator Field = RD->field_begin(),
2782 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002783 if (Field == FieldEnd) {
2784 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2785 return;
2786 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002787
David Blaikie40ed2972012-06-06 20:45:41 +00002788 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002789 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002790 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002791 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002792 diag::warn_transparent_union_attribute_floating)
2793 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002794 return;
2795 }
2796
2797 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2798 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2799 for (; Field != FieldEnd; ++Field) {
2800 QualType FieldType = Field->getType();
2801 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2802 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2803 // Warn if we drop the attribute.
2804 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002805 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002806 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002807 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002808 diag::warn_transparent_union_attribute_field_size_align)
2809 << isSize << Field->getDeclName() << FieldBits;
2810 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002811 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002812 diag::note_transparent_union_first_field_size_align)
2813 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002814 return;
2815 }
2816 }
2817
Michael Han99315932013-01-24 16:46:58 +00002818 RD->addAttr(::new (S.Context)
2819 TransparentUnionAttr(Attr.getRange(), S.Context,
2820 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002821}
2822
Chandler Carruthedc2c642011-07-02 00:01:44 +00002823static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002824 // Make sure that there is a string literal as the annotation's single
2825 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002826 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002827 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002828 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002829
2830 // Don't duplicate annotations that are already set.
2831 for (specific_attr_iterator<AnnotateAttr>
2832 i = D->specific_attr_begin<AnnotateAttr>(),
2833 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002834 if ((*i)->getAnnotation() == Str)
2835 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002836 }
Michael Han99315932013-01-24 16:46:58 +00002837
2838 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002839 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002840 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002841}
2842
Chandler Carruthedc2c642011-07-02 00:01:44 +00002843static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002844 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002845 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002846 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2847 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002848 return;
2849 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002850
Richard Smith848e1f12013-02-01 08:12:08 +00002851 if (Attr.getNumArgs() == 0) {
2852 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2853 true, 0, Attr.getAttributeSpellingListIndex()));
2854 return;
2855 }
2856
Aaron Ballman00e99962013-08-31 01:11:41 +00002857 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002858 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2859 S.Diag(Attr.getEllipsisLoc(),
2860 diag::err_pack_expansion_without_parameter_packs);
2861 return;
2862 }
2863
2864 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2865 return;
2866
2867 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2868 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002869}
2870
2871void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002872 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002873 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2874 SourceLocation AttrLoc = AttrRange.getBegin();
2875
Richard Smith1dba27c2013-01-29 09:02:09 +00002876 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002877 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002878 // C++11 [dcl.align]p1:
2879 // An alignment-specifier may be applied to a variable or to a class
2880 // data member, but it shall not be applied to a bit-field, a function
2881 // parameter, the formal parameter of a catch clause, or a variable
2882 // declared with the register storage class specifier. An
2883 // alignment-specifier may also be applied to the declaration of a class
2884 // or enumeration type.
2885 // C11 6.7.5/2:
2886 // An alignment attribute shall not be specified in a declaration of
2887 // a typedef, or a bit-field, or a function, or a parameter, or an
2888 // object declared with the register storage-class specifier.
2889 int DiagKind = -1;
2890 if (isa<ParmVarDecl>(D)) {
2891 DiagKind = 0;
2892 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2893 if (VD->getStorageClass() == SC_Register)
2894 DiagKind = 1;
2895 if (VD->isExceptionVariable())
2896 DiagKind = 2;
2897 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2898 if (FD->isBitField())
2899 DiagKind = 3;
2900 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00002901 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
2902 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00002903 << (TmpAttr.isC11() ? ExpectedVariableOrField
2904 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002905 return;
2906 }
2907 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002908 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smithbc8caaf2013-02-22 04:55:39 +00002909 << TmpAttr.isC11() << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002910 return;
2911 }
2912 }
2913
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002914 if (E->isTypeDependent() || E->isValueDependent()) {
2915 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002916 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2917 AA->setPackExpansion(IsPackExpansion);
2918 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002919 return;
2920 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002921
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002922 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002923 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002924 ExprResult ICE
2925 = VerifyIntegerConstantExpression(E, &Alignment,
2926 diag::err_aligned_attribute_argument_not_int,
2927 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002928 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002929 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002930
2931 // C++11 [dcl.align]p2:
2932 // -- if the constant expression evaluates to zero, the alignment
2933 // specifier shall have no effect
2934 // C11 6.7.5p6:
2935 // An alignment specification of zero has no effect.
2936 if (!(TmpAttr.isAlignas() && !Alignment) &&
2937 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002938 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2939 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002940 return;
2941 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002942
Richard Smith848e1f12013-02-01 08:12:08 +00002943 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00002944 // We've already verified it's a power of 2, now let's make sure it's
2945 // 8192 or less.
2946 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00002947 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00002948 << E->getSourceRange();
2949 return;
2950 }
2951 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002952
Richard Smith44c247f2013-02-22 08:32:16 +00002953 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
2954 ICE.take(), SpellingListIndex);
2955 AA->setPackExpansion(IsPackExpansion);
2956 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002957}
2958
Michael Hanaf02bbe2013-02-01 01:19:17 +00002959void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002960 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002961 // FIXME: Cache the number on the Attr object if non-dependent?
2962 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002963 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2964 SpellingListIndex);
2965 AA->setPackExpansion(IsPackExpansion);
2966 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002967}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002968
Richard Smith848e1f12013-02-01 08:12:08 +00002969void Sema::CheckAlignasUnderalignment(Decl *D) {
2970 assert(D->hasAttrs() && "no attributes on decl");
2971
2972 QualType Ty;
2973 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2974 Ty = VD->getType();
2975 else
2976 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002977 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002978 return;
2979
2980 // C++11 [dcl.align]p5, C11 6.7.5/4:
2981 // The combined effect of all alignment attributes in a declaration shall
2982 // not specify an alignment that is less strict than the alignment that
2983 // would otherwise be required for the entity being declared.
2984 AlignedAttr *AlignasAttr = 0;
2985 unsigned Align = 0;
2986 for (specific_attr_iterator<AlignedAttr>
2987 I = D->specific_attr_begin<AlignedAttr>(),
2988 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
2989 if (I->isAlignmentDependent())
2990 return;
2991 if (I->isAlignas())
2992 AlignasAttr = *I;
2993 Align = std::max(Align, I->getAlignment(Context));
2994 }
2995
2996 if (AlignasAttr && Align) {
2997 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2998 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2999 if (NaturalAlign > RequestedAlign)
3000 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3001 << Ty << (unsigned)NaturalAlign.getQuantity();
3002 }
3003}
3004
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003005/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003006/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003007///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003008/// Despite what would be logical, the mode attribute is a decl attribute, not a
3009/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3010/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003011static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003012 // This attribute isn't documented, but glibc uses it. It changes
3013 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00003014 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00003015 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3016 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003017 return;
3018 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003019
Aaron Ballman00e99962013-08-31 01:11:41 +00003020 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3021 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003022
3023 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003024 if (Str.startswith("__") && Str.endswith("__"))
3025 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003026
3027 unsigned DestWidth = 0;
3028 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003029 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003030 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003031 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003032 switch (Str[0]) {
3033 case 'Q': DestWidth = 8; break;
3034 case 'H': DestWidth = 16; break;
3035 case 'S': DestWidth = 32; break;
3036 case 'D': DestWidth = 64; break;
3037 case 'X': DestWidth = 96; break;
3038 case 'T': DestWidth = 128; break;
3039 }
3040 if (Str[1] == 'F') {
3041 IntegerMode = false;
3042 } else if (Str[1] == 'C') {
3043 IntegerMode = false;
3044 ComplexMode = true;
3045 } else if (Str[1] != 'I') {
3046 DestWidth = 0;
3047 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003048 break;
3049 case 4:
3050 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3051 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003052 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003053 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003054 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003055 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003056 break;
3057 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003058 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003059 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003060 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003061 case 11:
3062 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003063 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003064 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003065 }
3066
3067 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003068 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003069 OldTy = TD->getUnderlyingType();
3070 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3071 OldTy = VD->getType();
3072 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003073 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003074 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003075 return;
3076 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003077
John McCall9dd450b2009-09-21 23:43:11 +00003078 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003079 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3080 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003081 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003082 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3083 } else if (ComplexMode) {
3084 if (!OldTy->isComplexType())
3085 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3086 } else {
3087 if (!OldTy->isFloatingType())
3088 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3089 }
3090
Mike Stump87c57ac2009-05-16 07:39:55 +00003091 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3092 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003093 // FIXME: Make sure floating-point mappings are accurate
3094 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003095 if (!DestWidth) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003096 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003097 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003098 }
3099
3100 QualType NewTy;
3101
3102 if (IntegerMode)
3103 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3104 OldTy->isSignedIntegerType());
3105 else
3106 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3107
3108 if (NewTy.isNull()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003109 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003110 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003111 }
3112
Eli Friedman4735374e2009-03-03 06:41:03 +00003113 if (ComplexMode) {
3114 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003115 }
3116
3117 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003118 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3119 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3120 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003121 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003122
3123 D->addAttr(::new (S.Context)
3124 ModeAttr(Attr.getRange(), S.Context, Name,
3125 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003126}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003127
Chandler Carruthedc2c642011-07-02 00:01:44 +00003128static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003129 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3130 if (!VD->hasGlobalStorage())
3131 S.Diag(Attr.getLoc(),
3132 diag::warn_attribute_requires_functions_or_static_globals)
3133 << Attr.getName();
3134 } else if (!isFunctionOrMethod(D)) {
3135 S.Diag(Attr.getLoc(),
3136 diag::warn_attribute_requires_functions_or_static_globals)
3137 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003138 return;
3139 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003140
Michael Han99315932013-01-24 16:46:58 +00003141 D->addAttr(::new (S.Context)
3142 NoDebugAttr(Attr.getRange(), S.Context,
3143 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003144}
3145
Chandler Carruthedc2c642011-07-02 00:01:44 +00003146static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003147 // check the attribute arguments.
3148 if (Attr.getNumArgs() != 0) {
3149 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3150 << Attr.getName() << 0;
3151 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003152 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003153
3154 D->addAttr(::new (S.Context)
3155 CUDADeviceAttr(Attr.getRange(), S.Context,
3156 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003157}
3158
Chandler Carruthedc2c642011-07-02 00:01:44 +00003159static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003160 FunctionDecl *FD = cast<FunctionDecl>(D);
3161 if (!FD->getResultType()->isVoidType()) {
3162 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
3163 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
3164 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3165 << FD->getType()
3166 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
3167 "void");
3168 } else {
3169 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3170 << FD->getType();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003171 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003172 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003173 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003174
Aaron Ballman3aff6332013-12-02 19:30:36 +00003175 D->addAttr(::new (S.Context)
3176 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00003177 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003178}
3179
Chandler Carruthedc2c642011-07-02 00:01:44 +00003180static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003181 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003182 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003183 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003184 return;
3185 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003186
Michael Han99315932013-01-24 16:46:58 +00003187 D->addAttr(::new (S.Context)
3188 GNUInlineAttr(Attr.getRange(), S.Context,
3189 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003190}
3191
Chandler Carruthedc2c642011-07-02 00:01:44 +00003192static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003193 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003194
Aaron Ballman02df2e02012-12-09 17:45:41 +00003195 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003196 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003197 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3198 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003199 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003200 return;
3201
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003202 if (!isa<ObjCMethodDecl>(D)) {
3203 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3204 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003205 return;
3206 }
3207
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003208 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003209 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003210 D->addAttr(::new (S.Context)
3211 FastCallAttr(Attr.getRange(), S.Context,
3212 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003213 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003214 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003215 D->addAttr(::new (S.Context)
3216 StdCallAttr(Attr.getRange(), S.Context,
3217 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003218 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003219 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003220 D->addAttr(::new (S.Context)
3221 ThisCallAttr(Attr.getRange(), S.Context,
3222 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003223 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003224 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003225 D->addAttr(::new (S.Context)
3226 CDeclAttr(Attr.getRange(), S.Context,
3227 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003228 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003229 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003230 D->addAttr(::new (S.Context)
3231 PascalAttr(Attr.getRange(), S.Context,
3232 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003233 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003234 case AttributeList::AT_MSABI:
3235 D->addAttr(::new (S.Context)
3236 MSABIAttr(Attr.getRange(), S.Context,
3237 Attr.getAttributeSpellingListIndex()));
3238 return;
3239 case AttributeList::AT_SysVABI:
3240 D->addAttr(::new (S.Context)
3241 SysVABIAttr(Attr.getRange(), S.Context,
3242 Attr.getAttributeSpellingListIndex()));
3243 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003244 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003245 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003246 switch (CC) {
3247 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003248 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003249 break;
3250 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003251 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003252 break;
3253 default:
3254 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003255 }
3256
Michael Han99315932013-01-24 16:46:58 +00003257 D->addAttr(::new (S.Context)
3258 PcsAttr(Attr.getRange(), S.Context, PCS,
3259 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003260 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003261 }
Derek Schuffa2020962012-10-16 22:30:41 +00003262 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003263 D->addAttr(::new (S.Context)
3264 PnaclCallAttr(Attr.getRange(), S.Context,
3265 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003266 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003267 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003268 D->addAttr(::new (S.Context)
3269 IntelOclBiccAttr(Attr.getRange(), S.Context,
3270 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003271 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003272
Abramo Bagnara50099372010-04-30 13:10:51 +00003273 default:
3274 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003275 }
3276}
3277
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003278static void handleOpenCLImageAccessAttr(Sema &S, Decl *D,
3279 const AttributeList &Attr) {
3280 uint32_t ArgNum;
3281 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), ArgNum))
Guy Benyeifb36ede2013-03-24 13:58:12 +00003282 return;
Guy Benyeifb36ede2013-03-24 13:58:12 +00003283
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003284 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(Attr.getRange(),
3285 S.Context, ArgNum));
Guy Benyeifb36ede2013-03-24 13:58:12 +00003286}
3287
Aaron Ballman02df2e02012-12-09 17:45:41 +00003288bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3289 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003290 if (attr.isInvalid())
3291 return true;
3292
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003293 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003294 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003295 attr.setInvalid();
3296 return true;
3297 }
3298
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003299 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3300 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003301 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003302 case AttributeList::AT_CDecl: CC = CC_C; break;
3303 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3304 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3305 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3306 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003307 case AttributeList::AT_MSABI:
3308 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3309 CC_X86_64Win64;
3310 break;
3311 case AttributeList::AT_SysVABI:
3312 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3313 CC_C;
3314 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003315 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003316 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003317 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003318 attr.setInvalid();
3319 return true;
3320 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003321 if (StrRef == "aapcs") {
3322 CC = CC_AAPCS;
3323 break;
3324 } else if (StrRef == "aapcs-vfp") {
3325 CC = CC_AAPCS_VFP;
3326 break;
3327 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003328
3329 attr.setInvalid();
3330 Diag(attr.getLoc(), diag::err_invalid_pcs);
3331 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003332 }
Derek Schuffa2020962012-10-16 22:30:41 +00003333 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003334 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003335 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003336 }
3337
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003338 const TargetInfo &TI = Context.getTargetInfo();
3339 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3340 if (A == TargetInfo::CCCR_Warning) {
3341 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003342
3343 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3344 if (FD)
3345 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3346 TargetInfo::CCMT_NonMember;
3347 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003348 }
3349
John McCall3882ace2011-01-05 12:14:39 +00003350 return false;
3351}
3352
Chandler Carruthedc2c642011-07-02 00:01:44 +00003353static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003354 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003355
3356 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003357 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003358 return;
3359
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003360 if (!isa<ObjCMethodDecl>(D)) {
3361 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3362 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003363 return;
3364 }
Eli Friedman7044b762009-03-27 21:06:47 +00003365
Michael Han99315932013-01-24 16:46:58 +00003366 D->addAttr(::new (S.Context)
3367 RegparmAttr(Attr.getRange(), S.Context, numParams,
3368 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00003369}
3370
3371/// Checks a regparm attribute, returning true if it is ill-formed and
3372/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003373bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3374 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003375 return true;
3376
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003377 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003378 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003379 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003380 }
Eli Friedman7044b762009-03-27 21:06:47 +00003381
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003382 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003383 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003384 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003385 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003386 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003387 }
3388
Douglas Gregore8bbc122011-09-02 00:18:52 +00003389 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003390 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003391 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003392 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003393 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003394 }
3395
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003396 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003397 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003398 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003399 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003400 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003401 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003402 }
3403
John McCall3882ace2011-01-05 12:14:39 +00003404 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003405}
3406
Chandler Carruthedc2c642011-07-02 00:01:44 +00003407static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Aaron Ballman3aff6332013-12-02 19:30:36 +00003408 // check the attribute arguments.
3409 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
3410 // FIXME: 0 is not okay.
3411 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
3412 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003413 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003414
3415 if (!isFunctionOrMethod(D)) {
3416 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3417 << Attr.getName() << ExpectedFunctionOrMethod;
3418 return;
3419 }
3420
3421 uint32_t MaxThreads, MinBlocks;
3422 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1) ||
3423 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(1), MinBlocks, 2))
3424 return;
3425
3426 D->addAttr(::new (S.Context)
3427 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3428 MaxThreads, MinBlocks,
3429 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003430}
3431
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003432static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3433 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003434 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003435 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003436 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003437 return;
3438 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003439
3440 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003441 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003442
Aaron Ballman00e99962013-08-31 01:11:41 +00003443 StringRef AttrName = Attr.getName()->getName();
3444 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003445
3446 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3447 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3448 << Attr.getName() << ExpectedFunctionOrMethod;
3449 return;
3450 }
3451
3452 uint64_t ArgumentIdx;
3453 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3454 Attr.getLoc(), 2,
Aaron Ballman00e99962013-08-31 01:11:41 +00003455 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003456 return;
3457
3458 uint64_t TypeTagIdx;
3459 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3460 Attr.getLoc(), 3,
Aaron Ballman00e99962013-08-31 01:11:41 +00003461 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003462 return;
3463
3464 bool IsPointer = (AttrName == "pointer_with_type_tag");
3465 if (IsPointer) {
3466 // Ensure that buffer has a pointer type.
3467 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3468 if (!BufferTy->isPointerType()) {
3469 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003470 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003471 }
3472 }
3473
Michael Han99315932013-01-24 16:46:58 +00003474 D->addAttr(::new (S.Context)
3475 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3476 ArgumentIdx, TypeTagIdx, IsPointer,
3477 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003478}
3479
3480static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3481 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003482 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003483 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003484 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003485 return;
3486 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003487
3488 if (!checkAttributeNumArgs(S, Attr, 1))
3489 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003490
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003491 if (!isa<VarDecl>(D)) {
3492 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3493 << Attr.getName() << ExpectedVariable;
3494 return;
3495 }
3496
Aaron Ballman00e99962013-08-31 01:11:41 +00003497 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003498 TypeSourceInfo *MatchingCTypeLoc = 0;
3499 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3500 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003501
Michael Han99315932013-01-24 16:46:58 +00003502 D->addAttr(::new (S.Context)
3503 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003504 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003505 Attr.getLayoutCompatible(),
3506 Attr.getMustBeNull(),
3507 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003508}
3509
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003510//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003511// Checker-specific attribute handlers.
3512//===----------------------------------------------------------------------===//
3513
John McCalled433932011-01-25 03:31:58 +00003514static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003515 return type->isDependentType() ||
3516 type->isObjCObjectPointerType() ||
3517 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003518}
3519static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003520 return type->isDependentType() ||
3521 type->isPointerType() ||
3522 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003523}
3524
Chandler Carruthedc2c642011-07-02 00:01:44 +00003525static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003526 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003527 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003528
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003529 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003530 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3531 cf = false;
3532 } else {
3533 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3534 cf = true;
3535 }
3536
3537 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003538 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003539 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003540 return;
3541 }
3542
3543 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003544 param->addAttr(::new (S.Context)
3545 CFConsumedAttr(Attr.getRange(), S.Context,
3546 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003547 else
Michael Han99315932013-01-24 16:46:58 +00003548 param->addAttr(::new (S.Context)
3549 NSConsumedAttr(Attr.getRange(), S.Context,
3550 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003551}
3552
Chandler Carruthedc2c642011-07-02 00:01:44 +00003553static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3554 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003555
John McCalled433932011-01-25 03:31:58 +00003556 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003557
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003558 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003559 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003560 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003561 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003562 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003563 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3564 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003565 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003566 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003567 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003568 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003569 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003570 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003571 return;
3572 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003573
John McCalled433932011-01-25 03:31:58 +00003574 bool typeOK;
3575 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003576 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003577 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003578 case AttributeList::AT_NSReturnsAutoreleased:
3579 case AttributeList::AT_NSReturnsRetained:
3580 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003581 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3582 cf = false;
3583 break;
3584
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003585 case AttributeList::AT_CFReturnsRetained:
3586 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003587 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3588 cf = true;
3589 break;
3590 }
3591
3592 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003593 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003594 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003595 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003596 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003597
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003598 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003599 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003600 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003601 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003602 D->addAttr(::new (S.Context)
3603 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3604 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003605 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003606 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003607 D->addAttr(::new (S.Context)
3608 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3609 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003610 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003611 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003612 D->addAttr(::new (S.Context)
3613 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3614 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003615 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003616 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003617 D->addAttr(::new (S.Context)
3618 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3619 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003620 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003621 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003622 D->addAttr(::new (S.Context)
3623 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3624 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003625 return;
3626 };
3627}
3628
John McCallcf166702011-07-22 08:53:00 +00003629static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3630 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003631 const int EP_ObjCMethod = 1;
3632 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003633
John McCallcf166702011-07-22 08:53:00 +00003634 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003635 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003636 if (isa<ObjCMethodDecl>(D))
3637 resultType = cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003638 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003639 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003640
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003641 if (!resultType->isReferenceType() &&
3642 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003643 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003644 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003645 << attr.getName()
3646 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003647 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003648
3649 // Drop the attribute.
3650 return;
3651 }
3652
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003653 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003654 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3655 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003656}
3657
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003658static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3659 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003660 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003661
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003662 DeclContext *DC = method->getDeclContext();
3663 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3664 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3665 << attr.getName() << 0;
3666 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3667 return;
3668 }
3669 if (method->getMethodFamily() == OMF_dealloc) {
3670 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3671 << attr.getName() << 1;
3672 return;
3673 }
3674
Michael Han99315932013-01-24 16:46:58 +00003675 method->addAttr(::new (S.Context)
3676 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3677 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003678}
3679
Aaron Ballmanfb763042013-12-02 18:05:46 +00003680static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3681 const AttributeList &Attr) {
3682 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr,
3683 "cf_unknown_transfer"))
John McCall32f5fe12011-09-30 05:12:12 +00003684 return;
John McCall32f5fe12011-09-30 05:12:12 +00003685
Aaron Ballmanfb763042013-12-02 18:05:46 +00003686 D->addAttr(::new (S.Context)
3687 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3688 Attr.getAttributeSpellingListIndex()));
3689}
3690
3691static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3692 const AttributeList &Attr) {
3693 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr,
3694 "cf_audited_transfer"))
3695 return;
3696
3697 D->addAttr(::new (S.Context)
3698 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3699 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003700}
3701
John McCallf1e8b342011-09-29 07:17:38 +00003702static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
3703 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003704 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallf1e8b342011-09-29 07:17:38 +00003705
3706 // In Objective-C, verify that the type names an Objective-C type.
3707 // We don't want to check this outside of ObjC because people sometimes
3708 // do crazy C declarations of Objective-C types.
Aaron Ballman00e99962013-08-31 01:11:41 +00003709 if (Parm && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00003710 // Check for an existing type with this name.
Aaron Ballman00e99962013-08-31 01:11:41 +00003711 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallf1e8b342011-09-29 07:17:38 +00003712 Sema::LookupOrdinaryName);
3713 if (S.LookupName(R, Sc)) {
3714 NamedDecl *Target = R.getFoundDecl();
3715 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
3716 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
3717 S.Diag(Target->getLocStart(), diag::note_declared_at);
3718 }
3719 }
3720 }
3721
Michael Han99315932013-01-24 16:46:58 +00003722 D->addAttr(::new (S.Context)
Aaron Ballman00e99962013-08-31 01:11:41 +00003723 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han99315932013-01-24 16:46:58 +00003724 Attr.getAttributeSpellingListIndex()));
John McCallf1e8b342011-09-29 07:17:38 +00003725}
3726
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003727static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3728 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003729 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003730
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003731 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003732 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003733 return;
3734 }
3735
3736 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003737 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003738 Attr.getAttributeSpellingListIndex()));
3739}
3740
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003741static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3742 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003743 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003744
3745 if (!Parm) {
3746 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3747 return;
3748 }
3749
3750 D->addAttr(::new (S.Context)
3751 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3752 Attr.getAttributeSpellingListIndex()));
3753}
3754
Chandler Carruthedc2c642011-07-02 00:01:44 +00003755static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3756 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003757 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003758
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003759 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003760 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003761}
3762
Chandler Carruthedc2c642011-07-02 00:01:44 +00003763static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3764 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003765 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003766 QualType type = vd->getType();
3767
3768 if (!type->isDependentType() &&
3769 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003770 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003771 << type;
3772 return;
3773 }
3774
3775 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3776
3777 // If we have no lifetime yet, check the lifetime we're presumably
3778 // going to infer.
3779 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3780 lifetime = type->getObjCARCImplicitLifetime();
3781
3782 switch (lifetime) {
3783 case Qualifiers::OCL_None:
3784 assert(type->isDependentType() &&
3785 "didn't infer lifetime for non-dependent type?");
3786 break;
3787
3788 case Qualifiers::OCL_Weak: // meaningful
3789 case Qualifiers::OCL_Strong: // meaningful
3790 break;
3791
3792 case Qualifiers::OCL_ExplicitNone:
3793 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003794 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003795 << (lifetime == Qualifiers::OCL_Autoreleasing);
3796 break;
3797 }
3798
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003799 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003800 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3801 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003802}
3803
Francois Picheta83957a2010-12-19 06:50:37 +00003804//===----------------------------------------------------------------------===//
3805// Microsoft specific attribute handlers.
3806//===----------------------------------------------------------------------===//
3807
Chandler Carruthedc2c642011-07-02 00:01:44 +00003808static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003809 if (!S.LangOpts.CPlusPlus) {
3810 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3811 << Attr.getName() << AttributeLangSupport::C;
3812 return;
3813 }
3814
Aaron Ballman60e705e2013-11-24 20:58:02 +00003815 if (!isa<CXXRecordDecl>(D)) {
3816 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3817 << Attr.getName() << ExpectedClass;
3818 return;
3819 }
3820
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003821 StringRef StrRef;
3822 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003823 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003824 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003825
David Majnemer89085342013-08-09 08:56:20 +00003826 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3827 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003828 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3829 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003830
Reid Kleckner140c4a72013-05-17 14:04:52 +00003831 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003832 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003833 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003834 return;
3835 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003836
David Majnemer89085342013-08-09 08:56:20 +00003837 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003838 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003839 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003840 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003841 return;
3842 }
David Majnemer89085342013-08-09 08:56:20 +00003843 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003844 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003845 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003846 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003847 }
Francois Picheta83957a2010-12-19 06:50:37 +00003848
David Majnemer89085342013-08-09 08:56:20 +00003849 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3850 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003851}
3852
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003853/// Handles semantic checking for features that are common to all attributes,
3854/// such as checking whether a parameter was properly specified, or the correct
3855/// number of arguments were passed, etc.
3856static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
3857 const AttributeList &Attr) {
3858 // Several attributes carry different semantics than the parsing requires, so
3859 // those are opted out of the common handling.
3860 //
3861 // We also bail on unknown and ignored attributes because those are handled
3862 // as part of the target-specific handling logic.
3863 if (Attr.hasCustomParsing() ||
3864 Attr.getKind() == AttributeList::UnknownAttribute ||
3865 Attr.getKind() == AttributeList::IgnoredAttribute)
3866 return false;
3867
Aaron Ballman3aff6332013-12-02 19:30:36 +00003868 // Check whether the attribute requires specific language extensions to be
3869 // enabled.
3870 if (!Attr.diagnoseLangOpts(S))
3871 return true;
3872
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003873 // If there are no optional arguments, then checking for the argument count
3874 // is trivial.
3875 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
3876 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
3877 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003878
3879 // Check whether the attribute appertains to the given subject.
3880 if (!Attr.diagnoseAppertainsTo(S, D))
3881 return true;
3882
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003883 return false;
3884}
3885
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003886//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003887// Top Level Sema Entry Points
3888//===----------------------------------------------------------------------===//
3889
Richard Smithf8a75c32013-08-29 00:47:48 +00003890/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3891/// the attribute applies to decls. If the attribute is a type attribute, just
3892/// silently ignore it if a GNU attribute.
3893static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3894 const AttributeList &Attr,
3895 bool IncludeCXX11Attributes) {
3896 if (Attr.isInvalid())
3897 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003898
Richard Smithf8a75c32013-08-29 00:47:48 +00003899 // Ignore C++11 attributes on declarator chunks: they appertain to the type
3900 // instead.
3901 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
3902 return;
3903
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003904 if (handleCommonAttributeFeatures(S, scope, D, Attr))
3905 return;
3906
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003907 switch (Attr.getKind()) {
Aaron Ballman9beb5172013-12-02 15:13:14 +00003908 case AttributeList::AT_IBAction:
3909 handleSimpleAttribute<IBActionAttr>(S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003910 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
3911 case AttributeList::AT_IBOutletCollection:
3912 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003913 case AttributeList::AT_AddressSpace:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003914 case AttributeList::AT_ObjCGC:
3915 case AttributeList::AT_VectorSize:
3916 case AttributeList::AT_NeonVectorType:
3917 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballman317a77f2013-05-22 23:25:32 +00003918 case AttributeList::AT_Ptr32:
3919 case AttributeList::AT_Ptr64:
3920 case AttributeList::AT_SPtr:
3921 case AttributeList::AT_UPtr:
Mike Stumpd3bb5572009-07-24 19:02:52 +00003922 // Ignore these, these are type attributes, handled by
3923 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003924 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003925 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
3926 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
3927 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
3928 case AttributeList::AT_AlwaysInline:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003929 handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003930 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003931 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00003932 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003933 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
3934 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
3935 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00003936 handleDependencyAttr(S, scope, D, Attr);
3937 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003938 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003939 case AttributeList::AT_CUDAConstant:
3940 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003941 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003942 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman3a8e2d92013-11-27 18:53:58 +00003943 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003944 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00003945 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00003946 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003947 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
3948 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003949 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003950 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00003951 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003952 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00003953 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003954 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
3955 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
3956 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman9a226212013-08-28 23:13:26 +00003957 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003958 case AttributeList::AT_CUDAHost:
3959 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003960 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
3961 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003962 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00003963 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003964 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003965 case AttributeList::AT_MayAlias:
3966 handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00003967 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003968 case AttributeList::AT_NoCommon:
3969 handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003970 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003971 case AttributeList::AT_Overloadable:
3972 handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break;
Richard Smith852e9ce2013-11-27 01:46:48 +00003973 case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003974 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
3975 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003976 case AttributeList::AT_Naked:
3977 handleSimpleAttribute<NakedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003978 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
3979 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003980 case AttributeList::AT_CUDAShared:
3981 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003982 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003983
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003984 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003985 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003986 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003987 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003988
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003989 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00003990 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3991
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003992 case AttributeList::AT_ObjCRequiresSuper:
3993 handleObjCRequiresSuperAttr(S, D, Attr); break;
3994
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003995 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00003996 handleNSBridgedAttr(S, scope, D, Attr); break;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003997
3998 case AttributeList::AT_ObjCBridge:
3999 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004000
4001 case AttributeList::AT_ObjCBridgeMutable:
4002 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00004003
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004004 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00004005 handleCFAuditedTransferAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004006 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00004007 handleCFUnknownTransferAttr(S, D, Attr); break;
John McCall32f5fe12011-09-30 05:12:12 +00004008
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004009 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004010 case AttributeList::AT_CFConsumed:
4011 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4012 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004013 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004014
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004015 case AttributeList::AT_NSReturnsAutoreleased:
4016 case AttributeList::AT_NSReturnsNotRetained:
4017 case AttributeList::AT_CFReturnsNotRetained:
4018 case AttributeList::AT_NSReturnsRetained:
4019 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004020 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004021
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004022 case AttributeList::AT_WorkGroupSizeHint:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004023 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004024 handleWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00004025
Joey Goulyaba589c2013-03-08 09:42:32 +00004026 case AttributeList::AT_VecTypeHint:
4027 handleVecTypeHint(S, D, Attr); break;
4028
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004029 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004030 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004031
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004032 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4033 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4034 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004035 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004036 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004037 case AttributeList::AT_ArcWeakrefUnavailable:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004038 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004039 case AttributeList::AT_ObjCRootClass:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004040 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break;
Ted Kremenek28eace62013-11-23 01:01:34 +00004041 case AttributeList::AT_ObjCSuppressProtocol:
4042 handleObjCSuppresProtocolAttr(S, D, Attr);
4043 break;
4044 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004045 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004046 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4047 case AttributeList::AT_ReturnsTwice:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004048 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004049 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004050 case AttributeList::AT_Visibility:
4051 handleVisibilityAttr(S, D, Attr, false);
4052 break;
4053 case AttributeList::AT_TypeVisibility:
4054 handleVisibilityAttr(S, D, Attr, true);
4055 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004056 case AttributeList::AT_WarnUnused:
Aaron Ballman6a42b5a2013-11-27 16:59:17 +00004057 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004058 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004059 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004060 case AttributeList::AT_Weak:
4061 handleSimpleAttribute<WeakAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004062 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4063 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4064 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004065 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004066 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004067 case AttributeList::AT_ObjCException:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004068 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004069 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004070 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004071 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004072 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4073 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4074 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4075 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004076 case AttributeList::AT_Pure:
4077 handleSimpleAttribute<PureAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004078 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4079 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004080 case AttributeList::AT_NoInline:
4081 handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004082 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004083 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004084 // Just ignore
4085 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004086 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004087 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004088 case AttributeList::AT_StdCall:
4089 case AttributeList::AT_CDecl:
4090 case AttributeList::AT_FastCall:
4091 case AttributeList::AT_ThisCall:
4092 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004093 case AttributeList::AT_MSABI:
4094 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004095 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004096 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004097 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004098 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004099 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004100 case AttributeList::AT_OpenCLKernel:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004101 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004102 case AttributeList::AT_OpenCLImageAccess:
4103 handleOpenCLImageAccessAttr(S, D, Attr);
4104 break;
John McCall8d32c052012-05-22 21:28:12 +00004105
4106 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004107 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004108 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004109 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004110 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004111 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004112 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004113 case AttributeList::AT_SingleInheritance:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004114 handleSimpleAttribute<SingleInheritanceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004115 case AttributeList::AT_MultipleInheritance:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004116 handleSimpleAttribute<MultipleInheritanceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004117 case AttributeList::AT_VirtualInheritance:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004118 handleSimpleAttribute<VirtualInheritanceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004119 case AttributeList::AT_Win64:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004120 handleSimpleAttribute<Win64Attr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004121 case AttributeList::AT_ForceInline:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004122 handleSimpleAttribute<ForceInlineAttr>(S, D, Attr); break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004123 case AttributeList::AT_SelectAny:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004124 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004125
4126 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004127 case AttributeList::AT_AssertExclusiveLock:
4128 handleAssertExclusiveLockAttr(S, D, Attr);
4129 break;
4130 case AttributeList::AT_AssertSharedLock:
4131 handleAssertSharedLockAttr(S, D, Attr);
4132 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004133 case AttributeList::AT_GuardedVar:
Aaron Ballmane61b8b82013-12-02 15:02:49 +00004134 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004135 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004136 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004137 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004138 case AttributeList::AT_ScopedLockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004139 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004140 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004141 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004142 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004143 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004144 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004145 break;
4146 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004147 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004148 break;
4149 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004150 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004151 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004152 case AttributeList::AT_Lockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004153 handleSimpleAttribute<LockableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004154 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004155 handleGuardedByAttr(S, D, Attr);
4156 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004157 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004158 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004159 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004160 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004161 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004162 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004163 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004164 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004165 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004166 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004167 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004168 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004169 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004170 handleLockReturnedAttr(S, D, Attr);
4171 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004172 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004173 handleLocksExcludedAttr(S, D, Attr);
4174 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004175 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004176 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004177 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004178 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004179 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004180 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004181 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004182 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004183 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004184 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004185 handleUnlockFunAttr(S, D, Attr);
4186 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004187 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004188 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004189 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004190 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004191 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004192 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004193
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004194 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004195 case AttributeList::AT_Consumable:
4196 handleConsumableAttr(S, D, Attr);
4197 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004198 case AttributeList::AT_CallableWhen:
4199 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004200 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004201 case AttributeList::AT_ParamTypestate:
4202 handleParamTypestateAttr(S, D, Attr);
4203 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004204 case AttributeList::AT_ReturnTypestate:
4205 handleReturnTypestateAttr(S, D, Attr);
4206 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004207 case AttributeList::AT_SetTypestate:
4208 handleSetTypestateAttr(S, D, Attr);
4209 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004210 case AttributeList::AT_TestTypestate:
4211 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004212 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004213
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004214 // Type safety attributes.
4215 case AttributeList::AT_ArgumentWithTypeTag:
4216 handleArgumentWithTypeTagAttr(S, D, Attr);
4217 break;
4218 case AttributeList::AT_TypeTagForDatatype:
4219 handleTypeTagForDatatypeAttr(S, D, Attr);
4220 break;
4221
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004222 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004223 // Ask target about the attribute.
4224 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4225 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004226 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4227 diag::warn_unhandled_ms_attribute_ignored :
4228 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004229 break;
4230 }
4231}
4232
4233/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4234/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004235void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004236 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004237 bool IncludeCXX11Attributes) {
4238 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004239 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004240
4241 // GCC accepts
4242 // static int a9 __attribute__((weakref));
4243 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004244 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004245 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00004246 cast<NamedDecl>(D)->getNameAsString();
4247 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004248 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004249 }
4250}
4251
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004252// Annotation attributes are the only attributes allowed after an access
4253// specifier.
4254bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4255 const AttributeList *AttrList) {
4256 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004257 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004258 handleAnnotateAttr(*this, ASDecl, *l);
4259 } else {
4260 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4261 return true;
4262 }
4263 }
4264
4265 return false;
4266}
4267
John McCall42856de2011-10-01 05:17:03 +00004268/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4269/// contains any decl attributes that we should warn about.
4270static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4271 for ( ; A; A = A->getNext()) {
4272 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004273 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004274 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4275
4276 if (A->getKind() == AttributeList::UnknownAttribute) {
4277 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4278 << A->getName() << A->getRange();
4279 } else {
4280 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4281 << A->getName() << A->getRange();
4282 }
4283 }
4284}
4285
4286/// checkUnusedDeclAttributes - Given a declarator which is not being
4287/// used to build a declaration, complain about any decl attributes
4288/// which might be lying around on it.
4289void Sema::checkUnusedDeclAttributes(Declarator &D) {
4290 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4291 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4292 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4293 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4294}
4295
Ryan Flynn7d470f32009-07-30 03:15:39 +00004296/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004297/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004298NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4299 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004300 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004301 NamedDecl *NewD = 0;
4302 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004303 FunctionDecl *NewFD;
4304 // FIXME: Missing call to CheckFunctionDeclaration().
4305 // FIXME: Mangling?
4306 // FIXME: Is the qualifier info correct?
4307 // FIXME: Is the DeclContext correct?
4308 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4309 Loc, Loc, DeclarationName(II),
4310 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004311 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004312 FD->hasPrototype(),
4313 false/*isConstexprSpecified*/);
4314 NewD = NewFD;
4315
4316 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004317 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004318
4319 // Fake up parameter variables; they are declared as if this were
4320 // a typedef.
4321 QualType FDTy = FD->getType();
4322 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4323 SmallVector<ParmVarDecl*, 16> Params;
4324 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4325 AE = FT->arg_type_end(); AI != AE; ++AI) {
4326 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4327 Param->setScopeInfo(0, Params.size());
4328 Params.push_back(Param);
4329 }
David Blaikie9c70e042011-09-21 18:16:56 +00004330 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004331 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004332 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4333 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004334 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004335 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004336 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004337 if (VD->getQualifier()) {
4338 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004339 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004340 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004341 }
4342 return NewD;
4343}
4344
James Dennett634962f2012-06-14 21:40:34 +00004345/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004346/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004347void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004348 if (W.getUsed()) return; // only do this once
4349 W.setUsed(true);
4350 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4351 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004352 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004353 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4354 NDId->getName()));
4355 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00004356 WeakTopLevelDecl.push_back(NewD);
4357 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4358 // to insert Decl at TU scope, sorry.
4359 DeclContext *SavedContext = CurContext;
4360 CurContext = Context.getTranslationUnitDecl();
4361 PushOnScopeChains(NewD, S);
4362 CurContext = SavedContext;
4363 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004364 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004365 }
4366}
4367
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004368void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4369 // It's valid to "forward-declare" #pragma weak, in which case we
4370 // have to do this.
4371 LoadExternalWeakUndeclaredIdentifiers();
4372 if (!WeakUndeclaredIdentifiers.empty()) {
4373 NamedDecl *ND = NULL;
4374 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4375 if (VD->isExternC())
4376 ND = VD;
4377 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4378 if (FD->isExternC())
4379 ND = FD;
4380 if (ND) {
4381 if (IdentifierInfo *Id = ND->getIdentifier()) {
4382 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4383 = WeakUndeclaredIdentifiers.find(Id);
4384 if (I != WeakUndeclaredIdentifiers.end()) {
4385 WeakInfo W = I->second;
4386 DeclApplyPragmaWeak(S, ND, W);
4387 WeakUndeclaredIdentifiers[Id] = W;
4388 }
4389 }
4390 }
4391 }
4392}
4393
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004394/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4395/// it, apply them to D. This is a bit tricky because PD can have attributes
4396/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004397void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004398 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004399 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004400 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004401
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004402 // Walk the declarator structure, applying decl attributes that were in a type
4403 // position to the decl itself. This handles cases like:
4404 // int *__attr__(x)** D;
4405 // when X is a decl attribute.
4406 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4407 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004408 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004409
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004410 // Finally, apply any attributes on the decl itself.
4411 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004412 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004413}
John McCall28a6aea2009-11-04 02:18:39 +00004414
John McCall31168b02011-06-15 23:02:42 +00004415/// Is the given declaration allowed to use a forbidden type?
4416static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4417 // Private ivars are always okay. Unfortunately, people don't
4418 // always properly make their ivars private, even in system headers.
4419 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004420 // Function declarations in sys headers will be marked unavailable.
4421 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4422 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004423 return false;
4424
4425 // Require it to be declared in a system header.
4426 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4427}
4428
4429/// Handle a delayed forbidden-type diagnostic.
4430static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4431 Decl *decl) {
4432 if (decl && isForbiddenTypeAllowed(S, decl)) {
4433 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4434 "this system declaration uses an unsupported type"));
4435 return;
4436 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004437 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004438 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004439 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004440 // kind of forbidden type messages on unavailable functions.
4441 if (FD->hasAttr<UnavailableAttr>() &&
4442 diag.getForbiddenTypeDiagnostic() ==
4443 diag::err_arc_array_param_no_ownership) {
4444 diag.Triggered = true;
4445 return;
4446 }
4447 }
John McCall31168b02011-06-15 23:02:42 +00004448
4449 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4450 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4451 diag.Triggered = true;
4452}
4453
John McCall2ec85372012-05-07 06:16:41 +00004454void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4455 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004456 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004457 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004458
John McCall2ec85372012-05-07 06:16:41 +00004459 // When delaying diagnostics to run in the context of a parsed
4460 // declaration, we only want to actually emit anything if parsing
4461 // succeeds.
4462 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004463
John McCall2ec85372012-05-07 06:16:41 +00004464 // We emit all the active diagnostics in this pool or any of its
4465 // parents. In general, we'll get one pool for the decl spec
4466 // and a child pool for each declarator; in a decl group like:
4467 // deprecated_typedef foo, *bar, baz();
4468 // only the declarator pops will be passed decls. This is correct;
4469 // we really do need to consider delayed diagnostics from the decl spec
4470 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004471 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004472 do {
John McCall6347b682012-05-07 06:16:58 +00004473 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004474 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4475 // This const_cast is a bit lame. Really, Triggered should be mutable.
4476 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004477 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004478 continue;
4479
John McCallc1465822011-02-14 07:13:47 +00004480 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004481 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00004482 // Don't bother giving deprecation diagnostics if the decl is invalid.
4483 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00004484 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004485 break;
4486
4487 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004488 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004489 break;
John McCall31168b02011-06-15 23:02:42 +00004490
4491 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004492 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004493 break;
John McCall86121512010-01-27 03:50:35 +00004494 }
4495 }
John McCall2ec85372012-05-07 06:16:41 +00004496 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004497}
4498
John McCall6347b682012-05-07 06:16:58 +00004499/// Given a set of delayed diagnostics, re-emit them as if they had
4500/// been delayed in the current context instead of in the given pool.
4501/// Essentially, this just moves them to the current pool.
4502void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4503 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4504 assert(curPool && "re-emitting in undelayed context not supported");
4505 curPool->steal(pool);
4506}
4507
John McCall28a6aea2009-11-04 02:18:39 +00004508static bool isDeclDeprecated(Decl *D) {
4509 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004510 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004511 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004512 // A category implicitly has the availability of the interface.
4513 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4514 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004515 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4516 return false;
4517}
4518
Eli Friedman971bfa12012-08-08 21:52:41 +00004519static void
4520DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
4521 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004522 const ObjCInterfaceDecl *UnknownObjCClass,
4523 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedman971bfa12012-08-08 21:52:41 +00004524 DeclarationName Name = D->getDeclName();
4525 if (!Message.empty()) {
4526 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
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 if (!UnknownObjCClass) {
4534 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
4535 S.Diag(D->getLocation(),
4536 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4537 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004538 if (ObjCPropery)
4539 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4540 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00004541 } else {
4542 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
4543 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4544 }
4545}
4546
John McCallb45a1e72010-08-26 02:13:20 +00004547void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00004548 Decl *Ctx) {
4549 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00004550 return;
4551
John McCall86121512010-01-27 03:50:35 +00004552 DD.Triggered = true;
Eli Friedman971bfa12012-08-08 21:52:41 +00004553 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
4554 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004555 DD.getUnknownObjCClass(),
4556 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004557}
4558
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004559void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004560 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004561 const ObjCInterfaceDecl *UnknownObjCClass,
4562 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004563 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004564 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004565 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4566 UnknownObjCClass,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004567 ObjCProperty,
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004568 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004569 return;
4570 }
4571
4572 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00004573 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00004574 return;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004575 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004576}