blob: 5a0b72fcb50b686e0ac8332340027ff05b5ab5dd [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) {
Michael Han99315932013-01-24 16:46:58 +00002119 D->addAttr(::new (S.Context)
2120 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2121 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00002122}
2123
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002124static void handleObjCRootClassAttr(Sema &S, Decl *D,
2125 const AttributeList &Attr) {
2126 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballmanf90ccb02013-07-18 14:56:42 +00002127 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2128 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002129 return;
2130 }
2131
Michael Han99315932013-01-24 16:46:58 +00002132 D->addAttr(::new (S.Context)
2133 ObjCRootClassAttr(Attr.getRange(), S.Context,
2134 Attr.getAttributeSpellingListIndex()));
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002135}
2136
Ted Kremenek28eace62013-11-23 01:01:34 +00002137static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
2138 const AttributeList &Attr) {
2139 if (!isa<ObjCInterfaceDecl>(D)) {
2140 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2141 << Attr.getName() << ExpectedObjectiveCInterface;
2142 return;
2143 }
2144
Ted Kremenek7559b472013-11-23 22:29:11 +00002145 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek28eace62013-11-23 01:01:34 +00002146
2147 if (!Parm) {
2148 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 1;
2149 return;
2150 }
2151
2152 D->addAttr(::new (S.Context)
2153 ObjCSuppressProtocolAttr(Attr.getRange(), S.Context, Parm->Ident,
2154 Attr.getAttributeSpellingListIndex()));
2155}
2156
2157
Michael Han99315932013-01-24 16:46:58 +00002158static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2159 const AttributeList &Attr) {
Fariborz Jahanian7249e362012-01-03 22:52:32 +00002160 if (!isa<ObjCInterfaceDecl>(D)) {
2161 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2162 return;
2163 }
2164
Michael Han99315932013-01-24 16:46:58 +00002165 D->addAttr(::new (S.Context)
2166 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2167 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00002168}
2169
Jordy Rose740b0c22012-05-08 03:27:22 +00002170static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2171 IdentifierInfo *Platform,
2172 VersionTuple Introduced,
2173 VersionTuple Deprecated,
2174 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002175 StringRef PlatformName
2176 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2177 if (PlatformName.empty())
2178 PlatformName = Platform->getName();
2179
2180 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2181 // of these steps are needed).
2182 if (!Introduced.empty() && !Deprecated.empty() &&
2183 !(Introduced <= Deprecated)) {
2184 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2185 << 1 << PlatformName << Deprecated.getAsString()
2186 << 0 << Introduced.getAsString();
2187 return true;
2188 }
2189
2190 if (!Introduced.empty() && !Obsoleted.empty() &&
2191 !(Introduced <= Obsoleted)) {
2192 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2193 << 2 << PlatformName << Obsoleted.getAsString()
2194 << 0 << Introduced.getAsString();
2195 return true;
2196 }
2197
2198 if (!Deprecated.empty() && !Obsoleted.empty() &&
2199 !(Deprecated <= Obsoleted)) {
2200 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2201 << 2 << PlatformName << Obsoleted.getAsString()
2202 << 1 << Deprecated.getAsString();
2203 return true;
2204 }
2205
2206 return false;
2207}
2208
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002209/// \brief Check whether the two versions match.
2210///
2211/// If either version tuple is empty, then they are assumed to match. If
2212/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2213static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2214 bool BeforeIsOkay) {
2215 if (X.empty() || Y.empty())
2216 return true;
2217
2218 if (X == Y)
2219 return true;
2220
2221 if (BeforeIsOkay && X < Y)
2222 return true;
2223
2224 return false;
2225}
2226
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002227AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002228 IdentifierInfo *Platform,
2229 VersionTuple Introduced,
2230 VersionTuple Deprecated,
2231 VersionTuple Obsoleted,
2232 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002233 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00002234 bool Override,
2235 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002236 VersionTuple MergedIntroduced = Introduced;
2237 VersionTuple MergedDeprecated = Deprecated;
2238 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002239 bool FoundAny = false;
2240
Rafael Espindolac67f2232012-05-10 02:50:16 +00002241 if (D->hasAttrs()) {
2242 AttrVec &Attrs = D->getAttrs();
2243 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2244 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2245 if (!OldAA) {
2246 ++i;
2247 continue;
2248 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002249
Rafael Espindolac67f2232012-05-10 02:50:16 +00002250 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2251 if (OldPlatform != Platform) {
2252 ++i;
2253 continue;
2254 }
2255
2256 FoundAny = true;
2257 VersionTuple OldIntroduced = OldAA->getIntroduced();
2258 VersionTuple OldDeprecated = OldAA->getDeprecated();
2259 VersionTuple OldObsoleted = OldAA->getObsoleted();
2260 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002261
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002262 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2263 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2264 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2265 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00002266 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002267 if (Override) {
2268 int Which = -1;
2269 VersionTuple FirstVersion;
2270 VersionTuple SecondVersion;
2271 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2272 Which = 0;
2273 FirstVersion = OldIntroduced;
2274 SecondVersion = Introduced;
2275 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2276 Which = 1;
2277 FirstVersion = Deprecated;
2278 SecondVersion = OldDeprecated;
2279 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2280 Which = 2;
2281 FirstVersion = Obsoleted;
2282 SecondVersion = OldObsoleted;
2283 }
2284
2285 if (Which == -1) {
2286 Diag(OldAA->getLocation(),
2287 diag::warn_mismatched_availability_override_unavail)
2288 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2289 } else {
2290 Diag(OldAA->getLocation(),
2291 diag::warn_mismatched_availability_override)
2292 << Which
2293 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2294 << FirstVersion.getAsString() << SecondVersion.getAsString();
2295 }
2296 Diag(Range.getBegin(), diag::note_overridden_method);
2297 } else {
2298 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2299 Diag(Range.getBegin(), diag::note_previous_attribute);
2300 }
2301
Rafael Espindolac67f2232012-05-10 02:50:16 +00002302 Attrs.erase(Attrs.begin() + i);
2303 --e;
2304 continue;
2305 }
2306
2307 VersionTuple MergedIntroduced2 = MergedIntroduced;
2308 VersionTuple MergedDeprecated2 = MergedDeprecated;
2309 VersionTuple MergedObsoleted2 = MergedObsoleted;
2310
2311 if (MergedIntroduced2.empty())
2312 MergedIntroduced2 = OldIntroduced;
2313 if (MergedDeprecated2.empty())
2314 MergedDeprecated2 = OldDeprecated;
2315 if (MergedObsoleted2.empty())
2316 MergedObsoleted2 = OldObsoleted;
2317
2318 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2319 MergedIntroduced2, MergedDeprecated2,
2320 MergedObsoleted2)) {
2321 Attrs.erase(Attrs.begin() + i);
2322 --e;
2323 continue;
2324 }
2325
2326 MergedIntroduced = MergedIntroduced2;
2327 MergedDeprecated = MergedDeprecated2;
2328 MergedObsoleted = MergedObsoleted2;
2329 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002330 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002331 }
2332
2333 if (FoundAny &&
2334 MergedIntroduced == Introduced &&
2335 MergedDeprecated == Deprecated &&
2336 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002337 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002338
Ted Kremenekb5445722013-04-06 00:34:27 +00002339 // Only create a new attribute if !Override, but we want to do
2340 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002341 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002342 MergedDeprecated, MergedObsoleted) &&
2343 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002344 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2345 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002346 Obsoleted, IsUnavailable, Message,
2347 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002348 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002349 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002350}
2351
Chandler Carruthedc2c642011-07-02 00:01:44 +00002352static void handleAvailabilityAttr(Sema &S, Decl *D,
2353 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002354 if (!checkAttributeNumArgs(S, Attr, 1))
2355 return;
2356 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00002357 unsigned Index = Attr.getAttributeSpellingListIndex();
2358
Aaron Ballman00e99962013-08-31 01:11:41 +00002359 IdentifierInfo *II = Platform->Ident;
2360 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2361 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2362 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002363
Rafael Espindolac231fab2013-01-08 21:30:32 +00002364 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2365 if (!ND) {
2366 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2367 return;
2368 }
2369
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002370 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2371 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2372 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002373 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002374 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00002375 if (const StringLiteral *SE =
2376 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002377 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002378
Aaron Ballman00e99962013-08-31 01:11:41 +00002379 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002380 Introduced.Version,
2381 Deprecated.Version,
2382 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002383 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00002384 /*Override=*/false,
2385 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002386 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002387 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002388}
2389
John McCalld041a9b2013-02-20 01:54:26 +00002390template <class T>
2391static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2392 typename T::VisibilityType value,
2393 unsigned attrSpellingListIndex) {
2394 T *existingAttr = D->getAttr<T>();
2395 if (existingAttr) {
2396 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2397 if (existingValue == value)
2398 return NULL;
2399 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2400 S.Diag(range.getBegin(), diag::note_previous_attribute);
2401 D->dropAttr<T>();
2402 }
2403 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2404}
2405
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002406VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002407 VisibilityAttr::VisibilityType Vis,
2408 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002409 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2410 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002411}
2412
John McCalld041a9b2013-02-20 01:54:26 +00002413TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2414 TypeVisibilityAttr::VisibilityType Vis,
2415 unsigned AttrSpellingListIndex) {
2416 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2417 AttrSpellingListIndex);
2418}
2419
2420static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2421 bool isTypeVisibility) {
2422 // Visibility attributes don't mean anything on a typedef.
2423 if (isa<TypedefNameDecl>(D)) {
2424 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2425 << Attr.getName();
2426 return;
2427 }
2428
2429 // 'type_visibility' can only go on a type or namespace.
2430 if (isTypeVisibility &&
2431 !(isa<TagDecl>(D) ||
2432 isa<ObjCInterfaceDecl>(D) ||
2433 isa<NamespaceDecl>(D))) {
2434 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2435 << Attr.getName() << ExpectedTypeOrNamespace;
2436 return;
2437 }
2438
Benjamin Kramer70370212013-09-09 15:08:57 +00002439 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002440 StringRef TypeStr;
2441 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002442 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002443 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002444
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002445 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002446 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002447 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002448 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002449 return;
2450 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002451
2452 // Complain about attempts to use protected visibility on targets
2453 // (like Darwin) that don't support it.
2454 if (type == VisibilityAttr::Protected &&
2455 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2456 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2457 type = VisibilityAttr::Default;
2458 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002459
Michael Han99315932013-01-24 16:46:58 +00002460 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002461 clang::Attr *newAttr;
2462 if (isTypeVisibility) {
2463 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2464 (TypeVisibilityAttr::VisibilityType) type,
2465 Index);
2466 } else {
2467 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2468 }
2469 if (newAttr)
2470 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002471}
2472
Chandler Carruthedc2c642011-07-02 00:01:44 +00002473static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2474 const AttributeList &Attr) {
John McCall86bc21f2011-03-02 11:33:24 +00002475 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2476 if (!method) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002477 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002478 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00002479 return;
2480 }
2481
Aaron Ballman00e99962013-08-31 01:11:41 +00002482 if (!Attr.isArgIdent(0)) {
2483 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2484 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002485 return;
2486 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002487
Aaron Ballman682ee422013-09-11 19:47:58 +00002488 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2489 ObjCMethodFamilyAttr::FamilyKind F;
2490 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2491 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2492 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002493 return;
2494 }
2495
Aaron Ballman682ee422013-09-11 19:47:58 +00002496 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCall31168b02011-06-15 23:02:42 +00002497 !method->getResultType()->isObjCObjectPointerType()) {
2498 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2499 << method->getResultType();
2500 // Ignore the attribute.
2501 return;
2502 }
2503
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002504 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman682ee422013-09-11 19:47:58 +00002505 S.Context, F));
John McCall86bc21f2011-03-02 11:33:24 +00002506}
2507
Chandler Carruthedc2c642011-07-02 00:01:44 +00002508static void handleObjCExceptionAttr(Sema &S, Decl *D,
2509 const AttributeList &Attr) {
Chris Lattner677a3582009-02-14 08:09:34 +00002510 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2511 if (OCI == 0) {
Aaron Ballmanf90ccb02013-07-18 14:56:42 +00002512 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2513 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner677a3582009-02-14 08:09:34 +00002514 return;
2515 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002516
Michael Han99315932013-01-24 16:46:58 +00002517 D->addAttr(::new (S.Context)
2518 ObjCExceptionAttr(Attr.getRange(), S.Context,
2519 Attr.getAttributeSpellingListIndex()));
Chris Lattner677a3582009-02-14 08:09:34 +00002520}
2521
Chandler Carruthedc2c642011-07-02 00:01:44 +00002522static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002523 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002524 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002525 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002526 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2527 return;
2528 }
2529 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002530 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2531 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002532 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002533 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2534 return;
2535 }
2536 }
2537 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002538 // It is okay to include this attribute on properties, e.g.:
2539 //
2540 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2541 //
2542 // In this case it follows tradition and suppresses an error in the above
2543 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002544 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002545 }
Michael Han99315932013-01-24 16:46:58 +00002546 D->addAttr(::new (S.Context)
2547 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2548 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002549}
2550
Mike Stumpd3bb5572009-07-24 19:02:52 +00002551static void
Chandler Carruthedc2c642011-07-02 00:01:44 +00002552handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002553 if (!isa<FunctionDecl>(D)) {
2554 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2555 return;
2556 }
2557
Michael Han99315932013-01-24 16:46:58 +00002558 D->addAttr(::new (S.Context)
2559 OverloadableAttr(Attr.getRange(), S.Context,
2560 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002561}
2562
Chandler Carruthedc2c642011-07-02 00:01:44 +00002563static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002564 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002565 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002566 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002567 return;
2568 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002569
Aaron Ballman00e99962013-08-31 01:11:41 +00002570 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002571 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002572 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2573 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2574 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002575 return;
2576 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002577
Michael Han99315932013-01-24 16:46:58 +00002578 D->addAttr(::new (S.Context)
2579 BlocksAttr(Attr.getRange(), S.Context, type,
2580 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002581}
2582
Chandler Carruthedc2c642011-07-02 00:01:44 +00002583static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002584 // check the attribute arguments.
2585 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002586 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002587 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002588 }
2589
Aaron Ballman18a78382013-11-21 00:28:23 +00002590 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002591 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002592 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002593 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002594 if (E->isTypeDependent() || E->isValueDependent() ||
2595 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002596 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002597 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002598 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002599 return;
2600 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002601
John McCallb46f2872011-09-09 07:56:05 +00002602 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002603 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2604 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002605 return;
2606 }
John McCallb46f2872011-09-09 07:56:05 +00002607
2608 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002609 }
2610
Aaron Ballman18a78382013-11-21 00:28:23 +00002611 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002612 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002613 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002614 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002615 if (E->isTypeDependent() || E->isValueDependent() ||
2616 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002617 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002618 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002619 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002620 return;
2621 }
2622 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002623
John McCallb46f2872011-09-09 07:56:05 +00002624 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002625 // FIXME: This error message could be improved, it would be nice
2626 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002627 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2628 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002629 return;
2630 }
2631 }
2632
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002633 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002634 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002635 if (isa<FunctionNoProtoType>(FT)) {
2636 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2637 return;
2638 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002639
Chris Lattner9363e312009-03-17 23:03:47 +00002640 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002641 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002642 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002643 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002644 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002645 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002646 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002647 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002648 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002649 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2650 if (!BD->isVariadic()) {
2651 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2652 return;
2653 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002654 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002655 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002656 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002657 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002658 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002659 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002660 int m = Ty->isFunctionPointerType() ? 0 : 1;
2661 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002662 return;
2663 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002664 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002665 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002666 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002667 return;
2668 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002669 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002670 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002671 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002672 return;
2673 }
Michael Han99315932013-01-24 16:46:58 +00002674 D->addAttr(::new (S.Context)
2675 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2676 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002677}
2678
Lubos Lunakedc13882013-07-20 15:05:36 +00002679static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Lubos Lunakedc13882013-07-20 15:05:36 +00002680 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2681 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2682 else
2683 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2684}
2685
Chandler Carruthedc2c642011-07-02 00:01:44 +00002686static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002687 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002688 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002689 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002690 return;
2691 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002692
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002693 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2694 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2695 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002696 return;
2697 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002698 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2699 if (MD->getResultType()->isVoidType()) {
2700 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2701 << Attr.getName() << 1;
2702 return;
2703 }
2704
Michael Han99315932013-01-24 16:46:58 +00002705 D->addAttr(::new (S.Context)
2706 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2707 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002708}
2709
Chandler Carruthedc2c642011-07-02 00:01:44 +00002710static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002711 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002712 if (isa<CXXRecordDecl>(D)) {
2713 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2714 return;
2715 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002716 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2717 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002718 return;
2719 }
2720
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002721 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002722
Michael Han99315932013-01-24 16:46:58 +00002723 nd->addAttr(::new (S.Context)
2724 WeakAttr(Attr.getRange(), S.Context,
2725 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002726}
2727
Chandler Carruthedc2c642011-07-02 00:01:44 +00002728static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002729 // weak_import only applies to variable & function declarations.
2730 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002731 if (!D->canBeWeakImported(isDef)) {
2732 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002733 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2734 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002735 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002736 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002737 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002738 // Nothing to warn about here.
2739 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002740 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002741 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002742
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002743 return;
2744 }
2745
Michael Han99315932013-01-24 16:46:58 +00002746 D->addAttr(::new (S.Context)
2747 WeakImportAttr(Attr.getRange(), S.Context,
2748 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002749}
2750
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002751// Handles reqd_work_group_size and work_group_size_hint.
2752static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002753 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002754 uint32_t WGSize[3];
2755 for (unsigned i = 0; i < 3; ++i)
2756 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002757 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002758
2759 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2760 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2761 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2762 if (!(A->getXDim() == WGSize[0] &&
2763 A->getYDim() == WGSize[1] &&
2764 A->getZDim() == WGSize[2])) {
2765 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2766 Attr.getName();
2767 }
2768 }
2769
2770 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2771 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2772 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2773 if (!(A->getXDim() == WGSize[0] &&
2774 A->getYDim() == WGSize[1] &&
2775 A->getZDim() == WGSize[2])) {
2776 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2777 Attr.getName();
2778 }
2779 }
2780
2781 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2782 D->addAttr(::new (S.Context)
2783 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002784 WGSize[0], WGSize[1], WGSize[2],
2785 Attr.getAttributeSpellingListIndex()));
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002786 else
2787 D->addAttr(::new (S.Context)
2788 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002789 WGSize[0], WGSize[1], WGSize[2],
2790 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002791}
2792
Joey Goulyaba589c2013-03-08 09:42:32 +00002793static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2794 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2795
Aaron Ballman00e99962013-08-31 01:11:41 +00002796 if (!Attr.hasParsedType()) {
2797 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2798 << Attr.getName() << 1;
2799 return;
2800 }
2801
Richard Smithb87c4652013-10-31 21:23:20 +00002802 TypeSourceInfo *ParmTSI = 0;
2803 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2804 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002805
2806 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2807 (ParmType->isBooleanType() ||
2808 !ParmType->isIntegralType(S.getASTContext()))) {
2809 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2810 << ParmType;
2811 return;
2812 }
2813
2814 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2815 D->hasAttr<VecTypeHintAttr>()) {
2816 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
Richard Smithb87c4652013-10-31 21:23:20 +00002817 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002818 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2819 return;
2820 }
2821 }
2822
2823 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Richard Smithb87c4652013-10-31 21:23:20 +00002824 ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002825}
2826
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002827SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002828 StringRef Name,
2829 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002830 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2831 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002832 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002833 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2834 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002835 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002836 }
Michael Han99315932013-01-24 16:46:58 +00002837 return ::new (Context) SectionAttr(Range, Context, Name,
2838 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002839}
2840
Chandler Carruthedc2c642011-07-02 00:01:44 +00002841static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002842 // Make sure that there is a string literal as the sections's single
2843 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002844 StringRef Str;
2845 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002846 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002847 return;
Mike Stump11289f42009-09-09 15:08:12 +00002848
Chris Lattner30ba6742009-08-10 19:03:04 +00002849 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002850 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002851 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002852 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002853 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002854 return;
2855 }
Mike Stump11289f42009-09-09 15:08:12 +00002856
Chris Lattner20aee9b2010-01-12 20:58:53 +00002857 // This attribute cannot be applied to local variables.
2858 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002859 S.Diag(LiteralLoc, diag::err_attribute_section_local_variable);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002860 return;
2861 }
Michael Han99315932013-01-24 16:46:58 +00002862
2863 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002864 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002865 if (NewAttr)
2866 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002867}
2868
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002869
Chandler Carruthedc2c642011-07-02 00:01:44 +00002870static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002871 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002872 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002873 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002874 } else {
Michael Han99315932013-01-24 16:46:58 +00002875 D->addAttr(::new (S.Context)
2876 NoThrowAttr(Attr.getRange(), S.Context,
2877 Attr.getAttributeSpellingListIndex()));
Douglas Gregor88336832011-06-15 05:45:11 +00002878 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002879}
2880
Chandler Carruthedc2c642011-07-02 00:01:44 +00002881static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002882 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002883 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002884 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002885 } else {
Michael Han99315932013-01-24 16:46:58 +00002886 D->addAttr(::new (S.Context)
2887 ConstAttr(Attr.getRange(), S.Context,
2888 Attr.getAttributeSpellingListIndex() ));
Douglas Gregor88336832011-06-15 05:45:11 +00002889 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002890}
2891
Chandler Carruthedc2c642011-07-02 00:01:44 +00002892static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han99315932013-01-24 16:46:58 +00002893 D->addAttr(::new (S.Context)
2894 PureAttr(Attr.getRange(), S.Context,
2895 Attr.getAttributeSpellingListIndex()));
Anders Carlssonb8316282008-10-05 23:32:53 +00002896}
2897
Chandler Carruthedc2c642011-07-02 00:01:44 +00002898static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002899 VarDecl *VD = dyn_cast<VarDecl>(D);
Anders Carlssond277d792009-01-31 01:16:18 +00002900 if (!VD || !VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002901 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002902 return;
2903 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002904
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002905 Expr *E = Attr.getArgAsExpr(0);
2906 SourceLocation Loc = E->getExprLoc();
2907 FunctionDecl *FD = 0;
2908 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002909
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002910 // gcc only allows for simple identifiers. Since we support more than gcc, we
2911 // will warn the user.
2912 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2913 if (DRE->hasQualifier())
2914 S.Diag(Loc, diag::warn_cleanup_ext);
2915 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2916 NI = DRE->getNameInfo();
2917 if (!FD) {
2918 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2919 << NI.getName();
2920 return;
2921 }
2922 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2923 if (ULE->hasExplicitTemplateArgs())
2924 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002925 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2926 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002927 if (!FD) {
2928 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2929 << NI.getName();
2930 if (ULE->getType() == S.Context.OverloadTy)
2931 S.NoteAllOverloadCandidates(ULE);
2932 return;
2933 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002934 } else {
2935 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002936 return;
2937 }
2938
Anders Carlssond277d792009-01-31 01:16:18 +00002939 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002940 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2941 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002942 return;
2943 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002944
Anders Carlsson723f55d2009-02-07 23:16:50 +00002945 // We're currently more strict than GCC about what function types we accept.
2946 // If this ever proves to be a problem it should be easy to fix.
2947 QualType Ty = S.Context.getPointerType(VD->getType());
2948 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002949 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2950 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002951 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2952 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002953 return;
2954 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002955
Michael Han99315932013-01-24 16:46:58 +00002956 D->addAttr(::new (S.Context)
2957 CleanupAttr(Attr.getRange(), S.Context, FD,
2958 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002959}
2960
Mike Stumpd3bb5572009-07-24 19:02:52 +00002961/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002962/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002963static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002964 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002965 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002966 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002967 return;
2968 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002969
Aaron Ballman00e99962013-08-31 01:11:41 +00002970 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002971 uint64_t ArgIdx;
2972 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2973 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002974 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002975
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002976 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002977 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002978
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002979 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2980 if (not_nsstring_type &&
2981 !isCFStringType(Ty, S.Context) &&
2982 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002983 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002984 // FIXME: Should highlight the actual expression that has the wrong type.
2985 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002986 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002987 << IdxExpr->getSourceRange();
2988 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002989 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002990 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002991 if (!isNSStringType(Ty, S.Context) &&
2992 !isCFStringType(Ty, S.Context) &&
2993 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002994 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002995 // FIXME: Should highlight the actual expression that has the wrong type.
2996 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002997 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002998 << IdxExpr->getSourceRange();
2999 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003000 }
3001
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00003002 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
3003 // because that has corrected for the implicit this parameter, and is zero-
3004 // based. The attribute expects what the user wrote explicitly.
3005 llvm::APSInt Val;
3006 IdxExpr->EvaluateAsInt(Val, S.Context);
3007
Michael Han99315932013-01-24 16:46:58 +00003008 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00003009 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00003010 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003011}
3012
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003013enum FormatAttrKind {
3014 CFStringFormat,
3015 NSStringFormat,
3016 StrftimeFormat,
3017 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003018 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003019 InvalidFormat
3020};
3021
3022/// getFormatAttrKind - Map from format attribute names to supported format
3023/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003024static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003025 return llvm::StringSwitch<FormatAttrKind>(Format)
3026 // Check for formats that get handled specially.
3027 .Case("NSString", NSStringFormat)
3028 .Case("CFString", CFStringFormat)
3029 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003030
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003031 // Otherwise, check for supported formats.
3032 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3033 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3034 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003035
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003036 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3037 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003038}
3039
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003040/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003041/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003042static void handleInitPriorityAttr(Sema &S, Decl *D,
3043 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003044 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003045 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3046 return;
3047 }
3048
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003049 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003050 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3051 Attr.setInvalid();
3052 return;
3053 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003054 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003055 if (S.Context.getAsArrayType(T))
3056 T = S.Context.getBaseElementType(T);
3057 if (!T->getAs<RecordType>()) {
3058 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3059 Attr.setInvalid();
3060 return;
3061 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003062
3063 Expr *E = Attr.getArgAsExpr(0);
3064 uint32_t prioritynum;
3065 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003066 Attr.setInvalid();
3067 return;
3068 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003069
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003070 if (prioritynum < 101 || prioritynum > 65535) {
3071 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003072 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003073 Attr.setInvalid();
3074 return;
3075 }
Michael Han99315932013-01-24 16:46:58 +00003076 D->addAttr(::new (S.Context)
3077 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3078 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003079}
3080
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003081FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3082 IdentifierInfo *Format, int FormatIdx,
3083 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00003084 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003085 // Check whether we already have an equivalent format attribute.
3086 for (specific_attr_iterator<FormatAttr>
3087 i = D->specific_attr_begin<FormatAttr>(),
3088 e = D->specific_attr_end<FormatAttr>();
3089 i != e ; ++i) {
3090 FormatAttr *f = *i;
3091 if (f->getType() == Format &&
3092 f->getFormatIdx() == FormatIdx &&
3093 f->getFirstArg() == FirstArg) {
3094 // If we don't have a valid location for this attribute, adopt the
3095 // location.
3096 if (f->getLocation().isInvalid())
3097 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003098 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00003099 }
3100 }
3101
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003102 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3103 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003104}
3105
Mike Stumpd3bb5572009-07-24 19:02:52 +00003106/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003107/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003108static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003109 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003110 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00003111 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003112 return;
3113 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003114
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003115 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003116 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003117 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003118 return;
3119 }
3120
Chandler Carruth743682b2010-11-16 08:35:43 +00003121 // In C++ the implicit 'this' function parameter also counts, and they are
3122 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003123 bool HasImplicitThisParam = isInstanceMethod(D);
3124 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003125
Aaron Ballman00e99962013-08-31 01:11:41 +00003126 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
3127 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003128
3129 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003130 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003131 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003132 // If we've modified the string name, we need a new identifier for it.
3133 II = &S.Context.Idents.get(Format);
3134 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003135
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003136 // Check for supported formats.
3137 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00003138
3139 if (Kind == IgnoredFormat)
3140 return;
3141
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003142 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00003143 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman00e99962013-08-31 01:11:41 +00003144 << "format" << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003145 return;
3146 }
3147
3148 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00003149 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003150 uint32_t Idx;
3151 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003152 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003153
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003154 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003155 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003156 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003157 return;
3158 }
3159
3160 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003161 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003162
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003163 if (HasImplicitThisParam) {
3164 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00003165 S.Diag(Attr.getLoc(),
3166 diag::err_format_attribute_implicit_this_format_string)
3167 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003168 return;
3169 }
3170 ArgIdx--;
3171 }
Mike Stump11289f42009-09-09 15:08:12 +00003172
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003173 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003174 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003175
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003176 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003177 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003178 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3179 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00003180 return;
3181 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003182 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003183 // FIXME: do we need to check if the type is NSString*? What are the
3184 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003185 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003186 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003187 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3188 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003189 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003190 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003191 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003192 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003193 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003194 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3195 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003196 return;
3197 }
3198
3199 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00003200 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003201 uint32_t FirstArg;
3202 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003203 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003204
3205 // check if the function is variadic if the 3rd argument non-zero
3206 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003207 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003208 ++NumArgs; // +1 for ...
3209 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003210 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003211 return;
3212 }
3213 }
3214
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003215 // strftime requires FirstArg to be 0 because it doesn't read from any
3216 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003217 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003218 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00003219 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3220 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003221 return;
3222 }
3223 // if 0 it disables parameter checking (to use with e.g. va_list)
3224 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003225 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003226 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003227 return;
3228 }
3229
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003230 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003231 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00003232 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003233 if (NewAttr)
3234 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003235}
3236
Chandler Carruthedc2c642011-07-02 00:01:44 +00003237static void handleTransparentUnionAttr(Sema &S, Decl *D,
3238 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003239 // Try to find the underlying union declaration.
3240 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003241 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003242 if (TD && TD->getUnderlyingType()->isUnionType())
3243 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3244 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003245 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003246
3247 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00003248 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003249 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003250 return;
3251 }
3252
John McCallf937c022011-10-07 06:10:15 +00003253 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003254 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003255 diag::warn_transparent_union_attribute_not_definition);
3256 return;
3257 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003258
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003259 RecordDecl::field_iterator Field = RD->field_begin(),
3260 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003261 if (Field == FieldEnd) {
3262 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3263 return;
3264 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003265
David Blaikie40ed2972012-06-06 20:45:41 +00003266 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003267 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003268 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003269 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003270 diag::warn_transparent_union_attribute_floating)
3271 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003272 return;
3273 }
3274
3275 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3276 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3277 for (; Field != FieldEnd; ++Field) {
3278 QualType FieldType = Field->getType();
3279 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3280 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3281 // Warn if we drop the attribute.
3282 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003283 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003284 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003285 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003286 diag::warn_transparent_union_attribute_field_size_align)
3287 << isSize << Field->getDeclName() << FieldBits;
3288 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003289 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003290 diag::note_transparent_union_first_field_size_align)
3291 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003292 return;
3293 }
3294 }
3295
Michael Han99315932013-01-24 16:46:58 +00003296 RD->addAttr(::new (S.Context)
3297 TransparentUnionAttr(Attr.getRange(), S.Context,
3298 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003299}
3300
Chandler Carruthedc2c642011-07-02 00:01:44 +00003301static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003302 // Make sure that there is a string literal as the annotation's single
3303 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003304 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003305 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003306 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003307
3308 // Don't duplicate annotations that are already set.
3309 for (specific_attr_iterator<AnnotateAttr>
3310 i = D->specific_attr_begin<AnnotateAttr>(),
3311 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003312 if ((*i)->getAnnotation() == Str)
3313 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003314 }
Michael Han99315932013-01-24 16:46:58 +00003315
3316 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003317 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00003318 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003319}
3320
Chandler Carruthedc2c642011-07-02 00:01:44 +00003321static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003322 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003323 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003324 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3325 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003326 return;
3327 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003328
Richard Smith848e1f12013-02-01 08:12:08 +00003329 if (Attr.getNumArgs() == 0) {
3330 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3331 true, 0, Attr.getAttributeSpellingListIndex()));
3332 return;
3333 }
3334
Aaron Ballman00e99962013-08-31 01:11:41 +00003335 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00003336 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3337 S.Diag(Attr.getEllipsisLoc(),
3338 diag::err_pack_expansion_without_parameter_packs);
3339 return;
3340 }
3341
3342 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3343 return;
3344
3345 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3346 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003347}
3348
3349void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003350 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003351 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3352 SourceLocation AttrLoc = AttrRange.getBegin();
3353
Richard Smith1dba27c2013-01-29 09:02:09 +00003354 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003355 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003356 // C++11 [dcl.align]p1:
3357 // An alignment-specifier may be applied to a variable or to a class
3358 // data member, but it shall not be applied to a bit-field, a function
3359 // parameter, the formal parameter of a catch clause, or a variable
3360 // declared with the register storage class specifier. An
3361 // alignment-specifier may also be applied to the declaration of a class
3362 // or enumeration type.
3363 // C11 6.7.5/2:
3364 // An alignment attribute shall not be specified in a declaration of
3365 // a typedef, or a bit-field, or a function, or a parameter, or an
3366 // object declared with the register storage-class specifier.
3367 int DiagKind = -1;
3368 if (isa<ParmVarDecl>(D)) {
3369 DiagKind = 0;
3370 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3371 if (VD->getStorageClass() == SC_Register)
3372 DiagKind = 1;
3373 if (VD->isExceptionVariable())
3374 DiagKind = 2;
3375 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3376 if (FD->isBitField())
3377 DiagKind = 3;
3378 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00003379 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3380 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00003381 << (TmpAttr.isC11() ? ExpectedVariableOrField
3382 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003383 return;
3384 }
3385 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003386 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smithbc8caaf2013-02-22 04:55:39 +00003387 << TmpAttr.isC11() << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003388 return;
3389 }
3390 }
3391
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003392 if (E->isTypeDependent() || E->isValueDependent()) {
3393 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003394 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3395 AA->setPackExpansion(IsPackExpansion);
3396 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003397 return;
3398 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003399
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003400 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00003401 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00003402 ExprResult ICE
3403 = VerifyIntegerConstantExpression(E, &Alignment,
3404 diag::err_aligned_attribute_argument_not_int,
3405 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003406 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003407 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003408
3409 // C++11 [dcl.align]p2:
3410 // -- if the constant expression evaluates to zero, the alignment
3411 // specifier shall have no effect
3412 // C11 6.7.5p6:
3413 // An alignment specification of zero has no effect.
3414 if (!(TmpAttr.isAlignas() && !Alignment) &&
3415 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003416 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3417 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003418 return;
3419 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003420
Richard Smith848e1f12013-02-01 08:12:08 +00003421 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00003422 // We've already verified it's a power of 2, now let's make sure it's
3423 // 8192 or less.
3424 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00003425 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00003426 << E->getSourceRange();
3427 return;
3428 }
3429 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003430
Richard Smith44c247f2013-02-22 08:32:16 +00003431 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3432 ICE.take(), SpellingListIndex);
3433 AA->setPackExpansion(IsPackExpansion);
3434 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003435}
3436
Michael Hanaf02bbe2013-02-01 01:19:17 +00003437void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003438 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003439 // FIXME: Cache the number on the Attr object if non-dependent?
3440 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003441 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3442 SpellingListIndex);
3443 AA->setPackExpansion(IsPackExpansion);
3444 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003445}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003446
Richard Smith848e1f12013-02-01 08:12:08 +00003447void Sema::CheckAlignasUnderalignment(Decl *D) {
3448 assert(D->hasAttrs() && "no attributes on decl");
3449
3450 QualType Ty;
3451 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3452 Ty = VD->getType();
3453 else
3454 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00003455 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003456 return;
3457
3458 // C++11 [dcl.align]p5, C11 6.7.5/4:
3459 // The combined effect of all alignment attributes in a declaration shall
3460 // not specify an alignment that is less strict than the alignment that
3461 // would otherwise be required for the entity being declared.
3462 AlignedAttr *AlignasAttr = 0;
3463 unsigned Align = 0;
3464 for (specific_attr_iterator<AlignedAttr>
3465 I = D->specific_attr_begin<AlignedAttr>(),
3466 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3467 if (I->isAlignmentDependent())
3468 return;
3469 if (I->isAlignas())
3470 AlignasAttr = *I;
3471 Align = std::max(Align, I->getAlignment(Context));
3472 }
3473
3474 if (AlignasAttr && Align) {
3475 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3476 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3477 if (NaturalAlign > RequestedAlign)
3478 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3479 << Ty << (unsigned)NaturalAlign.getQuantity();
3480 }
3481}
3482
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003483/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003484/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003485///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003486/// Despite what would be logical, the mode attribute is a decl attribute, not a
3487/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3488/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003489static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003490 // This attribute isn't documented, but glibc uses it. It changes
3491 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00003492 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00003493 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3494 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003495 return;
3496 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003497
Aaron Ballman00e99962013-08-31 01:11:41 +00003498 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3499 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003500
3501 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003502 if (Str.startswith("__") && Str.endswith("__"))
3503 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003504
3505 unsigned DestWidth = 0;
3506 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003507 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003508 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003509 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003510 switch (Str[0]) {
3511 case 'Q': DestWidth = 8; break;
3512 case 'H': DestWidth = 16; break;
3513 case 'S': DestWidth = 32; break;
3514 case 'D': DestWidth = 64; break;
3515 case 'X': DestWidth = 96; break;
3516 case 'T': DestWidth = 128; break;
3517 }
3518 if (Str[1] == 'F') {
3519 IntegerMode = false;
3520 } else if (Str[1] == 'C') {
3521 IntegerMode = false;
3522 ComplexMode = true;
3523 } else if (Str[1] != 'I') {
3524 DestWidth = 0;
3525 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003526 break;
3527 case 4:
3528 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3529 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003530 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003531 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003532 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003533 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003534 break;
3535 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003536 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003537 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003538 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003539 case 11:
3540 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003541 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003542 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003543 }
3544
3545 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003546 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003547 OldTy = TD->getUnderlyingType();
3548 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3549 OldTy = VD->getType();
3550 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003551 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003552 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003553 return;
3554 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003555
John McCall9dd450b2009-09-21 23:43:11 +00003556 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003557 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3558 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003559 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003560 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3561 } else if (ComplexMode) {
3562 if (!OldTy->isComplexType())
3563 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3564 } else {
3565 if (!OldTy->isFloatingType())
3566 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3567 }
3568
Mike Stump87c57ac2009-05-16 07:39:55 +00003569 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3570 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003571 // FIXME: Make sure floating-point mappings are accurate
3572 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003573 if (!DestWidth) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003574 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003575 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003576 }
3577
3578 QualType NewTy;
3579
3580 if (IntegerMode)
3581 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3582 OldTy->isSignedIntegerType());
3583 else
3584 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3585
3586 if (NewTy.isNull()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003587 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003588 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003589 }
3590
Eli Friedman4735374e2009-03-03 06:41:03 +00003591 if (ComplexMode) {
3592 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003593 }
3594
3595 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003596 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3597 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3598 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003599 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003600
3601 D->addAttr(::new (S.Context)
3602 ModeAttr(Attr.getRange(), S.Context, Name,
3603 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003604}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003605
Chandler Carruthedc2c642011-07-02 00:01:44 +00003606static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003607 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3608 if (!VD->hasGlobalStorage())
3609 S.Diag(Attr.getLoc(),
3610 diag::warn_attribute_requires_functions_or_static_globals)
3611 << Attr.getName();
3612 } else if (!isFunctionOrMethod(D)) {
3613 S.Diag(Attr.getLoc(),
3614 diag::warn_attribute_requires_functions_or_static_globals)
3615 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003616 return;
3617 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003618
Michael Han99315932013-01-24 16:46:58 +00003619 D->addAttr(::new (S.Context)
3620 NoDebugAttr(Attr.getRange(), S.Context,
3621 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003622}
3623
Chandler Carruthedc2c642011-07-02 00:01:44 +00003624static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003625 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00003626 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003627 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00003628 return;
3629 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003630
Michael Han99315932013-01-24 16:46:58 +00003631 D->addAttr(::new (S.Context)
3632 NoInlineAttr(Attr.getRange(), S.Context,
3633 Attr.getAttributeSpellingListIndex()));
Anders Carlsson88097122009-02-19 19:16:48 +00003634}
3635
Chandler Carruthedc2c642011-07-02 00:01:44 +00003636static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3637 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003638 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003639 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003640 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00003641 return;
3642 }
3643
Michael Han99315932013-01-24 16:46:58 +00003644 D->addAttr(::new (S.Context)
3645 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3646 Attr.getAttributeSpellingListIndex()));
Chris Lattner3c77a352010-06-22 00:03:40 +00003647}
3648
Chandler Carruthedc2c642011-07-02 00:01:44 +00003649static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003650 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003651 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003652 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003653 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003654 return;
3655 }
3656
Michael Han99315932013-01-24 16:46:58 +00003657 D->addAttr(::new (S.Context)
3658 CUDAConstantAttr(Attr.getRange(), S.Context,
3659 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003660 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003661 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003662 }
3663}
3664
Chandler Carruthedc2c642011-07-02 00:01:44 +00003665static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003666 if (S.LangOpts.CUDA) {
3667 // check the attribute arguments.
3668 if (Attr.getNumArgs() != 0) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003669 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3670 << Attr.getName() << 0;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003671 return;
3672 }
3673
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003674 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003675 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003676 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003677 return;
3678 }
3679
Michael Han99315932013-01-24 16:46:58 +00003680 D->addAttr(::new (S.Context)
3681 CUDADeviceAttr(Attr.getRange(), S.Context,
3682 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003683 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003684 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003685 }
3686}
3687
Chandler Carruthedc2c642011-07-02 00:01:44 +00003688static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003689 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003690 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003691 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003692 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003693 return;
3694 }
3695
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003696 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003697 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00003698 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie6adc78e2013-02-18 22:06:02 +00003699 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003700 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3701 << FD->getType()
David Blaikie6adc78e2013-02-18 22:06:02 +00003702 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003703 "void");
3704 } else {
3705 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3706 << FD->getType();
3707 }
3708 return;
3709 }
3710
Michael Han99315932013-01-24 16:46:58 +00003711 D->addAttr(::new (S.Context)
3712 CUDAGlobalAttr(Attr.getRange(), S.Context,
3713 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003714 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003715 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003716 }
3717}
3718
Chandler Carruthedc2c642011-07-02 00:01:44 +00003719static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003720 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003721 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003722 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003723 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003724 return;
3725 }
3726
Michael Han99315932013-01-24 16:46:58 +00003727 D->addAttr(::new (S.Context)
3728 CUDAHostAttr(Attr.getRange(), S.Context,
3729 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003730 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003731 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003732 }
3733}
3734
Chandler Carruthedc2c642011-07-02 00:01:44 +00003735static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003736 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003737 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003738 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003739 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003740 return;
3741 }
3742
Michael Han99315932013-01-24 16:46:58 +00003743 D->addAttr(::new (S.Context)
3744 CUDASharedAttr(Attr.getRange(), S.Context,
3745 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003746 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003747 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003748 }
3749}
3750
Chandler Carruthedc2c642011-07-02 00:01:44 +00003751static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003752 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00003753 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003754 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003755 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00003756 return;
3757 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003758
Douglas Gregor35b57532009-10-27 21:01:01 +00003759 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003760 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003761 return;
3762 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003763
Michael Han99315932013-01-24 16:46:58 +00003764 D->addAttr(::new (S.Context)
3765 GNUInlineAttr(Attr.getRange(), S.Context,
3766 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003767}
3768
Chandler Carruthedc2c642011-07-02 00:01:44 +00003769static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003770 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003771
Aaron Ballman02df2e02012-12-09 17:45:41 +00003772 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003773 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003774 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3775 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003776 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003777 return;
3778
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003779 if (!isa<ObjCMethodDecl>(D)) {
3780 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3781 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003782 return;
3783 }
3784
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003785 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003786 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003787 D->addAttr(::new (S.Context)
3788 FastCallAttr(Attr.getRange(), S.Context,
3789 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003790 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003791 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003792 D->addAttr(::new (S.Context)
3793 StdCallAttr(Attr.getRange(), S.Context,
3794 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003795 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003796 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003797 D->addAttr(::new (S.Context)
3798 ThisCallAttr(Attr.getRange(), S.Context,
3799 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003800 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003801 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003802 D->addAttr(::new (S.Context)
3803 CDeclAttr(Attr.getRange(), S.Context,
3804 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003805 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003806 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003807 D->addAttr(::new (S.Context)
3808 PascalAttr(Attr.getRange(), S.Context,
3809 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003810 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003811 case AttributeList::AT_MSABI:
3812 D->addAttr(::new (S.Context)
3813 MSABIAttr(Attr.getRange(), S.Context,
3814 Attr.getAttributeSpellingListIndex()));
3815 return;
3816 case AttributeList::AT_SysVABI:
3817 D->addAttr(::new (S.Context)
3818 SysVABIAttr(Attr.getRange(), S.Context,
3819 Attr.getAttributeSpellingListIndex()));
3820 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003821 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003822 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003823 switch (CC) {
3824 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003825 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003826 break;
3827 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003828 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003829 break;
3830 default:
3831 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003832 }
3833
Michael Han99315932013-01-24 16:46:58 +00003834 D->addAttr(::new (S.Context)
3835 PcsAttr(Attr.getRange(), S.Context, PCS,
3836 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003837 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003838 }
Derek Schuffa2020962012-10-16 22:30:41 +00003839 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003840 D->addAttr(::new (S.Context)
3841 PnaclCallAttr(Attr.getRange(), S.Context,
3842 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003843 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003844 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003845 D->addAttr(::new (S.Context)
3846 IntelOclBiccAttr(Attr.getRange(), S.Context,
3847 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003848 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003849
Abramo Bagnara50099372010-04-30 13:10:51 +00003850 default:
3851 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003852 }
3853}
3854
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003855static void handleOpenCLKernelAttr(Sema &S, Decl *D,
3856 const AttributeList &Attr) {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003857 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003858}
3859
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003860static void handleOpenCLImageAccessAttr(Sema &S, Decl *D,
3861 const AttributeList &Attr) {
3862 uint32_t ArgNum;
3863 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), ArgNum))
Guy Benyeifb36ede2013-03-24 13:58:12 +00003864 return;
Guy Benyeifb36ede2013-03-24 13:58:12 +00003865
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003866 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(Attr.getRange(),
3867 S.Context, ArgNum));
Guy Benyeifb36ede2013-03-24 13:58:12 +00003868}
3869
Aaron Ballman02df2e02012-12-09 17:45:41 +00003870bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3871 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003872 if (attr.isInvalid())
3873 return true;
3874
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003875 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003876 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003877 attr.setInvalid();
3878 return true;
3879 }
3880
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003881 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3882 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003883 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003884 case AttributeList::AT_CDecl: CC = CC_C; break;
3885 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3886 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3887 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3888 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003889 case AttributeList::AT_MSABI:
3890 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3891 CC_X86_64Win64;
3892 break;
3893 case AttributeList::AT_SysVABI:
3894 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3895 CC_C;
3896 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003897 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003898 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003899 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003900 attr.setInvalid();
3901 return true;
3902 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003903 if (StrRef == "aapcs") {
3904 CC = CC_AAPCS;
3905 break;
3906 } else if (StrRef == "aapcs-vfp") {
3907 CC = CC_AAPCS_VFP;
3908 break;
3909 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003910
3911 attr.setInvalid();
3912 Diag(attr.getLoc(), diag::err_invalid_pcs);
3913 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003914 }
Derek Schuffa2020962012-10-16 22:30:41 +00003915 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003916 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003917 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003918 }
3919
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003920 const TargetInfo &TI = Context.getTargetInfo();
3921 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3922 if (A == TargetInfo::CCCR_Warning) {
3923 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003924
3925 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3926 if (FD)
3927 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3928 TargetInfo::CCMT_NonMember;
3929 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003930 }
3931
John McCall3882ace2011-01-05 12:14:39 +00003932 return false;
3933}
3934
Chandler Carruthedc2c642011-07-02 00:01:44 +00003935static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003936 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003937
3938 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003939 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003940 return;
3941
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003942 if (!isa<ObjCMethodDecl>(D)) {
3943 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3944 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003945 return;
3946 }
Eli Friedman7044b762009-03-27 21:06:47 +00003947
Michael Han99315932013-01-24 16:46:58 +00003948 D->addAttr(::new (S.Context)
3949 RegparmAttr(Attr.getRange(), S.Context, numParams,
3950 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00003951}
3952
3953/// Checks a regparm attribute, returning true if it is ill-formed and
3954/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003955bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3956 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003957 return true;
3958
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003959 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003960 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003961 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003962 }
Eli Friedman7044b762009-03-27 21:06:47 +00003963
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003964 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003965 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003966 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003967 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003968 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003969 }
3970
Douglas Gregore8bbc122011-09-02 00:18:52 +00003971 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003972 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003973 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003974 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003975 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003976 }
3977
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003978 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003979 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003980 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003981 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003982 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003983 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003984 }
3985
John McCall3882ace2011-01-05 12:14:39 +00003986 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003987}
3988
Chandler Carruthedc2c642011-07-02 00:01:44 +00003989static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00003990 if (S.LangOpts.CUDA) {
3991 // check the attribute arguments.
3992 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00003993 // FIXME: 0 is not okay.
3994 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003995 return;
3996 }
3997
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003998 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00003999 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00004000 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004001 return;
4002 }
4003
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004004 uint32_t MaxThreads, MinBlocks;
4005 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1) ||
4006 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(1), MinBlocks, 2))
Peter Collingbourne827301e2010-12-12 23:03:07 +00004007 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004008
Michael Han99315932013-01-24 16:46:58 +00004009 D->addAttr(::new (S.Context)
4010 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004011 MaxThreads, MinBlocks,
Michael Han99315932013-01-24 16:46:58 +00004012 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00004013 } else {
4014 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4015 }
4016}
4017
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004018static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4019 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00004020 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00004021 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00004022 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004023 return;
4024 }
Aaron Ballman00e99962013-08-31 01:11:41 +00004025
4026 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004027 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004028
Aaron Ballman00e99962013-08-31 01:11:41 +00004029 StringRef AttrName = Attr.getName()->getName();
4030 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004031
4032 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4033 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4034 << Attr.getName() << ExpectedFunctionOrMethod;
4035 return;
4036 }
4037
4038 uint64_t ArgumentIdx;
4039 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4040 Attr.getLoc(), 2,
Aaron Ballman00e99962013-08-31 01:11:41 +00004041 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004042 return;
4043
4044 uint64_t TypeTagIdx;
4045 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4046 Attr.getLoc(), 3,
Aaron Ballman00e99962013-08-31 01:11:41 +00004047 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004048 return;
4049
4050 bool IsPointer = (AttrName == "pointer_with_type_tag");
4051 if (IsPointer) {
4052 // Ensure that buffer has a pointer type.
4053 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4054 if (!BufferTy->isPointerType()) {
4055 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00004056 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004057 }
4058 }
4059
Michael Han99315932013-01-24 16:46:58 +00004060 D->addAttr(::new (S.Context)
4061 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4062 ArgumentIdx, TypeTagIdx, IsPointer,
4063 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004064}
4065
4066static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4067 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00004068 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00004069 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00004070 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004071 return;
4072 }
Aaron Ballman00e99962013-08-31 01:11:41 +00004073
4074 if (!checkAttributeNumArgs(S, Attr, 1))
4075 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004076
Aaron Ballman00e99962013-08-31 01:11:41 +00004077 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00004078 TypeSourceInfo *MatchingCTypeLoc = 0;
4079 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
4080 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004081
Michael Han99315932013-01-24 16:46:58 +00004082 D->addAttr(::new (S.Context)
4083 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00004084 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00004085 Attr.getLayoutCompatible(),
4086 Attr.getMustBeNull(),
4087 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004088}
4089
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004090//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004091// Checker-specific attribute handlers.
4092//===----------------------------------------------------------------------===//
4093
John McCalled433932011-01-25 03:31:58 +00004094static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004095 return type->isDependentType() ||
4096 type->isObjCObjectPointerType() ||
4097 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00004098}
4099static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004100 return type->isDependentType() ||
4101 type->isPointerType() ||
4102 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00004103}
4104
Chandler Carruthedc2c642011-07-02 00:01:44 +00004105static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004106 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00004107 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004108 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004109 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00004110 return;
4111 }
4112
4113 bool typeOK, cf;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004114 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00004115 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4116 cf = false;
4117 } else {
4118 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4119 cf = true;
4120 }
4121
4122 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004123 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004124 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00004125 return;
4126 }
4127
4128 if (cf)
Michael Han99315932013-01-24 16:46:58 +00004129 param->addAttr(::new (S.Context)
4130 CFConsumedAttr(Attr.getRange(), S.Context,
4131 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004132 else
Michael Han99315932013-01-24 16:46:58 +00004133 param->addAttr(::new (S.Context)
4134 NSConsumedAttr(Attr.getRange(), S.Context,
4135 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004136}
4137
Chandler Carruthedc2c642011-07-02 00:01:44 +00004138static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4139 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004140 if (!isa<ObjCMethodDecl>(D)) {
4141 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004142 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00004143 return;
4144 }
4145
Michael Han99315932013-01-24 16:46:58 +00004146 D->addAttr(::new (S.Context)
4147 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4148 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004149}
4150
Chandler Carruthedc2c642011-07-02 00:01:44 +00004151static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4152 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004153
John McCalled433932011-01-25 03:31:58 +00004154 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004155
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004156 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004157 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004158 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004159 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004160 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00004161 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4162 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004163 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004164 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00004165 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004166 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004167 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00004168 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004169 return;
4170 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004171
John McCalled433932011-01-25 03:31:58 +00004172 bool typeOK;
4173 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004174 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004175 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004176 case AttributeList::AT_NSReturnsAutoreleased:
4177 case AttributeList::AT_NSReturnsRetained:
4178 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004179 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4180 cf = false;
4181 break;
4182
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004183 case AttributeList::AT_CFReturnsRetained:
4184 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004185 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4186 cf = true;
4187 break;
4188 }
4189
4190 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004191 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004192 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004193 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004194 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004195
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004196 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004197 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004198 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004199 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00004200 D->addAttr(::new (S.Context)
4201 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4202 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004203 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004204 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004205 D->addAttr(::new (S.Context)
4206 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4207 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004208 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004209 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004210 D->addAttr(::new (S.Context)
4211 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4212 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004213 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004214 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004215 D->addAttr(::new (S.Context)
4216 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4217 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004218 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004219 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004220 D->addAttr(::new (S.Context)
4221 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4222 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004223 return;
4224 };
4225}
4226
John McCallcf166702011-07-22 08:53:00 +00004227static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4228 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00004229 const int EP_ObjCMethod = 1;
4230 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004231
John McCallcf166702011-07-22 08:53:00 +00004232 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004233 QualType resultType;
4234
John McCallcf166702011-07-22 08:53:00 +00004235 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4236
Fariborz Jahaniana53e5d72012-04-21 17:51:44 +00004237 if (!method) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004238 ObjCPropertyDecl *property = dyn_cast<ObjCPropertyDecl>(D);
4239 if (!property) {
4240 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4241 << SourceRange(loc, loc) << attr.getName() << ExpectedMethodOrProperty;
4242 return;
4243 }
4244 resultType = property->getType();
John McCallcf166702011-07-22 08:53:00 +00004245 }
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004246 else
4247 // Check that the method returns a normal pointer.
4248 resultType = method->getResultType();
John McCallcf166702011-07-22 08:53:00 +00004249
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004250 if (!resultType->isReferenceType() &&
4251 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004252 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00004253 << SourceRange(loc)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004254 << attr.getName() << (method ? EP_ObjCMethod : EP_ObjCProperty)
4255 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00004256
4257 // Drop the attribute.
4258 return;
4259 }
4260
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004261 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004262 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4263 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004264}
4265
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004266static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4267 const AttributeList &attr) {
4268 SourceLocation loc = attr.getLoc();
4269 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4270
4271 if (!method) {
4272 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4273 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4274 return;
4275 }
4276 DeclContext *DC = method->getDeclContext();
4277 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4278 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4279 << attr.getName() << 0;
4280 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4281 return;
4282 }
4283 if (method->getMethodFamily() == OMF_dealloc) {
4284 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4285 << attr.getName() << 1;
4286 return;
4287 }
4288
Michael Han99315932013-01-24 16:46:58 +00004289 method->addAttr(::new (S.Context)
4290 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4291 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004292}
4293
John McCall32f5fe12011-09-30 05:12:12 +00004294/// Handle cf_audited_transfer and cf_unknown_transfer.
4295static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4296 if (!isa<FunctionDecl>(D)) {
4297 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004298 << A.getRange() << A.getName() << ExpectedFunction;
John McCall32f5fe12011-09-30 05:12:12 +00004299 return;
4300 }
4301
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004302 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall32f5fe12011-09-30 05:12:12 +00004303
4304 // Check whether there's a conflicting attribute already present.
4305 Attr *Existing;
4306 if (IsAudited) {
4307 Existing = D->getAttr<CFUnknownTransferAttr>();
4308 } else {
4309 Existing = D->getAttr<CFAuditedTransferAttr>();
4310 }
4311 if (Existing) {
4312 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4313 << A.getName()
4314 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4315 << A.getRange() << Existing->getRange();
4316 return;
4317 }
4318
4319 // All clear; add the attribute.
4320 if (IsAudited) {
Michael Han99315932013-01-24 16:46:58 +00004321 D->addAttr(::new (S.Context)
4322 CFAuditedTransferAttr(A.getRange(), S.Context,
4323 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004324 } else {
Michael Han99315932013-01-24 16:46:58 +00004325 D->addAttr(::new (S.Context)
4326 CFUnknownTransferAttr(A.getRange(), S.Context,
4327 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004328 }
4329}
4330
John McCallf1e8b342011-09-29 07:17:38 +00004331static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4332 const AttributeList &Attr) {
4333 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4334 if (!RD || RD->isUnion()) {
4335 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004336 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00004337 }
4338
Aaron Ballman00e99962013-08-31 01:11:41 +00004339 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallf1e8b342011-09-29 07:17:38 +00004340
4341 // In Objective-C, verify that the type names an Objective-C type.
4342 // We don't want to check this outside of ObjC because people sometimes
4343 // do crazy C declarations of Objective-C types.
Aaron Ballman00e99962013-08-31 01:11:41 +00004344 if (Parm && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00004345 // Check for an existing type with this name.
Aaron Ballman00e99962013-08-31 01:11:41 +00004346 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallf1e8b342011-09-29 07:17:38 +00004347 Sema::LookupOrdinaryName);
4348 if (S.LookupName(R, Sc)) {
4349 NamedDecl *Target = R.getFoundDecl();
4350 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4351 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4352 S.Diag(Target->getLocStart(), diag::note_declared_at);
4353 }
4354 }
4355 }
4356
Michael Han99315932013-01-24 16:46:58 +00004357 D->addAttr(::new (S.Context)
Aaron Ballman00e99962013-08-31 01:11:41 +00004358 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han99315932013-01-24 16:46:58 +00004359 Attr.getAttributeSpellingListIndex()));
John McCallf1e8b342011-09-29 07:17:38 +00004360}
4361
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004362static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
4363 const AttributeList &Attr) {
Fariborz Jahaniandb3d8552013-11-19 00:09:48 +00004364 if (!isa<RecordDecl>(D)) {
Fariborz Jahanianf720f862013-11-19 17:42:25 +00004365 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4366 << Attr.getName()
4367 << (S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass
4368 : ExpectedStructOrUnion);
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004369 return;
4370 }
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004371
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00004372 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004373
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004374 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004375 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004376 return;
4377 }
4378
4379 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004380 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004381 Attr.getAttributeSpellingListIndex()));
4382}
4383
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004384static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
4385 const AttributeList &Attr) {
4386 if (!isa<RecordDecl>(D)) {
4387 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4388 << Attr.getName()
4389 << (S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass
4390 : ExpectedStructOrUnion);
4391 return;
4392 }
4393
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00004394 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004395
4396 if (!Parm) {
4397 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
4398 return;
4399 }
4400
4401 D->addAttr(::new (S.Context)
4402 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
4403 Attr.getAttributeSpellingListIndex()));
4404}
4405
Chandler Carruthedc2c642011-07-02 00:01:44 +00004406static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4407 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004408 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004409
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004410 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004411 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004412}
4413
Chandler Carruthedc2c642011-07-02 00:01:44 +00004414static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4415 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004416 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004417 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004418 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004419 return;
4420 }
4421
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004422 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004423 QualType type = vd->getType();
4424
4425 if (!type->isDependentType() &&
4426 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004427 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004428 << type;
4429 return;
4430 }
4431
4432 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4433
4434 // If we have no lifetime yet, check the lifetime we're presumably
4435 // going to infer.
4436 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4437 lifetime = type->getObjCARCImplicitLifetime();
4438
4439 switch (lifetime) {
4440 case Qualifiers::OCL_None:
4441 assert(type->isDependentType() &&
4442 "didn't infer lifetime for non-dependent type?");
4443 break;
4444
4445 case Qualifiers::OCL_Weak: // meaningful
4446 case Qualifiers::OCL_Strong: // meaningful
4447 break;
4448
4449 case Qualifiers::OCL_ExplicitNone:
4450 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004451 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004452 << (lifetime == Qualifiers::OCL_Autoreleasing);
4453 break;
4454 }
4455
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004456 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004457 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4458 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004459}
4460
Francois Picheta83957a2010-12-19 06:50:37 +00004461//===----------------------------------------------------------------------===//
4462// Microsoft specific attribute handlers.
4463//===----------------------------------------------------------------------===//
4464
Reid Kleckner140c4a72013-05-17 14:04:52 +00004465// Check if MS extensions or some other language extensions are enabled. If
4466// not, issue a diagnostic that the given attribute is unused.
4467static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4468 bool OtherExtension = false) {
4469 if (S.LangOpts.MicrosoftExt || OtherExtension)
4470 return true;
4471 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4472 return false;
4473}
4474
Chandler Carruthedc2c642011-07-02 00:01:44 +00004475static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004476 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4477 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004478
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004479 StringRef StrRef;
4480 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00004481 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00004482 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004483
David Majnemer89085342013-08-09 08:56:20 +00004484 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4485 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00004486 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4487 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00004488
Reid Kleckner140c4a72013-05-17 14:04:52 +00004489 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00004490 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004491 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004492 return;
4493 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004494
David Majnemer89085342013-08-09 08:56:20 +00004495 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004496 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00004497 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004498 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00004499 return;
4500 }
David Majnemer89085342013-08-09 08:56:20 +00004501 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004502 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004503 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004504 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004505 }
Francois Picheta83957a2010-12-19 06:50:37 +00004506
David Majnemer89085342013-08-09 08:56:20 +00004507 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4508 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004509}
4510
John McCall8d32c052012-05-22 21:28:12 +00004511static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004512 if (!checkMicrosoftExt(S, Attr))
Nico Weberefa45b22012-11-07 21:31:36 +00004513 return;
Nico Weberefa45b22012-11-07 21:31:36 +00004514
4515 AttributeList::Kind Kind = Attr.getKind();
4516 if (Kind == AttributeList::AT_SingleInheritance)
4517 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004518 ::new (S.Context)
4519 SingleInheritanceAttr(Attr.getRange(), S.Context,
4520 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004521 else if (Kind == AttributeList::AT_MultipleInheritance)
4522 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004523 ::new (S.Context)
4524 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4525 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004526 else if (Kind == AttributeList::AT_VirtualInheritance)
4527 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004528 ::new (S.Context)
4529 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4530 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004531}
4532
4533static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004534 if (!checkMicrosoftExt(S, Attr))
4535 return;
4536
4537 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmancc14f3a2013-11-22 21:49:04 +00004538 if (Kind == AttributeList::AT_Win64)
4539 D->addAttr(
4540 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4541 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004542}
4543
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004544static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004545 if (!checkMicrosoftExt(S, Attr))
4546 return;
4547 D->addAttr(::new (S.Context)
4548 ForceInlineAttr(Attr.getRange(), S.Context,
4549 Attr.getAttributeSpellingListIndex()));
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004550}
4551
Reid Klecknerb144d362013-05-20 14:02:37 +00004552static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4553 if (!checkMicrosoftExt(S, Attr))
4554 return;
4555 // Check linkage after possibly merging declaratinos. See
4556 // checkAttributesAfterMerging().
4557 D->addAttr(::new (S.Context)
4558 SelectAnyAttr(Attr.getRange(), S.Context,
4559 Attr.getAttributeSpellingListIndex()));
4560}
4561
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004562/// Handles semantic checking for features that are common to all attributes,
4563/// such as checking whether a parameter was properly specified, or the correct
4564/// number of arguments were passed, etc.
4565static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4566 const AttributeList &Attr) {
4567 // Several attributes carry different semantics than the parsing requires, so
4568 // those are opted out of the common handling.
4569 //
4570 // We also bail on unknown and ignored attributes because those are handled
4571 // as part of the target-specific handling logic.
4572 if (Attr.hasCustomParsing() ||
4573 Attr.getKind() == AttributeList::UnknownAttribute ||
4574 Attr.getKind() == AttributeList::IgnoredAttribute)
4575 return false;
4576
4577 // If there are no optional arguments, then checking for the argument count
4578 // is trivial.
4579 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
4580 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4581 return true;
4582 return false;
4583}
4584
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004585//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004586// Top Level Sema Entry Points
4587//===----------------------------------------------------------------------===//
4588
Richard Smithf8a75c32013-08-29 00:47:48 +00004589/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4590/// the attribute applies to decls. If the attribute is a type attribute, just
4591/// silently ignore it if a GNU attribute.
4592static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4593 const AttributeList &Attr,
4594 bool IncludeCXX11Attributes) {
4595 if (Attr.isInvalid())
4596 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004597
Richard Smithf8a75c32013-08-29 00:47:48 +00004598 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4599 // instead.
4600 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4601 return;
4602
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004603 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4604 return;
4605
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004606 switch (Attr.getKind()) {
Richard Smith10876ef2013-01-17 01:30:42 +00004607 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4608 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4609 case AttributeList::AT_IBOutletCollection:
4610 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004611 case AttributeList::AT_AddressSpace:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004612 case AttributeList::AT_ObjCGC:
4613 case AttributeList::AT_VectorSize:
4614 case AttributeList::AT_NeonVectorType:
4615 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004616 case AttributeList::AT_Ptr32:
4617 case AttributeList::AT_Ptr64:
4618 case AttributeList::AT_SPtr:
4619 case AttributeList::AT_UPtr:
Mike Stumpd3bb5572009-07-24 19:02:52 +00004620 // Ignore these, these are type attributes, handled by
4621 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004622 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004623 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4624 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4625 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4626 case AttributeList::AT_AlwaysInline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004627 handleAlwaysInlineAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004628 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004629 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00004630 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004631 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4632 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4633 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004634 handleDependencyAttr(S, scope, D, Attr);
4635 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004636 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4637 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4638 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004639 case AttributeList::AT_CXX11NoReturn:
4640 handleCXX11NoReturnAttr(S, D, Attr);
4641 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004642 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004643 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004644 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004645 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4646 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004647 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004648 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004649 case AttributeList::AT_MinSize:
4650 handleMinSizeAttr(S, D, Attr);
4651 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004652 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4653 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4654 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman9a226212013-08-28 23:13:26 +00004655 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4656 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004657 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4658 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004659 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004660 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004661 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4662 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00004663 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004664 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4665 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00004666 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00004667 case AttributeList::AT_ownership_returns:
4668 case AttributeList::AT_ownership_takes:
4669 case AttributeList::AT_ownership_holds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004670 handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004671 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4672 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4673 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4674 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4675 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4676 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4677 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004678
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004679 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004680 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004681 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004682 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004683
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004684 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004685 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4686
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004687 case AttributeList::AT_ObjCRequiresSuper:
4688 handleObjCRequiresSuperAttr(S, D, Attr); break;
4689
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004690 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00004691 handleNSBridgedAttr(S, scope, D, Attr); break;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004692
4693 case AttributeList::AT_ObjCBridge:
4694 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004695
4696 case AttributeList::AT_ObjCBridgeMutable:
4697 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00004698
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004699 case AttributeList::AT_CFAuditedTransfer:
4700 case AttributeList::AT_CFUnknownTransfer:
John McCall32f5fe12011-09-30 05:12:12 +00004701 handleCFTransferAttr(S, D, Attr); break;
4702
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004703 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004704 case AttributeList::AT_CFConsumed:
4705 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4706 case AttributeList::AT_NSConsumesSelf:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004707 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004708
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004709 case AttributeList::AT_NSReturnsAutoreleased:
4710 case AttributeList::AT_NSReturnsNotRetained:
4711 case AttributeList::AT_CFReturnsNotRetained:
4712 case AttributeList::AT_NSReturnsRetained:
4713 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004714 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004715
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004716 case AttributeList::AT_WorkGroupSizeHint:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004717 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004718 handleWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00004719
Joey Goulyaba589c2013-03-08 09:42:32 +00004720 case AttributeList::AT_VecTypeHint:
4721 handleVecTypeHint(S, D, Attr); break;
4722
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004723 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004724 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004725
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004726 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4727 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4728 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004729 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004730 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004731 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00004732 handleArcWeakrefUnavailableAttr (S, D, Attr);
4733 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004734 case AttributeList::AT_ObjCRootClass:
Patrick Beardacfbe9e2012-04-06 18:12:22 +00004735 handleObjCRootClassAttr(S, D, Attr);
4736 break;
Ted Kremenek28eace62013-11-23 01:01:34 +00004737 case AttributeList::AT_ObjCSuppressProtocol:
4738 handleObjCSuppresProtocolAttr(S, D, Attr);
4739 break;
4740 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00004741 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00004742 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004743 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4744 case AttributeList::AT_ReturnsTwice:
Rafael Espindola70107f92011-10-03 14:59:42 +00004745 handleReturnsTwiceAttr(S, D, Attr);
4746 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004747 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004748 case AttributeList::AT_Visibility:
4749 handleVisibilityAttr(S, D, Attr, false);
4750 break;
4751 case AttributeList::AT_TypeVisibility:
4752 handleVisibilityAttr(S, D, Attr, true);
4753 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004754 case AttributeList::AT_WarnUnused:
4755 handleWarnUnusedAttr(S, D, Attr);
4756 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004757 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004758 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004759 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4760 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4761 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4762 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004763 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004764 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004765 case AttributeList::AT_ObjCException:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004766 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00004767 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004768 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004769 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004770 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004771 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4772 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4773 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4774 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4775 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4776 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4777 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4778 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4779 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004780 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004781 // Just ignore
4782 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004783 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00004784 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00004785 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004786 case AttributeList::AT_StdCall:
4787 case AttributeList::AT_CDecl:
4788 case AttributeList::AT_FastCall:
4789 case AttributeList::AT_ThisCall:
4790 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004791 case AttributeList::AT_MSABI:
4792 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004793 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004794 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004795 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004796 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004797 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004798 case AttributeList::AT_OpenCLKernel:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004799 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00004800 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004801 case AttributeList::AT_OpenCLImageAccess:
4802 handleOpenCLImageAccessAttr(S, D, Attr);
4803 break;
John McCall8d32c052012-05-22 21:28:12 +00004804
4805 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004806 case AttributeList::AT_MsStruct:
John McCall8d32c052012-05-22 21:28:12 +00004807 handleMsStructAttr(S, D, Attr);
4808 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004809 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004810 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004811 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004812 case AttributeList::AT_SingleInheritance:
4813 case AttributeList::AT_MultipleInheritance:
4814 case AttributeList::AT_VirtualInheritance:
John McCall8d32c052012-05-22 21:28:12 +00004815 handleInheritanceAttr(S, D, Attr);
4816 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004817 case AttributeList::AT_Win64:
John McCall8d32c052012-05-22 21:28:12 +00004818 handlePortabilityAttr(S, D, Attr);
4819 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004820 case AttributeList::AT_ForceInline:
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004821 handleForceInlineAttr(S, D, Attr);
4822 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004823 case AttributeList::AT_SelectAny:
4824 handleSelectAnyAttr(S, D, Attr);
4825 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004826
4827 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004828 case AttributeList::AT_AssertExclusiveLock:
4829 handleAssertExclusiveLockAttr(S, D, Attr);
4830 break;
4831 case AttributeList::AT_AssertSharedLock:
4832 handleAssertSharedLockAttr(S, D, Attr);
4833 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004834 case AttributeList::AT_GuardedVar:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004835 handleGuardedVarAttr(S, D, Attr);
4836 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004837 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004838 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004839 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004840 case AttributeList::AT_ScopedLockable:
Michael Han3be3b442012-07-23 18:48:41 +00004841 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004842 break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004843 case AttributeList::AT_NoSanitizeAddress:
4844 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004845 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004846 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004847 handleNoThreadSafetyAnalysis(S, D, Attr);
4848 break;
4849 case AttributeList::AT_NoSanitizeThread:
4850 handleNoSanitizeThread(S, D, Attr);
4851 break;
4852 case AttributeList::AT_NoSanitizeMemory:
4853 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004854 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004855 case AttributeList::AT_Lockable:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004856 handleLockableAttr(S, D, Attr);
4857 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004858 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004859 handleGuardedByAttr(S, D, Attr);
4860 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004861 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004862 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004863 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004864 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004865 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004866 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004867 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004868 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004869 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004870 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004871 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004872 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004873 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004874 handleLockReturnedAttr(S, D, Attr);
4875 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004876 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004877 handleLocksExcludedAttr(S, D, Attr);
4878 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004879 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004880 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004881 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004882 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004883 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004884 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004885 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004886 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004887 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004888 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004889 handleUnlockFunAttr(S, D, Attr);
4890 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004891 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004892 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004893 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004894 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004895 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004896 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004897
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004898 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004899 case AttributeList::AT_Consumable:
4900 handleConsumableAttr(S, D, Attr);
4901 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004902 case AttributeList::AT_CallableWhen:
4903 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004904 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004905 case AttributeList::AT_ParamTypestate:
4906 handleParamTypestateAttr(S, D, Attr);
4907 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004908 case AttributeList::AT_ReturnTypestate:
4909 handleReturnTypestateAttr(S, D, Attr);
4910 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004911 case AttributeList::AT_SetTypestate:
4912 handleSetTypestateAttr(S, D, Attr);
4913 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004914 case AttributeList::AT_TestTypestate:
4915 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004916 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004917
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004918 // Type safety attributes.
4919 case AttributeList::AT_ArgumentWithTypeTag:
4920 handleArgumentWithTypeTagAttr(S, D, Attr);
4921 break;
4922 case AttributeList::AT_TypeTagForDatatype:
4923 handleTypeTagForDatatypeAttr(S, D, Attr);
4924 break;
4925
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004926 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004927 // Ask target about the attribute.
4928 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4929 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004930 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4931 diag::warn_unhandled_ms_attribute_ignored :
4932 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004933 break;
4934 }
4935}
4936
4937/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4938/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004939void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004940 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004941 bool IncludeCXX11Attributes) {
4942 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004943 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004944
4945 // GCC accepts
4946 // static int a9 __attribute__((weakref));
4947 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004948 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004949 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00004950 cast<NamedDecl>(D)->getNameAsString();
4951 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004952 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004953 }
4954}
4955
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004956// Annotation attributes are the only attributes allowed after an access
4957// specifier.
4958bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4959 const AttributeList *AttrList) {
4960 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004961 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004962 handleAnnotateAttr(*this, ASDecl, *l);
4963 } else {
4964 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4965 return true;
4966 }
4967 }
4968
4969 return false;
4970}
4971
John McCall42856de2011-10-01 05:17:03 +00004972/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4973/// contains any decl attributes that we should warn about.
4974static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4975 for ( ; A; A = A->getNext()) {
4976 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004977 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004978 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4979
4980 if (A->getKind() == AttributeList::UnknownAttribute) {
4981 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4982 << A->getName() << A->getRange();
4983 } else {
4984 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4985 << A->getName() << A->getRange();
4986 }
4987 }
4988}
4989
4990/// checkUnusedDeclAttributes - Given a declarator which is not being
4991/// used to build a declaration, complain about any decl attributes
4992/// which might be lying around on it.
4993void Sema::checkUnusedDeclAttributes(Declarator &D) {
4994 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4995 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4996 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4997 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4998}
4999
Ryan Flynn7d470f32009-07-30 03:15:39 +00005000/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00005001/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00005002NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5003 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00005004 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005005 NamedDecl *NewD = 0;
5006 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00005007 FunctionDecl *NewFD;
5008 // FIXME: Missing call to CheckFunctionDeclaration().
5009 // FIXME: Mangling?
5010 // FIXME: Is the qualifier info correct?
5011 // FIXME: Is the DeclContext correct?
5012 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5013 Loc, Loc, DeclarationName(II),
5014 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005015 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00005016 FD->hasPrototype(),
5017 false/*isConstexprSpecified*/);
5018 NewD = NewFD;
5019
5020 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00005021 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00005022
5023 // Fake up parameter variables; they are declared as if this were
5024 // a typedef.
5025 QualType FDTy = FD->getType();
5026 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5027 SmallVector<ParmVarDecl*, 16> Params;
5028 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5029 AE = FT->arg_type_end(); AI != AE; ++AI) {
5030 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5031 Param->setScopeInfo(0, Params.size());
5032 Params.push_back(Param);
5033 }
David Blaikie9c70e042011-09-21 18:16:56 +00005034 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00005035 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005036 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5037 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00005038 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00005039 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005040 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00005041 if (VD->getQualifier()) {
5042 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00005043 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00005044 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005045 }
5046 return NewD;
5047}
5048
James Dennett634962f2012-06-14 21:40:34 +00005049/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00005050/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00005051void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00005052 if (W.getUsed()) return; // only do this once
5053 W.setUsed(true);
5054 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5055 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00005056 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005057 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5058 NDId->getName()));
5059 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00005060 WeakTopLevelDecl.push_back(NewD);
5061 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5062 // to insert Decl at TU scope, sorry.
5063 DeclContext *SavedContext = CurContext;
5064 CurContext = Context.getTranslationUnitDecl();
5065 PushOnScopeChains(NewD, S);
5066 CurContext = SavedContext;
5067 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005068 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005069 }
5070}
5071
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005072void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5073 // It's valid to "forward-declare" #pragma weak, in which case we
5074 // have to do this.
5075 LoadExternalWeakUndeclaredIdentifiers();
5076 if (!WeakUndeclaredIdentifiers.empty()) {
5077 NamedDecl *ND = NULL;
5078 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5079 if (VD->isExternC())
5080 ND = VD;
5081 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5082 if (FD->isExternC())
5083 ND = FD;
5084 if (ND) {
5085 if (IdentifierInfo *Id = ND->getIdentifier()) {
5086 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5087 = WeakUndeclaredIdentifiers.find(Id);
5088 if (I != WeakUndeclaredIdentifiers.end()) {
5089 WeakInfo W = I->second;
5090 DeclApplyPragmaWeak(S, ND, W);
5091 WeakUndeclaredIdentifiers[Id] = W;
5092 }
5093 }
5094 }
5095 }
5096}
5097
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005098/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5099/// it, apply them to D. This is a bit tricky because PD can have attributes
5100/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00005101void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005102 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00005103 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00005104 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005105
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005106 // Walk the declarator structure, applying decl attributes that were in a type
5107 // position to the decl itself. This handles cases like:
5108 // int *__attr__(x)** D;
5109 // when X is a decl attribute.
5110 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5111 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00005112 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005113
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005114 // Finally, apply any attributes on the decl itself.
5115 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00005116 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005117}
John McCall28a6aea2009-11-04 02:18:39 +00005118
John McCall31168b02011-06-15 23:02:42 +00005119/// Is the given declaration allowed to use a forbidden type?
5120static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5121 // Private ivars are always okay. Unfortunately, people don't
5122 // always properly make their ivars private, even in system headers.
5123 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005124 // Function declarations in sys headers will be marked unavailable.
5125 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5126 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005127 return false;
5128
5129 // Require it to be declared in a system header.
5130 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5131}
5132
5133/// Handle a delayed forbidden-type diagnostic.
5134static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5135 Decl *decl) {
5136 if (decl && isForbiddenTypeAllowed(S, decl)) {
5137 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5138 "this system declaration uses an unsupported type"));
5139 return;
5140 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005141 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005142 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005143 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005144 // kind of forbidden type messages on unavailable functions.
5145 if (FD->hasAttr<UnavailableAttr>() &&
5146 diag.getForbiddenTypeDiagnostic() ==
5147 diag::err_arc_array_param_no_ownership) {
5148 diag.Triggered = true;
5149 return;
5150 }
5151 }
John McCall31168b02011-06-15 23:02:42 +00005152
5153 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5154 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5155 diag.Triggered = true;
5156}
5157
John McCall2ec85372012-05-07 06:16:41 +00005158void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5159 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005160 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005161 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005162
John McCall2ec85372012-05-07 06:16:41 +00005163 // When delaying diagnostics to run in the context of a parsed
5164 // declaration, we only want to actually emit anything if parsing
5165 // succeeds.
5166 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005167
John McCall2ec85372012-05-07 06:16:41 +00005168 // We emit all the active diagnostics in this pool or any of its
5169 // parents. In general, we'll get one pool for the decl spec
5170 // and a child pool for each declarator; in a decl group like:
5171 // deprecated_typedef foo, *bar, baz();
5172 // only the declarator pops will be passed decls. This is correct;
5173 // we really do need to consider delayed diagnostics from the decl spec
5174 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00005175 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00005176 do {
John McCall6347b682012-05-07 06:16:58 +00005177 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00005178 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5179 // This const_cast is a bit lame. Really, Triggered should be mutable.
5180 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00005181 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00005182 continue;
5183
John McCallc1465822011-02-14 07:13:47 +00005184 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00005185 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00005186 // Don't bother giving deprecation diagnostics if the decl is invalid.
5187 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00005188 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005189 break;
5190
5191 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00005192 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005193 break;
John McCall31168b02011-06-15 23:02:42 +00005194
5195 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00005196 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00005197 break;
John McCall86121512010-01-27 03:50:35 +00005198 }
5199 }
John McCall2ec85372012-05-07 06:16:41 +00005200 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00005201}
5202
John McCall6347b682012-05-07 06:16:58 +00005203/// Given a set of delayed diagnostics, re-emit them as if they had
5204/// been delayed in the current context instead of in the given pool.
5205/// Essentially, this just moves them to the current pool.
5206void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5207 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5208 assert(curPool && "re-emitting in undelayed context not supported");
5209 curPool->steal(pool);
5210}
5211
John McCall28a6aea2009-11-04 02:18:39 +00005212static bool isDeclDeprecated(Decl *D) {
5213 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005214 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00005215 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00005216 // A category implicitly has the availability of the interface.
5217 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5218 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00005219 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5220 return false;
5221}
5222
Eli Friedman971bfa12012-08-08 21:52:41 +00005223static void
5224DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5225 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005226 const ObjCInterfaceDecl *UnknownObjCClass,
5227 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedman971bfa12012-08-08 21:52:41 +00005228 DeclarationName Name = D->getDeclName();
5229 if (!Message.empty()) {
5230 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5231 S.Diag(D->getLocation(),
5232 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5233 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005234 if (ObjCPropery)
5235 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5236 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005237 } else if (!UnknownObjCClass) {
5238 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5239 S.Diag(D->getLocation(),
5240 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5241 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005242 if (ObjCPropery)
5243 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5244 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005245 } else {
5246 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5247 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5248 }
5249}
5250
John McCallb45a1e72010-08-26 02:13:20 +00005251void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00005252 Decl *Ctx) {
5253 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00005254 return;
5255
John McCall86121512010-01-27 03:50:35 +00005256 DD.Triggered = true;
Eli Friedman971bfa12012-08-08 21:52:41 +00005257 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5258 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005259 DD.getUnknownObjCClass(),
5260 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00005261}
5262
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005263void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00005264 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005265 const ObjCInterfaceDecl *UnknownObjCClass,
5266 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00005267 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00005268 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005269 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5270 UnknownObjCClass,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005271 ObjCProperty,
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005272 Message));
John McCall28a6aea2009-11-04 02:18:39 +00005273 return;
5274 }
5275
5276 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00005277 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00005278 return;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005279 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00005280}