blob: 01a1bed0d4158b4d14cf5bf0edaacc0c13457a15 [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
Michael Han99315932013-01-24 16:46:58 +00002352 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002353 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002354 if (NewAttr)
2355 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002356}
2357
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002358
Chandler Carruthedc2c642011-07-02 00:01:44 +00002359static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002360 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002361 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002362 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002363 } else {
Michael Han99315932013-01-24 16:46:58 +00002364 D->addAttr(::new (S.Context)
2365 NoThrowAttr(Attr.getRange(), S.Context,
2366 Attr.getAttributeSpellingListIndex()));
Douglas Gregor88336832011-06-15 05:45:11 +00002367 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002368}
2369
Chandler Carruthedc2c642011-07-02 00:01:44 +00002370static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002371 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002372 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002373 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002374 } else {
Michael Han99315932013-01-24 16:46:58 +00002375 D->addAttr(::new (S.Context)
2376 ConstAttr(Attr.getRange(), S.Context,
2377 Attr.getAttributeSpellingListIndex() ));
Douglas Gregor88336832011-06-15 05:45:11 +00002378 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002379}
2380
Chandler Carruthedc2c642011-07-02 00:01:44 +00002381static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002382 VarDecl *VD = cast<VarDecl>(D);
2383 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002384 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002385 return;
2386 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002387
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002388 Expr *E = Attr.getArgAsExpr(0);
2389 SourceLocation Loc = E->getExprLoc();
2390 FunctionDecl *FD = 0;
2391 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002392
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002393 // gcc only allows for simple identifiers. Since we support more than gcc, we
2394 // will warn the user.
2395 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2396 if (DRE->hasQualifier())
2397 S.Diag(Loc, diag::warn_cleanup_ext);
2398 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2399 NI = DRE->getNameInfo();
2400 if (!FD) {
2401 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2402 << NI.getName();
2403 return;
2404 }
2405 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2406 if (ULE->hasExplicitTemplateArgs())
2407 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002408 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2409 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002410 if (!FD) {
2411 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2412 << NI.getName();
2413 if (ULE->getType() == S.Context.OverloadTy)
2414 S.NoteAllOverloadCandidates(ULE);
2415 return;
2416 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002417 } else {
2418 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002419 return;
2420 }
2421
Anders Carlssond277d792009-01-31 01:16:18 +00002422 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002423 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2424 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002425 return;
2426 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002427
Anders Carlsson723f55d2009-02-07 23:16:50 +00002428 // We're currently more strict than GCC about what function types we accept.
2429 // If this ever proves to be a problem it should be easy to fix.
2430 QualType Ty = S.Context.getPointerType(VD->getType());
2431 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002432 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2433 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002434 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2435 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002436 return;
2437 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002438
Michael Han99315932013-01-24 16:46:58 +00002439 D->addAttr(::new (S.Context)
2440 CleanupAttr(Attr.getRange(), S.Context, FD,
2441 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002442}
2443
Mike Stumpd3bb5572009-07-24 19:02:52 +00002444/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002445/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002446static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002447 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002448 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002449 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002450 return;
2451 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002452
Aaron Ballman00e99962013-08-31 01:11:41 +00002453 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002454 uint64_t ArgIdx;
2455 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2456 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002457 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002458
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002459 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002460 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002461
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002462 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2463 if (not_nsstring_type &&
2464 !isCFStringType(Ty, S.Context) &&
2465 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002466 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002467 // FIXME: Should highlight the actual expression that has the wrong type.
2468 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002469 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002470 << IdxExpr->getSourceRange();
2471 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002472 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002473 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002474 if (!isNSStringType(Ty, S.Context) &&
2475 !isCFStringType(Ty, S.Context) &&
2476 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002477 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002478 // FIXME: Should highlight the actual expression that has the wrong type.
2479 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002480 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002481 << IdxExpr->getSourceRange();
2482 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002483 }
2484
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002485 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2486 // because that has corrected for the implicit this parameter, and is zero-
2487 // based. The attribute expects what the user wrote explicitly.
2488 llvm::APSInt Val;
2489 IdxExpr->EvaluateAsInt(Val, S.Context);
2490
Michael Han99315932013-01-24 16:46:58 +00002491 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002492 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002493 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002494}
2495
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002496enum FormatAttrKind {
2497 CFStringFormat,
2498 NSStringFormat,
2499 StrftimeFormat,
2500 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002501 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002502 InvalidFormat
2503};
2504
2505/// getFormatAttrKind - Map from format attribute names to supported format
2506/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002507static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002508 return llvm::StringSwitch<FormatAttrKind>(Format)
2509 // Check for formats that get handled specially.
2510 .Case("NSString", NSStringFormat)
2511 .Case("CFString", CFStringFormat)
2512 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002513
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002514 // Otherwise, check for supported formats.
2515 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2516 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2517 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002518
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002519 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2520 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002521}
2522
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002523/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002524/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002525static void handleInitPriorityAttr(Sema &S, Decl *D,
2526 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002527 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002528 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2529 return;
2530 }
2531
Aaron Ballman4a611152013-11-27 16:34:09 +00002532 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002533 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2534 Attr.setInvalid();
2535 return;
2536 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002537 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002538 if (S.Context.getAsArrayType(T))
2539 T = S.Context.getBaseElementType(T);
2540 if (!T->getAs<RecordType>()) {
2541 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2542 Attr.setInvalid();
2543 return;
2544 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002545
2546 Expr *E = Attr.getArgAsExpr(0);
2547 uint32_t prioritynum;
2548 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002549 Attr.setInvalid();
2550 return;
2551 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002552
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002553 if (prioritynum < 101 || prioritynum > 65535) {
2554 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002555 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002556 Attr.setInvalid();
2557 return;
2558 }
Michael Han99315932013-01-24 16:46:58 +00002559 D->addAttr(::new (S.Context)
2560 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2561 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002562}
2563
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002564FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2565 IdentifierInfo *Format, int FormatIdx,
2566 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002567 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002568 // Check whether we already have an equivalent format attribute.
2569 for (specific_attr_iterator<FormatAttr>
2570 i = D->specific_attr_begin<FormatAttr>(),
2571 e = D->specific_attr_end<FormatAttr>();
2572 i != e ; ++i) {
2573 FormatAttr *f = *i;
2574 if (f->getType() == Format &&
2575 f->getFormatIdx() == FormatIdx &&
2576 f->getFirstArg() == FirstArg) {
2577 // If we don't have a valid location for this attribute, adopt the
2578 // location.
2579 if (f->getLocation().isInvalid())
2580 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002581 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002582 }
2583 }
2584
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002585 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2586 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002587}
2588
Mike Stumpd3bb5572009-07-24 19:02:52 +00002589/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002590/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002591static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002592 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002593 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002594 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002595 return;
2596 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002597
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002598 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002599 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002600 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002601 return;
2602 }
2603
Chandler Carruth743682b2010-11-16 08:35:43 +00002604 // In C++ the implicit 'this' function parameter also counts, and they are
2605 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002606 bool HasImplicitThisParam = isInstanceMethod(D);
2607 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002608
Aaron Ballman00e99962013-08-31 01:11:41 +00002609 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2610 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002611
2612 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002613 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002614 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002615 // If we've modified the string name, we need a new identifier for it.
2616 II = &S.Context.Idents.get(Format);
2617 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002618
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002619 // Check for supported formats.
2620 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002621
2622 if (Kind == IgnoredFormat)
2623 return;
2624
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002625 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002626 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman00e99962013-08-31 01:11:41 +00002627 << "format" << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002628 return;
2629 }
2630
2631 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002632 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002633 uint32_t Idx;
2634 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002635 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002636
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002637 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002638 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002639 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002640 return;
2641 }
2642
2643 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002644 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002645
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002646 if (HasImplicitThisParam) {
2647 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002648 S.Diag(Attr.getLoc(),
2649 diag::err_format_attribute_implicit_this_format_string)
2650 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002651 return;
2652 }
2653 ArgIdx--;
2654 }
Mike Stump11289f42009-09-09 15:08:12 +00002655
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002656 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002657 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002658
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002659 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002660 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002661 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2662 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002663 return;
2664 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002665 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002666 // FIXME: do we need to check if the type is NSString*? What are the
2667 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002668 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002669 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002670 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2671 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002672 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002673 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002674 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002675 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002676 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002677 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2678 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002679 return;
2680 }
2681
2682 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002683 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002684 uint32_t FirstArg;
2685 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002686 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002687
2688 // check if the function is variadic if the 3rd argument non-zero
2689 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002690 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002691 ++NumArgs; // +1 for ...
2692 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002693 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002694 return;
2695 }
2696 }
2697
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002698 // strftime requires FirstArg to be 0 because it doesn't read from any
2699 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002700 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002701 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002702 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2703 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002704 return;
2705 }
2706 // if 0 it disables parameter checking (to use with e.g. va_list)
2707 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002708 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002709 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002710 return;
2711 }
2712
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002713 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002714 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002715 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002716 if (NewAttr)
2717 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002718}
2719
Chandler Carruthedc2c642011-07-02 00:01:44 +00002720static void handleTransparentUnionAttr(Sema &S, Decl *D,
2721 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002722 // Try to find the underlying union declaration.
2723 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002724 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002725 if (TD && TD->getUnderlyingType()->isUnionType())
2726 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2727 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002728 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002729
2730 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002731 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002732 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002733 return;
2734 }
2735
John McCallf937c022011-10-07 06:10:15 +00002736 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002737 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002738 diag::warn_transparent_union_attribute_not_definition);
2739 return;
2740 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002741
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002742 RecordDecl::field_iterator Field = RD->field_begin(),
2743 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002744 if (Field == FieldEnd) {
2745 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2746 return;
2747 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002748
David Blaikie40ed2972012-06-06 20:45:41 +00002749 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002750 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002751 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002752 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002753 diag::warn_transparent_union_attribute_floating)
2754 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002755 return;
2756 }
2757
2758 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2759 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2760 for (; Field != FieldEnd; ++Field) {
2761 QualType FieldType = Field->getType();
2762 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2763 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2764 // Warn if we drop the attribute.
2765 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002766 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002767 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002768 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002769 diag::warn_transparent_union_attribute_field_size_align)
2770 << isSize << Field->getDeclName() << FieldBits;
2771 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002772 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002773 diag::note_transparent_union_first_field_size_align)
2774 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002775 return;
2776 }
2777 }
2778
Michael Han99315932013-01-24 16:46:58 +00002779 RD->addAttr(::new (S.Context)
2780 TransparentUnionAttr(Attr.getRange(), S.Context,
2781 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002782}
2783
Chandler Carruthedc2c642011-07-02 00:01:44 +00002784static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002785 // Make sure that there is a string literal as the annotation's single
2786 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002787 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002788 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002789 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002790
2791 // Don't duplicate annotations that are already set.
2792 for (specific_attr_iterator<AnnotateAttr>
2793 i = D->specific_attr_begin<AnnotateAttr>(),
2794 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002795 if ((*i)->getAnnotation() == Str)
2796 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002797 }
Michael Han99315932013-01-24 16:46:58 +00002798
2799 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002800 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002801 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002802}
2803
Chandler Carruthedc2c642011-07-02 00:01:44 +00002804static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002805 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002806 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002807 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2808 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002809 return;
2810 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002811
Richard Smith848e1f12013-02-01 08:12:08 +00002812 if (Attr.getNumArgs() == 0) {
2813 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2814 true, 0, Attr.getAttributeSpellingListIndex()));
2815 return;
2816 }
2817
Aaron Ballman00e99962013-08-31 01:11:41 +00002818 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002819 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2820 S.Diag(Attr.getEllipsisLoc(),
2821 diag::err_pack_expansion_without_parameter_packs);
2822 return;
2823 }
2824
2825 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2826 return;
2827
2828 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2829 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002830}
2831
2832void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002833 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002834 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2835 SourceLocation AttrLoc = AttrRange.getBegin();
2836
Richard Smith1dba27c2013-01-29 09:02:09 +00002837 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002838 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002839 // C++11 [dcl.align]p1:
2840 // An alignment-specifier may be applied to a variable or to a class
2841 // data member, but it shall not be applied to a bit-field, a function
2842 // parameter, the formal parameter of a catch clause, or a variable
2843 // declared with the register storage class specifier. An
2844 // alignment-specifier may also be applied to the declaration of a class
2845 // or enumeration type.
2846 // C11 6.7.5/2:
2847 // An alignment attribute shall not be specified in a declaration of
2848 // a typedef, or a bit-field, or a function, or a parameter, or an
2849 // object declared with the register storage-class specifier.
2850 int DiagKind = -1;
2851 if (isa<ParmVarDecl>(D)) {
2852 DiagKind = 0;
2853 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2854 if (VD->getStorageClass() == SC_Register)
2855 DiagKind = 1;
2856 if (VD->isExceptionVariable())
2857 DiagKind = 2;
2858 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2859 if (FD->isBitField())
2860 DiagKind = 3;
2861 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00002862 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
2863 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00002864 << (TmpAttr.isC11() ? ExpectedVariableOrField
2865 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002866 return;
2867 }
2868 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002869 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smithbc8caaf2013-02-22 04:55:39 +00002870 << TmpAttr.isC11() << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002871 return;
2872 }
2873 }
2874
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002875 if (E->isTypeDependent() || E->isValueDependent()) {
2876 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002877 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2878 AA->setPackExpansion(IsPackExpansion);
2879 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002880 return;
2881 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002882
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002883 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002884 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002885 ExprResult ICE
2886 = VerifyIntegerConstantExpression(E, &Alignment,
2887 diag::err_aligned_attribute_argument_not_int,
2888 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002889 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002890 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002891
2892 // C++11 [dcl.align]p2:
2893 // -- if the constant expression evaluates to zero, the alignment
2894 // specifier shall have no effect
2895 // C11 6.7.5p6:
2896 // An alignment specification of zero has no effect.
2897 if (!(TmpAttr.isAlignas() && !Alignment) &&
2898 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002899 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2900 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002901 return;
2902 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002903
Richard Smith848e1f12013-02-01 08:12:08 +00002904 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00002905 // We've already verified it's a power of 2, now let's make sure it's
2906 // 8192 or less.
2907 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00002908 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00002909 << E->getSourceRange();
2910 return;
2911 }
2912 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002913
Richard Smith44c247f2013-02-22 08:32:16 +00002914 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
2915 ICE.take(), SpellingListIndex);
2916 AA->setPackExpansion(IsPackExpansion);
2917 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002918}
2919
Michael Hanaf02bbe2013-02-01 01:19:17 +00002920void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002921 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002922 // FIXME: Cache the number on the Attr object if non-dependent?
2923 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002924 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2925 SpellingListIndex);
2926 AA->setPackExpansion(IsPackExpansion);
2927 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002928}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002929
Richard Smith848e1f12013-02-01 08:12:08 +00002930void Sema::CheckAlignasUnderalignment(Decl *D) {
2931 assert(D->hasAttrs() && "no attributes on decl");
2932
2933 QualType Ty;
2934 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2935 Ty = VD->getType();
2936 else
2937 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002938 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002939 return;
2940
2941 // C++11 [dcl.align]p5, C11 6.7.5/4:
2942 // The combined effect of all alignment attributes in a declaration shall
2943 // not specify an alignment that is less strict than the alignment that
2944 // would otherwise be required for the entity being declared.
2945 AlignedAttr *AlignasAttr = 0;
2946 unsigned Align = 0;
2947 for (specific_attr_iterator<AlignedAttr>
2948 I = D->specific_attr_begin<AlignedAttr>(),
2949 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
2950 if (I->isAlignmentDependent())
2951 return;
2952 if (I->isAlignas())
2953 AlignasAttr = *I;
2954 Align = std::max(Align, I->getAlignment(Context));
2955 }
2956
2957 if (AlignasAttr && Align) {
2958 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2959 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2960 if (NaturalAlign > RequestedAlign)
2961 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2962 << Ty << (unsigned)NaturalAlign.getQuantity();
2963 }
2964}
2965
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002966/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002967/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002968///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002969/// Despite what would be logical, the mode attribute is a decl attribute, not a
2970/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2971/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002972static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002973 // This attribute isn't documented, but glibc uses it. It changes
2974 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00002975 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00002976 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
2977 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002978 return;
2979 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002980
Aaron Ballman00e99962013-08-31 01:11:41 +00002981 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
2982 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002983
2984 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002985 if (Str.startswith("__") && Str.endswith("__"))
2986 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002987
2988 unsigned DestWidth = 0;
2989 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002990 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002991 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002992 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002993 switch (Str[0]) {
2994 case 'Q': DestWidth = 8; break;
2995 case 'H': DestWidth = 16; break;
2996 case 'S': DestWidth = 32; break;
2997 case 'D': DestWidth = 64; break;
2998 case 'X': DestWidth = 96; break;
2999 case 'T': DestWidth = 128; break;
3000 }
3001 if (Str[1] == 'F') {
3002 IntegerMode = false;
3003 } else if (Str[1] == 'C') {
3004 IntegerMode = false;
3005 ComplexMode = true;
3006 } else if (Str[1] != 'I') {
3007 DestWidth = 0;
3008 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003009 break;
3010 case 4:
3011 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3012 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003013 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003014 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003015 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003016 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003017 break;
3018 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003019 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003020 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003021 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003022 case 11:
3023 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003024 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003025 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003026 }
3027
3028 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003029 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003030 OldTy = TD->getUnderlyingType();
3031 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3032 OldTy = VD->getType();
3033 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003034 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003035 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003036 return;
3037 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003038
John McCall9dd450b2009-09-21 23:43:11 +00003039 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003040 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3041 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003042 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003043 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3044 } else if (ComplexMode) {
3045 if (!OldTy->isComplexType())
3046 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3047 } else {
3048 if (!OldTy->isFloatingType())
3049 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3050 }
3051
Mike Stump87c57ac2009-05-16 07:39:55 +00003052 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3053 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003054 // FIXME: Make sure floating-point mappings are accurate
3055 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003056 if (!DestWidth) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003057 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003058 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003059 }
3060
3061 QualType NewTy;
3062
3063 if (IntegerMode)
3064 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3065 OldTy->isSignedIntegerType());
3066 else
3067 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3068
3069 if (NewTy.isNull()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003070 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003071 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003072 }
3073
Eli Friedman4735374e2009-03-03 06:41:03 +00003074 if (ComplexMode) {
3075 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003076 }
3077
3078 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003079 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3080 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3081 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003082 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003083
3084 D->addAttr(::new (S.Context)
3085 ModeAttr(Attr.getRange(), S.Context, Name,
3086 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003087}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003088
Chandler Carruthedc2c642011-07-02 00:01:44 +00003089static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003090 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3091 if (!VD->hasGlobalStorage())
3092 S.Diag(Attr.getLoc(),
3093 diag::warn_attribute_requires_functions_or_static_globals)
3094 << Attr.getName();
3095 } else if (!isFunctionOrMethod(D)) {
3096 S.Diag(Attr.getLoc(),
3097 diag::warn_attribute_requires_functions_or_static_globals)
3098 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003099 return;
3100 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003101
Michael Han99315932013-01-24 16:46:58 +00003102 D->addAttr(::new (S.Context)
3103 NoDebugAttr(Attr.getRange(), S.Context,
3104 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003105}
3106
Chandler Carruthedc2c642011-07-02 00:01:44 +00003107static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003108 FunctionDecl *FD = cast<FunctionDecl>(D);
3109 if (!FD->getResultType()->isVoidType()) {
3110 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
3111 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
3112 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3113 << FD->getType()
3114 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
3115 "void");
3116 } else {
3117 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3118 << FD->getType();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003119 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003120 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003121 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003122
Aaron Ballman3aff6332013-12-02 19:30:36 +00003123 D->addAttr(::new (S.Context)
3124 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00003125 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003126}
3127
Chandler Carruthedc2c642011-07-02 00:01:44 +00003128static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003129 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003130 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003131 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003132 return;
3133 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003134
Michael Han99315932013-01-24 16:46:58 +00003135 D->addAttr(::new (S.Context)
3136 GNUInlineAttr(Attr.getRange(), S.Context,
3137 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003138}
3139
Chandler Carruthedc2c642011-07-02 00:01:44 +00003140static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003141 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003142
Aaron Ballman02df2e02012-12-09 17:45:41 +00003143 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003144 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003145 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3146 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003147 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003148 return;
3149
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003150 if (!isa<ObjCMethodDecl>(D)) {
3151 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3152 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003153 return;
3154 }
3155
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003156 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003157 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003158 D->addAttr(::new (S.Context)
3159 FastCallAttr(Attr.getRange(), S.Context,
3160 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003161 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003162 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003163 D->addAttr(::new (S.Context)
3164 StdCallAttr(Attr.getRange(), S.Context,
3165 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003166 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003167 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003168 D->addAttr(::new (S.Context)
3169 ThisCallAttr(Attr.getRange(), S.Context,
3170 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003171 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003172 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003173 D->addAttr(::new (S.Context)
3174 CDeclAttr(Attr.getRange(), S.Context,
3175 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003176 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003177 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003178 D->addAttr(::new (S.Context)
3179 PascalAttr(Attr.getRange(), S.Context,
3180 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003181 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003182 case AttributeList::AT_MSABI:
3183 D->addAttr(::new (S.Context)
3184 MSABIAttr(Attr.getRange(), S.Context,
3185 Attr.getAttributeSpellingListIndex()));
3186 return;
3187 case AttributeList::AT_SysVABI:
3188 D->addAttr(::new (S.Context)
3189 SysVABIAttr(Attr.getRange(), S.Context,
3190 Attr.getAttributeSpellingListIndex()));
3191 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003192 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003193 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003194 switch (CC) {
3195 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003196 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003197 break;
3198 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003199 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003200 break;
3201 default:
3202 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003203 }
3204
Michael Han99315932013-01-24 16:46:58 +00003205 D->addAttr(::new (S.Context)
3206 PcsAttr(Attr.getRange(), S.Context, PCS,
3207 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003208 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003209 }
Derek Schuffa2020962012-10-16 22:30:41 +00003210 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003211 D->addAttr(::new (S.Context)
3212 PnaclCallAttr(Attr.getRange(), S.Context,
3213 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003214 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003215 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003216 D->addAttr(::new (S.Context)
3217 IntelOclBiccAttr(Attr.getRange(), S.Context,
3218 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003219 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003220
Abramo Bagnara50099372010-04-30 13:10:51 +00003221 default:
3222 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003223 }
3224}
3225
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003226static void handleOpenCLImageAccessAttr(Sema &S, Decl *D,
3227 const AttributeList &Attr) {
3228 uint32_t ArgNum;
3229 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), ArgNum))
Guy Benyeifb36ede2013-03-24 13:58:12 +00003230 return;
Guy Benyeifb36ede2013-03-24 13:58:12 +00003231
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003232 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(Attr.getRange(),
3233 S.Context, ArgNum));
Guy Benyeifb36ede2013-03-24 13:58:12 +00003234}
3235
Aaron Ballman02df2e02012-12-09 17:45:41 +00003236bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3237 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003238 if (attr.isInvalid())
3239 return true;
3240
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003241 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003242 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003243 attr.setInvalid();
3244 return true;
3245 }
3246
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003247 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3248 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003249 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003250 case AttributeList::AT_CDecl: CC = CC_C; break;
3251 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3252 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3253 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3254 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003255 case AttributeList::AT_MSABI:
3256 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3257 CC_X86_64Win64;
3258 break;
3259 case AttributeList::AT_SysVABI:
3260 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3261 CC_C;
3262 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003263 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003264 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003265 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003266 attr.setInvalid();
3267 return true;
3268 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003269 if (StrRef == "aapcs") {
3270 CC = CC_AAPCS;
3271 break;
3272 } else if (StrRef == "aapcs-vfp") {
3273 CC = CC_AAPCS_VFP;
3274 break;
3275 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003276
3277 attr.setInvalid();
3278 Diag(attr.getLoc(), diag::err_invalid_pcs);
3279 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003280 }
Derek Schuffa2020962012-10-16 22:30:41 +00003281 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003282 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003283 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003284 }
3285
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003286 const TargetInfo &TI = Context.getTargetInfo();
3287 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3288 if (A == TargetInfo::CCCR_Warning) {
3289 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003290
3291 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3292 if (FD)
3293 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3294 TargetInfo::CCMT_NonMember;
3295 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003296 }
3297
John McCall3882ace2011-01-05 12:14:39 +00003298 return false;
3299}
3300
Chandler Carruthedc2c642011-07-02 00:01:44 +00003301static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003302 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003303
3304 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003305 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003306 return;
3307
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003308 if (!isa<ObjCMethodDecl>(D)) {
3309 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3310 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003311 return;
3312 }
Eli Friedman7044b762009-03-27 21:06:47 +00003313
Michael Han99315932013-01-24 16:46:58 +00003314 D->addAttr(::new (S.Context)
3315 RegparmAttr(Attr.getRange(), S.Context, numParams,
3316 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00003317}
3318
3319/// Checks a regparm attribute, returning true if it is ill-formed and
3320/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003321bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3322 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003323 return true;
3324
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003325 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003326 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003327 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003328 }
Eli Friedman7044b762009-03-27 21:06:47 +00003329
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003330 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003331 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003332 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003333 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003334 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003335 }
3336
Douglas Gregore8bbc122011-09-02 00:18:52 +00003337 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003338 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003339 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003340 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003341 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003342 }
3343
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003344 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003345 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003346 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003347 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003348 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003349 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003350 }
3351
John McCall3882ace2011-01-05 12:14:39 +00003352 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003353}
3354
Chandler Carruthedc2c642011-07-02 00:01:44 +00003355static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Aaron Ballman3aff6332013-12-02 19:30:36 +00003356 // check the attribute arguments.
3357 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
3358 // FIXME: 0 is not okay.
3359 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
3360 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003361 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003362
3363 if (!isFunctionOrMethod(D)) {
3364 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3365 << Attr.getName() << ExpectedFunctionOrMethod;
3366 return;
3367 }
3368
3369 uint32_t MaxThreads, MinBlocks;
3370 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1) ||
3371 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(1), MinBlocks, 2))
3372 return;
3373
3374 D->addAttr(::new (S.Context)
3375 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3376 MaxThreads, MinBlocks,
3377 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003378}
3379
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003380static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3381 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003382 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003383 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003384 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003385 return;
3386 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003387
3388 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003389 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003390
Aaron Ballman00e99962013-08-31 01:11:41 +00003391 StringRef AttrName = Attr.getName()->getName();
3392 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003393
3394 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3395 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3396 << Attr.getName() << ExpectedFunctionOrMethod;
3397 return;
3398 }
3399
3400 uint64_t ArgumentIdx;
3401 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3402 Attr.getLoc(), 2,
Aaron Ballman00e99962013-08-31 01:11:41 +00003403 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003404 return;
3405
3406 uint64_t TypeTagIdx;
3407 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3408 Attr.getLoc(), 3,
Aaron Ballman00e99962013-08-31 01:11:41 +00003409 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003410 return;
3411
3412 bool IsPointer = (AttrName == "pointer_with_type_tag");
3413 if (IsPointer) {
3414 // Ensure that buffer has a pointer type.
3415 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3416 if (!BufferTy->isPointerType()) {
3417 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003418 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003419 }
3420 }
3421
Michael Han99315932013-01-24 16:46:58 +00003422 D->addAttr(::new (S.Context)
3423 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3424 ArgumentIdx, TypeTagIdx, IsPointer,
3425 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003426}
3427
3428static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3429 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003430 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003431 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003432 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003433 return;
3434 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003435
3436 if (!checkAttributeNumArgs(S, Attr, 1))
3437 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003438
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003439 if (!isa<VarDecl>(D)) {
3440 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3441 << Attr.getName() << ExpectedVariable;
3442 return;
3443 }
3444
Aaron Ballman00e99962013-08-31 01:11:41 +00003445 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003446 TypeSourceInfo *MatchingCTypeLoc = 0;
3447 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3448 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003449
Michael Han99315932013-01-24 16:46:58 +00003450 D->addAttr(::new (S.Context)
3451 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003452 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003453 Attr.getLayoutCompatible(),
3454 Attr.getMustBeNull(),
3455 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003456}
3457
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003458//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003459// Checker-specific attribute handlers.
3460//===----------------------------------------------------------------------===//
3461
John McCalled433932011-01-25 03:31:58 +00003462static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003463 return type->isDependentType() ||
3464 type->isObjCObjectPointerType() ||
3465 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003466}
3467static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003468 return type->isDependentType() ||
3469 type->isPointerType() ||
3470 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003471}
3472
Chandler Carruthedc2c642011-07-02 00:01:44 +00003473static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003474 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003475 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003476
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003477 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003478 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3479 cf = false;
3480 } else {
3481 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3482 cf = true;
3483 }
3484
3485 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003486 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003487 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003488 return;
3489 }
3490
3491 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003492 param->addAttr(::new (S.Context)
3493 CFConsumedAttr(Attr.getRange(), S.Context,
3494 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003495 else
Michael Han99315932013-01-24 16:46:58 +00003496 param->addAttr(::new (S.Context)
3497 NSConsumedAttr(Attr.getRange(), S.Context,
3498 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003499}
3500
Chandler Carruthedc2c642011-07-02 00:01:44 +00003501static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3502 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003503
John McCalled433932011-01-25 03:31:58 +00003504 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003505
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003506 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003507 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003508 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003509 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003510 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003511 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3512 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003513 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003514 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003515 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003516 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003517 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003518 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003519 return;
3520 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003521
John McCalled433932011-01-25 03:31:58 +00003522 bool typeOK;
3523 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003524 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003525 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003526 case AttributeList::AT_NSReturnsAutoreleased:
3527 case AttributeList::AT_NSReturnsRetained:
3528 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003529 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3530 cf = false;
3531 break;
3532
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003533 case AttributeList::AT_CFReturnsRetained:
3534 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003535 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3536 cf = true;
3537 break;
3538 }
3539
3540 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003541 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003542 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003543 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003544 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003545
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003546 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003547 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003548 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003549 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003550 D->addAttr(::new (S.Context)
3551 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3552 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003553 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003554 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003555 D->addAttr(::new (S.Context)
3556 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3557 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003558 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003559 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003560 D->addAttr(::new (S.Context)
3561 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3562 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003563 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003564 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003565 D->addAttr(::new (S.Context)
3566 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3567 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003568 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003569 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003570 D->addAttr(::new (S.Context)
3571 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3572 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003573 return;
3574 };
3575}
3576
John McCallcf166702011-07-22 08:53:00 +00003577static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3578 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003579 const int EP_ObjCMethod = 1;
3580 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003581
John McCallcf166702011-07-22 08:53:00 +00003582 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003583 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003584 if (isa<ObjCMethodDecl>(D))
3585 resultType = cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003586 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003587 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003588
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003589 if (!resultType->isReferenceType() &&
3590 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003591 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003592 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003593 << attr.getName()
3594 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003595 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003596
3597 // Drop the attribute.
3598 return;
3599 }
3600
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003601 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003602 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3603 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003604}
3605
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003606static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3607 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003608 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003609
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003610 DeclContext *DC = method->getDeclContext();
3611 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3612 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3613 << attr.getName() << 0;
3614 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3615 return;
3616 }
3617 if (method->getMethodFamily() == OMF_dealloc) {
3618 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3619 << attr.getName() << 1;
3620 return;
3621 }
3622
Michael Han99315932013-01-24 16:46:58 +00003623 method->addAttr(::new (S.Context)
3624 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3625 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003626}
3627
Aaron Ballmanfb763042013-12-02 18:05:46 +00003628static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3629 const AttributeList &Attr) {
3630 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr,
3631 "cf_unknown_transfer"))
John McCall32f5fe12011-09-30 05:12:12 +00003632 return;
John McCall32f5fe12011-09-30 05:12:12 +00003633
Aaron Ballmanfb763042013-12-02 18:05:46 +00003634 D->addAttr(::new (S.Context)
3635 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3636 Attr.getAttributeSpellingListIndex()));
3637}
3638
3639static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3640 const AttributeList &Attr) {
3641 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr,
3642 "cf_audited_transfer"))
3643 return;
3644
3645 D->addAttr(::new (S.Context)
3646 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3647 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003648}
3649
John McCallf1e8b342011-09-29 07:17:38 +00003650static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
3651 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003652 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallf1e8b342011-09-29 07:17:38 +00003653
3654 // In Objective-C, verify that the type names an Objective-C type.
3655 // We don't want to check this outside of ObjC because people sometimes
3656 // do crazy C declarations of Objective-C types.
Aaron Ballman00e99962013-08-31 01:11:41 +00003657 if (Parm && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00003658 // Check for an existing type with this name.
Aaron Ballman00e99962013-08-31 01:11:41 +00003659 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallf1e8b342011-09-29 07:17:38 +00003660 Sema::LookupOrdinaryName);
3661 if (S.LookupName(R, Sc)) {
3662 NamedDecl *Target = R.getFoundDecl();
3663 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
3664 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
3665 S.Diag(Target->getLocStart(), diag::note_declared_at);
3666 }
3667 }
3668 }
3669
Michael Han99315932013-01-24 16:46:58 +00003670 D->addAttr(::new (S.Context)
Aaron Ballman00e99962013-08-31 01:11:41 +00003671 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han99315932013-01-24 16:46:58 +00003672 Attr.getAttributeSpellingListIndex()));
John McCallf1e8b342011-09-29 07:17:38 +00003673}
3674
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003675static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3676 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003677 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003678
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003679 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003680 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003681 return;
3682 }
3683
3684 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003685 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003686 Attr.getAttributeSpellingListIndex()));
3687}
3688
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003689static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3690 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003691 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003692
3693 if (!Parm) {
3694 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3695 return;
3696 }
3697
3698 D->addAttr(::new (S.Context)
3699 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3700 Attr.getAttributeSpellingListIndex()));
3701}
3702
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003703static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3704 const AttributeList &Attr) {
3705 IdentifierInfo *RelatedClass =
3706 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0;
3707 if (!RelatedClass) {
3708 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3709 return;
3710 }
3711 IdentifierInfo *ClassMethod =
3712 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0;
3713 IdentifierInfo *InstanceMethod =
3714 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0;
3715 D->addAttr(::new (S.Context)
3716 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3717 ClassMethod, InstanceMethod,
3718 Attr.getAttributeSpellingListIndex()));
3719}
3720
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003721static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3722 const AttributeList &Attr) {
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003723 ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003724 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003725 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003726 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3727 Attr.getAttributeSpellingListIndex()));
3728}
3729
Chandler Carruthedc2c642011-07-02 00:01:44 +00003730static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3731 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003732 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003733
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003734 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003735 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003736}
3737
Chandler Carruthedc2c642011-07-02 00:01:44 +00003738static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3739 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003740 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003741 QualType type = vd->getType();
3742
3743 if (!type->isDependentType() &&
3744 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003745 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003746 << type;
3747 return;
3748 }
3749
3750 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3751
3752 // If we have no lifetime yet, check the lifetime we're presumably
3753 // going to infer.
3754 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3755 lifetime = type->getObjCARCImplicitLifetime();
3756
3757 switch (lifetime) {
3758 case Qualifiers::OCL_None:
3759 assert(type->isDependentType() &&
3760 "didn't infer lifetime for non-dependent type?");
3761 break;
3762
3763 case Qualifiers::OCL_Weak: // meaningful
3764 case Qualifiers::OCL_Strong: // meaningful
3765 break;
3766
3767 case Qualifiers::OCL_ExplicitNone:
3768 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003769 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003770 << (lifetime == Qualifiers::OCL_Autoreleasing);
3771 break;
3772 }
3773
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003774 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003775 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3776 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003777}
3778
Francois Picheta83957a2010-12-19 06:50:37 +00003779//===----------------------------------------------------------------------===//
3780// Microsoft specific attribute handlers.
3781//===----------------------------------------------------------------------===//
3782
Chandler Carruthedc2c642011-07-02 00:01:44 +00003783static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003784 if (!S.LangOpts.CPlusPlus) {
3785 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3786 << Attr.getName() << AttributeLangSupport::C;
3787 return;
3788 }
3789
Aaron Ballman60e705e2013-11-24 20:58:02 +00003790 if (!isa<CXXRecordDecl>(D)) {
3791 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3792 << Attr.getName() << ExpectedClass;
3793 return;
3794 }
3795
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003796 StringRef StrRef;
3797 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003798 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003799 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003800
David Majnemer89085342013-08-09 08:56:20 +00003801 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3802 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003803 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3804 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003805
Reid Kleckner140c4a72013-05-17 14:04:52 +00003806 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003807 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003808 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003809 return;
3810 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003811
David Majnemer89085342013-08-09 08:56:20 +00003812 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003813 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003814 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003815 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003816 return;
3817 }
David Majnemer89085342013-08-09 08:56:20 +00003818 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003819 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003820 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003821 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003822 }
Francois Picheta83957a2010-12-19 06:50:37 +00003823
David Majnemer89085342013-08-09 08:56:20 +00003824 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3825 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003826}
3827
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003828/// Handles semantic checking for features that are common to all attributes,
3829/// such as checking whether a parameter was properly specified, or the correct
3830/// number of arguments were passed, etc.
3831static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
3832 const AttributeList &Attr) {
3833 // Several attributes carry different semantics than the parsing requires, so
3834 // those are opted out of the common handling.
3835 //
3836 // We also bail on unknown and ignored attributes because those are handled
3837 // as part of the target-specific handling logic.
3838 if (Attr.hasCustomParsing() ||
3839 Attr.getKind() == AttributeList::UnknownAttribute ||
3840 Attr.getKind() == AttributeList::IgnoredAttribute)
3841 return false;
3842
Aaron Ballman3aff6332013-12-02 19:30:36 +00003843 // Check whether the attribute requires specific language extensions to be
3844 // enabled.
3845 if (!Attr.diagnoseLangOpts(S))
3846 return true;
3847
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003848 // If there are no optional arguments, then checking for the argument count
3849 // is trivial.
3850 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
3851 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
3852 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003853
3854 // Check whether the attribute appertains to the given subject.
3855 if (!Attr.diagnoseAppertainsTo(S, D))
3856 return true;
3857
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003858 return false;
3859}
3860
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003861//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003862// Top Level Sema Entry Points
3863//===----------------------------------------------------------------------===//
3864
Richard Smithf8a75c32013-08-29 00:47:48 +00003865/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3866/// the attribute applies to decls. If the attribute is a type attribute, just
3867/// silently ignore it if a GNU attribute.
3868static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3869 const AttributeList &Attr,
3870 bool IncludeCXX11Attributes) {
3871 if (Attr.isInvalid())
3872 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003873
Richard Smithf8a75c32013-08-29 00:47:48 +00003874 // Ignore C++11 attributes on declarator chunks: they appertain to the type
3875 // instead.
3876 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
3877 return;
3878
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003879 if (handleCommonAttributeFeatures(S, scope, D, Attr))
3880 return;
3881
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003882 switch (Attr.getKind()) {
Aaron Ballman9beb5172013-12-02 15:13:14 +00003883 case AttributeList::AT_IBAction:
3884 handleSimpleAttribute<IBActionAttr>(S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003885 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
3886 case AttributeList::AT_IBOutletCollection:
3887 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003888 case AttributeList::AT_AddressSpace:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003889 case AttributeList::AT_ObjCGC:
3890 case AttributeList::AT_VectorSize:
3891 case AttributeList::AT_NeonVectorType:
3892 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballman317a77f2013-05-22 23:25:32 +00003893 case AttributeList::AT_Ptr32:
3894 case AttributeList::AT_Ptr64:
3895 case AttributeList::AT_SPtr:
3896 case AttributeList::AT_UPtr:
Mike Stumpd3bb5572009-07-24 19:02:52 +00003897 // Ignore these, these are type attributes, handled by
3898 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003899 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003900 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
3901 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
3902 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
3903 case AttributeList::AT_AlwaysInline:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003904 handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003905 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003906 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00003907 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003908 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
3909 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
3910 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00003911 handleDependencyAttr(S, scope, D, Attr);
3912 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003913 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003914 case AttributeList::AT_CUDAConstant:
3915 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003916 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003917 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman3a8e2d92013-11-27 18:53:58 +00003918 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003919 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00003920 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00003921 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003922 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
3923 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003924 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003925 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00003926 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003927 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00003928 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003929 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
3930 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
3931 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00003932 case AttributeList::AT_CUDADevice:
3933 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003934 case AttributeList::AT_CUDAHost:
3935 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003936 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
3937 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003938 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00003939 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003940 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003941 case AttributeList::AT_MayAlias:
3942 handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00003943 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003944 case AttributeList::AT_NoCommon:
3945 handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003946 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003947 case AttributeList::AT_Overloadable:
3948 handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break;
Richard Smith852e9ce2013-11-27 01:46:48 +00003949 case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003950 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
3951 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003952 case AttributeList::AT_Naked:
3953 handleSimpleAttribute<NakedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003954 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
3955 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003956 case AttributeList::AT_CUDAShared:
3957 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003958 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003959
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003960 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003961 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003962 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003963 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003964
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003965 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00003966 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3967
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003968 case AttributeList::AT_ObjCRequiresSuper:
3969 handleObjCRequiresSuperAttr(S, D, Attr); break;
3970
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003971 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00003972 handleNSBridgedAttr(S, scope, D, Attr); break;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003973
3974 case AttributeList::AT_ObjCBridge:
3975 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003976
3977 case AttributeList::AT_ObjCBridgeMutable:
3978 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003979
3980 case AttributeList::AT_ObjCBridgeRelated:
3981 handleObjCBridgeRelatedAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00003982
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003983 case AttributeList::AT_ObjCDesignatedInitializer:
3984 handleObjCDesignatedInitializer(S, D, Attr); break;
3985
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003986 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00003987 handleCFAuditedTransferAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003988 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00003989 handleCFUnknownTransferAttr(S, D, Attr); break;
John McCall32f5fe12011-09-30 05:12:12 +00003990
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003991 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003992 case AttributeList::AT_CFConsumed:
3993 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
3994 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003995 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00003996
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003997 case AttributeList::AT_NSReturnsAutoreleased:
3998 case AttributeList::AT_NSReturnsNotRetained:
3999 case AttributeList::AT_CFReturnsNotRetained:
4000 case AttributeList::AT_NSReturnsRetained:
4001 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004002 handleNSReturnsRetainedAttr(S, D, Attr); break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004003 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00004004 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004005 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00004006 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); break;
Joey Goulyaba589c2013-03-08 09:42:32 +00004007 case AttributeList::AT_VecTypeHint:
4008 handleVecTypeHint(S, D, Attr); break;
4009
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004010 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004011 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004012
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004013 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4014 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4015 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004016 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004017 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004018 case AttributeList::AT_ArcWeakrefUnavailable:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004019 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004020 case AttributeList::AT_ObjCRootClass:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004021 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00004022 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek28eace62013-11-23 01:01:34 +00004023 handleObjCSuppresProtocolAttr(S, D, Attr);
4024 break;
4025 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004026 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004027 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4028 case AttributeList::AT_ReturnsTwice:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004029 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004030 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004031 case AttributeList::AT_Visibility:
4032 handleVisibilityAttr(S, D, Attr, false);
4033 break;
4034 case AttributeList::AT_TypeVisibility:
4035 handleVisibilityAttr(S, D, Attr, true);
4036 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004037 case AttributeList::AT_WarnUnused:
Aaron Ballman6a42b5a2013-11-27 16:59:17 +00004038 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004039 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004040 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00004041 case AttributeList::AT_Weak:
4042 handleSimpleAttribute<WeakAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004043 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4044 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4045 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004046 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004047 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004048 case AttributeList::AT_ObjCException:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004049 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004050 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004051 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004052 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004053 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4054 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4055 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4056 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004057 case AttributeList::AT_Pure:
4058 handleSimpleAttribute<PureAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004059 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4060 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004061 case AttributeList::AT_NoInline:
4062 handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004063 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004064 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004065 // Just ignore
4066 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004067 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004068 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004069 case AttributeList::AT_StdCall:
4070 case AttributeList::AT_CDecl:
4071 case AttributeList::AT_FastCall:
4072 case AttributeList::AT_ThisCall:
4073 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004074 case AttributeList::AT_MSABI:
4075 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004076 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004077 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004078 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004079 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004080 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004081 case AttributeList::AT_OpenCLKernel:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004082 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004083 case AttributeList::AT_OpenCLImageAccess:
4084 handleOpenCLImageAccessAttr(S, D, Attr);
4085 break;
John McCall8d32c052012-05-22 21:28:12 +00004086
4087 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004088 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004089 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00004090 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004091 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004092 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004093 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004094 case AttributeList::AT_MSInheritance:
4095 handleSimpleAttribute<MSInheritanceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004096 case AttributeList::AT_ForceInline:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004097 handleSimpleAttribute<ForceInlineAttr>(S, D, Attr); break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004098 case AttributeList::AT_SelectAny:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004099 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004100
4101 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004102 case AttributeList::AT_AssertExclusiveLock:
4103 handleAssertExclusiveLockAttr(S, D, Attr);
4104 break;
4105 case AttributeList::AT_AssertSharedLock:
4106 handleAssertSharedLockAttr(S, D, Attr);
4107 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004108 case AttributeList::AT_GuardedVar:
Aaron Ballmane61b8b82013-12-02 15:02:49 +00004109 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004110 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004111 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004112 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004113 case AttributeList::AT_ScopedLockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004114 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004115 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004116 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004117 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004118 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004119 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004120 break;
4121 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004122 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004123 break;
4124 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004125 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004126 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004127 case AttributeList::AT_Lockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004128 handleSimpleAttribute<LockableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004129 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004130 handleGuardedByAttr(S, D, Attr);
4131 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004132 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004133 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004134 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004135 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004136 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004137 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004138 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004139 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004140 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004141 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004142 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004143 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004144 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004145 handleLockReturnedAttr(S, D, Attr);
4146 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004147 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004148 handleLocksExcludedAttr(S, D, Attr);
4149 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004150 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004151 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004152 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004153 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004154 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004155 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004156 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004157 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004158 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004159 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004160 handleUnlockFunAttr(S, D, Attr);
4161 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004162 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004163 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004164 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004165 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004166 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004167 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004168
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004169 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004170 case AttributeList::AT_Consumable:
4171 handleConsumableAttr(S, D, Attr);
4172 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004173 case AttributeList::AT_CallableWhen:
4174 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004175 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004176 case AttributeList::AT_ParamTypestate:
4177 handleParamTypestateAttr(S, D, Attr);
4178 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004179 case AttributeList::AT_ReturnTypestate:
4180 handleReturnTypestateAttr(S, D, Attr);
4181 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004182 case AttributeList::AT_SetTypestate:
4183 handleSetTypestateAttr(S, D, Attr);
4184 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004185 case AttributeList::AT_TestTypestate:
4186 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004187 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004188
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004189 // Type safety attributes.
4190 case AttributeList::AT_ArgumentWithTypeTag:
4191 handleArgumentWithTypeTagAttr(S, D, Attr);
4192 break;
4193 case AttributeList::AT_TypeTagForDatatype:
4194 handleTypeTagForDatatypeAttr(S, D, Attr);
4195 break;
4196
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004197 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004198 // Ask target about the attribute.
4199 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4200 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004201 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4202 diag::warn_unhandled_ms_attribute_ignored :
4203 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004204 break;
4205 }
4206}
4207
4208/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4209/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004210void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004211 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004212 bool IncludeCXX11Attributes) {
4213 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004214 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004215
Joey Gouly2cd9db12013-12-13 16:15:28 +00004216 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004217 // GCC accepts
4218 // static int a9 __attribute__((weakref));
4219 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004220 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004221 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00004222 cast<NamedDecl>(D)->getNameAsString();
4223 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004224 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004225 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004226
4227 if (!D->hasAttr<OpenCLKernelAttr>()) {
4228 // These attributes cannot be applied to a non-kernel function.
4229 if (D->hasAttr<ReqdWorkGroupSizeAttr>()) {
4230 Diag(D->getLocation(), diag::err_opencl_kernel_attr)
4231 << "reqd_work_group_size";
4232 D->setInvalidDecl();
4233 }
4234 if (D->hasAttr<WorkGroupSizeHintAttr>()) {
4235 Diag(D->getLocation(), diag::err_opencl_kernel_attr)
4236 << "work_group_size_hint";
4237 D->setInvalidDecl();
4238 }
4239 if (D->hasAttr<VecTypeHintAttr>()) {
4240 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << "vec_type_hint";
4241 D->setInvalidDecl();
4242 }
4243 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004244}
4245
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004246// Annotation attributes are the only attributes allowed after an access
4247// specifier.
4248bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4249 const AttributeList *AttrList) {
4250 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004251 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004252 handleAnnotateAttr(*this, ASDecl, *l);
4253 } else {
4254 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4255 return true;
4256 }
4257 }
4258
4259 return false;
4260}
4261
John McCall42856de2011-10-01 05:17:03 +00004262/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4263/// contains any decl attributes that we should warn about.
4264static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4265 for ( ; A; A = A->getNext()) {
4266 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004267 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004268 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4269
4270 if (A->getKind() == AttributeList::UnknownAttribute) {
4271 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4272 << A->getName() << A->getRange();
4273 } else {
4274 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4275 << A->getName() << A->getRange();
4276 }
4277 }
4278}
4279
4280/// checkUnusedDeclAttributes - Given a declarator which is not being
4281/// used to build a declaration, complain about any decl attributes
4282/// which might be lying around on it.
4283void Sema::checkUnusedDeclAttributes(Declarator &D) {
4284 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4285 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4286 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4287 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4288}
4289
Ryan Flynn7d470f32009-07-30 03:15:39 +00004290/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004291/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004292NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4293 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004294 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004295 NamedDecl *NewD = 0;
4296 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004297 FunctionDecl *NewFD;
4298 // FIXME: Missing call to CheckFunctionDeclaration().
4299 // FIXME: Mangling?
4300 // FIXME: Is the qualifier info correct?
4301 // FIXME: Is the DeclContext correct?
4302 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4303 Loc, Loc, DeclarationName(II),
4304 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004305 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004306 FD->hasPrototype(),
4307 false/*isConstexprSpecified*/);
4308 NewD = NewFD;
4309
4310 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004311 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004312
4313 // Fake up parameter variables; they are declared as if this were
4314 // a typedef.
4315 QualType FDTy = FD->getType();
4316 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4317 SmallVector<ParmVarDecl*, 16> Params;
4318 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4319 AE = FT->arg_type_end(); AI != AE; ++AI) {
4320 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4321 Param->setScopeInfo(0, Params.size());
4322 Params.push_back(Param);
4323 }
David Blaikie9c70e042011-09-21 18:16:56 +00004324 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004325 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004326 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4327 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004328 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004329 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004330 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004331 if (VD->getQualifier()) {
4332 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004333 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004334 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004335 }
4336 return NewD;
4337}
4338
James Dennett634962f2012-06-14 21:40:34 +00004339/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004340/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004341void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004342 if (W.getUsed()) return; // only do this once
4343 W.setUsed(true);
4344 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4345 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004346 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004347 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4348 NDId->getName()));
4349 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00004350 WeakTopLevelDecl.push_back(NewD);
4351 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4352 // to insert Decl at TU scope, sorry.
4353 DeclContext *SavedContext = CurContext;
4354 CurContext = Context.getTranslationUnitDecl();
4355 PushOnScopeChains(NewD, S);
4356 CurContext = SavedContext;
4357 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004358 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004359 }
4360}
4361
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004362void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4363 // It's valid to "forward-declare" #pragma weak, in which case we
4364 // have to do this.
4365 LoadExternalWeakUndeclaredIdentifiers();
4366 if (!WeakUndeclaredIdentifiers.empty()) {
4367 NamedDecl *ND = NULL;
4368 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4369 if (VD->isExternC())
4370 ND = VD;
4371 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4372 if (FD->isExternC())
4373 ND = FD;
4374 if (ND) {
4375 if (IdentifierInfo *Id = ND->getIdentifier()) {
4376 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4377 = WeakUndeclaredIdentifiers.find(Id);
4378 if (I != WeakUndeclaredIdentifiers.end()) {
4379 WeakInfo W = I->second;
4380 DeclApplyPragmaWeak(S, ND, W);
4381 WeakUndeclaredIdentifiers[Id] = W;
4382 }
4383 }
4384 }
4385 }
4386}
4387
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004388/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4389/// it, apply them to D. This is a bit tricky because PD can have attributes
4390/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004391void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004392 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004393 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004394 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004395
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004396 // Walk the declarator structure, applying decl attributes that were in a type
4397 // position to the decl itself. This handles cases like:
4398 // int *__attr__(x)** D;
4399 // when X is a decl attribute.
4400 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4401 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004402 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004403
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004404 // Finally, apply any attributes on the decl itself.
4405 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004406 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004407}
John McCall28a6aea2009-11-04 02:18:39 +00004408
John McCall31168b02011-06-15 23:02:42 +00004409/// Is the given declaration allowed to use a forbidden type?
4410static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4411 // Private ivars are always okay. Unfortunately, people don't
4412 // always properly make their ivars private, even in system headers.
4413 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004414 // Function declarations in sys headers will be marked unavailable.
4415 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4416 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004417 return false;
4418
4419 // Require it to be declared in a system header.
4420 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4421}
4422
4423/// Handle a delayed forbidden-type diagnostic.
4424static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4425 Decl *decl) {
4426 if (decl && isForbiddenTypeAllowed(S, decl)) {
4427 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4428 "this system declaration uses an unsupported type"));
4429 return;
4430 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004431 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004432 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004433 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004434 // kind of forbidden type messages on unavailable functions.
4435 if (FD->hasAttr<UnavailableAttr>() &&
4436 diag.getForbiddenTypeDiagnostic() ==
4437 diag::err_arc_array_param_no_ownership) {
4438 diag.Triggered = true;
4439 return;
4440 }
4441 }
John McCall31168b02011-06-15 23:02:42 +00004442
4443 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4444 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4445 diag.Triggered = true;
4446}
4447
John McCall2ec85372012-05-07 06:16:41 +00004448void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4449 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004450 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004451 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004452
John McCall2ec85372012-05-07 06:16:41 +00004453 // When delaying diagnostics to run in the context of a parsed
4454 // declaration, we only want to actually emit anything if parsing
4455 // succeeds.
4456 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004457
John McCall2ec85372012-05-07 06:16:41 +00004458 // We emit all the active diagnostics in this pool or any of its
4459 // parents. In general, we'll get one pool for the decl spec
4460 // and a child pool for each declarator; in a decl group like:
4461 // deprecated_typedef foo, *bar, baz();
4462 // only the declarator pops will be passed decls. This is correct;
4463 // we really do need to consider delayed diagnostics from the decl spec
4464 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004465 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004466 do {
John McCall6347b682012-05-07 06:16:58 +00004467 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004468 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4469 // This const_cast is a bit lame. Really, Triggered should be mutable.
4470 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004471 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004472 continue;
4473
John McCallc1465822011-02-14 07:13:47 +00004474 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004475 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00004476 case DelayedDiagnostic::Unavailable:
4477 // Don't bother giving deprecation/unavailable diagnostics if
4478 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00004479 if (!decl->isInvalidDecl())
Ted Kremenekb79ee572013-12-18 23:30:06 +00004480 HandleDelayedAvailabilityCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004481 break;
4482
4483 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004484 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004485 break;
John McCall31168b02011-06-15 23:02:42 +00004486
4487 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004488 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004489 break;
John McCall86121512010-01-27 03:50:35 +00004490 }
4491 }
John McCall2ec85372012-05-07 06:16:41 +00004492 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004493}
4494
John McCall6347b682012-05-07 06:16:58 +00004495/// Given a set of delayed diagnostics, re-emit them as if they had
4496/// been delayed in the current context instead of in the given pool.
4497/// Essentially, this just moves them to the current pool.
4498void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4499 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4500 assert(curPool && "re-emitting in undelayed context not supported");
4501 curPool->steal(pool);
4502}
4503
John McCall28a6aea2009-11-04 02:18:39 +00004504static bool isDeclDeprecated(Decl *D) {
4505 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004506 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004507 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004508 // A category implicitly has the availability of the interface.
4509 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4510 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004511 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4512 return false;
4513}
4514
Ted Kremenekb79ee572013-12-18 23:30:06 +00004515static bool isDeclUnavailable(Decl *D) {
4516 do {
4517 if (D->isUnavailable())
4518 return true;
4519 // A category implicitly has the availability of the interface.
4520 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4521 return CatD->getClassInterface()->isUnavailable();
4522 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4523 return false;
4524}
4525
Eli Friedman971bfa12012-08-08 21:52:41 +00004526static void
Ted Kremenekb79ee572013-12-18 23:30:06 +00004527DoEmitAvailabilityWarning(Sema &S,
4528 DelayedDiagnostic::DDKind K,
4529 Decl *Ctx,
4530 const NamedDecl *D,
4531 StringRef Message,
4532 SourceLocation Loc,
4533 const ObjCInterfaceDecl *UnknownObjCClass,
4534 const ObjCPropertyDecl *ObjCProperty) {
4535
4536 // Diagnostics for deprecated or unavailable.
4537 unsigned diag, diag_message, diag_fwdclass_message;
4538
4539 // Matches 'diag::note_property_attribute' options.
4540 unsigned property_note_select;
4541
4542 // Matches diag::note_availability_specified_here.
4543 unsigned available_here_select_kind;
4544
4545 // Don't warn if our current context is deprecated or unavailable.
4546 switch (K) {
4547 case DelayedDiagnostic::Deprecation:
4548 if (isDeclDeprecated(Ctx))
4549 return;
4550 diag = diag::warn_deprecated;
4551 diag_message = diag::warn_deprecated_message;
4552 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
4553 property_note_select = /* deprecated */ 0;
4554 available_here_select_kind = /* deprecated */ 2;
4555 break;
4556
4557 case DelayedDiagnostic::Unavailable:
4558 if (isDeclUnavailable(Ctx))
4559 return;
4560 diag = diag::err_unavailable;
4561 diag_message = diag::err_unavailable_message;
4562 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
4563 property_note_select = /* unavailable */ 1;
4564 available_here_select_kind = /* unavailable */ 0;
4565 break;
4566
4567 default:
4568 llvm_unreachable("Neither a deprecation or unavailable kind");
4569 }
4570
Eli Friedman971bfa12012-08-08 21:52:41 +00004571 DeclarationName Name = D->getDeclName();
4572 if (!Message.empty()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004573 S.Diag(Loc, diag_message) << Name << Message;
4574// S.Diag(D->getLocation(), diag::note_availability_specified_here)
4575// << D << available_here_select_kind;
4576 if (ObjCProperty)
4577 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4578 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004579 } else if (!UnknownObjCClass) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004580 S.Diag(Loc, diag) << Name;
4581// S.Diag(D->getLocation(), diag::note_availability_specified_here)
4582// << D << available_here_select_kind;
4583 if (ObjCProperty)
4584 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4585 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004586 } else {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004587 S.Diag(Loc, diag_fwdclass_message) << Name;
Eli Friedman971bfa12012-08-08 21:52:41 +00004588 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4589 }
Ted Kremenekb79ee572013-12-18 23:30:06 +00004590
4591 S.Diag(D->getLocation(), diag::note_availability_specified_here)
4592 << D << available_here_select_kind;
Eli Friedman971bfa12012-08-08 21:52:41 +00004593}
4594
Ted Kremenekb79ee572013-12-18 23:30:06 +00004595void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD,
4596 Decl *Ctx) {
John McCall86121512010-01-27 03:50:35 +00004597 DD.Triggered = true;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004598 DoEmitAvailabilityWarning(*this,
4599 (DelayedDiagnostic::DDKind) DD.Kind,
4600 Ctx,
4601 DD.getDeprecationDecl(),
4602 DD.getDeprecationMessage(),
4603 DD.Loc,
4604 DD.getUnknownObjCClass(),
4605 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004606}
4607
Ted Kremenekb79ee572013-12-18 23:30:06 +00004608void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
4609 NamedDecl *D, StringRef Message,
4610 SourceLocation Loc,
4611 const ObjCInterfaceDecl *UnknownObjCClass,
4612 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004613 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004614 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004615 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D,
4616 UnknownObjCClass,
4617 ObjCProperty,
4618 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004619 return;
4620 }
4621
Ted Kremenekb79ee572013-12-18 23:30:06 +00004622 Decl *Ctx = cast<Decl>(getCurLexicalContext());
4623 DelayedDiagnostic::DDKind K;
4624 switch (AD) {
4625 case AD_Deprecation:
4626 K = DelayedDiagnostic::Deprecation;
4627 break;
4628 case AD_Unavailable:
4629 K = DelayedDiagnostic::Unavailable;
4630 break;
4631 }
4632
4633 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
4634 UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004635}