blob: 0edef9e52a46e251ea0db735634a041447abc29a [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 Kremenek28eace62013-11-23 01:01:34 +00001743 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001744 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1745 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001746}
1747
Jordy Rose740b0c22012-05-08 03:27:22 +00001748static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1749 IdentifierInfo *Platform,
1750 VersionTuple Introduced,
1751 VersionTuple Deprecated,
1752 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001753 StringRef PlatformName
1754 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1755 if (PlatformName.empty())
1756 PlatformName = Platform->getName();
1757
1758 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1759 // of these steps are needed).
1760 if (!Introduced.empty() && !Deprecated.empty() &&
1761 !(Introduced <= Deprecated)) {
1762 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1763 << 1 << PlatformName << Deprecated.getAsString()
1764 << 0 << Introduced.getAsString();
1765 return true;
1766 }
1767
1768 if (!Introduced.empty() && !Obsoleted.empty() &&
1769 !(Introduced <= Obsoleted)) {
1770 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1771 << 2 << PlatformName << Obsoleted.getAsString()
1772 << 0 << Introduced.getAsString();
1773 return true;
1774 }
1775
1776 if (!Deprecated.empty() && !Obsoleted.empty() &&
1777 !(Deprecated <= Obsoleted)) {
1778 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1779 << 2 << PlatformName << Obsoleted.getAsString()
1780 << 1 << Deprecated.getAsString();
1781 return true;
1782 }
1783
1784 return false;
1785}
1786
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001787/// \brief Check whether the two versions match.
1788///
1789/// If either version tuple is empty, then they are assumed to match. If
1790/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1791static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1792 bool BeforeIsOkay) {
1793 if (X.empty() || Y.empty())
1794 return true;
1795
1796 if (X == Y)
1797 return true;
1798
1799 if (BeforeIsOkay && X < Y)
1800 return true;
1801
1802 return false;
1803}
1804
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001805AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001806 IdentifierInfo *Platform,
1807 VersionTuple Introduced,
1808 VersionTuple Deprecated,
1809 VersionTuple Obsoleted,
1810 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001811 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001812 bool Override,
1813 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001814 VersionTuple MergedIntroduced = Introduced;
1815 VersionTuple MergedDeprecated = Deprecated;
1816 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001817 bool FoundAny = false;
1818
Rafael Espindolac67f2232012-05-10 02:50:16 +00001819 if (D->hasAttrs()) {
1820 AttrVec &Attrs = D->getAttrs();
1821 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1822 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1823 if (!OldAA) {
1824 ++i;
1825 continue;
1826 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001827
Rafael Espindolac67f2232012-05-10 02:50:16 +00001828 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1829 if (OldPlatform != Platform) {
1830 ++i;
1831 continue;
1832 }
1833
1834 FoundAny = true;
1835 VersionTuple OldIntroduced = OldAA->getIntroduced();
1836 VersionTuple OldDeprecated = OldAA->getDeprecated();
1837 VersionTuple OldObsoleted = OldAA->getObsoleted();
1838 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001839
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001840 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1841 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1842 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1843 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001844 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001845 if (Override) {
1846 int Which = -1;
1847 VersionTuple FirstVersion;
1848 VersionTuple SecondVersion;
1849 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1850 Which = 0;
1851 FirstVersion = OldIntroduced;
1852 SecondVersion = Introduced;
1853 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1854 Which = 1;
1855 FirstVersion = Deprecated;
1856 SecondVersion = OldDeprecated;
1857 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1858 Which = 2;
1859 FirstVersion = Obsoleted;
1860 SecondVersion = OldObsoleted;
1861 }
1862
1863 if (Which == -1) {
1864 Diag(OldAA->getLocation(),
1865 diag::warn_mismatched_availability_override_unavail)
1866 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1867 } else {
1868 Diag(OldAA->getLocation(),
1869 diag::warn_mismatched_availability_override)
1870 << Which
1871 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1872 << FirstVersion.getAsString() << SecondVersion.getAsString();
1873 }
1874 Diag(Range.getBegin(), diag::note_overridden_method);
1875 } else {
1876 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1877 Diag(Range.getBegin(), diag::note_previous_attribute);
1878 }
1879
Rafael Espindolac67f2232012-05-10 02:50:16 +00001880 Attrs.erase(Attrs.begin() + i);
1881 --e;
1882 continue;
1883 }
1884
1885 VersionTuple MergedIntroduced2 = MergedIntroduced;
1886 VersionTuple MergedDeprecated2 = MergedDeprecated;
1887 VersionTuple MergedObsoleted2 = MergedObsoleted;
1888
1889 if (MergedIntroduced2.empty())
1890 MergedIntroduced2 = OldIntroduced;
1891 if (MergedDeprecated2.empty())
1892 MergedDeprecated2 = OldDeprecated;
1893 if (MergedObsoleted2.empty())
1894 MergedObsoleted2 = OldObsoleted;
1895
1896 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1897 MergedIntroduced2, MergedDeprecated2,
1898 MergedObsoleted2)) {
1899 Attrs.erase(Attrs.begin() + i);
1900 --e;
1901 continue;
1902 }
1903
1904 MergedIntroduced = MergedIntroduced2;
1905 MergedDeprecated = MergedDeprecated2;
1906 MergedObsoleted = MergedObsoleted2;
1907 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001908 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001909 }
1910
1911 if (FoundAny &&
1912 MergedIntroduced == Introduced &&
1913 MergedDeprecated == Deprecated &&
1914 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001915 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001916
Ted Kremenekb5445722013-04-06 00:34:27 +00001917 // Only create a new attribute if !Override, but we want to do
1918 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001919 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001920 MergedDeprecated, MergedObsoleted) &&
1921 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001922 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1923 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001924 Obsoleted, IsUnavailable, Message,
1925 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001926 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001927 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001928}
1929
Chandler Carruthedc2c642011-07-02 00:01:44 +00001930static void handleAvailabilityAttr(Sema &S, Decl *D,
1931 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001932 if (!checkAttributeNumArgs(S, Attr, 1))
1933 return;
1934 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001935 unsigned Index = Attr.getAttributeSpellingListIndex();
1936
Aaron Ballman00e99962013-08-31 01:11:41 +00001937 IdentifierInfo *II = Platform->Ident;
1938 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1939 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1940 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001941
Rafael Espindolac231fab2013-01-08 21:30:32 +00001942 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1943 if (!ND) {
1944 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1945 return;
1946 }
1947
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001948 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1949 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1950 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001951 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001952 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001953 if (const StringLiteral *SE =
1954 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001955 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001956
Aaron Ballman00e99962013-08-31 01:11:41 +00001957 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001958 Introduced.Version,
1959 Deprecated.Version,
1960 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001961 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001962 /*Override=*/false,
1963 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001964 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001965 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001966}
1967
John McCalld041a9b2013-02-20 01:54:26 +00001968template <class T>
1969static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1970 typename T::VisibilityType value,
1971 unsigned attrSpellingListIndex) {
1972 T *existingAttr = D->getAttr<T>();
1973 if (existingAttr) {
1974 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1975 if (existingValue == value)
1976 return NULL;
1977 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1978 S.Diag(range.getBegin(), diag::note_previous_attribute);
1979 D->dropAttr<T>();
1980 }
1981 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1982}
1983
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001984VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001985 VisibilityAttr::VisibilityType Vis,
1986 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001987 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1988 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001989}
1990
John McCalld041a9b2013-02-20 01:54:26 +00001991TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1992 TypeVisibilityAttr::VisibilityType Vis,
1993 unsigned AttrSpellingListIndex) {
1994 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1995 AttrSpellingListIndex);
1996}
1997
1998static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1999 bool isTypeVisibility) {
2000 // Visibility attributes don't mean anything on a typedef.
2001 if (isa<TypedefNameDecl>(D)) {
2002 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2003 << Attr.getName();
2004 return;
2005 }
2006
2007 // 'type_visibility' can only go on a type or namespace.
2008 if (isTypeVisibility &&
2009 !(isa<TagDecl>(D) ||
2010 isa<ObjCInterfaceDecl>(D) ||
2011 isa<NamespaceDecl>(D))) {
2012 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2013 << Attr.getName() << ExpectedTypeOrNamespace;
2014 return;
2015 }
2016
Benjamin Kramer70370212013-09-09 15:08:57 +00002017 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002018 StringRef TypeStr;
2019 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002020 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002021 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002022
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002023 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002024 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002025 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002026 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002027 return;
2028 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002029
2030 // Complain about attempts to use protected visibility on targets
2031 // (like Darwin) that don't support it.
2032 if (type == VisibilityAttr::Protected &&
2033 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2034 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2035 type = VisibilityAttr::Default;
2036 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002037
Michael Han99315932013-01-24 16:46:58 +00002038 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002039 clang::Attr *newAttr;
2040 if (isTypeVisibility) {
2041 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2042 (TypeVisibilityAttr::VisibilityType) type,
2043 Index);
2044 } else {
2045 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2046 }
2047 if (newAttr)
2048 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002049}
2050
Chandler Carruthedc2c642011-07-02 00:01:44 +00002051static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2052 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002053 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002054 if (!Attr.isArgIdent(0)) {
2055 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2056 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002057 return;
2058 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002059
Aaron Ballman682ee422013-09-11 19:47:58 +00002060 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2061 ObjCMethodFamilyAttr::FamilyKind F;
2062 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2063 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2064 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002065 return;
2066 }
2067
Aaron Ballman682ee422013-09-11 19:47:58 +00002068 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCall31168b02011-06-15 23:02:42 +00002069 !method->getResultType()->isObjCObjectPointerType()) {
2070 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2071 << method->getResultType();
2072 // Ignore the attribute.
2073 return;
2074 }
2075
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002076 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman682ee422013-09-11 19:47:58 +00002077 S.Context, F));
John McCall86bc21f2011-03-02 11:33:24 +00002078}
2079
Chandler Carruthedc2c642011-07-02 00:01:44 +00002080static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002081 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002082 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002083 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002084 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2085 return;
2086 }
2087 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002088 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2089 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002090 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002091 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2092 return;
2093 }
2094 }
2095 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002096 // It is okay to include this attribute on properties, e.g.:
2097 //
2098 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2099 //
2100 // In this case it follows tradition and suppresses an error in the above
2101 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002102 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002103 }
Michael Han99315932013-01-24 16:46:58 +00002104 D->addAttr(::new (S.Context)
2105 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2106 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002107}
2108
Chandler Carruthedc2c642011-07-02 00:01:44 +00002109static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002110 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002111 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002112 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002113 return;
2114 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002115
Aaron Ballman00e99962013-08-31 01:11:41 +00002116 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002117 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002118 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2119 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2120 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002121 return;
2122 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002123
Michael Han99315932013-01-24 16:46:58 +00002124 D->addAttr(::new (S.Context)
2125 BlocksAttr(Attr.getRange(), S.Context, type,
2126 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002127}
2128
Chandler Carruthedc2c642011-07-02 00:01:44 +00002129static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002130 // check the attribute arguments.
2131 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002132 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002133 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002134 }
2135
Aaron Ballman18a78382013-11-21 00:28:23 +00002136 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002137 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002138 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002139 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002140 if (E->isTypeDependent() || E->isValueDependent() ||
2141 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002142 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002143 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002144 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002145 return;
2146 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002147
John McCallb46f2872011-09-09 07:56:05 +00002148 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002149 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2150 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002151 return;
2152 }
John McCallb46f2872011-09-09 07:56:05 +00002153
2154 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002155 }
2156
Aaron Ballman18a78382013-11-21 00:28:23 +00002157 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002158 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002159 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002160 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002161 if (E->isTypeDependent() || E->isValueDependent() ||
2162 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002163 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002164 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002165 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002166 return;
2167 }
2168 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002169
John McCallb46f2872011-09-09 07:56:05 +00002170 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002171 // FIXME: This error message could be improved, it would be nice
2172 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002173 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2174 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002175 return;
2176 }
2177 }
2178
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002179 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002180 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002181 if (isa<FunctionNoProtoType>(FT)) {
2182 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2183 return;
2184 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002185
Chris Lattner9363e312009-03-17 23:03:47 +00002186 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002187 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002188 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002189 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002190 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002191 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002192 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002193 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002194 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002195 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2196 if (!BD->isVariadic()) {
2197 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2198 return;
2199 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002200 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002201 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002202 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002203 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002204 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002205 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002206 int m = Ty->isFunctionPointerType() ? 0 : 1;
2207 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002208 return;
2209 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002210 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002211 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002212 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002213 return;
2214 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002215 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002216 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002217 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002218 return;
2219 }
Michael Han99315932013-01-24 16:46:58 +00002220 D->addAttr(::new (S.Context)
2221 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2222 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002223}
2224
Chandler Carruthedc2c642011-07-02 00:01:44 +00002225static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002226 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002227 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002228 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002229 return;
2230 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002231
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002232 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2233 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2234 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002235 return;
2236 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002237 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2238 if (MD->getResultType()->isVoidType()) {
2239 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2240 << Attr.getName() << 1;
2241 return;
2242 }
2243
Michael Han99315932013-01-24 16:46:58 +00002244 D->addAttr(::new (S.Context)
2245 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2246 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002247}
2248
Chandler Carruthedc2c642011-07-02 00:01:44 +00002249static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002250 // weak_import only applies to variable & function declarations.
2251 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002252 if (!D->canBeWeakImported(isDef)) {
2253 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002254 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2255 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002256 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002257 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002258 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002259 // Nothing to warn about here.
2260 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002261 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002262 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002263
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002264 return;
2265 }
2266
Michael Han99315932013-01-24 16:46:58 +00002267 D->addAttr(::new (S.Context)
2268 WeakImportAttr(Attr.getRange(), S.Context,
2269 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002270}
2271
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002272// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002273template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002274static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002275 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002276 uint32_t WGSize[3];
2277 for (unsigned i = 0; i < 3; ++i)
2278 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002279 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002280
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002281 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2282 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2283 Existing->getYDim() == WGSize[1] &&
2284 Existing->getZDim() == WGSize[2]))
2285 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002286
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002287 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2288 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002289 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002290}
2291
Joey Goulyaba589c2013-03-08 09:42:32 +00002292static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002293 if (!Attr.hasParsedType()) {
2294 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2295 << Attr.getName() << 1;
2296 return;
2297 }
2298
Richard Smithb87c4652013-10-31 21:23:20 +00002299 TypeSourceInfo *ParmTSI = 0;
2300 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2301 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002302
2303 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2304 (ParmType->isBooleanType() ||
2305 !ParmType->isIntegralType(S.getASTContext()))) {
2306 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2307 << ParmType;
2308 return;
2309 }
2310
Aaron Ballmana9e05402013-12-02 22:16:55 +00002311 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002312 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002313 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2314 return;
2315 }
2316 }
2317
2318 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Richard Smithb87c4652013-10-31 21:23:20 +00002319 ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002320}
2321
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002322SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002323 StringRef Name,
2324 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002325 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2326 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002327 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002328 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2329 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002330 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002331 }
Michael Han99315932013-01-24 16:46:58 +00002332 return ::new (Context) SectionAttr(Range, Context, Name,
2333 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002334}
2335
Chandler Carruthedc2c642011-07-02 00:01:44 +00002336static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002337 // Make sure that there is a string literal as the sections's single
2338 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002339 StringRef Str;
2340 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002341 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002342 return;
Mike Stump11289f42009-09-09 15:08:12 +00002343
Chris Lattner30ba6742009-08-10 19:03:04 +00002344 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002345 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002346 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002347 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002348 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002349 return;
2350 }
Mike Stump11289f42009-09-09 15:08:12 +00002351
Chris Lattner20aee9b2010-01-12 20:58:53 +00002352 // This attribute cannot be applied to local variables.
2353 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002354 S.Diag(LiteralLoc, diag::err_attribute_section_local_variable);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002355 return;
2356 }
Michael Han99315932013-01-24 16:46:58 +00002357
2358 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002359 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002360 if (NewAttr)
2361 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002362}
2363
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002364
Chandler Carruthedc2c642011-07-02 00:01:44 +00002365static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002366 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002367 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002368 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002369 } else {
Michael Han99315932013-01-24 16:46:58 +00002370 D->addAttr(::new (S.Context)
2371 NoThrowAttr(Attr.getRange(), S.Context,
2372 Attr.getAttributeSpellingListIndex()));
Douglas Gregor88336832011-06-15 05:45:11 +00002373 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002374}
2375
Chandler Carruthedc2c642011-07-02 00:01:44 +00002376static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002377 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002378 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002379 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002380 } else {
Michael Han99315932013-01-24 16:46:58 +00002381 D->addAttr(::new (S.Context)
2382 ConstAttr(Attr.getRange(), S.Context,
2383 Attr.getAttributeSpellingListIndex() ));
Douglas Gregor88336832011-06-15 05:45:11 +00002384 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002385}
2386
Chandler Carruthedc2c642011-07-02 00:01:44 +00002387static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002388 VarDecl *VD = cast<VarDecl>(D);
2389 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002390 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002391 return;
2392 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002393
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002394 Expr *E = Attr.getArgAsExpr(0);
2395 SourceLocation Loc = E->getExprLoc();
2396 FunctionDecl *FD = 0;
2397 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002398
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002399 // gcc only allows for simple identifiers. Since we support more than gcc, we
2400 // will warn the user.
2401 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2402 if (DRE->hasQualifier())
2403 S.Diag(Loc, diag::warn_cleanup_ext);
2404 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2405 NI = DRE->getNameInfo();
2406 if (!FD) {
2407 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2408 << NI.getName();
2409 return;
2410 }
2411 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2412 if (ULE->hasExplicitTemplateArgs())
2413 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002414 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2415 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002416 if (!FD) {
2417 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2418 << NI.getName();
2419 if (ULE->getType() == S.Context.OverloadTy)
2420 S.NoteAllOverloadCandidates(ULE);
2421 return;
2422 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002423 } else {
2424 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002425 return;
2426 }
2427
Anders Carlssond277d792009-01-31 01:16:18 +00002428 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002429 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2430 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002431 return;
2432 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002433
Anders Carlsson723f55d2009-02-07 23:16:50 +00002434 // We're currently more strict than GCC about what function types we accept.
2435 // If this ever proves to be a problem it should be easy to fix.
2436 QualType Ty = S.Context.getPointerType(VD->getType());
2437 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002438 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2439 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002440 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2441 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002442 return;
2443 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002444
Michael Han99315932013-01-24 16:46:58 +00002445 D->addAttr(::new (S.Context)
2446 CleanupAttr(Attr.getRange(), S.Context, FD,
2447 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002448}
2449
Mike Stumpd3bb5572009-07-24 19:02:52 +00002450/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002451/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002452static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002453 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002454 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002455 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002456 return;
2457 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002458
Aaron Ballman00e99962013-08-31 01:11:41 +00002459 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002460 uint64_t ArgIdx;
2461 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2462 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002463 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002464
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002465 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002466 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002467
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002468 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2469 if (not_nsstring_type &&
2470 !isCFStringType(Ty, S.Context) &&
2471 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002472 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002473 // FIXME: Should highlight the actual expression that has the wrong type.
2474 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002475 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002476 << IdxExpr->getSourceRange();
2477 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002478 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002479 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002480 if (!isNSStringType(Ty, S.Context) &&
2481 !isCFStringType(Ty, S.Context) &&
2482 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002483 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002484 // FIXME: Should highlight the actual expression that has the wrong type.
2485 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002486 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002487 << IdxExpr->getSourceRange();
2488 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002489 }
2490
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002491 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2492 // because that has corrected for the implicit this parameter, and is zero-
2493 // based. The attribute expects what the user wrote explicitly.
2494 llvm::APSInt Val;
2495 IdxExpr->EvaluateAsInt(Val, S.Context);
2496
Michael Han99315932013-01-24 16:46:58 +00002497 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002498 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002499 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002500}
2501
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002502enum FormatAttrKind {
2503 CFStringFormat,
2504 NSStringFormat,
2505 StrftimeFormat,
2506 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002507 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002508 InvalidFormat
2509};
2510
2511/// getFormatAttrKind - Map from format attribute names to supported format
2512/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002513static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002514 return llvm::StringSwitch<FormatAttrKind>(Format)
2515 // Check for formats that get handled specially.
2516 .Case("NSString", NSStringFormat)
2517 .Case("CFString", CFStringFormat)
2518 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002519
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002520 // Otherwise, check for supported formats.
2521 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2522 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2523 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002524
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002525 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2526 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002527}
2528
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002529/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002530/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002531static void handleInitPriorityAttr(Sema &S, Decl *D,
2532 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002533 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002534 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2535 return;
2536 }
2537
Aaron Ballman4a611152013-11-27 16:34:09 +00002538 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002539 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2540 Attr.setInvalid();
2541 return;
2542 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002543 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002544 if (S.Context.getAsArrayType(T))
2545 T = S.Context.getBaseElementType(T);
2546 if (!T->getAs<RecordType>()) {
2547 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2548 Attr.setInvalid();
2549 return;
2550 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002551
2552 Expr *E = Attr.getArgAsExpr(0);
2553 uint32_t prioritynum;
2554 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002555 Attr.setInvalid();
2556 return;
2557 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002558
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002559 if (prioritynum < 101 || prioritynum > 65535) {
2560 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002561 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002562 Attr.setInvalid();
2563 return;
2564 }
Michael Han99315932013-01-24 16:46:58 +00002565 D->addAttr(::new (S.Context)
2566 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2567 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002568}
2569
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002570FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2571 IdentifierInfo *Format, int FormatIdx,
2572 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002573 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002574 // Check whether we already have an equivalent format attribute.
2575 for (specific_attr_iterator<FormatAttr>
2576 i = D->specific_attr_begin<FormatAttr>(),
2577 e = D->specific_attr_end<FormatAttr>();
2578 i != e ; ++i) {
2579 FormatAttr *f = *i;
2580 if (f->getType() == Format &&
2581 f->getFormatIdx() == FormatIdx &&
2582 f->getFirstArg() == FirstArg) {
2583 // If we don't have a valid location for this attribute, adopt the
2584 // location.
2585 if (f->getLocation().isInvalid())
2586 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002587 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002588 }
2589 }
2590
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002591 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2592 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002593}
2594
Mike Stumpd3bb5572009-07-24 19:02:52 +00002595/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002596/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002597static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002598 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002599 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002600 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002601 return;
2602 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002603
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002604 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002605 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002606 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002607 return;
2608 }
2609
Chandler Carruth743682b2010-11-16 08:35:43 +00002610 // In C++ the implicit 'this' function parameter also counts, and they are
2611 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002612 bool HasImplicitThisParam = isInstanceMethod(D);
2613 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002614
Aaron Ballman00e99962013-08-31 01:11:41 +00002615 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2616 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002617
2618 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002619 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002620 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002621 // If we've modified the string name, we need a new identifier for it.
2622 II = &S.Context.Idents.get(Format);
2623 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002624
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002625 // Check for supported formats.
2626 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002627
2628 if (Kind == IgnoredFormat)
2629 return;
2630
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002631 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002632 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman00e99962013-08-31 01:11:41 +00002633 << "format" << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002634 return;
2635 }
2636
2637 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002638 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002639 uint32_t Idx;
2640 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002641 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002642
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002643 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002644 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002645 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002646 return;
2647 }
2648
2649 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002650 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002651
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002652 if (HasImplicitThisParam) {
2653 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002654 S.Diag(Attr.getLoc(),
2655 diag::err_format_attribute_implicit_this_format_string)
2656 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002657 return;
2658 }
2659 ArgIdx--;
2660 }
Mike Stump11289f42009-09-09 15:08:12 +00002661
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002662 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002663 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002664
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002665 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002666 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002667 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2668 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002669 return;
2670 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002671 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002672 // FIXME: do we need to check if the type is NSString*? What are the
2673 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002674 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002675 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002676 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2677 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002678 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002679 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002680 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002681 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002682 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002683 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2684 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002685 return;
2686 }
2687
2688 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002689 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002690 uint32_t FirstArg;
2691 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002692 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002693
2694 // check if the function is variadic if the 3rd argument non-zero
2695 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002696 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002697 ++NumArgs; // +1 for ...
2698 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002699 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002700 return;
2701 }
2702 }
2703
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002704 // strftime requires FirstArg to be 0 because it doesn't read from any
2705 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002706 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002707 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002708 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2709 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002710 return;
2711 }
2712 // if 0 it disables parameter checking (to use with e.g. va_list)
2713 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002714 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002715 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002716 return;
2717 }
2718
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002719 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002720 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002721 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002722 if (NewAttr)
2723 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002724}
2725
Chandler Carruthedc2c642011-07-02 00:01:44 +00002726static void handleTransparentUnionAttr(Sema &S, Decl *D,
2727 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002728 // Try to find the underlying union declaration.
2729 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002730 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002731 if (TD && TD->getUnderlyingType()->isUnionType())
2732 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2733 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002734 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002735
2736 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002737 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002738 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002739 return;
2740 }
2741
John McCallf937c022011-10-07 06:10:15 +00002742 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002743 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002744 diag::warn_transparent_union_attribute_not_definition);
2745 return;
2746 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002747
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002748 RecordDecl::field_iterator Field = RD->field_begin(),
2749 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002750 if (Field == FieldEnd) {
2751 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2752 return;
2753 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002754
David Blaikie40ed2972012-06-06 20:45:41 +00002755 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002756 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002757 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002758 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002759 diag::warn_transparent_union_attribute_floating)
2760 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002761 return;
2762 }
2763
2764 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2765 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2766 for (; Field != FieldEnd; ++Field) {
2767 QualType FieldType = Field->getType();
2768 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2769 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2770 // Warn if we drop the attribute.
2771 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002772 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002773 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002774 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002775 diag::warn_transparent_union_attribute_field_size_align)
2776 << isSize << Field->getDeclName() << FieldBits;
2777 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002778 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002779 diag::note_transparent_union_first_field_size_align)
2780 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002781 return;
2782 }
2783 }
2784
Michael Han99315932013-01-24 16:46:58 +00002785 RD->addAttr(::new (S.Context)
2786 TransparentUnionAttr(Attr.getRange(), S.Context,
2787 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002788}
2789
Chandler Carruthedc2c642011-07-02 00:01:44 +00002790static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002791 // Make sure that there is a string literal as the annotation's single
2792 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002793 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002794 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002795 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002796
2797 // Don't duplicate annotations that are already set.
2798 for (specific_attr_iterator<AnnotateAttr>
2799 i = D->specific_attr_begin<AnnotateAttr>(),
2800 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002801 if ((*i)->getAnnotation() == Str)
2802 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002803 }
Michael Han99315932013-01-24 16:46:58 +00002804
2805 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002806 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002807 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002808}
2809
Chandler Carruthedc2c642011-07-02 00:01:44 +00002810static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002811 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002812 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002813 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2814 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002815 return;
2816 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002817
Richard Smith848e1f12013-02-01 08:12:08 +00002818 if (Attr.getNumArgs() == 0) {
2819 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2820 true, 0, Attr.getAttributeSpellingListIndex()));
2821 return;
2822 }
2823
Aaron Ballman00e99962013-08-31 01:11:41 +00002824 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002825 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2826 S.Diag(Attr.getEllipsisLoc(),
2827 diag::err_pack_expansion_without_parameter_packs);
2828 return;
2829 }
2830
2831 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2832 return;
2833
2834 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2835 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002836}
2837
2838void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002839 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002840 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2841 SourceLocation AttrLoc = AttrRange.getBegin();
2842
Richard Smith1dba27c2013-01-29 09:02:09 +00002843 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002844 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002845 // C++11 [dcl.align]p1:
2846 // An alignment-specifier may be applied to a variable or to a class
2847 // data member, but it shall not be applied to a bit-field, a function
2848 // parameter, the formal parameter of a catch clause, or a variable
2849 // declared with the register storage class specifier. An
2850 // alignment-specifier may also be applied to the declaration of a class
2851 // or enumeration type.
2852 // C11 6.7.5/2:
2853 // An alignment attribute shall not be specified in a declaration of
2854 // a typedef, or a bit-field, or a function, or a parameter, or an
2855 // object declared with the register storage-class specifier.
2856 int DiagKind = -1;
2857 if (isa<ParmVarDecl>(D)) {
2858 DiagKind = 0;
2859 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2860 if (VD->getStorageClass() == SC_Register)
2861 DiagKind = 1;
2862 if (VD->isExceptionVariable())
2863 DiagKind = 2;
2864 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2865 if (FD->isBitField())
2866 DiagKind = 3;
2867 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00002868 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
2869 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00002870 << (TmpAttr.isC11() ? ExpectedVariableOrField
2871 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002872 return;
2873 }
2874 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002875 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smithbc8caaf2013-02-22 04:55:39 +00002876 << TmpAttr.isC11() << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002877 return;
2878 }
2879 }
2880
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002881 if (E->isTypeDependent() || E->isValueDependent()) {
2882 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002883 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2884 AA->setPackExpansion(IsPackExpansion);
2885 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002886 return;
2887 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002888
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002889 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002890 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002891 ExprResult ICE
2892 = VerifyIntegerConstantExpression(E, &Alignment,
2893 diag::err_aligned_attribute_argument_not_int,
2894 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002895 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002896 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002897
2898 // C++11 [dcl.align]p2:
2899 // -- if the constant expression evaluates to zero, the alignment
2900 // specifier shall have no effect
2901 // C11 6.7.5p6:
2902 // An alignment specification of zero has no effect.
2903 if (!(TmpAttr.isAlignas() && !Alignment) &&
2904 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002905 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2906 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002907 return;
2908 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002909
Richard Smith848e1f12013-02-01 08:12:08 +00002910 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00002911 // We've already verified it's a power of 2, now let's make sure it's
2912 // 8192 or less.
2913 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00002914 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00002915 << E->getSourceRange();
2916 return;
2917 }
2918 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002919
Richard Smith44c247f2013-02-22 08:32:16 +00002920 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
2921 ICE.take(), SpellingListIndex);
2922 AA->setPackExpansion(IsPackExpansion);
2923 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002924}
2925
Michael Hanaf02bbe2013-02-01 01:19:17 +00002926void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002927 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002928 // FIXME: Cache the number on the Attr object if non-dependent?
2929 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002930 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2931 SpellingListIndex);
2932 AA->setPackExpansion(IsPackExpansion);
2933 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002934}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002935
Richard Smith848e1f12013-02-01 08:12:08 +00002936void Sema::CheckAlignasUnderalignment(Decl *D) {
2937 assert(D->hasAttrs() && "no attributes on decl");
2938
2939 QualType Ty;
2940 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2941 Ty = VD->getType();
2942 else
2943 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002944 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002945 return;
2946
2947 // C++11 [dcl.align]p5, C11 6.7.5/4:
2948 // The combined effect of all alignment attributes in a declaration shall
2949 // not specify an alignment that is less strict than the alignment that
2950 // would otherwise be required for the entity being declared.
2951 AlignedAttr *AlignasAttr = 0;
2952 unsigned Align = 0;
2953 for (specific_attr_iterator<AlignedAttr>
2954 I = D->specific_attr_begin<AlignedAttr>(),
2955 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
2956 if (I->isAlignmentDependent())
2957 return;
2958 if (I->isAlignas())
2959 AlignasAttr = *I;
2960 Align = std::max(Align, I->getAlignment(Context));
2961 }
2962
2963 if (AlignasAttr && Align) {
2964 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2965 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2966 if (NaturalAlign > RequestedAlign)
2967 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2968 << Ty << (unsigned)NaturalAlign.getQuantity();
2969 }
2970}
2971
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002972/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002973/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002974///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002975/// Despite what would be logical, the mode attribute is a decl attribute, not a
2976/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2977/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002978static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002979 // This attribute isn't documented, but glibc uses it. It changes
2980 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00002981 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00002982 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
2983 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002984 return;
2985 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002986
Aaron Ballman00e99962013-08-31 01:11:41 +00002987 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
2988 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002989
2990 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002991 if (Str.startswith("__") && Str.endswith("__"))
2992 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002993
2994 unsigned DestWidth = 0;
2995 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002996 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002997 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002998 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002999 switch (Str[0]) {
3000 case 'Q': DestWidth = 8; break;
3001 case 'H': DestWidth = 16; break;
3002 case 'S': DestWidth = 32; break;
3003 case 'D': DestWidth = 64; break;
3004 case 'X': DestWidth = 96; break;
3005 case 'T': DestWidth = 128; break;
3006 }
3007 if (Str[1] == 'F') {
3008 IntegerMode = false;
3009 } else if (Str[1] == 'C') {
3010 IntegerMode = false;
3011 ComplexMode = true;
3012 } else if (Str[1] != 'I') {
3013 DestWidth = 0;
3014 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003015 break;
3016 case 4:
3017 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3018 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003019 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003020 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003021 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003022 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003023 break;
3024 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003025 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003026 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003027 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003028 case 11:
3029 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003030 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003031 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003032 }
3033
3034 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003035 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003036 OldTy = TD->getUnderlyingType();
3037 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3038 OldTy = VD->getType();
3039 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003040 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003041 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003042 return;
3043 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003044
John McCall9dd450b2009-09-21 23:43:11 +00003045 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003046 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3047 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003048 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003049 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3050 } else if (ComplexMode) {
3051 if (!OldTy->isComplexType())
3052 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3053 } else {
3054 if (!OldTy->isFloatingType())
3055 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3056 }
3057
Mike Stump87c57ac2009-05-16 07:39:55 +00003058 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3059 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003060 // FIXME: Make sure floating-point mappings are accurate
3061 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003062 if (!DestWidth) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003063 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003064 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003065 }
3066
3067 QualType NewTy;
3068
3069 if (IntegerMode)
3070 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3071 OldTy->isSignedIntegerType());
3072 else
3073 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3074
3075 if (NewTy.isNull()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003076 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003077 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003078 }
3079
Eli Friedman4735374e2009-03-03 06:41:03 +00003080 if (ComplexMode) {
3081 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003082 }
3083
3084 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003085 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3086 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3087 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003088 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003089
3090 D->addAttr(::new (S.Context)
3091 ModeAttr(Attr.getRange(), S.Context, Name,
3092 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003093}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003094
Chandler Carruthedc2c642011-07-02 00:01:44 +00003095static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003096 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3097 if (!VD->hasGlobalStorage())
3098 S.Diag(Attr.getLoc(),
3099 diag::warn_attribute_requires_functions_or_static_globals)
3100 << Attr.getName();
3101 } else if (!isFunctionOrMethod(D)) {
3102 S.Diag(Attr.getLoc(),
3103 diag::warn_attribute_requires_functions_or_static_globals)
3104 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003105 return;
3106 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003107
Michael Han99315932013-01-24 16:46:58 +00003108 D->addAttr(::new (S.Context)
3109 NoDebugAttr(Attr.getRange(), S.Context,
3110 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003111}
3112
Chandler Carruthedc2c642011-07-02 00:01:44 +00003113static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003114 FunctionDecl *FD = cast<FunctionDecl>(D);
3115 if (!FD->getResultType()->isVoidType()) {
3116 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
3117 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
3118 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3119 << FD->getType()
3120 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
3121 "void");
3122 } else {
3123 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3124 << FD->getType();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003125 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003126 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003127 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003128
Aaron Ballman3aff6332013-12-02 19:30:36 +00003129 D->addAttr(::new (S.Context)
3130 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00003131 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003132}
3133
Chandler Carruthedc2c642011-07-02 00:01:44 +00003134static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003135 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003136 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003137 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +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 GNUInlineAttr(Attr.getRange(), S.Context,
3143 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003144}
3145
Chandler Carruthedc2c642011-07-02 00:01:44 +00003146static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003147 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003148
Aaron Ballman02df2e02012-12-09 17:45:41 +00003149 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003150 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003151 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3152 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003153 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003154 return;
3155
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003156 if (!isa<ObjCMethodDecl>(D)) {
3157 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3158 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003159 return;
3160 }
3161
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003162 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003163 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003164 D->addAttr(::new (S.Context)
3165 FastCallAttr(Attr.getRange(), S.Context,
3166 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003167 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003168 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003169 D->addAttr(::new (S.Context)
3170 StdCallAttr(Attr.getRange(), S.Context,
3171 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003172 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003173 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003174 D->addAttr(::new (S.Context)
3175 ThisCallAttr(Attr.getRange(), S.Context,
3176 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003177 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003178 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003179 D->addAttr(::new (S.Context)
3180 CDeclAttr(Attr.getRange(), S.Context,
3181 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003182 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003183 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003184 D->addAttr(::new (S.Context)
3185 PascalAttr(Attr.getRange(), S.Context,
3186 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003187 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003188 case AttributeList::AT_MSABI:
3189 D->addAttr(::new (S.Context)
3190 MSABIAttr(Attr.getRange(), S.Context,
3191 Attr.getAttributeSpellingListIndex()));
3192 return;
3193 case AttributeList::AT_SysVABI:
3194 D->addAttr(::new (S.Context)
3195 SysVABIAttr(Attr.getRange(), S.Context,
3196 Attr.getAttributeSpellingListIndex()));
3197 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003198 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003199 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003200 switch (CC) {
3201 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003202 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003203 break;
3204 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003205 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003206 break;
3207 default:
3208 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003209 }
3210
Michael Han99315932013-01-24 16:46:58 +00003211 D->addAttr(::new (S.Context)
3212 PcsAttr(Attr.getRange(), S.Context, PCS,
3213 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003214 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003215 }
Derek Schuffa2020962012-10-16 22:30:41 +00003216 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003217 D->addAttr(::new (S.Context)
3218 PnaclCallAttr(Attr.getRange(), S.Context,
3219 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003220 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003221 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003222 D->addAttr(::new (S.Context)
3223 IntelOclBiccAttr(Attr.getRange(), S.Context,
3224 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003225 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003226
Abramo Bagnara50099372010-04-30 13:10:51 +00003227 default:
3228 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003229 }
3230}
3231
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003232static void handleOpenCLImageAccessAttr(Sema &S, Decl *D,
3233 const AttributeList &Attr) {
3234 uint32_t ArgNum;
3235 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), ArgNum))
Guy Benyeifb36ede2013-03-24 13:58:12 +00003236 return;
Guy Benyeifb36ede2013-03-24 13:58:12 +00003237
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003238 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(Attr.getRange(),
3239 S.Context, ArgNum));
Guy Benyeifb36ede2013-03-24 13:58:12 +00003240}
3241
Aaron Ballman02df2e02012-12-09 17:45:41 +00003242bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3243 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003244 if (attr.isInvalid())
3245 return true;
3246
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003247 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003248 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003249 attr.setInvalid();
3250 return true;
3251 }
3252
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003253 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3254 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003255 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003256 case AttributeList::AT_CDecl: CC = CC_C; break;
3257 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3258 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3259 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3260 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003261 case AttributeList::AT_MSABI:
3262 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3263 CC_X86_64Win64;
3264 break;
3265 case AttributeList::AT_SysVABI:
3266 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3267 CC_C;
3268 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003269 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003270 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003271 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003272 attr.setInvalid();
3273 return true;
3274 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003275 if (StrRef == "aapcs") {
3276 CC = CC_AAPCS;
3277 break;
3278 } else if (StrRef == "aapcs-vfp") {
3279 CC = CC_AAPCS_VFP;
3280 break;
3281 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003282
3283 attr.setInvalid();
3284 Diag(attr.getLoc(), diag::err_invalid_pcs);
3285 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003286 }
Derek Schuffa2020962012-10-16 22:30:41 +00003287 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003288 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003289 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003290 }
3291
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003292 const TargetInfo &TI = Context.getTargetInfo();
3293 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3294 if (A == TargetInfo::CCCR_Warning) {
3295 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003296
3297 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3298 if (FD)
3299 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3300 TargetInfo::CCMT_NonMember;
3301 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003302 }
3303
John McCall3882ace2011-01-05 12:14:39 +00003304 return false;
3305}
3306
Chandler Carruthedc2c642011-07-02 00:01:44 +00003307static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003308 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003309
3310 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003311 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003312 return;
3313
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003314 if (!isa<ObjCMethodDecl>(D)) {
3315 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3316 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003317 return;
3318 }
Eli Friedman7044b762009-03-27 21:06:47 +00003319
Michael Han99315932013-01-24 16:46:58 +00003320 D->addAttr(::new (S.Context)
3321 RegparmAttr(Attr.getRange(), S.Context, numParams,
3322 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00003323}
3324
3325/// Checks a regparm attribute, returning true if it is ill-formed and
3326/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003327bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3328 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003329 return true;
3330
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003331 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003332 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003333 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003334 }
Eli Friedman7044b762009-03-27 21:06:47 +00003335
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003336 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003337 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003338 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003339 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003340 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003341 }
3342
Douglas Gregore8bbc122011-09-02 00:18:52 +00003343 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003344 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003345 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003346 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003347 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003348 }
3349
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003350 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003351 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003352 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003353 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003354 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003355 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003356 }
3357
John McCall3882ace2011-01-05 12:14:39 +00003358 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003359}
3360
Chandler Carruthedc2c642011-07-02 00:01:44 +00003361static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Aaron Ballman3aff6332013-12-02 19:30:36 +00003362 // check the attribute arguments.
3363 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
3364 // FIXME: 0 is not okay.
3365 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
3366 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003367 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003368
3369 if (!isFunctionOrMethod(D)) {
3370 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3371 << Attr.getName() << ExpectedFunctionOrMethod;
3372 return;
3373 }
3374
3375 uint32_t MaxThreads, MinBlocks;
3376 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1) ||
3377 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(1), MinBlocks, 2))
3378 return;
3379
3380 D->addAttr(::new (S.Context)
3381 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3382 MaxThreads, MinBlocks,
3383 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003384}
3385
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003386static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3387 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003388 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003389 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003390 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003391 return;
3392 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003393
3394 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003395 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003396
Aaron Ballman00e99962013-08-31 01:11:41 +00003397 StringRef AttrName = Attr.getName()->getName();
3398 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003399
3400 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3401 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3402 << Attr.getName() << ExpectedFunctionOrMethod;
3403 return;
3404 }
3405
3406 uint64_t ArgumentIdx;
3407 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3408 Attr.getLoc(), 2,
Aaron Ballman00e99962013-08-31 01:11:41 +00003409 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003410 return;
3411
3412 uint64_t TypeTagIdx;
3413 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3414 Attr.getLoc(), 3,
Aaron Ballman00e99962013-08-31 01:11:41 +00003415 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003416 return;
3417
3418 bool IsPointer = (AttrName == "pointer_with_type_tag");
3419 if (IsPointer) {
3420 // Ensure that buffer has a pointer type.
3421 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3422 if (!BufferTy->isPointerType()) {
3423 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003424 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003425 }
3426 }
3427
Michael Han99315932013-01-24 16:46:58 +00003428 D->addAttr(::new (S.Context)
3429 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3430 ArgumentIdx, TypeTagIdx, IsPointer,
3431 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003432}
3433
3434static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3435 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003436 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003437 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003438 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003439 return;
3440 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003441
3442 if (!checkAttributeNumArgs(S, Attr, 1))
3443 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003444
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003445 if (!isa<VarDecl>(D)) {
3446 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3447 << Attr.getName() << ExpectedVariable;
3448 return;
3449 }
3450
Aaron Ballman00e99962013-08-31 01:11:41 +00003451 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003452 TypeSourceInfo *MatchingCTypeLoc = 0;
3453 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3454 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003455
Michael Han99315932013-01-24 16:46:58 +00003456 D->addAttr(::new (S.Context)
3457 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003458 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003459 Attr.getLayoutCompatible(),
3460 Attr.getMustBeNull(),
3461 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003462}
3463
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003464//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003465// Checker-specific attribute handlers.
3466//===----------------------------------------------------------------------===//
3467
John McCalled433932011-01-25 03:31:58 +00003468static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003469 return type->isDependentType() ||
3470 type->isObjCObjectPointerType() ||
3471 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003472}
3473static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003474 return type->isDependentType() ||
3475 type->isPointerType() ||
3476 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003477}
3478
Chandler Carruthedc2c642011-07-02 00:01:44 +00003479static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003480 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003481 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003482
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003483 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003484 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3485 cf = false;
3486 } else {
3487 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3488 cf = true;
3489 }
3490
3491 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003492 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003493 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003494 return;
3495 }
3496
3497 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003498 param->addAttr(::new (S.Context)
3499 CFConsumedAttr(Attr.getRange(), S.Context,
3500 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003501 else
Michael Han99315932013-01-24 16:46:58 +00003502 param->addAttr(::new (S.Context)
3503 NSConsumedAttr(Attr.getRange(), S.Context,
3504 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003505}
3506
Chandler Carruthedc2c642011-07-02 00:01:44 +00003507static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3508 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003509
John McCalled433932011-01-25 03:31:58 +00003510 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003511
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003512 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003513 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003514 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003515 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003516 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003517 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3518 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003519 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003520 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003521 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003522 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003523 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003524 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003525 return;
3526 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003527
John McCalled433932011-01-25 03:31:58 +00003528 bool typeOK;
3529 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003530 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003531 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003532 case AttributeList::AT_NSReturnsAutoreleased:
3533 case AttributeList::AT_NSReturnsRetained:
3534 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003535 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3536 cf = false;
3537 break;
3538
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003539 case AttributeList::AT_CFReturnsRetained:
3540 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003541 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3542 cf = true;
3543 break;
3544 }
3545
3546 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003547 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003548 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003549 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003550 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003551
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003552 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003553 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003554 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003555 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003556 D->addAttr(::new (S.Context)
3557 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3558 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003559 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003560 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003561 D->addAttr(::new (S.Context)
3562 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3563 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003564 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003565 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003566 D->addAttr(::new (S.Context)
3567 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3568 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003569 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003570 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003571 D->addAttr(::new (S.Context)
3572 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3573 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003574 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003575 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003576 D->addAttr(::new (S.Context)
3577 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3578 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003579 return;
3580 };
3581}
3582
John McCallcf166702011-07-22 08:53:00 +00003583static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3584 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003585 const int EP_ObjCMethod = 1;
3586 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003587
John McCallcf166702011-07-22 08:53:00 +00003588 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003589 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003590 if (isa<ObjCMethodDecl>(D))
3591 resultType = cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003592 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003593 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003594
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003595 if (!resultType->isReferenceType() &&
3596 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003597 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003598 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003599 << attr.getName()
3600 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003601 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003602
3603 // Drop the attribute.
3604 return;
3605 }
3606
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003607 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003608 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3609 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003610}
3611
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003612static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3613 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003614 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003615
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003616 DeclContext *DC = method->getDeclContext();
3617 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3618 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3619 << attr.getName() << 0;
3620 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3621 return;
3622 }
3623 if (method->getMethodFamily() == OMF_dealloc) {
3624 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3625 << attr.getName() << 1;
3626 return;
3627 }
3628
Michael Han99315932013-01-24 16:46:58 +00003629 method->addAttr(::new (S.Context)
3630 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3631 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003632}
3633
Aaron Ballmanfb763042013-12-02 18:05:46 +00003634static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3635 const AttributeList &Attr) {
3636 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr,
3637 "cf_unknown_transfer"))
John McCall32f5fe12011-09-30 05:12:12 +00003638 return;
John McCall32f5fe12011-09-30 05:12:12 +00003639
Aaron Ballmanfb763042013-12-02 18:05:46 +00003640 D->addAttr(::new (S.Context)
3641 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3642 Attr.getAttributeSpellingListIndex()));
3643}
3644
3645static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3646 const AttributeList &Attr) {
3647 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr,
3648 "cf_audited_transfer"))
3649 return;
3650
3651 D->addAttr(::new (S.Context)
3652 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3653 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003654}
3655
John McCallf1e8b342011-09-29 07:17:38 +00003656static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
3657 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003658 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallf1e8b342011-09-29 07:17:38 +00003659
3660 // In Objective-C, verify that the type names an Objective-C type.
3661 // We don't want to check this outside of ObjC because people sometimes
3662 // do crazy C declarations of Objective-C types.
Aaron Ballman00e99962013-08-31 01:11:41 +00003663 if (Parm && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00003664 // Check for an existing type with this name.
Aaron Ballman00e99962013-08-31 01:11:41 +00003665 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallf1e8b342011-09-29 07:17:38 +00003666 Sema::LookupOrdinaryName);
3667 if (S.LookupName(R, Sc)) {
3668 NamedDecl *Target = R.getFoundDecl();
3669 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
3670 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
3671 S.Diag(Target->getLocStart(), diag::note_declared_at);
3672 }
3673 }
3674 }
3675
Michael Han99315932013-01-24 16:46:58 +00003676 D->addAttr(::new (S.Context)
Aaron Ballman00e99962013-08-31 01:11:41 +00003677 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han99315932013-01-24 16:46:58 +00003678 Attr.getAttributeSpellingListIndex()));
John McCallf1e8b342011-09-29 07:17:38 +00003679}
3680
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003681static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3682 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003683 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003684
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003685 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003686 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003687 return;
3688 }
3689
3690 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003691 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003692 Attr.getAttributeSpellingListIndex()));
3693}
3694
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003695static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3696 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003697 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003698
3699 if (!Parm) {
3700 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3701 return;
3702 }
3703
3704 D->addAttr(::new (S.Context)
3705 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3706 Attr.getAttributeSpellingListIndex()));
3707}
3708
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003709static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3710 const AttributeList &Attr) {
3711 IdentifierInfo *RelatedClass =
3712 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0;
3713 if (!RelatedClass) {
3714 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3715 return;
3716 }
3717 IdentifierInfo *ClassMethod =
3718 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0;
3719 IdentifierInfo *InstanceMethod =
3720 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0;
3721 D->addAttr(::new (S.Context)
3722 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3723 ClassMethod, InstanceMethod,
3724 Attr.getAttributeSpellingListIndex()));
3725}
3726
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003727static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3728 const AttributeList &Attr) {
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003729 ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003730 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003731 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003732 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3733 Attr.getAttributeSpellingListIndex()));
3734}
3735
Chandler Carruthedc2c642011-07-02 00:01:44 +00003736static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3737 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003738 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003739
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003740 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003741 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003742}
3743
Chandler Carruthedc2c642011-07-02 00:01:44 +00003744static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3745 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003746 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003747 QualType type = vd->getType();
3748
3749 if (!type->isDependentType() &&
3750 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003751 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003752 << type;
3753 return;
3754 }
3755
3756 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3757
3758 // If we have no lifetime yet, check the lifetime we're presumably
3759 // going to infer.
3760 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3761 lifetime = type->getObjCARCImplicitLifetime();
3762
3763 switch (lifetime) {
3764 case Qualifiers::OCL_None:
3765 assert(type->isDependentType() &&
3766 "didn't infer lifetime for non-dependent type?");
3767 break;
3768
3769 case Qualifiers::OCL_Weak: // meaningful
3770 case Qualifiers::OCL_Strong: // meaningful
3771 break;
3772
3773 case Qualifiers::OCL_ExplicitNone:
3774 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003775 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003776 << (lifetime == Qualifiers::OCL_Autoreleasing);
3777 break;
3778 }
3779
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003780 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003781 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3782 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003783}
3784
Francois Picheta83957a2010-12-19 06:50:37 +00003785//===----------------------------------------------------------------------===//
3786// Microsoft specific attribute handlers.
3787//===----------------------------------------------------------------------===//
3788
Chandler Carruthedc2c642011-07-02 00:01:44 +00003789static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003790 if (!S.LangOpts.CPlusPlus) {
3791 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3792 << Attr.getName() << AttributeLangSupport::C;
3793 return;
3794 }
3795
Aaron Ballman60e705e2013-11-24 20:58:02 +00003796 if (!isa<CXXRecordDecl>(D)) {
3797 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3798 << Attr.getName() << ExpectedClass;
3799 return;
3800 }
3801
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003802 StringRef StrRef;
3803 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003804 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003805 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003806
David Majnemer89085342013-08-09 08:56:20 +00003807 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3808 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003809 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3810 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003811
Reid Kleckner140c4a72013-05-17 14:04:52 +00003812 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003813 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003814 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003815 return;
3816 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003817
David Majnemer89085342013-08-09 08:56:20 +00003818 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003819 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003820 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003821 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003822 return;
3823 }
David Majnemer89085342013-08-09 08:56:20 +00003824 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003825 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003826 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003827 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003828 }
Francois Picheta83957a2010-12-19 06:50:37 +00003829
David Majnemer89085342013-08-09 08:56:20 +00003830 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3831 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003832}
3833
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003834/// Handles semantic checking for features that are common to all attributes,
3835/// such as checking whether a parameter was properly specified, or the correct
3836/// number of arguments were passed, etc.
3837static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
3838 const AttributeList &Attr) {
3839 // Several attributes carry different semantics than the parsing requires, so
3840 // those are opted out of the common handling.
3841 //
3842 // We also bail on unknown and ignored attributes because those are handled
3843 // as part of the target-specific handling logic.
3844 if (Attr.hasCustomParsing() ||
3845 Attr.getKind() == AttributeList::UnknownAttribute ||
3846 Attr.getKind() == AttributeList::IgnoredAttribute)
3847 return false;
3848
Aaron Ballman3aff6332013-12-02 19:30:36 +00003849 // Check whether the attribute requires specific language extensions to be
3850 // enabled.
3851 if (!Attr.diagnoseLangOpts(S))
3852 return true;
3853
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003854 // If there are no optional arguments, then checking for the argument count
3855 // is trivial.
3856 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
3857 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
3858 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003859
3860 // Check whether the attribute appertains to the given subject.
3861 if (!Attr.diagnoseAppertainsTo(S, D))
3862 return true;
3863
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003864 return false;
3865}
3866
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003867//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003868// Top Level Sema Entry Points
3869//===----------------------------------------------------------------------===//
3870
Richard Smithf8a75c32013-08-29 00:47:48 +00003871/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3872/// the attribute applies to decls. If the attribute is a type attribute, just
3873/// silently ignore it if a GNU attribute.
3874static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3875 const AttributeList &Attr,
3876 bool IncludeCXX11Attributes) {
3877 if (Attr.isInvalid())
3878 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003879
Richard Smithf8a75c32013-08-29 00:47:48 +00003880 // Ignore C++11 attributes on declarator chunks: they appertain to the type
3881 // instead.
3882 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
3883 return;
3884
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003885 if (handleCommonAttributeFeatures(S, scope, D, Attr))
3886 return;
3887
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003888 switch (Attr.getKind()) {
Aaron Ballman9beb5172013-12-02 15:13:14 +00003889 case AttributeList::AT_IBAction:
3890 handleSimpleAttribute<IBActionAttr>(S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003891 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
3892 case AttributeList::AT_IBOutletCollection:
3893 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003894 case AttributeList::AT_AddressSpace:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003895 case AttributeList::AT_ObjCGC:
3896 case AttributeList::AT_VectorSize:
3897 case AttributeList::AT_NeonVectorType:
3898 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballman317a77f2013-05-22 23:25:32 +00003899 case AttributeList::AT_Ptr32:
3900 case AttributeList::AT_Ptr64:
3901 case AttributeList::AT_SPtr:
3902 case AttributeList::AT_UPtr:
Mike Stumpd3bb5572009-07-24 19:02:52 +00003903 // Ignore these, these are type attributes, handled by
3904 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003905 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003906 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
3907 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
3908 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
3909 case AttributeList::AT_AlwaysInline:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003910 handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003911 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003912 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00003913 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003914 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
3915 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
3916 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00003917 handleDependencyAttr(S, scope, D, Attr);
3918 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003919 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003920 case AttributeList::AT_CUDAConstant:
3921 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003922 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003923 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman3a8e2d92013-11-27 18:53:58 +00003924 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003925 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00003926 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00003927 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003928 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
3929 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003930 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003931 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00003932 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003933 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00003934 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003935 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
3936 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
3937 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00003938 case AttributeList::AT_CUDADevice:
3939 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003940 case AttributeList::AT_CUDAHost:
3941 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003942 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
3943 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003944 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00003945 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003946 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003947 case AttributeList::AT_MayAlias:
3948 handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00003949 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003950 case AttributeList::AT_NoCommon:
3951 handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003952 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003953 case AttributeList::AT_Overloadable:
3954 handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break;
Richard Smith852e9ce2013-11-27 01:46:48 +00003955 case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003956 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
3957 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003958 case AttributeList::AT_Naked:
3959 handleSimpleAttribute<NakedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003960 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
3961 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003962 case AttributeList::AT_CUDAShared:
3963 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003964 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003965
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003966 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003967 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003968 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003969 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003970
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003971 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00003972 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3973
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003974 case AttributeList::AT_ObjCRequiresSuper:
3975 handleObjCRequiresSuperAttr(S, D, Attr); break;
3976
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003977 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00003978 handleNSBridgedAttr(S, scope, D, Attr); break;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003979
3980 case AttributeList::AT_ObjCBridge:
3981 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003982
3983 case AttributeList::AT_ObjCBridgeMutable:
3984 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003985
3986 case AttributeList::AT_ObjCBridgeRelated:
3987 handleObjCBridgeRelatedAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00003988
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003989 case AttributeList::AT_ObjCDesignatedInitializer:
3990 handleObjCDesignatedInitializer(S, D, Attr); break;
3991
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003992 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00003993 handleCFAuditedTransferAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003994 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00003995 handleCFUnknownTransferAttr(S, D, Attr); break;
John McCall32f5fe12011-09-30 05:12:12 +00003996
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003997 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003998 case AttributeList::AT_CFConsumed:
3999 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4000 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004001 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004002
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004003 case AttributeList::AT_NSReturnsAutoreleased:
4004 case AttributeList::AT_NSReturnsNotRetained:
4005 case AttributeList::AT_CFReturnsNotRetained:
4006 case AttributeList::AT_NSReturnsRetained:
4007 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004008 handleNSReturnsRetainedAttr(S, D, Attr); break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004009 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00004010 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004011 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00004012 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004013 case AttributeList::AT_VecTypeHint:
4014 handleVecTypeHint(S, D, Attr); break;
4015
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004016 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004017 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004018
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004019 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4020 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4021 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004022 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004023 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004024 case AttributeList::AT_ArcWeakrefUnavailable:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004025 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004026 case AttributeList::AT_ObjCRootClass:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004027 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00004028 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek28eace62013-11-23 01:01:34 +00004029 handleObjCSuppresProtocolAttr(S, D, Attr);
4030 break;
4031 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004032 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004033 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4034 case AttributeList::AT_ReturnsTwice:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004035 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004036 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004037 case AttributeList::AT_Visibility:
4038 handleVisibilityAttr(S, D, Attr, false);
4039 break;
4040 case AttributeList::AT_TypeVisibility:
4041 handleVisibilityAttr(S, D, Attr, true);
4042 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004043 case AttributeList::AT_WarnUnused:
Aaron Ballman6a42b5a2013-11-27 16:59:17 +00004044 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004045 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004046 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004047 case AttributeList::AT_Weak:
4048 handleSimpleAttribute<WeakAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004049 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4050 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4051 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004052 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004053 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004054 case AttributeList::AT_ObjCException:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004055 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004056 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004057 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004058 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004059 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4060 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4061 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4062 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004063 case AttributeList::AT_Pure:
4064 handleSimpleAttribute<PureAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004065 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4066 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004067 case AttributeList::AT_NoInline:
4068 handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004069 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004070 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004071 // Just ignore
4072 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004073 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004074 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004075 case AttributeList::AT_StdCall:
4076 case AttributeList::AT_CDecl:
4077 case AttributeList::AT_FastCall:
4078 case AttributeList::AT_ThisCall:
4079 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004080 case AttributeList::AT_MSABI:
4081 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004082 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004083 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004084 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004085 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004086 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004087 case AttributeList::AT_OpenCLKernel:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004088 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004089 case AttributeList::AT_OpenCLImageAccess:
4090 handleOpenCLImageAccessAttr(S, D, Attr);
4091 break;
John McCall8d32c052012-05-22 21:28:12 +00004092
4093 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004094 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004095 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004096 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004097 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004098 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004099 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004100 case AttributeList::AT_SingleInheritance:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004101 handleSimpleAttribute<SingleInheritanceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004102 case AttributeList::AT_MultipleInheritance:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004103 handleSimpleAttribute<MultipleInheritanceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004104 case AttributeList::AT_VirtualInheritance:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004105 handleSimpleAttribute<VirtualInheritanceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004106 case AttributeList::AT_ForceInline:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004107 handleSimpleAttribute<ForceInlineAttr>(S, D, Attr); break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004108 case AttributeList::AT_SelectAny:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004109 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004110
4111 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004112 case AttributeList::AT_AssertExclusiveLock:
4113 handleAssertExclusiveLockAttr(S, D, Attr);
4114 break;
4115 case AttributeList::AT_AssertSharedLock:
4116 handleAssertSharedLockAttr(S, D, Attr);
4117 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004118 case AttributeList::AT_GuardedVar:
Aaron Ballmane61b8b82013-12-02 15:02:49 +00004119 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004120 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004121 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004122 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004123 case AttributeList::AT_ScopedLockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004124 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004125 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004126 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004127 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004128 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004129 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004130 break;
4131 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004132 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004133 break;
4134 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004135 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004136 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004137 case AttributeList::AT_Lockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004138 handleSimpleAttribute<LockableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004139 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004140 handleGuardedByAttr(S, D, Attr);
4141 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004142 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004143 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004144 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004145 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004146 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004147 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004148 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004149 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004150 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004151 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004152 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004153 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004154 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004155 handleLockReturnedAttr(S, D, Attr);
4156 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004157 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004158 handleLocksExcludedAttr(S, D, Attr);
4159 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004160 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004161 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004162 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004163 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004164 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004165 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004166 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004167 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004168 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004169 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004170 handleUnlockFunAttr(S, D, Attr);
4171 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004172 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004173 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004174 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004175 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004176 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004177 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004178
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004179 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004180 case AttributeList::AT_Consumable:
4181 handleConsumableAttr(S, D, Attr);
4182 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004183 case AttributeList::AT_CallableWhen:
4184 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004185 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004186 case AttributeList::AT_ParamTypestate:
4187 handleParamTypestateAttr(S, D, Attr);
4188 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004189 case AttributeList::AT_ReturnTypestate:
4190 handleReturnTypestateAttr(S, D, Attr);
4191 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004192 case AttributeList::AT_SetTypestate:
4193 handleSetTypestateAttr(S, D, Attr);
4194 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004195 case AttributeList::AT_TestTypestate:
4196 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004197 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004198
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004199 // Type safety attributes.
4200 case AttributeList::AT_ArgumentWithTypeTag:
4201 handleArgumentWithTypeTagAttr(S, D, Attr);
4202 break;
4203 case AttributeList::AT_TypeTagForDatatype:
4204 handleTypeTagForDatatypeAttr(S, D, Attr);
4205 break;
4206
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004207 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004208 // Ask target about the attribute.
4209 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4210 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004211 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4212 diag::warn_unhandled_ms_attribute_ignored :
4213 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004214 break;
4215 }
4216}
4217
4218/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4219/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004220void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004221 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004222 bool IncludeCXX11Attributes) {
4223 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004224 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004225
4226 // GCC accepts
4227 // static int a9 __attribute__((weakref));
4228 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004229 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004230 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00004231 cast<NamedDecl>(D)->getNameAsString();
4232 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004233 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004234 }
4235}
4236
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004237// Annotation attributes are the only attributes allowed after an access
4238// specifier.
4239bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4240 const AttributeList *AttrList) {
4241 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004242 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004243 handleAnnotateAttr(*this, ASDecl, *l);
4244 } else {
4245 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4246 return true;
4247 }
4248 }
4249
4250 return false;
4251}
4252
John McCall42856de2011-10-01 05:17:03 +00004253/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4254/// contains any decl attributes that we should warn about.
4255static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4256 for ( ; A; A = A->getNext()) {
4257 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004258 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004259 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4260
4261 if (A->getKind() == AttributeList::UnknownAttribute) {
4262 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4263 << A->getName() << A->getRange();
4264 } else {
4265 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4266 << A->getName() << A->getRange();
4267 }
4268 }
4269}
4270
4271/// checkUnusedDeclAttributes - Given a declarator which is not being
4272/// used to build a declaration, complain about any decl attributes
4273/// which might be lying around on it.
4274void Sema::checkUnusedDeclAttributes(Declarator &D) {
4275 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4276 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4277 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4278 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4279}
4280
Ryan Flynn7d470f32009-07-30 03:15:39 +00004281/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004282/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004283NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4284 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004285 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004286 NamedDecl *NewD = 0;
4287 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004288 FunctionDecl *NewFD;
4289 // FIXME: Missing call to CheckFunctionDeclaration().
4290 // FIXME: Mangling?
4291 // FIXME: Is the qualifier info correct?
4292 // FIXME: Is the DeclContext correct?
4293 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4294 Loc, Loc, DeclarationName(II),
4295 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004296 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004297 FD->hasPrototype(),
4298 false/*isConstexprSpecified*/);
4299 NewD = NewFD;
4300
4301 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004302 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004303
4304 // Fake up parameter variables; they are declared as if this were
4305 // a typedef.
4306 QualType FDTy = FD->getType();
4307 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4308 SmallVector<ParmVarDecl*, 16> Params;
4309 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4310 AE = FT->arg_type_end(); AI != AE; ++AI) {
4311 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4312 Param->setScopeInfo(0, Params.size());
4313 Params.push_back(Param);
4314 }
David Blaikie9c70e042011-09-21 18:16:56 +00004315 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004316 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004317 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4318 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004319 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004320 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004321 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004322 if (VD->getQualifier()) {
4323 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004324 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004325 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004326 }
4327 return NewD;
4328}
4329
James Dennett634962f2012-06-14 21:40:34 +00004330/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004331/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004332void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004333 if (W.getUsed()) return; // only do this once
4334 W.setUsed(true);
4335 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4336 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004337 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004338 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4339 NDId->getName()));
4340 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00004341 WeakTopLevelDecl.push_back(NewD);
4342 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4343 // to insert Decl at TU scope, sorry.
4344 DeclContext *SavedContext = CurContext;
4345 CurContext = Context.getTranslationUnitDecl();
4346 PushOnScopeChains(NewD, S);
4347 CurContext = SavedContext;
4348 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004349 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004350 }
4351}
4352
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004353void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4354 // It's valid to "forward-declare" #pragma weak, in which case we
4355 // have to do this.
4356 LoadExternalWeakUndeclaredIdentifiers();
4357 if (!WeakUndeclaredIdentifiers.empty()) {
4358 NamedDecl *ND = NULL;
4359 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4360 if (VD->isExternC())
4361 ND = VD;
4362 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4363 if (FD->isExternC())
4364 ND = FD;
4365 if (ND) {
4366 if (IdentifierInfo *Id = ND->getIdentifier()) {
4367 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4368 = WeakUndeclaredIdentifiers.find(Id);
4369 if (I != WeakUndeclaredIdentifiers.end()) {
4370 WeakInfo W = I->second;
4371 DeclApplyPragmaWeak(S, ND, W);
4372 WeakUndeclaredIdentifiers[Id] = W;
4373 }
4374 }
4375 }
4376 }
4377}
4378
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004379/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4380/// it, apply them to D. This is a bit tricky because PD can have attributes
4381/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004382void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004383 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004384 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004385 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004386
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004387 // Walk the declarator structure, applying decl attributes that were in a type
4388 // position to the decl itself. This handles cases like:
4389 // int *__attr__(x)** D;
4390 // when X is a decl attribute.
4391 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4392 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004393 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004394
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004395 // Finally, apply any attributes on the decl itself.
4396 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004397 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004398}
John McCall28a6aea2009-11-04 02:18:39 +00004399
John McCall31168b02011-06-15 23:02:42 +00004400/// Is the given declaration allowed to use a forbidden type?
4401static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4402 // Private ivars are always okay. Unfortunately, people don't
4403 // always properly make their ivars private, even in system headers.
4404 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004405 // Function declarations in sys headers will be marked unavailable.
4406 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4407 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004408 return false;
4409
4410 // Require it to be declared in a system header.
4411 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4412}
4413
4414/// Handle a delayed forbidden-type diagnostic.
4415static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4416 Decl *decl) {
4417 if (decl && isForbiddenTypeAllowed(S, decl)) {
4418 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4419 "this system declaration uses an unsupported type"));
4420 return;
4421 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004422 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004423 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004424 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004425 // kind of forbidden type messages on unavailable functions.
4426 if (FD->hasAttr<UnavailableAttr>() &&
4427 diag.getForbiddenTypeDiagnostic() ==
4428 diag::err_arc_array_param_no_ownership) {
4429 diag.Triggered = true;
4430 return;
4431 }
4432 }
John McCall31168b02011-06-15 23:02:42 +00004433
4434 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4435 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4436 diag.Triggered = true;
4437}
4438
John McCall2ec85372012-05-07 06:16:41 +00004439void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4440 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004441 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004442 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004443
John McCall2ec85372012-05-07 06:16:41 +00004444 // When delaying diagnostics to run in the context of a parsed
4445 // declaration, we only want to actually emit anything if parsing
4446 // succeeds.
4447 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004448
John McCall2ec85372012-05-07 06:16:41 +00004449 // We emit all the active diagnostics in this pool or any of its
4450 // parents. In general, we'll get one pool for the decl spec
4451 // and a child pool for each declarator; in a decl group like:
4452 // deprecated_typedef foo, *bar, baz();
4453 // only the declarator pops will be passed decls. This is correct;
4454 // we really do need to consider delayed diagnostics from the decl spec
4455 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004456 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004457 do {
John McCall6347b682012-05-07 06:16:58 +00004458 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004459 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4460 // This const_cast is a bit lame. Really, Triggered should be mutable.
4461 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004462 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004463 continue;
4464
John McCallc1465822011-02-14 07:13:47 +00004465 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004466 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00004467 // Don't bother giving deprecation diagnostics if the decl is invalid.
4468 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00004469 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004470 break;
4471
4472 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004473 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004474 break;
John McCall31168b02011-06-15 23:02:42 +00004475
4476 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004477 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004478 break;
John McCall86121512010-01-27 03:50:35 +00004479 }
4480 }
John McCall2ec85372012-05-07 06:16:41 +00004481 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004482}
4483
John McCall6347b682012-05-07 06:16:58 +00004484/// Given a set of delayed diagnostics, re-emit them as if they had
4485/// been delayed in the current context instead of in the given pool.
4486/// Essentially, this just moves them to the current pool.
4487void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4488 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4489 assert(curPool && "re-emitting in undelayed context not supported");
4490 curPool->steal(pool);
4491}
4492
John McCall28a6aea2009-11-04 02:18:39 +00004493static bool isDeclDeprecated(Decl *D) {
4494 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004495 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004496 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004497 // A category implicitly has the availability of the interface.
4498 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4499 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004500 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4501 return false;
4502}
4503
Eli Friedman971bfa12012-08-08 21:52:41 +00004504static void
4505DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
4506 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004507 const ObjCInterfaceDecl *UnknownObjCClass,
4508 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedman971bfa12012-08-08 21:52:41 +00004509 DeclarationName Name = D->getDeclName();
4510 if (!Message.empty()) {
4511 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
4512 S.Diag(D->getLocation(),
4513 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4514 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004515 if (ObjCPropery)
4516 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4517 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00004518 } else if (!UnknownObjCClass) {
4519 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
4520 S.Diag(D->getLocation(),
4521 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4522 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004523 if (ObjCPropery)
4524 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4525 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00004526 } else {
4527 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
4528 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4529 }
4530}
4531
John McCallb45a1e72010-08-26 02:13:20 +00004532void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00004533 Decl *Ctx) {
4534 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00004535 return;
4536
John McCall86121512010-01-27 03:50:35 +00004537 DD.Triggered = true;
Eli Friedman971bfa12012-08-08 21:52:41 +00004538 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
4539 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004540 DD.getUnknownObjCClass(),
4541 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004542}
4543
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004544void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004545 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004546 const ObjCInterfaceDecl *UnknownObjCClass,
4547 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004548 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004549 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004550 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4551 UnknownObjCClass,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004552 ObjCProperty,
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004553 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004554 return;
4555 }
4556
4557 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00004558 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00004559 return;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004560 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004561}