blob: 76cbaa297f13531e8aab85e0bb639840c96362b4 [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
John McCall5fca7ea2011-03-02 12:29:23 +000035/// These constants match the enumerated choices of
36/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000037enum AttributeDeclKind {
John McCall5fca7ea2011-03-02 12:29:23 +000038 ExpectedFunction,
39 ExpectedUnion,
40 ExpectedVariableOrFunction,
41 ExpectedFunctionOrMethod,
42 ExpectedParameter,
John McCall5fca7ea2011-03-02 12:29:23 +000043 ExpectedFunctionMethodOrBlock,
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +000044 ExpectedFunctionMethodOrClass,
John McCall5fca7ea2011-03-02 12:29:23 +000045 ExpectedFunctionMethodOrParameter,
46 ExpectedClass,
John McCall5fca7ea2011-03-02 12:29:23 +000047 ExpectedVariable,
48 ExpectedMethod,
Caitlin Sadowski63fa6672011-07-28 20:12:35 +000049 ExpectedVariableFunctionOrLabel,
Douglas Gregor5c3cc422012-03-14 16:55:17 +000050 ExpectedFieldOrGlobalVar,
Hans Wennborgd3b01bc2012-06-23 11:51:46 +000051 ExpectedStruct,
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +000052 ExpectedVariableFunctionOrTag,
Richard Smith9eaab4b2013-02-01 08:25:07 +000053 ExpectedTLSVar,
54 ExpectedVariableOrField,
John McCalld041a9b2013-02-20 01:54:26 +000055 ExpectedVariableFieldOrTag,
Aaron Ballmanf90ccb02013-07-18 14:56:42 +000056 ExpectedTypeOrNamespace,
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +000057 ExpectedObjectiveCInterface,
Fariborz Jahanianf720f862013-11-19 17:42:25 +000058 ExpectedMethodOrProperty,
59 ExpectedStructOrUnion,
Aaron Ballmandbb634f2013-11-20 01:35:23 +000060 ExpectedStructOrUnionOrClass,
61 ExpectedType
John McCall5fca7ea2011-03-02 12:29:23 +000062};
63
Chris Lattner58418ff2008-06-29 00:16:31 +000064//===----------------------------------------------------------------------===//
65// Helper functions
66//===----------------------------------------------------------------------===//
67
Chandler Carruthff4c4f02011-07-01 23:49:12 +000068static const FunctionType *getFunctionType(const Decl *D,
Ted Kremenek527042b2009-08-14 20:49:40 +000069 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000070 QualType Ty;
Chandler Carruthff4c4f02011-07-01 23:49:12 +000071 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000072 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000073 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000074 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000075 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000076 Ty = decl->getUnderlyingType();
77 else
78 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000079
Chris Lattner2c6fcf52008-06-26 18:38:35 +000080 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000081 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000082 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000083 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000084
John McCall9dd450b2009-09-21 23:43:11 +000085 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000086}
87
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000088// FIXME: We should provide an abstraction around a method or function
89// to provide the following bits of information.
90
Nuno Lopes518e3702009-12-20 23:11:08 +000091/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000092/// type (function or function-typed variable).
Chandler Carruthff4c4f02011-07-01 23:49:12 +000093static bool isFunction(const Decl *D) {
94 return getFunctionType(D, false) != NULL;
Ted Kremenek527042b2009-08-14 20:49:40 +000095}
96
97/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000098/// type (function or function-typed variable) or an Objective-C
99/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000100static bool isFunctionOrMethod(const Decl *D) {
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +0000101 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000102}
103
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000104/// isFunctionOrMethodOrBlock - Return true if the given decl has function
105/// type (function or function-typed variable) or an Objective-C
106/// method or a block.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000107static bool isFunctionOrMethodOrBlock(const Decl *D) {
108 if (isFunctionOrMethod(D))
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000109 return true;
110 // check for block is more involved.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000111 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000112 QualType Ty = V->getType();
113 return Ty->isBlockPointerType();
114 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000115 return isa<BlockDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000116}
117
John McCall3882ace2011-01-05 12:14:39 +0000118/// Return true if the given decl has a declarator that should have
119/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000120static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +0000121 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000122 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
123 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +0000124}
125
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000126/// hasFunctionProto - Return true if the given decl has a argument
127/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000128/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000129static bool hasFunctionProto(const Decl *D) {
130 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000131 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000132 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000133 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000134 return true;
135 }
136}
137
138/// getFunctionOrMethodNumArgs - Return number of function or method
139/// arguments. It is an error to call this on a K&R function (use
140/// hasFunctionProto first).
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000141static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
142 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000143 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000144 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000145 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000146 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000147}
148
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000149static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
150 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000151 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000152 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000153 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000154
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000155 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000156}
157
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000158static QualType getFunctionOrMethodResultType(const Decl *D) {
159 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000160 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000161 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000162}
163
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000164static bool isFunctionOrMethodVariadic(const Decl *D) {
165 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000166 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000167 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000168 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000169 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000170 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000171 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000172 }
173}
174
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000175static bool isInstanceMethod(const Decl *D) {
176 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000177 return MethodDecl->isInstance();
178 return false;
179}
180
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000181static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000182 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000183 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000184 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000185
John McCall96fa4842010-05-17 21:00:27 +0000186 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
187 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000188 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000189
John McCall96fa4842010-05-17 21:00:27 +0000190 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000191
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000192 // FIXME: Should we walk the chain of classes?
193 return ClsName == &Ctx.Idents.get("NSString") ||
194 ClsName == &Ctx.Idents.get("NSMutableString");
195}
196
Daniel Dunbar980c6692008-09-26 03:32:58 +0000197static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000198 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000199 if (!PT)
200 return false;
201
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000202 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000203 if (!RT)
204 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000205
Daniel Dunbar980c6692008-09-26 03:32:58 +0000206 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000207 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000208 return false;
209
210 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
211}
212
Richard Smithb87c4652013-10-31 21:23:20 +0000213static unsigned getNumAttributeArgs(const AttributeList &Attr) {
214 // FIXME: Include the type in the argument list.
215 return Attr.getNumArgs() + Attr.hasParsedType();
216}
217
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000218/// \brief Check if the attribute has exactly as many args as Num. May
219/// output an error.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000220static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000221 unsigned Num) {
222 if (getNumAttributeArgs(Attr) != Num) {
Aaron Ballmanb7243382013-07-23 19:30:11 +0000223 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
224 << Attr.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000225 return false;
226 }
227
228 return true;
229}
230
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000231
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000232/// \brief Check if the attribute has at least as many args as Num. May
233/// output an error.
234static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000235 unsigned Num) {
236 if (getNumAttributeArgs(Attr) < Num) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000237 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
238 return false;
239 }
240
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000241 return true;
242}
243
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000244/// \brief If Expr is a valid integer constant, get the value of the integer
245/// expression and return success or failure. May output an error.
246static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
247 const Expr *Expr, uint32_t &Val,
248 unsigned Idx = UINT_MAX) {
249 llvm::APSInt I(32);
250 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
251 !Expr->isIntegerConstantExpr(I, S.Context)) {
252 if (Idx != UINT_MAX)
253 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
254 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
255 << Expr->getSourceRange();
256 else
257 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
258 << Attr.getName() << AANT_ArgumentIntegerConstant
259 << Expr->getSourceRange();
260 return false;
261 }
262 Val = (uint32_t)I.getZExtValue();
263 return true;
264}
265
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000266/// \brief Check if IdxExpr is a valid argument index for a function or
267/// instance method D. May output an error.
268///
269/// \returns true if IdxExpr is a valid index.
270static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
271 StringRef AttrName,
272 SourceLocation AttrLoc,
273 unsigned AttrArgNum,
274 const Expr *IdxExpr,
275 uint64_t &Idx)
276{
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000277 assert(isFunctionOrMethod(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000278
279 // In C++ the implicit 'this' function parameter also counts.
280 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000281 bool HP = hasFunctionProto(D);
282 bool HasImplicitThisParam = isInstanceMethod(D);
283 bool IV = HP && isFunctionOrMethodVariadic(D);
284 unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) +
285 HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000286
287 llvm::APSInt IdxInt;
288 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
289 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +0000290 std::string Name = std::string("'") + AttrName.str() + std::string("'");
291 S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str()
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000292 << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000293 return false;
294 }
295
296 Idx = IdxInt.getLimitedValue();
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000297 if (Idx < 1 || (!IV && Idx > NumArgs)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000298 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
299 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
300 return false;
301 }
302 Idx--; // Convert to zero-based.
303 if (HasImplicitThisParam) {
304 if (Idx == 0) {
305 S.Diag(AttrLoc,
306 diag::err_attribute_invalid_implicit_this_argument)
307 << AttrName << IdxExpr->getSourceRange();
308 return false;
309 }
310 --Idx;
311 }
312
313 return true;
314}
315
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000316/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
317/// If not emit an error and return false. If the argument is an identifier it
318/// will emit an error with a fixit hint and treat it as if it was a string
319/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000320bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
321 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000322 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000323 // Look for identifiers. If we have one emit a hint to fix it to a literal.
324 if (Attr.isArgIdent(ArgNum)) {
325 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000326 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000327 << Attr.getName() << AANT_ArgumentString
328 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000329 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000330 Str = Loc->Ident->getName();
331 if (ArgLocation)
332 *ArgLocation = Loc->Loc;
333 return true;
334 }
335
336 // Now check for an actual string literal.
337 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
338 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
339 if (ArgLocation)
340 *ArgLocation = ArgExpr->getLocStart();
341
342 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000343 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000344 << Attr.getName() << AANT_ArgumentString;
345 return false;
346 }
347
348 Str = Literal->getString();
349 return true;
350}
351
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000352///
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000353/// \brief Check if passed in Decl is a field or potentially shared global var
354/// \return true if the Decl is a field or potentially shared global variable
355///
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000356static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000357 if (isa<FieldDecl>(D))
358 return true;
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000359 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Richard Smithfd3834f2013-04-13 02:43:54 +0000360 return vd->hasGlobalStorage() && !vd->getTLSKind();
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000361
362 return false;
363}
364
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000365/// \brief Check if the passed-in expression is of type int or bool.
366static bool isIntOrBool(Expr *Exp) {
367 QualType QT = Exp->getType();
368 return QT->isBooleanType() || QT->isIntegerType();
369}
370
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000371
372// Check to see if the type is a smart pointer of some kind. We assume
373// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000374static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
375 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
376 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000377 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000378 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000379
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000380 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
381 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000382 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000383 return false;
384
385 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000386}
387
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000388/// \brief Check if passed in Decl is a pointer type.
389/// Note that this function may produce an error message.
390/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000391static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
392 const AttributeList &Attr) {
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000393 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000394 QualType QT = vd->getType();
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000395 if (QT->isAnyPointerType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000396 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000397
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000398 if (const RecordType *RT = QT->getAs<RecordType>()) {
399 // If it's an incomplete type, it could be a smart pointer; skip it.
400 // (We don't want to force template instantiation if we can avoid it,
401 // since that would alter the order in which templates are instantiated.)
402 if (RT->isIncompleteType())
403 return true;
404
405 if (threadSafetyCheckIsSmartPointer(S, RT))
406 return true;
407 }
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000408
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000409 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000410 << Attr.getName()->getName() << QT;
411 } else {
412 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
413 << Attr.getName();
414 }
415 return false;
416}
417
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000418/// \brief Checks that the passed in QualType either is of RecordType or points
419/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000420static const RecordType *getRecordType(QualType QT) {
421 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000422 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000423
424 // Now check if we point to record type.
425 if (const PointerType *PT = QT->getAs<PointerType>())
426 return PT->getPointeeType()->getAs<RecordType>();
427
428 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000429}
430
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000431
Jordy Rose740b0c22012-05-08 03:27:22 +0000432static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
433 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000434 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
435 if (RT->getDecl()->getAttr<LockableAttr>())
436 return true;
437 return false;
438}
439
440
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000441/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000442/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000443static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
444 QualType Ty) {
445 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000446
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000447 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000448 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000449 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000450 << Attr.getName() << Ty.getAsString();
451 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000452 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000453
Michael Hana9171bc2012-08-03 17:40:43 +0000454 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000455 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000456 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000457
458 // Allow smart pointers to be used as lockable objects.
459 // FIXME -- Check the type that the smart pointer points to.
460 if (threadSafetyCheckIsSmartPointer(S, RT))
461 return;
462
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000463 // Check if the type is lockable.
464 RecordDecl *RD = RT->getDecl();
465 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000466 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000467
468 // Else check if any base classes are lockable.
469 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
470 CXXBasePaths BPaths(false, false);
471 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
472 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000473 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000474
475 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
476 << Attr.getName() << Ty.getAsString();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000477}
478
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000479/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000480/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000481/// \param Sidx The attribute argument index to start checking with.
482/// \param ParamIdxOk Whether an argument can be indexing into a function
483/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000484static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000485 const AttributeList &Attr,
486 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000487 int Sidx = 0,
488 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000489 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000490 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000491
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000492 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000493 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000494 Args.push_back(ArgExp);
495 continue;
496 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000497
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000498 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000499 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000500 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000501 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000502 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000503 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000504 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000505 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000506
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000507 // We allow constant strings to be used as a placeholder for expressions
508 // that are not valid C++ syntax, but warn that they are ignored.
509 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
510 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000511 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000512 continue;
513 }
514
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000515 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000516
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000517 // A pointer to member expression of the form &MyClass::mu is treated
518 // specially -- we need to look at the type of the member.
519 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
520 if (UOp->getOpcode() == UO_AddrOf)
521 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
522 if (DRE->getDecl()->isCXXInstanceMember())
523 ArgTy = DRE->getDecl()->getType();
524
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000525 // First see if we can just cast to record type, or point to record type.
526 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000527
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000528 // Now check if we index into a record type function param.
529 if(!RT && ParamIdxOk) {
530 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000531 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
532 if(FD && IL) {
533 unsigned int NumParams = FD->getNumParams();
534 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000535 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
536 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
537 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000538 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
539 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000540 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000541 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000542 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000543 }
544 }
545
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000546 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000547
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000548 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000549 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000550}
551
Chris Lattner58418ff2008-06-29 00:16:31 +0000552//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000553// Attribute Implementations
554//===----------------------------------------------------------------------===//
555
Daniel Dunbar032db472008-07-31 22:40:48 +0000556// FIXME: All this manual attribute parsing code is gross. At the
557// least add some helper functions to check most argument patterns (#
558// and types of args).
559
Michael Hana9171bc2012-08-03 17:40:43 +0000560static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000561 const AttributeList &Attr) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000562 // D must be either a member field or global (potentially shared) variable.
563 if (!mayBeSharedVariable(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000564 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
565 << Attr.getName() << ExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000566 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000567 }
568
Michael Han3be3b442012-07-23 18:48:41 +0000569 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000570}
571
Michael Han3be3b442012-07-23 18:48:41 +0000572static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
573 if (!checkGuardedVarAttrCommon(S, D, Attr))
574 return;
Michael Hana9171bc2012-08-03 17:40:43 +0000575
Michael Han99315932013-01-24 16:46:58 +0000576 D->addAttr(::new (S.Context)
577 GuardedVarAttr(Attr.getRange(), S.Context,
578 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000579}
580
Michael Hana9171bc2012-08-03 17:40:43 +0000581static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000582 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000583 if (!checkGuardedVarAttrCommon(S, D, Attr))
584 return;
585
586 if (!threadSafetyCheckIsPointer(S, D, Attr))
587 return;
588
Michael Han99315932013-01-24 16:46:58 +0000589 D->addAttr(::new (S.Context)
590 PtGuardedVarAttr(Attr.getRange(), S.Context,
591 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000592}
593
Michael Hana9171bc2012-08-03 17:40:43 +0000594static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
595 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000596 Expr* &Arg) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000597 // D must be either a member field or global (potentially shared) variable.
598 if (!mayBeSharedVariable(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000599 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
600 << Attr.getName() << ExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000601 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000602 }
603
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000604 SmallVector<Expr*, 1> Args;
605 // check that all arguments are lockable objects
606 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
607 unsigned Size = Args.size();
608 if (Size != 1)
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 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000612
Michael Han3be3b442012-07-23 18:48:41 +0000613 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000614}
615
Michael Han3be3b442012-07-23 18:48:41 +0000616static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
617 Expr *Arg = 0;
618 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
619 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000620
Michael Han3be3b442012-07-23 18:48:41 +0000621 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
622}
623
Michael Hana9171bc2012-08-03 17:40:43 +0000624static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000625 const AttributeList &Attr) {
626 Expr *Arg = 0;
627 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
628 return;
629
630 if (!threadSafetyCheckIsPointer(S, D, Attr))
631 return;
632
633 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
634 S.Context, Arg));
635}
636
Michael Hana9171bc2012-08-03 17:40:43 +0000637static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000638 const AttributeList &Attr) {
Caitlin Sadowski086fb952011-09-16 00:35:54 +0000639 // FIXME: Lockable structs for C code.
David Blaikie021221d2013-07-29 18:24:03 +0000640 if (!isa<RecordDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000641 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
642 << Attr.getName() << ExpectedStructOrUnionOrClass;
Michael Han3be3b442012-07-23 18:48:41 +0000643 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000644 }
645
Michael Han3be3b442012-07-23 18:48:41 +0000646 return true;
647}
648
649static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
650 if (!checkLockableAttrCommon(S, D, Attr))
651 return;
652
653 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
654}
655
Michael Hana9171bc2012-08-03 17:40:43 +0000656static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000657 const AttributeList &Attr) {
658 if (!checkLockableAttrCommon(S, D, Attr))
659 return;
660
Michael Han99315932013-01-24 16:46:58 +0000661 D->addAttr(::new (S.Context)
662 ScopedLockableAttr(Attr.getRange(), S.Context,
663 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000664}
665
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000666static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D,
667 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000668 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000669 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
670 << Attr.getName() << ExpectedFunctionOrMethod;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000671 return;
672 }
673
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000674 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000675 S.Context));
676}
677
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000678static void handleNoSanitizeAddressAttr(Sema &S, Decl *D,
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000679 const AttributeList &Attr) {
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000680 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000681 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000682 << Attr.getName() << ExpectedFunctionOrMethod;
683 return;
684 }
685
Michael Han99315932013-01-24 16:46:58 +0000686 D->addAttr(::new (S.Context)
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000687 NoSanitizeAddressAttr(Attr.getRange(), S.Context,
688 Attr.getAttributeSpellingListIndex()));
689}
690
691static void handleNoSanitizeMemory(Sema &S, Decl *D,
692 const AttributeList &Attr) {
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000693 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
694 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
695 << Attr.getName() << ExpectedFunctionOrMethod;
696 return;
697 }
698
699 D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(),
700 S.Context));
701}
702
703static void handleNoSanitizeThread(Sema &S, Decl *D,
704 const AttributeList &Attr) {
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000705 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
706 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
707 << Attr.getName() << ExpectedFunctionOrMethod;
708 return;
709 }
710
711 D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(),
712 S.Context));
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000713}
714
Michael Hana9171bc2012-08-03 17:40:43 +0000715static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
716 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000717 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000718 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000719 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000720
721 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000722 ValueDecl *VD = dyn_cast<ValueDecl>(D);
723 if (!VD || !mayBeSharedVariable(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000724 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
725 << Attr.getName() << ExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000726 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000727 }
728
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000729 // Check that this attribute only applies to lockable types.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000730 QualType QT = VD->getType();
731 if (!QT->isDependentType()) {
732 const RecordType *RT = getRecordType(QT);
733 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000734 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000735 << Attr.getName();
736 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000737 }
738 }
739
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000740 // Check that all arguments are lockable objects.
741 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000742 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000743 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000744
Michael Han3be3b442012-07-23 18:48:41 +0000745 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000746}
747
Michael Hana9171bc2012-08-03 17:40:43 +0000748static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000749 const AttributeList &Attr) {
750 SmallVector<Expr*, 1> Args;
751 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
752 return;
753
754 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000755 D->addAttr(::new (S.Context)
756 AcquiredAfterAttr(Attr.getRange(), S.Context,
757 StartArg, Args.size(),
758 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000759}
760
Michael Hana9171bc2012-08-03 17:40:43 +0000761static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000762 const AttributeList &Attr) {
763 SmallVector<Expr*, 1> Args;
764 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
765 return;
766
767 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000768 D->addAttr(::new (S.Context)
769 AcquiredBeforeAttr(Attr.getRange(), S.Context,
770 StartArg, Args.size(),
771 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000772}
773
Michael Hana9171bc2012-08-03 17:40:43 +0000774static bool checkLockFunAttrCommon(Sema &S, Decl *D,
775 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000776 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000777 // zero or more arguments ok
778
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000779 // check that the attribute is applied to a function
780 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000781 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
782 << Attr.getName() << ExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000783 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000784 }
785
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000786 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000787 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000788
Michael Han3be3b442012-07-23 18:48:41 +0000789 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000790}
791
Michael Hana9171bc2012-08-03 17:40:43 +0000792static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000793 const AttributeList &Attr) {
794 SmallVector<Expr*, 1> Args;
795 if (!checkLockFunAttrCommon(S, D, Attr, Args))
796 return;
797
798 unsigned Size = Args.size();
799 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000800 D->addAttr(::new (S.Context)
801 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
802 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000803}
804
Michael Hana9171bc2012-08-03 17:40:43 +0000805static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000806 const AttributeList &Attr) {
807 SmallVector<Expr*, 1> Args;
808 if (!checkLockFunAttrCommon(S, D, Attr, Args))
809 return;
810
811 unsigned Size = Args.size();
812 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000813 D->addAttr(::new (S.Context)
814 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
815 StartArg, Size,
816 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000817}
818
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000819static void handleAssertSharedLockAttr(Sema &S, Decl *D,
820 const AttributeList &Attr) {
821 SmallVector<Expr*, 1> Args;
822 if (!checkLockFunAttrCommon(S, D, Attr, Args))
823 return;
824
825 unsigned Size = Args.size();
826 Expr **StartArg = Size == 0 ? 0 : &Args[0];
827 D->addAttr(::new (S.Context)
828 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
829 Attr.getAttributeSpellingListIndex()));
830}
831
832static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
833 const AttributeList &Attr) {
834 SmallVector<Expr*, 1> Args;
835 if (!checkLockFunAttrCommon(S, D, Attr, Args))
836 return;
837
838 unsigned Size = Args.size();
839 Expr **StartArg = Size == 0 ? 0 : &Args[0];
840 D->addAttr(::new (S.Context)
841 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
842 StartArg, Size,
843 Attr.getAttributeSpellingListIndex()));
844}
845
846
Michael Hana9171bc2012-08-03 17:40:43 +0000847static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
848 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000849 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000850 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000851 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000852
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000853 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000854 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
855 << Attr.getName() << ExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000856 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000857 }
858
Aaron Ballman00e99962013-08-31 01:11:41 +0000859 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000860 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000861 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000862 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000863 }
864
865 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000866 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000867
Michael Han3be3b442012-07-23 18:48:41 +0000868 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000869}
870
Michael Hana9171bc2012-08-03 17:40:43 +0000871static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000872 const AttributeList &Attr) {
873 SmallVector<Expr*, 2> Args;
874 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
875 return;
876
Michael Han99315932013-01-24 16:46:58 +0000877 D->addAttr(::new (S.Context)
878 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000879 Attr.getArgAsExpr(0),
880 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000881 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000882}
883
Michael Hana9171bc2012-08-03 17:40:43 +0000884static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000885 const AttributeList &Attr) {
886 SmallVector<Expr*, 2> Args;
887 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
888 return;
889
Michael Han99315932013-01-24 16:46:58 +0000890 D->addAttr(::new (S.Context)
891 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000892 Attr.getArgAsExpr(0),
893 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000894 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000895}
896
Michael Hana9171bc2012-08-03 17:40:43 +0000897static bool checkLocksRequiredCommon(Sema &S, Decl *D,
898 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000899 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000900 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000901 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000902
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000903 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000904 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
905 << Attr.getName() << ExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000906 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000907 }
908
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000909 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000910 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000911 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000912 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000913
Michael Han3be3b442012-07-23 18:48:41 +0000914 return true;
915}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000916
Michael Hana9171bc2012-08-03 17:40:43 +0000917static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000918 const AttributeList &Attr) {
919 SmallVector<Expr*, 1> Args;
920 if (!checkLocksRequiredCommon(S, D, Attr, Args))
921 return;
922
923 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000924 D->addAttr(::new (S.Context)
925 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
926 StartArg, Args.size(),
927 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000928}
929
Michael Hana9171bc2012-08-03 17:40:43 +0000930static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000931 const AttributeList &Attr) {
932 SmallVector<Expr*, 1> Args;
933 if (!checkLocksRequiredCommon(S, D, Attr, Args))
934 return;
935
936 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000937 D->addAttr(::new (S.Context)
938 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
939 StartArg, Args.size(),
940 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000941}
942
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000943static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000944 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000945 // zero or more arguments ok
946
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000947 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000948 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
949 << Attr.getName() << ExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000950 return;
951 }
952
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000953 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000954 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000955 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000956 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000957 Expr **StartArg = Size == 0 ? 0 : &Args[0];
958
Michael Han99315932013-01-24 16:46:58 +0000959 D->addAttr(::new (S.Context)
960 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
961 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000962}
963
964static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000965 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000966 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000967 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
968 << Attr.getName() << ExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000969 return;
970 }
971
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000972 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000973 SmallVector<Expr*, 1> Args;
974 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
975 unsigned Size = Args.size();
976 if (Size == 0)
977 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000978
Michael Han99315932013-01-24 16:46:58 +0000979 D->addAttr(::new (S.Context)
980 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
981 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000982}
983
984static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000985 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000986 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000987 return;
988
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000989 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000990 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
991 << Attr.getName() << ExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000992 return;
993 }
994
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000995 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000996 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000997 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000998 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000999 if (Size == 0)
1000 return;
1001 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +00001002
Michael Han99315932013-01-24 16:46:58 +00001003 D->addAttr(::new (S.Context)
1004 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
1005 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00001006}
1007
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001008static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +00001009 ConsumableAttr::ConsumedState DefaultState;
1010
1011 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +00001012 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1013 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1014 DefaultState)) {
1015 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
1016 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +00001017 return;
1018 }
David Blaikie16f76d22013-09-06 01:28:43 +00001019 } else {
1020 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1021 << Attr.getName() << AANT_ArgumentIdentifier;
1022 return;
1023 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001024
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001025 if (!isa<CXXRecordDecl>(D)) {
1026 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1027 Attr.getName() << ExpectedClass;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001028 return;
1029 }
1030
1031 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +00001032 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001033 Attr.getAttributeSpellingListIndex()));
1034}
1035
1036static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
1037 const AttributeList &Attr) {
1038 ASTContext &CurrContext = S.getASTContext();
1039 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
1040
1041 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1042 if (!RD->hasAttr<ConsumableAttr>()) {
1043 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
1044 RD->getNameAsString();
1045
1046 return false;
1047 }
1048 }
1049
1050 return true;
1051}
1052
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001053
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001054static void handleCallableWhenAttr(Sema &S, Decl *D,
1055 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001056 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1057 return;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001058
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001059 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001060 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1061 Attr.getName() << ExpectedMethod;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001062 return;
1063 }
1064
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001065 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1066 return;
1067
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001068 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
1069 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
1070 CallableWhenAttr::ConsumedState CallableState;
1071
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001072 StringRef StateString;
1073 SourceLocation Loc;
1074 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
1075 return;
1076
1077 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +00001078 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001079 S.Diag(Loc, diag::warn_attribute_type_not_supported)
1080 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001081 return;
1082 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001083
1084 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001085 }
1086
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001087 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001088 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
1089 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001090}
1091
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001092
DeLesley Hutchins69391772013-10-17 23:23:53 +00001093static void handleParamTypestateAttr(Sema &S, Decl *D,
1094 const AttributeList &Attr) {
1095 if (!checkAttributeNumArgs(S, Attr, 1)) return;
1096
1097 if (!isa<ParmVarDecl>(D)) {
1098 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1099 Attr.getName() << ExpectedParameter;
1100 return;
1101 }
1102
1103 ParamTypestateAttr::ConsumedState ParamState;
1104
1105 if (Attr.isArgIdent(0)) {
1106 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1107 StringRef StateString = Ident->Ident->getName();
1108
1109 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
1110 ParamState)) {
1111 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1112 << Attr.getName() << StateString;
1113 return;
1114 }
1115 } else {
1116 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1117 Attr.getName() << AANT_ArgumentIdentifier;
1118 return;
1119 }
1120
1121 // FIXME: This check is currently being done in the analysis. It can be
1122 // enabled here only after the parser propagates attributes at
1123 // template specialization definition, not declaration.
1124 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
1125 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1126 //
1127 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1128 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1129 // ReturnType.getAsString();
1130 // return;
1131 //}
1132
1133 D->addAttr(::new (S.Context)
1134 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
1135 Attr.getAttributeSpellingListIndex()));
1136}
1137
1138
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001139static void handleReturnTypestateAttr(Sema &S, Decl *D,
1140 const AttributeList &Attr) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001141 if (!checkAttributeNumArgs(S, Attr, 1)) return;
1142
1143 if (!(isa<FunctionDecl>(D) || isa<ParmVarDecl>(D))) {
1144 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1145 Attr.getName() << ExpectedFunctionMethodOrParameter;
1146 return;
1147 }
1148
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001149 ReturnTypestateAttr::ConsumedState ReturnState;
1150
1151 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +00001152 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1153 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1154 ReturnState)) {
1155 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
1156 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001157 return;
1158 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001159 } else {
1160 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1161 Attr.getName() << AANT_ArgumentIdentifier;
1162 return;
1163 }
1164
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001165 // FIXME: This check is currently being done in the analysis. It can be
1166 // enabled here only after the parser propagates attributes at
1167 // template specialization definition, not declaration.
1168 //QualType ReturnType;
1169 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001170 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1171 // ReturnType = Param->getType();
1172 //
1173 //} else if (const CXXConstructorDecl *Constructor =
1174 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001175 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1176 //
1177 //} else {
1178 //
1179 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1180 //}
1181 //
1182 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1183 //
1184 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1185 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1186 // ReturnType.getAsString();
1187 // return;
1188 //}
1189
1190 D->addAttr(::new (S.Context)
1191 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1192 Attr.getAttributeSpellingListIndex()));
1193}
1194
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001195
1196static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001197 if (!checkAttributeNumArgs(S, Attr, 1))
1198 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001199
1200 if (!isa<CXXMethodDecl>(D)) {
1201 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1202 Attr.getName() << ExpectedMethod;
1203 return;
1204 }
1205
1206 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1207 return;
1208
1209 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001210 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001211 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1212 StringRef Param = Ident->Ident->getName();
1213 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1214 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1215 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001216 return;
1217 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001218 } else {
1219 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1220 Attr.getName() << AANT_ArgumentIdentifier;
1221 return;
1222 }
1223
1224 D->addAttr(::new (S.Context)
1225 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1226 Attr.getAttributeSpellingListIndex()));
1227}
1228
Chris Wailes9385f9f2013-10-29 20:28:41 +00001229static void handleTestTypestateAttr(Sema &S, Decl *D,
1230 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001231 if (!checkAttributeNumArgs(S, Attr, 1))
1232 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001233
1234 if (!isa<CXXMethodDecl>(D)) {
1235 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1236 Attr.getName() << ExpectedMethod;
1237 return;
1238 }
1239
1240 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1241 return;
1242
Chris Wailes9385f9f2013-10-29 20:28:41 +00001243 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001244 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001245 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1246 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001247 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001248 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1249 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001250 return;
1251 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001252 } else {
1253 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1254 Attr.getName() << AANT_ArgumentIdentifier;
1255 return;
1256 }
1257
1258 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001259 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001260 Attr.getAttributeSpellingListIndex()));
1261}
1262
Chandler Carruthedc2c642011-07-02 00:01:44 +00001263static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1264 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001265 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1266 if (TD == 0) {
1267 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1268 // and type-ids.
Aaron Ballmandbb634f2013-11-20 01:35:23 +00001269 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) <<
1270 Attr.getName() << ExpectedType;
Chris Lattner4a927cb2008-06-28 23:36:30 +00001271 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001272 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001273
Richard Smith1f5a4322013-01-13 02:11:23 +00001274 // Remember this typedef decl, we will need it later for diagnostics.
1275 S.ExtVectorDecls.push_back(TD);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001276}
1277
Chandler Carruthedc2c642011-07-02 00:01:44 +00001278static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001279 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001280 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001281 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001282 // If the alignment is less than or equal to 8 bits, the packed attribute
1283 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001284 if (!FD->getType()->isDependentType() &&
1285 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001286 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001287 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001288 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001289 else
Michael Han99315932013-01-24 16:46:58 +00001290 FD->addAttr(::new (S.Context)
1291 PackedAttr(Attr.getRange(), S.Context,
1292 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001293 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001294 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001295}
1296
Chandler Carruthedc2c642011-07-02 00:01:44 +00001297static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman9ee2d0472012-10-12 23:29:20 +00001298 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001299 RD->addAttr(::new (S.Context)
1300 MsStructAttr(Attr.getRange(), S.Context,
1301 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +00001302 else
Aaron Ballmanb80f94b2013-11-20 22:22:04 +00001303 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1304 << Attr.getName() << ExpectedStructOrUnionOrClass;
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +00001305}
1306
Chandler Carruthedc2c642011-07-02 00:01:44 +00001307static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek1f672822010-02-18 03:08:58 +00001308 // The IBAction attributes only apply to instance methods.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001309 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek1f672822010-02-18 03:08:58 +00001310 if (MD->isInstanceMethod()) {
Michael Han99315932013-01-24 16:46:58 +00001311 D->addAttr(::new (S.Context)
1312 IBActionAttr(Attr.getRange(), S.Context,
1313 Attr.getAttributeSpellingListIndex()));
Ted Kremenek1f672822010-02-18 03:08:58 +00001314 return;
1315 }
1316
Ted Kremenekd68ec812011-02-04 06:54:16 +00001317 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek1f672822010-02-18 03:08:58 +00001318}
1319
Ted Kremenek7fd17232011-09-29 07:02:25 +00001320static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1321 // The IBOutlet/IBOutletCollection attributes only apply to instance
1322 // variables or properties of Objective-C classes. The outlet must also
1323 // have an object reference type.
1324 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1325 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001326 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001327 << Attr.getName() << VD->getType() << 0;
1328 return false;
1329 }
1330 }
1331 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1332 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001333 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001334 << Attr.getName() << PD->getType() << 1;
1335 return false;
1336 }
1337 }
1338 else {
1339 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1340 return false;
1341 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001342
Ted Kremenek7fd17232011-09-29 07:02:25 +00001343 return true;
1344}
1345
Chandler Carruthedc2c642011-07-02 00:01:44 +00001346static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001347 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001348 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001349
Michael Han99315932013-01-24 16:46:58 +00001350 D->addAttr(::new (S.Context)
1351 IBOutletAttr(Attr.getRange(), S.Context,
1352 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001353}
1354
Chandler Carruthedc2c642011-07-02 00:01:44 +00001355static void handleIBOutletCollection(Sema &S, Decl *D,
1356 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001357
1358 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001359 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001360 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1361 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001362 return;
1363 }
1364
Ted Kremenek7fd17232011-09-29 07:02:25 +00001365 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001366 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001367
Richard Smithb1f9a282013-10-31 01:56:18 +00001368 ParsedType PT;
1369
1370 if (Attr.hasParsedType())
1371 PT = Attr.getTypeArg();
1372 else {
1373 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1374 S.getScopeForContext(D->getDeclContext()->getParent()));
1375 if (!PT) {
1376 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1377 return;
1378 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001379 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001380
Richard Smithb87c4652013-10-31 21:23:20 +00001381 TypeSourceInfo *QTLoc = 0;
1382 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1383 if (!QTLoc)
1384 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001385
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001386 // Diagnose use of non-object type in iboutletcollection attribute.
1387 // FIXME. Gnu attribute extension ignores use of builtin types in
1388 // attributes. So, __attribute__((iboutletcollection(char))) will be
1389 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001390 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001391 S.Diag(Attr.getLoc(),
1392 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1393 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001394 return;
1395 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001396
Michael Han99315932013-01-24 16:46:58 +00001397 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001398 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001399 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001400}
1401
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001402static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001403 if (const RecordType *UT = T->getAsUnionType())
1404 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1405 RecordDecl *UD = UT->getDecl();
1406 for (RecordDecl::field_iterator it = UD->field_begin(),
1407 itend = UD->field_end(); it != itend; ++it) {
1408 QualType QT = it->getType();
1409 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1410 T = QT;
1411 return;
1412 }
1413 }
1414 }
1415}
1416
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001417static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopese881ce22012-06-18 16:39:04 +00001418 if (!isFunctionOrMethod(D)) {
1419 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001420 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopese881ce22012-06-18 16:39:04 +00001421 return;
1422 }
1423
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001424 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1425 return;
1426
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001427 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001428 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001429 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001430 uint64_t Idx;
1431 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1432 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001433 return;
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001434
1435 // check if the function argument is of an integer type
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001436 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001437 if (!T->isIntegerType()) {
Aaron Ballman9d695092013-07-30 14:10:17 +00001438 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1439 << Attr.getName() << AANT_ArgumentIntegerConstant
1440 << Ex->getSourceRange();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001441 return;
1442 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001443 SizeArgs.push_back(Idx);
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001444 }
1445
1446 // check if the function returns a pointer
1447 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1448 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001449 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001450 }
1451
Michael Han99315932013-01-24 16:46:58 +00001452 D->addAttr(::new (S.Context)
1453 AllocSizeAttr(Attr.getRange(), S.Context,
1454 SizeArgs.data(), SizeArgs.size(),
1455 Attr.getAttributeSpellingListIndex()));
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001456}
1457
Chandler Carruthedc2c642011-07-02 00:01:44 +00001458static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001459 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1460 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001461 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001462 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001463 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001464 return;
1465 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001466
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001467 SmallVector<unsigned, 8> NonNullArgs;
1468 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001469 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001470 uint64_t Idx;
1471 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1472 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001473 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001474
1475 // Is the function argument a pointer type?
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001476 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001477 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001478
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001479 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001480 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +00001481 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +00001482 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001483 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001484 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001485
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001486 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001487 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001488
1489 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1490 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001491 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001492 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1493 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001494 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001495 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001496 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001497 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001498
Ted Kremenek22813f42010-10-21 18:49:36 +00001499 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001500 if (NonNullArgs.empty()) {
1501 // Warn the trivial case only if attribute is not coming from a
1502 // macro instantiation.
1503 if (Attr.getLoc().isFileID())
1504 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001505 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001506 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001507 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001508
Nick Lewyckye1121512013-01-24 01:12:16 +00001509 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001510 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001511 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001512 D->addAttr(::new (S.Context)
1513 NonNullAttr(Attr.getRange(), S.Context, start, size,
1514 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001515}
1516
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001517static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1518 switch (K) {
1519 case OwnershipAttr::Holds: return "'ownership_holds'";
1520 case OwnershipAttr::Takes: return "'ownership_takes'";
1521 case OwnershipAttr::Returns: return "'ownership_returns'";
1522 }
1523 llvm_unreachable("unknown ownership");
1524}
1525
Chandler Carruthedc2c642011-07-02 00:01:44 +00001526static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001527 // This attribute must be applied to a function declaration. The first
1528 // argument to the attribute must be an identifier, the name of the resource,
1529 // for example: malloc. The following arguments must be argument indexes, the
1530 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001531 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001532 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001533 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001534
Aaron Ballman00e99962013-08-31 01:11:41 +00001535 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001536 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001537 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001538 return;
1539 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001540
Ted Kremenekd21139a2010-07-31 01:52:11 +00001541 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001542 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001543 switch (AL.getKind()) {
1544 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001545 K = OwnershipAttr::Takes;
Aaron Ballman00e99962013-08-31 01:11:41 +00001546 if (AL.getNumArgs() < 2) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001547 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001548 return;
1549 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001550 break;
1551 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001552 K = OwnershipAttr::Holds;
Aaron Ballman00e99962013-08-31 01:11:41 +00001553 if (AL.getNumArgs() < 2) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001554 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001555 return;
1556 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001557 break;
1558 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001559 K = OwnershipAttr::Returns;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001560
Aaron Ballman00e99962013-08-31 01:11:41 +00001561 if (AL.getNumArgs() > 2) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001562 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001563 return;
1564 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001565 break;
1566 default:
1567 // This should never happen given how we are called.
1568 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +00001569 }
1570
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001571 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001572 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1573 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001574 return;
1575 }
1576
Aaron Ballman00e99962013-08-31 01:11:41 +00001577 StringRef Module = AL.getArgAsIdent(0)->Ident->getName();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001578
1579 // Normalize the argument, __foo__ becomes foo.
1580 if (Module.startswith("__") && Module.endswith("__"))
1581 Module = Module.substr(2, Module.size() - 4);
1582
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001583 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001584 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1585 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001586 uint64_t Idx;
1587 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
Aaron Ballman00e99962013-08-31 01:11:41 +00001588 AL.getLoc(), i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001589 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001590
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001591 // Is the function argument a pointer type?
1592 QualType T = getFunctionOrMethodArgType(D, Idx);
1593 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001594 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001595 case OwnershipAttr::Takes:
1596 case OwnershipAttr::Holds:
1597 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1598 Err = 0;
1599 break;
1600 case OwnershipAttr::Returns:
1601 if (!T->isIntegerType())
1602 Err = 1;
1603 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001604 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001605 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001606 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001607 << Ex->getSourceRange();
1608 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001609 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001610
1611 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001612 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001613 i = D->specific_attr_begin<OwnershipAttr>(),
1614 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
1615 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1616 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1617 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1618 << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind());
1619 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001620 }
1621 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001622 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001623 }
1624
1625 unsigned* start = OwnershipArgs.data();
1626 unsigned size = OwnershipArgs.size();
1627 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001628
Michael Han99315932013-01-24 16:46:58 +00001629 D->addAttr(::new (S.Context)
1630 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1631 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001632}
1633
Chandler Carruthedc2c642011-07-02 00:01:44 +00001634static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001635 // Check the attribute arguments.
1636 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001637 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1638 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001639 return;
1640 }
1641
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001642 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall7a198ce2011-02-08 22:35:49 +00001643 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001644 << Attr.getName() << ExpectedVariableOrFunction;
John McCall7a198ce2011-02-08 22:35:49 +00001645 return;
1646 }
1647
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001648 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001649
Rafael Espindolac18086a2010-02-23 22:00:30 +00001650 // gcc rejects
1651 // class c {
1652 // static int a __attribute__((weakref ("v2")));
1653 // static int b() __attribute__((weakref ("f3")));
1654 // };
1655 // and ignores the attributes of
1656 // void f(void) {
1657 // static int a __attribute__((weakref ("v2")));
1658 // }
1659 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001660 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001661 if (!Ctx->isFileContext()) {
1662 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +00001663 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +00001664 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001665 }
1666
1667 // The GCC manual says
1668 //
1669 // At present, a declaration to which `weakref' is attached can only
1670 // be `static'.
1671 //
1672 // It also says
1673 //
1674 // Without a TARGET,
1675 // given as an argument to `weakref' or to `alias', `weakref' is
1676 // equivalent to `weak'.
1677 //
1678 // gcc 4.4.1 will accept
1679 // int a7 __attribute__((weakref));
1680 // as
1681 // int a7 __attribute__((weak));
1682 // This looks like a bug in gcc. We reject that for now. We should revisit
1683 // it if this behaviour is actually used.
1684
Rafael Espindolac18086a2010-02-23 22:00:30 +00001685 // GCC rejects
1686 // static ((alias ("y"), weakref)).
1687 // Should we? How to check that weakref is before or after alias?
1688
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001689 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1690 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1691 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001692 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001693 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001694 // GCC will accept anything as the argument of weakref. Should we
1695 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001696 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1697 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001698
Michael Han99315932013-01-24 16:46:58 +00001699 D->addAttr(::new (S.Context)
1700 WeakRefAttr(Attr.getRange(), S.Context,
1701 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001702}
1703
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001704static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1705 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001706 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001707 return;
1708
Douglas Gregore8bbc122011-09-02 00:18:52 +00001709 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001710 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1711 return;
1712 }
1713
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001714 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001715
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001716 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001717 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001718}
1719
Quentin Colombet4e172062012-11-01 23:55:47 +00001720static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Quentin Colombet4e172062012-11-01 23:55:47 +00001721 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1722 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1723 << Attr.getName() << ExpectedFunctionOrMethod;
1724 return;
1725 }
1726
Michael Han99315932013-01-24 16:46:58 +00001727 D->addAttr(::new (S.Context)
1728 MinSizeAttr(Attr.getRange(), S.Context,
1729 Attr.getAttributeSpellingListIndex()));
Quentin Colombet4e172062012-11-01 23:55:47 +00001730}
1731
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001732static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001733 if (!isa<FunctionDecl>(D)) {
1734 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1735 << Attr.getName() << ExpectedFunction;
1736 return;
1737 }
1738
1739 if (D->hasAttr<HotAttr>()) {
1740 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1741 << Attr.getName() << "hot";
1742 return;
1743 }
1744
Michael Han99315932013-01-24 16:46:58 +00001745 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1746 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001747}
1748
1749static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001750 if (!isa<FunctionDecl>(D)) {
1751 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1752 << Attr.getName() << ExpectedFunction;
1753 return;
1754 }
1755
1756 if (D->hasAttr<ColdAttr>()) {
1757 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1758 << Attr.getName() << "cold";
1759 return;
1760 }
1761
Michael Han99315932013-01-24 16:46:58 +00001762 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1763 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001764}
1765
Chandler Carruthedc2c642011-07-02 00:01:44 +00001766static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001767 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001768 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001769 << Attr.getName() << ExpectedFunction;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001770 return;
1771 }
1772
Michael Han99315932013-01-24 16:46:58 +00001773 D->addAttr(::new (S.Context)
1774 NakedAttr(Attr.getRange(), S.Context,
1775 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001776}
1777
Chandler Carruthedc2c642011-07-02 00:01:44 +00001778static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1779 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001780 if (!isa<FunctionDecl>(D)) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001781 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001782 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00001783 return;
1784 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001785
Michael Han99315932013-01-24 16:46:58 +00001786 D->addAttr(::new (S.Context)
1787 AlwaysInlineAttr(Attr.getRange(), S.Context,
1788 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar03a38442008-10-28 00:17:57 +00001789}
1790
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001791static void handleTLSModelAttr(Sema &S, Decl *D,
1792 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001793 StringRef Model;
1794 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001795 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001796 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001797 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001798
Richard Smithfd3834f2013-04-13 02:43:54 +00001799 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001800 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1801 << Attr.getName() << ExpectedTLSVar;
1802 return;
1803 }
1804
1805 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001806 if (Model != "global-dynamic" && Model != "local-dynamic"
1807 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001808 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001809 return;
1810 }
1811
Michael Han99315932013-01-24 16:46:58 +00001812 D->addAttr(::new (S.Context)
1813 TLSModelAttr(Attr.getRange(), S.Context, Model,
1814 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001815}
1816
Chandler Carruthedc2c642011-07-02 00:01:44 +00001817static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001818 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001819 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001820 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001821 D->addAttr(::new (S.Context)
1822 MallocAttr(Attr.getRange(), S.Context,
1823 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001824 return;
1825 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001826 }
1827
Ted Kremenek08479ae2009-08-15 00:51:46 +00001828 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001829}
1830
Chandler Carruthedc2c642011-07-02 00:01:44 +00001831static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han99315932013-01-24 16:46:58 +00001832 D->addAttr(::new (S.Context)
1833 MayAliasAttr(Attr.getRange(), S.Context,
1834 Attr.getAttributeSpellingListIndex()));
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001835}
1836
Chandler Carruthedc2c642011-07-02 00:01:44 +00001837static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001838 if (isa<VarDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001839 D->addAttr(::new (S.Context)
1840 NoCommonAttr(Attr.getRange(), S.Context,
1841 Attr.getAttributeSpellingListIndex()));
Eric Christopher515d87f2010-12-03 06:58:14 +00001842 else
1843 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001844 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001845}
1846
Chandler Carruthedc2c642011-07-02 00:01:44 +00001847static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001848 if (S.LangOpts.CPlusPlus) {
1849 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1850 return;
1851 }
1852
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001853 if (isa<VarDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001854 D->addAttr(::new (S.Context)
1855 CommonAttr(Attr.getRange(), S.Context,
1856 Attr.getAttributeSpellingListIndex()));
Eric Christopher515d87f2010-12-03 06:58:14 +00001857 else
1858 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001859 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001860}
1861
Chandler Carruthedc2c642011-07-02 00:01:44 +00001862static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001863 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001864
1865 if (S.CheckNoReturnAttr(attr)) return;
1866
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001867 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001868 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001869 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001870 return;
1871 }
1872
Michael Han99315932013-01-24 16:46:58 +00001873 D->addAttr(::new (S.Context)
1874 NoReturnAttr(attr.getRange(), S.Context,
1875 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001876}
1877
1878bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001879 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001880 attr.setInvalid();
1881 return true;
1882 }
1883
1884 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001885}
1886
Chandler Carruthedc2c642011-07-02 00:01:44 +00001887static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1888 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001889
1890 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1891 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001892 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1893 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001894 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1895 && !VD->getType()->isFunctionPointerType())) {
1896 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001897 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001898 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001899 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001900 return;
1901 }
1902 }
1903
Michael Han99315932013-01-24 16:46:58 +00001904 D->addAttr(::new (S.Context)
1905 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1906 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001907}
1908
Richard Smith10876ef2013-01-17 01:30:42 +00001909static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1910 const AttributeList &Attr) {
1911 // C++11 [dcl.attr.noreturn]p1:
1912 // The attribute may be applied to the declarator-id in a function
1913 // declaration.
1914 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1915 if (!FD) {
1916 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1917 << Attr.getName() << ExpectedFunctionOrMethod;
1918 return;
1919 }
1920
Michael Han99315932013-01-24 16:46:58 +00001921 D->addAttr(::new (S.Context)
1922 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1923 Attr.getAttributeSpellingListIndex()));
Richard Smith10876ef2013-01-17 01:30:42 +00001924}
1925
John Thompsoncdb847ba2010-08-09 21:53:52 +00001926// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001927static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001928/*
1929 Returning a Vector Class in Registers
1930
Eric Christopherbc638a82010-12-01 22:13:54 +00001931 According to the PPU ABI specifications, a class with a single member of
1932 vector type is returned in memory when used as the return value of a function.
1933 This results in inefficient code when implementing vector classes. To return
1934 the value in a single vector register, add the vecreturn attribute to the
1935 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001936
1937 Example:
1938
1939 struct Vector
1940 {
1941 __vector float xyzw;
1942 } __attribute__((vecreturn));
1943
1944 Vector Add(Vector lhs, Vector rhs)
1945 {
1946 Vector result;
1947 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1948 return result; // This will be returned in a register
1949 }
1950*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001951 if (!isa<RecordDecl>(D)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001952 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001953 << Attr.getName() << ExpectedClass;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001954 return;
1955 }
1956
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001957 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001958 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1959 return;
1960 }
1961
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001962 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001963 int count = 0;
1964
1965 if (!isa<CXXRecordDecl>(record)) {
1966 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1967 return;
1968 }
1969
1970 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1971 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1972 return;
1973 }
1974
Eric Christopherbc638a82010-12-01 22:13:54 +00001975 for (RecordDecl::field_iterator iter = record->field_begin();
1976 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001977 if ((count == 1) || !iter->getType()->isVectorType()) {
1978 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1979 return;
1980 }
1981 count++;
1982 }
1983
Michael Han99315932013-01-24 16:46:58 +00001984 D->addAttr(::new (S.Context)
1985 VecReturnAttr(Attr.getRange(), S.Context,
1986 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001987}
1988
Richard Smithe233fbf2013-01-28 22:42:45 +00001989static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1990 const AttributeList &Attr) {
1991 if (isa<ParmVarDecl>(D)) {
1992 // [[carries_dependency]] can only be applied to a parameter if it is a
1993 // parameter of a function declaration or lambda.
1994 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1995 S.Diag(Attr.getLoc(),
1996 diag::err_carries_dependency_param_not_function_decl);
1997 return;
1998 }
1999 } else if (!isa<FunctionDecl>(D)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002000 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002001 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002002 return;
2003 }
Richard Smithe233fbf2013-01-28 22:42:45 +00002004
2005 D->addAttr(::new (S.Context) CarriesDependencyAttr(
2006 Attr.getRange(), S.Context,
2007 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002008}
2009
Chandler Carruthedc2c642011-07-02 00:01:44 +00002010static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002011 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00002012 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002013 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002014 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00002015 return;
2016 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002017
Michael Han99315932013-01-24 16:46:58 +00002018 D->addAttr(::new (S.Context)
2019 UnusedAttr(Attr.getRange(), S.Context,
2020 Attr.getAttributeSpellingListIndex()));
Ted Kremenek39c59a82008-07-25 04:39:19 +00002021}
2022
Rafael Espindola70107f92011-10-03 14:59:42 +00002023static void handleReturnsTwiceAttr(Sema &S, Decl *D,
2024 const AttributeList &Attr) {
Rafael Espindola70107f92011-10-03 14:59:42 +00002025 if (!isa<FunctionDecl>(D)) {
2026 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2027 << Attr.getName() << ExpectedFunction;
2028 return;
2029 }
2030
Michael Han99315932013-01-24 16:46:58 +00002031 D->addAttr(::new (S.Context)
2032 ReturnsTwiceAttr(Attr.getRange(), S.Context,
2033 Attr.getAttributeSpellingListIndex()));
Rafael Espindola70107f92011-10-03 14:59:42 +00002034}
2035
Chandler Carruthedc2c642011-07-02 00:01:44 +00002036static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002037 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00002038 if (VD->hasLocalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002039 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
2040 return;
2041 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002042 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002043 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002044 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002045 return;
2046 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002047
Michael Han99315932013-01-24 16:46:58 +00002048 D->addAttr(::new (S.Context)
2049 UsedAttr(Attr.getRange(), S.Context,
2050 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002051}
2052
Chandler Carruthedc2c642011-07-02 00:01:44 +00002053static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00002054 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00002055 if (Attr.getNumArgs() > 1) {
2056 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00002057 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002058 }
Daniel Dunbar032db472008-07-31 22:40:48 +00002059
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002060 uint32_t priority = ConstructorAttr::DefaultPriority;
2061 if (Attr.getNumArgs() > 0 &&
2062 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
2063 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002064
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002065 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002066 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002067 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00002068 return;
2069 }
2070
Michael Han99315932013-01-24 16:46:58 +00002071 D->addAttr(::new (S.Context)
2072 ConstructorAttr(Attr.getRange(), S.Context, priority,
2073 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002074}
2075
Chandler Carruthedc2c642011-07-02 00:01:44 +00002076static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00002077 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00002078 if (Attr.getNumArgs() > 1) {
2079 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00002080 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002081 }
Daniel Dunbar032db472008-07-31 22:40:48 +00002082
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002083 uint32_t priority = ConstructorAttr::DefaultPriority;
2084 if (Attr.getNumArgs() > 0 &&
2085 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
2086 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002087
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002088 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002089 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002090 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00002091 return;
2092 }
2093
Michael Han99315932013-01-24 16:46:58 +00002094 D->addAttr(::new (S.Context)
2095 DestructorAttr(Attr.getRange(), S.Context, priority,
2096 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002097}
2098
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002099template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00002100static void handleAttrWithMessage(Sema &S, Decl *D,
2101 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00002102 unsigned NumArgs = Attr.getNumArgs();
2103 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00002104 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002105 return;
2106 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002107
2108 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002109 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002110 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002111 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002112
Michael Han99315932013-01-24 16:46:58 +00002113 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2114 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00002115}
2116
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00002117static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2118 const AttributeList &Attr) {
Aaron Ballmanf3614532013-11-24 20:36:50 +00002119 if (!isa<ObjCInterfaceDecl>(D)) {
2120 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2121 << Attr.getName() << ExpectedObjectiveCInterface;
2122 return;
2123 }
Michael Han99315932013-01-24 16:46:58 +00002124 D->addAttr(::new (S.Context)
2125 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2126 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00002127}
2128
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002129static void handleObjCRootClassAttr(Sema &S, Decl *D,
2130 const AttributeList &Attr) {
2131 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballmanf90ccb02013-07-18 14:56:42 +00002132 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2133 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002134 return;
2135 }
2136
Michael Han99315932013-01-24 16:46:58 +00002137 D->addAttr(::new (S.Context)
2138 ObjCRootClassAttr(Attr.getRange(), S.Context,
2139 Attr.getAttributeSpellingListIndex()));
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002140}
2141
Ted Kremenek28eace62013-11-23 01:01:34 +00002142static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
2143 const AttributeList &Attr) {
2144 if (!isa<ObjCInterfaceDecl>(D)) {
2145 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2146 << Attr.getName() << ExpectedObjectiveCInterface;
2147 return;
2148 }
2149
Ted Kremenek7559b472013-11-23 22:29:11 +00002150 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek28eace62013-11-23 01:01:34 +00002151
2152 if (!Parm) {
2153 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 1;
2154 return;
2155 }
2156
2157 D->addAttr(::new (S.Context)
2158 ObjCSuppressProtocolAttr(Attr.getRange(), S.Context, Parm->Ident,
2159 Attr.getAttributeSpellingListIndex()));
2160}
2161
2162
Michael Han99315932013-01-24 16:46:58 +00002163static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2164 const AttributeList &Attr) {
Fariborz Jahanian7249e362012-01-03 22:52:32 +00002165 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballmanf3614532013-11-24 20:36:50 +00002166 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2167 << Attr.getName() << ExpectedObjectiveCInterface;
Fariborz Jahanian7249e362012-01-03 22:52:32 +00002168 return;
2169 }
2170
Michael Han99315932013-01-24 16:46:58 +00002171 D->addAttr(::new (S.Context)
2172 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2173 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00002174}
2175
Jordy Rose740b0c22012-05-08 03:27:22 +00002176static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2177 IdentifierInfo *Platform,
2178 VersionTuple Introduced,
2179 VersionTuple Deprecated,
2180 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002181 StringRef PlatformName
2182 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2183 if (PlatformName.empty())
2184 PlatformName = Platform->getName();
2185
2186 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2187 // of these steps are needed).
2188 if (!Introduced.empty() && !Deprecated.empty() &&
2189 !(Introduced <= Deprecated)) {
2190 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2191 << 1 << PlatformName << Deprecated.getAsString()
2192 << 0 << Introduced.getAsString();
2193 return true;
2194 }
2195
2196 if (!Introduced.empty() && !Obsoleted.empty() &&
2197 !(Introduced <= Obsoleted)) {
2198 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2199 << 2 << PlatformName << Obsoleted.getAsString()
2200 << 0 << Introduced.getAsString();
2201 return true;
2202 }
2203
2204 if (!Deprecated.empty() && !Obsoleted.empty() &&
2205 !(Deprecated <= Obsoleted)) {
2206 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2207 << 2 << PlatformName << Obsoleted.getAsString()
2208 << 1 << Deprecated.getAsString();
2209 return true;
2210 }
2211
2212 return false;
2213}
2214
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002215/// \brief Check whether the two versions match.
2216///
2217/// If either version tuple is empty, then they are assumed to match. If
2218/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2219static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2220 bool BeforeIsOkay) {
2221 if (X.empty() || Y.empty())
2222 return true;
2223
2224 if (X == Y)
2225 return true;
2226
2227 if (BeforeIsOkay && X < Y)
2228 return true;
2229
2230 return false;
2231}
2232
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002233AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002234 IdentifierInfo *Platform,
2235 VersionTuple Introduced,
2236 VersionTuple Deprecated,
2237 VersionTuple Obsoleted,
2238 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002239 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00002240 bool Override,
2241 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002242 VersionTuple MergedIntroduced = Introduced;
2243 VersionTuple MergedDeprecated = Deprecated;
2244 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002245 bool FoundAny = false;
2246
Rafael Espindolac67f2232012-05-10 02:50:16 +00002247 if (D->hasAttrs()) {
2248 AttrVec &Attrs = D->getAttrs();
2249 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2250 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2251 if (!OldAA) {
2252 ++i;
2253 continue;
2254 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002255
Rafael Espindolac67f2232012-05-10 02:50:16 +00002256 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2257 if (OldPlatform != Platform) {
2258 ++i;
2259 continue;
2260 }
2261
2262 FoundAny = true;
2263 VersionTuple OldIntroduced = OldAA->getIntroduced();
2264 VersionTuple OldDeprecated = OldAA->getDeprecated();
2265 VersionTuple OldObsoleted = OldAA->getObsoleted();
2266 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002267
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002268 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2269 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2270 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2271 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00002272 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002273 if (Override) {
2274 int Which = -1;
2275 VersionTuple FirstVersion;
2276 VersionTuple SecondVersion;
2277 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2278 Which = 0;
2279 FirstVersion = OldIntroduced;
2280 SecondVersion = Introduced;
2281 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2282 Which = 1;
2283 FirstVersion = Deprecated;
2284 SecondVersion = OldDeprecated;
2285 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2286 Which = 2;
2287 FirstVersion = Obsoleted;
2288 SecondVersion = OldObsoleted;
2289 }
2290
2291 if (Which == -1) {
2292 Diag(OldAA->getLocation(),
2293 diag::warn_mismatched_availability_override_unavail)
2294 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2295 } else {
2296 Diag(OldAA->getLocation(),
2297 diag::warn_mismatched_availability_override)
2298 << Which
2299 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2300 << FirstVersion.getAsString() << SecondVersion.getAsString();
2301 }
2302 Diag(Range.getBegin(), diag::note_overridden_method);
2303 } else {
2304 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2305 Diag(Range.getBegin(), diag::note_previous_attribute);
2306 }
2307
Rafael Espindolac67f2232012-05-10 02:50:16 +00002308 Attrs.erase(Attrs.begin() + i);
2309 --e;
2310 continue;
2311 }
2312
2313 VersionTuple MergedIntroduced2 = MergedIntroduced;
2314 VersionTuple MergedDeprecated2 = MergedDeprecated;
2315 VersionTuple MergedObsoleted2 = MergedObsoleted;
2316
2317 if (MergedIntroduced2.empty())
2318 MergedIntroduced2 = OldIntroduced;
2319 if (MergedDeprecated2.empty())
2320 MergedDeprecated2 = OldDeprecated;
2321 if (MergedObsoleted2.empty())
2322 MergedObsoleted2 = OldObsoleted;
2323
2324 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2325 MergedIntroduced2, MergedDeprecated2,
2326 MergedObsoleted2)) {
2327 Attrs.erase(Attrs.begin() + i);
2328 --e;
2329 continue;
2330 }
2331
2332 MergedIntroduced = MergedIntroduced2;
2333 MergedDeprecated = MergedDeprecated2;
2334 MergedObsoleted = MergedObsoleted2;
2335 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002336 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002337 }
2338
2339 if (FoundAny &&
2340 MergedIntroduced == Introduced &&
2341 MergedDeprecated == Deprecated &&
2342 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002343 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002344
Ted Kremenekb5445722013-04-06 00:34:27 +00002345 // Only create a new attribute if !Override, but we want to do
2346 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002347 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002348 MergedDeprecated, MergedObsoleted) &&
2349 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002350 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2351 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002352 Obsoleted, IsUnavailable, Message,
2353 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002354 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002355 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002356}
2357
Chandler Carruthedc2c642011-07-02 00:01:44 +00002358static void handleAvailabilityAttr(Sema &S, Decl *D,
2359 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002360 if (!checkAttributeNumArgs(S, Attr, 1))
2361 return;
2362 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00002363 unsigned Index = Attr.getAttributeSpellingListIndex();
2364
Aaron Ballman00e99962013-08-31 01:11:41 +00002365 IdentifierInfo *II = Platform->Ident;
2366 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2367 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2368 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002369
Rafael Espindolac231fab2013-01-08 21:30:32 +00002370 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2371 if (!ND) {
2372 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2373 return;
2374 }
2375
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002376 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2377 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2378 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002379 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002380 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00002381 if (const StringLiteral *SE =
2382 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002383 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002384
Aaron Ballman00e99962013-08-31 01:11:41 +00002385 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002386 Introduced.Version,
2387 Deprecated.Version,
2388 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002389 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00002390 /*Override=*/false,
2391 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002392 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002393 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002394}
2395
John McCalld041a9b2013-02-20 01:54:26 +00002396template <class T>
2397static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2398 typename T::VisibilityType value,
2399 unsigned attrSpellingListIndex) {
2400 T *existingAttr = D->getAttr<T>();
2401 if (existingAttr) {
2402 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2403 if (existingValue == value)
2404 return NULL;
2405 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2406 S.Diag(range.getBegin(), diag::note_previous_attribute);
2407 D->dropAttr<T>();
2408 }
2409 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2410}
2411
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002412VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002413 VisibilityAttr::VisibilityType Vis,
2414 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002415 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2416 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002417}
2418
John McCalld041a9b2013-02-20 01:54:26 +00002419TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2420 TypeVisibilityAttr::VisibilityType Vis,
2421 unsigned AttrSpellingListIndex) {
2422 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2423 AttrSpellingListIndex);
2424}
2425
2426static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2427 bool isTypeVisibility) {
2428 // Visibility attributes don't mean anything on a typedef.
2429 if (isa<TypedefNameDecl>(D)) {
2430 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2431 << Attr.getName();
2432 return;
2433 }
2434
2435 // 'type_visibility' can only go on a type or namespace.
2436 if (isTypeVisibility &&
2437 !(isa<TagDecl>(D) ||
2438 isa<ObjCInterfaceDecl>(D) ||
2439 isa<NamespaceDecl>(D))) {
2440 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2441 << Attr.getName() << ExpectedTypeOrNamespace;
2442 return;
2443 }
2444
Benjamin Kramer70370212013-09-09 15:08:57 +00002445 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002446 StringRef TypeStr;
2447 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002448 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002449 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002450
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002451 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002452 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002453 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002454 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002455 return;
2456 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002457
2458 // Complain about attempts to use protected visibility on targets
2459 // (like Darwin) that don't support it.
2460 if (type == VisibilityAttr::Protected &&
2461 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2462 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2463 type = VisibilityAttr::Default;
2464 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002465
Michael Han99315932013-01-24 16:46:58 +00002466 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002467 clang::Attr *newAttr;
2468 if (isTypeVisibility) {
2469 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2470 (TypeVisibilityAttr::VisibilityType) type,
2471 Index);
2472 } else {
2473 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2474 }
2475 if (newAttr)
2476 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002477}
2478
Chandler Carruthedc2c642011-07-02 00:01:44 +00002479static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2480 const AttributeList &Attr) {
John McCall86bc21f2011-03-02 11:33:24 +00002481 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2482 if (!method) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002483 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002484 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00002485 return;
2486 }
2487
Aaron Ballman00e99962013-08-31 01:11:41 +00002488 if (!Attr.isArgIdent(0)) {
2489 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2490 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002491 return;
2492 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002493
Aaron Ballman682ee422013-09-11 19:47:58 +00002494 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2495 ObjCMethodFamilyAttr::FamilyKind F;
2496 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2497 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2498 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002499 return;
2500 }
2501
Aaron Ballman682ee422013-09-11 19:47:58 +00002502 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCall31168b02011-06-15 23:02:42 +00002503 !method->getResultType()->isObjCObjectPointerType()) {
2504 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2505 << method->getResultType();
2506 // Ignore the attribute.
2507 return;
2508 }
2509
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002510 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman682ee422013-09-11 19:47:58 +00002511 S.Context, F));
John McCall86bc21f2011-03-02 11:33:24 +00002512}
2513
Chandler Carruthedc2c642011-07-02 00:01:44 +00002514static void handleObjCExceptionAttr(Sema &S, Decl *D,
2515 const AttributeList &Attr) {
Chris Lattner677a3582009-02-14 08:09:34 +00002516 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2517 if (OCI == 0) {
Aaron Ballmanf90ccb02013-07-18 14:56:42 +00002518 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2519 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner677a3582009-02-14 08:09:34 +00002520 return;
2521 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002522
Michael Han99315932013-01-24 16:46:58 +00002523 D->addAttr(::new (S.Context)
2524 ObjCExceptionAttr(Attr.getRange(), S.Context,
2525 Attr.getAttributeSpellingListIndex()));
Chris Lattner677a3582009-02-14 08:09:34 +00002526}
2527
Chandler Carruthedc2c642011-07-02 00:01:44 +00002528static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002529 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002530 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002531 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002532 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2533 return;
2534 }
2535 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002536 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2537 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002538 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002539 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2540 return;
2541 }
2542 }
2543 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002544 // It is okay to include this attribute on properties, e.g.:
2545 //
2546 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2547 //
2548 // In this case it follows tradition and suppresses an error in the above
2549 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002550 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002551 }
Michael Han99315932013-01-24 16:46:58 +00002552 D->addAttr(::new (S.Context)
2553 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2554 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002555}
2556
Mike Stumpd3bb5572009-07-24 19:02:52 +00002557static void
Chandler Carruthedc2c642011-07-02 00:01:44 +00002558handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002559 if (!isa<FunctionDecl>(D)) {
2560 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2561 return;
2562 }
2563
Michael Han99315932013-01-24 16:46:58 +00002564 D->addAttr(::new (S.Context)
2565 OverloadableAttr(Attr.getRange(), S.Context,
2566 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002567}
2568
Chandler Carruthedc2c642011-07-02 00:01:44 +00002569static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002570 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002571 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002572 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002573 return;
2574 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002575
Aaron Ballman00e99962013-08-31 01:11:41 +00002576 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002577 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002578 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2579 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2580 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002581 return;
2582 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002583
Michael Han99315932013-01-24 16:46:58 +00002584 D->addAttr(::new (S.Context)
2585 BlocksAttr(Attr.getRange(), S.Context, type,
2586 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002587}
2588
Chandler Carruthedc2c642011-07-02 00:01:44 +00002589static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002590 // check the attribute arguments.
2591 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002592 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002593 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002594 }
2595
Aaron Ballman18a78382013-11-21 00:28:23 +00002596 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002597 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002598 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002599 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002600 if (E->isTypeDependent() || E->isValueDependent() ||
2601 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002602 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002603 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002604 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002605 return;
2606 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002607
John McCallb46f2872011-09-09 07:56:05 +00002608 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002609 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2610 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002611 return;
2612 }
John McCallb46f2872011-09-09 07:56:05 +00002613
2614 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002615 }
2616
Aaron Ballman18a78382013-11-21 00:28:23 +00002617 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002618 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002619 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002620 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002621 if (E->isTypeDependent() || E->isValueDependent() ||
2622 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002623 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002624 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002625 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002626 return;
2627 }
2628 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002629
John McCallb46f2872011-09-09 07:56:05 +00002630 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002631 // FIXME: This error message could be improved, it would be nice
2632 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002633 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2634 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002635 return;
2636 }
2637 }
2638
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002639 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002640 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002641 if (isa<FunctionNoProtoType>(FT)) {
2642 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2643 return;
2644 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002645
Chris Lattner9363e312009-03-17 23:03:47 +00002646 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002647 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002648 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002649 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002650 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002651 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002652 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002653 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002654 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002655 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2656 if (!BD->isVariadic()) {
2657 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2658 return;
2659 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002660 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002661 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002662 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002663 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002664 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002665 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002666 int m = Ty->isFunctionPointerType() ? 0 : 1;
2667 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002668 return;
2669 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002670 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002671 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002672 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002673 return;
2674 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002675 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002676 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002677 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002678 return;
2679 }
Michael Han99315932013-01-24 16:46:58 +00002680 D->addAttr(::new (S.Context)
2681 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2682 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002683}
2684
Lubos Lunakedc13882013-07-20 15:05:36 +00002685static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Lubos Lunakedc13882013-07-20 15:05:36 +00002686 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2687 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2688 else
2689 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2690}
2691
Chandler Carruthedc2c642011-07-02 00:01:44 +00002692static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002693 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002694 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002695 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002696 return;
2697 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002698
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002699 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2700 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2701 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002702 return;
2703 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002704 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2705 if (MD->getResultType()->isVoidType()) {
2706 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2707 << Attr.getName() << 1;
2708 return;
2709 }
2710
Michael Han99315932013-01-24 16:46:58 +00002711 D->addAttr(::new (S.Context)
2712 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2713 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002714}
2715
Chandler Carruthedc2c642011-07-02 00:01:44 +00002716static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002717 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002718 if (isa<CXXRecordDecl>(D)) {
2719 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2720 return;
2721 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002722 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2723 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002724 return;
2725 }
2726
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002727 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002728
Michael Han99315932013-01-24 16:46:58 +00002729 nd->addAttr(::new (S.Context)
2730 WeakAttr(Attr.getRange(), S.Context,
2731 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002732}
2733
Chandler Carruthedc2c642011-07-02 00:01:44 +00002734static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002735 // weak_import only applies to variable & function declarations.
2736 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002737 if (!D->canBeWeakImported(isDef)) {
2738 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002739 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2740 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002741 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002742 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002743 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002744 // Nothing to warn about here.
2745 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002746 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002747 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002748
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002749 return;
2750 }
2751
Michael Han99315932013-01-24 16:46:58 +00002752 D->addAttr(::new (S.Context)
2753 WeakImportAttr(Attr.getRange(), S.Context,
2754 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002755}
2756
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002757// Handles reqd_work_group_size and work_group_size_hint.
2758static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002759 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002760 uint32_t WGSize[3];
2761 for (unsigned i = 0; i < 3; ++i)
2762 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002763 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002764
2765 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2766 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2767 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2768 if (!(A->getXDim() == WGSize[0] &&
2769 A->getYDim() == WGSize[1] &&
2770 A->getZDim() == WGSize[2])) {
2771 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2772 Attr.getName();
2773 }
2774 }
2775
2776 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2777 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2778 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2779 if (!(A->getXDim() == WGSize[0] &&
2780 A->getYDim() == WGSize[1] &&
2781 A->getZDim() == WGSize[2])) {
2782 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2783 Attr.getName();
2784 }
2785 }
2786
2787 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2788 D->addAttr(::new (S.Context)
2789 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002790 WGSize[0], WGSize[1], WGSize[2],
2791 Attr.getAttributeSpellingListIndex()));
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002792 else
2793 D->addAttr(::new (S.Context)
2794 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002795 WGSize[0], WGSize[1], WGSize[2],
2796 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002797}
2798
Joey Goulyaba589c2013-03-08 09:42:32 +00002799static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2800 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2801
Aaron Ballman00e99962013-08-31 01:11:41 +00002802 if (!Attr.hasParsedType()) {
2803 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2804 << Attr.getName() << 1;
2805 return;
2806 }
2807
Richard Smithb87c4652013-10-31 21:23:20 +00002808 TypeSourceInfo *ParmTSI = 0;
2809 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2810 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002811
2812 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2813 (ParmType->isBooleanType() ||
2814 !ParmType->isIntegralType(S.getASTContext()))) {
2815 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2816 << ParmType;
2817 return;
2818 }
2819
2820 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2821 D->hasAttr<VecTypeHintAttr>()) {
2822 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
Richard Smithb87c4652013-10-31 21:23:20 +00002823 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002824 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2825 return;
2826 }
2827 }
2828
2829 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Richard Smithb87c4652013-10-31 21:23:20 +00002830 ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002831}
2832
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002833SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002834 StringRef Name,
2835 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002836 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2837 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002838 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002839 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2840 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002841 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002842 }
Michael Han99315932013-01-24 16:46:58 +00002843 return ::new (Context) SectionAttr(Range, Context, Name,
2844 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002845}
2846
Chandler Carruthedc2c642011-07-02 00:01:44 +00002847static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002848 // Make sure that there is a string literal as the sections's single
2849 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002850 StringRef Str;
2851 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002852 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002853 return;
Mike Stump11289f42009-09-09 15:08:12 +00002854
Chris Lattner30ba6742009-08-10 19:03:04 +00002855 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002856 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002857 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002858 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002859 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002860 return;
2861 }
Mike Stump11289f42009-09-09 15:08:12 +00002862
Chris Lattner20aee9b2010-01-12 20:58:53 +00002863 // This attribute cannot be applied to local variables.
2864 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002865 S.Diag(LiteralLoc, diag::err_attribute_section_local_variable);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002866 return;
2867 }
Michael Han99315932013-01-24 16:46:58 +00002868
2869 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002870 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002871 if (NewAttr)
2872 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002873}
2874
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002875
Chandler Carruthedc2c642011-07-02 00:01:44 +00002876static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002877 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002878 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002879 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002880 } else {
Michael Han99315932013-01-24 16:46:58 +00002881 D->addAttr(::new (S.Context)
2882 NoThrowAttr(Attr.getRange(), S.Context,
2883 Attr.getAttributeSpellingListIndex()));
Douglas Gregor88336832011-06-15 05:45:11 +00002884 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002885}
2886
Chandler Carruthedc2c642011-07-02 00:01:44 +00002887static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002888 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002889 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002890 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002891 } else {
Michael Han99315932013-01-24 16:46:58 +00002892 D->addAttr(::new (S.Context)
2893 ConstAttr(Attr.getRange(), S.Context,
2894 Attr.getAttributeSpellingListIndex() ));
Douglas Gregor88336832011-06-15 05:45:11 +00002895 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002896}
2897
Chandler Carruthedc2c642011-07-02 00:01:44 +00002898static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han99315932013-01-24 16:46:58 +00002899 D->addAttr(::new (S.Context)
2900 PureAttr(Attr.getRange(), S.Context,
2901 Attr.getAttributeSpellingListIndex()));
Anders Carlssonb8316282008-10-05 23:32:53 +00002902}
2903
Chandler Carruthedc2c642011-07-02 00:01:44 +00002904static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002905 VarDecl *VD = dyn_cast<VarDecl>(D);
Anders Carlssond277d792009-01-31 01:16:18 +00002906 if (!VD || !VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002907 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002908 return;
2909 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002910
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002911 Expr *E = Attr.getArgAsExpr(0);
2912 SourceLocation Loc = E->getExprLoc();
2913 FunctionDecl *FD = 0;
2914 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002915
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002916 // gcc only allows for simple identifiers. Since we support more than gcc, we
2917 // will warn the user.
2918 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2919 if (DRE->hasQualifier())
2920 S.Diag(Loc, diag::warn_cleanup_ext);
2921 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2922 NI = DRE->getNameInfo();
2923 if (!FD) {
2924 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2925 << NI.getName();
2926 return;
2927 }
2928 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2929 if (ULE->hasExplicitTemplateArgs())
2930 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002931 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2932 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002933 if (!FD) {
2934 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2935 << NI.getName();
2936 if (ULE->getType() == S.Context.OverloadTy)
2937 S.NoteAllOverloadCandidates(ULE);
2938 return;
2939 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002940 } else {
2941 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002942 return;
2943 }
2944
Anders Carlssond277d792009-01-31 01:16:18 +00002945 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002946 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2947 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002948 return;
2949 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002950
Anders Carlsson723f55d2009-02-07 23:16:50 +00002951 // We're currently more strict than GCC about what function types we accept.
2952 // If this ever proves to be a problem it should be easy to fix.
2953 QualType Ty = S.Context.getPointerType(VD->getType());
2954 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002955 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2956 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002957 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2958 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002959 return;
2960 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002961
Michael Han99315932013-01-24 16:46:58 +00002962 D->addAttr(::new (S.Context)
2963 CleanupAttr(Attr.getRange(), S.Context, FD,
2964 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002965}
2966
Mike Stumpd3bb5572009-07-24 19:02:52 +00002967/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002968/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002969static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002970 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002971 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002972 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002973 return;
2974 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002975
Aaron Ballman00e99962013-08-31 01:11:41 +00002976 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002977 uint64_t ArgIdx;
2978 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2979 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002980 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002981
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002982 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002983 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002984
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002985 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2986 if (not_nsstring_type &&
2987 !isCFStringType(Ty, S.Context) &&
2988 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002989 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002990 // FIXME: Should highlight the actual expression that has the wrong type.
2991 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002992 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002993 << IdxExpr->getSourceRange();
2994 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002995 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002996 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002997 if (!isNSStringType(Ty, S.Context) &&
2998 !isCFStringType(Ty, S.Context) &&
2999 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003000 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003001 // FIXME: Should highlight the actual expression that has the wrong type.
3002 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00003003 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003004 << IdxExpr->getSourceRange();
3005 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003006 }
3007
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00003008 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
3009 // because that has corrected for the implicit this parameter, and is zero-
3010 // based. The attribute expects what the user wrote explicitly.
3011 llvm::APSInt Val;
3012 IdxExpr->EvaluateAsInt(Val, S.Context);
3013
Michael Han99315932013-01-24 16:46:58 +00003014 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00003015 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00003016 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003017}
3018
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003019enum FormatAttrKind {
3020 CFStringFormat,
3021 NSStringFormat,
3022 StrftimeFormat,
3023 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003024 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003025 InvalidFormat
3026};
3027
3028/// getFormatAttrKind - Map from format attribute names to supported format
3029/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003030static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003031 return llvm::StringSwitch<FormatAttrKind>(Format)
3032 // Check for formats that get handled specially.
3033 .Case("NSString", NSStringFormat)
3034 .Case("CFString", CFStringFormat)
3035 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003036
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003037 // Otherwise, check for supported formats.
3038 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3039 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3040 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003041
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003042 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3043 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003044}
3045
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003046/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003047/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003048static void handleInitPriorityAttr(Sema &S, Decl *D,
3049 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003050 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003051 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3052 return;
3053 }
3054
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003055 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003056 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3057 Attr.setInvalid();
3058 return;
3059 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003060 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003061 if (S.Context.getAsArrayType(T))
3062 T = S.Context.getBaseElementType(T);
3063 if (!T->getAs<RecordType>()) {
3064 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3065 Attr.setInvalid();
3066 return;
3067 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003068
3069 Expr *E = Attr.getArgAsExpr(0);
3070 uint32_t prioritynum;
3071 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003072 Attr.setInvalid();
3073 return;
3074 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003075
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003076 if (prioritynum < 101 || prioritynum > 65535) {
3077 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003078 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003079 Attr.setInvalid();
3080 return;
3081 }
Michael Han99315932013-01-24 16:46:58 +00003082 D->addAttr(::new (S.Context)
3083 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3084 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003085}
3086
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003087FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3088 IdentifierInfo *Format, int FormatIdx,
3089 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00003090 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003091 // Check whether we already have an equivalent format attribute.
3092 for (specific_attr_iterator<FormatAttr>
3093 i = D->specific_attr_begin<FormatAttr>(),
3094 e = D->specific_attr_end<FormatAttr>();
3095 i != e ; ++i) {
3096 FormatAttr *f = *i;
3097 if (f->getType() == Format &&
3098 f->getFormatIdx() == FormatIdx &&
3099 f->getFirstArg() == FirstArg) {
3100 // If we don't have a valid location for this attribute, adopt the
3101 // location.
3102 if (f->getLocation().isInvalid())
3103 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003104 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00003105 }
3106 }
3107
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003108 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3109 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003110}
3111
Mike Stumpd3bb5572009-07-24 19:02:52 +00003112/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003113/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003114static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003115 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003116 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00003117 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003118 return;
3119 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003120
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003121 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003122 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003123 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003124 return;
3125 }
3126
Chandler Carruth743682b2010-11-16 08:35:43 +00003127 // In C++ the implicit 'this' function parameter also counts, and they are
3128 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003129 bool HasImplicitThisParam = isInstanceMethod(D);
3130 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003131
Aaron Ballman00e99962013-08-31 01:11:41 +00003132 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
3133 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003134
3135 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003136 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003137 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003138 // If we've modified the string name, we need a new identifier for it.
3139 II = &S.Context.Idents.get(Format);
3140 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003141
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003142 // Check for supported formats.
3143 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00003144
3145 if (Kind == IgnoredFormat)
3146 return;
3147
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003148 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00003149 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman00e99962013-08-31 01:11:41 +00003150 << "format" << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003151 return;
3152 }
3153
3154 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00003155 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003156 uint32_t Idx;
3157 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003158 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003159
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003160 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003161 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003162 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003163 return;
3164 }
3165
3166 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003167 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003168
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003169 if (HasImplicitThisParam) {
3170 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00003171 S.Diag(Attr.getLoc(),
3172 diag::err_format_attribute_implicit_this_format_string)
3173 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003174 return;
3175 }
3176 ArgIdx--;
3177 }
Mike Stump11289f42009-09-09 15:08:12 +00003178
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003179 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003180 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003181
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003182 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003183 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003184 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3185 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00003186 return;
3187 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003188 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003189 // FIXME: do we need to check if the type is NSString*? What are the
3190 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003191 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003192 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003193 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3194 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003195 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003196 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003197 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003198 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003199 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003200 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3201 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003202 return;
3203 }
3204
3205 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00003206 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003207 uint32_t FirstArg;
3208 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003209 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003210
3211 // check if the function is variadic if the 3rd argument non-zero
3212 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003213 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003214 ++NumArgs; // +1 for ...
3215 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003216 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003217 return;
3218 }
3219 }
3220
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003221 // strftime requires FirstArg to be 0 because it doesn't read from any
3222 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003223 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003224 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00003225 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3226 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003227 return;
3228 }
3229 // if 0 it disables parameter checking (to use with e.g. va_list)
3230 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003231 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003232 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003233 return;
3234 }
3235
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003236 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003237 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00003238 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003239 if (NewAttr)
3240 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003241}
3242
Chandler Carruthedc2c642011-07-02 00:01:44 +00003243static void handleTransparentUnionAttr(Sema &S, Decl *D,
3244 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003245 // Try to find the underlying union declaration.
3246 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003247 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003248 if (TD && TD->getUnderlyingType()->isUnionType())
3249 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3250 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003251 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003252
3253 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00003254 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003255 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003256 return;
3257 }
3258
John McCallf937c022011-10-07 06:10:15 +00003259 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003260 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003261 diag::warn_transparent_union_attribute_not_definition);
3262 return;
3263 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003264
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003265 RecordDecl::field_iterator Field = RD->field_begin(),
3266 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003267 if (Field == FieldEnd) {
3268 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3269 return;
3270 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003271
David Blaikie40ed2972012-06-06 20:45:41 +00003272 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003273 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003274 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003275 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003276 diag::warn_transparent_union_attribute_floating)
3277 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003278 return;
3279 }
3280
3281 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3282 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3283 for (; Field != FieldEnd; ++Field) {
3284 QualType FieldType = Field->getType();
3285 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3286 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3287 // Warn if we drop the attribute.
3288 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003289 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003290 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003291 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003292 diag::warn_transparent_union_attribute_field_size_align)
3293 << isSize << Field->getDeclName() << FieldBits;
3294 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003295 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003296 diag::note_transparent_union_first_field_size_align)
3297 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003298 return;
3299 }
3300 }
3301
Michael Han99315932013-01-24 16:46:58 +00003302 RD->addAttr(::new (S.Context)
3303 TransparentUnionAttr(Attr.getRange(), S.Context,
3304 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003305}
3306
Chandler Carruthedc2c642011-07-02 00:01:44 +00003307static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003308 // Make sure that there is a string literal as the annotation's single
3309 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003310 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003311 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003312 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003313
3314 // Don't duplicate annotations that are already set.
3315 for (specific_attr_iterator<AnnotateAttr>
3316 i = D->specific_attr_begin<AnnotateAttr>(),
3317 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003318 if ((*i)->getAnnotation() == Str)
3319 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003320 }
Michael Han99315932013-01-24 16:46:58 +00003321
3322 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003323 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00003324 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003325}
3326
Chandler Carruthedc2c642011-07-02 00:01:44 +00003327static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003328 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003329 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003330 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3331 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003332 return;
3333 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003334
Richard Smith848e1f12013-02-01 08:12:08 +00003335 if (Attr.getNumArgs() == 0) {
3336 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3337 true, 0, Attr.getAttributeSpellingListIndex()));
3338 return;
3339 }
3340
Aaron Ballman00e99962013-08-31 01:11:41 +00003341 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00003342 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3343 S.Diag(Attr.getEllipsisLoc(),
3344 diag::err_pack_expansion_without_parameter_packs);
3345 return;
3346 }
3347
3348 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3349 return;
3350
3351 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3352 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003353}
3354
3355void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003356 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003357 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3358 SourceLocation AttrLoc = AttrRange.getBegin();
3359
Richard Smith1dba27c2013-01-29 09:02:09 +00003360 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003361 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003362 // C++11 [dcl.align]p1:
3363 // An alignment-specifier may be applied to a variable or to a class
3364 // data member, but it shall not be applied to a bit-field, a function
3365 // parameter, the formal parameter of a catch clause, or a variable
3366 // declared with the register storage class specifier. An
3367 // alignment-specifier may also be applied to the declaration of a class
3368 // or enumeration type.
3369 // C11 6.7.5/2:
3370 // An alignment attribute shall not be specified in a declaration of
3371 // a typedef, or a bit-field, or a function, or a parameter, or an
3372 // object declared with the register storage-class specifier.
3373 int DiagKind = -1;
3374 if (isa<ParmVarDecl>(D)) {
3375 DiagKind = 0;
3376 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3377 if (VD->getStorageClass() == SC_Register)
3378 DiagKind = 1;
3379 if (VD->isExceptionVariable())
3380 DiagKind = 2;
3381 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3382 if (FD->isBitField())
3383 DiagKind = 3;
3384 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00003385 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3386 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00003387 << (TmpAttr.isC11() ? ExpectedVariableOrField
3388 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003389 return;
3390 }
3391 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003392 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smithbc8caaf2013-02-22 04:55:39 +00003393 << TmpAttr.isC11() << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003394 return;
3395 }
3396 }
3397
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003398 if (E->isTypeDependent() || E->isValueDependent()) {
3399 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003400 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3401 AA->setPackExpansion(IsPackExpansion);
3402 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003403 return;
3404 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003405
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003406 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00003407 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00003408 ExprResult ICE
3409 = VerifyIntegerConstantExpression(E, &Alignment,
3410 diag::err_aligned_attribute_argument_not_int,
3411 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003412 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003413 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003414
3415 // C++11 [dcl.align]p2:
3416 // -- if the constant expression evaluates to zero, the alignment
3417 // specifier shall have no effect
3418 // C11 6.7.5p6:
3419 // An alignment specification of zero has no effect.
3420 if (!(TmpAttr.isAlignas() && !Alignment) &&
3421 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003422 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3423 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003424 return;
3425 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003426
Richard Smith848e1f12013-02-01 08:12:08 +00003427 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00003428 // We've already verified it's a power of 2, now let's make sure it's
3429 // 8192 or less.
3430 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00003431 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00003432 << E->getSourceRange();
3433 return;
3434 }
3435 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003436
Richard Smith44c247f2013-02-22 08:32:16 +00003437 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3438 ICE.take(), SpellingListIndex);
3439 AA->setPackExpansion(IsPackExpansion);
3440 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003441}
3442
Michael Hanaf02bbe2013-02-01 01:19:17 +00003443void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003444 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003445 // FIXME: Cache the number on the Attr object if non-dependent?
3446 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003447 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3448 SpellingListIndex);
3449 AA->setPackExpansion(IsPackExpansion);
3450 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003451}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003452
Richard Smith848e1f12013-02-01 08:12:08 +00003453void Sema::CheckAlignasUnderalignment(Decl *D) {
3454 assert(D->hasAttrs() && "no attributes on decl");
3455
3456 QualType Ty;
3457 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3458 Ty = VD->getType();
3459 else
3460 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00003461 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003462 return;
3463
3464 // C++11 [dcl.align]p5, C11 6.7.5/4:
3465 // The combined effect of all alignment attributes in a declaration shall
3466 // not specify an alignment that is less strict than the alignment that
3467 // would otherwise be required for the entity being declared.
3468 AlignedAttr *AlignasAttr = 0;
3469 unsigned Align = 0;
3470 for (specific_attr_iterator<AlignedAttr>
3471 I = D->specific_attr_begin<AlignedAttr>(),
3472 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3473 if (I->isAlignmentDependent())
3474 return;
3475 if (I->isAlignas())
3476 AlignasAttr = *I;
3477 Align = std::max(Align, I->getAlignment(Context));
3478 }
3479
3480 if (AlignasAttr && Align) {
3481 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3482 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3483 if (NaturalAlign > RequestedAlign)
3484 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3485 << Ty << (unsigned)NaturalAlign.getQuantity();
3486 }
3487}
3488
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003489/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003490/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003491///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003492/// Despite what would be logical, the mode attribute is a decl attribute, not a
3493/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3494/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003495static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003496 // This attribute isn't documented, but glibc uses it. It changes
3497 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00003498 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00003499 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3500 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003501 return;
3502 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003503
Aaron Ballman00e99962013-08-31 01:11:41 +00003504 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3505 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003506
3507 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003508 if (Str.startswith("__") && Str.endswith("__"))
3509 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003510
3511 unsigned DestWidth = 0;
3512 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003513 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003514 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003515 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003516 switch (Str[0]) {
3517 case 'Q': DestWidth = 8; break;
3518 case 'H': DestWidth = 16; break;
3519 case 'S': DestWidth = 32; break;
3520 case 'D': DestWidth = 64; break;
3521 case 'X': DestWidth = 96; break;
3522 case 'T': DestWidth = 128; break;
3523 }
3524 if (Str[1] == 'F') {
3525 IntegerMode = false;
3526 } else if (Str[1] == 'C') {
3527 IntegerMode = false;
3528 ComplexMode = true;
3529 } else if (Str[1] != 'I') {
3530 DestWidth = 0;
3531 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003532 break;
3533 case 4:
3534 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3535 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003536 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003537 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003538 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003539 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003540 break;
3541 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003542 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003543 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003544 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003545 case 11:
3546 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003547 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003548 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003549 }
3550
3551 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003552 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003553 OldTy = TD->getUnderlyingType();
3554 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3555 OldTy = VD->getType();
3556 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003557 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003558 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003559 return;
3560 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003561
John McCall9dd450b2009-09-21 23:43:11 +00003562 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003563 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3564 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003565 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003566 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3567 } else if (ComplexMode) {
3568 if (!OldTy->isComplexType())
3569 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3570 } else {
3571 if (!OldTy->isFloatingType())
3572 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3573 }
3574
Mike Stump87c57ac2009-05-16 07:39:55 +00003575 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3576 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003577 // FIXME: Make sure floating-point mappings are accurate
3578 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003579 if (!DestWidth) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003580 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003581 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003582 }
3583
3584 QualType NewTy;
3585
3586 if (IntegerMode)
3587 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3588 OldTy->isSignedIntegerType());
3589 else
3590 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3591
3592 if (NewTy.isNull()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003593 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003594 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003595 }
3596
Eli Friedman4735374e2009-03-03 06:41:03 +00003597 if (ComplexMode) {
3598 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003599 }
3600
3601 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003602 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3603 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3604 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003605 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003606
3607 D->addAttr(::new (S.Context)
3608 ModeAttr(Attr.getRange(), S.Context, Name,
3609 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003610}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003611
Chandler Carruthedc2c642011-07-02 00:01:44 +00003612static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003613 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3614 if (!VD->hasGlobalStorage())
3615 S.Diag(Attr.getLoc(),
3616 diag::warn_attribute_requires_functions_or_static_globals)
3617 << Attr.getName();
3618 } else if (!isFunctionOrMethod(D)) {
3619 S.Diag(Attr.getLoc(),
3620 diag::warn_attribute_requires_functions_or_static_globals)
3621 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003622 return;
3623 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003624
Michael Han99315932013-01-24 16:46:58 +00003625 D->addAttr(::new (S.Context)
3626 NoDebugAttr(Attr.getRange(), S.Context,
3627 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003628}
3629
Chandler Carruthedc2c642011-07-02 00:01:44 +00003630static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003631 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00003632 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003633 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00003634 return;
3635 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003636
Michael Han99315932013-01-24 16:46:58 +00003637 D->addAttr(::new (S.Context)
3638 NoInlineAttr(Attr.getRange(), S.Context,
3639 Attr.getAttributeSpellingListIndex()));
Anders Carlsson88097122009-02-19 19:16:48 +00003640}
3641
Chandler Carruthedc2c642011-07-02 00:01:44 +00003642static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3643 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003644 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003645 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003646 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00003647 return;
3648 }
3649
Michael Han99315932013-01-24 16:46:58 +00003650 D->addAttr(::new (S.Context)
3651 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3652 Attr.getAttributeSpellingListIndex()));
Chris Lattner3c77a352010-06-22 00:03:40 +00003653}
3654
Chandler Carruthedc2c642011-07-02 00:01:44 +00003655static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003656 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003657 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003658 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003659 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003660 return;
3661 }
3662
Michael Han99315932013-01-24 16:46:58 +00003663 D->addAttr(::new (S.Context)
3664 CUDAConstantAttr(Attr.getRange(), S.Context,
3665 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003666 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003667 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003668 }
3669}
3670
Chandler Carruthedc2c642011-07-02 00:01:44 +00003671static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003672 if (S.LangOpts.CUDA) {
3673 // check the attribute arguments.
3674 if (Attr.getNumArgs() != 0) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003675 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3676 << Attr.getName() << 0;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003677 return;
3678 }
3679
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003680 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003681 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003682 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003683 return;
3684 }
3685
Michael Han99315932013-01-24 16:46:58 +00003686 D->addAttr(::new (S.Context)
3687 CUDADeviceAttr(Attr.getRange(), S.Context,
3688 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003689 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003690 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003691 }
3692}
3693
Chandler Carruthedc2c642011-07-02 00:01:44 +00003694static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003695 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003696 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003697 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003698 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003699 return;
3700 }
3701
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003702 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003703 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00003704 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie6adc78e2013-02-18 22:06:02 +00003705 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003706 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3707 << FD->getType()
David Blaikie6adc78e2013-02-18 22:06:02 +00003708 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003709 "void");
3710 } else {
3711 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3712 << FD->getType();
3713 }
3714 return;
3715 }
3716
Michael Han99315932013-01-24 16:46:58 +00003717 D->addAttr(::new (S.Context)
3718 CUDAGlobalAttr(Attr.getRange(), S.Context,
3719 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003720 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003721 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003722 }
3723}
3724
Chandler Carruthedc2c642011-07-02 00:01:44 +00003725static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003726 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003727 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003728 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003729 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003730 return;
3731 }
3732
Michael Han99315932013-01-24 16:46:58 +00003733 D->addAttr(::new (S.Context)
3734 CUDAHostAttr(Attr.getRange(), S.Context,
3735 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003736 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003737 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003738 }
3739}
3740
Chandler Carruthedc2c642011-07-02 00:01:44 +00003741static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003742 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003743 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003744 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003745 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003746 return;
3747 }
3748
Michael Han99315932013-01-24 16:46:58 +00003749 D->addAttr(::new (S.Context)
3750 CUDASharedAttr(Attr.getRange(), S.Context,
3751 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003752 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003753 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003754 }
3755}
3756
Chandler Carruthedc2c642011-07-02 00:01:44 +00003757static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003758 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00003759 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003760 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003761 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00003762 return;
3763 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003764
Douglas Gregor35b57532009-10-27 21:01:01 +00003765 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003766 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003767 return;
3768 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003769
Michael Han99315932013-01-24 16:46:58 +00003770 D->addAttr(::new (S.Context)
3771 GNUInlineAttr(Attr.getRange(), S.Context,
3772 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003773}
3774
Chandler Carruthedc2c642011-07-02 00:01:44 +00003775static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003776 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003777
Aaron Ballman02df2e02012-12-09 17:45:41 +00003778 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003779 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003780 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3781 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003782 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003783 return;
3784
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003785 if (!isa<ObjCMethodDecl>(D)) {
3786 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3787 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003788 return;
3789 }
3790
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003791 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003792 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003793 D->addAttr(::new (S.Context)
3794 FastCallAttr(Attr.getRange(), S.Context,
3795 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003796 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003797 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003798 D->addAttr(::new (S.Context)
3799 StdCallAttr(Attr.getRange(), S.Context,
3800 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003801 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003802 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003803 D->addAttr(::new (S.Context)
3804 ThisCallAttr(Attr.getRange(), S.Context,
3805 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003806 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003807 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003808 D->addAttr(::new (S.Context)
3809 CDeclAttr(Attr.getRange(), S.Context,
3810 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003811 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003812 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003813 D->addAttr(::new (S.Context)
3814 PascalAttr(Attr.getRange(), S.Context,
3815 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003816 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003817 case AttributeList::AT_MSABI:
3818 D->addAttr(::new (S.Context)
3819 MSABIAttr(Attr.getRange(), S.Context,
3820 Attr.getAttributeSpellingListIndex()));
3821 return;
3822 case AttributeList::AT_SysVABI:
3823 D->addAttr(::new (S.Context)
3824 SysVABIAttr(Attr.getRange(), S.Context,
3825 Attr.getAttributeSpellingListIndex()));
3826 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003827 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003828 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003829 switch (CC) {
3830 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003831 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003832 break;
3833 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003834 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003835 break;
3836 default:
3837 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003838 }
3839
Michael Han99315932013-01-24 16:46:58 +00003840 D->addAttr(::new (S.Context)
3841 PcsAttr(Attr.getRange(), S.Context, PCS,
3842 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003843 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003844 }
Derek Schuffa2020962012-10-16 22:30:41 +00003845 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003846 D->addAttr(::new (S.Context)
3847 PnaclCallAttr(Attr.getRange(), S.Context,
3848 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003849 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003850 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003851 D->addAttr(::new (S.Context)
3852 IntelOclBiccAttr(Attr.getRange(), S.Context,
3853 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003854 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003855
Abramo Bagnara50099372010-04-30 13:10:51 +00003856 default:
3857 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003858 }
3859}
3860
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003861static void handleOpenCLKernelAttr(Sema &S, Decl *D,
3862 const AttributeList &Attr) {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003863 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003864}
3865
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003866static void handleOpenCLImageAccessAttr(Sema &S, Decl *D,
3867 const AttributeList &Attr) {
3868 uint32_t ArgNum;
3869 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), ArgNum))
Guy Benyeifb36ede2013-03-24 13:58:12 +00003870 return;
Guy Benyeifb36ede2013-03-24 13:58:12 +00003871
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003872 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(Attr.getRange(),
3873 S.Context, ArgNum));
Guy Benyeifb36ede2013-03-24 13:58:12 +00003874}
3875
Aaron Ballman02df2e02012-12-09 17:45:41 +00003876bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3877 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003878 if (attr.isInvalid())
3879 return true;
3880
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003881 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003882 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003883 attr.setInvalid();
3884 return true;
3885 }
3886
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003887 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3888 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003889 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003890 case AttributeList::AT_CDecl: CC = CC_C; break;
3891 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3892 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3893 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3894 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003895 case AttributeList::AT_MSABI:
3896 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3897 CC_X86_64Win64;
3898 break;
3899 case AttributeList::AT_SysVABI:
3900 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3901 CC_C;
3902 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003903 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003904 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003905 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003906 attr.setInvalid();
3907 return true;
3908 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003909 if (StrRef == "aapcs") {
3910 CC = CC_AAPCS;
3911 break;
3912 } else if (StrRef == "aapcs-vfp") {
3913 CC = CC_AAPCS_VFP;
3914 break;
3915 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003916
3917 attr.setInvalid();
3918 Diag(attr.getLoc(), diag::err_invalid_pcs);
3919 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003920 }
Derek Schuffa2020962012-10-16 22:30:41 +00003921 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003922 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003923 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003924 }
3925
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003926 const TargetInfo &TI = Context.getTargetInfo();
3927 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3928 if (A == TargetInfo::CCCR_Warning) {
3929 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003930
3931 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3932 if (FD)
3933 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3934 TargetInfo::CCMT_NonMember;
3935 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003936 }
3937
John McCall3882ace2011-01-05 12:14:39 +00003938 return false;
3939}
3940
Chandler Carruthedc2c642011-07-02 00:01:44 +00003941static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003942 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003943
3944 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003945 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003946 return;
3947
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003948 if (!isa<ObjCMethodDecl>(D)) {
3949 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3950 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003951 return;
3952 }
Eli Friedman7044b762009-03-27 21:06:47 +00003953
Michael Han99315932013-01-24 16:46:58 +00003954 D->addAttr(::new (S.Context)
3955 RegparmAttr(Attr.getRange(), S.Context, numParams,
3956 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00003957}
3958
3959/// Checks a regparm attribute, returning true if it is ill-formed and
3960/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003961bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3962 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003963 return true;
3964
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003965 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003966 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003967 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003968 }
Eli Friedman7044b762009-03-27 21:06:47 +00003969
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003970 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003971 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003972 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003973 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003974 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003975 }
3976
Douglas Gregore8bbc122011-09-02 00:18:52 +00003977 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003978 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003979 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003980 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003981 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003982 }
3983
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003984 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003985 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003986 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003987 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003988 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003989 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003990 }
3991
John McCall3882ace2011-01-05 12:14:39 +00003992 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003993}
3994
Chandler Carruthedc2c642011-07-02 00:01:44 +00003995static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00003996 if (S.LangOpts.CUDA) {
3997 // check the attribute arguments.
3998 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00003999 // FIXME: 0 is not okay.
4000 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004001 return;
4002 }
4003
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004004 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00004005 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00004006 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004007 return;
4008 }
4009
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004010 uint32_t MaxThreads, MinBlocks;
4011 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1) ||
4012 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(1), MinBlocks, 2))
Peter Collingbourne827301e2010-12-12 23:03:07 +00004013 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004014
Michael Han99315932013-01-24 16:46:58 +00004015 D->addAttr(::new (S.Context)
4016 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004017 MaxThreads, MinBlocks,
Michael Han99315932013-01-24 16:46:58 +00004018 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00004019 } else {
4020 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4021 }
4022}
4023
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004024static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4025 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00004026 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00004027 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00004028 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004029 return;
4030 }
Aaron Ballman00e99962013-08-31 01:11:41 +00004031
4032 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004033 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004034
Aaron Ballman00e99962013-08-31 01:11:41 +00004035 StringRef AttrName = Attr.getName()->getName();
4036 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004037
4038 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4039 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4040 << Attr.getName() << ExpectedFunctionOrMethod;
4041 return;
4042 }
4043
4044 uint64_t ArgumentIdx;
4045 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4046 Attr.getLoc(), 2,
Aaron Ballman00e99962013-08-31 01:11:41 +00004047 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004048 return;
4049
4050 uint64_t TypeTagIdx;
4051 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4052 Attr.getLoc(), 3,
Aaron Ballman00e99962013-08-31 01:11:41 +00004053 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004054 return;
4055
4056 bool IsPointer = (AttrName == "pointer_with_type_tag");
4057 if (IsPointer) {
4058 // Ensure that buffer has a pointer type.
4059 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4060 if (!BufferTy->isPointerType()) {
4061 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00004062 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004063 }
4064 }
4065
Michael Han99315932013-01-24 16:46:58 +00004066 D->addAttr(::new (S.Context)
4067 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4068 ArgumentIdx, TypeTagIdx, IsPointer,
4069 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004070}
4071
4072static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4073 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00004074 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00004075 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00004076 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004077 return;
4078 }
Aaron Ballman00e99962013-08-31 01:11:41 +00004079
4080 if (!checkAttributeNumArgs(S, Attr, 1))
4081 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004082
Aaron Ballman00e99962013-08-31 01:11:41 +00004083 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00004084 TypeSourceInfo *MatchingCTypeLoc = 0;
4085 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
4086 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004087
Michael Han99315932013-01-24 16:46:58 +00004088 D->addAttr(::new (S.Context)
4089 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00004090 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00004091 Attr.getLayoutCompatible(),
4092 Attr.getMustBeNull(),
4093 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004094}
4095
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004096//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004097// Checker-specific attribute handlers.
4098//===----------------------------------------------------------------------===//
4099
John McCalled433932011-01-25 03:31:58 +00004100static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004101 return type->isDependentType() ||
4102 type->isObjCObjectPointerType() ||
4103 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00004104}
4105static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004106 return type->isDependentType() ||
4107 type->isPointerType() ||
4108 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00004109}
4110
Chandler Carruthedc2c642011-07-02 00:01:44 +00004111static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004112 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00004113 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004114 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004115 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00004116 return;
4117 }
4118
4119 bool typeOK, cf;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004120 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00004121 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4122 cf = false;
4123 } else {
4124 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4125 cf = true;
4126 }
4127
4128 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004129 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004130 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00004131 return;
4132 }
4133
4134 if (cf)
Michael Han99315932013-01-24 16:46:58 +00004135 param->addAttr(::new (S.Context)
4136 CFConsumedAttr(Attr.getRange(), S.Context,
4137 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004138 else
Michael Han99315932013-01-24 16:46:58 +00004139 param->addAttr(::new (S.Context)
4140 NSConsumedAttr(Attr.getRange(), S.Context,
4141 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004142}
4143
Chandler Carruthedc2c642011-07-02 00:01:44 +00004144static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4145 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004146 if (!isa<ObjCMethodDecl>(D)) {
4147 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004148 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00004149 return;
4150 }
4151
Michael Han99315932013-01-24 16:46:58 +00004152 D->addAttr(::new (S.Context)
4153 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4154 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004155}
4156
Chandler Carruthedc2c642011-07-02 00:01:44 +00004157static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4158 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004159
John McCalled433932011-01-25 03:31:58 +00004160 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004161
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004162 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004163 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004164 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004165 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004166 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00004167 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4168 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004169 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004170 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00004171 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004172 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004173 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00004174 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004175 return;
4176 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004177
John McCalled433932011-01-25 03:31:58 +00004178 bool typeOK;
4179 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004180 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004181 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004182 case AttributeList::AT_NSReturnsAutoreleased:
4183 case AttributeList::AT_NSReturnsRetained:
4184 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004185 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4186 cf = false;
4187 break;
4188
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004189 case AttributeList::AT_CFReturnsRetained:
4190 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004191 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4192 cf = true;
4193 break;
4194 }
4195
4196 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004197 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004198 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004199 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004200 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004201
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004202 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004203 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004204 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004205 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00004206 D->addAttr(::new (S.Context)
4207 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4208 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004209 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004210 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004211 D->addAttr(::new (S.Context)
4212 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4213 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004214 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004215 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004216 D->addAttr(::new (S.Context)
4217 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4218 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004219 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004220 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004221 D->addAttr(::new (S.Context)
4222 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4223 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004224 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004225 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004226 D->addAttr(::new (S.Context)
4227 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4228 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004229 return;
4230 };
4231}
4232
John McCallcf166702011-07-22 08:53:00 +00004233static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4234 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00004235 const int EP_ObjCMethod = 1;
4236 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004237
John McCallcf166702011-07-22 08:53:00 +00004238 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004239 QualType resultType;
4240
John McCallcf166702011-07-22 08:53:00 +00004241 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4242
Fariborz Jahaniana53e5d72012-04-21 17:51:44 +00004243 if (!method) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004244 ObjCPropertyDecl *property = dyn_cast<ObjCPropertyDecl>(D);
4245 if (!property) {
4246 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4247 << SourceRange(loc, loc) << attr.getName() << ExpectedMethodOrProperty;
4248 return;
4249 }
4250 resultType = property->getType();
John McCallcf166702011-07-22 08:53:00 +00004251 }
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004252 else
4253 // Check that the method returns a normal pointer.
4254 resultType = method->getResultType();
John McCallcf166702011-07-22 08:53:00 +00004255
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004256 if (!resultType->isReferenceType() &&
4257 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004258 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00004259 << SourceRange(loc)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004260 << attr.getName() << (method ? EP_ObjCMethod : EP_ObjCProperty)
4261 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00004262
4263 // Drop the attribute.
4264 return;
4265 }
4266
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004267 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004268 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4269 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004270}
4271
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004272static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4273 const AttributeList &attr) {
4274 SourceLocation loc = attr.getLoc();
4275 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4276
4277 if (!method) {
4278 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4279 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4280 return;
4281 }
4282 DeclContext *DC = method->getDeclContext();
4283 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4284 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4285 << attr.getName() << 0;
4286 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4287 return;
4288 }
4289 if (method->getMethodFamily() == OMF_dealloc) {
4290 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4291 << attr.getName() << 1;
4292 return;
4293 }
4294
Michael Han99315932013-01-24 16:46:58 +00004295 method->addAttr(::new (S.Context)
4296 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4297 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004298}
4299
John McCall32f5fe12011-09-30 05:12:12 +00004300/// Handle cf_audited_transfer and cf_unknown_transfer.
4301static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4302 if (!isa<FunctionDecl>(D)) {
4303 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004304 << A.getRange() << A.getName() << ExpectedFunction;
John McCall32f5fe12011-09-30 05:12:12 +00004305 return;
4306 }
4307
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004308 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall32f5fe12011-09-30 05:12:12 +00004309
4310 // Check whether there's a conflicting attribute already present.
4311 Attr *Existing;
4312 if (IsAudited) {
4313 Existing = D->getAttr<CFUnknownTransferAttr>();
4314 } else {
4315 Existing = D->getAttr<CFAuditedTransferAttr>();
4316 }
4317 if (Existing) {
4318 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4319 << A.getName()
4320 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4321 << A.getRange() << Existing->getRange();
4322 return;
4323 }
4324
4325 // All clear; add the attribute.
4326 if (IsAudited) {
Michael Han99315932013-01-24 16:46:58 +00004327 D->addAttr(::new (S.Context)
4328 CFAuditedTransferAttr(A.getRange(), S.Context,
4329 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004330 } else {
Michael Han99315932013-01-24 16:46:58 +00004331 D->addAttr(::new (S.Context)
4332 CFUnknownTransferAttr(A.getRange(), S.Context,
4333 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004334 }
4335}
4336
John McCallf1e8b342011-09-29 07:17:38 +00004337static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4338 const AttributeList &Attr) {
4339 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4340 if (!RD || RD->isUnion()) {
4341 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004342 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00004343 }
4344
Aaron Ballman00e99962013-08-31 01:11:41 +00004345 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallf1e8b342011-09-29 07:17:38 +00004346
4347 // In Objective-C, verify that the type names an Objective-C type.
4348 // We don't want to check this outside of ObjC because people sometimes
4349 // do crazy C declarations of Objective-C types.
Aaron Ballman00e99962013-08-31 01:11:41 +00004350 if (Parm && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00004351 // Check for an existing type with this name.
Aaron Ballman00e99962013-08-31 01:11:41 +00004352 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallf1e8b342011-09-29 07:17:38 +00004353 Sema::LookupOrdinaryName);
4354 if (S.LookupName(R, Sc)) {
4355 NamedDecl *Target = R.getFoundDecl();
4356 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4357 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4358 S.Diag(Target->getLocStart(), diag::note_declared_at);
4359 }
4360 }
4361 }
4362
Michael Han99315932013-01-24 16:46:58 +00004363 D->addAttr(::new (S.Context)
Aaron Ballman00e99962013-08-31 01:11:41 +00004364 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han99315932013-01-24 16:46:58 +00004365 Attr.getAttributeSpellingListIndex()));
John McCallf1e8b342011-09-29 07:17:38 +00004366}
4367
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004368static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
4369 const AttributeList &Attr) {
Fariborz Jahaniandb3d8552013-11-19 00:09:48 +00004370 if (!isa<RecordDecl>(D)) {
Fariborz Jahanianf720f862013-11-19 17:42:25 +00004371 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4372 << Attr.getName()
4373 << (S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass
4374 : ExpectedStructOrUnion);
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004375 return;
4376 }
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004377
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00004378 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004379
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004380 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004381 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004382 return;
4383 }
4384
4385 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004386 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004387 Attr.getAttributeSpellingListIndex()));
4388}
4389
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004390static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
4391 const AttributeList &Attr) {
4392 if (!isa<RecordDecl>(D)) {
4393 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4394 << Attr.getName()
4395 << (S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass
4396 : ExpectedStructOrUnion);
4397 return;
4398 }
4399
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00004400 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004401
4402 if (!Parm) {
4403 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
4404 return;
4405 }
4406
4407 D->addAttr(::new (S.Context)
4408 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
4409 Attr.getAttributeSpellingListIndex()));
4410}
4411
Chandler Carruthedc2c642011-07-02 00:01:44 +00004412static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4413 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004414 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004415
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004416 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004417 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004418}
4419
Chandler Carruthedc2c642011-07-02 00:01:44 +00004420static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4421 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004422 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004423 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004424 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004425 return;
4426 }
4427
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004428 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004429 QualType type = vd->getType();
4430
4431 if (!type->isDependentType() &&
4432 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004433 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004434 << type;
4435 return;
4436 }
4437
4438 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4439
4440 // If we have no lifetime yet, check the lifetime we're presumably
4441 // going to infer.
4442 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4443 lifetime = type->getObjCARCImplicitLifetime();
4444
4445 switch (lifetime) {
4446 case Qualifiers::OCL_None:
4447 assert(type->isDependentType() &&
4448 "didn't infer lifetime for non-dependent type?");
4449 break;
4450
4451 case Qualifiers::OCL_Weak: // meaningful
4452 case Qualifiers::OCL_Strong: // meaningful
4453 break;
4454
4455 case Qualifiers::OCL_ExplicitNone:
4456 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004457 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004458 << (lifetime == Qualifiers::OCL_Autoreleasing);
4459 break;
4460 }
4461
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004462 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004463 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4464 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004465}
4466
Francois Picheta83957a2010-12-19 06:50:37 +00004467//===----------------------------------------------------------------------===//
4468// Microsoft specific attribute handlers.
4469//===----------------------------------------------------------------------===//
4470
Reid Kleckner140c4a72013-05-17 14:04:52 +00004471// Check if MS extensions or some other language extensions are enabled. If
4472// not, issue a diagnostic that the given attribute is unused.
4473static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4474 bool OtherExtension = false) {
4475 if (S.LangOpts.MicrosoftExt || OtherExtension)
4476 return true;
4477 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4478 return false;
4479}
4480
Chandler Carruthedc2c642011-07-02 00:01:44 +00004481static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004482 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4483 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004484
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004485 StringRef StrRef;
4486 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00004487 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00004488 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004489
David Majnemer89085342013-08-09 08:56:20 +00004490 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4491 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00004492 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4493 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00004494
Reid Kleckner140c4a72013-05-17 14:04:52 +00004495 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00004496 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004497 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004498 return;
4499 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004500
David Majnemer89085342013-08-09 08:56:20 +00004501 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004502 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00004503 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004504 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00004505 return;
4506 }
David Majnemer89085342013-08-09 08:56:20 +00004507 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004508 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004509 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004510 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004511 }
Francois Picheta83957a2010-12-19 06:50:37 +00004512
David Majnemer89085342013-08-09 08:56:20 +00004513 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4514 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004515}
4516
John McCall8d32c052012-05-22 21:28:12 +00004517static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004518 if (!checkMicrosoftExt(S, Attr))
Nico Weberefa45b22012-11-07 21:31:36 +00004519 return;
Nico Weberefa45b22012-11-07 21:31:36 +00004520
4521 AttributeList::Kind Kind = Attr.getKind();
4522 if (Kind == AttributeList::AT_SingleInheritance)
4523 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004524 ::new (S.Context)
4525 SingleInheritanceAttr(Attr.getRange(), S.Context,
4526 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004527 else if (Kind == AttributeList::AT_MultipleInheritance)
4528 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004529 ::new (S.Context)
4530 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4531 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004532 else if (Kind == AttributeList::AT_VirtualInheritance)
4533 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004534 ::new (S.Context)
4535 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4536 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004537}
4538
4539static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004540 if (!checkMicrosoftExt(S, Attr))
4541 return;
4542
4543 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmancc14f3a2013-11-22 21:49:04 +00004544 if (Kind == AttributeList::AT_Win64)
4545 D->addAttr(
4546 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4547 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004548}
4549
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004550static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004551 if (!checkMicrosoftExt(S, Attr))
4552 return;
4553 D->addAttr(::new (S.Context)
4554 ForceInlineAttr(Attr.getRange(), S.Context,
4555 Attr.getAttributeSpellingListIndex()));
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004556}
4557
Reid Klecknerb144d362013-05-20 14:02:37 +00004558static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4559 if (!checkMicrosoftExt(S, Attr))
4560 return;
4561 // Check linkage after possibly merging declaratinos. See
4562 // checkAttributesAfterMerging().
4563 D->addAttr(::new (S.Context)
4564 SelectAnyAttr(Attr.getRange(), S.Context,
4565 Attr.getAttributeSpellingListIndex()));
4566}
4567
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004568/// Handles semantic checking for features that are common to all attributes,
4569/// such as checking whether a parameter was properly specified, or the correct
4570/// number of arguments were passed, etc.
4571static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4572 const AttributeList &Attr) {
4573 // Several attributes carry different semantics than the parsing requires, so
4574 // those are opted out of the common handling.
4575 //
4576 // We also bail on unknown and ignored attributes because those are handled
4577 // as part of the target-specific handling logic.
4578 if (Attr.hasCustomParsing() ||
4579 Attr.getKind() == AttributeList::UnknownAttribute ||
4580 Attr.getKind() == AttributeList::IgnoredAttribute)
4581 return false;
4582
4583 // If there are no optional arguments, then checking for the argument count
4584 // is trivial.
4585 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
4586 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4587 return true;
4588 return false;
4589}
4590
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004591//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004592// Top Level Sema Entry Points
4593//===----------------------------------------------------------------------===//
4594
Richard Smithf8a75c32013-08-29 00:47:48 +00004595/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4596/// the attribute applies to decls. If the attribute is a type attribute, just
4597/// silently ignore it if a GNU attribute.
4598static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4599 const AttributeList &Attr,
4600 bool IncludeCXX11Attributes) {
4601 if (Attr.isInvalid())
4602 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004603
Richard Smithf8a75c32013-08-29 00:47:48 +00004604 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4605 // instead.
4606 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4607 return;
4608
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004609 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4610 return;
4611
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004612 switch (Attr.getKind()) {
Richard Smith10876ef2013-01-17 01:30:42 +00004613 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4614 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4615 case AttributeList::AT_IBOutletCollection:
4616 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004617 case AttributeList::AT_AddressSpace:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004618 case AttributeList::AT_ObjCGC:
4619 case AttributeList::AT_VectorSize:
4620 case AttributeList::AT_NeonVectorType:
4621 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004622 case AttributeList::AT_Ptr32:
4623 case AttributeList::AT_Ptr64:
4624 case AttributeList::AT_SPtr:
4625 case AttributeList::AT_UPtr:
Mike Stumpd3bb5572009-07-24 19:02:52 +00004626 // Ignore these, these are type attributes, handled by
4627 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004628 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004629 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4630 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4631 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4632 case AttributeList::AT_AlwaysInline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004633 handleAlwaysInlineAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004634 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004635 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00004636 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004637 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4638 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4639 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004640 handleDependencyAttr(S, scope, D, Attr);
4641 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004642 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4643 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4644 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004645 case AttributeList::AT_CXX11NoReturn:
4646 handleCXX11NoReturnAttr(S, D, Attr);
4647 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004648 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004649 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004650 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004651 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4652 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004653 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004654 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004655 case AttributeList::AT_MinSize:
4656 handleMinSizeAttr(S, D, Attr);
4657 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004658 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4659 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4660 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman9a226212013-08-28 23:13:26 +00004661 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4662 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004663 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4664 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004665 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004666 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004667 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4668 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00004669 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004670 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4671 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00004672 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00004673 case AttributeList::AT_ownership_returns:
4674 case AttributeList::AT_ownership_takes:
4675 case AttributeList::AT_ownership_holds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004676 handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004677 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4678 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4679 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4680 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4681 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4682 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4683 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004684
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004685 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004686 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004687 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004688 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004689
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004690 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004691 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4692
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004693 case AttributeList::AT_ObjCRequiresSuper:
4694 handleObjCRequiresSuperAttr(S, D, Attr); break;
4695
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004696 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00004697 handleNSBridgedAttr(S, scope, D, Attr); break;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004698
4699 case AttributeList::AT_ObjCBridge:
4700 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004701
4702 case AttributeList::AT_ObjCBridgeMutable:
4703 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00004704
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004705 case AttributeList::AT_CFAuditedTransfer:
4706 case AttributeList::AT_CFUnknownTransfer:
John McCall32f5fe12011-09-30 05:12:12 +00004707 handleCFTransferAttr(S, D, Attr); break;
4708
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004709 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004710 case AttributeList::AT_CFConsumed:
4711 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4712 case AttributeList::AT_NSConsumesSelf:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004713 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004714
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004715 case AttributeList::AT_NSReturnsAutoreleased:
4716 case AttributeList::AT_NSReturnsNotRetained:
4717 case AttributeList::AT_CFReturnsNotRetained:
4718 case AttributeList::AT_NSReturnsRetained:
4719 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004720 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004721
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004722 case AttributeList::AT_WorkGroupSizeHint:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004723 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004724 handleWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00004725
Joey Goulyaba589c2013-03-08 09:42:32 +00004726 case AttributeList::AT_VecTypeHint:
4727 handleVecTypeHint(S, D, Attr); break;
4728
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004729 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004730 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004731
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004732 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4733 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4734 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004735 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004736 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004737 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00004738 handleArcWeakrefUnavailableAttr (S, D, Attr);
4739 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004740 case AttributeList::AT_ObjCRootClass:
Patrick Beardacfbe9e2012-04-06 18:12:22 +00004741 handleObjCRootClassAttr(S, D, Attr);
4742 break;
Ted Kremenek28eace62013-11-23 01:01:34 +00004743 case AttributeList::AT_ObjCSuppressProtocol:
4744 handleObjCSuppresProtocolAttr(S, D, Attr);
4745 break;
4746 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00004747 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00004748 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004749 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4750 case AttributeList::AT_ReturnsTwice:
Rafael Espindola70107f92011-10-03 14:59:42 +00004751 handleReturnsTwiceAttr(S, D, Attr);
4752 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004753 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004754 case AttributeList::AT_Visibility:
4755 handleVisibilityAttr(S, D, Attr, false);
4756 break;
4757 case AttributeList::AT_TypeVisibility:
4758 handleVisibilityAttr(S, D, Attr, true);
4759 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004760 case AttributeList::AT_WarnUnused:
4761 handleWarnUnusedAttr(S, D, Attr);
4762 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004763 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004764 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004765 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4766 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4767 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4768 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004769 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004770 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004771 case AttributeList::AT_ObjCException:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004772 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00004773 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004774 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004775 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004776 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004777 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4778 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4779 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4780 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4781 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4782 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4783 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4784 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4785 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004786 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004787 // Just ignore
4788 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004789 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00004790 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00004791 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004792 case AttributeList::AT_StdCall:
4793 case AttributeList::AT_CDecl:
4794 case AttributeList::AT_FastCall:
4795 case AttributeList::AT_ThisCall:
4796 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004797 case AttributeList::AT_MSABI:
4798 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004799 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004800 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004801 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004802 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004803 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004804 case AttributeList::AT_OpenCLKernel:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004805 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00004806 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004807 case AttributeList::AT_OpenCLImageAccess:
4808 handleOpenCLImageAccessAttr(S, D, Attr);
4809 break;
John McCall8d32c052012-05-22 21:28:12 +00004810
4811 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004812 case AttributeList::AT_MsStruct:
John McCall8d32c052012-05-22 21:28:12 +00004813 handleMsStructAttr(S, D, Attr);
4814 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004815 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004816 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004817 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004818 case AttributeList::AT_SingleInheritance:
4819 case AttributeList::AT_MultipleInheritance:
4820 case AttributeList::AT_VirtualInheritance:
John McCall8d32c052012-05-22 21:28:12 +00004821 handleInheritanceAttr(S, D, Attr);
4822 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004823 case AttributeList::AT_Win64:
John McCall8d32c052012-05-22 21:28:12 +00004824 handlePortabilityAttr(S, D, Attr);
4825 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004826 case AttributeList::AT_ForceInline:
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004827 handleForceInlineAttr(S, D, Attr);
4828 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004829 case AttributeList::AT_SelectAny:
4830 handleSelectAnyAttr(S, D, Attr);
4831 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004832
4833 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004834 case AttributeList::AT_AssertExclusiveLock:
4835 handleAssertExclusiveLockAttr(S, D, Attr);
4836 break;
4837 case AttributeList::AT_AssertSharedLock:
4838 handleAssertSharedLockAttr(S, D, Attr);
4839 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004840 case AttributeList::AT_GuardedVar:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004841 handleGuardedVarAttr(S, D, Attr);
4842 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004843 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004844 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004845 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004846 case AttributeList::AT_ScopedLockable:
Michael Han3be3b442012-07-23 18:48:41 +00004847 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004848 break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004849 case AttributeList::AT_NoSanitizeAddress:
4850 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004851 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004852 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004853 handleNoThreadSafetyAnalysis(S, D, Attr);
4854 break;
4855 case AttributeList::AT_NoSanitizeThread:
4856 handleNoSanitizeThread(S, D, Attr);
4857 break;
4858 case AttributeList::AT_NoSanitizeMemory:
4859 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004860 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004861 case AttributeList::AT_Lockable:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004862 handleLockableAttr(S, D, Attr);
4863 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004864 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004865 handleGuardedByAttr(S, D, Attr);
4866 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004867 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004868 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004869 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004870 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004871 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004872 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004873 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004874 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004875 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004876 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004877 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004878 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004879 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004880 handleLockReturnedAttr(S, D, Attr);
4881 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004882 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004883 handleLocksExcludedAttr(S, D, Attr);
4884 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004885 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004886 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004887 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004888 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004889 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004890 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004891 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004892 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004893 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004894 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004895 handleUnlockFunAttr(S, D, Attr);
4896 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004897 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004898 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004899 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004900 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004901 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004902 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004903
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004904 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004905 case AttributeList::AT_Consumable:
4906 handleConsumableAttr(S, D, Attr);
4907 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004908 case AttributeList::AT_CallableWhen:
4909 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004910 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004911 case AttributeList::AT_ParamTypestate:
4912 handleParamTypestateAttr(S, D, Attr);
4913 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004914 case AttributeList::AT_ReturnTypestate:
4915 handleReturnTypestateAttr(S, D, Attr);
4916 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004917 case AttributeList::AT_SetTypestate:
4918 handleSetTypestateAttr(S, D, Attr);
4919 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004920 case AttributeList::AT_TestTypestate:
4921 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004922 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004923
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004924 // Type safety attributes.
4925 case AttributeList::AT_ArgumentWithTypeTag:
4926 handleArgumentWithTypeTagAttr(S, D, Attr);
4927 break;
4928 case AttributeList::AT_TypeTagForDatatype:
4929 handleTypeTagForDatatypeAttr(S, D, Attr);
4930 break;
4931
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004932 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004933 // Ask target about the attribute.
4934 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4935 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004936 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4937 diag::warn_unhandled_ms_attribute_ignored :
4938 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004939 break;
4940 }
4941}
4942
4943/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4944/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004945void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004946 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004947 bool IncludeCXX11Attributes) {
4948 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004949 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004950
4951 // GCC accepts
4952 // static int a9 __attribute__((weakref));
4953 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004954 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004955 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00004956 cast<NamedDecl>(D)->getNameAsString();
4957 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004958 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004959 }
4960}
4961
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004962// Annotation attributes are the only attributes allowed after an access
4963// specifier.
4964bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4965 const AttributeList *AttrList) {
4966 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004967 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004968 handleAnnotateAttr(*this, ASDecl, *l);
4969 } else {
4970 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4971 return true;
4972 }
4973 }
4974
4975 return false;
4976}
4977
John McCall42856de2011-10-01 05:17:03 +00004978/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4979/// contains any decl attributes that we should warn about.
4980static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4981 for ( ; A; A = A->getNext()) {
4982 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004983 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004984 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4985
4986 if (A->getKind() == AttributeList::UnknownAttribute) {
4987 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4988 << A->getName() << A->getRange();
4989 } else {
4990 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4991 << A->getName() << A->getRange();
4992 }
4993 }
4994}
4995
4996/// checkUnusedDeclAttributes - Given a declarator which is not being
4997/// used to build a declaration, complain about any decl attributes
4998/// which might be lying around on it.
4999void Sema::checkUnusedDeclAttributes(Declarator &D) {
5000 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5001 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5002 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5003 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5004}
5005
Ryan Flynn7d470f32009-07-30 03:15:39 +00005006/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00005007/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00005008NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5009 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00005010 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005011 NamedDecl *NewD = 0;
5012 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00005013 FunctionDecl *NewFD;
5014 // FIXME: Missing call to CheckFunctionDeclaration().
5015 // FIXME: Mangling?
5016 // FIXME: Is the qualifier info correct?
5017 // FIXME: Is the DeclContext correct?
5018 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5019 Loc, Loc, DeclarationName(II),
5020 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005021 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00005022 FD->hasPrototype(),
5023 false/*isConstexprSpecified*/);
5024 NewD = NewFD;
5025
5026 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00005027 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00005028
5029 // Fake up parameter variables; they are declared as if this were
5030 // a typedef.
5031 QualType FDTy = FD->getType();
5032 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5033 SmallVector<ParmVarDecl*, 16> Params;
5034 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5035 AE = FT->arg_type_end(); AI != AE; ++AI) {
5036 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5037 Param->setScopeInfo(0, Params.size());
5038 Params.push_back(Param);
5039 }
David Blaikie9c70e042011-09-21 18:16:56 +00005040 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00005041 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005042 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5043 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00005044 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00005045 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005046 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00005047 if (VD->getQualifier()) {
5048 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00005049 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00005050 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005051 }
5052 return NewD;
5053}
5054
James Dennett634962f2012-06-14 21:40:34 +00005055/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00005056/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00005057void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00005058 if (W.getUsed()) return; // only do this once
5059 W.setUsed(true);
5060 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5061 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00005062 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005063 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5064 NDId->getName()));
5065 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00005066 WeakTopLevelDecl.push_back(NewD);
5067 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5068 // to insert Decl at TU scope, sorry.
5069 DeclContext *SavedContext = CurContext;
5070 CurContext = Context.getTranslationUnitDecl();
5071 PushOnScopeChains(NewD, S);
5072 CurContext = SavedContext;
5073 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005074 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005075 }
5076}
5077
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005078void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5079 // It's valid to "forward-declare" #pragma weak, in which case we
5080 // have to do this.
5081 LoadExternalWeakUndeclaredIdentifiers();
5082 if (!WeakUndeclaredIdentifiers.empty()) {
5083 NamedDecl *ND = NULL;
5084 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5085 if (VD->isExternC())
5086 ND = VD;
5087 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5088 if (FD->isExternC())
5089 ND = FD;
5090 if (ND) {
5091 if (IdentifierInfo *Id = ND->getIdentifier()) {
5092 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5093 = WeakUndeclaredIdentifiers.find(Id);
5094 if (I != WeakUndeclaredIdentifiers.end()) {
5095 WeakInfo W = I->second;
5096 DeclApplyPragmaWeak(S, ND, W);
5097 WeakUndeclaredIdentifiers[Id] = W;
5098 }
5099 }
5100 }
5101 }
5102}
5103
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005104/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5105/// it, apply them to D. This is a bit tricky because PD can have attributes
5106/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00005107void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005108 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00005109 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00005110 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005111
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005112 // Walk the declarator structure, applying decl attributes that were in a type
5113 // position to the decl itself. This handles cases like:
5114 // int *__attr__(x)** D;
5115 // when X is a decl attribute.
5116 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5117 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00005118 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005119
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005120 // Finally, apply any attributes on the decl itself.
5121 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00005122 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005123}
John McCall28a6aea2009-11-04 02:18:39 +00005124
John McCall31168b02011-06-15 23:02:42 +00005125/// Is the given declaration allowed to use a forbidden type?
5126static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5127 // Private ivars are always okay. Unfortunately, people don't
5128 // always properly make their ivars private, even in system headers.
5129 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005130 // Function declarations in sys headers will be marked unavailable.
5131 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5132 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005133 return false;
5134
5135 // Require it to be declared in a system header.
5136 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5137}
5138
5139/// Handle a delayed forbidden-type diagnostic.
5140static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5141 Decl *decl) {
5142 if (decl && isForbiddenTypeAllowed(S, decl)) {
5143 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5144 "this system declaration uses an unsupported type"));
5145 return;
5146 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005147 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005148 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005149 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005150 // kind of forbidden type messages on unavailable functions.
5151 if (FD->hasAttr<UnavailableAttr>() &&
5152 diag.getForbiddenTypeDiagnostic() ==
5153 diag::err_arc_array_param_no_ownership) {
5154 diag.Triggered = true;
5155 return;
5156 }
5157 }
John McCall31168b02011-06-15 23:02:42 +00005158
5159 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5160 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5161 diag.Triggered = true;
5162}
5163
John McCall2ec85372012-05-07 06:16:41 +00005164void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5165 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005166 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005167 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005168
John McCall2ec85372012-05-07 06:16:41 +00005169 // When delaying diagnostics to run in the context of a parsed
5170 // declaration, we only want to actually emit anything if parsing
5171 // succeeds.
5172 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005173
John McCall2ec85372012-05-07 06:16:41 +00005174 // We emit all the active diagnostics in this pool or any of its
5175 // parents. In general, we'll get one pool for the decl spec
5176 // and a child pool for each declarator; in a decl group like:
5177 // deprecated_typedef foo, *bar, baz();
5178 // only the declarator pops will be passed decls. This is correct;
5179 // we really do need to consider delayed diagnostics from the decl spec
5180 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00005181 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00005182 do {
John McCall6347b682012-05-07 06:16:58 +00005183 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00005184 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5185 // This const_cast is a bit lame. Really, Triggered should be mutable.
5186 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00005187 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00005188 continue;
5189
John McCallc1465822011-02-14 07:13:47 +00005190 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00005191 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00005192 // Don't bother giving deprecation diagnostics if the decl is invalid.
5193 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00005194 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005195 break;
5196
5197 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00005198 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005199 break;
John McCall31168b02011-06-15 23:02:42 +00005200
5201 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00005202 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00005203 break;
John McCall86121512010-01-27 03:50:35 +00005204 }
5205 }
John McCall2ec85372012-05-07 06:16:41 +00005206 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00005207}
5208
John McCall6347b682012-05-07 06:16:58 +00005209/// Given a set of delayed diagnostics, re-emit them as if they had
5210/// been delayed in the current context instead of in the given pool.
5211/// Essentially, this just moves them to the current pool.
5212void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5213 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5214 assert(curPool && "re-emitting in undelayed context not supported");
5215 curPool->steal(pool);
5216}
5217
John McCall28a6aea2009-11-04 02:18:39 +00005218static bool isDeclDeprecated(Decl *D) {
5219 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005220 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00005221 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00005222 // A category implicitly has the availability of the interface.
5223 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5224 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00005225 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5226 return false;
5227}
5228
Eli Friedman971bfa12012-08-08 21:52:41 +00005229static void
5230DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5231 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005232 const ObjCInterfaceDecl *UnknownObjCClass,
5233 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedman971bfa12012-08-08 21:52:41 +00005234 DeclarationName Name = D->getDeclName();
5235 if (!Message.empty()) {
5236 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5237 S.Diag(D->getLocation(),
5238 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5239 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005240 if (ObjCPropery)
5241 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5242 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005243 } else if (!UnknownObjCClass) {
5244 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5245 S.Diag(D->getLocation(),
5246 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5247 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005248 if (ObjCPropery)
5249 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5250 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005251 } else {
5252 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5253 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5254 }
5255}
5256
John McCallb45a1e72010-08-26 02:13:20 +00005257void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00005258 Decl *Ctx) {
5259 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00005260 return;
5261
John McCall86121512010-01-27 03:50:35 +00005262 DD.Triggered = true;
Eli Friedman971bfa12012-08-08 21:52:41 +00005263 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5264 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005265 DD.getUnknownObjCClass(),
5266 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00005267}
5268
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005269void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00005270 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005271 const ObjCInterfaceDecl *UnknownObjCClass,
5272 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00005273 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00005274 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005275 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5276 UnknownObjCClass,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005277 ObjCProperty,
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005278 Message));
John McCall28a6aea2009-11-04 02:18:39 +00005279 return;
5280 }
5281
5282 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00005283 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00005284 return;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005285 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00005286}