blob: 15ec5857a956e673a33b2f9d2355620571a560cf [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
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000064namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000065 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000066 C,
67 Cpp,
68 ObjC
69 };
70}
71
Chris Lattner58418ff2008-06-29 00:16:31 +000072//===----------------------------------------------------------------------===//
73// Helper functions
74//===----------------------------------------------------------------------===//
75
Chandler Carruthff4c4f02011-07-01 23:49:12 +000076static const FunctionType *getFunctionType(const Decl *D,
Ted Kremenek527042b2009-08-14 20:49:40 +000077 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000078 QualType Ty;
Chandler Carruthff4c4f02011-07-01 23:49:12 +000079 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000080 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000081 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000082 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000083 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000084 Ty = decl->getUnderlyingType();
85 else
86 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000087
Chris Lattner2c6fcf52008-06-26 18:38:35 +000088 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000089 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000090 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000091 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000092
John McCall9dd450b2009-09-21 23:43:11 +000093 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000094}
95
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000096// FIXME: We should provide an abstraction around a method or function
97// to provide the following bits of information.
98
Nuno Lopes518e3702009-12-20 23:11:08 +000099/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +0000100/// type (function or function-typed variable).
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000101static bool isFunction(const Decl *D) {
102 return getFunctionType(D, false) != NULL;
Ted Kremenek527042b2009-08-14 20:49:40 +0000103}
104
105/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000106/// type (function or function-typed variable) or an Objective-C
107/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000108static bool isFunctionOrMethod(const Decl *D) {
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +0000109 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000110}
111
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000112/// isFunctionOrMethodOrBlock - Return true if the given decl has function
113/// type (function or function-typed variable) or an Objective-C
114/// method or a block.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000115static bool isFunctionOrMethodOrBlock(const Decl *D) {
116 if (isFunctionOrMethod(D))
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000117 return true;
118 // check for block is more involved.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000119 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000120 QualType Ty = V->getType();
121 return Ty->isBlockPointerType();
122 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000123 return isa<BlockDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000124}
125
John McCall3882ace2011-01-05 12:14:39 +0000126/// Return true if the given decl has a declarator that should have
127/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000128static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +0000129 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000130 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
131 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +0000132}
133
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000134/// hasFunctionProto - Return true if the given decl has a argument
135/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000136/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000137static bool hasFunctionProto(const Decl *D) {
138 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000139 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000140 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000141 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000142 return true;
143 }
144}
145
146/// getFunctionOrMethodNumArgs - Return number of function or method
147/// arguments. It is an error to call this on a K&R function (use
148/// hasFunctionProto first).
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000149static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
150 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000151 return cast<FunctionProtoType>(FnTy)->getNumArgs();
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->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000154 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000155}
156
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000157static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
158 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000159 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000160 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000161 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000162
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000163 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000164}
165
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000166static QualType getFunctionOrMethodResultType(const Decl *D) {
167 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000168 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000169 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000170}
171
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000172static bool isFunctionOrMethodVariadic(const Decl *D) {
173 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000174 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000175 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000176 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000177 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000178 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000179 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000180 }
181}
182
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000183static bool isInstanceMethod(const Decl *D) {
184 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000185 return MethodDecl->isInstance();
186 return false;
187}
188
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000189static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000190 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000191 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000192 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000193
John McCall96fa4842010-05-17 21:00:27 +0000194 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
195 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000196 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000197
John McCall96fa4842010-05-17 21:00:27 +0000198 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000199
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000200 // FIXME: Should we walk the chain of classes?
201 return ClsName == &Ctx.Idents.get("NSString") ||
202 ClsName == &Ctx.Idents.get("NSMutableString");
203}
204
Daniel Dunbar980c6692008-09-26 03:32:58 +0000205static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000206 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000207 if (!PT)
208 return false;
209
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000210 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000211 if (!RT)
212 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000213
Daniel Dunbar980c6692008-09-26 03:32:58 +0000214 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000215 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000216 return false;
217
218 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
219}
220
Richard Smithb87c4652013-10-31 21:23:20 +0000221static unsigned getNumAttributeArgs(const AttributeList &Attr) {
222 // FIXME: Include the type in the argument list.
223 return Attr.getNumArgs() + Attr.hasParsedType();
224}
225
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000226/// \brief Check if the attribute has exactly as many args as Num. May
227/// output an error.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000228static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000229 unsigned Num) {
230 if (getNumAttributeArgs(Attr) != Num) {
Aaron Ballmanb7243382013-07-23 19:30:11 +0000231 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
232 << Attr.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000233 return false;
234 }
235
236 return true;
237}
238
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000239
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000240/// \brief Check if the attribute has at least as many args as Num. May
241/// output an error.
242static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000243 unsigned Num) {
244 if (getNumAttributeArgs(Attr) < Num) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000245 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
246 return false;
247 }
248
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000249 return true;
250}
251
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000252/// \brief If Expr is a valid integer constant, get the value of the integer
253/// expression and return success or failure. May output an error.
254static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
255 const Expr *Expr, uint32_t &Val,
256 unsigned Idx = UINT_MAX) {
257 llvm::APSInt I(32);
258 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
259 !Expr->isIntegerConstantExpr(I, S.Context)) {
260 if (Idx != UINT_MAX)
261 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
262 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
263 << Expr->getSourceRange();
264 else
265 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
266 << Attr.getName() << AANT_ArgumentIntegerConstant
267 << Expr->getSourceRange();
268 return false;
269 }
270 Val = (uint32_t)I.getZExtValue();
271 return true;
272}
273
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000274/// \brief Check if IdxExpr is a valid argument index for a function or
275/// instance method D. May output an error.
276///
277/// \returns true if IdxExpr is a valid index.
278static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
279 StringRef AttrName,
280 SourceLocation AttrLoc,
281 unsigned AttrArgNum,
282 const Expr *IdxExpr,
283 uint64_t &Idx)
284{
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000285 assert(isFunctionOrMethod(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000286
287 // In C++ the implicit 'this' function parameter also counts.
288 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000289 bool HP = hasFunctionProto(D);
290 bool HasImplicitThisParam = isInstanceMethod(D);
291 bool IV = HP && isFunctionOrMethodVariadic(D);
292 unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) +
293 HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000294
295 llvm::APSInt IdxInt;
296 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
297 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +0000298 std::string Name = std::string("'") + AttrName.str() + std::string("'");
299 S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str()
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000300 << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000301 return false;
302 }
303
304 Idx = IdxInt.getLimitedValue();
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000305 if (Idx < 1 || (!IV && Idx > NumArgs)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000306 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
307 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
308 return false;
309 }
310 Idx--; // Convert to zero-based.
311 if (HasImplicitThisParam) {
312 if (Idx == 0) {
313 S.Diag(AttrLoc,
314 diag::err_attribute_invalid_implicit_this_argument)
315 << AttrName << IdxExpr->getSourceRange();
316 return false;
317 }
318 --Idx;
319 }
320
321 return true;
322}
323
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000324/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
325/// If not emit an error and return false. If the argument is an identifier it
326/// will emit an error with a fixit hint and treat it as if it was a string
327/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000328bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
329 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000330 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000331 // Look for identifiers. If we have one emit a hint to fix it to a literal.
332 if (Attr.isArgIdent(ArgNum)) {
333 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000334 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000335 << Attr.getName() << AANT_ArgumentString
336 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000337 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000338 Str = Loc->Ident->getName();
339 if (ArgLocation)
340 *ArgLocation = Loc->Loc;
341 return true;
342 }
343
344 // Now check for an actual string literal.
345 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
346 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
347 if (ArgLocation)
348 *ArgLocation = ArgExpr->getLocStart();
349
350 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000351 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000352 << Attr.getName() << AANT_ArgumentString;
353 return false;
354 }
355
356 Str = Literal->getString();
357 return true;
358}
359
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000360///
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000361/// \brief Check if passed in Decl is a field or potentially shared global var
362/// \return true if the Decl is a field or potentially shared global variable
363///
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000364static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000365 if (isa<FieldDecl>(D))
366 return true;
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000367 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Richard Smithfd3834f2013-04-13 02:43:54 +0000368 return vd->hasGlobalStorage() && !vd->getTLSKind();
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000369
370 return false;
371}
372
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000373/// \brief Check if the passed-in expression is of type int or bool.
374static bool isIntOrBool(Expr *Exp) {
375 QualType QT = Exp->getType();
376 return QT->isBooleanType() || QT->isIntegerType();
377}
378
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000379
380// Check to see if the type is a smart pointer of some kind. We assume
381// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000382static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
383 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
384 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000385 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000386 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000387
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000388 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
389 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000390 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000391 return false;
392
393 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000394}
395
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000396/// \brief Check if passed in Decl is a pointer type.
397/// Note that this function may produce an error message.
398/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000399static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
400 const AttributeList &Attr) {
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000401 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000402 QualType QT = vd->getType();
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000403 if (QT->isAnyPointerType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000404 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000405
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000406 if (const RecordType *RT = QT->getAs<RecordType>()) {
407 // If it's an incomplete type, it could be a smart pointer; skip it.
408 // (We don't want to force template instantiation if we can avoid it,
409 // since that would alter the order in which templates are instantiated.)
410 if (RT->isIncompleteType())
411 return true;
412
413 if (threadSafetyCheckIsSmartPointer(S, RT))
414 return true;
415 }
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000416
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000417 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000418 << Attr.getName()->getName() << QT;
419 } else {
420 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
421 << Attr.getName();
422 }
423 return false;
424}
425
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000426/// \brief Checks that the passed in QualType either is of RecordType or points
427/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000428static const RecordType *getRecordType(QualType QT) {
429 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000430 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000431
432 // Now check if we point to record type.
433 if (const PointerType *PT = QT->getAs<PointerType>())
434 return PT->getPointeeType()->getAs<RecordType>();
435
436 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000437}
438
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000439
Jordy Rose740b0c22012-05-08 03:27:22 +0000440static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
441 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000442 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
443 if (RT->getDecl()->getAttr<LockableAttr>())
444 return true;
445 return false;
446}
447
448
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000449/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000450/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000451static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
452 QualType Ty) {
453 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000454
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000455 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000456 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000457 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000458 << Attr.getName() << Ty.getAsString();
459 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000460 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000461
Michael Hana9171bc2012-08-03 17:40:43 +0000462 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000463 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000464 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000465
466 // Allow smart pointers to be used as lockable objects.
467 // FIXME -- Check the type that the smart pointer points to.
468 if (threadSafetyCheckIsSmartPointer(S, RT))
469 return;
470
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000471 // Check if the type is lockable.
472 RecordDecl *RD = RT->getDecl();
473 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000474 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000475
476 // Else check if any base classes are lockable.
477 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
478 CXXBasePaths BPaths(false, false);
479 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
480 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000481 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000482
483 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
484 << Attr.getName() << Ty.getAsString();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000485}
486
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000487/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000488/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000489/// \param Sidx The attribute argument index to start checking with.
490/// \param ParamIdxOk Whether an argument can be indexing into a function
491/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000492static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000493 const AttributeList &Attr,
494 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000495 int Sidx = 0,
496 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000497 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000498 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000499
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000500 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000501 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000502 Args.push_back(ArgExp);
503 continue;
504 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000505
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000506 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000507 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000508 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000509 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000510 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000511 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000512 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000513 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000514
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000515 // We allow constant strings to be used as a placeholder for expressions
516 // that are not valid C++ syntax, but warn that they are ignored.
517 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
518 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000519 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000520 continue;
521 }
522
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000523 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000524
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000525 // A pointer to member expression of the form &MyClass::mu is treated
526 // specially -- we need to look at the type of the member.
527 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
528 if (UOp->getOpcode() == UO_AddrOf)
529 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
530 if (DRE->getDecl()->isCXXInstanceMember())
531 ArgTy = DRE->getDecl()->getType();
532
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000533 // First see if we can just cast to record type, or point to record type.
534 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000535
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000536 // Now check if we index into a record type function param.
537 if(!RT && ParamIdxOk) {
538 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000539 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
540 if(FD && IL) {
541 unsigned int NumParams = FD->getNumParams();
542 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000543 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
544 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
545 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000546 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
547 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000548 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000549 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000550 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000551 }
552 }
553
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000554 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000555
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000556 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000557 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000558}
559
Chris Lattner58418ff2008-06-29 00:16:31 +0000560//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000561// Attribute Implementations
562//===----------------------------------------------------------------------===//
563
Daniel Dunbar032db472008-07-31 22:40:48 +0000564// FIXME: All this manual attribute parsing code is gross. At the
565// least add some helper functions to check most argument patterns (#
566// and types of args).
567
Michael Hana9171bc2012-08-03 17:40:43 +0000568static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000569 const AttributeList &Attr) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000570 // D must be either a member field or global (potentially shared) variable.
571 if (!mayBeSharedVariable(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000572 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
573 << Attr.getName() << ExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000574 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000575 }
576
Michael Han3be3b442012-07-23 18:48:41 +0000577 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000578}
579
Michael Han3be3b442012-07-23 18:48:41 +0000580static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
581 if (!checkGuardedVarAttrCommon(S, D, Attr))
582 return;
Michael Hana9171bc2012-08-03 17:40:43 +0000583
Michael Han99315932013-01-24 16:46:58 +0000584 D->addAttr(::new (S.Context)
585 GuardedVarAttr(Attr.getRange(), S.Context,
586 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000587}
588
Michael Hana9171bc2012-08-03 17:40:43 +0000589static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000590 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000591 if (!checkGuardedVarAttrCommon(S, D, Attr))
592 return;
593
594 if (!threadSafetyCheckIsPointer(S, D, Attr))
595 return;
596
Michael Han99315932013-01-24 16:46:58 +0000597 D->addAttr(::new (S.Context)
598 PtGuardedVarAttr(Attr.getRange(), S.Context,
599 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000600}
601
Michael Hana9171bc2012-08-03 17:40:43 +0000602static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
603 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000604 Expr* &Arg) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000605 // D must be either a member field or global (potentially shared) variable.
606 if (!mayBeSharedVariable(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000607 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
608 << Attr.getName() << ExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000609 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000610 }
611
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000612 SmallVector<Expr*, 1> Args;
613 // check that all arguments are lockable objects
614 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
615 unsigned Size = Args.size();
616 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000617 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000618
Michael Han3be3b442012-07-23 18:48:41 +0000619 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000620
Michael Han3be3b442012-07-23 18:48:41 +0000621 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000622}
623
Michael Han3be3b442012-07-23 18:48:41 +0000624static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
625 Expr *Arg = 0;
626 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
627 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000628
Michael Han3be3b442012-07-23 18:48:41 +0000629 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
630}
631
Michael Hana9171bc2012-08-03 17:40:43 +0000632static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000633 const AttributeList &Attr) {
634 Expr *Arg = 0;
635 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
636 return;
637
638 if (!threadSafetyCheckIsPointer(S, D, Attr))
639 return;
640
641 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
642 S.Context, Arg));
643}
644
Michael Hana9171bc2012-08-03 17:40:43 +0000645static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000646 const AttributeList &Attr) {
Caitlin Sadowski086fb952011-09-16 00:35:54 +0000647 // FIXME: Lockable structs for C code.
David Blaikie021221d2013-07-29 18:24:03 +0000648 if (!isa<RecordDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000649 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
650 << Attr.getName() << ExpectedStructOrUnionOrClass;
Michael Han3be3b442012-07-23 18:48:41 +0000651 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000652 }
653
Michael Han3be3b442012-07-23 18:48:41 +0000654 return true;
655}
656
657static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
658 if (!checkLockableAttrCommon(S, D, Attr))
659 return;
660
661 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
662}
663
Michael Hana9171bc2012-08-03 17:40:43 +0000664static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000665 const AttributeList &Attr) {
666 if (!checkLockableAttrCommon(S, D, Attr))
667 return;
668
Michael Han99315932013-01-24 16:46:58 +0000669 D->addAttr(::new (S.Context)
670 ScopedLockableAttr(Attr.getRange(), S.Context,
671 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000672}
673
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000674static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D,
675 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000676 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000677 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
678 << Attr.getName() << ExpectedFunctionOrMethod;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000679 return;
680 }
681
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000682 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000683 S.Context));
684}
685
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000686static void handleNoSanitizeAddressAttr(Sema &S, Decl *D,
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000687 const AttributeList &Attr) {
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000688 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000689 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000690 << Attr.getName() << ExpectedFunctionOrMethod;
691 return;
692 }
693
Michael Han99315932013-01-24 16:46:58 +0000694 D->addAttr(::new (S.Context)
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000695 NoSanitizeAddressAttr(Attr.getRange(), S.Context,
696 Attr.getAttributeSpellingListIndex()));
697}
698
699static void handleNoSanitizeMemory(Sema &S, Decl *D,
700 const AttributeList &Attr) {
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000701 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
702 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
703 << Attr.getName() << ExpectedFunctionOrMethod;
704 return;
705 }
706
707 D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(),
708 S.Context));
709}
710
711static void handleNoSanitizeThread(Sema &S, Decl *D,
712 const AttributeList &Attr) {
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000713 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
714 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
715 << Attr.getName() << ExpectedFunctionOrMethod;
716 return;
717 }
718
719 D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(),
720 S.Context));
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000721}
722
Michael Hana9171bc2012-08-03 17:40:43 +0000723static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
724 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000725 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000726 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000727 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000728
729 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000730 ValueDecl *VD = dyn_cast<ValueDecl>(D);
731 if (!VD || !mayBeSharedVariable(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000732 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
733 << Attr.getName() << ExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000734 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000735 }
736
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000737 // Check that this attribute only applies to lockable types.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000738 QualType QT = VD->getType();
739 if (!QT->isDependentType()) {
740 const RecordType *RT = getRecordType(QT);
741 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000742 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000743 << Attr.getName();
744 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000745 }
746 }
747
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000748 // Check that all arguments are lockable objects.
749 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000750 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000751 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000752
Michael Han3be3b442012-07-23 18:48:41 +0000753 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000754}
755
Michael Hana9171bc2012-08-03 17:40:43 +0000756static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000757 const AttributeList &Attr) {
758 SmallVector<Expr*, 1> Args;
759 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
760 return;
761
762 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000763 D->addAttr(::new (S.Context)
764 AcquiredAfterAttr(Attr.getRange(), S.Context,
765 StartArg, Args.size(),
766 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000767}
768
Michael Hana9171bc2012-08-03 17:40:43 +0000769static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000770 const AttributeList &Attr) {
771 SmallVector<Expr*, 1> Args;
772 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
773 return;
774
775 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000776 D->addAttr(::new (S.Context)
777 AcquiredBeforeAttr(Attr.getRange(), S.Context,
778 StartArg, Args.size(),
779 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000780}
781
Michael Hana9171bc2012-08-03 17:40:43 +0000782static bool checkLockFunAttrCommon(Sema &S, Decl *D,
783 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000784 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000785 // zero or more arguments ok
786
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000787 // check that the attribute is applied to a function
788 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000789 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
790 << Attr.getName() << ExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000791 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000792 }
793
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000794 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000795 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000796
Michael Han3be3b442012-07-23 18:48:41 +0000797 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000798}
799
Michael Hana9171bc2012-08-03 17:40:43 +0000800static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000801 const AttributeList &Attr) {
802 SmallVector<Expr*, 1> Args;
803 if (!checkLockFunAttrCommon(S, D, Attr, Args))
804 return;
805
806 unsigned Size = Args.size();
807 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000808 D->addAttr(::new (S.Context)
809 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
810 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000811}
812
Michael Hana9171bc2012-08-03 17:40:43 +0000813static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000814 const AttributeList &Attr) {
815 SmallVector<Expr*, 1> Args;
816 if (!checkLockFunAttrCommon(S, D, Attr, Args))
817 return;
818
819 unsigned Size = Args.size();
820 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000821 D->addAttr(::new (S.Context)
822 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
823 StartArg, Size,
824 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000825}
826
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000827static void handleAssertSharedLockAttr(Sema &S, Decl *D,
828 const AttributeList &Attr) {
829 SmallVector<Expr*, 1> Args;
830 if (!checkLockFunAttrCommon(S, D, Attr, Args))
831 return;
832
833 unsigned Size = Args.size();
834 Expr **StartArg = Size == 0 ? 0 : &Args[0];
835 D->addAttr(::new (S.Context)
836 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
837 Attr.getAttributeSpellingListIndex()));
838}
839
840static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
841 const AttributeList &Attr) {
842 SmallVector<Expr*, 1> Args;
843 if (!checkLockFunAttrCommon(S, D, Attr, Args))
844 return;
845
846 unsigned Size = Args.size();
847 Expr **StartArg = Size == 0 ? 0 : &Args[0];
848 D->addAttr(::new (S.Context)
849 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
850 StartArg, Size,
851 Attr.getAttributeSpellingListIndex()));
852}
853
854
Michael Hana9171bc2012-08-03 17:40:43 +0000855static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
856 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000857 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000858 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000859 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000860
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000861 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000862 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
863 << Attr.getName() << ExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000864 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000865 }
866
Aaron Ballman00e99962013-08-31 01:11:41 +0000867 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000868 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000869 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000870 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000871 }
872
873 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000874 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000875
Michael Han3be3b442012-07-23 18:48:41 +0000876 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000877}
878
Michael Hana9171bc2012-08-03 17:40:43 +0000879static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000880 const AttributeList &Attr) {
881 SmallVector<Expr*, 2> Args;
882 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
883 return;
884
Michael Han99315932013-01-24 16:46:58 +0000885 D->addAttr(::new (S.Context)
886 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000887 Attr.getArgAsExpr(0),
888 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000889 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000890}
891
Michael Hana9171bc2012-08-03 17:40:43 +0000892static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000893 const AttributeList &Attr) {
894 SmallVector<Expr*, 2> Args;
895 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
896 return;
897
Michael Han99315932013-01-24 16:46:58 +0000898 D->addAttr(::new (S.Context)
899 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000900 Attr.getArgAsExpr(0),
901 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000902 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000903}
904
Michael Hana9171bc2012-08-03 17:40:43 +0000905static bool checkLocksRequiredCommon(Sema &S, Decl *D,
906 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000907 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000908 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000909 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000910
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000911 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000912 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
913 << Attr.getName() << ExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000914 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000915 }
916
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000917 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000918 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000919 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000920 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000921
Michael Han3be3b442012-07-23 18:48:41 +0000922 return true;
923}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000924
Michael Hana9171bc2012-08-03 17:40:43 +0000925static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000926 const AttributeList &Attr) {
927 SmallVector<Expr*, 1> Args;
928 if (!checkLocksRequiredCommon(S, D, Attr, Args))
929 return;
930
931 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000932 D->addAttr(::new (S.Context)
933 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
934 StartArg, Args.size(),
935 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000936}
937
Michael Hana9171bc2012-08-03 17:40:43 +0000938static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000939 const AttributeList &Attr) {
940 SmallVector<Expr*, 1> Args;
941 if (!checkLocksRequiredCommon(S, D, Attr, Args))
942 return;
943
944 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000945 D->addAttr(::new (S.Context)
946 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
947 StartArg, Args.size(),
948 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000949}
950
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000951static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000952 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000953 // zero or more arguments ok
954
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000955 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000956 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
957 << Attr.getName() << ExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000958 return;
959 }
960
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000961 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000962 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000963 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000964 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000965 Expr **StartArg = Size == 0 ? 0 : &Args[0];
966
Michael Han99315932013-01-24 16:46:58 +0000967 D->addAttr(::new (S.Context)
968 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
969 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000970}
971
972static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000973 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000974 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000975 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
976 << Attr.getName() << ExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000977 return;
978 }
979
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000980 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000981 SmallVector<Expr*, 1> Args;
982 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
983 unsigned Size = Args.size();
984 if (Size == 0)
985 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000986
Michael Han99315932013-01-24 16:46:58 +0000987 D->addAttr(::new (S.Context)
988 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
989 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000990}
991
992static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000993 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000994 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000995 return;
996
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000997 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000998 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
999 << Attr.getName() << ExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00001000 return;
1001 }
1002
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001003 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +00001004 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +00001005 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +00001006 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +00001007 if (Size == 0)
1008 return;
1009 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +00001010
Michael Han99315932013-01-24 16:46:58 +00001011 D->addAttr(::new (S.Context)
1012 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
1013 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00001014}
1015
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001016static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +00001017 ConsumableAttr::ConsumedState DefaultState;
1018
1019 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +00001020 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1021 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1022 DefaultState)) {
1023 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
1024 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +00001025 return;
1026 }
David Blaikie16f76d22013-09-06 01:28:43 +00001027 } else {
1028 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1029 << Attr.getName() << AANT_ArgumentIdentifier;
1030 return;
1031 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001032
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001033 if (!isa<CXXRecordDecl>(D)) {
1034 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1035 Attr.getName() << ExpectedClass;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001036 return;
1037 }
1038
1039 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +00001040 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001041 Attr.getAttributeSpellingListIndex()));
1042}
1043
1044static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
1045 const AttributeList &Attr) {
1046 ASTContext &CurrContext = S.getASTContext();
1047 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
1048
1049 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1050 if (!RD->hasAttr<ConsumableAttr>()) {
1051 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
1052 RD->getNameAsString();
1053
1054 return false;
1055 }
1056 }
1057
1058 return true;
1059}
1060
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001061
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001062static void handleCallableWhenAttr(Sema &S, Decl *D,
1063 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001064 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1065 return;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001066
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001067 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001068 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1069 Attr.getName() << ExpectedMethod;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001070 return;
1071 }
1072
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001073 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1074 return;
1075
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001076 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
1077 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
1078 CallableWhenAttr::ConsumedState CallableState;
1079
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001080 StringRef StateString;
1081 SourceLocation Loc;
1082 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
1083 return;
1084
1085 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +00001086 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001087 S.Diag(Loc, diag::warn_attribute_type_not_supported)
1088 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001089 return;
1090 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001091
1092 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001093 }
1094
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001095 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001096 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
1097 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001098}
1099
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001100
DeLesley Hutchins69391772013-10-17 23:23:53 +00001101static void handleParamTypestateAttr(Sema &S, Decl *D,
1102 const AttributeList &Attr) {
1103 if (!checkAttributeNumArgs(S, Attr, 1)) return;
1104
1105 if (!isa<ParmVarDecl>(D)) {
1106 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1107 Attr.getName() << ExpectedParameter;
1108 return;
1109 }
1110
1111 ParamTypestateAttr::ConsumedState ParamState;
1112
1113 if (Attr.isArgIdent(0)) {
1114 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1115 StringRef StateString = Ident->Ident->getName();
1116
1117 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
1118 ParamState)) {
1119 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1120 << Attr.getName() << StateString;
1121 return;
1122 }
1123 } else {
1124 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1125 Attr.getName() << AANT_ArgumentIdentifier;
1126 return;
1127 }
1128
1129 // FIXME: This check is currently being done in the analysis. It can be
1130 // enabled here only after the parser propagates attributes at
1131 // template specialization definition, not declaration.
1132 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
1133 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1134 //
1135 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1136 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1137 // ReturnType.getAsString();
1138 // return;
1139 //}
1140
1141 D->addAttr(::new (S.Context)
1142 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
1143 Attr.getAttributeSpellingListIndex()));
1144}
1145
1146
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001147static void handleReturnTypestateAttr(Sema &S, Decl *D,
1148 const AttributeList &Attr) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001149 if (!checkAttributeNumArgs(S, Attr, 1)) return;
1150
1151 if (!(isa<FunctionDecl>(D) || isa<ParmVarDecl>(D))) {
1152 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1153 Attr.getName() << ExpectedFunctionMethodOrParameter;
1154 return;
1155 }
1156
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001157 ReturnTypestateAttr::ConsumedState ReturnState;
1158
1159 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +00001160 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1161 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1162 ReturnState)) {
1163 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
1164 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001165 return;
1166 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001167 } else {
1168 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1169 Attr.getName() << AANT_ArgumentIdentifier;
1170 return;
1171 }
1172
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001173 // FIXME: This check is currently being done in the analysis. It can be
1174 // enabled here only after the parser propagates attributes at
1175 // template specialization definition, not declaration.
1176 //QualType ReturnType;
1177 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001178 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1179 // ReturnType = Param->getType();
1180 //
1181 //} else if (const CXXConstructorDecl *Constructor =
1182 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001183 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1184 //
1185 //} else {
1186 //
1187 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1188 //}
1189 //
1190 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1191 //
1192 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1193 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1194 // ReturnType.getAsString();
1195 // return;
1196 //}
1197
1198 D->addAttr(::new (S.Context)
1199 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1200 Attr.getAttributeSpellingListIndex()));
1201}
1202
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001203
1204static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001205 if (!checkAttributeNumArgs(S, Attr, 1))
1206 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001207
1208 if (!isa<CXXMethodDecl>(D)) {
1209 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1210 Attr.getName() << ExpectedMethod;
1211 return;
1212 }
1213
1214 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1215 return;
1216
1217 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001218 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001219 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1220 StringRef Param = Ident->Ident->getName();
1221 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1222 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1223 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001224 return;
1225 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001226 } else {
1227 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1228 Attr.getName() << AANT_ArgumentIdentifier;
1229 return;
1230 }
1231
1232 D->addAttr(::new (S.Context)
1233 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1234 Attr.getAttributeSpellingListIndex()));
1235}
1236
Chris Wailes9385f9f2013-10-29 20:28:41 +00001237static void handleTestTypestateAttr(Sema &S, Decl *D,
1238 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001239 if (!checkAttributeNumArgs(S, Attr, 1))
1240 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001241
1242 if (!isa<CXXMethodDecl>(D)) {
1243 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1244 Attr.getName() << ExpectedMethod;
1245 return;
1246 }
1247
1248 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1249 return;
1250
Chris Wailes9385f9f2013-10-29 20:28:41 +00001251 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001252 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001253 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1254 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001255 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001256 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1257 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001258 return;
1259 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001260 } else {
1261 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1262 Attr.getName() << AANT_ArgumentIdentifier;
1263 return;
1264 }
1265
1266 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001267 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001268 Attr.getAttributeSpellingListIndex()));
1269}
1270
Chandler Carruthedc2c642011-07-02 00:01:44 +00001271static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1272 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001273 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1274 if (TD == 0) {
1275 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1276 // and type-ids.
Aaron Ballmandbb634f2013-11-20 01:35:23 +00001277 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) <<
1278 Attr.getName() << ExpectedType;
Chris Lattner4a927cb2008-06-28 23:36:30 +00001279 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001280 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001281
Richard Smith1f5a4322013-01-13 02:11:23 +00001282 // Remember this typedef decl, we will need it later for diagnostics.
1283 S.ExtVectorDecls.push_back(TD);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001284}
1285
Chandler Carruthedc2c642011-07-02 00:01:44 +00001286static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001287 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001288 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001289 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001290 // If the alignment is less than or equal to 8 bits, the packed attribute
1291 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001292 if (!FD->getType()->isDependentType() &&
1293 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001294 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001295 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001296 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001297 else
Michael Han99315932013-01-24 16:46:58 +00001298 FD->addAttr(::new (S.Context)
1299 PackedAttr(Attr.getRange(), S.Context,
1300 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001301 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001302 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001303}
1304
Chandler Carruthedc2c642011-07-02 00:01:44 +00001305static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman9ee2d0472012-10-12 23:29:20 +00001306 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001307 RD->addAttr(::new (S.Context)
1308 MsStructAttr(Attr.getRange(), S.Context,
1309 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +00001310 else
Aaron Ballmanb80f94b2013-11-20 22:22:04 +00001311 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1312 << Attr.getName() << ExpectedStructOrUnionOrClass;
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +00001313}
1314
Chandler Carruthedc2c642011-07-02 00:01:44 +00001315static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek1f672822010-02-18 03:08:58 +00001316 // The IBAction attributes only apply to instance methods.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001317 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek1f672822010-02-18 03:08:58 +00001318 if (MD->isInstanceMethod()) {
Michael Han99315932013-01-24 16:46:58 +00001319 D->addAttr(::new (S.Context)
1320 IBActionAttr(Attr.getRange(), S.Context,
1321 Attr.getAttributeSpellingListIndex()));
Ted Kremenek1f672822010-02-18 03:08:58 +00001322 return;
1323 }
1324
Ted Kremenekd68ec812011-02-04 06:54:16 +00001325 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek1f672822010-02-18 03:08:58 +00001326}
1327
Ted Kremenek7fd17232011-09-29 07:02:25 +00001328static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1329 // The IBOutlet/IBOutletCollection attributes only apply to instance
1330 // variables or properties of Objective-C classes. The outlet must also
1331 // have an object reference type.
1332 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1333 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001334 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001335 << Attr.getName() << VD->getType() << 0;
1336 return false;
1337 }
1338 }
1339 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1340 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001341 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001342 << Attr.getName() << PD->getType() << 1;
1343 return false;
1344 }
1345 }
1346 else {
1347 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1348 return false;
1349 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001350
Ted Kremenek7fd17232011-09-29 07:02:25 +00001351 return true;
1352}
1353
Chandler Carruthedc2c642011-07-02 00:01:44 +00001354static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001355 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001356 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001357
Michael Han99315932013-01-24 16:46:58 +00001358 D->addAttr(::new (S.Context)
1359 IBOutletAttr(Attr.getRange(), S.Context,
1360 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001361}
1362
Chandler Carruthedc2c642011-07-02 00:01:44 +00001363static void handleIBOutletCollection(Sema &S, Decl *D,
1364 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001365
1366 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001367 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001368 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1369 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001370 return;
1371 }
1372
Ted Kremenek7fd17232011-09-29 07:02:25 +00001373 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001374 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001375
Richard Smithb1f9a282013-10-31 01:56:18 +00001376 ParsedType PT;
1377
1378 if (Attr.hasParsedType())
1379 PT = Attr.getTypeArg();
1380 else {
1381 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1382 S.getScopeForContext(D->getDeclContext()->getParent()));
1383 if (!PT) {
1384 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1385 return;
1386 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001387 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001388
Richard Smithb87c4652013-10-31 21:23:20 +00001389 TypeSourceInfo *QTLoc = 0;
1390 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1391 if (!QTLoc)
1392 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001393
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001394 // Diagnose use of non-object type in iboutletcollection attribute.
1395 // FIXME. Gnu attribute extension ignores use of builtin types in
1396 // attributes. So, __attribute__((iboutletcollection(char))) will be
1397 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001398 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001399 S.Diag(Attr.getLoc(),
1400 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1401 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001402 return;
1403 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001404
Michael Han99315932013-01-24 16:46:58 +00001405 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001406 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001407 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001408}
1409
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001410static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001411 if (const RecordType *UT = T->getAsUnionType())
1412 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1413 RecordDecl *UD = UT->getDecl();
1414 for (RecordDecl::field_iterator it = UD->field_begin(),
1415 itend = UD->field_end(); it != itend; ++it) {
1416 QualType QT = it->getType();
1417 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1418 T = QT;
1419 return;
1420 }
1421 }
1422 }
1423}
1424
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001425static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopese881ce22012-06-18 16:39:04 +00001426 if (!isFunctionOrMethod(D)) {
1427 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001428 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopese881ce22012-06-18 16:39:04 +00001429 return;
1430 }
1431
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001432 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1433 return;
1434
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001435 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001436 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001437 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001438 uint64_t Idx;
1439 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1440 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001441 return;
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001442
1443 // check if the function argument is of an integer type
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001444 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001445 if (!T->isIntegerType()) {
Aaron Ballman9d695092013-07-30 14:10:17 +00001446 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1447 << Attr.getName() << AANT_ArgumentIntegerConstant
1448 << Ex->getSourceRange();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001449 return;
1450 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001451 SizeArgs.push_back(Idx);
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001452 }
1453
1454 // check if the function returns a pointer
1455 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1456 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001457 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001458 }
1459
Michael Han99315932013-01-24 16:46:58 +00001460 D->addAttr(::new (S.Context)
1461 AllocSizeAttr(Attr.getRange(), S.Context,
1462 SizeArgs.data(), SizeArgs.size(),
1463 Attr.getAttributeSpellingListIndex()));
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001464}
1465
Chandler Carruthedc2c642011-07-02 00:01:44 +00001466static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001467 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1468 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001469 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001470 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001471 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001472 return;
1473 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001474
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001475 SmallVector<unsigned, 8> NonNullArgs;
1476 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001477 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001478 uint64_t Idx;
1479 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1480 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001481 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001482
1483 // Is the function argument a pointer type?
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001484 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001485 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001486
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001487 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001488 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +00001489 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +00001490 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001491 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001492 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001493
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001494 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001495 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001496
1497 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1498 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001499 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001500 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1501 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001502 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001503 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001504 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001505 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001506
Ted Kremenek22813f42010-10-21 18:49:36 +00001507 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001508 if (NonNullArgs.empty()) {
1509 // Warn the trivial case only if attribute is not coming from a
1510 // macro instantiation.
1511 if (Attr.getLoc().isFileID())
1512 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001513 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001514 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001515 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001516
Nick Lewyckye1121512013-01-24 01:12:16 +00001517 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001518 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001519 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001520 D->addAttr(::new (S.Context)
1521 NonNullAttr(Attr.getRange(), S.Context, start, size,
1522 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001523}
1524
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001525static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1526 switch (K) {
1527 case OwnershipAttr::Holds: return "'ownership_holds'";
1528 case OwnershipAttr::Takes: return "'ownership_takes'";
1529 case OwnershipAttr::Returns: return "'ownership_returns'";
1530 }
1531 llvm_unreachable("unknown ownership");
1532}
1533
Chandler Carruthedc2c642011-07-02 00:01:44 +00001534static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001535 // This attribute must be applied to a function declaration. The first
1536 // argument to the attribute must be an identifier, the name of the resource,
1537 // for example: malloc. The following arguments must be argument indexes, the
1538 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001539 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001540 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001541 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001542
Aaron Ballman00e99962013-08-31 01:11:41 +00001543 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001544 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001545 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001546 return;
1547 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001548
Ted Kremenekd21139a2010-07-31 01:52:11 +00001549 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001550 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001551 switch (AL.getKind()) {
1552 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001553 K = OwnershipAttr::Takes;
Aaron Ballman00e99962013-08-31 01:11:41 +00001554 if (AL.getNumArgs() < 2) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001555 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001556 return;
1557 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001558 break;
1559 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001560 K = OwnershipAttr::Holds;
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_few_arguments) << 2;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001563 return;
1564 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001565 break;
1566 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001567 K = OwnershipAttr::Returns;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001568
Aaron Ballman00e99962013-08-31 01:11:41 +00001569 if (AL.getNumArgs() > 2) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001570 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001571 return;
1572 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001573 break;
1574 default:
1575 // This should never happen given how we are called.
1576 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +00001577 }
1578
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001579 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001580 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1581 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001582 return;
1583 }
1584
Aaron Ballman00e99962013-08-31 01:11:41 +00001585 StringRef Module = AL.getArgAsIdent(0)->Ident->getName();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001586
1587 // Normalize the argument, __foo__ becomes foo.
1588 if (Module.startswith("__") && Module.endswith("__"))
1589 Module = Module.substr(2, Module.size() - 4);
1590
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001591 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001592 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1593 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001594 uint64_t Idx;
1595 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
Aaron Ballman00e99962013-08-31 01:11:41 +00001596 AL.getLoc(), i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001597 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001598
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001599 // Is the function argument a pointer type?
1600 QualType T = getFunctionOrMethodArgType(D, Idx);
1601 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001602 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001603 case OwnershipAttr::Takes:
1604 case OwnershipAttr::Holds:
1605 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1606 Err = 0;
1607 break;
1608 case OwnershipAttr::Returns:
1609 if (!T->isIntegerType())
1610 Err = 1;
1611 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001612 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001613 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001614 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001615 << Ex->getSourceRange();
1616 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001617 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001618
1619 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001620 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001621 i = D->specific_attr_begin<OwnershipAttr>(),
1622 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
1623 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1624 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1625 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1626 << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind());
1627 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001628 }
1629 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001630 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001631 }
1632
1633 unsigned* start = OwnershipArgs.data();
1634 unsigned size = OwnershipArgs.size();
1635 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001636
Michael Han99315932013-01-24 16:46:58 +00001637 D->addAttr(::new (S.Context)
1638 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1639 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001640}
1641
Chandler Carruthedc2c642011-07-02 00:01:44 +00001642static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001643 // Check the attribute arguments.
1644 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001645 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1646 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001647 return;
1648 }
1649
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001650 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall7a198ce2011-02-08 22:35:49 +00001651 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001652 << Attr.getName() << ExpectedVariableOrFunction;
John McCall7a198ce2011-02-08 22:35:49 +00001653 return;
1654 }
1655
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001656 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001657
Rafael Espindolac18086a2010-02-23 22:00:30 +00001658 // gcc rejects
1659 // class c {
1660 // static int a __attribute__((weakref ("v2")));
1661 // static int b() __attribute__((weakref ("f3")));
1662 // };
1663 // and ignores the attributes of
1664 // void f(void) {
1665 // static int a __attribute__((weakref ("v2")));
1666 // }
1667 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001668 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001669 if (!Ctx->isFileContext()) {
1670 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +00001671 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +00001672 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001673 }
1674
1675 // The GCC manual says
1676 //
1677 // At present, a declaration to which `weakref' is attached can only
1678 // be `static'.
1679 //
1680 // It also says
1681 //
1682 // Without a TARGET,
1683 // given as an argument to `weakref' or to `alias', `weakref' is
1684 // equivalent to `weak'.
1685 //
1686 // gcc 4.4.1 will accept
1687 // int a7 __attribute__((weakref));
1688 // as
1689 // int a7 __attribute__((weak));
1690 // This looks like a bug in gcc. We reject that for now. We should revisit
1691 // it if this behaviour is actually used.
1692
Rafael Espindolac18086a2010-02-23 22:00:30 +00001693 // GCC rejects
1694 // static ((alias ("y"), weakref)).
1695 // Should we? How to check that weakref is before or after alias?
1696
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001697 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1698 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1699 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001700 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001701 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001702 // GCC will accept anything as the argument of weakref. Should we
1703 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001704 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1705 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001706
Michael Han99315932013-01-24 16:46:58 +00001707 D->addAttr(::new (S.Context)
1708 WeakRefAttr(Attr.getRange(), S.Context,
1709 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001710}
1711
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001712static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1713 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001714 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001715 return;
1716
Douglas Gregore8bbc122011-09-02 00:18:52 +00001717 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001718 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1719 return;
1720 }
1721
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001722 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001723
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001724 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001725 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001726}
1727
Quentin Colombet4e172062012-11-01 23:55:47 +00001728static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Quentin Colombet4e172062012-11-01 23:55:47 +00001729 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1730 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1731 << Attr.getName() << ExpectedFunctionOrMethod;
1732 return;
1733 }
1734
Michael Han99315932013-01-24 16:46:58 +00001735 D->addAttr(::new (S.Context)
1736 MinSizeAttr(Attr.getRange(), S.Context,
1737 Attr.getAttributeSpellingListIndex()));
Quentin Colombet4e172062012-11-01 23:55:47 +00001738}
1739
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001740static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001741 if (!isa<FunctionDecl>(D)) {
1742 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1743 << Attr.getName() << ExpectedFunction;
1744 return;
1745 }
1746
1747 if (D->hasAttr<HotAttr>()) {
1748 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1749 << Attr.getName() << "hot";
1750 return;
1751 }
1752
Michael Han99315932013-01-24 16:46:58 +00001753 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1754 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001755}
1756
1757static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001758 if (!isa<FunctionDecl>(D)) {
1759 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1760 << Attr.getName() << ExpectedFunction;
1761 return;
1762 }
1763
1764 if (D->hasAttr<ColdAttr>()) {
1765 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1766 << Attr.getName() << "cold";
1767 return;
1768 }
1769
Michael Han99315932013-01-24 16:46:58 +00001770 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1771 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001772}
1773
Chandler Carruthedc2c642011-07-02 00:01:44 +00001774static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001775 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001776 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001777 << Attr.getName() << ExpectedFunction;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001778 return;
1779 }
1780
Michael Han99315932013-01-24 16:46:58 +00001781 D->addAttr(::new (S.Context)
1782 NakedAttr(Attr.getRange(), S.Context,
1783 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001784}
1785
Chandler Carruthedc2c642011-07-02 00:01:44 +00001786static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1787 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001788 if (!isa<FunctionDecl>(D)) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001789 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001790 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00001791 return;
1792 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001793
Michael Han99315932013-01-24 16:46:58 +00001794 D->addAttr(::new (S.Context)
1795 AlwaysInlineAttr(Attr.getRange(), S.Context,
1796 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar03a38442008-10-28 00:17:57 +00001797}
1798
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001799static void handleTLSModelAttr(Sema &S, Decl *D,
1800 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001801 StringRef Model;
1802 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001803 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001804 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001805 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001806
Richard Smithfd3834f2013-04-13 02:43:54 +00001807 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001808 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1809 << Attr.getName() << ExpectedTLSVar;
1810 return;
1811 }
1812
1813 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001814 if (Model != "global-dynamic" && Model != "local-dynamic"
1815 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001816 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001817 return;
1818 }
1819
Michael Han99315932013-01-24 16:46:58 +00001820 D->addAttr(::new (S.Context)
1821 TLSModelAttr(Attr.getRange(), S.Context, Model,
1822 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001823}
1824
Chandler Carruthedc2c642011-07-02 00:01:44 +00001825static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001826 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001827 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001828 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001829 D->addAttr(::new (S.Context)
1830 MallocAttr(Attr.getRange(), S.Context,
1831 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001832 return;
1833 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001834 }
1835
Ted Kremenek08479ae2009-08-15 00:51:46 +00001836 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001837}
1838
Chandler Carruthedc2c642011-07-02 00:01:44 +00001839static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han99315932013-01-24 16:46:58 +00001840 D->addAttr(::new (S.Context)
1841 MayAliasAttr(Attr.getRange(), S.Context,
1842 Attr.getAttributeSpellingListIndex()));
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001843}
1844
Chandler Carruthedc2c642011-07-02 00:01:44 +00001845static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001846 if (isa<VarDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001847 D->addAttr(::new (S.Context)
1848 NoCommonAttr(Attr.getRange(), S.Context,
1849 Attr.getAttributeSpellingListIndex()));
Eric Christopher515d87f2010-12-03 06:58:14 +00001850 else
1851 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001852 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001853}
1854
Chandler Carruthedc2c642011-07-02 00:01:44 +00001855static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001856 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001857 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1858 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001859 return;
1860 }
1861
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001862 if (isa<VarDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001863 D->addAttr(::new (S.Context)
1864 CommonAttr(Attr.getRange(), S.Context,
1865 Attr.getAttributeSpellingListIndex()));
Eric Christopher515d87f2010-12-03 06:58:14 +00001866 else
1867 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001868 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001869}
1870
Chandler Carruthedc2c642011-07-02 00:01:44 +00001871static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001872 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001873
1874 if (S.CheckNoReturnAttr(attr)) return;
1875
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001876 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001877 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001878 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001879 return;
1880 }
1881
Michael Han99315932013-01-24 16:46:58 +00001882 D->addAttr(::new (S.Context)
1883 NoReturnAttr(attr.getRange(), S.Context,
1884 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001885}
1886
1887bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001888 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001889 attr.setInvalid();
1890 return true;
1891 }
1892
1893 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001894}
1895
Chandler Carruthedc2c642011-07-02 00:01:44 +00001896static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1897 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001898
1899 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1900 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001901 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1902 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001903 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1904 && !VD->getType()->isFunctionPointerType())) {
1905 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001906 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001907 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001908 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001909 return;
1910 }
1911 }
1912
Michael Han99315932013-01-24 16:46:58 +00001913 D->addAttr(::new (S.Context)
1914 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1915 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001916}
1917
Richard Smith10876ef2013-01-17 01:30:42 +00001918static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1919 const AttributeList &Attr) {
1920 // C++11 [dcl.attr.noreturn]p1:
1921 // The attribute may be applied to the declarator-id in a function
1922 // declaration.
1923 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1924 if (!FD) {
1925 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1926 << Attr.getName() << ExpectedFunctionOrMethod;
1927 return;
1928 }
1929
Michael Han99315932013-01-24 16:46:58 +00001930 D->addAttr(::new (S.Context)
1931 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1932 Attr.getAttributeSpellingListIndex()));
Richard Smith10876ef2013-01-17 01:30:42 +00001933}
1934
John Thompsoncdb847ba2010-08-09 21:53:52 +00001935// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001936static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001937/*
1938 Returning a Vector Class in Registers
1939
Eric Christopherbc638a82010-12-01 22:13:54 +00001940 According to the PPU ABI specifications, a class with a single member of
1941 vector type is returned in memory when used as the return value of a function.
1942 This results in inefficient code when implementing vector classes. To return
1943 the value in a single vector register, add the vecreturn attribute to the
1944 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001945
1946 Example:
1947
1948 struct Vector
1949 {
1950 __vector float xyzw;
1951 } __attribute__((vecreturn));
1952
1953 Vector Add(Vector lhs, Vector rhs)
1954 {
1955 Vector result;
1956 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1957 return result; // This will be returned in a register
1958 }
1959*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001960 if (!isa<RecordDecl>(D)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001961 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001962 << Attr.getName() << ExpectedClass;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001963 return;
1964 }
1965
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001966 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001967 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1968 return;
1969 }
1970
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001971 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001972 int count = 0;
1973
1974 if (!isa<CXXRecordDecl>(record)) {
1975 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1976 return;
1977 }
1978
1979 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1980 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1981 return;
1982 }
1983
Eric Christopherbc638a82010-12-01 22:13:54 +00001984 for (RecordDecl::field_iterator iter = record->field_begin();
1985 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001986 if ((count == 1) || !iter->getType()->isVectorType()) {
1987 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1988 return;
1989 }
1990 count++;
1991 }
1992
Michael Han99315932013-01-24 16:46:58 +00001993 D->addAttr(::new (S.Context)
1994 VecReturnAttr(Attr.getRange(), S.Context,
1995 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001996}
1997
Richard Smithe233fbf2013-01-28 22:42:45 +00001998static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1999 const AttributeList &Attr) {
2000 if (isa<ParmVarDecl>(D)) {
2001 // [[carries_dependency]] can only be applied to a parameter if it is a
2002 // parameter of a function declaration or lambda.
2003 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
2004 S.Diag(Attr.getLoc(),
2005 diag::err_carries_dependency_param_not_function_decl);
2006 return;
2007 }
2008 } else if (!isa<FunctionDecl>(D)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002009 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002010 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002011 return;
2012 }
Richard Smithe233fbf2013-01-28 22:42:45 +00002013
2014 D->addAttr(::new (S.Context) CarriesDependencyAttr(
2015 Attr.getRange(), S.Context,
2016 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002017}
2018
Chandler Carruthedc2c642011-07-02 00:01:44 +00002019static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002020 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00002021 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002022 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002023 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00002024 return;
2025 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002026
Michael Han99315932013-01-24 16:46:58 +00002027 D->addAttr(::new (S.Context)
2028 UnusedAttr(Attr.getRange(), S.Context,
2029 Attr.getAttributeSpellingListIndex()));
Ted Kremenek39c59a82008-07-25 04:39:19 +00002030}
2031
Rafael Espindola70107f92011-10-03 14:59:42 +00002032static void handleReturnsTwiceAttr(Sema &S, Decl *D,
2033 const AttributeList &Attr) {
Rafael Espindola70107f92011-10-03 14:59:42 +00002034 if (!isa<FunctionDecl>(D)) {
2035 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2036 << Attr.getName() << ExpectedFunction;
2037 return;
2038 }
2039
Michael Han99315932013-01-24 16:46:58 +00002040 D->addAttr(::new (S.Context)
2041 ReturnsTwiceAttr(Attr.getRange(), S.Context,
2042 Attr.getAttributeSpellingListIndex()));
Rafael Espindola70107f92011-10-03 14:59:42 +00002043}
2044
Chandler Carruthedc2c642011-07-02 00:01:44 +00002045static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002046 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00002047 if (VD->hasLocalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002048 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
2049 return;
2050 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002051 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002052 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002053 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002054 return;
2055 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002056
Michael Han99315932013-01-24 16:46:58 +00002057 D->addAttr(::new (S.Context)
2058 UsedAttr(Attr.getRange(), S.Context,
2059 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002060}
2061
Chandler Carruthedc2c642011-07-02 00:01:44 +00002062static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00002063 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00002064 if (Attr.getNumArgs() > 1) {
2065 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00002066 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002067 }
Daniel Dunbar032db472008-07-31 22:40:48 +00002068
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002069 uint32_t priority = ConstructorAttr::DefaultPriority;
2070 if (Attr.getNumArgs() > 0 &&
2071 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
2072 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002073
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002074 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002075 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002076 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00002077 return;
2078 }
2079
Michael Han99315932013-01-24 16:46:58 +00002080 D->addAttr(::new (S.Context)
2081 ConstructorAttr(Attr.getRange(), S.Context, priority,
2082 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002083}
2084
Chandler Carruthedc2c642011-07-02 00:01:44 +00002085static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00002086 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00002087 if (Attr.getNumArgs() > 1) {
2088 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00002089 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002090 }
Daniel Dunbar032db472008-07-31 22:40:48 +00002091
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002092 uint32_t priority = ConstructorAttr::DefaultPriority;
2093 if (Attr.getNumArgs() > 0 &&
2094 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
2095 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002096
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002097 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002098 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002099 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00002100 return;
2101 }
2102
Michael Han99315932013-01-24 16:46:58 +00002103 D->addAttr(::new (S.Context)
2104 DestructorAttr(Attr.getRange(), S.Context, priority,
2105 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002106}
2107
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002108template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00002109static void handleAttrWithMessage(Sema &S, Decl *D,
2110 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00002111 unsigned NumArgs = Attr.getNumArgs();
2112 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00002113 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002114 return;
2115 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002116
2117 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002118 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002119 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002120 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002121
Michael Han99315932013-01-24 16:46:58 +00002122 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2123 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00002124}
2125
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00002126static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2127 const AttributeList &Attr) {
Aaron Ballmanf3614532013-11-24 20:36:50 +00002128 if (!isa<ObjCInterfaceDecl>(D)) {
2129 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2130 << Attr.getName() << ExpectedObjectiveCInterface;
2131 return;
2132 }
Michael Han99315932013-01-24 16:46:58 +00002133 D->addAttr(::new (S.Context)
2134 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2135 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00002136}
2137
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002138static void handleObjCRootClassAttr(Sema &S, Decl *D,
2139 const AttributeList &Attr) {
2140 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballmanf90ccb02013-07-18 14:56:42 +00002141 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2142 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002143 return;
2144 }
2145
Michael Han99315932013-01-24 16:46:58 +00002146 D->addAttr(::new (S.Context)
2147 ObjCRootClassAttr(Attr.getRange(), S.Context,
2148 Attr.getAttributeSpellingListIndex()));
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002149}
2150
Ted Kremenek28eace62013-11-23 01:01:34 +00002151static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
2152 const AttributeList &Attr) {
2153 if (!isa<ObjCInterfaceDecl>(D)) {
2154 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2155 << Attr.getName() << ExpectedObjectiveCInterface;
2156 return;
2157 }
2158
Ted Kremenek7559b472013-11-23 22:29:11 +00002159 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek28eace62013-11-23 01:01:34 +00002160
2161 if (!Parm) {
2162 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 1;
2163 return;
2164 }
2165
2166 D->addAttr(::new (S.Context)
2167 ObjCSuppressProtocolAttr(Attr.getRange(), S.Context, Parm->Ident,
2168 Attr.getAttributeSpellingListIndex()));
2169}
2170
2171
Michael Han99315932013-01-24 16:46:58 +00002172static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2173 const AttributeList &Attr) {
Fariborz Jahanian7249e362012-01-03 22:52:32 +00002174 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballmanf3614532013-11-24 20:36:50 +00002175 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2176 << Attr.getName() << ExpectedObjectiveCInterface;
Fariborz Jahanian7249e362012-01-03 22:52:32 +00002177 return;
2178 }
2179
Michael Han99315932013-01-24 16:46:58 +00002180 D->addAttr(::new (S.Context)
2181 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2182 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00002183}
2184
Jordy Rose740b0c22012-05-08 03:27:22 +00002185static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2186 IdentifierInfo *Platform,
2187 VersionTuple Introduced,
2188 VersionTuple Deprecated,
2189 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002190 StringRef PlatformName
2191 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2192 if (PlatformName.empty())
2193 PlatformName = Platform->getName();
2194
2195 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2196 // of these steps are needed).
2197 if (!Introduced.empty() && !Deprecated.empty() &&
2198 !(Introduced <= Deprecated)) {
2199 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2200 << 1 << PlatformName << Deprecated.getAsString()
2201 << 0 << Introduced.getAsString();
2202 return true;
2203 }
2204
2205 if (!Introduced.empty() && !Obsoleted.empty() &&
2206 !(Introduced <= Obsoleted)) {
2207 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2208 << 2 << PlatformName << Obsoleted.getAsString()
2209 << 0 << Introduced.getAsString();
2210 return true;
2211 }
2212
2213 if (!Deprecated.empty() && !Obsoleted.empty() &&
2214 !(Deprecated <= Obsoleted)) {
2215 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2216 << 2 << PlatformName << Obsoleted.getAsString()
2217 << 1 << Deprecated.getAsString();
2218 return true;
2219 }
2220
2221 return false;
2222}
2223
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002224/// \brief Check whether the two versions match.
2225///
2226/// If either version tuple is empty, then they are assumed to match. If
2227/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2228static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2229 bool BeforeIsOkay) {
2230 if (X.empty() || Y.empty())
2231 return true;
2232
2233 if (X == Y)
2234 return true;
2235
2236 if (BeforeIsOkay && X < Y)
2237 return true;
2238
2239 return false;
2240}
2241
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002242AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002243 IdentifierInfo *Platform,
2244 VersionTuple Introduced,
2245 VersionTuple Deprecated,
2246 VersionTuple Obsoleted,
2247 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002248 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00002249 bool Override,
2250 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002251 VersionTuple MergedIntroduced = Introduced;
2252 VersionTuple MergedDeprecated = Deprecated;
2253 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002254 bool FoundAny = false;
2255
Rafael Espindolac67f2232012-05-10 02:50:16 +00002256 if (D->hasAttrs()) {
2257 AttrVec &Attrs = D->getAttrs();
2258 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2259 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2260 if (!OldAA) {
2261 ++i;
2262 continue;
2263 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002264
Rafael Espindolac67f2232012-05-10 02:50:16 +00002265 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2266 if (OldPlatform != Platform) {
2267 ++i;
2268 continue;
2269 }
2270
2271 FoundAny = true;
2272 VersionTuple OldIntroduced = OldAA->getIntroduced();
2273 VersionTuple OldDeprecated = OldAA->getDeprecated();
2274 VersionTuple OldObsoleted = OldAA->getObsoleted();
2275 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002276
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002277 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2278 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2279 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2280 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00002281 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002282 if (Override) {
2283 int Which = -1;
2284 VersionTuple FirstVersion;
2285 VersionTuple SecondVersion;
2286 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2287 Which = 0;
2288 FirstVersion = OldIntroduced;
2289 SecondVersion = Introduced;
2290 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2291 Which = 1;
2292 FirstVersion = Deprecated;
2293 SecondVersion = OldDeprecated;
2294 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2295 Which = 2;
2296 FirstVersion = Obsoleted;
2297 SecondVersion = OldObsoleted;
2298 }
2299
2300 if (Which == -1) {
2301 Diag(OldAA->getLocation(),
2302 diag::warn_mismatched_availability_override_unavail)
2303 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2304 } else {
2305 Diag(OldAA->getLocation(),
2306 diag::warn_mismatched_availability_override)
2307 << Which
2308 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2309 << FirstVersion.getAsString() << SecondVersion.getAsString();
2310 }
2311 Diag(Range.getBegin(), diag::note_overridden_method);
2312 } else {
2313 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2314 Diag(Range.getBegin(), diag::note_previous_attribute);
2315 }
2316
Rafael Espindolac67f2232012-05-10 02:50:16 +00002317 Attrs.erase(Attrs.begin() + i);
2318 --e;
2319 continue;
2320 }
2321
2322 VersionTuple MergedIntroduced2 = MergedIntroduced;
2323 VersionTuple MergedDeprecated2 = MergedDeprecated;
2324 VersionTuple MergedObsoleted2 = MergedObsoleted;
2325
2326 if (MergedIntroduced2.empty())
2327 MergedIntroduced2 = OldIntroduced;
2328 if (MergedDeprecated2.empty())
2329 MergedDeprecated2 = OldDeprecated;
2330 if (MergedObsoleted2.empty())
2331 MergedObsoleted2 = OldObsoleted;
2332
2333 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2334 MergedIntroduced2, MergedDeprecated2,
2335 MergedObsoleted2)) {
2336 Attrs.erase(Attrs.begin() + i);
2337 --e;
2338 continue;
2339 }
2340
2341 MergedIntroduced = MergedIntroduced2;
2342 MergedDeprecated = MergedDeprecated2;
2343 MergedObsoleted = MergedObsoleted2;
2344 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002345 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002346 }
2347
2348 if (FoundAny &&
2349 MergedIntroduced == Introduced &&
2350 MergedDeprecated == Deprecated &&
2351 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002352 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002353
Ted Kremenekb5445722013-04-06 00:34:27 +00002354 // Only create a new attribute if !Override, but we want to do
2355 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002356 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002357 MergedDeprecated, MergedObsoleted) &&
2358 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002359 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2360 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002361 Obsoleted, IsUnavailable, Message,
2362 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002363 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002364 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002365}
2366
Chandler Carruthedc2c642011-07-02 00:01:44 +00002367static void handleAvailabilityAttr(Sema &S, Decl *D,
2368 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002369 if (!checkAttributeNumArgs(S, Attr, 1))
2370 return;
2371 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00002372 unsigned Index = Attr.getAttributeSpellingListIndex();
2373
Aaron Ballman00e99962013-08-31 01:11:41 +00002374 IdentifierInfo *II = Platform->Ident;
2375 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2376 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2377 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002378
Rafael Espindolac231fab2013-01-08 21:30:32 +00002379 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2380 if (!ND) {
2381 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2382 return;
2383 }
2384
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002385 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2386 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2387 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002388 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002389 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00002390 if (const StringLiteral *SE =
2391 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002392 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002393
Aaron Ballman00e99962013-08-31 01:11:41 +00002394 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002395 Introduced.Version,
2396 Deprecated.Version,
2397 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002398 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00002399 /*Override=*/false,
2400 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002401 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002402 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002403}
2404
John McCalld041a9b2013-02-20 01:54:26 +00002405template <class T>
2406static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2407 typename T::VisibilityType value,
2408 unsigned attrSpellingListIndex) {
2409 T *existingAttr = D->getAttr<T>();
2410 if (existingAttr) {
2411 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2412 if (existingValue == value)
2413 return NULL;
2414 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2415 S.Diag(range.getBegin(), diag::note_previous_attribute);
2416 D->dropAttr<T>();
2417 }
2418 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2419}
2420
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002421VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002422 VisibilityAttr::VisibilityType Vis,
2423 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002424 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2425 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002426}
2427
John McCalld041a9b2013-02-20 01:54:26 +00002428TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2429 TypeVisibilityAttr::VisibilityType Vis,
2430 unsigned AttrSpellingListIndex) {
2431 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2432 AttrSpellingListIndex);
2433}
2434
2435static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2436 bool isTypeVisibility) {
2437 // Visibility attributes don't mean anything on a typedef.
2438 if (isa<TypedefNameDecl>(D)) {
2439 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2440 << Attr.getName();
2441 return;
2442 }
2443
2444 // 'type_visibility' can only go on a type or namespace.
2445 if (isTypeVisibility &&
2446 !(isa<TagDecl>(D) ||
2447 isa<ObjCInterfaceDecl>(D) ||
2448 isa<NamespaceDecl>(D))) {
2449 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2450 << Attr.getName() << ExpectedTypeOrNamespace;
2451 return;
2452 }
2453
Benjamin Kramer70370212013-09-09 15:08:57 +00002454 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002455 StringRef TypeStr;
2456 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002457 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002458 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002459
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002460 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002461 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002462 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002463 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002464 return;
2465 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002466
2467 // Complain about attempts to use protected visibility on targets
2468 // (like Darwin) that don't support it.
2469 if (type == VisibilityAttr::Protected &&
2470 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2471 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2472 type = VisibilityAttr::Default;
2473 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002474
Michael Han99315932013-01-24 16:46:58 +00002475 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002476 clang::Attr *newAttr;
2477 if (isTypeVisibility) {
2478 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2479 (TypeVisibilityAttr::VisibilityType) type,
2480 Index);
2481 } else {
2482 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2483 }
2484 if (newAttr)
2485 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002486}
2487
Chandler Carruthedc2c642011-07-02 00:01:44 +00002488static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2489 const AttributeList &Attr) {
John McCall86bc21f2011-03-02 11:33:24 +00002490 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2491 if (!method) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002492 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002493 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00002494 return;
2495 }
2496
Aaron Ballman00e99962013-08-31 01:11:41 +00002497 if (!Attr.isArgIdent(0)) {
2498 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2499 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002500 return;
2501 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002502
Aaron Ballman682ee422013-09-11 19:47:58 +00002503 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2504 ObjCMethodFamilyAttr::FamilyKind F;
2505 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2506 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2507 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002508 return;
2509 }
2510
Aaron Ballman682ee422013-09-11 19:47:58 +00002511 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCall31168b02011-06-15 23:02:42 +00002512 !method->getResultType()->isObjCObjectPointerType()) {
2513 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2514 << method->getResultType();
2515 // Ignore the attribute.
2516 return;
2517 }
2518
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002519 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman682ee422013-09-11 19:47:58 +00002520 S.Context, F));
John McCall86bc21f2011-03-02 11:33:24 +00002521}
2522
Chandler Carruthedc2c642011-07-02 00:01:44 +00002523static void handleObjCExceptionAttr(Sema &S, Decl *D,
2524 const AttributeList &Attr) {
Chris Lattner677a3582009-02-14 08:09:34 +00002525 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2526 if (OCI == 0) {
Aaron Ballmanf90ccb02013-07-18 14:56:42 +00002527 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2528 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner677a3582009-02-14 08:09:34 +00002529 return;
2530 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002531
Michael Han99315932013-01-24 16:46:58 +00002532 D->addAttr(::new (S.Context)
2533 ObjCExceptionAttr(Attr.getRange(), S.Context,
2534 Attr.getAttributeSpellingListIndex()));
Chris Lattner677a3582009-02-14 08:09:34 +00002535}
2536
Chandler Carruthedc2c642011-07-02 00:01:44 +00002537static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002538 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002539 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002540 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002541 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2542 return;
2543 }
2544 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002545 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2546 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002547 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002548 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2549 return;
2550 }
2551 }
2552 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002553 // It is okay to include this attribute on properties, e.g.:
2554 //
2555 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2556 //
2557 // In this case it follows tradition and suppresses an error in the above
2558 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002559 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002560 }
Michael Han99315932013-01-24 16:46:58 +00002561 D->addAttr(::new (S.Context)
2562 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2563 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002564}
2565
Mike Stumpd3bb5572009-07-24 19:02:52 +00002566static void
Chandler Carruthedc2c642011-07-02 00:01:44 +00002567handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002568 if (!isa<FunctionDecl>(D)) {
2569 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2570 return;
2571 }
2572
Michael Han99315932013-01-24 16:46:58 +00002573 D->addAttr(::new (S.Context)
2574 OverloadableAttr(Attr.getRange(), S.Context,
2575 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002576}
2577
Chandler Carruthedc2c642011-07-02 00:01:44 +00002578static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002579 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002580 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002581 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002582 return;
2583 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002584
Aaron Ballman00e99962013-08-31 01:11:41 +00002585 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002586 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002587 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2588 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2589 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002590 return;
2591 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002592
Michael Han99315932013-01-24 16:46:58 +00002593 D->addAttr(::new (S.Context)
2594 BlocksAttr(Attr.getRange(), S.Context, type,
2595 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002596}
2597
Chandler Carruthedc2c642011-07-02 00:01:44 +00002598static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002599 // check the attribute arguments.
2600 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002601 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002602 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002603 }
2604
Aaron Ballman18a78382013-11-21 00:28:23 +00002605 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002606 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002607 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002608 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002609 if (E->isTypeDependent() || E->isValueDependent() ||
2610 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002611 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002612 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002613 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002614 return;
2615 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002616
John McCallb46f2872011-09-09 07:56:05 +00002617 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002618 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2619 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002620 return;
2621 }
John McCallb46f2872011-09-09 07:56:05 +00002622
2623 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002624 }
2625
Aaron Ballman18a78382013-11-21 00:28:23 +00002626 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002627 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002628 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002629 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002630 if (E->isTypeDependent() || E->isValueDependent() ||
2631 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002632 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002633 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002634 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002635 return;
2636 }
2637 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002638
John McCallb46f2872011-09-09 07:56:05 +00002639 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002640 // FIXME: This error message could be improved, it would be nice
2641 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002642 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2643 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002644 return;
2645 }
2646 }
2647
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002648 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002649 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002650 if (isa<FunctionNoProtoType>(FT)) {
2651 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2652 return;
2653 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002654
Chris Lattner9363e312009-03-17 23:03:47 +00002655 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002656 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002657 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002658 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002659 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002660 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002661 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002662 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002663 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002664 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2665 if (!BD->isVariadic()) {
2666 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2667 return;
2668 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002669 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002670 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002671 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002672 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002673 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002674 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002675 int m = Ty->isFunctionPointerType() ? 0 : 1;
2676 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002677 return;
2678 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002679 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002680 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002681 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002682 return;
2683 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002684 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002685 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002686 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002687 return;
2688 }
Michael Han99315932013-01-24 16:46:58 +00002689 D->addAttr(::new (S.Context)
2690 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2691 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002692}
2693
Lubos Lunakedc13882013-07-20 15:05:36 +00002694static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Lubos Lunakedc13882013-07-20 15:05:36 +00002695 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2696 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2697 else
2698 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2699}
2700
Chandler Carruthedc2c642011-07-02 00:01:44 +00002701static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002702 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002703 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002704 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002705 return;
2706 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002707
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002708 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2709 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2710 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002711 return;
2712 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002713 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2714 if (MD->getResultType()->isVoidType()) {
2715 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2716 << Attr.getName() << 1;
2717 return;
2718 }
2719
Michael Han99315932013-01-24 16:46:58 +00002720 D->addAttr(::new (S.Context)
2721 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2722 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002723}
2724
Chandler Carruthedc2c642011-07-02 00:01:44 +00002725static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002726 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002727 if (isa<CXXRecordDecl>(D)) {
2728 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2729 return;
2730 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002731 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2732 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002733 return;
2734 }
2735
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002736 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002737
Michael Han99315932013-01-24 16:46:58 +00002738 nd->addAttr(::new (S.Context)
2739 WeakAttr(Attr.getRange(), S.Context,
2740 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002741}
2742
Chandler Carruthedc2c642011-07-02 00:01:44 +00002743static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002744 // weak_import only applies to variable & function declarations.
2745 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002746 if (!D->canBeWeakImported(isDef)) {
2747 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002748 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2749 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002750 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002751 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002752 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002753 // Nothing to warn about here.
2754 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002755 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002756 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002757
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002758 return;
2759 }
2760
Michael Han99315932013-01-24 16:46:58 +00002761 D->addAttr(::new (S.Context)
2762 WeakImportAttr(Attr.getRange(), S.Context,
2763 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002764}
2765
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002766// Handles reqd_work_group_size and work_group_size_hint.
2767static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002768 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002769 uint32_t WGSize[3];
2770 for (unsigned i = 0; i < 3; ++i)
2771 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002772 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002773
2774 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2775 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2776 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2777 if (!(A->getXDim() == WGSize[0] &&
2778 A->getYDim() == WGSize[1] &&
2779 A->getZDim() == WGSize[2])) {
2780 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2781 Attr.getName();
2782 }
2783 }
2784
2785 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2786 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2787 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2788 if (!(A->getXDim() == WGSize[0] &&
2789 A->getYDim() == WGSize[1] &&
2790 A->getZDim() == WGSize[2])) {
2791 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2792 Attr.getName();
2793 }
2794 }
2795
2796 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2797 D->addAttr(::new (S.Context)
2798 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002799 WGSize[0], WGSize[1], WGSize[2],
2800 Attr.getAttributeSpellingListIndex()));
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002801 else
2802 D->addAttr(::new (S.Context)
2803 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002804 WGSize[0], WGSize[1], WGSize[2],
2805 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002806}
2807
Joey Goulyaba589c2013-03-08 09:42:32 +00002808static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2809 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2810
Aaron Ballman00e99962013-08-31 01:11:41 +00002811 if (!Attr.hasParsedType()) {
2812 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2813 << Attr.getName() << 1;
2814 return;
2815 }
2816
Richard Smithb87c4652013-10-31 21:23:20 +00002817 TypeSourceInfo *ParmTSI = 0;
2818 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2819 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002820
2821 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2822 (ParmType->isBooleanType() ||
2823 !ParmType->isIntegralType(S.getASTContext()))) {
2824 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2825 << ParmType;
2826 return;
2827 }
2828
2829 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2830 D->hasAttr<VecTypeHintAttr>()) {
2831 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
Richard Smithb87c4652013-10-31 21:23:20 +00002832 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002833 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2834 return;
2835 }
2836 }
2837
2838 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Richard Smithb87c4652013-10-31 21:23:20 +00002839 ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002840}
2841
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002842SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002843 StringRef Name,
2844 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002845 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2846 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002847 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002848 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2849 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002850 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002851 }
Michael Han99315932013-01-24 16:46:58 +00002852 return ::new (Context) SectionAttr(Range, Context, Name,
2853 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002854}
2855
Chandler Carruthedc2c642011-07-02 00:01:44 +00002856static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002857 // Make sure that there is a string literal as the sections's single
2858 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002859 StringRef Str;
2860 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002861 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002862 return;
Mike Stump11289f42009-09-09 15:08:12 +00002863
Chris Lattner30ba6742009-08-10 19:03:04 +00002864 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002865 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002866 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002867 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002868 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002869 return;
2870 }
Mike Stump11289f42009-09-09 15:08:12 +00002871
Chris Lattner20aee9b2010-01-12 20:58:53 +00002872 // This attribute cannot be applied to local variables.
2873 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002874 S.Diag(LiteralLoc, diag::err_attribute_section_local_variable);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002875 return;
2876 }
Michael Han99315932013-01-24 16:46:58 +00002877
2878 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002879 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002880 if (NewAttr)
2881 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002882}
2883
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002884
Chandler Carruthedc2c642011-07-02 00:01:44 +00002885static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002886 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002887 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002888 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002889 } else {
Michael Han99315932013-01-24 16:46:58 +00002890 D->addAttr(::new (S.Context)
2891 NoThrowAttr(Attr.getRange(), S.Context,
2892 Attr.getAttributeSpellingListIndex()));
Douglas Gregor88336832011-06-15 05:45:11 +00002893 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002894}
2895
Chandler Carruthedc2c642011-07-02 00:01:44 +00002896static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002897 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002898 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002899 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002900 } else {
Michael Han99315932013-01-24 16:46:58 +00002901 D->addAttr(::new (S.Context)
2902 ConstAttr(Attr.getRange(), S.Context,
2903 Attr.getAttributeSpellingListIndex() ));
Douglas Gregor88336832011-06-15 05:45:11 +00002904 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002905}
2906
Chandler Carruthedc2c642011-07-02 00:01:44 +00002907static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han99315932013-01-24 16:46:58 +00002908 D->addAttr(::new (S.Context)
2909 PureAttr(Attr.getRange(), S.Context,
2910 Attr.getAttributeSpellingListIndex()));
Anders Carlssonb8316282008-10-05 23:32:53 +00002911}
2912
Chandler Carruthedc2c642011-07-02 00:01:44 +00002913static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002914 VarDecl *VD = dyn_cast<VarDecl>(D);
Anders Carlssond277d792009-01-31 01:16:18 +00002915 if (!VD || !VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002916 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002917 return;
2918 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002919
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002920 Expr *E = Attr.getArgAsExpr(0);
2921 SourceLocation Loc = E->getExprLoc();
2922 FunctionDecl *FD = 0;
2923 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002924
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002925 // gcc only allows for simple identifiers. Since we support more than gcc, we
2926 // will warn the user.
2927 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2928 if (DRE->hasQualifier())
2929 S.Diag(Loc, diag::warn_cleanup_ext);
2930 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2931 NI = DRE->getNameInfo();
2932 if (!FD) {
2933 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2934 << NI.getName();
2935 return;
2936 }
2937 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2938 if (ULE->hasExplicitTemplateArgs())
2939 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002940 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2941 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002942 if (!FD) {
2943 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2944 << NI.getName();
2945 if (ULE->getType() == S.Context.OverloadTy)
2946 S.NoteAllOverloadCandidates(ULE);
2947 return;
2948 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002949 } else {
2950 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002951 return;
2952 }
2953
Anders Carlssond277d792009-01-31 01:16:18 +00002954 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002955 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2956 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002957 return;
2958 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002959
Anders Carlsson723f55d2009-02-07 23:16:50 +00002960 // We're currently more strict than GCC about what function types we accept.
2961 // If this ever proves to be a problem it should be easy to fix.
2962 QualType Ty = S.Context.getPointerType(VD->getType());
2963 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002964 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2965 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002966 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2967 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002968 return;
2969 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002970
Michael Han99315932013-01-24 16:46:58 +00002971 D->addAttr(::new (S.Context)
2972 CleanupAttr(Attr.getRange(), S.Context, FD,
2973 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002974}
2975
Mike Stumpd3bb5572009-07-24 19:02:52 +00002976/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002977/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002978static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002979 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002980 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002981 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002982 return;
2983 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002984
Aaron Ballman00e99962013-08-31 01:11:41 +00002985 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002986 uint64_t ArgIdx;
2987 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2988 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002989 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002990
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002991 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002992 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002993
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002994 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2995 if (not_nsstring_type &&
2996 !isCFStringType(Ty, S.Context) &&
2997 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002998 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002999 // FIXME: Should highlight the actual expression that has the wrong type.
3000 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00003001 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003002 << IdxExpr->getSourceRange();
3003 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003004 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003005 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003006 if (!isNSStringType(Ty, S.Context) &&
3007 !isCFStringType(Ty, S.Context) &&
3008 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003009 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003010 // FIXME: Should highlight the actual expression that has the wrong type.
3011 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00003012 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003013 << IdxExpr->getSourceRange();
3014 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003015 }
3016
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00003017 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
3018 // because that has corrected for the implicit this parameter, and is zero-
3019 // based. The attribute expects what the user wrote explicitly.
3020 llvm::APSInt Val;
3021 IdxExpr->EvaluateAsInt(Val, S.Context);
3022
Michael Han99315932013-01-24 16:46:58 +00003023 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00003024 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00003025 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003026}
3027
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003028enum FormatAttrKind {
3029 CFStringFormat,
3030 NSStringFormat,
3031 StrftimeFormat,
3032 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003033 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003034 InvalidFormat
3035};
3036
3037/// getFormatAttrKind - Map from format attribute names to supported format
3038/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003039static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003040 return llvm::StringSwitch<FormatAttrKind>(Format)
3041 // Check for formats that get handled specially.
3042 .Case("NSString", NSStringFormat)
3043 .Case("CFString", CFStringFormat)
3044 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003045
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003046 // Otherwise, check for supported formats.
3047 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3048 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3049 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003050
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003051 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3052 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003053}
3054
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003055/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003056/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003057static void handleInitPriorityAttr(Sema &S, Decl *D,
3058 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003059 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003060 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3061 return;
3062 }
3063
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003064 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003065 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3066 Attr.setInvalid();
3067 return;
3068 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003069 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003070 if (S.Context.getAsArrayType(T))
3071 T = S.Context.getBaseElementType(T);
3072 if (!T->getAs<RecordType>()) {
3073 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3074 Attr.setInvalid();
3075 return;
3076 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003077
3078 Expr *E = Attr.getArgAsExpr(0);
3079 uint32_t prioritynum;
3080 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003081 Attr.setInvalid();
3082 return;
3083 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003084
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003085 if (prioritynum < 101 || prioritynum > 65535) {
3086 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003087 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003088 Attr.setInvalid();
3089 return;
3090 }
Michael Han99315932013-01-24 16:46:58 +00003091 D->addAttr(::new (S.Context)
3092 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3093 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003094}
3095
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003096FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3097 IdentifierInfo *Format, int FormatIdx,
3098 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00003099 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003100 // Check whether we already have an equivalent format attribute.
3101 for (specific_attr_iterator<FormatAttr>
3102 i = D->specific_attr_begin<FormatAttr>(),
3103 e = D->specific_attr_end<FormatAttr>();
3104 i != e ; ++i) {
3105 FormatAttr *f = *i;
3106 if (f->getType() == Format &&
3107 f->getFormatIdx() == FormatIdx &&
3108 f->getFirstArg() == FirstArg) {
3109 // If we don't have a valid location for this attribute, adopt the
3110 // location.
3111 if (f->getLocation().isInvalid())
3112 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003113 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00003114 }
3115 }
3116
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003117 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3118 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003119}
3120
Mike Stumpd3bb5572009-07-24 19:02:52 +00003121/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003122/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003123static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003124 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003125 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00003126 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003127 return;
3128 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003129
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003130 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003131 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003132 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003133 return;
3134 }
3135
Chandler Carruth743682b2010-11-16 08:35:43 +00003136 // In C++ the implicit 'this' function parameter also counts, and they are
3137 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003138 bool HasImplicitThisParam = isInstanceMethod(D);
3139 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003140
Aaron Ballman00e99962013-08-31 01:11:41 +00003141 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
3142 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003143
3144 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003145 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003146 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003147 // If we've modified the string name, we need a new identifier for it.
3148 II = &S.Context.Idents.get(Format);
3149 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003150
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003151 // Check for supported formats.
3152 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00003153
3154 if (Kind == IgnoredFormat)
3155 return;
3156
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003157 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00003158 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman00e99962013-08-31 01:11:41 +00003159 << "format" << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003160 return;
3161 }
3162
3163 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00003164 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003165 uint32_t Idx;
3166 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003167 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003168
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003169 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003170 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003171 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003172 return;
3173 }
3174
3175 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003176 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003177
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003178 if (HasImplicitThisParam) {
3179 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00003180 S.Diag(Attr.getLoc(),
3181 diag::err_format_attribute_implicit_this_format_string)
3182 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003183 return;
3184 }
3185 ArgIdx--;
3186 }
Mike Stump11289f42009-09-09 15:08:12 +00003187
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003188 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003189 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003190
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003191 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003192 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003193 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3194 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00003195 return;
3196 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003197 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003198 // FIXME: do we need to check if the type is NSString*? What are the
3199 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003200 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003201 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003202 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3203 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003204 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003205 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003206 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003207 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003208 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003209 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3210 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003211 return;
3212 }
3213
3214 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00003215 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003216 uint32_t FirstArg;
3217 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003218 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003219
3220 // check if the function is variadic if the 3rd argument non-zero
3221 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003222 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003223 ++NumArgs; // +1 for ...
3224 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003225 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003226 return;
3227 }
3228 }
3229
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003230 // strftime requires FirstArg to be 0 because it doesn't read from any
3231 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003232 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003233 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00003234 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3235 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003236 return;
3237 }
3238 // if 0 it disables parameter checking (to use with e.g. va_list)
3239 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003240 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003241 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003242 return;
3243 }
3244
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003245 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003246 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00003247 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003248 if (NewAttr)
3249 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003250}
3251
Chandler Carruthedc2c642011-07-02 00:01:44 +00003252static void handleTransparentUnionAttr(Sema &S, Decl *D,
3253 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003254 // Try to find the underlying union declaration.
3255 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003256 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003257 if (TD && TD->getUnderlyingType()->isUnionType())
3258 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3259 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003260 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003261
3262 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00003263 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003264 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003265 return;
3266 }
3267
John McCallf937c022011-10-07 06:10:15 +00003268 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003269 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003270 diag::warn_transparent_union_attribute_not_definition);
3271 return;
3272 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003273
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003274 RecordDecl::field_iterator Field = RD->field_begin(),
3275 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003276 if (Field == FieldEnd) {
3277 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3278 return;
3279 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003280
David Blaikie40ed2972012-06-06 20:45:41 +00003281 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003282 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003283 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003284 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003285 diag::warn_transparent_union_attribute_floating)
3286 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003287 return;
3288 }
3289
3290 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3291 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3292 for (; Field != FieldEnd; ++Field) {
3293 QualType FieldType = Field->getType();
3294 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3295 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3296 // Warn if we drop the attribute.
3297 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003298 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003299 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003300 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003301 diag::warn_transparent_union_attribute_field_size_align)
3302 << isSize << Field->getDeclName() << FieldBits;
3303 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003304 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003305 diag::note_transparent_union_first_field_size_align)
3306 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003307 return;
3308 }
3309 }
3310
Michael Han99315932013-01-24 16:46:58 +00003311 RD->addAttr(::new (S.Context)
3312 TransparentUnionAttr(Attr.getRange(), S.Context,
3313 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003314}
3315
Chandler Carruthedc2c642011-07-02 00:01:44 +00003316static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003317 // Make sure that there is a string literal as the annotation's single
3318 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003319 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003320 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003321 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003322
3323 // Don't duplicate annotations that are already set.
3324 for (specific_attr_iterator<AnnotateAttr>
3325 i = D->specific_attr_begin<AnnotateAttr>(),
3326 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003327 if ((*i)->getAnnotation() == Str)
3328 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003329 }
Michael Han99315932013-01-24 16:46:58 +00003330
3331 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003332 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00003333 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003334}
3335
Chandler Carruthedc2c642011-07-02 00:01:44 +00003336static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003337 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003338 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003339 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3340 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003341 return;
3342 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003343
Richard Smith848e1f12013-02-01 08:12:08 +00003344 if (Attr.getNumArgs() == 0) {
3345 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3346 true, 0, Attr.getAttributeSpellingListIndex()));
3347 return;
3348 }
3349
Aaron Ballman00e99962013-08-31 01:11:41 +00003350 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00003351 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3352 S.Diag(Attr.getEllipsisLoc(),
3353 diag::err_pack_expansion_without_parameter_packs);
3354 return;
3355 }
3356
3357 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3358 return;
3359
3360 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3361 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003362}
3363
3364void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003365 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003366 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3367 SourceLocation AttrLoc = AttrRange.getBegin();
3368
Richard Smith1dba27c2013-01-29 09:02:09 +00003369 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003370 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003371 // C++11 [dcl.align]p1:
3372 // An alignment-specifier may be applied to a variable or to a class
3373 // data member, but it shall not be applied to a bit-field, a function
3374 // parameter, the formal parameter of a catch clause, or a variable
3375 // declared with the register storage class specifier. An
3376 // alignment-specifier may also be applied to the declaration of a class
3377 // or enumeration type.
3378 // C11 6.7.5/2:
3379 // An alignment attribute shall not be specified in a declaration of
3380 // a typedef, or a bit-field, or a function, or a parameter, or an
3381 // object declared with the register storage-class specifier.
3382 int DiagKind = -1;
3383 if (isa<ParmVarDecl>(D)) {
3384 DiagKind = 0;
3385 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3386 if (VD->getStorageClass() == SC_Register)
3387 DiagKind = 1;
3388 if (VD->isExceptionVariable())
3389 DiagKind = 2;
3390 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3391 if (FD->isBitField())
3392 DiagKind = 3;
3393 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00003394 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3395 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00003396 << (TmpAttr.isC11() ? ExpectedVariableOrField
3397 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003398 return;
3399 }
3400 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003401 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smithbc8caaf2013-02-22 04:55:39 +00003402 << TmpAttr.isC11() << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003403 return;
3404 }
3405 }
3406
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003407 if (E->isTypeDependent() || E->isValueDependent()) {
3408 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003409 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3410 AA->setPackExpansion(IsPackExpansion);
3411 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003412 return;
3413 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003414
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003415 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00003416 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00003417 ExprResult ICE
3418 = VerifyIntegerConstantExpression(E, &Alignment,
3419 diag::err_aligned_attribute_argument_not_int,
3420 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003421 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003422 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003423
3424 // C++11 [dcl.align]p2:
3425 // -- if the constant expression evaluates to zero, the alignment
3426 // specifier shall have no effect
3427 // C11 6.7.5p6:
3428 // An alignment specification of zero has no effect.
3429 if (!(TmpAttr.isAlignas() && !Alignment) &&
3430 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003431 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3432 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003433 return;
3434 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003435
Richard Smith848e1f12013-02-01 08:12:08 +00003436 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00003437 // We've already verified it's a power of 2, now let's make sure it's
3438 // 8192 or less.
3439 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00003440 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00003441 << E->getSourceRange();
3442 return;
3443 }
3444 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003445
Richard Smith44c247f2013-02-22 08:32:16 +00003446 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3447 ICE.take(), SpellingListIndex);
3448 AA->setPackExpansion(IsPackExpansion);
3449 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003450}
3451
Michael Hanaf02bbe2013-02-01 01:19:17 +00003452void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003453 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003454 // FIXME: Cache the number on the Attr object if non-dependent?
3455 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003456 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3457 SpellingListIndex);
3458 AA->setPackExpansion(IsPackExpansion);
3459 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003460}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003461
Richard Smith848e1f12013-02-01 08:12:08 +00003462void Sema::CheckAlignasUnderalignment(Decl *D) {
3463 assert(D->hasAttrs() && "no attributes on decl");
3464
3465 QualType Ty;
3466 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3467 Ty = VD->getType();
3468 else
3469 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00003470 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003471 return;
3472
3473 // C++11 [dcl.align]p5, C11 6.7.5/4:
3474 // The combined effect of all alignment attributes in a declaration shall
3475 // not specify an alignment that is less strict than the alignment that
3476 // would otherwise be required for the entity being declared.
3477 AlignedAttr *AlignasAttr = 0;
3478 unsigned Align = 0;
3479 for (specific_attr_iterator<AlignedAttr>
3480 I = D->specific_attr_begin<AlignedAttr>(),
3481 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3482 if (I->isAlignmentDependent())
3483 return;
3484 if (I->isAlignas())
3485 AlignasAttr = *I;
3486 Align = std::max(Align, I->getAlignment(Context));
3487 }
3488
3489 if (AlignasAttr && Align) {
3490 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3491 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3492 if (NaturalAlign > RequestedAlign)
3493 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3494 << Ty << (unsigned)NaturalAlign.getQuantity();
3495 }
3496}
3497
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003498/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003499/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003500///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003501/// Despite what would be logical, the mode attribute is a decl attribute, not a
3502/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3503/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003504static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003505 // This attribute isn't documented, but glibc uses it. It changes
3506 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00003507 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00003508 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3509 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003510 return;
3511 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003512
Aaron Ballman00e99962013-08-31 01:11:41 +00003513 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3514 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003515
3516 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003517 if (Str.startswith("__") && Str.endswith("__"))
3518 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003519
3520 unsigned DestWidth = 0;
3521 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003522 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003523 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003524 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003525 switch (Str[0]) {
3526 case 'Q': DestWidth = 8; break;
3527 case 'H': DestWidth = 16; break;
3528 case 'S': DestWidth = 32; break;
3529 case 'D': DestWidth = 64; break;
3530 case 'X': DestWidth = 96; break;
3531 case 'T': DestWidth = 128; break;
3532 }
3533 if (Str[1] == 'F') {
3534 IntegerMode = false;
3535 } else if (Str[1] == 'C') {
3536 IntegerMode = false;
3537 ComplexMode = true;
3538 } else if (Str[1] != 'I') {
3539 DestWidth = 0;
3540 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003541 break;
3542 case 4:
3543 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3544 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003545 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003546 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003547 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003548 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003549 break;
3550 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003551 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003552 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003553 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003554 case 11:
3555 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003556 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003557 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003558 }
3559
3560 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003561 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003562 OldTy = TD->getUnderlyingType();
3563 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3564 OldTy = VD->getType();
3565 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003566 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003567 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003568 return;
3569 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003570
John McCall9dd450b2009-09-21 23:43:11 +00003571 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003572 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3573 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003574 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003575 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3576 } else if (ComplexMode) {
3577 if (!OldTy->isComplexType())
3578 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3579 } else {
3580 if (!OldTy->isFloatingType())
3581 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3582 }
3583
Mike Stump87c57ac2009-05-16 07:39:55 +00003584 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3585 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003586 // FIXME: Make sure floating-point mappings are accurate
3587 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003588 if (!DestWidth) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003589 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003590 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003591 }
3592
3593 QualType NewTy;
3594
3595 if (IntegerMode)
3596 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3597 OldTy->isSignedIntegerType());
3598 else
3599 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3600
3601 if (NewTy.isNull()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003602 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003603 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003604 }
3605
Eli Friedman4735374e2009-03-03 06:41:03 +00003606 if (ComplexMode) {
3607 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003608 }
3609
3610 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003611 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3612 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3613 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003614 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003615
3616 D->addAttr(::new (S.Context)
3617 ModeAttr(Attr.getRange(), S.Context, Name,
3618 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003619}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003620
Chandler Carruthedc2c642011-07-02 00:01:44 +00003621static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003622 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3623 if (!VD->hasGlobalStorage())
3624 S.Diag(Attr.getLoc(),
3625 diag::warn_attribute_requires_functions_or_static_globals)
3626 << Attr.getName();
3627 } else if (!isFunctionOrMethod(D)) {
3628 S.Diag(Attr.getLoc(),
3629 diag::warn_attribute_requires_functions_or_static_globals)
3630 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003631 return;
3632 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003633
Michael Han99315932013-01-24 16:46:58 +00003634 D->addAttr(::new (S.Context)
3635 NoDebugAttr(Attr.getRange(), S.Context,
3636 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003637}
3638
Chandler Carruthedc2c642011-07-02 00:01:44 +00003639static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003640 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00003641 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003642 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00003643 return;
3644 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003645
Michael Han99315932013-01-24 16:46:58 +00003646 D->addAttr(::new (S.Context)
3647 NoInlineAttr(Attr.getRange(), S.Context,
3648 Attr.getAttributeSpellingListIndex()));
Anders Carlsson88097122009-02-19 19:16:48 +00003649}
3650
Chandler Carruthedc2c642011-07-02 00:01:44 +00003651static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3652 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003653 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003654 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003655 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00003656 return;
3657 }
3658
Michael Han99315932013-01-24 16:46:58 +00003659 D->addAttr(::new (S.Context)
3660 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3661 Attr.getAttributeSpellingListIndex()));
Chris Lattner3c77a352010-06-22 00:03:40 +00003662}
3663
Chandler Carruthedc2c642011-07-02 00:01:44 +00003664static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003665 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003666 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003667 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003668 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003669 return;
3670 }
3671
Michael Han99315932013-01-24 16:46:58 +00003672 D->addAttr(::new (S.Context)
3673 CUDAConstantAttr(Attr.getRange(), S.Context,
3674 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003675 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003676 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003677 }
3678}
3679
Chandler Carruthedc2c642011-07-02 00:01:44 +00003680static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003681 if (S.LangOpts.CUDA) {
3682 // check the attribute arguments.
3683 if (Attr.getNumArgs() != 0) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003684 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3685 << Attr.getName() << 0;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003686 return;
3687 }
3688
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003689 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003690 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003691 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003692 return;
3693 }
3694
Michael Han99315932013-01-24 16:46:58 +00003695 D->addAttr(::new (S.Context)
3696 CUDADeviceAttr(Attr.getRange(), S.Context,
3697 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003698 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003699 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003700 }
3701}
3702
Chandler Carruthedc2c642011-07-02 00:01:44 +00003703static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003704 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003705 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003706 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003707 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003708 return;
3709 }
3710
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003711 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003712 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00003713 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie6adc78e2013-02-18 22:06:02 +00003714 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003715 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3716 << FD->getType()
David Blaikie6adc78e2013-02-18 22:06:02 +00003717 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003718 "void");
3719 } else {
3720 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3721 << FD->getType();
3722 }
3723 return;
3724 }
3725
Michael Han99315932013-01-24 16:46:58 +00003726 D->addAttr(::new (S.Context)
3727 CUDAGlobalAttr(Attr.getRange(), S.Context,
3728 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003729 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003730 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003731 }
3732}
3733
Chandler Carruthedc2c642011-07-02 00:01:44 +00003734static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003735 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003736 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003737 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003738 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003739 return;
3740 }
3741
Michael Han99315932013-01-24 16:46:58 +00003742 D->addAttr(::new (S.Context)
3743 CUDAHostAttr(Attr.getRange(), S.Context,
3744 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003745 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003746 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003747 }
3748}
3749
Chandler Carruthedc2c642011-07-02 00:01:44 +00003750static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003751 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003752 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003753 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003754 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003755 return;
3756 }
3757
Michael Han99315932013-01-24 16:46:58 +00003758 D->addAttr(::new (S.Context)
3759 CUDASharedAttr(Attr.getRange(), S.Context,
3760 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003761 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003762 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003763 }
3764}
3765
Chandler Carruthedc2c642011-07-02 00:01:44 +00003766static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003767 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00003768 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003769 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003770 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00003771 return;
3772 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003773
Douglas Gregor35b57532009-10-27 21:01:01 +00003774 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003775 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003776 return;
3777 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003778
Michael Han99315932013-01-24 16:46:58 +00003779 D->addAttr(::new (S.Context)
3780 GNUInlineAttr(Attr.getRange(), S.Context,
3781 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003782}
3783
Chandler Carruthedc2c642011-07-02 00:01:44 +00003784static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003785 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003786
Aaron Ballman02df2e02012-12-09 17:45:41 +00003787 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003788 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003789 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3790 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003791 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003792 return;
3793
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003794 if (!isa<ObjCMethodDecl>(D)) {
3795 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3796 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003797 return;
3798 }
3799
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003800 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003801 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003802 D->addAttr(::new (S.Context)
3803 FastCallAttr(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_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003807 D->addAttr(::new (S.Context)
3808 StdCallAttr(Attr.getRange(), S.Context,
3809 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003810 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003811 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003812 D->addAttr(::new (S.Context)
3813 ThisCallAttr(Attr.getRange(), S.Context,
3814 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003815 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003816 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003817 D->addAttr(::new (S.Context)
3818 CDeclAttr(Attr.getRange(), S.Context,
3819 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003820 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003821 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003822 D->addAttr(::new (S.Context)
3823 PascalAttr(Attr.getRange(), S.Context,
3824 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003825 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003826 case AttributeList::AT_MSABI:
3827 D->addAttr(::new (S.Context)
3828 MSABIAttr(Attr.getRange(), S.Context,
3829 Attr.getAttributeSpellingListIndex()));
3830 return;
3831 case AttributeList::AT_SysVABI:
3832 D->addAttr(::new (S.Context)
3833 SysVABIAttr(Attr.getRange(), S.Context,
3834 Attr.getAttributeSpellingListIndex()));
3835 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003836 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003837 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003838 switch (CC) {
3839 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003840 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003841 break;
3842 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003843 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003844 break;
3845 default:
3846 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003847 }
3848
Michael Han99315932013-01-24 16:46:58 +00003849 D->addAttr(::new (S.Context)
3850 PcsAttr(Attr.getRange(), S.Context, PCS,
3851 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003852 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003853 }
Derek Schuffa2020962012-10-16 22:30:41 +00003854 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003855 D->addAttr(::new (S.Context)
3856 PnaclCallAttr(Attr.getRange(), S.Context,
3857 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003858 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003859 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003860 D->addAttr(::new (S.Context)
3861 IntelOclBiccAttr(Attr.getRange(), S.Context,
3862 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003863 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003864
Abramo Bagnara50099372010-04-30 13:10:51 +00003865 default:
3866 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003867 }
3868}
3869
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003870static void handleOpenCLKernelAttr(Sema &S, Decl *D,
3871 const AttributeList &Attr) {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003872 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003873}
3874
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003875static void handleOpenCLImageAccessAttr(Sema &S, Decl *D,
3876 const AttributeList &Attr) {
3877 uint32_t ArgNum;
3878 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), ArgNum))
Guy Benyeifb36ede2013-03-24 13:58:12 +00003879 return;
Guy Benyeifb36ede2013-03-24 13:58:12 +00003880
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003881 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(Attr.getRange(),
3882 S.Context, ArgNum));
Guy Benyeifb36ede2013-03-24 13:58:12 +00003883}
3884
Aaron Ballman02df2e02012-12-09 17:45:41 +00003885bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3886 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003887 if (attr.isInvalid())
3888 return true;
3889
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003890 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003891 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003892 attr.setInvalid();
3893 return true;
3894 }
3895
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003896 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3897 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003898 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003899 case AttributeList::AT_CDecl: CC = CC_C; break;
3900 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3901 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3902 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3903 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003904 case AttributeList::AT_MSABI:
3905 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3906 CC_X86_64Win64;
3907 break;
3908 case AttributeList::AT_SysVABI:
3909 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3910 CC_C;
3911 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003912 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003913 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003914 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003915 attr.setInvalid();
3916 return true;
3917 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003918 if (StrRef == "aapcs") {
3919 CC = CC_AAPCS;
3920 break;
3921 } else if (StrRef == "aapcs-vfp") {
3922 CC = CC_AAPCS_VFP;
3923 break;
3924 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003925
3926 attr.setInvalid();
3927 Diag(attr.getLoc(), diag::err_invalid_pcs);
3928 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003929 }
Derek Schuffa2020962012-10-16 22:30:41 +00003930 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003931 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003932 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003933 }
3934
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003935 const TargetInfo &TI = Context.getTargetInfo();
3936 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3937 if (A == TargetInfo::CCCR_Warning) {
3938 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003939
3940 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3941 if (FD)
3942 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3943 TargetInfo::CCMT_NonMember;
3944 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003945 }
3946
John McCall3882ace2011-01-05 12:14:39 +00003947 return false;
3948}
3949
Chandler Carruthedc2c642011-07-02 00:01:44 +00003950static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003951 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003952
3953 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003954 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003955 return;
3956
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003957 if (!isa<ObjCMethodDecl>(D)) {
3958 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3959 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003960 return;
3961 }
Eli Friedman7044b762009-03-27 21:06:47 +00003962
Michael Han99315932013-01-24 16:46:58 +00003963 D->addAttr(::new (S.Context)
3964 RegparmAttr(Attr.getRange(), S.Context, numParams,
3965 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00003966}
3967
3968/// Checks a regparm attribute, returning true if it is ill-formed and
3969/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003970bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3971 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003972 return true;
3973
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003974 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003975 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003976 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003977 }
Eli Friedman7044b762009-03-27 21:06:47 +00003978
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003979 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003980 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003981 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
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
Douglas Gregore8bbc122011-09-02 00:18:52 +00003986 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003987 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003988 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003989 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003990 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003991 }
3992
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003993 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003994 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003995 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003996 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003997 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003998 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003999 }
4000
John McCall3882ace2011-01-05 12:14:39 +00004001 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004002}
4003
Chandler Carruthedc2c642011-07-02 00:01:44 +00004004static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00004005 if (S.LangOpts.CUDA) {
4006 // check the attribute arguments.
4007 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00004008 // FIXME: 0 is not okay.
4009 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004010 return;
4011 }
4012
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004013 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00004014 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00004015 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004016 return;
4017 }
4018
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004019 uint32_t MaxThreads, MinBlocks;
4020 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1) ||
4021 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(1), MinBlocks, 2))
Peter Collingbourne827301e2010-12-12 23:03:07 +00004022 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004023
Michael Han99315932013-01-24 16:46:58 +00004024 D->addAttr(::new (S.Context)
4025 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004026 MaxThreads, MinBlocks,
Michael Han99315932013-01-24 16:46:58 +00004027 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00004028 } else {
4029 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4030 }
4031}
4032
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004033static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4034 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00004035 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00004036 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00004037 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004038 return;
4039 }
Aaron Ballman00e99962013-08-31 01:11:41 +00004040
4041 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004042 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004043
Aaron Ballman00e99962013-08-31 01:11:41 +00004044 StringRef AttrName = Attr.getName()->getName();
4045 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004046
4047 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4048 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4049 << Attr.getName() << ExpectedFunctionOrMethod;
4050 return;
4051 }
4052
4053 uint64_t ArgumentIdx;
4054 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4055 Attr.getLoc(), 2,
Aaron Ballman00e99962013-08-31 01:11:41 +00004056 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004057 return;
4058
4059 uint64_t TypeTagIdx;
4060 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4061 Attr.getLoc(), 3,
Aaron Ballman00e99962013-08-31 01:11:41 +00004062 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004063 return;
4064
4065 bool IsPointer = (AttrName == "pointer_with_type_tag");
4066 if (IsPointer) {
4067 // Ensure that buffer has a pointer type.
4068 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4069 if (!BufferTy->isPointerType()) {
4070 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00004071 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004072 }
4073 }
4074
Michael Han99315932013-01-24 16:46:58 +00004075 D->addAttr(::new (S.Context)
4076 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4077 ArgumentIdx, TypeTagIdx, IsPointer,
4078 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004079}
4080
4081static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4082 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00004083 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00004084 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00004085 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004086 return;
4087 }
Aaron Ballman00e99962013-08-31 01:11:41 +00004088
4089 if (!checkAttributeNumArgs(S, Attr, 1))
4090 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004091
Aaron Ballman00e99962013-08-31 01:11:41 +00004092 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00004093 TypeSourceInfo *MatchingCTypeLoc = 0;
4094 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
4095 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004096
Michael Han99315932013-01-24 16:46:58 +00004097 D->addAttr(::new (S.Context)
4098 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00004099 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00004100 Attr.getLayoutCompatible(),
4101 Attr.getMustBeNull(),
4102 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004103}
4104
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004105//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004106// Checker-specific attribute handlers.
4107//===----------------------------------------------------------------------===//
4108
John McCalled433932011-01-25 03:31:58 +00004109static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004110 return type->isDependentType() ||
4111 type->isObjCObjectPointerType() ||
4112 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00004113}
4114static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004115 return type->isDependentType() ||
4116 type->isPointerType() ||
4117 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00004118}
4119
Chandler Carruthedc2c642011-07-02 00:01:44 +00004120static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004121 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00004122 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004123 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004124 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00004125 return;
4126 }
4127
4128 bool typeOK, cf;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004129 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00004130 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4131 cf = false;
4132 } else {
4133 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4134 cf = true;
4135 }
4136
4137 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004138 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004139 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00004140 return;
4141 }
4142
4143 if (cf)
Michael Han99315932013-01-24 16:46:58 +00004144 param->addAttr(::new (S.Context)
4145 CFConsumedAttr(Attr.getRange(), S.Context,
4146 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004147 else
Michael Han99315932013-01-24 16:46:58 +00004148 param->addAttr(::new (S.Context)
4149 NSConsumedAttr(Attr.getRange(), S.Context,
4150 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004151}
4152
Chandler Carruthedc2c642011-07-02 00:01:44 +00004153static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4154 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004155 if (!isa<ObjCMethodDecl>(D)) {
4156 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004157 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00004158 return;
4159 }
4160
Michael Han99315932013-01-24 16:46:58 +00004161 D->addAttr(::new (S.Context)
4162 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4163 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004164}
4165
Chandler Carruthedc2c642011-07-02 00:01:44 +00004166static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4167 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004168
John McCalled433932011-01-25 03:31:58 +00004169 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004170
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004171 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004172 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004173 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004174 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004175 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00004176 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4177 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004178 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004179 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00004180 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004181 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004182 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00004183 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004184 return;
4185 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004186
John McCalled433932011-01-25 03:31:58 +00004187 bool typeOK;
4188 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004189 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004190 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004191 case AttributeList::AT_NSReturnsAutoreleased:
4192 case AttributeList::AT_NSReturnsRetained:
4193 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004194 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4195 cf = false;
4196 break;
4197
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004198 case AttributeList::AT_CFReturnsRetained:
4199 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004200 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4201 cf = true;
4202 break;
4203 }
4204
4205 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004206 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004207 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004208 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004209 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004210
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004211 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004212 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004213 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004214 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00004215 D->addAttr(::new (S.Context)
4216 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4217 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004218 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004219 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004220 D->addAttr(::new (S.Context)
4221 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4222 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004223 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004224 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004225 D->addAttr(::new (S.Context)
4226 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4227 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004228 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004229 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004230 D->addAttr(::new (S.Context)
4231 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4232 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004233 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004234 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004235 D->addAttr(::new (S.Context)
4236 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4237 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004238 return;
4239 };
4240}
4241
John McCallcf166702011-07-22 08:53:00 +00004242static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4243 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00004244 const int EP_ObjCMethod = 1;
4245 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004246
John McCallcf166702011-07-22 08:53:00 +00004247 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004248 QualType resultType;
4249
John McCallcf166702011-07-22 08:53:00 +00004250 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4251
Fariborz Jahaniana53e5d72012-04-21 17:51:44 +00004252 if (!method) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004253 ObjCPropertyDecl *property = dyn_cast<ObjCPropertyDecl>(D);
4254 if (!property) {
4255 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4256 << SourceRange(loc, loc) << attr.getName() << ExpectedMethodOrProperty;
4257 return;
4258 }
4259 resultType = property->getType();
John McCallcf166702011-07-22 08:53:00 +00004260 }
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004261 else
4262 // Check that the method returns a normal pointer.
4263 resultType = method->getResultType();
John McCallcf166702011-07-22 08:53:00 +00004264
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004265 if (!resultType->isReferenceType() &&
4266 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004267 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00004268 << SourceRange(loc)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004269 << attr.getName() << (method ? EP_ObjCMethod : EP_ObjCProperty)
4270 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00004271
4272 // Drop the attribute.
4273 return;
4274 }
4275
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004276 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004277 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4278 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004279}
4280
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004281static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4282 const AttributeList &attr) {
4283 SourceLocation loc = attr.getLoc();
4284 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4285
4286 if (!method) {
4287 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4288 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4289 return;
4290 }
4291 DeclContext *DC = method->getDeclContext();
4292 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4293 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4294 << attr.getName() << 0;
4295 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4296 return;
4297 }
4298 if (method->getMethodFamily() == OMF_dealloc) {
4299 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4300 << attr.getName() << 1;
4301 return;
4302 }
4303
Michael Han99315932013-01-24 16:46:58 +00004304 method->addAttr(::new (S.Context)
4305 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4306 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004307}
4308
John McCall32f5fe12011-09-30 05:12:12 +00004309/// Handle cf_audited_transfer and cf_unknown_transfer.
4310static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4311 if (!isa<FunctionDecl>(D)) {
4312 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004313 << A.getRange() << A.getName() << ExpectedFunction;
John McCall32f5fe12011-09-30 05:12:12 +00004314 return;
4315 }
4316
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004317 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall32f5fe12011-09-30 05:12:12 +00004318
4319 // Check whether there's a conflicting attribute already present.
4320 Attr *Existing;
4321 if (IsAudited) {
4322 Existing = D->getAttr<CFUnknownTransferAttr>();
4323 } else {
4324 Existing = D->getAttr<CFAuditedTransferAttr>();
4325 }
4326 if (Existing) {
4327 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4328 << A.getName()
4329 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4330 << A.getRange() << Existing->getRange();
4331 return;
4332 }
4333
4334 // All clear; add the attribute.
4335 if (IsAudited) {
Michael Han99315932013-01-24 16:46:58 +00004336 D->addAttr(::new (S.Context)
4337 CFAuditedTransferAttr(A.getRange(), S.Context,
4338 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004339 } else {
Michael Han99315932013-01-24 16:46:58 +00004340 D->addAttr(::new (S.Context)
4341 CFUnknownTransferAttr(A.getRange(), S.Context,
4342 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004343 }
4344}
4345
John McCallf1e8b342011-09-29 07:17:38 +00004346static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4347 const AttributeList &Attr) {
4348 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4349 if (!RD || RD->isUnion()) {
4350 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004351 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00004352 }
4353
Aaron Ballman00e99962013-08-31 01:11:41 +00004354 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallf1e8b342011-09-29 07:17:38 +00004355
4356 // In Objective-C, verify that the type names an Objective-C type.
4357 // We don't want to check this outside of ObjC because people sometimes
4358 // do crazy C declarations of Objective-C types.
Aaron Ballman00e99962013-08-31 01:11:41 +00004359 if (Parm && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00004360 // Check for an existing type with this name.
Aaron Ballman00e99962013-08-31 01:11:41 +00004361 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallf1e8b342011-09-29 07:17:38 +00004362 Sema::LookupOrdinaryName);
4363 if (S.LookupName(R, Sc)) {
4364 NamedDecl *Target = R.getFoundDecl();
4365 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4366 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4367 S.Diag(Target->getLocStart(), diag::note_declared_at);
4368 }
4369 }
4370 }
4371
Michael Han99315932013-01-24 16:46:58 +00004372 D->addAttr(::new (S.Context)
Aaron Ballman00e99962013-08-31 01:11:41 +00004373 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han99315932013-01-24 16:46:58 +00004374 Attr.getAttributeSpellingListIndex()));
John McCallf1e8b342011-09-29 07:17:38 +00004375}
4376
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004377static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
4378 const AttributeList &Attr) {
Fariborz Jahaniandb3d8552013-11-19 00:09:48 +00004379 if (!isa<RecordDecl>(D)) {
Fariborz Jahanianf720f862013-11-19 17:42:25 +00004380 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4381 << Attr.getName()
4382 << (S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass
4383 : ExpectedStructOrUnion);
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004384 return;
4385 }
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004386
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00004387 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004388
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004389 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004390 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004391 return;
4392 }
4393
4394 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004395 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004396 Attr.getAttributeSpellingListIndex()));
4397}
4398
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004399static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
4400 const AttributeList &Attr) {
4401 if (!isa<RecordDecl>(D)) {
4402 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4403 << Attr.getName()
4404 << (S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass
4405 : ExpectedStructOrUnion);
4406 return;
4407 }
4408
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00004409 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004410
4411 if (!Parm) {
4412 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
4413 return;
4414 }
4415
4416 D->addAttr(::new (S.Context)
4417 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
4418 Attr.getAttributeSpellingListIndex()));
4419}
4420
Chandler Carruthedc2c642011-07-02 00:01:44 +00004421static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4422 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004423 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004424
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004425 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004426 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004427}
4428
Chandler Carruthedc2c642011-07-02 00:01:44 +00004429static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4430 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004431 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004432 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004433 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004434 return;
4435 }
4436
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004437 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004438 QualType type = vd->getType();
4439
4440 if (!type->isDependentType() &&
4441 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004442 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004443 << type;
4444 return;
4445 }
4446
4447 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4448
4449 // If we have no lifetime yet, check the lifetime we're presumably
4450 // going to infer.
4451 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4452 lifetime = type->getObjCARCImplicitLifetime();
4453
4454 switch (lifetime) {
4455 case Qualifiers::OCL_None:
4456 assert(type->isDependentType() &&
4457 "didn't infer lifetime for non-dependent type?");
4458 break;
4459
4460 case Qualifiers::OCL_Weak: // meaningful
4461 case Qualifiers::OCL_Strong: // meaningful
4462 break;
4463
4464 case Qualifiers::OCL_ExplicitNone:
4465 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004466 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004467 << (lifetime == Qualifiers::OCL_Autoreleasing);
4468 break;
4469 }
4470
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004471 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004472 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4473 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004474}
4475
Francois Picheta83957a2010-12-19 06:50:37 +00004476//===----------------------------------------------------------------------===//
4477// Microsoft specific attribute handlers.
4478//===----------------------------------------------------------------------===//
4479
Reid Kleckner140c4a72013-05-17 14:04:52 +00004480// Check if MS extensions or some other language extensions are enabled. If
4481// not, issue a diagnostic that the given attribute is unused.
4482static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4483 bool OtherExtension = false) {
4484 if (S.LangOpts.MicrosoftExt || OtherExtension)
4485 return true;
4486 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4487 return false;
4488}
4489
Chandler Carruthedc2c642011-07-02 00:01:44 +00004490static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00004491 if (!S.LangOpts.CPlusPlus) {
4492 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4493 << Attr.getName() << AttributeLangSupport::C;
4494 return;
4495 }
4496
Reid Kleckner140c4a72013-05-17 14:04:52 +00004497 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4498 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004499
Aaron Ballman60e705e2013-11-24 20:58:02 +00004500 if (!isa<CXXRecordDecl>(D)) {
4501 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4502 << Attr.getName() << ExpectedClass;
4503 return;
4504 }
4505
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004506 StringRef StrRef;
4507 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00004508 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00004509 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004510
David Majnemer89085342013-08-09 08:56:20 +00004511 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4512 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00004513 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4514 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00004515
Reid Kleckner140c4a72013-05-17 14:04:52 +00004516 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00004517 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004518 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004519 return;
4520 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004521
David Majnemer89085342013-08-09 08:56:20 +00004522 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004523 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00004524 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004525 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00004526 return;
4527 }
David Majnemer89085342013-08-09 08:56:20 +00004528 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004529 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004530 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004531 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004532 }
Francois Picheta83957a2010-12-19 06:50:37 +00004533
David Majnemer89085342013-08-09 08:56:20 +00004534 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4535 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004536}
4537
John McCall8d32c052012-05-22 21:28:12 +00004538static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004539 if (!checkMicrosoftExt(S, Attr))
Nico Weberefa45b22012-11-07 21:31:36 +00004540 return;
Nico Weberefa45b22012-11-07 21:31:36 +00004541
4542 AttributeList::Kind Kind = Attr.getKind();
4543 if (Kind == AttributeList::AT_SingleInheritance)
4544 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004545 ::new (S.Context)
4546 SingleInheritanceAttr(Attr.getRange(), S.Context,
4547 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004548 else if (Kind == AttributeList::AT_MultipleInheritance)
4549 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004550 ::new (S.Context)
4551 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4552 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004553 else if (Kind == AttributeList::AT_VirtualInheritance)
4554 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004555 ::new (S.Context)
4556 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4557 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004558}
4559
4560static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004561 if (!checkMicrosoftExt(S, Attr))
4562 return;
4563
4564 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmancc14f3a2013-11-22 21:49:04 +00004565 if (Kind == AttributeList::AT_Win64)
4566 D->addAttr(
4567 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4568 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004569}
4570
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004571static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004572 if (!checkMicrosoftExt(S, Attr))
4573 return;
4574 D->addAttr(::new (S.Context)
4575 ForceInlineAttr(Attr.getRange(), S.Context,
4576 Attr.getAttributeSpellingListIndex()));
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004577}
4578
Reid Klecknerb144d362013-05-20 14:02:37 +00004579static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4580 if (!checkMicrosoftExt(S, Attr))
4581 return;
4582 // Check linkage after possibly merging declaratinos. See
4583 // checkAttributesAfterMerging().
4584 D->addAttr(::new (S.Context)
4585 SelectAnyAttr(Attr.getRange(), S.Context,
4586 Attr.getAttributeSpellingListIndex()));
4587}
4588
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004589/// Handles semantic checking for features that are common to all attributes,
4590/// such as checking whether a parameter was properly specified, or the correct
4591/// number of arguments were passed, etc.
4592static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4593 const AttributeList &Attr) {
4594 // Several attributes carry different semantics than the parsing requires, so
4595 // those are opted out of the common handling.
4596 //
4597 // We also bail on unknown and ignored attributes because those are handled
4598 // as part of the target-specific handling logic.
4599 if (Attr.hasCustomParsing() ||
4600 Attr.getKind() == AttributeList::UnknownAttribute ||
4601 Attr.getKind() == AttributeList::IgnoredAttribute)
4602 return false;
4603
4604 // If there are no optional arguments, then checking for the argument count
4605 // is trivial.
4606 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
4607 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4608 return true;
4609 return false;
4610}
4611
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004612//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004613// Top Level Sema Entry Points
4614//===----------------------------------------------------------------------===//
4615
Richard Smithf8a75c32013-08-29 00:47:48 +00004616/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4617/// the attribute applies to decls. If the attribute is a type attribute, just
4618/// silently ignore it if a GNU attribute.
4619static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4620 const AttributeList &Attr,
4621 bool IncludeCXX11Attributes) {
4622 if (Attr.isInvalid())
4623 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004624
Richard Smithf8a75c32013-08-29 00:47:48 +00004625 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4626 // instead.
4627 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4628 return;
4629
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004630 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4631 return;
4632
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004633 switch (Attr.getKind()) {
Richard Smith10876ef2013-01-17 01:30:42 +00004634 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4635 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4636 case AttributeList::AT_IBOutletCollection:
4637 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004638 case AttributeList::AT_AddressSpace:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004639 case AttributeList::AT_ObjCGC:
4640 case AttributeList::AT_VectorSize:
4641 case AttributeList::AT_NeonVectorType:
4642 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004643 case AttributeList::AT_Ptr32:
4644 case AttributeList::AT_Ptr64:
4645 case AttributeList::AT_SPtr:
4646 case AttributeList::AT_UPtr:
Mike Stumpd3bb5572009-07-24 19:02:52 +00004647 // Ignore these, these are type attributes, handled by
4648 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004649 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004650 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4651 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4652 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4653 case AttributeList::AT_AlwaysInline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004654 handleAlwaysInlineAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004655 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004656 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00004657 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004658 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4659 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4660 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004661 handleDependencyAttr(S, scope, D, Attr);
4662 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004663 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4664 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4665 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004666 case AttributeList::AT_CXX11NoReturn:
4667 handleCXX11NoReturnAttr(S, D, Attr);
4668 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004669 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004670 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004671 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004672 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4673 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004674 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004675 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004676 case AttributeList::AT_MinSize:
4677 handleMinSizeAttr(S, D, Attr);
4678 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004679 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4680 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4681 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman9a226212013-08-28 23:13:26 +00004682 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4683 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004684 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4685 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004686 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004687 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004688 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4689 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00004690 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004691 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4692 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00004693 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00004694 case AttributeList::AT_ownership_returns:
4695 case AttributeList::AT_ownership_takes:
4696 case AttributeList::AT_ownership_holds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004697 handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004698 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4699 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4700 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4701 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4702 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4703 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4704 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004705
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004706 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004707 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004708 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004709 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004710
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004711 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004712 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4713
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004714 case AttributeList::AT_ObjCRequiresSuper:
4715 handleObjCRequiresSuperAttr(S, D, Attr); break;
4716
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004717 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00004718 handleNSBridgedAttr(S, scope, D, Attr); break;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004719
4720 case AttributeList::AT_ObjCBridge:
4721 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004722
4723 case AttributeList::AT_ObjCBridgeMutable:
4724 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00004725
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004726 case AttributeList::AT_CFAuditedTransfer:
4727 case AttributeList::AT_CFUnknownTransfer:
John McCall32f5fe12011-09-30 05:12:12 +00004728 handleCFTransferAttr(S, D, Attr); break;
4729
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004730 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004731 case AttributeList::AT_CFConsumed:
4732 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4733 case AttributeList::AT_NSConsumesSelf:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004734 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004735
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004736 case AttributeList::AT_NSReturnsAutoreleased:
4737 case AttributeList::AT_NSReturnsNotRetained:
4738 case AttributeList::AT_CFReturnsNotRetained:
4739 case AttributeList::AT_NSReturnsRetained:
4740 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004741 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004742
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004743 case AttributeList::AT_WorkGroupSizeHint:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004744 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004745 handleWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00004746
Joey Goulyaba589c2013-03-08 09:42:32 +00004747 case AttributeList::AT_VecTypeHint:
4748 handleVecTypeHint(S, D, Attr); break;
4749
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004750 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004751 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004752
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004753 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4754 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4755 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004756 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004757 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004758 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00004759 handleArcWeakrefUnavailableAttr (S, D, Attr);
4760 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004761 case AttributeList::AT_ObjCRootClass:
Patrick Beardacfbe9e2012-04-06 18:12:22 +00004762 handleObjCRootClassAttr(S, D, Attr);
4763 break;
Ted Kremenek28eace62013-11-23 01:01:34 +00004764 case AttributeList::AT_ObjCSuppressProtocol:
4765 handleObjCSuppresProtocolAttr(S, D, Attr);
4766 break;
4767 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00004768 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00004769 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004770 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4771 case AttributeList::AT_ReturnsTwice:
Rafael Espindola70107f92011-10-03 14:59:42 +00004772 handleReturnsTwiceAttr(S, D, Attr);
4773 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004774 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004775 case AttributeList::AT_Visibility:
4776 handleVisibilityAttr(S, D, Attr, false);
4777 break;
4778 case AttributeList::AT_TypeVisibility:
4779 handleVisibilityAttr(S, D, Attr, true);
4780 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004781 case AttributeList::AT_WarnUnused:
4782 handleWarnUnusedAttr(S, D, Attr);
4783 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004784 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004785 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004786 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4787 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4788 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4789 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004790 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004791 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004792 case AttributeList::AT_ObjCException:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004793 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00004794 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004795 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004796 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004797 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004798 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4799 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4800 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4801 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4802 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4803 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4804 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4805 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4806 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004807 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004808 // Just ignore
4809 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004810 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00004811 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00004812 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004813 case AttributeList::AT_StdCall:
4814 case AttributeList::AT_CDecl:
4815 case AttributeList::AT_FastCall:
4816 case AttributeList::AT_ThisCall:
4817 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004818 case AttributeList::AT_MSABI:
4819 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004820 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004821 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004822 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004823 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004824 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004825 case AttributeList::AT_OpenCLKernel:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004826 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00004827 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004828 case AttributeList::AT_OpenCLImageAccess:
4829 handleOpenCLImageAccessAttr(S, D, Attr);
4830 break;
John McCall8d32c052012-05-22 21:28:12 +00004831
4832 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004833 case AttributeList::AT_MsStruct:
John McCall8d32c052012-05-22 21:28:12 +00004834 handleMsStructAttr(S, D, Attr);
4835 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004836 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004837 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004838 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004839 case AttributeList::AT_SingleInheritance:
4840 case AttributeList::AT_MultipleInheritance:
4841 case AttributeList::AT_VirtualInheritance:
John McCall8d32c052012-05-22 21:28:12 +00004842 handleInheritanceAttr(S, D, Attr);
4843 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004844 case AttributeList::AT_Win64:
John McCall8d32c052012-05-22 21:28:12 +00004845 handlePortabilityAttr(S, D, Attr);
4846 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004847 case AttributeList::AT_ForceInline:
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004848 handleForceInlineAttr(S, D, Attr);
4849 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004850 case AttributeList::AT_SelectAny:
4851 handleSelectAnyAttr(S, D, Attr);
4852 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004853
4854 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004855 case AttributeList::AT_AssertExclusiveLock:
4856 handleAssertExclusiveLockAttr(S, D, Attr);
4857 break;
4858 case AttributeList::AT_AssertSharedLock:
4859 handleAssertSharedLockAttr(S, D, Attr);
4860 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004861 case AttributeList::AT_GuardedVar:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004862 handleGuardedVarAttr(S, D, Attr);
4863 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004864 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004865 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004866 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004867 case AttributeList::AT_ScopedLockable:
Michael Han3be3b442012-07-23 18:48:41 +00004868 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004869 break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004870 case AttributeList::AT_NoSanitizeAddress:
4871 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004872 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004873 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004874 handleNoThreadSafetyAnalysis(S, D, Attr);
4875 break;
4876 case AttributeList::AT_NoSanitizeThread:
4877 handleNoSanitizeThread(S, D, Attr);
4878 break;
4879 case AttributeList::AT_NoSanitizeMemory:
4880 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004881 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004882 case AttributeList::AT_Lockable:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004883 handleLockableAttr(S, D, Attr);
4884 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004885 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004886 handleGuardedByAttr(S, D, Attr);
4887 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004888 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004889 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004890 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004891 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004892 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004893 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004894 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004895 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004896 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004897 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004898 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004899 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004900 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004901 handleLockReturnedAttr(S, D, Attr);
4902 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004903 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004904 handleLocksExcludedAttr(S, D, Attr);
4905 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004906 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004907 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004908 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004909 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004910 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004911 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004912 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004913 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004914 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004915 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004916 handleUnlockFunAttr(S, D, Attr);
4917 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004918 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004919 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004920 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004921 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004922 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004923 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004924
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004925 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004926 case AttributeList::AT_Consumable:
4927 handleConsumableAttr(S, D, Attr);
4928 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004929 case AttributeList::AT_CallableWhen:
4930 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004931 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004932 case AttributeList::AT_ParamTypestate:
4933 handleParamTypestateAttr(S, D, Attr);
4934 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004935 case AttributeList::AT_ReturnTypestate:
4936 handleReturnTypestateAttr(S, D, Attr);
4937 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004938 case AttributeList::AT_SetTypestate:
4939 handleSetTypestateAttr(S, D, Attr);
4940 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004941 case AttributeList::AT_TestTypestate:
4942 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004943 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004944
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004945 // Type safety attributes.
4946 case AttributeList::AT_ArgumentWithTypeTag:
4947 handleArgumentWithTypeTagAttr(S, D, Attr);
4948 break;
4949 case AttributeList::AT_TypeTagForDatatype:
4950 handleTypeTagForDatatypeAttr(S, D, Attr);
4951 break;
4952
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004953 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004954 // Ask target about the attribute.
4955 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4956 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004957 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4958 diag::warn_unhandled_ms_attribute_ignored :
4959 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004960 break;
4961 }
4962}
4963
4964/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4965/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004966void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004967 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004968 bool IncludeCXX11Attributes) {
4969 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004970 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004971
4972 // GCC accepts
4973 // static int a9 __attribute__((weakref));
4974 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004975 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004976 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00004977 cast<NamedDecl>(D)->getNameAsString();
4978 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004979 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004980 }
4981}
4982
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004983// Annotation attributes are the only attributes allowed after an access
4984// specifier.
4985bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4986 const AttributeList *AttrList) {
4987 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004988 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004989 handleAnnotateAttr(*this, ASDecl, *l);
4990 } else {
4991 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4992 return true;
4993 }
4994 }
4995
4996 return false;
4997}
4998
John McCall42856de2011-10-01 05:17:03 +00004999/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5000/// contains any decl attributes that we should warn about.
5001static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5002 for ( ; A; A = A->getNext()) {
5003 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00005004 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00005005 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5006
5007 if (A->getKind() == AttributeList::UnknownAttribute) {
5008 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5009 << A->getName() << A->getRange();
5010 } else {
5011 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5012 << A->getName() << A->getRange();
5013 }
5014 }
5015}
5016
5017/// checkUnusedDeclAttributes - Given a declarator which is not being
5018/// used to build a declaration, complain about any decl attributes
5019/// which might be lying around on it.
5020void Sema::checkUnusedDeclAttributes(Declarator &D) {
5021 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5022 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5023 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5024 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5025}
5026
Ryan Flynn7d470f32009-07-30 03:15:39 +00005027/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00005028/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00005029NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5030 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00005031 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005032 NamedDecl *NewD = 0;
5033 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00005034 FunctionDecl *NewFD;
5035 // FIXME: Missing call to CheckFunctionDeclaration().
5036 // FIXME: Mangling?
5037 // FIXME: Is the qualifier info correct?
5038 // FIXME: Is the DeclContext correct?
5039 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5040 Loc, Loc, DeclarationName(II),
5041 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005042 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00005043 FD->hasPrototype(),
5044 false/*isConstexprSpecified*/);
5045 NewD = NewFD;
5046
5047 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00005048 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00005049
5050 // Fake up parameter variables; they are declared as if this were
5051 // a typedef.
5052 QualType FDTy = FD->getType();
5053 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5054 SmallVector<ParmVarDecl*, 16> Params;
5055 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5056 AE = FT->arg_type_end(); AI != AE; ++AI) {
5057 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5058 Param->setScopeInfo(0, Params.size());
5059 Params.push_back(Param);
5060 }
David Blaikie9c70e042011-09-21 18:16:56 +00005061 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00005062 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005063 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5064 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00005065 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00005066 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005067 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00005068 if (VD->getQualifier()) {
5069 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00005070 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00005071 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005072 }
5073 return NewD;
5074}
5075
James Dennett634962f2012-06-14 21:40:34 +00005076/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00005077/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00005078void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00005079 if (W.getUsed()) return; // only do this once
5080 W.setUsed(true);
5081 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5082 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00005083 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005084 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5085 NDId->getName()));
5086 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00005087 WeakTopLevelDecl.push_back(NewD);
5088 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5089 // to insert Decl at TU scope, sorry.
5090 DeclContext *SavedContext = CurContext;
5091 CurContext = Context.getTranslationUnitDecl();
5092 PushOnScopeChains(NewD, S);
5093 CurContext = SavedContext;
5094 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005095 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005096 }
5097}
5098
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005099void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5100 // It's valid to "forward-declare" #pragma weak, in which case we
5101 // have to do this.
5102 LoadExternalWeakUndeclaredIdentifiers();
5103 if (!WeakUndeclaredIdentifiers.empty()) {
5104 NamedDecl *ND = NULL;
5105 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5106 if (VD->isExternC())
5107 ND = VD;
5108 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5109 if (FD->isExternC())
5110 ND = FD;
5111 if (ND) {
5112 if (IdentifierInfo *Id = ND->getIdentifier()) {
5113 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5114 = WeakUndeclaredIdentifiers.find(Id);
5115 if (I != WeakUndeclaredIdentifiers.end()) {
5116 WeakInfo W = I->second;
5117 DeclApplyPragmaWeak(S, ND, W);
5118 WeakUndeclaredIdentifiers[Id] = W;
5119 }
5120 }
5121 }
5122 }
5123}
5124
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005125/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5126/// it, apply them to D. This is a bit tricky because PD can have attributes
5127/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00005128void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005129 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00005130 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00005131 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005132
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005133 // Walk the declarator structure, applying decl attributes that were in a type
5134 // position to the decl itself. This handles cases like:
5135 // int *__attr__(x)** D;
5136 // when X is a decl attribute.
5137 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5138 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00005139 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005140
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005141 // Finally, apply any attributes on the decl itself.
5142 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00005143 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005144}
John McCall28a6aea2009-11-04 02:18:39 +00005145
John McCall31168b02011-06-15 23:02:42 +00005146/// Is the given declaration allowed to use a forbidden type?
5147static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5148 // Private ivars are always okay. Unfortunately, people don't
5149 // always properly make their ivars private, even in system headers.
5150 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005151 // Function declarations in sys headers will be marked unavailable.
5152 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5153 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005154 return false;
5155
5156 // Require it to be declared in a system header.
5157 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5158}
5159
5160/// Handle a delayed forbidden-type diagnostic.
5161static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5162 Decl *decl) {
5163 if (decl && isForbiddenTypeAllowed(S, decl)) {
5164 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5165 "this system declaration uses an unsupported type"));
5166 return;
5167 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005168 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005169 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005170 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005171 // kind of forbidden type messages on unavailable functions.
5172 if (FD->hasAttr<UnavailableAttr>() &&
5173 diag.getForbiddenTypeDiagnostic() ==
5174 diag::err_arc_array_param_no_ownership) {
5175 diag.Triggered = true;
5176 return;
5177 }
5178 }
John McCall31168b02011-06-15 23:02:42 +00005179
5180 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5181 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5182 diag.Triggered = true;
5183}
5184
John McCall2ec85372012-05-07 06:16:41 +00005185void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5186 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005187 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005188 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005189
John McCall2ec85372012-05-07 06:16:41 +00005190 // When delaying diagnostics to run in the context of a parsed
5191 // declaration, we only want to actually emit anything if parsing
5192 // succeeds.
5193 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005194
John McCall2ec85372012-05-07 06:16:41 +00005195 // We emit all the active diagnostics in this pool or any of its
5196 // parents. In general, we'll get one pool for the decl spec
5197 // and a child pool for each declarator; in a decl group like:
5198 // deprecated_typedef foo, *bar, baz();
5199 // only the declarator pops will be passed decls. This is correct;
5200 // we really do need to consider delayed diagnostics from the decl spec
5201 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00005202 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00005203 do {
John McCall6347b682012-05-07 06:16:58 +00005204 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00005205 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5206 // This const_cast is a bit lame. Really, Triggered should be mutable.
5207 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00005208 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00005209 continue;
5210
John McCallc1465822011-02-14 07:13:47 +00005211 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00005212 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00005213 // Don't bother giving deprecation diagnostics if the decl is invalid.
5214 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00005215 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005216 break;
5217
5218 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00005219 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005220 break;
John McCall31168b02011-06-15 23:02:42 +00005221
5222 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00005223 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00005224 break;
John McCall86121512010-01-27 03:50:35 +00005225 }
5226 }
John McCall2ec85372012-05-07 06:16:41 +00005227 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00005228}
5229
John McCall6347b682012-05-07 06:16:58 +00005230/// Given a set of delayed diagnostics, re-emit them as if they had
5231/// been delayed in the current context instead of in the given pool.
5232/// Essentially, this just moves them to the current pool.
5233void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5234 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5235 assert(curPool && "re-emitting in undelayed context not supported");
5236 curPool->steal(pool);
5237}
5238
John McCall28a6aea2009-11-04 02:18:39 +00005239static bool isDeclDeprecated(Decl *D) {
5240 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005241 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00005242 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00005243 // A category implicitly has the availability of the interface.
5244 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5245 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00005246 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5247 return false;
5248}
5249
Eli Friedman971bfa12012-08-08 21:52:41 +00005250static void
5251DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5252 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005253 const ObjCInterfaceDecl *UnknownObjCClass,
5254 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedman971bfa12012-08-08 21:52:41 +00005255 DeclarationName Name = D->getDeclName();
5256 if (!Message.empty()) {
5257 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5258 S.Diag(D->getLocation(),
5259 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5260 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005261 if (ObjCPropery)
5262 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5263 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005264 } else if (!UnknownObjCClass) {
5265 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5266 S.Diag(D->getLocation(),
5267 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5268 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005269 if (ObjCPropery)
5270 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5271 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005272 } else {
5273 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5274 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5275 }
5276}
5277
John McCallb45a1e72010-08-26 02:13:20 +00005278void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00005279 Decl *Ctx) {
5280 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00005281 return;
5282
John McCall86121512010-01-27 03:50:35 +00005283 DD.Triggered = true;
Eli Friedman971bfa12012-08-08 21:52:41 +00005284 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5285 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005286 DD.getUnknownObjCClass(),
5287 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00005288}
5289
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005290void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00005291 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005292 const ObjCInterfaceDecl *UnknownObjCClass,
5293 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00005294 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00005295 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005296 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5297 UnknownObjCClass,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005298 ObjCProperty,
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005299 Message));
John McCall28a6aea2009-11-04 02:18:39 +00005300 return;
5301 }
5302
5303 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00005304 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00005305 return;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005306 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00005307}