blob: 8a9806586d9008692bd525d134ec7930d99f1a50 [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) {
Aaron Ballman05e420a2014-01-02 21:26:14 +0000214 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments)
215 << Attr.getName() << Num;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000216 return false;
217 }
218
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000219 return true;
220}
221
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000222/// \brief If Expr is a valid integer constant, get the value of the integer
223/// expression and return success or failure. May output an error.
224static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
225 const Expr *Expr, uint32_t &Val,
226 unsigned Idx = UINT_MAX) {
227 llvm::APSInt I(32);
228 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
229 !Expr->isIntegerConstantExpr(I, S.Context)) {
230 if (Idx != UINT_MAX)
231 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
232 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
233 << Expr->getSourceRange();
234 else
235 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
236 << Attr.getName() << AANT_ArgumentIntegerConstant
237 << Expr->getSourceRange();
238 return false;
239 }
240 Val = (uint32_t)I.getZExtValue();
241 return true;
242}
243
Aaron Ballmanfb763042013-12-02 18:05:46 +0000244/// \brief Diagnose mutually exclusive attributes when present on a given
245/// declaration. Returns true if diagnosed.
246template <typename AttrTy>
247static bool checkAttrMutualExclusion(Sema &S, Decl *D,
248 const AttributeList &Attr,
249 const char *OtherName) {
250 // FIXME: it would be nice if OtherName did not have to be passed in, but was
251 // instead determined based on the AttrTy template parameter.
252 if (D->hasAttr<AttrTy>()) {
253 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
254 << Attr.getName() << OtherName;
255 return true;
256 }
257 return false;
258}
259
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000260/// \brief Check if IdxExpr is a valid argument index for a function or
261/// instance method D. May output an error.
262///
263/// \returns true if IdxExpr is a valid index.
264static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000265 const AttributeList &Attr,
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000266 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 Ballmanfaed0fa2013-12-26 16:30:30 +0000283 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
284 << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant
285 << 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)) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000291 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
292 << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000293 return false;
294 }
295 Idx--; // Convert to zero-based.
296 if (HasImplicitThisParam) {
297 if (Idx == 0) {
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000298 S.Diag(Attr.getLoc(),
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000299 diag::err_attribute_invalid_implicit_this_argument)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +0000300 << Attr.getName() << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000301 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;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00001189 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001190 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001191
1192 // Is the function argument a pointer type?
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001193 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001194 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001195
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001196 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001197 // FIXME: Should also highlight argument in decl.
Aaron Ballmancedaaea2013-12-26 17:07:49 +00001198 S.Diag(Attr.getLoc(), diag::warn_attribute_pointers_only)
1199 << Attr.getName() << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001200 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001201 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001202
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001203 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001204 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001205
1206 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1207 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001208 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001209 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1210 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001211 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001212 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001213 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001214 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001215
Ted Kremenek22813f42010-10-21 18:49:36 +00001216 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001217 if (NonNullArgs.empty()) {
1218 // Warn the trivial case only if attribute is not coming from a
1219 // macro instantiation.
1220 if (Attr.getLoc().isFileID())
1221 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001222 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001223 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001224 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001225
Nick Lewyckye1121512013-01-24 01:12:16 +00001226 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001227 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001228 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001229 D->addAttr(::new (S.Context)
1230 NonNullAttr(Attr.getRange(), S.Context, start, size,
1231 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001232}
1233
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001234static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1235 switch (K) {
1236 case OwnershipAttr::Holds: return "'ownership_holds'";
1237 case OwnershipAttr::Takes: return "'ownership_takes'";
1238 case OwnershipAttr::Returns: return "'ownership_returns'";
1239 }
1240 llvm_unreachable("unknown ownership");
1241}
1242
Chandler Carruthedc2c642011-07-02 00:01:44 +00001243static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001244 // This attribute must be applied to a function declaration. The first
1245 // argument to the attribute must be an identifier, the name of the resource,
1246 // for example: malloc. The following arguments must be argument indexes, the
1247 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001248 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001249 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001250 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001251
Aaron Ballman00e99962013-08-31 01:11:41 +00001252 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001253 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001254 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001255 return;
1256 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001257
Richard Smith852e9ce2013-11-27 01:46:48 +00001258 // Figure out our Kind.
1259 OwnershipAttr::OwnershipKind K =
1260 OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0,
1261 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001262
Richard Smith852e9ce2013-11-27 01:46:48 +00001263 // Check arguments.
1264 switch (K) {
1265 case OwnershipAttr::Takes:
1266 case OwnershipAttr::Holds:
1267 if (AL.getNumArgs() < 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001268 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments)
1269 << AL.getName() << 2;
Richard Smith852e9ce2013-11-27 01:46:48 +00001270 return;
1271 }
1272 break;
1273 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001274 if (AL.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001275 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments)
1276 << AL.getName() << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001277 return;
1278 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001279 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001280 }
1281
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001282 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001283 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1284 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001285 return;
1286 }
1287
Richard Smith852e9ce2013-11-27 01:46:48 +00001288 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001289
1290 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001291 StringRef ModuleName = Module->getName();
1292 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1293 ModuleName.size() > 4) {
1294 ModuleName = ModuleName.drop_front(2).drop_back(2);
1295 Module = &S.PP.getIdentifierTable().get(ModuleName);
1296 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001297
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001298 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001299 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1300 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001301 uint64_t Idx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00001302 if (!checkFunctionOrMethodArgumentIndex(S, D, AL, 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 Ballman3e424b52013-12-26 18:30:57 +00001563 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
1564 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001565 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()) {
Aaron Ballman88fe3222013-12-26 17:30:44 +00001628 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001629 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) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001645 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1646 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001647 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001648 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001649
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001650 uint32_t priority = ConstructorAttr::DefaultPriority;
1651 if (Attr.getNumArgs() > 0 &&
1652 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1653 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001654
Michael Han99315932013-01-24 16:46:58 +00001655 D->addAttr(::new (S.Context)
1656 ConstructorAttr(Attr.getRange(), S.Context, priority,
1657 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001658}
1659
Chandler Carruthedc2c642011-07-02 00:01:44 +00001660static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001661 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001662 if (Attr.getNumArgs() > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001663 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1664 << Attr.getName() << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001665 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001666 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001667
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00001668 uint32_t priority = ConstructorAttr::DefaultPriority;
1669 if (Attr.getNumArgs() > 0 &&
1670 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
1671 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001672
Michael Han99315932013-01-24 16:46:58 +00001673 D->addAttr(::new (S.Context)
1674 DestructorAttr(Attr.getRange(), S.Context, priority,
1675 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001676}
1677
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001678template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00001679static void handleAttrWithMessage(Sema &S, Decl *D,
1680 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001681 unsigned NumArgs = Attr.getNumArgs();
1682 if (NumArgs > 1) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00001683 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
1684 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001685 return;
1686 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001687
1688 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001689 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001690 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001691 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001692
Michael Han99315932013-01-24 16:46:58 +00001693 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1694 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001695}
1696
Ted Kremenek28eace62013-11-23 01:01:34 +00001697static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
1698 const AttributeList &Attr) {
Ted Kremenek28eace62013-11-23 01:01:34 +00001699 D->addAttr(::new (S.Context)
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001700 ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context,
1701 Attr.getAttributeSpellingListIndex()));
Ted Kremenek28eace62013-11-23 01:01:34 +00001702}
1703
Jordy Rose740b0c22012-05-08 03:27:22 +00001704static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1705 IdentifierInfo *Platform,
1706 VersionTuple Introduced,
1707 VersionTuple Deprecated,
1708 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001709 StringRef PlatformName
1710 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1711 if (PlatformName.empty())
1712 PlatformName = Platform->getName();
1713
1714 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1715 // of these steps are needed).
1716 if (!Introduced.empty() && !Deprecated.empty() &&
1717 !(Introduced <= Deprecated)) {
1718 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1719 << 1 << PlatformName << Deprecated.getAsString()
1720 << 0 << Introduced.getAsString();
1721 return true;
1722 }
1723
1724 if (!Introduced.empty() && !Obsoleted.empty() &&
1725 !(Introduced <= Obsoleted)) {
1726 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1727 << 2 << PlatformName << Obsoleted.getAsString()
1728 << 0 << Introduced.getAsString();
1729 return true;
1730 }
1731
1732 if (!Deprecated.empty() && !Obsoleted.empty() &&
1733 !(Deprecated <= Obsoleted)) {
1734 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
1735 << 2 << PlatformName << Obsoleted.getAsString()
1736 << 1 << Deprecated.getAsString();
1737 return true;
1738 }
1739
1740 return false;
1741}
1742
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001743/// \brief Check whether the two versions match.
1744///
1745/// If either version tuple is empty, then they are assumed to match. If
1746/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
1747static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
1748 bool BeforeIsOkay) {
1749 if (X.empty() || Y.empty())
1750 return true;
1751
1752 if (X == Y)
1753 return true;
1754
1755 if (BeforeIsOkay && X < Y)
1756 return true;
1757
1758 return false;
1759}
1760
Rafael Espindolaa3aea432013-01-08 22:04:34 +00001761AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001762 IdentifierInfo *Platform,
1763 VersionTuple Introduced,
1764 VersionTuple Deprecated,
1765 VersionTuple Obsoleted,
1766 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001767 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00001768 bool Override,
1769 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00001770 VersionTuple MergedIntroduced = Introduced;
1771 VersionTuple MergedDeprecated = Deprecated;
1772 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001773 bool FoundAny = false;
1774
Rafael Espindolac67f2232012-05-10 02:50:16 +00001775 if (D->hasAttrs()) {
1776 AttrVec &Attrs = D->getAttrs();
1777 for (unsigned i = 0, e = Attrs.size(); i != e;) {
1778 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
1779 if (!OldAA) {
1780 ++i;
1781 continue;
1782 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001783
Rafael Espindolac67f2232012-05-10 02:50:16 +00001784 IdentifierInfo *OldPlatform = OldAA->getPlatform();
1785 if (OldPlatform != Platform) {
1786 ++i;
1787 continue;
1788 }
1789
1790 FoundAny = true;
1791 VersionTuple OldIntroduced = OldAA->getIntroduced();
1792 VersionTuple OldDeprecated = OldAA->getDeprecated();
1793 VersionTuple OldObsoleted = OldAA->getObsoleted();
1794 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00001795
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001796 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
1797 !versionsMatch(Deprecated, OldDeprecated, Override) ||
1798 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
1799 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00001800 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001801 if (Override) {
1802 int Which = -1;
1803 VersionTuple FirstVersion;
1804 VersionTuple SecondVersion;
1805 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
1806 Which = 0;
1807 FirstVersion = OldIntroduced;
1808 SecondVersion = Introduced;
1809 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
1810 Which = 1;
1811 FirstVersion = Deprecated;
1812 SecondVersion = OldDeprecated;
1813 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
1814 Which = 2;
1815 FirstVersion = Obsoleted;
1816 SecondVersion = OldObsoleted;
1817 }
1818
1819 if (Which == -1) {
1820 Diag(OldAA->getLocation(),
1821 diag::warn_mismatched_availability_override_unavail)
1822 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1823 } else {
1824 Diag(OldAA->getLocation(),
1825 diag::warn_mismatched_availability_override)
1826 << Which
1827 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
1828 << FirstVersion.getAsString() << SecondVersion.getAsString();
1829 }
1830 Diag(Range.getBegin(), diag::note_overridden_method);
1831 } else {
1832 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
1833 Diag(Range.getBegin(), diag::note_previous_attribute);
1834 }
1835
Rafael Espindolac67f2232012-05-10 02:50:16 +00001836 Attrs.erase(Attrs.begin() + i);
1837 --e;
1838 continue;
1839 }
1840
1841 VersionTuple MergedIntroduced2 = MergedIntroduced;
1842 VersionTuple MergedDeprecated2 = MergedDeprecated;
1843 VersionTuple MergedObsoleted2 = MergedObsoleted;
1844
1845 if (MergedIntroduced2.empty())
1846 MergedIntroduced2 = OldIntroduced;
1847 if (MergedDeprecated2.empty())
1848 MergedDeprecated2 = OldDeprecated;
1849 if (MergedObsoleted2.empty())
1850 MergedObsoleted2 = OldObsoleted;
1851
1852 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
1853 MergedIntroduced2, MergedDeprecated2,
1854 MergedObsoleted2)) {
1855 Attrs.erase(Attrs.begin() + i);
1856 --e;
1857 continue;
1858 }
1859
1860 MergedIntroduced = MergedIntroduced2;
1861 MergedDeprecated = MergedDeprecated2;
1862 MergedObsoleted = MergedObsoleted2;
1863 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001864 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001865 }
1866
1867 if (FoundAny &&
1868 MergedIntroduced == Introduced &&
1869 MergedDeprecated == Deprecated &&
1870 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001871 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001872
Ted Kremenekb5445722013-04-06 00:34:27 +00001873 // Only create a new attribute if !Override, but we want to do
1874 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00001875 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00001876 MergedDeprecated, MergedObsoleted) &&
1877 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001878 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
1879 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00001880 Obsoleted, IsUnavailable, Message,
1881 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001882 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001883 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001884}
1885
Chandler Carruthedc2c642011-07-02 00:01:44 +00001886static void handleAvailabilityAttr(Sema &S, Decl *D,
1887 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001888 if (!checkAttributeNumArgs(S, Attr, 1))
1889 return;
1890 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00001891 unsigned Index = Attr.getAttributeSpellingListIndex();
1892
Aaron Ballman00e99962013-08-31 01:11:41 +00001893 IdentifierInfo *II = Platform->Ident;
1894 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
1895 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
1896 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001897
Rafael Espindolac231fab2013-01-08 21:30:32 +00001898 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1899 if (!ND) {
1900 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1901 return;
1902 }
1903
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001904 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1905 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1906 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001907 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001908 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00001909 if (const StringLiteral *SE =
1910 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001911 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001912
Aaron Ballman00e99962013-08-31 01:11:41 +00001913 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001914 Introduced.Version,
1915 Deprecated.Version,
1916 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00001917 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00001918 /*Override=*/false,
1919 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00001920 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001921 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00001922}
1923
John McCalld041a9b2013-02-20 01:54:26 +00001924template <class T>
1925static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
1926 typename T::VisibilityType value,
1927 unsigned attrSpellingListIndex) {
1928 T *existingAttr = D->getAttr<T>();
1929 if (existingAttr) {
1930 typename T::VisibilityType existingValue = existingAttr->getVisibility();
1931 if (existingValue == value)
1932 return NULL;
1933 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
1934 S.Diag(range.getBegin(), diag::note_previous_attribute);
1935 D->dropAttr<T>();
1936 }
1937 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
1938}
1939
Rafael Espindolae200f1c2012-05-13 03:25:18 +00001940VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00001941 VisibilityAttr::VisibilityType Vis,
1942 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00001943 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
1944 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001945}
1946
John McCalld041a9b2013-02-20 01:54:26 +00001947TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
1948 TypeVisibilityAttr::VisibilityType Vis,
1949 unsigned AttrSpellingListIndex) {
1950 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
1951 AttrSpellingListIndex);
1952}
1953
1954static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
1955 bool isTypeVisibility) {
1956 // Visibility attributes don't mean anything on a typedef.
1957 if (isa<TypedefNameDecl>(D)) {
1958 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
1959 << Attr.getName();
1960 return;
1961 }
1962
1963 // 'type_visibility' can only go on a type or namespace.
1964 if (isTypeVisibility &&
1965 !(isa<TagDecl>(D) ||
1966 isa<ObjCInterfaceDecl>(D) ||
1967 isa<NamespaceDecl>(D))) {
1968 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
1969 << Attr.getName() << ExpectedTypeOrNamespace;
1970 return;
1971 }
1972
Benjamin Kramer70370212013-09-09 15:08:57 +00001973 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001974 StringRef TypeStr;
1975 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001976 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001977 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001978
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001979 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00001980 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001981 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00001982 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001983 return;
1984 }
Aaron Ballman682ee422013-09-11 19:47:58 +00001985
1986 // Complain about attempts to use protected visibility on targets
1987 // (like Darwin) that don't support it.
1988 if (type == VisibilityAttr::Protected &&
1989 !S.Context.getTargetInfo().hasProtectedVisibility()) {
1990 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1991 type = VisibilityAttr::Default;
1992 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001993
Michael Han99315932013-01-24 16:46:58 +00001994 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00001995 clang::Attr *newAttr;
1996 if (isTypeVisibility) {
1997 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
1998 (TypeVisibilityAttr::VisibilityType) type,
1999 Index);
2000 } else {
2001 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2002 }
2003 if (newAttr)
2004 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002005}
2006
Chandler Carruthedc2c642011-07-02 00:01:44 +00002007static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2008 const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002009 ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl);
Aaron Ballman00e99962013-08-31 01:11:41 +00002010 if (!Attr.isArgIdent(0)) {
2011 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2012 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002013 return;
2014 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002015
Aaron Ballman682ee422013-09-11 19:47:58 +00002016 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2017 ObjCMethodFamilyAttr::FamilyKind F;
2018 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2019 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2020 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002021 return;
2022 }
2023
Aaron Ballman682ee422013-09-11 19:47:58 +00002024 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCall31168b02011-06-15 23:02:42 +00002025 !method->getResultType()->isObjCObjectPointerType()) {
2026 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2027 << method->getResultType();
2028 // Ignore the attribute.
2029 return;
2030 }
2031
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002032 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman682ee422013-09-11 19:47:58 +00002033 S.Context, F));
John McCall86bc21f2011-03-02 11:33:24 +00002034}
2035
Chandler Carruthedc2c642011-07-02 00:01:44 +00002036static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002037 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002038 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002039 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002040 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2041 return;
2042 }
2043 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002044 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2045 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002046 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002047 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2048 return;
2049 }
2050 }
2051 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002052 // It is okay to include this attribute on properties, e.g.:
2053 //
2054 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2055 //
2056 // In this case it follows tradition and suppresses an error in the above
2057 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002058 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002059 }
Michael Han99315932013-01-24 16:46:58 +00002060 D->addAttr(::new (S.Context)
2061 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2062 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002063}
2064
Chandler Carruthedc2c642011-07-02 00:01:44 +00002065static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002066 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002067 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002068 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002069 return;
2070 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002071
Aaron Ballman00e99962013-08-31 01:11:41 +00002072 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002073 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002074 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2075 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2076 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002077 return;
2078 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002079
Michael Han99315932013-01-24 16:46:58 +00002080 D->addAttr(::new (S.Context)
2081 BlocksAttr(Attr.getRange(), S.Context, type,
2082 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002083}
2084
Chandler Carruthedc2c642011-07-02 00:01:44 +00002085static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002086 // check the attribute arguments.
2087 if (Attr.getNumArgs() > 2) {
Aaron Ballman05e420a2014-01-02 21:26:14 +00002088 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
2089 << Attr.getName() << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002090 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002091 }
2092
Aaron Ballman18a78382013-11-21 00:28:23 +00002093 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002094 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002095 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002096 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002097 if (E->isTypeDependent() || E->isValueDependent() ||
2098 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002099 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002100 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002101 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002102 return;
2103 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002104
John McCallb46f2872011-09-09 07:56:05 +00002105 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002106 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2107 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002108 return;
2109 }
John McCallb46f2872011-09-09 07:56:05 +00002110
2111 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002112 }
2113
Aaron Ballman18a78382013-11-21 00:28:23 +00002114 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002115 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002116 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002117 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002118 if (E->isTypeDependent() || E->isValueDependent() ||
2119 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002120 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002121 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002122 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002123 return;
2124 }
2125 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002126
John McCallb46f2872011-09-09 07:56:05 +00002127 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002128 // FIXME: This error message could be improved, it would be nice
2129 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002130 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2131 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002132 return;
2133 }
2134 }
2135
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002136 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002137 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002138 if (isa<FunctionNoProtoType>(FT)) {
2139 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2140 return;
2141 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002142
Chris Lattner9363e312009-03-17 23:03:47 +00002143 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002144 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002145 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002146 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002147 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002148 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002149 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002150 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002151 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002152 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2153 if (!BD->isVariadic()) {
2154 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2155 return;
2156 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002157 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002158 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002159 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002160 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002161 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002162 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002163 int m = Ty->isFunctionPointerType() ? 0 : 1;
2164 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002165 return;
2166 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002167 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002168 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002169 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002170 return;
2171 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002172 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002173 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002174 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002175 return;
2176 }
Michael Han99315932013-01-24 16:46:58 +00002177 D->addAttr(::new (S.Context)
2178 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2179 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002180}
2181
Chandler Carruthedc2c642011-07-02 00:01:44 +00002182static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002183 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002184 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002185 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002186 return;
2187 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002188
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002189 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2190 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2191 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002192 return;
2193 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002194 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2195 if (MD->getResultType()->isVoidType()) {
2196 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2197 << Attr.getName() << 1;
2198 return;
2199 }
2200
Michael Han99315932013-01-24 16:46:58 +00002201 D->addAttr(::new (S.Context)
2202 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2203 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002204}
2205
Chandler Carruthedc2c642011-07-02 00:01:44 +00002206static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002207 // weak_import only applies to variable & function declarations.
2208 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002209 if (!D->canBeWeakImported(isDef)) {
2210 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002211 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2212 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002213 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002214 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002215 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002216 // Nothing to warn about here.
2217 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002218 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002219 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002220
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002221 return;
2222 }
2223
Michael Han99315932013-01-24 16:46:58 +00002224 D->addAttr(::new (S.Context)
2225 WeakImportAttr(Attr.getRange(), S.Context,
2226 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002227}
2228
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002229// Handles reqd_work_group_size and work_group_size_hint.
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002230template <typename WorkGroupAttr>
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002231static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002232 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002233 uint32_t WGSize[3];
2234 for (unsigned i = 0; i < 3; ++i)
2235 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002236 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002237
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002238 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2239 if (Existing && !(Existing->getXDim() == WGSize[0] &&
2240 Existing->getYDim() == WGSize[1] &&
2241 Existing->getZDim() == WGSize[2]))
2242 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002243
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00002244 D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context,
2245 WGSize[0], WGSize[1], WGSize[2],
Michael Han99315932013-01-24 16:46:58 +00002246 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002247}
2248
Joey Goulyaba589c2013-03-08 09:42:32 +00002249static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002250 if (!Attr.hasParsedType()) {
2251 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2252 << Attr.getName() << 1;
2253 return;
2254 }
2255
Richard Smithb87c4652013-10-31 21:23:20 +00002256 TypeSourceInfo *ParmTSI = 0;
2257 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2258 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002259
2260 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2261 (ParmType->isBooleanType() ||
2262 !ParmType->isIntegralType(S.getASTContext()))) {
2263 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2264 << ParmType;
2265 return;
2266 }
2267
Aaron Ballmana9e05402013-12-02 22:16:55 +00002268 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
Richard Smithb87c4652013-10-31 21:23:20 +00002269 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002270 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2271 return;
2272 }
2273 }
2274
2275 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Richard Smithb87c4652013-10-31 21:23:20 +00002276 ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002277}
2278
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002279SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002280 StringRef Name,
2281 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002282 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2283 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002284 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002285 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2286 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002287 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002288 }
Michael Han99315932013-01-24 16:46:58 +00002289 return ::new (Context) SectionAttr(Range, Context, Name,
2290 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002291}
2292
Chandler Carruthedc2c642011-07-02 00:01:44 +00002293static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002294 // Make sure that there is a string literal as the sections's single
2295 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002296 StringRef Str;
2297 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002298 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002299 return;
Mike Stump11289f42009-09-09 15:08:12 +00002300
Chris Lattner30ba6742009-08-10 19:03:04 +00002301 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002302 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002303 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002304 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002305 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002306 return;
2307 }
Mike Stump11289f42009-09-09 15:08:12 +00002308
Michael Han99315932013-01-24 16:46:58 +00002309 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002310 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002311 if (NewAttr)
2312 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002313}
2314
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002315
Chandler Carruthedc2c642011-07-02 00:01:44 +00002316static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002317 VarDecl *VD = cast<VarDecl>(D);
2318 if (!VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002319 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002320 return;
2321 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002322
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002323 Expr *E = Attr.getArgAsExpr(0);
2324 SourceLocation Loc = E->getExprLoc();
2325 FunctionDecl *FD = 0;
2326 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002327
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002328 // gcc only allows for simple identifiers. Since we support more than gcc, we
2329 // will warn the user.
2330 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2331 if (DRE->hasQualifier())
2332 S.Diag(Loc, diag::warn_cleanup_ext);
2333 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2334 NI = DRE->getNameInfo();
2335 if (!FD) {
2336 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2337 << NI.getName();
2338 return;
2339 }
2340 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2341 if (ULE->hasExplicitTemplateArgs())
2342 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002343 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2344 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002345 if (!FD) {
2346 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2347 << NI.getName();
2348 if (ULE->getType() == S.Context.OverloadTy)
2349 S.NoteAllOverloadCandidates(ULE);
2350 return;
2351 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002352 } else {
2353 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002354 return;
2355 }
2356
Anders Carlssond277d792009-01-31 01:16:18 +00002357 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002358 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2359 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002360 return;
2361 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002362
Anders Carlsson723f55d2009-02-07 23:16:50 +00002363 // We're currently more strict than GCC about what function types we accept.
2364 // If this ever proves to be a problem it should be easy to fix.
2365 QualType Ty = S.Context.getPointerType(VD->getType());
2366 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002367 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2368 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002369 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2370 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002371 return;
2372 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002373
Michael Han99315932013-01-24 16:46:58 +00002374 D->addAttr(::new (S.Context)
2375 CleanupAttr(Attr.getRange(), S.Context, FD,
2376 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002377}
2378
Mike Stumpd3bb5572009-07-24 19:02:52 +00002379/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002380/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002381static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002382 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002383 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002384 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002385 return;
2386 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002387
Aaron Ballman00e99962013-08-31 01:11:41 +00002388 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002389 uint64_t ArgIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002390 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002391 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002392
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002393 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002394 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002395
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002396 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2397 if (not_nsstring_type &&
2398 !isCFStringType(Ty, S.Context) &&
2399 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002400 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002401 // FIXME: Should highlight the actual expression that has the wrong type.
2402 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002403 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002404 << IdxExpr->getSourceRange();
2405 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002406 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002407 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002408 if (!isNSStringType(Ty, S.Context) &&
2409 !isCFStringType(Ty, S.Context) &&
2410 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002411 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002412 // FIXME: Should highlight the actual expression that has the wrong type.
2413 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002414 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002415 << IdxExpr->getSourceRange();
2416 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002417 }
2418
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002419 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2420 // because that has corrected for the implicit this parameter, and is zero-
2421 // based. The attribute expects what the user wrote explicitly.
2422 llvm::APSInt Val;
2423 IdxExpr->EvaluateAsInt(Val, S.Context);
2424
Michael Han99315932013-01-24 16:46:58 +00002425 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002426 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00002427 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002428}
2429
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002430enum FormatAttrKind {
2431 CFStringFormat,
2432 NSStringFormat,
2433 StrftimeFormat,
2434 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002435 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002436 InvalidFormat
2437};
2438
2439/// getFormatAttrKind - Map from format attribute names to supported format
2440/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002441static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002442 return llvm::StringSwitch<FormatAttrKind>(Format)
2443 // Check for formats that get handled specially.
2444 .Case("NSString", NSStringFormat)
2445 .Case("CFString", CFStringFormat)
2446 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002447
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002448 // Otherwise, check for supported formats.
2449 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2450 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2451 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002452
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002453 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2454 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002455}
2456
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002457/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002458/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002459static void handleInitPriorityAttr(Sema &S, Decl *D,
2460 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002461 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002462 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2463 return;
2464 }
2465
Aaron Ballman4a611152013-11-27 16:34:09 +00002466 if (S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002467 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2468 Attr.setInvalid();
2469 return;
2470 }
Aaron Ballman4a611152013-11-27 16:34:09 +00002471 QualType T = cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002472 if (S.Context.getAsArrayType(T))
2473 T = S.Context.getBaseElementType(T);
2474 if (!T->getAs<RecordType>()) {
2475 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2476 Attr.setInvalid();
2477 return;
2478 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002479
2480 Expr *E = Attr.getArgAsExpr(0);
2481 uint32_t prioritynum;
2482 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002483 Attr.setInvalid();
2484 return;
2485 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002486
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002487 if (prioritynum < 101 || prioritynum > 65535) {
2488 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002489 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002490 Attr.setInvalid();
2491 return;
2492 }
Michael Han99315932013-01-24 16:46:58 +00002493 D->addAttr(::new (S.Context)
2494 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2495 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002496}
2497
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002498FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2499 IdentifierInfo *Format, int FormatIdx,
2500 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002501 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002502 // Check whether we already have an equivalent format attribute.
2503 for (specific_attr_iterator<FormatAttr>
2504 i = D->specific_attr_begin<FormatAttr>(),
2505 e = D->specific_attr_end<FormatAttr>();
2506 i != e ; ++i) {
2507 FormatAttr *f = *i;
2508 if (f->getType() == Format &&
2509 f->getFormatIdx() == FormatIdx &&
2510 f->getFirstArg() == FirstArg) {
2511 // If we don't have a valid location for this attribute, adopt the
2512 // location.
2513 if (f->getLocation().isInvalid())
2514 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002515 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002516 }
2517 }
2518
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002519 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2520 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00002521}
2522
Mike Stumpd3bb5572009-07-24 19:02:52 +00002523/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002524/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002525static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002526 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002527 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002528 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002529 return;
2530 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002531
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002532 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002533 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002534 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002535 return;
2536 }
2537
Chandler Carruth743682b2010-11-16 08:35:43 +00002538 // In C++ the implicit 'this' function parameter also counts, and they are
2539 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002540 bool HasImplicitThisParam = isInstanceMethod(D);
2541 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002542
Aaron Ballman00e99962013-08-31 01:11:41 +00002543 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
2544 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002545
2546 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002547 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002548 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002549 // If we've modified the string name, we need a new identifier for it.
2550 II = &S.Context.Idents.get(Format);
2551 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002552
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002553 // Check for supported formats.
2554 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002555
2556 if (Kind == IgnoredFormat)
2557 return;
2558
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002559 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002560 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman190bad42013-12-26 16:13:50 +00002561 << Attr.getName() << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002562 return;
2563 }
2564
2565 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002566 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002567 uint32_t Idx;
2568 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002569 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002570
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002571 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002572 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002573 << Attr.getName() << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002574 return;
2575 }
2576
2577 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002578 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002579
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002580 if (HasImplicitThisParam) {
2581 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002582 S.Diag(Attr.getLoc(),
2583 diag::err_format_attribute_implicit_this_format_string)
2584 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002585 return;
2586 }
2587 ArgIdx--;
2588 }
Mike Stump11289f42009-09-09 15:08:12 +00002589
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002590 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002591 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002592
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002593 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002594 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002595 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2596 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002597 return;
2598 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002599 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002600 // FIXME: do we need to check if the type is NSString*? What are the
2601 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002602 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002603 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002604 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2605 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002606 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002607 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002608 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002609 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002610 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002611 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2612 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002613 return;
2614 }
2615
2616 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00002617 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002618 uint32_t FirstArg;
2619 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002620 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002621
2622 // check if the function is variadic if the 3rd argument non-zero
2623 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002624 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002625 ++NumArgs; // +1 for ...
2626 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002627 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002628 return;
2629 }
2630 }
2631
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002632 // strftime requires FirstArg to be 0 because it doesn't read from any
2633 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002634 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002635 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00002636 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2637 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002638 return;
2639 }
2640 // if 0 it disables parameter checking (to use with e.g. va_list)
2641 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002642 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00002643 << Attr.getName() << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002644 return;
2645 }
2646
Aaron Ballmanf58070b2013-09-03 21:02:22 +00002647 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002648 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00002649 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002650 if (NewAttr)
2651 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002652}
2653
Chandler Carruthedc2c642011-07-02 00:01:44 +00002654static void handleTransparentUnionAttr(Sema &S, Decl *D,
2655 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002656 // Try to find the underlying union declaration.
2657 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002658 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002659 if (TD && TD->getUnderlyingType()->isUnionType())
2660 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2661 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002662 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002663
2664 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002665 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002666 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002667 return;
2668 }
2669
John McCallf937c022011-10-07 06:10:15 +00002670 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002671 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002672 diag::warn_transparent_union_attribute_not_definition);
2673 return;
2674 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002675
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002676 RecordDecl::field_iterator Field = RD->field_begin(),
2677 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002678 if (Field == FieldEnd) {
2679 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2680 return;
2681 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002682
David Blaikie40ed2972012-06-06 20:45:41 +00002683 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002684 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002685 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002686 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002687 diag::warn_transparent_union_attribute_floating)
2688 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002689 return;
2690 }
2691
2692 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2693 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2694 for (; Field != FieldEnd; ++Field) {
2695 QualType FieldType = Field->getType();
2696 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2697 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2698 // Warn if we drop the attribute.
2699 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002700 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002701 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002702 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002703 diag::warn_transparent_union_attribute_field_size_align)
2704 << isSize << Field->getDeclName() << FieldBits;
2705 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002706 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002707 diag::note_transparent_union_first_field_size_align)
2708 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002709 return;
2710 }
2711 }
2712
Michael Han99315932013-01-24 16:46:58 +00002713 RD->addAttr(::new (S.Context)
2714 TransparentUnionAttr(Attr.getRange(), S.Context,
2715 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002716}
2717
Chandler Carruthedc2c642011-07-02 00:01:44 +00002718static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002719 // Make sure that there is a string literal as the annotation's single
2720 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002721 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002722 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002723 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002724
2725 // Don't duplicate annotations that are already set.
2726 for (specific_attr_iterator<AnnotateAttr>
2727 i = D->specific_attr_begin<AnnotateAttr>(),
2728 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002729 if ((*i)->getAnnotation() == Str)
2730 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00002731 }
Michael Han99315932013-01-24 16:46:58 +00002732
2733 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002734 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00002735 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002736}
2737
Chandler Carruthedc2c642011-07-02 00:01:44 +00002738static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002739 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002740 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00002741 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2742 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002743 return;
2744 }
Aaron Ballman478faed2012-06-19 22:09:27 +00002745
Richard Smith848e1f12013-02-01 08:12:08 +00002746 if (Attr.getNumArgs() == 0) {
2747 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
2748 true, 0, Attr.getAttributeSpellingListIndex()));
2749 return;
2750 }
2751
Aaron Ballman00e99962013-08-31 01:11:41 +00002752 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00002753 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
2754 S.Diag(Attr.getEllipsisLoc(),
2755 diag::err_pack_expansion_without_parameter_packs);
2756 return;
2757 }
2758
2759 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
2760 return;
2761
2762 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
2763 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00002764}
2765
2766void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00002767 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00002768 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
2769 SourceLocation AttrLoc = AttrRange.getBegin();
2770
Richard Smith1dba27c2013-01-29 09:02:09 +00002771 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00002772 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002773 // C++11 [dcl.align]p1:
2774 // An alignment-specifier may be applied to a variable or to a class
2775 // data member, but it shall not be applied to a bit-field, a function
2776 // parameter, the formal parameter of a catch clause, or a variable
2777 // declared with the register storage class specifier. An
2778 // alignment-specifier may also be applied to the declaration of a class
2779 // or enumeration type.
2780 // C11 6.7.5/2:
2781 // An alignment attribute shall not be specified in a declaration of
2782 // a typedef, or a bit-field, or a function, or a parameter, or an
2783 // object declared with the register storage-class specifier.
2784 int DiagKind = -1;
2785 if (isa<ParmVarDecl>(D)) {
2786 DiagKind = 0;
2787 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2788 if (VD->getStorageClass() == SC_Register)
2789 DiagKind = 1;
2790 if (VD->isExceptionVariable())
2791 DiagKind = 2;
2792 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
2793 if (FD->isBitField())
2794 DiagKind = 3;
2795 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00002796 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
2797 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00002798 << (TmpAttr.isC11() ? ExpectedVariableOrField
2799 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00002800 return;
2801 }
2802 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00002803 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smithbc8caaf2013-02-22 04:55:39 +00002804 << TmpAttr.isC11() << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00002805 return;
2806 }
2807 }
2808
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002809 if (E->isTypeDependent() || E->isValueDependent()) {
2810 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00002811 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
2812 AA->setPackExpansion(IsPackExpansion);
2813 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002814 return;
2815 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002816
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002817 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002818 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00002819 ExprResult ICE
2820 = VerifyIntegerConstantExpression(E, &Alignment,
2821 diag::err_aligned_attribute_argument_not_int,
2822 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00002823 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00002824 return;
Richard Smith848e1f12013-02-01 08:12:08 +00002825
2826 // C++11 [dcl.align]p2:
2827 // -- if the constant expression evaluates to zero, the alignment
2828 // specifier shall have no effect
2829 // C11 6.7.5p6:
2830 // An alignment specification of zero has no effect.
2831 if (!(TmpAttr.isAlignas() && !Alignment) &&
2832 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002833 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2834 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002835 return;
2836 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00002837
Richard Smith848e1f12013-02-01 08:12:08 +00002838 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00002839 // We've already verified it's a power of 2, now let's make sure it's
2840 // 8192 or less.
2841 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00002842 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00002843 << E->getSourceRange();
2844 return;
2845 }
2846 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002847
Richard Smith44c247f2013-02-22 08:32:16 +00002848 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
2849 ICE.take(), SpellingListIndex);
2850 AA->setPackExpansion(IsPackExpansion);
2851 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002852}
2853
Michael Hanaf02bbe2013-02-01 01:19:17 +00002854void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00002855 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002856 // FIXME: Cache the number on the Attr object if non-dependent?
2857 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00002858 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
2859 SpellingListIndex);
2860 AA->setPackExpansion(IsPackExpansion);
2861 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002862}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002863
Richard Smith848e1f12013-02-01 08:12:08 +00002864void Sema::CheckAlignasUnderalignment(Decl *D) {
2865 assert(D->hasAttrs() && "no attributes on decl");
2866
2867 QualType Ty;
2868 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2869 Ty = VD->getType();
2870 else
2871 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00002872 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00002873 return;
2874
2875 // C++11 [dcl.align]p5, C11 6.7.5/4:
2876 // The combined effect of all alignment attributes in a declaration shall
2877 // not specify an alignment that is less strict than the alignment that
2878 // would otherwise be required for the entity being declared.
2879 AlignedAttr *AlignasAttr = 0;
2880 unsigned Align = 0;
2881 for (specific_attr_iterator<AlignedAttr>
2882 I = D->specific_attr_begin<AlignedAttr>(),
2883 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
2884 if (I->isAlignmentDependent())
2885 return;
2886 if (I->isAlignas())
2887 AlignasAttr = *I;
2888 Align = std::max(Align, I->getAlignment(Context));
2889 }
2890
2891 if (AlignasAttr && Align) {
2892 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
2893 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
2894 if (NaturalAlign > RequestedAlign)
2895 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
2896 << Ty << (unsigned)NaturalAlign.getQuantity();
2897 }
2898}
2899
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002900/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002901/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002902///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002903/// Despite what would be logical, the mode attribute is a decl attribute, not a
2904/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2905/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002906static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002907 // This attribute isn't documented, but glibc uses it. It changes
2908 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00002909 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00002910 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
2911 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002912 return;
2913 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002914
Aaron Ballman00e99962013-08-31 01:11:41 +00002915 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
2916 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002917
2918 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002919 if (Str.startswith("__") && Str.endswith("__"))
2920 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002921
2922 unsigned DestWidth = 0;
2923 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002924 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002925 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002926 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002927 switch (Str[0]) {
2928 case 'Q': DestWidth = 8; break;
2929 case 'H': DestWidth = 16; break;
2930 case 'S': DestWidth = 32; break;
2931 case 'D': DestWidth = 64; break;
2932 case 'X': DestWidth = 96; break;
2933 case 'T': DestWidth = 128; break;
2934 }
2935 if (Str[1] == 'F') {
2936 IntegerMode = false;
2937 } else if (Str[1] == 'C') {
2938 IntegerMode = false;
2939 ComplexMode = true;
2940 } else if (Str[1] != 'I') {
2941 DestWidth = 0;
2942 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002943 break;
2944 case 4:
2945 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2946 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002947 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002948 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002949 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002950 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002951 break;
2952 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002953 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00002954 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002955 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002956 case 11:
2957 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00002958 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00002959 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002960 }
2961
2962 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002963 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002964 OldTy = TD->getUnderlyingType();
2965 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2966 OldTy = VD->getType();
2967 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002968 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Aaron Ballman2dfb03f2013-12-27 16:30:46 +00002969 << Attr.getName() << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002970 return;
2971 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002972
John McCall9dd450b2009-09-21 23:43:11 +00002973 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002974 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2975 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002976 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002977 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2978 } else if (ComplexMode) {
2979 if (!OldTy->isComplexType())
2980 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2981 } else {
2982 if (!OldTy->isFloatingType())
2983 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2984 }
2985
Mike Stump87c57ac2009-05-16 07:39:55 +00002986 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2987 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002988 // FIXME: Make sure floating-point mappings are accurate
2989 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002990 if (!DestWidth) {
Aaron Ballman03909082013-12-23 15:23:11 +00002991 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002992 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00002993 }
2994
2995 QualType NewTy;
2996
2997 if (IntegerMode)
2998 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
2999 OldTy->isSignedIntegerType());
3000 else
3001 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3002
3003 if (NewTy.isNull()) {
Aaron Ballman03909082013-12-23 15:23:11 +00003004 S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003005 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003006 }
3007
Eli Friedman4735374e2009-03-03 06:41:03 +00003008 if (ComplexMode) {
3009 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003010 }
3011
3012 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003013 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3014 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3015 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003016 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003017
3018 D->addAttr(::new (S.Context)
3019 ModeAttr(Attr.getRange(), S.Context, Name,
3020 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003021}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003022
Chandler Carruthedc2c642011-07-02 00:01:44 +00003023static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003024 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3025 if (!VD->hasGlobalStorage())
3026 S.Diag(Attr.getLoc(),
3027 diag::warn_attribute_requires_functions_or_static_globals)
3028 << Attr.getName();
3029 } else if (!isFunctionOrMethod(D)) {
3030 S.Diag(Attr.getLoc(),
3031 diag::warn_attribute_requires_functions_or_static_globals)
3032 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003033 return;
3034 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003035
Michael Han99315932013-01-24 16:46:58 +00003036 D->addAttr(::new (S.Context)
3037 NoDebugAttr(Attr.getRange(), S.Context,
3038 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003039}
3040
Chandler Carruthedc2c642011-07-02 00:01:44 +00003041static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003042 FunctionDecl *FD = cast<FunctionDecl>(D);
3043 if (!FD->getResultType()->isVoidType()) {
3044 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
3045 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
3046 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3047 << FD->getType()
3048 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
3049 "void");
3050 } else {
3051 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3052 << FD->getType();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003053 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003054 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003055 }
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003056
Aaron Ballman3aff6332013-12-02 19:30:36 +00003057 D->addAttr(::new (S.Context)
3058 CUDAGlobalAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00003059 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003060}
3061
Chandler Carruthedc2c642011-07-02 00:01:44 +00003062static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003063 FunctionDecl *Fn = cast<FunctionDecl>(D);
Douglas Gregor35b57532009-10-27 21:01:01 +00003064 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003065 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003066 return;
3067 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003068
Michael Han99315932013-01-24 16:46:58 +00003069 D->addAttr(::new (S.Context)
3070 GNUInlineAttr(Attr.getRange(), S.Context,
3071 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003072}
3073
Chandler Carruthedc2c642011-07-02 00:01:44 +00003074static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003075 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003076
Aaron Ballman02df2e02012-12-09 17:45:41 +00003077 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003078 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003079 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3080 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003081 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003082 return;
3083
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003084 if (!isa<ObjCMethodDecl>(D)) {
3085 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3086 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003087 return;
3088 }
3089
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003090 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003091 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003092 D->addAttr(::new (S.Context)
3093 FastCallAttr(Attr.getRange(), S.Context,
3094 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003095 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003096 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003097 D->addAttr(::new (S.Context)
3098 StdCallAttr(Attr.getRange(), S.Context,
3099 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003100 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003101 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003102 D->addAttr(::new (S.Context)
3103 ThisCallAttr(Attr.getRange(), S.Context,
3104 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003105 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003106 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003107 D->addAttr(::new (S.Context)
3108 CDeclAttr(Attr.getRange(), S.Context,
3109 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003110 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003111 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003112 D->addAttr(::new (S.Context)
3113 PascalAttr(Attr.getRange(), S.Context,
3114 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003115 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003116 case AttributeList::AT_MSABI:
3117 D->addAttr(::new (S.Context)
3118 MSABIAttr(Attr.getRange(), S.Context,
3119 Attr.getAttributeSpellingListIndex()));
3120 return;
3121 case AttributeList::AT_SysVABI:
3122 D->addAttr(::new (S.Context)
3123 SysVABIAttr(Attr.getRange(), S.Context,
3124 Attr.getAttributeSpellingListIndex()));
3125 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003126 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003127 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003128 switch (CC) {
3129 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003130 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003131 break;
3132 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003133 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003134 break;
3135 default:
3136 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003137 }
3138
Michael Han99315932013-01-24 16:46:58 +00003139 D->addAttr(::new (S.Context)
3140 PcsAttr(Attr.getRange(), S.Context, PCS,
3141 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003142 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003143 }
Derek Schuffa2020962012-10-16 22:30:41 +00003144 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003145 D->addAttr(::new (S.Context)
3146 PnaclCallAttr(Attr.getRange(), S.Context,
3147 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003148 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003149 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003150 D->addAttr(::new (S.Context)
3151 IntelOclBiccAttr(Attr.getRange(), S.Context,
3152 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003153 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003154
Abramo Bagnara50099372010-04-30 13:10:51 +00003155 default:
3156 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003157 }
3158}
3159
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003160static void handleOpenCLImageAccessAttr(Sema &S, Decl *D,
3161 const AttributeList &Attr) {
3162 uint32_t ArgNum;
3163 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), ArgNum))
Guy Benyeifb36ede2013-03-24 13:58:12 +00003164 return;
Guy Benyeifb36ede2013-03-24 13:58:12 +00003165
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003166 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(Attr.getRange(),
3167 S.Context, ArgNum));
Guy Benyeifb36ede2013-03-24 13:58:12 +00003168}
3169
Aaron Ballman02df2e02012-12-09 17:45:41 +00003170bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3171 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003172 if (attr.isInvalid())
3173 return true;
3174
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003175 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003176 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003177 attr.setInvalid();
3178 return true;
3179 }
3180
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003181 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3182 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003183 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003184 case AttributeList::AT_CDecl: CC = CC_C; break;
3185 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3186 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3187 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3188 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003189 case AttributeList::AT_MSABI:
3190 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3191 CC_X86_64Win64;
3192 break;
3193 case AttributeList::AT_SysVABI:
3194 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3195 CC_C;
3196 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003197 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003198 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003199 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003200 attr.setInvalid();
3201 return true;
3202 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003203 if (StrRef == "aapcs") {
3204 CC = CC_AAPCS;
3205 break;
3206 } else if (StrRef == "aapcs-vfp") {
3207 CC = CC_AAPCS_VFP;
3208 break;
3209 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003210
3211 attr.setInvalid();
3212 Diag(attr.getLoc(), diag::err_invalid_pcs);
3213 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003214 }
Derek Schuffa2020962012-10-16 22:30:41 +00003215 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003216 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003217 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003218 }
3219
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003220 const TargetInfo &TI = Context.getTargetInfo();
3221 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3222 if (A == TargetInfo::CCCR_Warning) {
3223 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003224
3225 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3226 if (FD)
3227 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3228 TargetInfo::CCMT_NonMember;
3229 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003230 }
3231
John McCall3882ace2011-01-05 12:14:39 +00003232 return false;
3233}
3234
Chandler Carruthedc2c642011-07-02 00:01:44 +00003235static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003236 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003237
3238 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003239 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003240 return;
3241
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003242 if (!isa<ObjCMethodDecl>(D)) {
3243 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3244 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003245 return;
3246 }
Eli Friedman7044b762009-03-27 21:06:47 +00003247
Michael Han99315932013-01-24 16:46:58 +00003248 D->addAttr(::new (S.Context)
3249 RegparmAttr(Attr.getRange(), S.Context, numParams,
3250 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00003251}
3252
3253/// Checks a regparm attribute, returning true if it is ill-formed and
3254/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003255bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3256 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003257 return true;
3258
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003259 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003260 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003261 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003262 }
Eli Friedman7044b762009-03-27 21:06:47 +00003263
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003264 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003265 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003266 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003267 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003268 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003269 }
3270
Douglas Gregore8bbc122011-09-02 00:18:52 +00003271 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003272 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003273 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003274 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003275 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003276 }
3277
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003278 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003279 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003280 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003281 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003282 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003283 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003284 }
3285
John McCall3882ace2011-01-05 12:14:39 +00003286 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003287}
3288
Aaron Ballman66039932013-12-19 00:41:31 +00003289static void handleLaunchBoundsAttr(Sema &S, Decl *D,
3290 const AttributeList &Attr) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003291 // check the attribute arguments.
3292 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
3293 // FIXME: 0 is not okay.
Aaron Ballman05e420a2014-01-02 21:26:14 +00003294 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments)
3295 << Attr.getName() << 2;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003296 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003297 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003298
3299 if (!isFunctionOrMethod(D)) {
3300 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3301 << Attr.getName() << ExpectedFunctionOrMethod;
3302 return;
3303 }
3304
Aaron Ballman66039932013-12-19 00:41:31 +00003305 uint32_t MaxThreads, MinBlocks = 0;
3306 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1))
3307 return;
3308 if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr,
3309 Attr.getArgAsExpr(1),
3310 MinBlocks, 2))
Aaron Ballman3aff6332013-12-02 19:30:36 +00003311 return;
3312
3313 D->addAttr(::new (S.Context)
3314 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3315 MaxThreads, MinBlocks,
3316 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00003317}
3318
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003319static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3320 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003321 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003322 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003323 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003324 return;
3325 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003326
3327 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003328 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003329
Aaron Ballman00e99962013-08-31 01:11:41 +00003330 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003331
3332 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3333 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3334 << Attr.getName() << ExpectedFunctionOrMethod;
3335 return;
3336 }
3337
3338 uint64_t ArgumentIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003339 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 2, Attr.getArgAsExpr(1),
3340 ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003341 return;
3342
3343 uint64_t TypeTagIdx;
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003344 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 3, Attr.getArgAsExpr(2),
3345 TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003346 return;
3347
Aaron Ballmanfaed0fa2013-12-26 16:30:30 +00003348 bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003349 if (IsPointer) {
3350 // Ensure that buffer has a pointer type.
3351 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3352 if (!BufferTy->isPointerType()) {
3353 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00003354 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003355 }
3356 }
3357
Michael Han99315932013-01-24 16:46:58 +00003358 D->addAttr(::new (S.Context)
3359 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
3360 ArgumentIdx, TypeTagIdx, IsPointer,
3361 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003362}
3363
3364static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3365 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003366 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003367 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00003368 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003369 return;
3370 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003371
3372 if (!checkAttributeNumArgs(S, Attr, 1))
3373 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003374
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00003375 if (!isa<VarDecl>(D)) {
3376 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3377 << Attr.getName() << ExpectedVariable;
3378 return;
3379 }
3380
Aaron Ballman00e99962013-08-31 01:11:41 +00003381 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00003382 TypeSourceInfo *MatchingCTypeLoc = 0;
3383 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
3384 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003385
Michael Han99315932013-01-24 16:46:58 +00003386 D->addAttr(::new (S.Context)
3387 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00003388 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00003389 Attr.getLayoutCompatible(),
3390 Attr.getMustBeNull(),
3391 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003392}
3393
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003394//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003395// Checker-specific attribute handlers.
3396//===----------------------------------------------------------------------===//
3397
John McCalled433932011-01-25 03:31:58 +00003398static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003399 return type->isDependentType() ||
3400 type->isObjCObjectPointerType() ||
3401 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003402}
3403static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003404 return type->isDependentType() ||
3405 type->isPointerType() ||
3406 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003407}
3408
Chandler Carruthedc2c642011-07-02 00:01:44 +00003409static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003410 ParmVarDecl *param = cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003411 bool typeOK, cf;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003412
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003413 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003414 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3415 cf = false;
3416 } else {
3417 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3418 cf = true;
3419 }
3420
3421 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003422 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003423 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003424 return;
3425 }
3426
3427 if (cf)
Michael Han99315932013-01-24 16:46:58 +00003428 param->addAttr(::new (S.Context)
3429 CFConsumedAttr(Attr.getRange(), S.Context,
3430 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003431 else
Michael Han99315932013-01-24 16:46:58 +00003432 param->addAttr(::new (S.Context)
3433 NSConsumedAttr(Attr.getRange(), S.Context,
3434 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003435}
3436
Chandler Carruthedc2c642011-07-02 00:01:44 +00003437static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3438 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003439
John McCalled433932011-01-25 03:31:58 +00003440 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003441
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003442 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003443 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003444 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003445 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003446 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003447 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3448 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003449 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003450 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003451 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003452 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003453 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003454 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003455 return;
3456 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003457
John McCalled433932011-01-25 03:31:58 +00003458 bool typeOK;
3459 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003460 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003461 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003462 case AttributeList::AT_NSReturnsAutoreleased:
3463 case AttributeList::AT_NSReturnsRetained:
3464 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003465 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3466 cf = false;
3467 break;
3468
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003469 case AttributeList::AT_CFReturnsRetained:
3470 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003471 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3472 cf = true;
3473 break;
3474 }
3475
3476 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003477 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003478 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003479 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003480 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003481
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003482 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003483 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003484 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003485 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00003486 D->addAttr(::new (S.Context)
3487 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
3488 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00003489 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003490 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003491 D->addAttr(::new (S.Context)
3492 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3493 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003494 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003495 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00003496 D->addAttr(::new (S.Context)
3497 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
3498 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003499 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003500 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003501 D->addAttr(::new (S.Context)
3502 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
3503 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003504 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003505 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00003506 D->addAttr(::new (S.Context)
3507 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
3508 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003509 return;
3510 };
3511}
3512
John McCallcf166702011-07-22 08:53:00 +00003513static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3514 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00003515 const int EP_ObjCMethod = 1;
3516 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003517
John McCallcf166702011-07-22 08:53:00 +00003518 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003519 QualType resultType;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003520 if (isa<ObjCMethodDecl>(D))
3521 resultType = cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003522 else
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003523 resultType = cast<ObjCPropertyDecl>(D)->getType();
John McCallcf166702011-07-22 08:53:00 +00003524
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003525 if (!resultType->isReferenceType() &&
3526 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003527 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00003528 << SourceRange(loc)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003529 << attr.getName()
3530 << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00003531 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00003532
3533 // Drop the attribute.
3534 return;
3535 }
3536
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00003537 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003538 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
3539 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00003540}
3541
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003542static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
3543 const AttributeList &attr) {
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003544 ObjCMethodDecl *method = cast<ObjCMethodDecl>(D);
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003545
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003546 DeclContext *DC = method->getDeclContext();
3547 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
3548 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3549 << attr.getName() << 0;
3550 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
3551 return;
3552 }
3553 if (method->getMethodFamily() == OMF_dealloc) {
3554 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
3555 << attr.getName() << 1;
3556 return;
3557 }
3558
Michael Han99315932013-01-24 16:46:58 +00003559 method->addAttr(::new (S.Context)
3560 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
3561 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003562}
3563
Aaron Ballmanfb763042013-12-02 18:05:46 +00003564static void handleCFAuditedTransferAttr(Sema &S, Decl *D,
3565 const AttributeList &Attr) {
3566 if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr,
3567 "cf_unknown_transfer"))
John McCall32f5fe12011-09-30 05:12:12 +00003568 return;
John McCall32f5fe12011-09-30 05:12:12 +00003569
Aaron Ballmanfb763042013-12-02 18:05:46 +00003570 D->addAttr(::new (S.Context)
3571 CFAuditedTransferAttr(Attr.getRange(), S.Context,
3572 Attr.getAttributeSpellingListIndex()));
3573}
3574
3575static void handleCFUnknownTransferAttr(Sema &S, Decl *D,
3576 const AttributeList &Attr) {
3577 if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr,
3578 "cf_audited_transfer"))
3579 return;
3580
3581 D->addAttr(::new (S.Context)
3582 CFUnknownTransferAttr(Attr.getRange(), S.Context,
3583 Attr.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00003584}
3585
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003586static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
3587 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003588 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003589
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003590 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003591 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003592 return;
3593 }
3594
3595 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00003596 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003597 Attr.getAttributeSpellingListIndex()));
3598}
3599
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003600static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
3601 const AttributeList &Attr) {
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00003602 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003603
3604 if (!Parm) {
3605 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3606 return;
3607 }
3608
3609 D->addAttr(::new (S.Context)
3610 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
3611 Attr.getAttributeSpellingListIndex()));
3612}
3613
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003614static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D,
3615 const AttributeList &Attr) {
3616 IdentifierInfo *RelatedClass =
3617 Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0;
3618 if (!RelatedClass) {
3619 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
3620 return;
3621 }
3622 IdentifierInfo *ClassMethod =
3623 Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0;
3624 IdentifierInfo *InstanceMethod =
3625 Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0;
3626 D->addAttr(::new (S.Context)
3627 ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass,
3628 ClassMethod, InstanceMethod,
3629 Attr.getAttributeSpellingListIndex()));
3630}
3631
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003632static void handleObjCDesignatedInitializer(Sema &S, Decl *D,
3633 const AttributeList &Attr) {
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003634 ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D->getDeclContext());
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00003635 IFace->setHasDesignatedInitializers();
Argyrios Kyrtzidise8186812013-12-07 06:08:04 +00003636 D->addAttr(::new (S.Context)
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003637 ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context,
3638 Attr.getAttributeSpellingListIndex()));
3639}
3640
Chandler Carruthedc2c642011-07-02 00:01:44 +00003641static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3642 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003643 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00003644
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003645 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003646 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00003647}
3648
Chandler Carruthedc2c642011-07-02 00:01:44 +00003649static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3650 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003651 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00003652 QualType type = vd->getType();
3653
3654 if (!type->isDependentType() &&
3655 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003656 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00003657 << type;
3658 return;
3659 }
3660
3661 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3662
3663 // If we have no lifetime yet, check the lifetime we're presumably
3664 // going to infer.
3665 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3666 lifetime = type->getObjCARCImplicitLifetime();
3667
3668 switch (lifetime) {
3669 case Qualifiers::OCL_None:
3670 assert(type->isDependentType() &&
3671 "didn't infer lifetime for non-dependent type?");
3672 break;
3673
3674 case Qualifiers::OCL_Weak: // meaningful
3675 case Qualifiers::OCL_Strong: // meaningful
3676 break;
3677
3678 case Qualifiers::OCL_ExplicitNone:
3679 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003680 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00003681 << (lifetime == Qualifiers::OCL_Autoreleasing);
3682 break;
3683 }
3684
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003685 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00003686 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
3687 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00003688}
3689
Francois Picheta83957a2010-12-19 06:50:37 +00003690//===----------------------------------------------------------------------===//
3691// Microsoft specific attribute handlers.
3692//===----------------------------------------------------------------------===//
3693
Chandler Carruthedc2c642011-07-02 00:01:44 +00003694static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00003695 if (!S.LangOpts.CPlusPlus) {
3696 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
3697 << Attr.getName() << AttributeLangSupport::C;
3698 return;
3699 }
3700
Aaron Ballman60e705e2013-11-24 20:58:02 +00003701 if (!isa<CXXRecordDecl>(D)) {
3702 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3703 << Attr.getName() << ExpectedClass;
3704 return;
3705 }
3706
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003707 StringRef StrRef;
3708 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003709 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00003710 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003711
David Majnemer89085342013-08-09 08:56:20 +00003712 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3713 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00003714 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
3715 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00003716
Reid Kleckner140c4a72013-05-17 14:04:52 +00003717 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00003718 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003719 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003720 return;
3721 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00003722
David Majnemer89085342013-08-09 08:56:20 +00003723 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00003724 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00003725 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003726 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00003727 return;
3728 }
David Majnemer89085342013-08-09 08:56:20 +00003729 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003730 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00003731 return;
Francois Pichet7da11662010-12-20 01:41:49 +00003732 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00003733 }
Francois Picheta83957a2010-12-19 06:50:37 +00003734
David Majnemer89085342013-08-09 08:56:20 +00003735 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
3736 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00003737}
3738
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003739/// Handles semantic checking for features that are common to all attributes,
3740/// such as checking whether a parameter was properly specified, or the correct
3741/// number of arguments were passed, etc.
3742static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
3743 const AttributeList &Attr) {
3744 // Several attributes carry different semantics than the parsing requires, so
3745 // those are opted out of the common handling.
3746 //
3747 // We also bail on unknown and ignored attributes because those are handled
3748 // as part of the target-specific handling logic.
3749 if (Attr.hasCustomParsing() ||
3750 Attr.getKind() == AttributeList::UnknownAttribute ||
3751 Attr.getKind() == AttributeList::IgnoredAttribute)
3752 return false;
3753
Aaron Ballman3aff6332013-12-02 19:30:36 +00003754 // Check whether the attribute requires specific language extensions to be
3755 // enabled.
3756 if (!Attr.diagnoseLangOpts(S))
3757 return true;
3758
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003759 // If there are no optional arguments, then checking for the argument count
3760 // is trivial.
3761 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
3762 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
3763 return true;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003764
3765 // Check whether the attribute appertains to the given subject.
3766 if (!Attr.diagnoseAppertainsTo(S, D))
3767 return true;
3768
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003769 return false;
3770}
3771
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003772//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003773// Top Level Sema Entry Points
3774//===----------------------------------------------------------------------===//
3775
Richard Smithf8a75c32013-08-29 00:47:48 +00003776/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3777/// the attribute applies to decls. If the attribute is a type attribute, just
3778/// silently ignore it if a GNU attribute.
3779static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3780 const AttributeList &Attr,
3781 bool IncludeCXX11Attributes) {
3782 if (Attr.isInvalid())
3783 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003784
Richard Smithf8a75c32013-08-29 00:47:48 +00003785 // Ignore C++11 attributes on declarator chunks: they appertain to the type
3786 // instead.
3787 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
3788 return;
3789
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003790 if (handleCommonAttributeFeatures(S, scope, D, Attr))
3791 return;
3792
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003793 switch (Attr.getKind()) {
Aaron Ballman9beb5172013-12-02 15:13:14 +00003794 case AttributeList::AT_IBAction:
3795 handleSimpleAttribute<IBActionAttr>(S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003796 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
3797 case AttributeList::AT_IBOutletCollection:
3798 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003799 case AttributeList::AT_AddressSpace:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003800 case AttributeList::AT_ObjCGC:
3801 case AttributeList::AT_VectorSize:
3802 case AttributeList::AT_NeonVectorType:
3803 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballman317a77f2013-05-22 23:25:32 +00003804 case AttributeList::AT_Ptr32:
3805 case AttributeList::AT_Ptr64:
3806 case AttributeList::AT_SPtr:
3807 case AttributeList::AT_UPtr:
Mike Stumpd3bb5572009-07-24 19:02:52 +00003808 // Ignore these, these are type attributes, handled by
3809 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003810 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003811 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
3812 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003813 case AttributeList::AT_AlwaysInline:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003814 handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003815 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003816 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00003817 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003818 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
3819 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
3820 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00003821 handleDependencyAttr(S, scope, D, Attr);
3822 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003823 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003824 case AttributeList::AT_CUDAConstant:
3825 handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003826 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00003827 case AttributeList::AT_CXX11NoReturn:
Aaron Ballman3a8e2d92013-11-27 18:53:58 +00003828 handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003829 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00003830 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00003831 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003832 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
3833 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003834 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003835 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00003836 case AttributeList::AT_MinSize:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003837 handleSimpleAttribute<MinSizeAttr>(S, D, Attr);
Quentin Colombet4e172062012-11-01 23:55:47 +00003838 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003839 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
3840 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
3841 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballmanf79ee272013-12-02 21:09:08 +00003842 case AttributeList::AT_CUDADevice:
3843 handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003844 case AttributeList::AT_CUDAHost:
3845 handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003846 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
3847 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003848 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00003849 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003850 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003851 case AttributeList::AT_MayAlias:
3852 handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00003853 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003854 case AttributeList::AT_NoCommon:
3855 handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003856 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003857 case AttributeList::AT_Overloadable:
3858 handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break;
Richard Smith852e9ce2013-11-27 01:46:48 +00003859 case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003860 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
3861 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003862 case AttributeList::AT_Naked:
3863 handleSimpleAttribute<NakedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003864 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00003865 case AttributeList::AT_NoThrow:
3866 handleSimpleAttribute<NoThrowAttr>(S, D, Attr); break;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003867 case AttributeList::AT_CUDAShared:
3868 handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003869 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003870
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003871 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003872 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003873 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003874 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003875
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003876 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00003877 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3878
Fariborz Jahanian566fff02012-09-07 23:46:23 +00003879 case AttributeList::AT_ObjCRequiresSuper:
3880 handleObjCRequiresSuperAttr(S, D, Attr); break;
3881
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00003882 case AttributeList::AT_ObjCBridge:
3883 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00003884
3885 case AttributeList::AT_ObjCBridgeMutable:
3886 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00003887
3888 case AttributeList::AT_ObjCBridgeRelated:
3889 handleObjCBridgeRelatedAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00003890
Argyrios Kyrtzidisd1438b42013-12-03 21:11:25 +00003891 case AttributeList::AT_ObjCDesignatedInitializer:
3892 handleObjCDesignatedInitializer(S, D, Attr); break;
3893
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003894 case AttributeList::AT_CFAuditedTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00003895 handleCFAuditedTransferAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003896 case AttributeList::AT_CFUnknownTransfer:
Aaron Ballmanfb763042013-12-02 18:05:46 +00003897 handleCFUnknownTransferAttr(S, D, Attr); break;
John McCall32f5fe12011-09-30 05:12:12 +00003898
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003899 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003900 case AttributeList::AT_CFConsumed:
3901 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
3902 case AttributeList::AT_NSConsumesSelf:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003903 handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00003904
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003905 case AttributeList::AT_NSReturnsAutoreleased:
3906 case AttributeList::AT_NSReturnsNotRetained:
3907 case AttributeList::AT_CFReturnsNotRetained:
3908 case AttributeList::AT_NSReturnsRetained:
3909 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003910 handleNSReturnsRetainedAttr(S, D, Attr); break;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00003911 case AttributeList::AT_WorkGroupSizeHint:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00003912 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003913 case AttributeList::AT_ReqdWorkGroupSize:
Aaron Ballman1d0d2a42013-12-02 22:38:33 +00003914 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); break;
Joey Goulyaba589c2013-03-08 09:42:32 +00003915 case AttributeList::AT_VecTypeHint:
3916 handleVecTypeHint(S, D, Attr); break;
3917
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003918 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003919 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003920
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003921 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
3922 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
3923 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00003924 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00003925 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003926 case AttributeList::AT_ArcWeakrefUnavailable:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003927 handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003928 case AttributeList::AT_ObjCRootClass:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003929 handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break;
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00003930 case AttributeList::AT_ObjCExplicitProtocolImpl:
Ted Kremenek28eace62013-11-23 01:01:34 +00003931 handleObjCSuppresProtocolAttr(S, D, Attr);
3932 break;
3933 case AttributeList::AT_ObjCRequiresPropertyDefs:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003934 handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003935 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
3936 case AttributeList::AT_ReturnsTwice:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003937 handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003938 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00003939 case AttributeList::AT_Visibility:
3940 handleVisibilityAttr(S, D, Attr, false);
3941 break;
3942 case AttributeList::AT_TypeVisibility:
3943 handleVisibilityAttr(S, D, Attr, true);
3944 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00003945 case AttributeList::AT_WarnUnused:
Aaron Ballman6a42b5a2013-11-27 16:59:17 +00003946 handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003947 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00003948 break;
Aaron Ballman604dfec2013-12-02 17:07:07 +00003949 case AttributeList::AT_Weak:
3950 handleSimpleAttribute<WeakAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003951 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
3952 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
3953 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003954 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003955 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003956 case AttributeList::AT_ObjCException:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003957 handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003958 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003959 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00003960 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003961 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
3962 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
3963 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
Aaron Ballmanbf7b1ee2013-12-21 16:49:29 +00003964 case AttributeList::AT_Const:
3965 handleSimpleAttribute<ConstAttr>(S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003966 case AttributeList::AT_Pure:
3967 handleSimpleAttribute<PureAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003968 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
3969 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003970 case AttributeList::AT_NoInline:
3971 handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003972 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003973 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00003974 // Just ignore
3975 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003976 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003977 handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003978 case AttributeList::AT_StdCall:
3979 case AttributeList::AT_CDecl:
3980 case AttributeList::AT_FastCall:
3981 case AttributeList::AT_ThisCall:
3982 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00003983 case AttributeList::AT_MSABI:
3984 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003985 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00003986 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00003987 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003988 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00003989 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003990 case AttributeList::AT_OpenCLKernel:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003991 handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00003992 case AttributeList::AT_OpenCLImageAccess:
3993 handleOpenCLImageAccessAttr(S, D, Attr);
3994 break;
John McCall8d32c052012-05-22 21:28:12 +00003995
3996 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003997 case AttributeList::AT_MsStruct:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00003998 handleSimpleAttribute<MsStructAttr>(S, D, Attr);
John McCall8d32c052012-05-22 21:28:12 +00003999 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004000 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004001 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004002 break;
Aaron Ballman8edb5c22013-12-18 23:44:18 +00004003 case AttributeList::AT_MSInheritance:
4004 handleSimpleAttribute<MSInheritanceAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004005 case AttributeList::AT_ForceInline:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004006 handleSimpleAttribute<ForceInlineAttr>(S, D, Attr); break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004007 case AttributeList::AT_SelectAny:
Aaron Ballman3aff6332013-12-02 19:30:36 +00004008 handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004009
4010 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004011 case AttributeList::AT_AssertExclusiveLock:
4012 handleAssertExclusiveLockAttr(S, D, Attr);
4013 break;
4014 case AttributeList::AT_AssertSharedLock:
4015 handleAssertSharedLockAttr(S, D, Attr);
4016 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004017 case AttributeList::AT_GuardedVar:
Aaron Ballmane61b8b82013-12-02 15:02:49 +00004018 handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004019 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004020 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004021 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004022 case AttributeList::AT_ScopedLockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004023 handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004024 case AttributeList::AT_NoSanitizeAddress:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004025 handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004026 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004027 case AttributeList::AT_NoThreadSafetyAnalysis:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004028 handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004029 break;
4030 case AttributeList::AT_NoSanitizeThread:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004031 handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr);
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004032 break;
4033 case AttributeList::AT_NoSanitizeMemory:
Aaron Ballman6f9165a2013-11-27 15:24:06 +00004034 handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004035 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004036 case AttributeList::AT_Lockable:
Aaron Ballman57ede3b2013-11-27 19:35:27 +00004037 handleSimpleAttribute<LockableAttr>(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004038 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004039 handleGuardedByAttr(S, D, Attr);
4040 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004041 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004042 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004043 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004044 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004045 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004046 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004047 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004048 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004049 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004050 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004051 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004052 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004053 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004054 handleLockReturnedAttr(S, D, Attr);
4055 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004056 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004057 handleLocksExcludedAttr(S, D, Attr);
4058 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004059 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004060 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004061 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004062 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004063 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004064 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004065 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004066 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004067 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004068 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004069 handleUnlockFunAttr(S, D, Attr);
4070 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004071 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004072 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004073 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004074 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004075 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004076 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004077
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004078 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004079 case AttributeList::AT_Consumable:
4080 handleConsumableAttr(S, D, Attr);
4081 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004082 case AttributeList::AT_CallableWhen:
4083 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004084 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004085 case AttributeList::AT_ParamTypestate:
4086 handleParamTypestateAttr(S, D, Attr);
4087 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004088 case AttributeList::AT_ReturnTypestate:
4089 handleReturnTypestateAttr(S, D, Attr);
4090 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004091 case AttributeList::AT_SetTypestate:
4092 handleSetTypestateAttr(S, D, Attr);
4093 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004094 case AttributeList::AT_TestTypestate:
4095 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004096 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004097
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004098 // Type safety attributes.
4099 case AttributeList::AT_ArgumentWithTypeTag:
4100 handleArgumentWithTypeTagAttr(S, D, Attr);
4101 break;
4102 case AttributeList::AT_TypeTagForDatatype:
4103 handleTypeTagForDatatypeAttr(S, D, Attr);
4104 break;
4105
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004106 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004107 // Ask target about the attribute.
4108 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4109 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004110 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4111 diag::warn_unhandled_ms_attribute_ignored :
4112 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004113 break;
4114 }
4115}
4116
4117/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4118/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004119void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004120 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004121 bool IncludeCXX11Attributes) {
4122 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004123 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004124
Joey Gouly2cd9db12013-12-13 16:15:28 +00004125 // FIXME: We should be able to handle these cases in TableGen.
Rafael Espindolac18086a2010-02-23 22:00:30 +00004126 // GCC accepts
4127 // static int a9 __attribute__((weakref));
4128 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004129 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004130 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00004131 cast<NamedDecl>(D)->getNameAsString();
4132 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004133 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004134 }
Joey Gouly2cd9db12013-12-13 16:15:28 +00004135
4136 if (!D->hasAttr<OpenCLKernelAttr>()) {
4137 // These attributes cannot be applied to a non-kernel function.
Aaron Ballman3e424b52013-12-26 18:30:57 +00004138 if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
4139 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004140 D->setInvalidDecl();
4141 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004142 if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) {
4143 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004144 D->setInvalidDecl();
4145 }
Aaron Ballman3e424b52013-12-26 18:30:57 +00004146 if (Attr *A = D->getAttr<VecTypeHintAttr>()) {
4147 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
Joey Gouly2cd9db12013-12-13 16:15:28 +00004148 D->setInvalidDecl();
4149 }
4150 }
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004151}
4152
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004153// Annotation attributes are the only attributes allowed after an access
4154// specifier.
4155bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4156 const AttributeList *AttrList) {
4157 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004158 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004159 handleAnnotateAttr(*this, ASDecl, *l);
4160 } else {
4161 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4162 return true;
4163 }
4164 }
4165
4166 return false;
4167}
4168
John McCall42856de2011-10-01 05:17:03 +00004169/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4170/// contains any decl attributes that we should warn about.
4171static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4172 for ( ; A; A = A->getNext()) {
4173 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004174 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004175 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4176
4177 if (A->getKind() == AttributeList::UnknownAttribute) {
4178 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4179 << A->getName() << A->getRange();
4180 } else {
4181 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4182 << A->getName() << A->getRange();
4183 }
4184 }
4185}
4186
4187/// checkUnusedDeclAttributes - Given a declarator which is not being
4188/// used to build a declaration, complain about any decl attributes
4189/// which might be lying around on it.
4190void Sema::checkUnusedDeclAttributes(Declarator &D) {
4191 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4192 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4193 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4194 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4195}
4196
Ryan Flynn7d470f32009-07-30 03:15:39 +00004197/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004198/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004199NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4200 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004201 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004202 NamedDecl *NewD = 0;
4203 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004204 FunctionDecl *NewFD;
4205 // FIXME: Missing call to CheckFunctionDeclaration().
4206 // FIXME: Mangling?
4207 // FIXME: Is the qualifier info correct?
4208 // FIXME: Is the DeclContext correct?
4209 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4210 Loc, Loc, DeclarationName(II),
4211 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004212 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00004213 FD->hasPrototype(),
4214 false/*isConstexprSpecified*/);
4215 NewD = NewFD;
4216
4217 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004218 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004219
4220 // Fake up parameter variables; they are declared as if this were
4221 // a typedef.
4222 QualType FDTy = FD->getType();
4223 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4224 SmallVector<ParmVarDecl*, 16> Params;
4225 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4226 AE = FT->arg_type_end(); AI != AE; ++AI) {
4227 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4228 Param->setScopeInfo(0, Params.size());
4229 Params.push_back(Param);
4230 }
David Blaikie9c70e042011-09-21 18:16:56 +00004231 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004232 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004233 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4234 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004235 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004236 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004237 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00004238 if (VD->getQualifier()) {
4239 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004240 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004241 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004242 }
4243 return NewD;
4244}
4245
James Dennett634962f2012-06-14 21:40:34 +00004246/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004247/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004248void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004249 if (W.getUsed()) return; // only do this once
4250 W.setUsed(true);
4251 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4252 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004253 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004254 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4255 NDId->getName()));
4256 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00004257 WeakTopLevelDecl.push_back(NewD);
4258 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4259 // to insert Decl at TU scope, sorry.
4260 DeclContext *SavedContext = CurContext;
4261 CurContext = Context.getTranslationUnitDecl();
4262 PushOnScopeChains(NewD, S);
4263 CurContext = SavedContext;
4264 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004265 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004266 }
4267}
4268
Rafael Espindolade6a39f2013-03-02 21:41:48 +00004269void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
4270 // It's valid to "forward-declare" #pragma weak, in which case we
4271 // have to do this.
4272 LoadExternalWeakUndeclaredIdentifiers();
4273 if (!WeakUndeclaredIdentifiers.empty()) {
4274 NamedDecl *ND = NULL;
4275 if (VarDecl *VD = dyn_cast<VarDecl>(D))
4276 if (VD->isExternC())
4277 ND = VD;
4278 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
4279 if (FD->isExternC())
4280 ND = FD;
4281 if (ND) {
4282 if (IdentifierInfo *Id = ND->getIdentifier()) {
4283 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4284 = WeakUndeclaredIdentifiers.find(Id);
4285 if (I != WeakUndeclaredIdentifiers.end()) {
4286 WeakInfo W = I->second;
4287 DeclApplyPragmaWeak(S, ND, W);
4288 WeakUndeclaredIdentifiers[Id] = W;
4289 }
4290 }
4291 }
4292 }
4293}
4294
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004295/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4296/// it, apply them to D. This is a bit tricky because PD can have attributes
4297/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00004298void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004299 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004300 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00004301 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004302
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004303 // Walk the declarator structure, applying decl attributes that were in a type
4304 // position to the decl itself. This handles cases like:
4305 // int *__attr__(x)** D;
4306 // when X is a decl attribute.
4307 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4308 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00004309 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004310
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004311 // Finally, apply any attributes on the decl itself.
4312 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00004313 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004314}
John McCall28a6aea2009-11-04 02:18:39 +00004315
John McCall31168b02011-06-15 23:02:42 +00004316/// Is the given declaration allowed to use a forbidden type?
4317static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4318 // Private ivars are always okay. Unfortunately, people don't
4319 // always properly make their ivars private, even in system headers.
4320 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004321 // Function declarations in sys headers will be marked unavailable.
4322 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4323 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004324 return false;
4325
4326 // Require it to be declared in a system header.
4327 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4328}
4329
4330/// Handle a delayed forbidden-type diagnostic.
4331static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4332 Decl *decl) {
4333 if (decl && isForbiddenTypeAllowed(S, decl)) {
4334 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4335 "this system declaration uses an unsupported type"));
4336 return;
4337 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004338 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004339 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004340 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004341 // kind of forbidden type messages on unavailable functions.
4342 if (FD->hasAttr<UnavailableAttr>() &&
4343 diag.getForbiddenTypeDiagnostic() ==
4344 diag::err_arc_array_param_no_ownership) {
4345 diag.Triggered = true;
4346 return;
4347 }
4348 }
John McCall31168b02011-06-15 23:02:42 +00004349
4350 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4351 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4352 diag.Triggered = true;
4353}
4354
John McCall2ec85372012-05-07 06:16:41 +00004355void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4356 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004357 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004358 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004359
John McCall2ec85372012-05-07 06:16:41 +00004360 // When delaying diagnostics to run in the context of a parsed
4361 // declaration, we only want to actually emit anything if parsing
4362 // succeeds.
4363 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004364
John McCall2ec85372012-05-07 06:16:41 +00004365 // We emit all the active diagnostics in this pool or any of its
4366 // parents. In general, we'll get one pool for the decl spec
4367 // and a child pool for each declarator; in a decl group like:
4368 // deprecated_typedef foo, *bar, baz();
4369 // only the declarator pops will be passed decls. This is correct;
4370 // we really do need to consider delayed diagnostics from the decl spec
4371 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004372 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004373 do {
John McCall6347b682012-05-07 06:16:58 +00004374 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004375 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4376 // This const_cast is a bit lame. Really, Triggered should be mutable.
4377 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004378 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004379 continue;
4380
John McCallc1465822011-02-14 07:13:47 +00004381 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004382 case DelayedDiagnostic::Deprecation:
Ted Kremenekb79ee572013-12-18 23:30:06 +00004383 case DelayedDiagnostic::Unavailable:
4384 // Don't bother giving deprecation/unavailable diagnostics if
4385 // the decl is invalid.
John McCall18a962b2012-01-26 20:04:03 +00004386 if (!decl->isInvalidDecl())
Ted Kremenekb79ee572013-12-18 23:30:06 +00004387 HandleDelayedAvailabilityCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004388 break;
4389
4390 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004391 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004392 break;
John McCall31168b02011-06-15 23:02:42 +00004393
4394 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004395 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004396 break;
John McCall86121512010-01-27 03:50:35 +00004397 }
4398 }
John McCall2ec85372012-05-07 06:16:41 +00004399 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004400}
4401
John McCall6347b682012-05-07 06:16:58 +00004402/// Given a set of delayed diagnostics, re-emit them as if they had
4403/// been delayed in the current context instead of in the given pool.
4404/// Essentially, this just moves them to the current pool.
4405void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4406 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4407 assert(curPool && "re-emitting in undelayed context not supported");
4408 curPool->steal(pool);
4409}
4410
John McCall28a6aea2009-11-04 02:18:39 +00004411static bool isDeclDeprecated(Decl *D) {
4412 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004413 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004414 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004415 // A category implicitly has the availability of the interface.
4416 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4417 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004418 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4419 return false;
4420}
4421
Ted Kremenekb79ee572013-12-18 23:30:06 +00004422static bool isDeclUnavailable(Decl *D) {
4423 do {
4424 if (D->isUnavailable())
4425 return true;
4426 // A category implicitly has the availability of the interface.
4427 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4428 return CatD->getClassInterface()->isUnavailable();
4429 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4430 return false;
4431}
4432
Eli Friedman971bfa12012-08-08 21:52:41 +00004433static void
Ted Kremenekb79ee572013-12-18 23:30:06 +00004434DoEmitAvailabilityWarning(Sema &S,
4435 DelayedDiagnostic::DDKind K,
4436 Decl *Ctx,
4437 const NamedDecl *D,
4438 StringRef Message,
4439 SourceLocation Loc,
4440 const ObjCInterfaceDecl *UnknownObjCClass,
4441 const ObjCPropertyDecl *ObjCProperty) {
4442
4443 // Diagnostics for deprecated or unavailable.
4444 unsigned diag, diag_message, diag_fwdclass_message;
4445
4446 // Matches 'diag::note_property_attribute' options.
4447 unsigned property_note_select;
4448
4449 // Matches diag::note_availability_specified_here.
4450 unsigned available_here_select_kind;
4451
4452 // Don't warn if our current context is deprecated or unavailable.
4453 switch (K) {
4454 case DelayedDiagnostic::Deprecation:
4455 if (isDeclDeprecated(Ctx))
4456 return;
4457 diag = diag::warn_deprecated;
4458 diag_message = diag::warn_deprecated_message;
4459 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
4460 property_note_select = /* deprecated */ 0;
4461 available_here_select_kind = /* deprecated */ 2;
4462 break;
4463
4464 case DelayedDiagnostic::Unavailable:
4465 if (isDeclUnavailable(Ctx))
4466 return;
4467 diag = diag::err_unavailable;
4468 diag_message = diag::err_unavailable_message;
4469 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
4470 property_note_select = /* unavailable */ 1;
4471 available_here_select_kind = /* unavailable */ 0;
4472 break;
4473
4474 default:
4475 llvm_unreachable("Neither a deprecation or unavailable kind");
4476 }
4477
Eli Friedman971bfa12012-08-08 21:52:41 +00004478 DeclarationName Name = D->getDeclName();
4479 if (!Message.empty()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004480 S.Diag(Loc, diag_message) << Name << Message;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004481 if (ObjCProperty)
4482 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4483 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004484 } else if (!UnknownObjCClass) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004485 S.Diag(Loc, diag) << Name;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004486 if (ObjCProperty)
4487 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
4488 << ObjCProperty->getDeclName() << property_note_select;
Eli Friedman971bfa12012-08-08 21:52:41 +00004489 } else {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004490 S.Diag(Loc, diag_fwdclass_message) << Name;
Eli Friedman971bfa12012-08-08 21:52:41 +00004491 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4492 }
Ted Kremenekb79ee572013-12-18 23:30:06 +00004493
4494 S.Diag(D->getLocation(), diag::note_availability_specified_here)
4495 << D << available_here_select_kind;
Eli Friedman971bfa12012-08-08 21:52:41 +00004496}
4497
Ted Kremenekb79ee572013-12-18 23:30:06 +00004498void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD,
4499 Decl *Ctx) {
John McCall86121512010-01-27 03:50:35 +00004500 DD.Triggered = true;
Ted Kremenekb79ee572013-12-18 23:30:06 +00004501 DoEmitAvailabilityWarning(*this,
4502 (DelayedDiagnostic::DDKind) DD.Kind,
4503 Ctx,
4504 DD.getDeprecationDecl(),
4505 DD.getDeprecationMessage(),
4506 DD.Loc,
4507 DD.getUnknownObjCClass(),
4508 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004509}
4510
Ted Kremenekb79ee572013-12-18 23:30:06 +00004511void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD,
4512 NamedDecl *D, StringRef Message,
4513 SourceLocation Loc,
4514 const ObjCInterfaceDecl *UnknownObjCClass,
4515 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004516 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004517 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Ted Kremenekb79ee572013-12-18 23:30:06 +00004518 DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D,
4519 UnknownObjCClass,
4520 ObjCProperty,
4521 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004522 return;
4523 }
4524
Ted Kremenekb79ee572013-12-18 23:30:06 +00004525 Decl *Ctx = cast<Decl>(getCurLexicalContext());
4526 DelayedDiagnostic::DDKind K;
4527 switch (AD) {
4528 case AD_Deprecation:
4529 K = DelayedDiagnostic::Deprecation;
4530 break;
4531 case AD_Unavailable:
4532 K = DelayedDiagnostic::Unavailable;
4533 break;
4534 }
4535
4536 DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc,
4537 UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004538}