blob: bf61f5b986a01d2dff5347d278079ada697347ee [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) {
Aaron Ballman553e6812013-12-26 14:54:11 +0000382 const ValueDecl *vd = cast<ValueDecl>(D);
383 QualType QT = vd->getType();
384 if (QT->isAnyPointerType())
385 return true;
386
387 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())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000392 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000393
Aaron Ballman553e6812013-12-26 14:54:11 +0000394 if (threadSafetyCheckIsSmartPointer(S, RT))
395 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000396 }
Aaron Ballman553e6812013-12-26 14:54:11 +0000397
398 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Aaron Ballman68289452013-12-26 15:06:01 +0000399 << Attr.getName() << QT;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000400 return false;
401}
402
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000403/// \brief Checks that the passed in QualType either is of RecordType or points
404/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000405static const RecordType *getRecordType(QualType QT) {
406 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000407 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000408
409 // Now check if we point to record type.
410 if (const PointerType *PT = QT->getAs<PointerType>())
411 return PT->getPointeeType()->getAs<RecordType>();
412
413 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000414}
415
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000416
Jordy Rose740b0c22012-05-08 03:27:22 +0000417static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
418 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000419 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
Aaron Ballman9ead1242013-12-19 02:39:40 +0000420 return RT->getDecl()->hasAttr<LockableAttr>();
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000421}
422
423
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000424/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000425/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000426static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
427 QualType Ty) {
428 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000429
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000430 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000431 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000432 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000433 << Attr.getName() << Ty.getAsString();
434 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000435 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000436
Michael Hana9171bc2012-08-03 17:40:43 +0000437 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000438 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000439 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000440
441 // Allow smart pointers to be used as lockable objects.
442 // FIXME -- Check the type that the smart pointer points to.
443 if (threadSafetyCheckIsSmartPointer(S, RT))
444 return;
445
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000446 // Check if the type is lockable.
447 RecordDecl *RD = RT->getDecl();
Aaron Ballman9ead1242013-12-19 02:39:40 +0000448 if (RD->hasAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000449 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000450
451 // Else check if any base classes are lockable.
452 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
453 CXXBasePaths BPaths(false, false);
454 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
455 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000456 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000457
458 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
459 << Attr.getName() << Ty.getAsString();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000460}
461
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000462/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000463/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000464/// \param Sidx The attribute argument index to start checking with.
465/// \param ParamIdxOk Whether an argument can be indexing into a function
466/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000467static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000468 const AttributeList &Attr,
469 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000470 int Sidx = 0,
471 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000472 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000473 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000474
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000475 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000476 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000477 Args.push_back(ArgExp);
478 continue;
479 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000480
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000481 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000482 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000483 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000484 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000485 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000486 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000487 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000488 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000489
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000490 // We allow constant strings to be used as a placeholder for expressions
491 // that are not valid C++ syntax, but warn that they are ignored.
492 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
493 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000494 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000495 continue;
496 }
497
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000498 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000499
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000500 // A pointer to member expression of the form &MyClass::mu is treated
501 // specially -- we need to look at the type of the member.
502 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
503 if (UOp->getOpcode() == UO_AddrOf)
504 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
505 if (DRE->getDecl()->isCXXInstanceMember())
506 ArgTy = DRE->getDecl()->getType();
507
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000508 // First see if we can just cast to record type, or point to record type.
509 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000510
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000511 // Now check if we index into a record type function param.
512 if(!RT && ParamIdxOk) {
513 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000514 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
515 if(FD && IL) {
516 unsigned int NumParams = FD->getNumParams();
517 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000518 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
519 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
520 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000521 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
522 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000523 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000524 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000525 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000526 }
527 }
528
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000529 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000530
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000531 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000532 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000533}
534
Chris Lattner58418ff2008-06-29 00:16:31 +0000535//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000536// Attribute Implementations
537//===----------------------------------------------------------------------===//
538
Daniel Dunbar032db472008-07-31 22:40:48 +0000539// FIXME: All this manual attribute parsing code is gross. At the
540// least add some helper functions to check most argument patterns (#
541// and types of args).
542
Michael Hana9171bc2012-08-03 17:40:43 +0000543static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000544 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000545 if (!threadSafetyCheckIsPointer(S, D, Attr))
546 return;
547
Michael Han99315932013-01-24 16:46:58 +0000548 D->addAttr(::new (S.Context)
549 PtGuardedVarAttr(Attr.getRange(), S.Context,
550 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000551}
552
Michael Hana9171bc2012-08-03 17:40:43 +0000553static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
554 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000555 Expr* &Arg) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000556 SmallVector<Expr*, 1> Args;
557 // check that all arguments are lockable objects
558 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
559 unsigned Size = Args.size();
560 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000561 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000562
Michael Han3be3b442012-07-23 18:48:41 +0000563 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000564
Michael Han3be3b442012-07-23 18:48:41 +0000565 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000566}
567
Michael Han3be3b442012-07-23 18:48:41 +0000568static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
569 Expr *Arg = 0;
570 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
571 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000572
Michael Han3be3b442012-07-23 18:48:41 +0000573 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
574}
575
Michael Hana9171bc2012-08-03 17:40:43 +0000576static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000577 const AttributeList &Attr) {
578 Expr *Arg = 0;
579 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
580 return;
581
582 if (!threadSafetyCheckIsPointer(S, D, Attr))
583 return;
584
585 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
586 S.Context, Arg));
587}
588
Michael Hana9171bc2012-08-03 17:40:43 +0000589static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
590 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000591 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000592 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000593 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000594
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000595 // Check that this attribute only applies to lockable types.
Aaron Ballmane61b8b82013-12-02 15:02:49 +0000596 QualType QT = cast<ValueDecl>(D)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000597 if (!QT->isDependentType()) {
598 const RecordType *RT = getRecordType(QT);
Aaron Ballman9ead1242013-12-19 02:39:40 +0000599 if (!RT || !RT->getDecl()->hasAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000600 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000601 << Attr.getName();
602 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000603 }
604 }
605
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000606 // Check that all arguments are lockable objects.
607 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000608 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000609 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000610
Michael Han3be3b442012-07-23 18:48:41 +0000611 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000612}
613
Michael Hana9171bc2012-08-03 17:40:43 +0000614static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000615 const AttributeList &Attr) {
616 SmallVector<Expr*, 1> Args;
617 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
618 return;
619
620 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000621 D->addAttr(::new (S.Context)
622 AcquiredAfterAttr(Attr.getRange(), S.Context,
623 StartArg, Args.size(),
624 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000625}
626
Michael Hana9171bc2012-08-03 17:40:43 +0000627static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000628 const AttributeList &Attr) {
629 SmallVector<Expr*, 1> Args;
630 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
631 return;
632
633 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000634 D->addAttr(::new (S.Context)
635 AcquiredBeforeAttr(Attr.getRange(), S.Context,
636 StartArg, Args.size(),
637 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000638}
639
Michael Hana9171bc2012-08-03 17:40:43 +0000640static bool checkLockFunAttrCommon(Sema &S, Decl *D,
641 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000642 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000643 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000644 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000645 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000646
Michael Han3be3b442012-07-23 18:48:41 +0000647 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000648}
649
Michael Hana9171bc2012-08-03 17:40:43 +0000650static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000651 const AttributeList &Attr) {
652 SmallVector<Expr*, 1> Args;
653 if (!checkLockFunAttrCommon(S, D, Attr, Args))
654 return;
655
656 unsigned Size = Args.size();
657 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000658 D->addAttr(::new (S.Context)
659 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
660 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000661}
662
Michael Hana9171bc2012-08-03 17:40:43 +0000663static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000664 const AttributeList &Attr) {
665 SmallVector<Expr*, 1> Args;
666 if (!checkLockFunAttrCommon(S, D, Attr, Args))
667 return;
668
669 unsigned Size = Args.size();
670 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000671 D->addAttr(::new (S.Context)
672 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
673 StartArg, Size,
674 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000675}
676
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000677static void handleAssertSharedLockAttr(Sema &S, Decl *D,
678 const AttributeList &Attr) {
679 SmallVector<Expr*, 1> Args;
680 if (!checkLockFunAttrCommon(S, D, Attr, Args))
681 return;
682
683 unsigned Size = Args.size();
684 Expr **StartArg = Size == 0 ? 0 : &Args[0];
685 D->addAttr(::new (S.Context)
686 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
687 Attr.getAttributeSpellingListIndex()));
688}
689
690static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
691 const AttributeList &Attr) {
692 SmallVector<Expr*, 1> Args;
693 if (!checkLockFunAttrCommon(S, D, Attr, Args))
694 return;
695
696 unsigned Size = Args.size();
697 Expr **StartArg = Size == 0 ? 0 : &Args[0];
698 D->addAttr(::new (S.Context)
699 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
700 StartArg, Size,
701 Attr.getAttributeSpellingListIndex()));
702}
703
704
Michael Hana9171bc2012-08-03 17:40:43 +0000705static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
706 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000707 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000708 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000709 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000710
Aaron Ballman00e99962013-08-31 01:11:41 +0000711 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000712 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000713 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000714 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000715 }
716
717 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000718 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000719
Michael Han3be3b442012-07-23 18:48:41 +0000720 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000721}
722
Michael Hana9171bc2012-08-03 17:40:43 +0000723static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000724 const AttributeList &Attr) {
725 SmallVector<Expr*, 2> Args;
726 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
727 return;
728
Michael Han99315932013-01-24 16:46:58 +0000729 D->addAttr(::new (S.Context)
730 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000731 Attr.getArgAsExpr(0),
732 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000733 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000734}
735
Michael Hana9171bc2012-08-03 17:40:43 +0000736static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000737 const AttributeList &Attr) {
738 SmallVector<Expr*, 2> Args;
739 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
740 return;
741
Michael Han99315932013-01-24 16:46:58 +0000742 D->addAttr(::new (S.Context)
743 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000744 Attr.getArgAsExpr(0),
745 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000746 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000747}
748
Michael Hana9171bc2012-08-03 17:40:43 +0000749static bool checkLocksRequiredCommon(Sema &S, Decl *D,
750 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000751 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000752 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000753 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000754
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000755 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000756 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000757 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000758 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000759
Michael Han3be3b442012-07-23 18:48:41 +0000760 return true;
761}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000762
Michael Hana9171bc2012-08-03 17:40:43 +0000763static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000764 const AttributeList &Attr) {
765 SmallVector<Expr*, 1> Args;
766 if (!checkLocksRequiredCommon(S, D, Attr, Args))
767 return;
768
769 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000770 D->addAttr(::new (S.Context)
771 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
772 StartArg, Args.size(),
773 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000774}
775
Michael Hana9171bc2012-08-03 17:40:43 +0000776static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000777 const AttributeList &Attr) {
778 SmallVector<Expr*, 1> Args;
779 if (!checkLocksRequiredCommon(S, D, Attr, Args))
780 return;
781
782 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000783 D->addAttr(::new (S.Context)
784 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
785 StartArg, Args.size(),
786 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000787}
788
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000789static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000790 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000791 // zero or more arguments ok
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000792 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000793 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000794 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000795 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000796 Expr **StartArg = Size == 0 ? 0 : &Args[0];
797
Michael Han99315932013-01-24 16:46:58 +0000798 D->addAttr(::new (S.Context)
799 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
800 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000801}
802
803static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000804 const AttributeList &Attr) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000805 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000806 SmallVector<Expr*, 1> Args;
807 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
808 unsigned Size = Args.size();
809 if (Size == 0)
810 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000811
Michael Han99315932013-01-24 16:46:58 +0000812 D->addAttr(::new (S.Context)
813 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
814 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000815}
816
817static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000818 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000819 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000820 return;
821
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000822 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000823 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000824 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000825 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000826 if (Size == 0)
827 return;
828 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000829
Michael Han99315932013-01-24 16:46:58 +0000830 D->addAttr(::new (S.Context)
831 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
832 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000833}
834
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000835static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +0000836 ConsumableAttr::ConsumedState DefaultState;
837
838 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000839 IdentifierLoc *IL = Attr.getArgAsIdent(0);
840 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
841 DefaultState)) {
842 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
843 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +0000844 return;
845 }
David Blaikie16f76d22013-09-06 01:28:43 +0000846 } else {
847 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
848 << Attr.getName() << AANT_ArgumentIdentifier;
849 return;
850 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000851
852 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +0000853 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000854 Attr.getAttributeSpellingListIndex()));
855}
856
857static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
858 const AttributeList &Attr) {
859 ASTContext &CurrContext = S.getASTContext();
860 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
861
862 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
863 if (!RD->hasAttr<ConsumableAttr>()) {
864 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
865 RD->getNameAsString();
866
867 return false;
868 }
869 }
870
871 return true;
872}
873
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000874
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000875static void handleCallableWhenAttr(Sema &S, Decl *D,
876 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +0000877 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
878 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000879
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000880 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
881 return;
882
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000883 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
884 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
885 CallableWhenAttr::ConsumedState CallableState;
886
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000887 StringRef StateString;
888 SourceLocation Loc;
889 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
890 return;
891
892 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +0000893 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000894 S.Diag(Loc, diag::warn_attribute_type_not_supported)
895 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000896 return;
897 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +0000898
899 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000900 }
901
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000902 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000903 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
904 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000905}
906
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000907
DeLesley Hutchins69391772013-10-17 23:23:53 +0000908static void handleParamTypestateAttr(Sema &S, Decl *D,
909 const AttributeList &Attr) {
910 if (!checkAttributeNumArgs(S, Attr, 1)) return;
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000911
DeLesley Hutchins69391772013-10-17 23:23:53 +0000912 ParamTypestateAttr::ConsumedState ParamState;
913
914 if (Attr.isArgIdent(0)) {
915 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
916 StringRef StateString = Ident->Ident->getName();
917
918 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
919 ParamState)) {
920 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
921 << Attr.getName() << StateString;
922 return;
923 }
924 } else {
925 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
926 Attr.getName() << AANT_ArgumentIdentifier;
927 return;
928 }
929
930 // FIXME: This check is currently being done in the analysis. It can be
931 // enabled here only after the parser propagates attributes at
932 // template specialization definition, not declaration.
933 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
934 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
935 //
936 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
937 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
938 // ReturnType.getAsString();
939 // return;
940 //}
941
942 D->addAttr(::new (S.Context)
943 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
944 Attr.getAttributeSpellingListIndex()));
945}
946
947
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000948static void handleReturnTypestateAttr(Sema &S, Decl *D,
949 const AttributeList &Attr) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000950 if (!checkAttributeNumArgs(S, Attr, 1)) return;
951
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000952 ReturnTypestateAttr::ConsumedState ReturnState;
953
954 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +0000955 IdentifierLoc *IL = Attr.getArgAsIdent(0);
956 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
957 ReturnState)) {
958 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
959 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000960 return;
961 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000962 } else {
963 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
964 Attr.getName() << AANT_ArgumentIdentifier;
965 return;
966 }
967
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000968 // FIXME: This check is currently being done in the analysis. It can be
969 // enabled here only after the parser propagates attributes at
970 // template specialization definition, not declaration.
971 //QualType ReturnType;
972 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000973 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
974 // ReturnType = Param->getType();
975 //
976 //} else if (const CXXConstructorDecl *Constructor =
977 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000978 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
979 //
980 //} else {
981 //
982 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
983 //}
984 //
985 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
986 //
987 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
988 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
989 // ReturnType.getAsString();
990 // return;
991 //}
992
993 D->addAttr(::new (S.Context)
994 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
995 Attr.getAttributeSpellingListIndex()));
996}
997
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000998
999static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001000 if (!checkAttributeNumArgs(S, Attr, 1))
1001 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001002
1003 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1004 return;
1005
1006 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001007 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001008 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1009 StringRef Param = Ident->Ident->getName();
1010 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1011 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1012 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001013 return;
1014 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001015 } else {
1016 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1017 Attr.getName() << AANT_ArgumentIdentifier;
1018 return;
1019 }
1020
1021 D->addAttr(::new (S.Context)
1022 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1023 Attr.getAttributeSpellingListIndex()));
1024}
1025
Chris Wailes9385f9f2013-10-29 20:28:41 +00001026static void handleTestTypestateAttr(Sema &S, Decl *D,
1027 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001028 if (!checkAttributeNumArgs(S, Attr, 1))
1029 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001030
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001031 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1032 return;
1033
Chris Wailes9385f9f2013-10-29 20:28:41 +00001034 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001035 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001036 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1037 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001038 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001039 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1040 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001041 return;
1042 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001043 } else {
1044 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1045 Attr.getName() << AANT_ArgumentIdentifier;
1046 return;
1047 }
1048
1049 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001050 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001051 Attr.getAttributeSpellingListIndex()));
1052}
1053
Chandler Carruthedc2c642011-07-02 00:01:44 +00001054static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1055 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001056 // Remember this typedef decl, we will need it later for diagnostics.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001057 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001058}
1059
Chandler Carruthedc2c642011-07-02 00:01:44 +00001060static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001061 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001062 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001063 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001064 // If the alignment is less than or equal to 8 bits, the packed attribute
1065 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001066 if (!FD->getType()->isDependentType() &&
1067 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001068 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001069 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001070 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001071 else
Michael Han99315932013-01-24 16:46:58 +00001072 FD->addAttr(::new (S.Context)
1073 PackedAttr(Attr.getRange(), S.Context,
1074 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001075 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001076 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001077}
1078
Ted Kremenek7fd17232011-09-29 07:02:25 +00001079static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1080 // The IBOutlet/IBOutletCollection attributes only apply to instance
1081 // variables or properties of Objective-C classes. The outlet must also
1082 // have an object reference type.
1083 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1084 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001085 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001086 << Attr.getName() << VD->getType() << 0;
1087 return false;
1088 }
1089 }
1090 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1091 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001092 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001093 << Attr.getName() << PD->getType() << 1;
1094 return false;
1095 }
1096 }
1097 else {
1098 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1099 return false;
1100 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001101
Ted Kremenek7fd17232011-09-29 07:02:25 +00001102 return true;
1103}
1104
Chandler Carruthedc2c642011-07-02 00:01:44 +00001105static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001106 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001107 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001108
Michael Han99315932013-01-24 16:46:58 +00001109 D->addAttr(::new (S.Context)
1110 IBOutletAttr(Attr.getRange(), S.Context,
1111 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001112}
1113
Chandler Carruthedc2c642011-07-02 00:01:44 +00001114static void handleIBOutletCollection(Sema &S, Decl *D,
1115 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001116
1117 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001118 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001119 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1120 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001121 return;
1122 }
1123
Ted Kremenek7fd17232011-09-29 07:02:25 +00001124 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001125 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001126
Richard Smithb1f9a282013-10-31 01:56:18 +00001127 ParsedType PT;
1128
1129 if (Attr.hasParsedType())
1130 PT = Attr.getTypeArg();
1131 else {
1132 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1133 S.getScopeForContext(D->getDeclContext()->getParent()));
1134 if (!PT) {
1135 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1136 return;
1137 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001138 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001139
Richard Smithb87c4652013-10-31 21:23:20 +00001140 TypeSourceInfo *QTLoc = 0;
1141 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1142 if (!QTLoc)
1143 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001144
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001145 // Diagnose use of non-object type in iboutletcollection attribute.
1146 // FIXME. Gnu attribute extension ignores use of builtin types in
1147 // attributes. So, __attribute__((iboutletcollection(char))) will be
1148 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001149 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001150 S.Diag(Attr.getLoc(),
1151 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1152 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001153 return;
1154 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001155
Michael Han99315932013-01-24 16:46:58 +00001156 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001157 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001158 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001159}
1160
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001161static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001162 if (const RecordType *UT = T->getAsUnionType())
1163 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1164 RecordDecl *UD = UT->getDecl();
1165 for (RecordDecl::field_iterator it = UD->field_begin(),
1166 itend = UD->field_end(); it != itend; ++it) {
1167 QualType QT = it->getType();
1168 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1169 T = QT;
1170 return;
1171 }
1172 }
1173 }
1174}
1175
Chandler Carruthedc2c642011-07-02 00:01:44 +00001176static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001177 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1178 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001179 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001180 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001181 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001182 return;
1183 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001184
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001185 SmallVector<unsigned, 8> NonNullArgs;
1186 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001187 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001188 uint64_t Idx;
1189 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1190 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001191 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001192
1193 // Is the function argument a pointer type?
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001194 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001195 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001196
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001197 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001198 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +00001199 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +00001200 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001201 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001202 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001203
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001204 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001205 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001206
1207 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1208 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001209 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001210 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1211 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001212 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001213 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001214 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001215 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001216
Ted Kremenek22813f42010-10-21 18:49:36 +00001217 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001218 if (NonNullArgs.empty()) {
1219 // Warn the trivial case only if attribute is not coming from a
1220 // macro instantiation.
1221 if (Attr.getLoc().isFileID())
1222 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001223 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001224 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001225 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001226
Nick Lewyckye1121512013-01-24 01:12:16 +00001227 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001228 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001229 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001230 D->addAttr(::new (S.Context)
1231 NonNullAttr(Attr.getRange(), S.Context, start, size,
1232 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001233}
1234
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001235static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1236 switch (K) {
1237 case OwnershipAttr::Holds: return "'ownership_holds'";
1238 case OwnershipAttr::Takes: return "'ownership_takes'";
1239 case OwnershipAttr::Returns: return "'ownership_returns'";
1240 }
1241 llvm_unreachable("unknown ownership");
1242}
1243
Chandler Carruthedc2c642011-07-02 00:01:44 +00001244static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001245 // This attribute must be applied to a function declaration. The first
1246 // argument to the attribute must be an identifier, the name of the resource,
1247 // for example: malloc. The following arguments must be argument indexes, the
1248 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001249 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001250 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001251 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001252
Aaron Ballman00e99962013-08-31 01:11:41 +00001253 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001254 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001255 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001256 return;
1257 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001258
Richard Smith852e9ce2013-11-27 01:46:48 +00001259 // Figure out our Kind.
1260 OwnershipAttr::OwnershipKind K =
1261 OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0,
1262 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001263
Richard Smith852e9ce2013-11-27 01:46:48 +00001264 // Check arguments.
1265 switch (K) {
1266 case OwnershipAttr::Takes:
1267 case OwnershipAttr::Holds:
1268 if (AL.getNumArgs() < 2) {
1269 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
1270 return;
1271 }
1272 break;
1273 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001274 if (AL.getNumArgs() > 2) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001275 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001276 return;
1277 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001278 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001279 }
1280
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001281 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001282 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1283 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001284 return;
1285 }
1286
Richard Smith852e9ce2013-11-27 01:46:48 +00001287 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001288
1289 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001290 StringRef ModuleName = Module->getName();
1291 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1292 ModuleName.size() > 4) {
1293 ModuleName = ModuleName.drop_front(2).drop_back(2);
1294 Module = &S.PP.getIdentifierTable().get(ModuleName);
1295 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001296
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001297 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001298 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1299 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001300 uint64_t Idx;
1301 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
Aaron Ballman00e99962013-08-31 01:11:41 +00001302 AL.getLoc(), i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001303 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001304
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001305 // Is the function argument a pointer type?
1306 QualType T = getFunctionOrMethodArgType(D, Idx);
1307 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001308 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001309 case OwnershipAttr::Takes:
1310 case OwnershipAttr::Holds:
1311 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1312 Err = 0;
1313 break;
1314 case OwnershipAttr::Returns:
1315 if (!T->isIntegerType())
1316 Err = 1;
1317 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001318 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001319 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001320 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001321 << Ex->getSourceRange();
1322 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001323 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001324
1325 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001326 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001327 i = D->specific_attr_begin<OwnershipAttr>(),
1328 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001329 // FIXME: A returns attribute should conflict with any returns attribute
1330 // with a different index too.
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001331 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1332 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1333 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1334 << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind());
1335 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001336 }
1337 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001338 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001339 }
1340
1341 unsigned* start = OwnershipArgs.data();
1342 unsigned size = OwnershipArgs.size();
1343 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001344
Michael Han99315932013-01-24 16:46:58 +00001345 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001346 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001347 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001348}
1349
Chandler Carruthedc2c642011-07-02 00:01:44 +00001350static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001351 // Check the attribute arguments.
1352 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001353 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1354 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001355 return;
1356 }
1357
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001358 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001359
Rafael Espindolac18086a2010-02-23 22:00:30 +00001360 // gcc rejects
1361 // class c {
1362 // static int a __attribute__((weakref ("v2")));
1363 // static int b() __attribute__((weakref ("f3")));
1364 // };
1365 // and ignores the attributes of
1366 // void f(void) {
1367 // static int a __attribute__((weakref ("v2")));
1368 // }
1369 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001370 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001371 if (!Ctx->isFileContext()) {
1372 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +00001373 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +00001374 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001375 }
1376
1377 // The GCC manual says
1378 //
1379 // At present, a declaration to which `weakref' is attached can only
1380 // be `static'.
1381 //
1382 // It also says
1383 //
1384 // Without a TARGET,
1385 // given as an argument to `weakref' or to `alias', `weakref' is
1386 // equivalent to `weak'.
1387 //
1388 // gcc 4.4.1 will accept
1389 // int a7 __attribute__((weakref));
1390 // as
1391 // int a7 __attribute__((weak));
1392 // This looks like a bug in gcc. We reject that for now. We should revisit
1393 // it if this behaviour is actually used.
1394
Rafael Espindolac18086a2010-02-23 22:00:30 +00001395 // GCC rejects
1396 // static ((alias ("y"), weakref)).
1397 // Should we? How to check that weakref is before or after alias?
1398
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001399 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1400 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1401 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001402 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001403 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001404 // GCC will accept anything as the argument of weakref. Should we
1405 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001406 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1407 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001408
Michael Han99315932013-01-24 16:46:58 +00001409 D->addAttr(::new (S.Context)
1410 WeakRefAttr(Attr.getRange(), S.Context,
1411 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001412}
1413
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001414static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1415 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001416 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001417 return;
1418
Douglas Gregore8bbc122011-09-02 00:18:52 +00001419 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001420 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1421 return;
1422 }
1423
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001424 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001425
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001426 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001427 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001428}
1429
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001430static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanfb763042013-12-02 18:05:46 +00001431 if (checkAttrMutualExclusion<HotAttr>(S, D, Attr, "hot"))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001432 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001433
Michael Han99315932013-01-24 16:46:58 +00001434 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1435 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001436}
1437
1438static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmanfb763042013-12-02 18:05:46 +00001439 if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr, "cold"))
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001440 return;
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001441
Michael Han99315932013-01-24 16:46:58 +00001442 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1443 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001444}
1445
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001446static void handleTLSModelAttr(Sema &S, Decl *D,
1447 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001448 StringRef Model;
1449 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001450 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001451 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001452 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001453
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001454 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001455 if (Model != "global-dynamic" && Model != "local-dynamic"
1456 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001457 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001458 return;
1459 }
1460
Michael Han99315932013-01-24 16:46:58 +00001461 D->addAttr(::new (S.Context)
1462 TLSModelAttr(Attr.getRange(), S.Context, Model,
1463 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001464}
1465
Chandler Carruthedc2c642011-07-02 00:01:44 +00001466static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001467 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001468 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001469 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001470 D->addAttr(::new (S.Context)
1471 MallocAttr(Attr.getRange(), S.Context,
1472 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001473 return;
1474 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001475 }
1476
Ted Kremenek08479ae2009-08-15 00:51:46 +00001477 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001478}
1479
Chandler Carruthedc2c642011-07-02 00:01:44 +00001480static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001481 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001482 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1483 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001484 return;
1485 }
1486
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001487 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context,
1488 Attr.getAttributeSpellingListIndex()));
Eric Christopher8a2ee392010-12-02 02:45:55 +00001489}
1490
Chandler Carruthedc2c642011-07-02 00:01:44 +00001491static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001492 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001493
1494 if (S.CheckNoReturnAttr(attr)) return;
1495
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001496 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001497 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001498 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001499 return;
1500 }
1501
Michael Han99315932013-01-24 16:46:58 +00001502 D->addAttr(::new (S.Context)
1503 NoReturnAttr(attr.getRange(), S.Context,
1504 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001505}
1506
1507bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001508 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001509 attr.setInvalid();
1510 return true;
1511 }
1512
1513 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001514}
1515
Chandler Carruthedc2c642011-07-02 00:01:44 +00001516static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1517 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001518
1519 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1520 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001521 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1522 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001523 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1524 && !VD->getType()->isFunctionPointerType())) {
1525 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001526 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001527 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001528 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001529 return;
1530 }
1531 }
1532
Michael Han99315932013-01-24 16:46:58 +00001533 D->addAttr(::new (S.Context)
1534 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1535 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001536}
1537
John Thompsoncdb847ba2010-08-09 21:53:52 +00001538// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001539static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001540/*
1541 Returning a Vector Class in Registers
1542
Eric Christopherbc638a82010-12-01 22:13:54 +00001543 According to the PPU ABI specifications, a class with a single member of
1544 vector type is returned in memory when used as the return value of a function.
1545 This results in inefficient code when implementing vector classes. To return
1546 the value in a single vector register, add the vecreturn attribute to the
1547 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001548
1549 Example:
1550
1551 struct Vector
1552 {
1553 __vector float xyzw;
1554 } __attribute__((vecreturn));
1555
1556 Vector Add(Vector lhs, Vector rhs)
1557 {
1558 Vector result;
1559 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1560 return result; // This will be returned in a register
1561 }
1562*/
Aaron Ballman9ead1242013-12-19 02:39:40 +00001563 if (D->hasAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001564 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1565 return;
1566 }
1567
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001568 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001569 int count = 0;
1570
1571 if (!isa<CXXRecordDecl>(record)) {
1572 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1573 return;
1574 }
1575
1576 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1577 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1578 return;
1579 }
1580
Eric Christopherbc638a82010-12-01 22:13:54 +00001581 for (RecordDecl::field_iterator iter = record->field_begin();
1582 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001583 if ((count == 1) || !iter->getType()->isVectorType()) {
1584 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1585 return;
1586 }
1587 count++;
1588 }
1589
Michael Han99315932013-01-24 16:46:58 +00001590 D->addAttr(::new (S.Context)
1591 VecReturnAttr(Attr.getRange(), S.Context,
1592 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001593}
1594
Richard Smithe233fbf2013-01-28 22:42:45 +00001595static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1596 const AttributeList &Attr) {
1597 if (isa<ParmVarDecl>(D)) {
1598 // [[carries_dependency]] can only be applied to a parameter if it is a
1599 // parameter of a function declaration or lambda.
1600 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1601 S.Diag(Attr.getLoc(),
1602 diag::err_carries_dependency_param_not_function_decl);
1603 return;
1604 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001605 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001606
1607 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1608 Attr.getRange(), S.Context,
1609 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001610}
1611
Chandler Carruthedc2c642011-07-02 00:01:44 +00001612static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001613 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00001614 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001615 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001616 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001617 return;
1618 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001619
Michael Han99315932013-01-24 16:46:58 +00001620 D->addAttr(::new (S.Context)
1621 UnusedAttr(Attr.getRange(), S.Context,
1622 Attr.getAttributeSpellingListIndex()));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001623}
1624
Chandler Carruthedc2c642011-07-02 00:01:44 +00001625static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001626 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00001627 if (VD->hasLocalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001628 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1629 return;
1630 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001631 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001632 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001633 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001634 return;
1635 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001636
Michael Han99315932013-01-24 16:46:58 +00001637 D->addAttr(::new (S.Context)
1638 UsedAttr(Attr.getRange(), S.Context,
1639 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001640}
1641
Chandler Carruthedc2c642011-07-02 00:01:44 +00001642static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001643 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001644 if (Attr.getNumArgs() > 1) {
1645 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001646 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001647 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001648
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001649 uint32_t priority = ConstructorAttr::DefaultPriority;
1650 if (Attr.getNumArgs() > 0 &&
1651 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1652 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001653
Michael Han99315932013-01-24 16:46:58 +00001654 D->addAttr(::new (S.Context)
1655 ConstructorAttr(Attr.getRange(), S.Context, priority,
1656 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001657}
1658
Chandler Carruthedc2c642011-07-02 00:01:44 +00001659static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001660 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001661 if (Attr.getNumArgs() > 1) {
1662 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001663 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001664 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001665
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001666 uint32_t priority = ConstructorAttr::DefaultPriority;
1667 if (Attr.getNumArgs() > 0 &&
1668 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1669 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001670
Michael Han99315932013-01-24 16:46:58 +00001671 D->addAttr(::new (S.Context)
1672 DestructorAttr(Attr.getRange(), S.Context, priority,
1673 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001674}
1675
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001676template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001677static void handleAttrWithMessage(Sema &S, Decl *D,
1678 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001679 unsigned NumArgs = Attr.getNumArgs();
1680 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001681 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001682 return;
1683 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001684
1685 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001686 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001687 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001688 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001689
Michael Han99315932013-01-24 16:46:58 +00001690 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1691 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001692}
1693
Ted Kremenek28eace62013-11-23 01:01:34 +00001694static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
1695 const AttributeList &Attr) {
Ted Kremenek28eace62013-11-23 01:01:34 +00001696 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001697 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1698 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001699}
1700
Jordy Rose740b0c22012-05-08 03:27:22 +00001701static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1702 IdentifierInfo *Platform,
1703 VersionTuple Introduced,
1704 VersionTuple Deprecated,
1705 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001706 StringRef PlatformName
1707 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1708 if (PlatformName.empty())
1709 PlatformName = Platform->getName();
1710
1711 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1712 // of these steps are needed).
1713 if (!Introduced.empty() && !Deprecated.empty() &&
1714 !(Introduced <= Deprecated)) {
1715 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1716 << 1 << PlatformName << Deprecated.getAsString()
1717 << 0 << Introduced.getAsString();
1718 return true;
1719 }
1720
1721 if (!Introduced.empty() && !Obsoleted.empty() &&
1722 !(Introduced <= Obsoleted)) {
1723 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1724 << 2 << PlatformName << Obsoleted.getAsString()
1725 << 0 << Introduced.getAsString();
1726 return true;
1727 }
1728
1729 if (!Deprecated.empty() && !Obsoleted.empty() &&
1730 !(Deprecated <= Obsoleted)) {
1731 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1732 << 2 << PlatformName << Obsoleted.getAsString()
1733 << 1 << Deprecated.getAsString();
1734 return true;
1735 }
1736
1737 return false;
1738}
1739
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001740/// \brief Check whether the two versions match.
1741///
1742/// If either version tuple is empty, then they are assumed to match. If
1743/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1744static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1745 bool BeforeIsOkay) {
1746 if (X.empty() || Y.empty())
1747 return true;
1748
1749 if (X == Y)
1750 return true;
1751
1752 if (BeforeIsOkay && X < Y)
1753 return true;
1754
1755 return false;
1756}
1757
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001758AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001759 IdentifierInfo *Platform,
1760 VersionTuple Introduced,
1761 VersionTuple Deprecated,
1762 VersionTuple Obsoleted,
1763 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001764 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001765 bool Override,
1766 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001767 VersionTuple MergedIntroduced = Introduced;
1768 VersionTuple MergedDeprecated = Deprecated;
1769 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001770 bool FoundAny = false;
1771
Rafael Espindolac67f2232012-05-10 02:50:16 +00001772 if (D->hasAttrs()) {
1773 AttrVec &Attrs = D->getAttrs();
1774 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1775 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1776 if (!OldAA) {
1777 ++i;
1778 continue;
1779 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001780
Rafael Espindolac67f2232012-05-10 02:50:16 +00001781 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1782 if (OldPlatform != Platform) {
1783 ++i;
1784 continue;
1785 }
1786
1787 FoundAny = true;
1788 VersionTuple OldIntroduced = OldAA->getIntroduced();
1789 VersionTuple OldDeprecated = OldAA->getDeprecated();
1790 VersionTuple OldObsoleted = OldAA->getObsoleted();
1791 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001792
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001793 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1794 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1795 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1796 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001797 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001798 if (Override) {
1799 int Which = -1;
1800 VersionTuple FirstVersion;
1801 VersionTuple SecondVersion;
1802 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1803 Which = 0;
1804 FirstVersion = OldIntroduced;
1805 SecondVersion = Introduced;
1806 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1807 Which = 1;
1808 FirstVersion = Deprecated;
1809 SecondVersion = OldDeprecated;
1810 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1811 Which = 2;
1812 FirstVersion = Obsoleted;
1813 SecondVersion = OldObsoleted;
1814 }
1815
1816 if (Which == -1) {
1817 Diag(OldAA->getLocation(),
1818 diag::warn_mismatched_availability_override_unavail)
1819 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1820 } else {
1821 Diag(OldAA->getLocation(),
1822 diag::warn_mismatched_availability_override)
1823 << Which
1824 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1825 << FirstVersion.getAsString() << SecondVersion.getAsString();
1826 }
1827 Diag(Range.getBegin(), diag::note_overridden_method);
1828 } else {
1829 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1830 Diag(Range.getBegin(), diag::note_previous_attribute);
1831 }
1832
Rafael Espindolac67f2232012-05-10 02:50:16 +00001833 Attrs.erase(Attrs.begin() + i);
1834 --e;
1835 continue;
1836 }
1837
1838 VersionTuple MergedIntroduced2 = MergedIntroduced;
1839 VersionTuple MergedDeprecated2 = MergedDeprecated;
1840 VersionTuple MergedObsoleted2 = MergedObsoleted;
1841
1842 if (MergedIntroduced2.empty())
1843 MergedIntroduced2 = OldIntroduced;
1844 if (MergedDeprecated2.empty())
1845 MergedDeprecated2 = OldDeprecated;
1846 if (MergedObsoleted2.empty())
1847 MergedObsoleted2 = OldObsoleted;
1848
1849 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1850 MergedIntroduced2, MergedDeprecated2,
1851 MergedObsoleted2)) {
1852 Attrs.erase(Attrs.begin() + i);
1853 --e;
1854 continue;
1855 }
1856
1857 MergedIntroduced = MergedIntroduced2;
1858 MergedDeprecated = MergedDeprecated2;
1859 MergedObsoleted = MergedObsoleted2;
1860 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001861 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001862 }
1863
1864 if (FoundAny &&
1865 MergedIntroduced == Introduced &&
1866 MergedDeprecated == Deprecated &&
1867 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001868 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001869
Ted Kremenekb5445722013-04-06 00:34:27 +00001870 // Only create a new attribute if !Override, but we want to do
1871 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001872 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001873 MergedDeprecated, MergedObsoleted) &&
1874 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001875 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1876 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001877 Obsoleted, IsUnavailable, Message,
1878 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001879 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001880 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001881}
1882
Chandler Carruthedc2c642011-07-02 00:01:44 +00001883static void handleAvailabilityAttr(Sema &S, Decl *D,
1884 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001885 if (!checkAttributeNumArgs(S, Attr, 1))
1886 return;
1887 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001888 unsigned Index = Attr.getAttributeSpellingListIndex();
1889
Aaron Ballman00e99962013-08-31 01:11:41 +00001890 IdentifierInfo *II = Platform->Ident;
1891 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1892 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1893 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001894
Rafael Espindolac231fab2013-01-08 21:30:32 +00001895 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1896 if (!ND) {
1897 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1898 return;
1899 }
1900
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001901 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1902 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1903 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001904 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001905 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001906 if (const StringLiteral *SE =
1907 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001908 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001909
Aaron Ballman00e99962013-08-31 01:11:41 +00001910 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001911 Introduced.Version,
1912 Deprecated.Version,
1913 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001914 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001915 /*Override=*/false,
1916 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001917 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001918 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001919}
1920
John McCalld041a9b2013-02-20 01:54:26 +00001921template <class T>
1922static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1923 typename T::VisibilityType value,
1924 unsigned attrSpellingListIndex) {
1925 T *existingAttr = D->getAttr<T>();
1926 if (existingAttr) {
1927 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1928 if (existingValue == value)
1929 return NULL;
1930 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1931 S.Diag(range.getBegin(), diag::note_previous_attribute);
1932 D->dropAttr<T>();
1933 }
1934 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1935}
1936
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001937VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001938 VisibilityAttr::VisibilityType Vis,
1939 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001940 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1941 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001942}
1943
John McCalld041a9b2013-02-20 01:54:26 +00001944TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1945 TypeVisibilityAttr::VisibilityType Vis,
1946 unsigned AttrSpellingListIndex) {
1947 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1948 AttrSpellingListIndex);
1949}
1950
1951static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1952 bool isTypeVisibility) {
1953 // Visibility attributes don't mean anything on a typedef.
1954 if (isa<TypedefNameDecl>(D)) {
1955 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
1956 << Attr.getName();
1957 return;
1958 }
1959
1960 // 'type_visibility' can only go on a type or namespace.
1961 if (isTypeVisibility &&
1962 !(isa<TagDecl>(D) ||
1963 isa<ObjCInterfaceDecl>(D) ||
1964 isa<NamespaceDecl>(D))) {
1965 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
1966 << Attr.getName() << ExpectedTypeOrNamespace;
1967 return;
1968 }
1969
Benjamin Kramer70370212013-09-09 15:08:57 +00001970 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001971 StringRef TypeStr;
1972 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001973 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001974 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001975
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001976 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00001977 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001978 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00001979 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001980 return;
1981 }
Aaron Ballman682ee422013-09-11 19:47:58 +00001982
1983 // Complain about attempts to use protected visibility on targets
1984 // (like Darwin) that don't support it.
1985 if (type == VisibilityAttr::Protected &&
1986 !S.Context.getTargetInfo().hasProtectedVisibility()) {
1987 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1988 type = VisibilityAttr::Default;
1989 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001990
Michael Han99315932013-01-24 16:46:58 +00001991 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00001992 clang::Attr *newAttr;
1993 if (isTypeVisibility) {
1994 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
1995 (TypeVisibilityAttr::VisibilityType) type,
1996 Index);
1997 } else {
1998 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
1999 }
2000 if (newAttr)
2001 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002002}
2003
Chandler Carruthedc2c642011-07-02 00:01:44 +00002004static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2005 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002006 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002007 if (!Attr.isArgIdent(0)) {
2008 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2009 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002010 return;
2011 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002012
Aaron Ballman682ee422013-09-11 19:47:58 +00002013 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2014 ObjCMethodFamilyAttr::FamilyKind F;
2015 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2016 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2017 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002018 return;
2019 }
2020
Aaron Ballman682ee422013-09-11 19:47:58 +00002021 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCall31168b02011-06-15 23:02:42 +00002022 !method->getResultType()->isObjCObjectPointerType()) {
2023 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2024 << method->getResultType();
2025 // Ignore the attribute.
2026 return;
2027 }
2028
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002029 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman682ee422013-09-11 19:47:58 +00002030 S.Context, F));
John McCall86bc21f2011-03-02 11:33:24 +00002031}
2032
Chandler Carruthedc2c642011-07-02 00:01:44 +00002033static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002034 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002035 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002036 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002037 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2038 return;
2039 }
2040 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002041 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2042 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002043 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002044 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2045 return;
2046 }
2047 }
2048 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002049 // It is okay to include this attribute on properties, e.g.:
2050 //
2051 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2052 //
2053 // In this case it follows tradition and suppresses an error in the above
2054 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002055 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002056 }
Michael Han99315932013-01-24 16:46:58 +00002057 D->addAttr(::new (S.Context)
2058 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2059 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002060}
2061
Chandler Carruthedc2c642011-07-02 00:01:44 +00002062static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002063 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002064 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002065 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002066 return;
2067 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002068
Aaron Ballman00e99962013-08-31 01:11:41 +00002069 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002070 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002071 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2072 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2073 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002074 return;
2075 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002076
Michael Han99315932013-01-24 16:46:58 +00002077 D->addAttr(::new (S.Context)
2078 BlocksAttr(Attr.getRange(), S.Context, type,
2079 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002080}
2081
Chandler Carruthedc2c642011-07-02 00:01:44 +00002082static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002083 // check the attribute arguments.
2084 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002085 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002086 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002087 }
2088
Aaron Ballman18a78382013-11-21 00:28:23 +00002089 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002090 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002091 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002092 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002093 if (E->isTypeDependent() || E->isValueDependent() ||
2094 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002095 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002096 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002097 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002098 return;
2099 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002100
John McCallb46f2872011-09-09 07:56:05 +00002101 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002102 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2103 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002104 return;
2105 }
John McCallb46f2872011-09-09 07:56:05 +00002106
2107 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002108 }
2109
Aaron Ballman18a78382013-11-21 00:28:23 +00002110 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002111 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002112 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002113 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002114 if (E->isTypeDependent() || E->isValueDependent() ||
2115 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002116 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002117 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002118 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002119 return;
2120 }
2121 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002122
John McCallb46f2872011-09-09 07:56:05 +00002123 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002124 // FIXME: This error message could be improved, it would be nice
2125 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002126 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2127 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002128 return;
2129 }
2130 }
2131
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002132 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002133 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002134 if (isa<FunctionNoProtoType>(FT)) {
2135 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2136 return;
2137 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002138
Chris Lattner9363e312009-03-17 23:03:47 +00002139 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002140 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002141 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002142 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002143 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002144 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002145 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002146 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002147 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002148 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2149 if (!BD->isVariadic()) {
2150 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2151 return;
2152 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002153 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002154 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002155 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002156 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002157 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002158 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002159 int m = Ty->isFunctionPointerType() ? 0 : 1;
2160 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002161 return;
2162 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002163 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002164 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002165 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002166 return;
2167 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002168 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002169 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002170 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002171 return;
2172 }
Michael Han99315932013-01-24 16:46:58 +00002173 D->addAttr(::new (S.Context)
2174 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2175 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002176}
2177
Chandler Carruthedc2c642011-07-02 00:01:44 +00002178static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002179 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002180 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002181 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002182 return;
2183 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002184
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002185 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2186 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2187 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002188 return;
2189 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002190 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2191 if (MD->getResultType()->isVoidType()) {
2192 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2193 << Attr.getName() << 1;
2194 return;
2195 }
2196
Michael Han99315932013-01-24 16:46:58 +00002197 D->addAttr(::new (S.Context)
2198 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2199 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002200}
2201
Chandler Carruthedc2c642011-07-02 00:01:44 +00002202static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002203 // weak_import only applies to variable & function declarations.
2204 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002205 if (!D->canBeWeakImported(isDef)) {
2206 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002207 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2208 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002209 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002210 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002211 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002212 // Nothing to warn about here.
2213 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002214 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002215 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002216
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002217 return;
2218 }
2219
Michael Han99315932013-01-24 16:46:58 +00002220 D->addAttr(::new (S.Context)
2221 WeakImportAttr(Attr.getRange(), S.Context,
2222 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002223}
2224
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002225// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002226template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002227static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002228 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002229 uint32_t WGSize[3];
2230 for (unsigned i = 0; i < 3; ++i)
2231 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002232 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002233
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002234 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2235 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2236 Existing->getYDim() == WGSize[1] &&
2237 Existing->getZDim() == WGSize[2]))
2238 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002239
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002240 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2241 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002242 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002243}
2244
Joey Goulyaba589c2013-03-08 09:42:32 +00002245static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002246 if (!Attr.hasParsedType()) {
2247 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2248 << Attr.getName() << 1;
2249 return;
2250 }
2251
Richard Smithb87c4652013-10-31 21:23:20 +00002252 TypeSourceInfo *ParmTSI = 0;
2253 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2254 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002255
2256 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2257 (ParmType->isBooleanType() ||
2258 !ParmType->isIntegralType(S.getASTContext()))) {
2259 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2260 << ParmType;
2261 return;
2262 }
2263
Aaron Ballmana9e05402013-12-02 22:16:55 +00002264 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002265 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002266 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2267 return;
2268 }
2269 }
2270
2271 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Richard Smithb87c4652013-10-31 21:23:20 +00002272 ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002273}
2274
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002275SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002276 StringRef Name,
2277 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002278 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2279 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002280 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002281 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2282 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002283 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002284 }
Michael Han99315932013-01-24 16:46:58 +00002285 return ::new (Context) SectionAttr(Range, Context, Name,
2286 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002287}
2288
Chandler Carruthedc2c642011-07-02 00:01:44 +00002289static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002290 // Make sure that there is a string literal as the sections's single
2291 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002292 StringRef Str;
2293 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002294 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002295 return;
Mike Stump11289f42009-09-09 15:08:12 +00002296
Chris Lattner30ba6742009-08-10 19:03:04 +00002297 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002298 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002299 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002300 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002301 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002302 return;
2303 }
Mike Stump11289f42009-09-09 15:08:12 +00002304
Michael Han99315932013-01-24 16:46:58 +00002305 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002306 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002307 if (NewAttr)
2308 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002309}
2310
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002311
Chandler Carruthedc2c642011-07-02 00:01:44 +00002312static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002313 VarDecl *VD = cast<VarDecl>(D);
2314 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002315 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002316 return;
2317 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002318
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002319 Expr *E = Attr.getArgAsExpr(0);
2320 SourceLocation Loc = E->getExprLoc();
2321 FunctionDecl *FD = 0;
2322 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002323
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002324 // gcc only allows for simple identifiers. Since we support more than gcc, we
2325 // will warn the user.
2326 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2327 if (DRE->hasQualifier())
2328 S.Diag(Loc, diag::warn_cleanup_ext);
2329 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2330 NI = DRE->getNameInfo();
2331 if (!FD) {
2332 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2333 << NI.getName();
2334 return;
2335 }
2336 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2337 if (ULE->hasExplicitTemplateArgs())
2338 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002339 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2340 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002341 if (!FD) {
2342 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2343 << NI.getName();
2344 if (ULE->getType() == S.Context.OverloadTy)
2345 S.NoteAllOverloadCandidates(ULE);
2346 return;
2347 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002348 } else {
2349 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002350 return;
2351 }
2352
Anders Carlssond277d792009-01-31 01:16:18 +00002353 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002354 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2355 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002356 return;
2357 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002358
Anders Carlsson723f55d2009-02-07 23:16:50 +00002359 // We're currently more strict than GCC about what function types we accept.
2360 // If this ever proves to be a problem it should be easy to fix.
2361 QualType Ty = S.Context.getPointerType(VD->getType());
2362 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002363 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2364 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002365 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2366 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002367 return;
2368 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002369
Michael Han99315932013-01-24 16:46:58 +00002370 D->addAttr(::new (S.Context)
2371 CleanupAttr(Attr.getRange(), S.Context, FD,
2372 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002373}
2374
Mike Stumpd3bb5572009-07-24 19:02:52 +00002375/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002376/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002377static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002378 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002379 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002380 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002381 return;
2382 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002383
Aaron Ballman00e99962013-08-31 01:11:41 +00002384 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002385 uint64_t ArgIdx;
2386 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2387 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002388 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002389
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002390 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002391 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002392
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002393 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2394 if (not_nsstring_type &&
2395 !isCFStringType(Ty, S.Context) &&
2396 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002397 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002398 // FIXME: Should highlight the actual expression that has the wrong type.
2399 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002400 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002401 << IdxExpr->getSourceRange();
2402 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002403 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002404 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002405 if (!isNSStringType(Ty, S.Context) &&
2406 !isCFStringType(Ty, S.Context) &&
2407 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002408 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002409 // FIXME: Should highlight the actual expression that has the wrong type.
2410 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002411 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002412 << IdxExpr->getSourceRange();
2413 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002414 }
2415
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002416 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2417 // because that has corrected for the implicit this parameter, and is zero-
2418 // based. The attribute expects what the user wrote explicitly.
2419 llvm::APSInt Val;
2420 IdxExpr->EvaluateAsInt(Val, S.Context);
2421
Michael Han99315932013-01-24 16:46:58 +00002422 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002423 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002424 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002425}
2426
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002427enum FormatAttrKind {
2428 CFStringFormat,
2429 NSStringFormat,
2430 StrftimeFormat,
2431 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002432 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002433 InvalidFormat
2434};
2435
2436/// getFormatAttrKind - Map from format attribute names to supported format
2437/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002438static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002439 return llvm::StringSwitch<FormatAttrKind>(Format)
2440 // Check for formats that get handled specially.
2441 .Case("NSString", NSStringFormat)
2442 .Case("CFString", CFStringFormat)
2443 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002444
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002445 // Otherwise, check for supported formats.
2446 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2447 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2448 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002449
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002450 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2451 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002452}
2453
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002454/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002455/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002456static void handleInitPriorityAttr(Sema &S, Decl *D,
2457 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002458 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002459 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2460 return;
2461 }
2462
Aaron Ballman4a611152013-11-27 16:34:09 +00002463 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002464 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2465 Attr.setInvalid();
2466 return;
2467 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002468 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002469 if (S.Context.getAsArrayType(T))
2470 T = S.Context.getBaseElementType(T);
2471 if (!T->getAs<RecordType>()) {
2472 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2473 Attr.setInvalid();
2474 return;
2475 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002476
2477 Expr *E = Attr.getArgAsExpr(0);
2478 uint32_t prioritynum;
2479 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002480 Attr.setInvalid();
2481 return;
2482 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002483
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002484 if (prioritynum < 101 || prioritynum > 65535) {
2485 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002486 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002487 Attr.setInvalid();
2488 return;
2489 }
Michael Han99315932013-01-24 16:46:58 +00002490 D->addAttr(::new (S.Context)
2491 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2492 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002493}
2494
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002495FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2496 IdentifierInfo *Format, int FormatIdx,
2497 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002498 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002499 // Check whether we already have an equivalent format attribute.
2500 for (specific_attr_iterator<FormatAttr>
2501 i = D->specific_attr_begin<FormatAttr>(),
2502 e = D->specific_attr_end<FormatAttr>();
2503 i != e ; ++i) {
2504 FormatAttr *f = *i;
2505 if (f->getType() == Format &&
2506 f->getFormatIdx() == FormatIdx &&
2507 f->getFirstArg() == FirstArg) {
2508 // If we don't have a valid location for this attribute, adopt the
2509 // location.
2510 if (f->getLocation().isInvalid())
2511 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002512 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002513 }
2514 }
2515
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002516 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2517 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002518}
2519
Mike Stumpd3bb5572009-07-24 19:02:52 +00002520/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002521/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002522static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002523 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002524 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002525 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002526 return;
2527 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002528
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002529 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002530 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002531 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002532 return;
2533 }
2534
Chandler Carruth743682b2010-11-16 08:35:43 +00002535 // In C++ the implicit 'this' function parameter also counts, and they are
2536 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002537 bool HasImplicitThisParam = isInstanceMethod(D);
2538 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002539
Aaron Ballman00e99962013-08-31 01:11:41 +00002540 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2541 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002542
2543 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002544 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002545 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002546 // If we've modified the string name, we need a new identifier for it.
2547 II = &S.Context.Idents.get(Format);
2548 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002549
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002550 // Check for supported formats.
2551 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002552
2553 if (Kind == IgnoredFormat)
2554 return;
2555
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002556 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002557 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman00e99962013-08-31 01:11:41 +00002558 << "format" << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002559 return;
2560 }
2561
2562 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002563 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002564 uint32_t Idx;
2565 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002566 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002567
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002568 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002569 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002570 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002571 return;
2572 }
2573
2574 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002575 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002576
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002577 if (HasImplicitThisParam) {
2578 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002579 S.Diag(Attr.getLoc(),
2580 diag::err_format_attribute_implicit_this_format_string)
2581 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002582 return;
2583 }
2584 ArgIdx--;
2585 }
Mike Stump11289f42009-09-09 15:08:12 +00002586
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002587 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002588 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002589
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002590 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002591 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002592 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2593 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002594 return;
2595 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002596 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002597 // FIXME: do we need to check if the type is NSString*? What are the
2598 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002599 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002600 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002601 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2602 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002603 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002604 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002605 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002606 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002607 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002608 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2609 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002610 return;
2611 }
2612
2613 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002614 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002615 uint32_t FirstArg;
2616 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002617 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002618
2619 // check if the function is variadic if the 3rd argument non-zero
2620 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002621 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002622 ++NumArgs; // +1 for ...
2623 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002624 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002625 return;
2626 }
2627 }
2628
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002629 // strftime requires FirstArg to be 0 because it doesn't read from any
2630 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002631 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002632 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002633 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2634 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002635 return;
2636 }
2637 // if 0 it disables parameter checking (to use with e.g. va_list)
2638 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002639 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002640 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002641 return;
2642 }
2643
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002644 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002645 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002646 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002647 if (NewAttr)
2648 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002649}
2650
Chandler Carruthedc2c642011-07-02 00:01:44 +00002651static void handleTransparentUnionAttr(Sema &S, Decl *D,
2652 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002653 // Try to find the underlying union declaration.
2654 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002655 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002656 if (TD && TD->getUnderlyingType()->isUnionType())
2657 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2658 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002659 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002660
2661 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002662 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002663 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002664 return;
2665 }
2666
John McCallf937c022011-10-07 06:10:15 +00002667 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002668 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002669 diag::warn_transparent_union_attribute_not_definition);
2670 return;
2671 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002672
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002673 RecordDecl::field_iterator Field = RD->field_begin(),
2674 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002675 if (Field == FieldEnd) {
2676 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2677 return;
2678 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002679
David Blaikie40ed2972012-06-06 20:45:41 +00002680 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002681 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002682 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002683 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002684 diag::warn_transparent_union_attribute_floating)
2685 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002686 return;
2687 }
2688
2689 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2690 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2691 for (; Field != FieldEnd; ++Field) {
2692 QualType FieldType = Field->getType();
2693 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2694 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2695 // Warn if we drop the attribute.
2696 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002697 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002698 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002699 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002700 diag::warn_transparent_union_attribute_field_size_align)
2701 << isSize << Field->getDeclName() << FieldBits;
2702 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002703 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002704 diag::note_transparent_union_first_field_size_align)
2705 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002706 return;
2707 }
2708 }
2709
Michael Han99315932013-01-24 16:46:58 +00002710 RD->addAttr(::new (S.Context)
2711 TransparentUnionAttr(Attr.getRange(), S.Context,
2712 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002713}
2714
Chandler Carruthedc2c642011-07-02 00:01:44 +00002715static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002716 // Make sure that there is a string literal as the annotation's single
2717 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002718 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002719 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002720 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002721
2722 // Don't duplicate annotations that are already set.
2723 for (specific_attr_iterator<AnnotateAttr>
2724 i = D->specific_attr_begin<AnnotateAttr>(),
2725 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002726 if ((*i)->getAnnotation() == Str)
2727 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002728 }
Michael Han99315932013-01-24 16:46:58 +00002729
2730 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002731 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002732 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002733}
2734
Chandler Carruthedc2c642011-07-02 00:01:44 +00002735static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002736 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002737 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002738 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2739 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002740 return;
2741 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002742
Richard Smith848e1f12013-02-01 08:12:08 +00002743 if (Attr.getNumArgs() == 0) {
2744 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2745 true, 0, Attr.getAttributeSpellingListIndex()));
2746 return;
2747 }
2748
Aaron Ballman00e99962013-08-31 01:11:41 +00002749 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002750 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2751 S.Diag(Attr.getEllipsisLoc(),
2752 diag::err_pack_expansion_without_parameter_packs);
2753 return;
2754 }
2755
2756 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2757 return;
2758
2759 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2760 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002761}
2762
2763void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002764 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002765 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2766 SourceLocation AttrLoc = AttrRange.getBegin();
2767
Richard Smith1dba27c2013-01-29 09:02:09 +00002768 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002769 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002770 // C++11 [dcl.align]p1:
2771 // An alignment-specifier may be applied to a variable or to a class
2772 // data member, but it shall not be applied to a bit-field, a function
2773 // parameter, the formal parameter of a catch clause, or a variable
2774 // declared with the register storage class specifier. An
2775 // alignment-specifier may also be applied to the declaration of a class
2776 // or enumeration type.
2777 // C11 6.7.5/2:
2778 // An alignment attribute shall not be specified in a declaration of
2779 // a typedef, or a bit-field, or a function, or a parameter, or an
2780 // object declared with the register storage-class specifier.
2781 int DiagKind = -1;
2782 if (isa<ParmVarDecl>(D)) {
2783 DiagKind = 0;
2784 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2785 if (VD->getStorageClass() == SC_Register)
2786 DiagKind = 1;
2787 if (VD->isExceptionVariable())
2788 DiagKind = 2;
2789 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2790 if (FD->isBitField())
2791 DiagKind = 3;
2792 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00002793 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
2794 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00002795 << (TmpAttr.isC11() ? ExpectedVariableOrField
2796 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002797 return;
2798 }
2799 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002800 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smithbc8caaf2013-02-22 04:55:39 +00002801 << TmpAttr.isC11() << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002802 return;
2803 }
2804 }
2805
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002806 if (E->isTypeDependent() || E->isValueDependent()) {
2807 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002808 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2809 AA->setPackExpansion(IsPackExpansion);
2810 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002811 return;
2812 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002813
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002814 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002815 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002816 ExprResult ICE
2817 = VerifyIntegerConstantExpression(E, &Alignment,
2818 diag::err_aligned_attribute_argument_not_int,
2819 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002820 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002821 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002822
2823 // C++11 [dcl.align]p2:
2824 // -- if the constant expression evaluates to zero, the alignment
2825 // specifier shall have no effect
2826 // C11 6.7.5p6:
2827 // An alignment specification of zero has no effect.
2828 if (!(TmpAttr.isAlignas() && !Alignment) &&
2829 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002830 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2831 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002832 return;
2833 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002834
Richard Smith848e1f12013-02-01 08:12:08 +00002835 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00002836 // We've already verified it's a power of 2, now let's make sure it's
2837 // 8192 or less.
2838 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00002839 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00002840 << E->getSourceRange();
2841 return;
2842 }
2843 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002844
Richard Smith44c247f2013-02-22 08:32:16 +00002845 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
2846 ICE.take(), SpellingListIndex);
2847 AA->setPackExpansion(IsPackExpansion);
2848 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002849}
2850
Michael Hanaf02bbe2013-02-01 01:19:17 +00002851void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002852 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002853 // FIXME: Cache the number on the Attr object if non-dependent?
2854 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002855 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2856 SpellingListIndex);
2857 AA->setPackExpansion(IsPackExpansion);
2858 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002859}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002860
Richard Smith848e1f12013-02-01 08:12:08 +00002861void Sema::CheckAlignasUnderalignment(Decl *D) {
2862 assert(D->hasAttrs() && "no attributes on decl");
2863
2864 QualType Ty;
2865 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2866 Ty = VD->getType();
2867 else
2868 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002869 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002870 return;
2871
2872 // C++11 [dcl.align]p5, C11 6.7.5/4:
2873 // The combined effect of all alignment attributes in a declaration shall
2874 // not specify an alignment that is less strict than the alignment that
2875 // would otherwise be required for the entity being declared.
2876 AlignedAttr *AlignasAttr = 0;
2877 unsigned Align = 0;
2878 for (specific_attr_iterator<AlignedAttr>
2879 I = D->specific_attr_begin<AlignedAttr>(),
2880 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
2881 if (I->isAlignmentDependent())
2882 return;
2883 if (I->isAlignas())
2884 AlignasAttr = *I;
2885 Align = std::max(Align, I->getAlignment(Context));
2886 }
2887
2888 if (AlignasAttr && Align) {
2889 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2890 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2891 if (NaturalAlign > RequestedAlign)
2892 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2893 << Ty << (unsigned)NaturalAlign.getQuantity();
2894 }
2895}
2896
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002897/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002898/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002899///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002900/// Despite what would be logical, the mode attribute is a decl attribute, not a
2901/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2902/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002903static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002904 // This attribute isn't documented, but glibc uses it. It changes
2905 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00002906 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00002907 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
2908 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002909 return;
2910 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002911
Aaron Ballman00e99962013-08-31 01:11:41 +00002912 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
2913 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002914
2915 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002916 if (Str.startswith("__") && Str.endswith("__"))
2917 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002918
2919 unsigned DestWidth = 0;
2920 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002921 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002922 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002923 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002924 switch (Str[0]) {
2925 case 'Q': DestWidth = 8; break;
2926 case 'H': DestWidth = 16; break;
2927 case 'S': DestWidth = 32; break;
2928 case 'D': DestWidth = 64; break;
2929 case 'X': DestWidth = 96; break;
2930 case 'T': DestWidth = 128; break;
2931 }
2932 if (Str[1] == 'F') {
2933 IntegerMode = false;
2934 } else if (Str[1] == 'C') {
2935 IntegerMode = false;
2936 ComplexMode = true;
2937 } else if (Str[1] != 'I') {
2938 DestWidth = 0;
2939 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002940 break;
2941 case 4:
2942 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2943 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002944 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002945 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002946 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002947 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002948 break;
2949 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002950 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002951 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002952 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002953 case 11:
2954 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00002955 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002956 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002957 }
2958
2959 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002960 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002961 OldTy = TD->getUnderlyingType();
2962 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2963 OldTy = VD->getType();
2964 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002965 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002966 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002967 return;
2968 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002969
John McCall9dd450b2009-09-21 23:43:11 +00002970 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002971 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2972 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002973 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002974 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2975 } else if (ComplexMode) {
2976 if (!OldTy->isComplexType())
2977 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2978 } else {
2979 if (!OldTy->isFloatingType())
2980 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2981 }
2982
Mike Stump87c57ac2009-05-16 07:39:55 +00002983 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2984 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002985 // FIXME: Make sure floating-point mappings are accurate
2986 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002987 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00002988 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002989 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002990 }
2991
2992 QualType NewTy;
2993
2994 if (IntegerMode)
2995 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
2996 OldTy->isSignedIntegerType());
2997 else
2998 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
2999
3000 if (NewTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00003001 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003002 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003003 }
3004
Eli Friedman4735374e2009-03-03 06:41:03 +00003005 if (ComplexMode) {
3006 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003007 }
3008
3009 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003010 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3011 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3012 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003013 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003014
3015 D->addAttr(::new (S.Context)
3016 ModeAttr(Attr.getRange(), S.Context, Name,
3017 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003018}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003019
Chandler Carruthedc2c642011-07-02 00:01:44 +00003020static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003021 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3022 if (!VD->hasGlobalStorage())
3023 S.Diag(Attr.getLoc(),
3024 diag::warn_attribute_requires_functions_or_static_globals)
3025 << Attr.getName();
3026 } else if (!isFunctionOrMethod(D)) {
3027 S.Diag(Attr.getLoc(),
3028 diag::warn_attribute_requires_functions_or_static_globals)
3029 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003030 return;
3031 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003032
Michael Han99315932013-01-24 16:46:58 +00003033 D->addAttr(::new (S.Context)
3034 NoDebugAttr(Attr.getRange(), S.Context,
3035 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003036}
3037
Chandler Carruthedc2c642011-07-02 00:01:44 +00003038static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003039 FunctionDecl *FD = cast<FunctionDecl>(D);
3040 if (!FD->getResultType()->isVoidType()) {
3041 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
3042 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
3043 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3044 << FD->getType()
3045 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
3046 "void");
3047 } else {
3048 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3049 << FD->getType();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003050 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003051 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003052 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003053
Aaron Ballman3aff6332013-12-02 19:30:36 +00003054 D->addAttr(::new (S.Context)
3055 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00003056 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003057}
3058
Chandler Carruthedc2c642011-07-02 00:01:44 +00003059static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003060 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003061 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003062 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003063 return;
3064 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003065
Michael Han99315932013-01-24 16:46:58 +00003066 D->addAttr(::new (S.Context)
3067 GNUInlineAttr(Attr.getRange(), S.Context,
3068 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003069}
3070
Chandler Carruthedc2c642011-07-02 00:01:44 +00003071static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003072 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003073
Aaron Ballman02df2e02012-12-09 17:45:41 +00003074 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003075 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003076 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3077 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003078 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003079 return;
3080
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003081 if (!isa<ObjCMethodDecl>(D)) {
3082 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3083 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003084 return;
3085 }
3086
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003087 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003088 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003089 D->addAttr(::new (S.Context)
3090 FastCallAttr(Attr.getRange(), S.Context,
3091 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003092 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003093 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003094 D->addAttr(::new (S.Context)
3095 StdCallAttr(Attr.getRange(), S.Context,
3096 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003097 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003098 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003099 D->addAttr(::new (S.Context)
3100 ThisCallAttr(Attr.getRange(), S.Context,
3101 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003102 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003103 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003104 D->addAttr(::new (S.Context)
3105 CDeclAttr(Attr.getRange(), S.Context,
3106 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003107 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003108 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003109 D->addAttr(::new (S.Context)
3110 PascalAttr(Attr.getRange(), S.Context,
3111 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003112 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003113 case AttributeList::AT_MSABI:
3114 D->addAttr(::new (S.Context)
3115 MSABIAttr(Attr.getRange(), S.Context,
3116 Attr.getAttributeSpellingListIndex()));
3117 return;
3118 case AttributeList::AT_SysVABI:
3119 D->addAttr(::new (S.Context)
3120 SysVABIAttr(Attr.getRange(), S.Context,
3121 Attr.getAttributeSpellingListIndex()));
3122 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003123 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003124 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003125 switch (CC) {
3126 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003127 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003128 break;
3129 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003130 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003131 break;
3132 default:
3133 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003134 }
3135
Michael Han99315932013-01-24 16:46:58 +00003136 D->addAttr(::new (S.Context)
3137 PcsAttr(Attr.getRange(), S.Context, PCS,
3138 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003139 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003140 }
Derek Schuffa2020962012-10-16 22:30:41 +00003141 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003142 D->addAttr(::new (S.Context)
3143 PnaclCallAttr(Attr.getRange(), S.Context,
3144 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003145 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003146 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003147 D->addAttr(::new (S.Context)
3148 IntelOclBiccAttr(Attr.getRange(), S.Context,
3149 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003150 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003151
Abramo Bagnara50099372010-04-30 13:10:51 +00003152 default:
3153 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003154 }
3155}
3156
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003157static void handleOpenCLImageAccessAttr(Sema &S, Decl *D,
3158 const AttributeList &Attr) {
3159 uint32_t ArgNum;
3160 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), ArgNum))
Guy Benyeifb36ede2013-03-24 13:58:12 +00003161 return;
Guy Benyeifb36ede2013-03-24 13:58:12 +00003162
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003163 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(Attr.getRange(),
3164 S.Context, ArgNum));
Guy Benyeifb36ede2013-03-24 13:58:12 +00003165}
3166
Aaron Ballman02df2e02012-12-09 17:45:41 +00003167bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3168 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003169 if (attr.isInvalid())
3170 return true;
3171
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003172 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003173 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003174 attr.setInvalid();
3175 return true;
3176 }
3177
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003178 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3179 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003180 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003181 case AttributeList::AT_CDecl: CC = CC_C; break;
3182 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3183 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3184 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3185 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003186 case AttributeList::AT_MSABI:
3187 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3188 CC_X86_64Win64;
3189 break;
3190 case AttributeList::AT_SysVABI:
3191 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3192 CC_C;
3193 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003194 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003195 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003196 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003197 attr.setInvalid();
3198 return true;
3199 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003200 if (StrRef == "aapcs") {
3201 CC = CC_AAPCS;
3202 break;
3203 } else if (StrRef == "aapcs-vfp") {
3204 CC = CC_AAPCS_VFP;
3205 break;
3206 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003207
3208 attr.setInvalid();
3209 Diag(attr.getLoc(), diag::err_invalid_pcs);
3210 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003211 }
Derek Schuffa2020962012-10-16 22:30:41 +00003212 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003213 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003214 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003215 }
3216
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003217 const TargetInfo &TI = Context.getTargetInfo();
3218 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3219 if (A == TargetInfo::CCCR_Warning) {
3220 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003221
3222 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3223 if (FD)
3224 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3225 TargetInfo::CCMT_NonMember;
3226 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003227 }
3228
John McCall3882ace2011-01-05 12:14:39 +00003229 return false;
3230}
3231
Chandler Carruthedc2c642011-07-02 00:01:44 +00003232static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003233 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003234
3235 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003236 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003237 return;
3238
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003239 if (!isa<ObjCMethodDecl>(D)) {
3240 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3241 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003242 return;
3243 }
Eli Friedman7044b762009-03-27 21:06:47 +00003244
Michael Han99315932013-01-24 16:46:58 +00003245 D->addAttr(::new (S.Context)
3246 RegparmAttr(Attr.getRange(), S.Context, numParams,
3247 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00003248}
3249
3250/// Checks a regparm attribute, returning true if it is ill-formed and
3251/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003252bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3253 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003254 return true;
3255
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003256 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003257 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003258 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003259 }
Eli Friedman7044b762009-03-27 21:06:47 +00003260
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003261 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003262 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003263 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003264 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003265 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003266 }
3267
Douglas Gregore8bbc122011-09-02 00:18:52 +00003268 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003269 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003270 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003271 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003272 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003273 }
3274
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003275 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003276 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003277 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003278 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003279 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003280 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003281 }
3282
John McCall3882ace2011-01-05 12:14:39 +00003283 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003284}
3285
Aaron Ballman66039932013-12-19 00:41:31 +00003286static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3287 const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003288 // check the attribute arguments.
3289 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
3290 // FIXME: 0 is not okay.
3291 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
3292 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003293 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003294
3295 if (!isFunctionOrMethod(D)) {
3296 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3297 << Attr.getName() << ExpectedFunctionOrMethod;
3298 return;
3299 }
3300
Aaron Ballman66039932013-12-19 00:41:31 +00003301 uint32_t MaxThreads, MinBlocks = 0;
3302 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1))
3303 return;
3304 if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr,
3305 Attr.getArgAsExpr(1),
3306 MinBlocks, 2))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003307 return;
3308
3309 D->addAttr(::new (S.Context)
3310 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3311 MaxThreads, MinBlocks,
3312 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003313}
3314
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003315static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3316 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003317 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003318 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003319 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003320 return;
3321 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003322
3323 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003324 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003325
Aaron Ballman00e99962013-08-31 01:11:41 +00003326 StringRef AttrName = Attr.getName()->getName();
3327 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003328
3329 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3330 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3331 << Attr.getName() << ExpectedFunctionOrMethod;
3332 return;
3333 }
3334
3335 uint64_t ArgumentIdx;
3336 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3337 Attr.getLoc(), 2,
Aaron Ballman00e99962013-08-31 01:11:41 +00003338 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003339 return;
3340
3341 uint64_t TypeTagIdx;
3342 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3343 Attr.getLoc(), 3,
Aaron Ballman00e99962013-08-31 01:11:41 +00003344 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003345 return;
3346
3347 bool IsPointer = (AttrName == "pointer_with_type_tag");
3348 if (IsPointer) {
3349 // Ensure that buffer has a pointer type.
3350 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3351 if (!BufferTy->isPointerType()) {
3352 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003353 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003354 }
3355 }
3356
Michael Han99315932013-01-24 16:46:58 +00003357 D->addAttr(::new (S.Context)
3358 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3359 ArgumentIdx, TypeTagIdx, IsPointer,
3360 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003361}
3362
3363static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3364 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003365 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003366 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003367 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003368 return;
3369 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003370
3371 if (!checkAttributeNumArgs(S, Attr, 1))
3372 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003373
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003374 if (!isa<VarDecl>(D)) {
3375 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3376 << Attr.getName() << ExpectedVariable;
3377 return;
3378 }
3379
Aaron Ballman00e99962013-08-31 01:11:41 +00003380 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003381 TypeSourceInfo *MatchingCTypeLoc = 0;
3382 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3383 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003384
Michael Han99315932013-01-24 16:46:58 +00003385 D->addAttr(::new (S.Context)
3386 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003387 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003388 Attr.getLayoutCompatible(),
3389 Attr.getMustBeNull(),
3390 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003391}
3392
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003393//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003394// Checker-specific attribute handlers.
3395//===----------------------------------------------------------------------===//
3396
John McCalled433932011-01-25 03:31:58 +00003397static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003398 return type->isDependentType() ||
3399 type->isObjCObjectPointerType() ||
3400 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003401}
3402static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003403 return type->isDependentType() ||
3404 type->isPointerType() ||
3405 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003406}
3407
Chandler Carruthedc2c642011-07-02 00:01:44 +00003408static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003409 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003410 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003411
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003412 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003413 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3414 cf = false;
3415 } else {
3416 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3417 cf = true;
3418 }
3419
3420 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003421 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003422 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003423 return;
3424 }
3425
3426 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003427 param->addAttr(::new (S.Context)
3428 CFConsumedAttr(Attr.getRange(), S.Context,
3429 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003430 else
Michael Han99315932013-01-24 16:46:58 +00003431 param->addAttr(::new (S.Context)
3432 NSConsumedAttr(Attr.getRange(), S.Context,
3433 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003434}
3435
Chandler Carruthedc2c642011-07-02 00:01:44 +00003436static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3437 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003438
John McCalled433932011-01-25 03:31:58 +00003439 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003440
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003441 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003442 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003443 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003444 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003445 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003446 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3447 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003448 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003449 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003450 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003451 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003452 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003453 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003454 return;
3455 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003456
John McCalled433932011-01-25 03:31:58 +00003457 bool typeOK;
3458 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003459 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003460 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003461 case AttributeList::AT_NSReturnsAutoreleased:
3462 case AttributeList::AT_NSReturnsRetained:
3463 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003464 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3465 cf = false;
3466 break;
3467
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003468 case AttributeList::AT_CFReturnsRetained:
3469 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003470 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3471 cf = true;
3472 break;
3473 }
3474
3475 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003476 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003477 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003478 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003479 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003480
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003481 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003482 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003483 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003484 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003485 D->addAttr(::new (S.Context)
3486 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3487 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003488 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003489 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003490 D->addAttr(::new (S.Context)
3491 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3492 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003493 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003494 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003495 D->addAttr(::new (S.Context)
3496 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3497 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003498 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003499 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003500 D->addAttr(::new (S.Context)
3501 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3502 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003503 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003504 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003505 D->addAttr(::new (S.Context)
3506 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3507 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003508 return;
3509 };
3510}
3511
John McCallcf166702011-07-22 08:53:00 +00003512static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3513 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003514 const int EP_ObjCMethod = 1;
3515 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003516
John McCallcf166702011-07-22 08:53:00 +00003517 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003518 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003519 if (isa<ObjCMethodDecl>(D))
3520 resultType = cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003521 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003522 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003523
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003524 if (!resultType->isReferenceType() &&
3525 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003526 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003527 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003528 << attr.getName()
3529 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003530 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003531
3532 // Drop the attribute.
3533 return;
3534 }
3535
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003536 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003537 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3538 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003539}
3540
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003541static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3542 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003543 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003544
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003545 DeclContext *DC = method->getDeclContext();
3546 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3547 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3548 << attr.getName() << 0;
3549 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3550 return;
3551 }
3552 if (method->getMethodFamily() == OMF_dealloc) {
3553 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3554 << attr.getName() << 1;
3555 return;
3556 }
3557
Michael Han99315932013-01-24 16:46:58 +00003558 method->addAttr(::new (S.Context)
3559 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3560 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003561}
3562
Aaron Ballmanfb763042013-12-02 18:05:46 +00003563static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3564 const AttributeList &Attr) {
3565 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr,
3566 "cf_unknown_transfer"))
John McCall32f5fe12011-09-30 05:12:12 +00003567 return;
John McCall32f5fe12011-09-30 05:12:12 +00003568
Aaron Ballmanfb763042013-12-02 18:05:46 +00003569 D->addAttr(::new (S.Context)
3570 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3571 Attr.getAttributeSpellingListIndex()));
3572}
3573
3574static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3575 const AttributeList &Attr) {
3576 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr,
3577 "cf_audited_transfer"))
3578 return;
3579
3580 D->addAttr(::new (S.Context)
3581 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3582 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003583}
3584
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003585static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3586 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003587 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003588
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003589 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003590 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003591 return;
3592 }
3593
3594 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003595 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003596 Attr.getAttributeSpellingListIndex()));
3597}
3598
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003599static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3600 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003601 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003602
3603 if (!Parm) {
3604 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3605 return;
3606 }
3607
3608 D->addAttr(::new (S.Context)
3609 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3610 Attr.getAttributeSpellingListIndex()));
3611}
3612
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003613static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3614 const AttributeList &Attr) {
3615 IdentifierInfo *RelatedClass =
3616 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0;
3617 if (!RelatedClass) {
3618 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3619 return;
3620 }
3621 IdentifierInfo *ClassMethod =
3622 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0;
3623 IdentifierInfo *InstanceMethod =
3624 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0;
3625 D->addAttr(::new (S.Context)
3626 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3627 ClassMethod, InstanceMethod,
3628 Attr.getAttributeSpellingListIndex()));
3629}
3630
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003631static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3632 const AttributeList &Attr) {
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003633 ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003634 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003635 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003636 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3637 Attr.getAttributeSpellingListIndex()));
3638}
3639
Chandler Carruthedc2c642011-07-02 00:01:44 +00003640static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3641 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003642 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003643
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003644 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003645 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003646}
3647
Chandler Carruthedc2c642011-07-02 00:01:44 +00003648static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3649 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003650 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003651 QualType type = vd->getType();
3652
3653 if (!type->isDependentType() &&
3654 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003655 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003656 << type;
3657 return;
3658 }
3659
3660 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3661
3662 // If we have no lifetime yet, check the lifetime we're presumably
3663 // going to infer.
3664 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3665 lifetime = type->getObjCARCImplicitLifetime();
3666
3667 switch (lifetime) {
3668 case Qualifiers::OCL_None:
3669 assert(type->isDependentType() &&
3670 "didn't infer lifetime for non-dependent type?");
3671 break;
3672
3673 case Qualifiers::OCL_Weak: // meaningful
3674 case Qualifiers::OCL_Strong: // meaningful
3675 break;
3676
3677 case Qualifiers::OCL_ExplicitNone:
3678 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003679 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003680 << (lifetime == Qualifiers::OCL_Autoreleasing);
3681 break;
3682 }
3683
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003684 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003685 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3686 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003687}
3688
Francois Picheta83957a2010-12-19 06:50:37 +00003689//===----------------------------------------------------------------------===//
3690// Microsoft specific attribute handlers.
3691//===----------------------------------------------------------------------===//
3692
Chandler Carruthedc2c642011-07-02 00:01:44 +00003693static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003694 if (!S.LangOpts.CPlusPlus) {
3695 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3696 << Attr.getName() << AttributeLangSupport::C;
3697 return;
3698 }
3699
Aaron Ballman60e705e2013-11-24 20:58:02 +00003700 if (!isa<CXXRecordDecl>(D)) {
3701 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3702 << Attr.getName() << ExpectedClass;
3703 return;
3704 }
3705
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003706 StringRef StrRef;
3707 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003708 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003709 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003710
David Majnemer89085342013-08-09 08:56:20 +00003711 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3712 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003713 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3714 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003715
Reid Kleckner140c4a72013-05-17 14:04:52 +00003716 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003717 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003718 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003719 return;
3720 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003721
David Majnemer89085342013-08-09 08:56:20 +00003722 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003723 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003724 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003725 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003726 return;
3727 }
David Majnemer89085342013-08-09 08:56:20 +00003728 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003729 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003730 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003731 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003732 }
Francois Picheta83957a2010-12-19 06:50:37 +00003733
David Majnemer89085342013-08-09 08:56:20 +00003734 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3735 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003736}
3737
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003738/// Handles semantic checking for features that are common to all attributes,
3739/// such as checking whether a parameter was properly specified, or the correct
3740/// number of arguments were passed, etc.
3741static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
3742 const AttributeList &Attr) {
3743 // Several attributes carry different semantics than the parsing requires, so
3744 // those are opted out of the common handling.
3745 //
3746 // We also bail on unknown and ignored attributes because those are handled
3747 // as part of the target-specific handling logic.
3748 if (Attr.hasCustomParsing() ||
3749 Attr.getKind() == AttributeList::UnknownAttribute ||
3750 Attr.getKind() == AttributeList::IgnoredAttribute)
3751 return false;
3752
Aaron Ballman3aff6332013-12-02 19:30:36 +00003753 // Check whether the attribute requires specific language extensions to be
3754 // enabled.
3755 if (!Attr.diagnoseLangOpts(S))
3756 return true;
3757
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003758 // If there are no optional arguments, then checking for the argument count
3759 // is trivial.
3760 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
3761 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
3762 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003763
3764 // Check whether the attribute appertains to the given subject.
3765 if (!Attr.diagnoseAppertainsTo(S, D))
3766 return true;
3767
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003768 return false;
3769}
3770
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003771//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003772// Top Level Sema Entry Points
3773//===----------------------------------------------------------------------===//
3774
Richard Smithf8a75c32013-08-29 00:47:48 +00003775/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3776/// the attribute applies to decls. If the attribute is a type attribute, just
3777/// silently ignore it if a GNU attribute.
3778static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3779 const AttributeList &Attr,
3780 bool IncludeCXX11Attributes) {
3781 if (Attr.isInvalid())
3782 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003783
Richard Smithf8a75c32013-08-29 00:47:48 +00003784 // Ignore C++11 attributes on declarator chunks: they appertain to the type
3785 // instead.
3786 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
3787 return;
3788
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003789 if (handleCommonAttributeFeatures(S, scope, D, Attr))
3790 return;
3791
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003792 switch (Attr.getKind()) {
Aaron Ballman9beb5172013-12-02 15:13:14 +00003793 case AttributeList::AT_IBAction:
3794 handleSimpleAttribute<IBActionAttr>(S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003795 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
3796 case AttributeList::AT_IBOutletCollection:
3797 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003798 case AttributeList::AT_AddressSpace:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003799 case AttributeList::AT_ObjCGC:
3800 case AttributeList::AT_VectorSize:
3801 case AttributeList::AT_NeonVectorType:
3802 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballman317a77f2013-05-22 23:25:32 +00003803 case AttributeList::AT_Ptr32:
3804 case AttributeList::AT_Ptr64:
3805 case AttributeList::AT_SPtr:
3806 case AttributeList::AT_UPtr:
Mike Stumpd3bb5572009-07-24 19:02:52 +00003807 // Ignore these, these are type attributes, handled by
3808 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003809 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003810 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
3811 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003812 case AttributeList::AT_AlwaysInline:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003813 handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003814 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003815 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00003816 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003817 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
3818 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
3819 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00003820 handleDependencyAttr(S, scope, D, Attr);
3821 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003822 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003823 case AttributeList::AT_CUDAConstant:
3824 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003825 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003826 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman3a8e2d92013-11-27 18:53:58 +00003827 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003828 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00003829 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00003830 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003831 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
3832 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003833 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003834 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00003835 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003836 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00003837 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003838 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
3839 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
3840 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00003841 case AttributeList::AT_CUDADevice:
3842 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003843 case AttributeList::AT_CUDAHost:
3844 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003845 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
3846 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003847 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00003848 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003849 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003850 case AttributeList::AT_MayAlias:
3851 handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00003852 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003853 case AttributeList::AT_NoCommon:
3854 handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003855 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003856 case AttributeList::AT_Overloadable:
3857 handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break;
Richard Smith852e9ce2013-11-27 01:46:48 +00003858 case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003859 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
3860 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003861 case AttributeList::AT_Naked:
3862 handleSimpleAttribute<NakedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003863 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00003864 case AttributeList::AT_NoThrow:
3865 handleSimpleAttribute<NoThrowAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003866 case AttributeList::AT_CUDAShared:
3867 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003868 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003869
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003870 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003871 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003872 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003873 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003874
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003875 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00003876 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3877
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003878 case AttributeList::AT_ObjCRequiresSuper:
3879 handleObjCRequiresSuperAttr(S, D, Attr); break;
3880
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003881 case AttributeList::AT_ObjCBridge:
3882 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003883
3884 case AttributeList::AT_ObjCBridgeMutable:
3885 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003886
3887 case AttributeList::AT_ObjCBridgeRelated:
3888 handleObjCBridgeRelatedAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00003889
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003890 case AttributeList::AT_ObjCDesignatedInitializer:
3891 handleObjCDesignatedInitializer(S, D, Attr); break;
3892
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003893 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00003894 handleCFAuditedTransferAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003895 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00003896 handleCFUnknownTransferAttr(S, D, Attr); break;
John McCall32f5fe12011-09-30 05:12:12 +00003897
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003898 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003899 case AttributeList::AT_CFConsumed:
3900 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
3901 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003902 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00003903
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003904 case AttributeList::AT_NSReturnsAutoreleased:
3905 case AttributeList::AT_NSReturnsNotRetained:
3906 case AttributeList::AT_CFReturnsNotRetained:
3907 case AttributeList::AT_NSReturnsRetained:
3908 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003909 handleNSReturnsRetainedAttr(S, D, Attr); break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00003910 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00003911 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003912 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00003913 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); break;
Joey Goulyaba589c2013-03-08 09:42:32 +00003914 case AttributeList::AT_VecTypeHint:
3915 handleVecTypeHint(S, D, Attr); break;
3916
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003917 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003918 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003919
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003920 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
3921 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
3922 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00003923 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00003924 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003925 case AttributeList::AT_ArcWeakrefUnavailable:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003926 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003927 case AttributeList::AT_ObjCRootClass:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003928 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00003929 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek28eace62013-11-23 01:01:34 +00003930 handleObjCSuppresProtocolAttr(S, D, Attr);
3931 break;
3932 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003933 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003934 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
3935 case AttributeList::AT_ReturnsTwice:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003936 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003937 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00003938 case AttributeList::AT_Visibility:
3939 handleVisibilityAttr(S, D, Attr, false);
3940 break;
3941 case AttributeList::AT_TypeVisibility:
3942 handleVisibilityAttr(S, D, Attr, true);
3943 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00003944 case AttributeList::AT_WarnUnused:
Aaron Ballman6a42b5a2013-11-27 16:59:17 +00003945 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003946 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00003947 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00003948 case AttributeList::AT_Weak:
3949 handleSimpleAttribute<WeakAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003950 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
3951 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
3952 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003953 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003954 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003955 case AttributeList::AT_ObjCException:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003956 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003957 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003958 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00003959 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003960 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
3961 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
3962 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00003963 case AttributeList::AT_Const:
3964 handleSimpleAttribute<ConstAttr>(S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003965 case AttributeList::AT_Pure:
3966 handleSimpleAttribute<PureAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003967 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
3968 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003969 case AttributeList::AT_NoInline:
3970 handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003971 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003972 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00003973 // Just ignore
3974 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003975 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003976 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003977 case AttributeList::AT_StdCall:
3978 case AttributeList::AT_CDecl:
3979 case AttributeList::AT_FastCall:
3980 case AttributeList::AT_ThisCall:
3981 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00003982 case AttributeList::AT_MSABI:
3983 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003984 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00003985 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00003986 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003987 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00003988 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003989 case AttributeList::AT_OpenCLKernel:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003990 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00003991 case AttributeList::AT_OpenCLImageAccess:
3992 handleOpenCLImageAccessAttr(S, D, Attr);
3993 break;
John McCall8d32c052012-05-22 21:28:12 +00003994
3995 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003996 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003997 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00003998 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003999 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004000 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004001 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004002 case AttributeList::AT_MSInheritance:
4003 handleSimpleAttribute<MSInheritanceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004004 case AttributeList::AT_ForceInline:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004005 handleSimpleAttribute<ForceInlineAttr>(S, D, Attr); break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004006 case AttributeList::AT_SelectAny:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004007 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004008
4009 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004010 case AttributeList::AT_AssertExclusiveLock:
4011 handleAssertExclusiveLockAttr(S, D, Attr);
4012 break;
4013 case AttributeList::AT_AssertSharedLock:
4014 handleAssertSharedLockAttr(S, D, Attr);
4015 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004016 case AttributeList::AT_GuardedVar:
Aaron Ballmane61b8b82013-12-02 15:02:49 +00004017 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004018 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004019 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004020 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004021 case AttributeList::AT_ScopedLockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004022 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004023 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004024 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004025 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004026 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004027 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004028 break;
4029 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004030 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004031 break;
4032 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004033 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004034 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004035 case AttributeList::AT_Lockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004036 handleSimpleAttribute<LockableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004037 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004038 handleGuardedByAttr(S, D, Attr);
4039 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004040 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004041 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004042 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004043 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004044 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004045 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004046 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004047 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004048 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004049 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004050 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004051 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004052 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004053 handleLockReturnedAttr(S, D, Attr);
4054 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004055 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004056 handleLocksExcludedAttr(S, D, Attr);
4057 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004058 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004059 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004060 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004061 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004062 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004063 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004064 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004065 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004066 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004067 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004068 handleUnlockFunAttr(S, D, Attr);
4069 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004070 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004071 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004072 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004073 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004074 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004075 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004076
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004077 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004078 case AttributeList::AT_Consumable:
4079 handleConsumableAttr(S, D, Attr);
4080 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004081 case AttributeList::AT_CallableWhen:
4082 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004083 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004084 case AttributeList::AT_ParamTypestate:
4085 handleParamTypestateAttr(S, D, Attr);
4086 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004087 case AttributeList::AT_ReturnTypestate:
4088 handleReturnTypestateAttr(S, D, Attr);
4089 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004090 case AttributeList::AT_SetTypestate:
4091 handleSetTypestateAttr(S, D, Attr);
4092 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004093 case AttributeList::AT_TestTypestate:
4094 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004095 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004096
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004097 // Type safety attributes.
4098 case AttributeList::AT_ArgumentWithTypeTag:
4099 handleArgumentWithTypeTagAttr(S, D, Attr);
4100 break;
4101 case AttributeList::AT_TypeTagForDatatype:
4102 handleTypeTagForDatatypeAttr(S, D, Attr);
4103 break;
4104
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004105 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004106 // Ask target about the attribute.
4107 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4108 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004109 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4110 diag::warn_unhandled_ms_attribute_ignored :
4111 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004112 break;
4113 }
4114}
4115
4116/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4117/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004118void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004119 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004120 bool IncludeCXX11Attributes) {
4121 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004122 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004123
Joey Gouly2cd9db12013-12-13 16:15:28 +00004124 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004125 // GCC accepts
4126 // static int a9 __attribute__((weakref));
4127 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004128 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004129 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00004130 cast<NamedDecl>(D)->getNameAsString();
4131 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004132 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004133 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004134
4135 if (!D->hasAttr<OpenCLKernelAttr>()) {
4136 // These attributes cannot be applied to a non-kernel function.
4137 if (D->hasAttr<ReqdWorkGroupSizeAttr>()) {
4138 Diag(D->getLocation(), diag::err_opencl_kernel_attr)
4139 << "reqd_work_group_size";
4140 D->setInvalidDecl();
4141 }
4142 if (D->hasAttr<WorkGroupSizeHintAttr>()) {
4143 Diag(D->getLocation(), diag::err_opencl_kernel_attr)
4144 << "work_group_size_hint";
4145 D->setInvalidDecl();
4146 }
4147 if (D->hasAttr<VecTypeHintAttr>()) {
4148 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << "vec_type_hint";
4149 D->setInvalidDecl();
4150 }
4151 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004152}
4153
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004154// Annotation attributes are the only attributes allowed after an access
4155// specifier.
4156bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4157 const AttributeList *AttrList) {
4158 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004159 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004160 handleAnnotateAttr(*this, ASDecl, *l);
4161 } else {
4162 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4163 return true;
4164 }
4165 }
4166
4167 return false;
4168}
4169
John McCall42856de2011-10-01 05:17:03 +00004170/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4171/// contains any decl attributes that we should warn about.
4172static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4173 for ( ; A; A = A->getNext()) {
4174 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004175 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004176 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4177
4178 if (A->getKind() == AttributeList::UnknownAttribute) {
4179 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4180 << A->getName() << A->getRange();
4181 } else {
4182 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4183 << A->getName() << A->getRange();
4184 }
4185 }
4186}
4187
4188/// checkUnusedDeclAttributes - Given a declarator which is not being
4189/// used to build a declaration, complain about any decl attributes
4190/// which might be lying around on it.
4191void Sema::checkUnusedDeclAttributes(Declarator &D) {
4192 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4193 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4194 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4195 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4196}
4197
Ryan Flynn7d470f32009-07-30 03:15:39 +00004198/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004199/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004200NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4201 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004202 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004203 NamedDecl *NewD = 0;
4204 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004205 FunctionDecl *NewFD;
4206 // FIXME: Missing call to CheckFunctionDeclaration().
4207 // FIXME: Mangling?
4208 // FIXME: Is the qualifier info correct?
4209 // FIXME: Is the DeclContext correct?
4210 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4211 Loc, Loc, DeclarationName(II),
4212 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004213 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004214 FD->hasPrototype(),
4215 false/*isConstexprSpecified*/);
4216 NewD = NewFD;
4217
4218 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004219 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004220
4221 // Fake up parameter variables; they are declared as if this were
4222 // a typedef.
4223 QualType FDTy = FD->getType();
4224 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4225 SmallVector<ParmVarDecl*, 16> Params;
4226 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4227 AE = FT->arg_type_end(); AI != AE; ++AI) {
4228 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4229 Param->setScopeInfo(0, Params.size());
4230 Params.push_back(Param);
4231 }
David Blaikie9c70e042011-09-21 18:16:56 +00004232 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004233 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004234 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4235 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004236 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004237 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004238 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004239 if (VD->getQualifier()) {
4240 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004241 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004242 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004243 }
4244 return NewD;
4245}
4246
James Dennett634962f2012-06-14 21:40:34 +00004247/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004248/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004249void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004250 if (W.getUsed()) return; // only do this once
4251 W.setUsed(true);
4252 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4253 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004254 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004255 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4256 NDId->getName()));
4257 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00004258 WeakTopLevelDecl.push_back(NewD);
4259 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4260 // to insert Decl at TU scope, sorry.
4261 DeclContext *SavedContext = CurContext;
4262 CurContext = Context.getTranslationUnitDecl();
4263 PushOnScopeChains(NewD, S);
4264 CurContext = SavedContext;
4265 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004266 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004267 }
4268}
4269
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004270void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4271 // It's valid to "forward-declare" #pragma weak, in which case we
4272 // have to do this.
4273 LoadExternalWeakUndeclaredIdentifiers();
4274 if (!WeakUndeclaredIdentifiers.empty()) {
4275 NamedDecl *ND = NULL;
4276 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4277 if (VD->isExternC())
4278 ND = VD;
4279 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4280 if (FD->isExternC())
4281 ND = FD;
4282 if (ND) {
4283 if (IdentifierInfo *Id = ND->getIdentifier()) {
4284 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4285 = WeakUndeclaredIdentifiers.find(Id);
4286 if (I != WeakUndeclaredIdentifiers.end()) {
4287 WeakInfo W = I->second;
4288 DeclApplyPragmaWeak(S, ND, W);
4289 WeakUndeclaredIdentifiers[Id] = W;
4290 }
4291 }
4292 }
4293 }
4294}
4295
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004296/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4297/// it, apply them to D. This is a bit tricky because PD can have attributes
4298/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004299void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004300 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004301 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004302 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004303
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004304 // Walk the declarator structure, applying decl attributes that were in a type
4305 // position to the decl itself. This handles cases like:
4306 // int *__attr__(x)** D;
4307 // when X is a decl attribute.
4308 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4309 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004310 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004311
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004312 // Finally, apply any attributes on the decl itself.
4313 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004314 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004315}
John McCall28a6aea2009-11-04 02:18:39 +00004316
John McCall31168b02011-06-15 23:02:42 +00004317/// Is the given declaration allowed to use a forbidden type?
4318static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4319 // Private ivars are always okay. Unfortunately, people don't
4320 // always properly make their ivars private, even in system headers.
4321 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004322 // Function declarations in sys headers will be marked unavailable.
4323 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4324 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004325 return false;
4326
4327 // Require it to be declared in a system header.
4328 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4329}
4330
4331/// Handle a delayed forbidden-type diagnostic.
4332static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4333 Decl *decl) {
4334 if (decl && isForbiddenTypeAllowed(S, decl)) {
4335 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4336 "this system declaration uses an unsupported type"));
4337 return;
4338 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004339 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004340 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004341 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004342 // kind of forbidden type messages on unavailable functions.
4343 if (FD->hasAttr<UnavailableAttr>() &&
4344 diag.getForbiddenTypeDiagnostic() ==
4345 diag::err_arc_array_param_no_ownership) {
4346 diag.Triggered = true;
4347 return;
4348 }
4349 }
John McCall31168b02011-06-15 23:02:42 +00004350
4351 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4352 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4353 diag.Triggered = true;
4354}
4355
John McCall2ec85372012-05-07 06:16:41 +00004356void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4357 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004358 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004359 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004360
John McCall2ec85372012-05-07 06:16:41 +00004361 // When delaying diagnostics to run in the context of a parsed
4362 // declaration, we only want to actually emit anything if parsing
4363 // succeeds.
4364 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004365
John McCall2ec85372012-05-07 06:16:41 +00004366 // We emit all the active diagnostics in this pool or any of its
4367 // parents. In general, we'll get one pool for the decl spec
4368 // and a child pool for each declarator; in a decl group like:
4369 // deprecated_typedef foo, *bar, baz();
4370 // only the declarator pops will be passed decls. This is correct;
4371 // we really do need to consider delayed diagnostics from the decl spec
4372 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004373 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004374 do {
John McCall6347b682012-05-07 06:16:58 +00004375 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004376 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4377 // This const_cast is a bit lame. Really, Triggered should be mutable.
4378 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004379 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004380 continue;
4381
John McCallc1465822011-02-14 07:13:47 +00004382 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004383 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00004384 case DelayedDiagnostic::Unavailable:
4385 // Don't bother giving deprecation/unavailable diagnostics if
4386 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00004387 if (!decl->isInvalidDecl())
Ted Kremenekb79ee572013-12-18 23:30:06 +00004388 HandleDelayedAvailabilityCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004389 break;
4390
4391 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004392 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004393 break;
John McCall31168b02011-06-15 23:02:42 +00004394
4395 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004396 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004397 break;
John McCall86121512010-01-27 03:50:35 +00004398 }
4399 }
John McCall2ec85372012-05-07 06:16:41 +00004400 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004401}
4402
John McCall6347b682012-05-07 06:16:58 +00004403/// Given a set of delayed diagnostics, re-emit them as if they had
4404/// been delayed in the current context instead of in the given pool.
4405/// Essentially, this just moves them to the current pool.
4406void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4407 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4408 assert(curPool && "re-emitting in undelayed context not supported");
4409 curPool->steal(pool);
4410}
4411
John McCall28a6aea2009-11-04 02:18:39 +00004412static bool isDeclDeprecated(Decl *D) {
4413 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004414 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004415 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004416 // A category implicitly has the availability of the interface.
4417 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4418 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004419 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4420 return false;
4421}
4422
Ted Kremenekb79ee572013-12-18 23:30:06 +00004423static bool isDeclUnavailable(Decl *D) {
4424 do {
4425 if (D->isUnavailable())
4426 return true;
4427 // A category implicitly has the availability of the interface.
4428 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4429 return CatD->getClassInterface()->isUnavailable();
4430 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4431 return false;
4432}
4433
Eli Friedman971bfa12012-08-08 21:52:41 +00004434static void
Ted Kremenekb79ee572013-12-18 23:30:06 +00004435DoEmitAvailabilityWarning(Sema &S,
4436 DelayedDiagnostic::DDKind K,
4437 Decl *Ctx,
4438 const NamedDecl *D,
4439 StringRef Message,
4440 SourceLocation Loc,
4441 const ObjCInterfaceDecl *UnknownObjCClass,
4442 const ObjCPropertyDecl *ObjCProperty) {
4443
4444 // Diagnostics for deprecated or unavailable.
4445 unsigned diag, diag_message, diag_fwdclass_message;
4446
4447 // Matches 'diag::note_property_attribute' options.
4448 unsigned property_note_select;
4449
4450 // Matches diag::note_availability_specified_here.
4451 unsigned available_here_select_kind;
4452
4453 // Don't warn if our current context is deprecated or unavailable.
4454 switch (K) {
4455 case DelayedDiagnostic::Deprecation:
4456 if (isDeclDeprecated(Ctx))
4457 return;
4458 diag = diag::warn_deprecated;
4459 diag_message = diag::warn_deprecated_message;
4460 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
4461 property_note_select = /* deprecated */ 0;
4462 available_here_select_kind = /* deprecated */ 2;
4463 break;
4464
4465 case DelayedDiagnostic::Unavailable:
4466 if (isDeclUnavailable(Ctx))
4467 return;
4468 diag = diag::err_unavailable;
4469 diag_message = diag::err_unavailable_message;
4470 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
4471 property_note_select = /* unavailable */ 1;
4472 available_here_select_kind = /* unavailable */ 0;
4473 break;
4474
4475 default:
4476 llvm_unreachable("Neither a deprecation or unavailable kind");
4477 }
4478
Eli Friedman971bfa12012-08-08 21:52:41 +00004479 DeclarationName Name = D->getDeclName();
4480 if (!Message.empty()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004481 S.Diag(Loc, diag_message) << Name << Message;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004482 if (ObjCProperty)
4483 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4484 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004485 } else if (!UnknownObjCClass) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004486 S.Diag(Loc, diag) << Name;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004487 if (ObjCProperty)
4488 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4489 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004490 } else {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004491 S.Diag(Loc, diag_fwdclass_message) << Name;
Eli Friedman971bfa12012-08-08 21:52:41 +00004492 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4493 }
Ted Kremenekb79ee572013-12-18 23:30:06 +00004494
4495 S.Diag(D->getLocation(), diag::note_availability_specified_here)
4496 << D << available_here_select_kind;
Eli Friedman971bfa12012-08-08 21:52:41 +00004497}
4498
Ted Kremenekb79ee572013-12-18 23:30:06 +00004499void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD,
4500 Decl *Ctx) {
John McCall86121512010-01-27 03:50:35 +00004501 DD.Triggered = true;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004502 DoEmitAvailabilityWarning(*this,
4503 (DelayedDiagnostic::DDKind) DD.Kind,
4504 Ctx,
4505 DD.getDeprecationDecl(),
4506 DD.getDeprecationMessage(),
4507 DD.Loc,
4508 DD.getUnknownObjCClass(),
4509 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004510}
4511
Ted Kremenekb79ee572013-12-18 23:30:06 +00004512void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
4513 NamedDecl *D, StringRef Message,
4514 SourceLocation Loc,
4515 const ObjCInterfaceDecl *UnknownObjCClass,
4516 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004517 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004518 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004519 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D,
4520 UnknownObjCClass,
4521 ObjCProperty,
4522 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004523 return;
4524 }
4525
Ted Kremenekb79ee572013-12-18 23:30:06 +00004526 Decl *Ctx = cast<Decl>(getCurLexicalContext());
4527 DelayedDiagnostic::DDKind K;
4528 switch (AD) {
4529 case AD_Deprecation:
4530 K = DelayedDiagnostic::Deprecation;
4531 break;
4532 case AD_Unavailable:
4533 K = DelayedDiagnostic::Unavailable;
4534 break;
4535 }
4536
4537 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
4538 UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004539}