blob: eff42954bf397a55a0c5e3354d002bcc8c7e2dbf [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
Richard Smith852e9ce2013-11-27 01:46:48 +00001549 // Figure out our Kind.
1550 OwnershipAttr::OwnershipKind K =
1551 OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0,
1552 AL.getAttributeSpellingListIndex()).getOwnKind();
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001553
Richard Smith852e9ce2013-11-27 01:46:48 +00001554 // Check arguments.
1555 switch (K) {
1556 case OwnershipAttr::Takes:
1557 case OwnershipAttr::Holds:
1558 if (AL.getNumArgs() < 2) {
1559 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
1560 return;
1561 }
1562 break;
1563 case OwnershipAttr::Returns:
Aaron Ballman00e99962013-08-31 01:11:41 +00001564 if (AL.getNumArgs() > 2) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001565 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001566 return;
1567 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001568 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001569 }
1570
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001571 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001572 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1573 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001574 return;
1575 }
1576
Richard Smith852e9ce2013-11-27 01:46:48 +00001577 IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001578
1579 // Normalize the argument, __foo__ becomes foo.
Richard Smith852e9ce2013-11-27 01:46:48 +00001580 StringRef ModuleName = Module->getName();
1581 if (ModuleName.startswith("__") && ModuleName.endswith("__") &&
1582 ModuleName.size() > 4) {
1583 ModuleName = ModuleName.drop_front(2).drop_back(2);
1584 Module = &S.PP.getIdentifierTable().get(ModuleName);
1585 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001586
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001587 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001588 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1589 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001590 uint64_t Idx;
1591 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
Aaron Ballman00e99962013-08-31 01:11:41 +00001592 AL.getLoc(), i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001593 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001594
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001595 // Is the function argument a pointer type?
1596 QualType T = getFunctionOrMethodArgType(D, Idx);
1597 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001598 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001599 case OwnershipAttr::Takes:
1600 case OwnershipAttr::Holds:
1601 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1602 Err = 0;
1603 break;
1604 case OwnershipAttr::Returns:
1605 if (!T->isIntegerType())
1606 Err = 1;
1607 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001608 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001609 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001610 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001611 << Ex->getSourceRange();
1612 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001613 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001614
1615 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001616 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001617 i = D->specific_attr_begin<OwnershipAttr>(),
1618 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
Richard Smith852e9ce2013-11-27 01:46:48 +00001619 // FIXME: A returns attribute should conflict with any returns attribute
1620 // with a different index too.
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001621 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1622 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1623 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1624 << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind());
1625 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001626 }
1627 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001628 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001629 }
1630
1631 unsigned* start = OwnershipArgs.data();
1632 unsigned size = OwnershipArgs.size();
1633 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001634
Michael Han99315932013-01-24 16:46:58 +00001635 D->addAttr(::new (S.Context)
Richard Smith852e9ce2013-11-27 01:46:48 +00001636 OwnershipAttr(AL.getLoc(), S.Context, Module, start, size,
Michael Han99315932013-01-24 16:46:58 +00001637 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001638}
1639
Chandler Carruthedc2c642011-07-02 00:01:44 +00001640static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001641 // Check the attribute arguments.
1642 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001643 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1644 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001645 return;
1646 }
1647
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001648 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall7a198ce2011-02-08 22:35:49 +00001649 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001650 << Attr.getName() << ExpectedVariableOrFunction;
John McCall7a198ce2011-02-08 22:35:49 +00001651 return;
1652 }
1653
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001654 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001655
Rafael Espindolac18086a2010-02-23 22:00:30 +00001656 // gcc rejects
1657 // class c {
1658 // static int a __attribute__((weakref ("v2")));
1659 // static int b() __attribute__((weakref ("f3")));
1660 // };
1661 // and ignores the attributes of
1662 // void f(void) {
1663 // static int a __attribute__((weakref ("v2")));
1664 // }
1665 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001666 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001667 if (!Ctx->isFileContext()) {
1668 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +00001669 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +00001670 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001671 }
1672
1673 // The GCC manual says
1674 //
1675 // At present, a declaration to which `weakref' is attached can only
1676 // be `static'.
1677 //
1678 // It also says
1679 //
1680 // Without a TARGET,
1681 // given as an argument to `weakref' or to `alias', `weakref' is
1682 // equivalent to `weak'.
1683 //
1684 // gcc 4.4.1 will accept
1685 // int a7 __attribute__((weakref));
1686 // as
1687 // int a7 __attribute__((weak));
1688 // This looks like a bug in gcc. We reject that for now. We should revisit
1689 // it if this behaviour is actually used.
1690
Rafael Espindolac18086a2010-02-23 22:00:30 +00001691 // GCC rejects
1692 // static ((alias ("y"), weakref)).
1693 // Should we? How to check that weakref is before or after alias?
1694
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001695 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1696 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1697 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001698 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001699 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001700 // GCC will accept anything as the argument of weakref. Should we
1701 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001702 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1703 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001704
Michael Han99315932013-01-24 16:46:58 +00001705 D->addAttr(::new (S.Context)
1706 WeakRefAttr(Attr.getRange(), S.Context,
1707 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001708}
1709
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001710static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1711 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001712 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001713 return;
1714
Douglas Gregore8bbc122011-09-02 00:18:52 +00001715 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001716 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1717 return;
1718 }
1719
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001720 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001721
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001722 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001723 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001724}
1725
Quentin Colombet4e172062012-11-01 23:55:47 +00001726static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Quentin Colombet4e172062012-11-01 23:55:47 +00001727 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1728 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1729 << Attr.getName() << ExpectedFunctionOrMethod;
1730 return;
1731 }
1732
Michael Han99315932013-01-24 16:46:58 +00001733 D->addAttr(::new (S.Context)
1734 MinSizeAttr(Attr.getRange(), S.Context,
1735 Attr.getAttributeSpellingListIndex()));
Quentin Colombet4e172062012-11-01 23:55:47 +00001736}
1737
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001738static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001739 if (!isa<FunctionDecl>(D)) {
1740 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1741 << Attr.getName() << ExpectedFunction;
1742 return;
1743 }
1744
1745 if (D->hasAttr<HotAttr>()) {
1746 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1747 << Attr.getName() << "hot";
1748 return;
1749 }
1750
Michael Han99315932013-01-24 16:46:58 +00001751 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1752 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001753}
1754
1755static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001756 if (!isa<FunctionDecl>(D)) {
1757 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1758 << Attr.getName() << ExpectedFunction;
1759 return;
1760 }
1761
1762 if (D->hasAttr<ColdAttr>()) {
1763 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1764 << Attr.getName() << "cold";
1765 return;
1766 }
1767
Michael Han99315932013-01-24 16:46:58 +00001768 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1769 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001770}
1771
Chandler Carruthedc2c642011-07-02 00:01:44 +00001772static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001773 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001774 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001775 << Attr.getName() << ExpectedFunction;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001776 return;
1777 }
1778
Michael Han99315932013-01-24 16:46:58 +00001779 D->addAttr(::new (S.Context)
1780 NakedAttr(Attr.getRange(), S.Context,
1781 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001782}
1783
Chandler Carruthedc2c642011-07-02 00:01:44 +00001784static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1785 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001786 if (!isa<FunctionDecl>(D)) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001787 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001788 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00001789 return;
1790 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001791
Michael Han99315932013-01-24 16:46:58 +00001792 D->addAttr(::new (S.Context)
1793 AlwaysInlineAttr(Attr.getRange(), S.Context,
1794 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar03a38442008-10-28 00:17:57 +00001795}
1796
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001797static void handleTLSModelAttr(Sema &S, Decl *D,
1798 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001799 StringRef Model;
1800 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001801 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001802 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001803 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001804
Richard Smithfd3834f2013-04-13 02:43:54 +00001805 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001806 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1807 << Attr.getName() << ExpectedTLSVar;
1808 return;
1809 }
1810
1811 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001812 if (Model != "global-dynamic" && Model != "local-dynamic"
1813 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001814 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001815 return;
1816 }
1817
Michael Han99315932013-01-24 16:46:58 +00001818 D->addAttr(::new (S.Context)
1819 TLSModelAttr(Attr.getRange(), S.Context, Model,
1820 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001821}
1822
Chandler Carruthedc2c642011-07-02 00:01:44 +00001823static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001824 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001825 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001826 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001827 D->addAttr(::new (S.Context)
1828 MallocAttr(Attr.getRange(), S.Context,
1829 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001830 return;
1831 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001832 }
1833
Ted Kremenek08479ae2009-08-15 00:51:46 +00001834 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001835}
1836
Chandler Carruthedc2c642011-07-02 00:01:44 +00001837static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han99315932013-01-24 16:46:58 +00001838 D->addAttr(::new (S.Context)
1839 MayAliasAttr(Attr.getRange(), S.Context,
1840 Attr.getAttributeSpellingListIndex()));
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001841}
1842
Chandler Carruthedc2c642011-07-02 00:01:44 +00001843static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001844 if (isa<VarDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001845 D->addAttr(::new (S.Context)
1846 NoCommonAttr(Attr.getRange(), S.Context,
1847 Attr.getAttributeSpellingListIndex()));
Eric Christopher515d87f2010-12-03 06:58:14 +00001848 else
1849 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001850 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001851}
1852
Chandler Carruthedc2c642011-07-02 00:01:44 +00001853static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001854 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001855 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1856 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001857 return;
1858 }
1859
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001860 if (isa<VarDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001861 D->addAttr(::new (S.Context)
1862 CommonAttr(Attr.getRange(), S.Context,
1863 Attr.getAttributeSpellingListIndex()));
Eric Christopher515d87f2010-12-03 06:58:14 +00001864 else
1865 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001866 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001867}
1868
Chandler Carruthedc2c642011-07-02 00:01:44 +00001869static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001870 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001871
1872 if (S.CheckNoReturnAttr(attr)) return;
1873
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001874 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001875 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001876 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001877 return;
1878 }
1879
Michael Han99315932013-01-24 16:46:58 +00001880 D->addAttr(::new (S.Context)
1881 NoReturnAttr(attr.getRange(), S.Context,
1882 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001883}
1884
1885bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001886 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001887 attr.setInvalid();
1888 return true;
1889 }
1890
1891 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001892}
1893
Chandler Carruthedc2c642011-07-02 00:01:44 +00001894static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1895 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001896
1897 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1898 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001899 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1900 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001901 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1902 && !VD->getType()->isFunctionPointerType())) {
1903 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001904 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001905 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001906 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001907 return;
1908 }
1909 }
1910
Michael Han99315932013-01-24 16:46:58 +00001911 D->addAttr(::new (S.Context)
1912 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1913 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001914}
1915
Richard Smith10876ef2013-01-17 01:30:42 +00001916static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1917 const AttributeList &Attr) {
1918 // C++11 [dcl.attr.noreturn]p1:
1919 // The attribute may be applied to the declarator-id in a function
1920 // declaration.
1921 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1922 if (!FD) {
1923 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1924 << Attr.getName() << ExpectedFunctionOrMethod;
1925 return;
1926 }
1927
Michael Han99315932013-01-24 16:46:58 +00001928 D->addAttr(::new (S.Context)
1929 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1930 Attr.getAttributeSpellingListIndex()));
Richard Smith10876ef2013-01-17 01:30:42 +00001931}
1932
John Thompsoncdb847ba2010-08-09 21:53:52 +00001933// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001934static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001935/*
1936 Returning a Vector Class in Registers
1937
Eric Christopherbc638a82010-12-01 22:13:54 +00001938 According to the PPU ABI specifications, a class with a single member of
1939 vector type is returned in memory when used as the return value of a function.
1940 This results in inefficient code when implementing vector classes. To return
1941 the value in a single vector register, add the vecreturn attribute to the
1942 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001943
1944 Example:
1945
1946 struct Vector
1947 {
1948 __vector float xyzw;
1949 } __attribute__((vecreturn));
1950
1951 Vector Add(Vector lhs, Vector rhs)
1952 {
1953 Vector result;
1954 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1955 return result; // This will be returned in a register
1956 }
1957*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001958 if (!isa<RecordDecl>(D)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001959 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001960 << Attr.getName() << ExpectedClass;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001961 return;
1962 }
1963
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001964 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001965 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1966 return;
1967 }
1968
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001969 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001970 int count = 0;
1971
1972 if (!isa<CXXRecordDecl>(record)) {
1973 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1974 return;
1975 }
1976
1977 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1978 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1979 return;
1980 }
1981
Eric Christopherbc638a82010-12-01 22:13:54 +00001982 for (RecordDecl::field_iterator iter = record->field_begin();
1983 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001984 if ((count == 1) || !iter->getType()->isVectorType()) {
1985 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1986 return;
1987 }
1988 count++;
1989 }
1990
Michael Han99315932013-01-24 16:46:58 +00001991 D->addAttr(::new (S.Context)
1992 VecReturnAttr(Attr.getRange(), S.Context,
1993 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001994}
1995
Richard Smithe233fbf2013-01-28 22:42:45 +00001996static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1997 const AttributeList &Attr) {
1998 if (isa<ParmVarDecl>(D)) {
1999 // [[carries_dependency]] can only be applied to a parameter if it is a
2000 // parameter of a function declaration or lambda.
2001 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
2002 S.Diag(Attr.getLoc(),
2003 diag::err_carries_dependency_param_not_function_decl);
2004 return;
2005 }
2006 } else if (!isa<FunctionDecl>(D)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002007 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002008 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002009 return;
2010 }
Richard Smithe233fbf2013-01-28 22:42:45 +00002011
2012 D->addAttr(::new (S.Context) CarriesDependencyAttr(
2013 Attr.getRange(), S.Context,
2014 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002015}
2016
Chandler Carruthedc2c642011-07-02 00:01:44 +00002017static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002018 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00002019 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002020 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002021 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00002022 return;
2023 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002024
Michael Han99315932013-01-24 16:46:58 +00002025 D->addAttr(::new (S.Context)
2026 UnusedAttr(Attr.getRange(), S.Context,
2027 Attr.getAttributeSpellingListIndex()));
Ted Kremenek39c59a82008-07-25 04:39:19 +00002028}
2029
Rafael Espindola70107f92011-10-03 14:59:42 +00002030static void handleReturnsTwiceAttr(Sema &S, Decl *D,
2031 const AttributeList &Attr) {
Rafael Espindola70107f92011-10-03 14:59:42 +00002032 if (!isa<FunctionDecl>(D)) {
2033 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2034 << Attr.getName() << ExpectedFunction;
2035 return;
2036 }
2037
Michael Han99315932013-01-24 16:46:58 +00002038 D->addAttr(::new (S.Context)
2039 ReturnsTwiceAttr(Attr.getRange(), S.Context,
2040 Attr.getAttributeSpellingListIndex()));
Rafael Espindola70107f92011-10-03 14:59:42 +00002041}
2042
Chandler Carruthedc2c642011-07-02 00:01:44 +00002043static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002044 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00002045 if (VD->hasLocalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002046 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
2047 return;
2048 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002049 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002050 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002051 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002052 return;
2053 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002054
Michael Han99315932013-01-24 16:46:58 +00002055 D->addAttr(::new (S.Context)
2056 UsedAttr(Attr.getRange(), S.Context,
2057 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002058}
2059
Chandler Carruthedc2c642011-07-02 00:01:44 +00002060static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00002061 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00002062 if (Attr.getNumArgs() > 1) {
2063 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00002064 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002065 }
Daniel Dunbar032db472008-07-31 22:40:48 +00002066
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002067 uint32_t priority = ConstructorAttr::DefaultPriority;
2068 if (Attr.getNumArgs() > 0 &&
2069 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
2070 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002071
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002072 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002073 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002074 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00002075 return;
2076 }
2077
Michael Han99315932013-01-24 16:46:58 +00002078 D->addAttr(::new (S.Context)
2079 ConstructorAttr(Attr.getRange(), S.Context, priority,
2080 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002081}
2082
Chandler Carruthedc2c642011-07-02 00:01:44 +00002083static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00002084 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00002085 if (Attr.getNumArgs() > 1) {
2086 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00002087 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002088 }
Daniel Dunbar032db472008-07-31 22:40:48 +00002089
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002090 uint32_t priority = ConstructorAttr::DefaultPriority;
2091 if (Attr.getNumArgs() > 0 &&
2092 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
2093 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002094
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002095 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002096 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002097 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00002098 return;
2099 }
2100
Michael Han99315932013-01-24 16:46:58 +00002101 D->addAttr(::new (S.Context)
2102 DestructorAttr(Attr.getRange(), S.Context, priority,
2103 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002104}
2105
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002106template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00002107static void handleAttrWithMessage(Sema &S, Decl *D,
2108 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00002109 unsigned NumArgs = Attr.getNumArgs();
2110 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00002111 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002112 return;
2113 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002114
2115 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002116 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002117 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002118 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002119
Michael Han99315932013-01-24 16:46:58 +00002120 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2121 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00002122}
2123
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00002124static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2125 const AttributeList &Attr) {
Aaron Ballmanf3614532013-11-24 20:36:50 +00002126 if (!isa<ObjCInterfaceDecl>(D)) {
2127 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2128 << Attr.getName() << ExpectedObjectiveCInterface;
2129 return;
2130 }
Michael Han99315932013-01-24 16:46:58 +00002131 D->addAttr(::new (S.Context)
2132 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2133 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00002134}
2135
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002136static void handleObjCRootClassAttr(Sema &S, Decl *D,
2137 const AttributeList &Attr) {
2138 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballmanf90ccb02013-07-18 14:56:42 +00002139 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2140 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002141 return;
2142 }
2143
Michael Han99315932013-01-24 16:46:58 +00002144 D->addAttr(::new (S.Context)
2145 ObjCRootClassAttr(Attr.getRange(), S.Context,
2146 Attr.getAttributeSpellingListIndex()));
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002147}
2148
Ted Kremenek28eace62013-11-23 01:01:34 +00002149static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
2150 const AttributeList &Attr) {
2151 if (!isa<ObjCInterfaceDecl>(D)) {
2152 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2153 << Attr.getName() << ExpectedObjectiveCInterface;
2154 return;
2155 }
2156
Ted Kremenek7559b472013-11-23 22:29:11 +00002157 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek28eace62013-11-23 01:01:34 +00002158
2159 if (!Parm) {
2160 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 1;
2161 return;
2162 }
2163
2164 D->addAttr(::new (S.Context)
2165 ObjCSuppressProtocolAttr(Attr.getRange(), S.Context, Parm->Ident,
2166 Attr.getAttributeSpellingListIndex()));
2167}
2168
2169
Michael Han99315932013-01-24 16:46:58 +00002170static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2171 const AttributeList &Attr) {
Fariborz Jahanian7249e362012-01-03 22:52:32 +00002172 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballmanf3614532013-11-24 20:36:50 +00002173 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2174 << Attr.getName() << ExpectedObjectiveCInterface;
Fariborz Jahanian7249e362012-01-03 22:52:32 +00002175 return;
2176 }
2177
Michael Han99315932013-01-24 16:46:58 +00002178 D->addAttr(::new (S.Context)
2179 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2180 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00002181}
2182
Jordy Rose740b0c22012-05-08 03:27:22 +00002183static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2184 IdentifierInfo *Platform,
2185 VersionTuple Introduced,
2186 VersionTuple Deprecated,
2187 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002188 StringRef PlatformName
2189 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2190 if (PlatformName.empty())
2191 PlatformName = Platform->getName();
2192
2193 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2194 // of these steps are needed).
2195 if (!Introduced.empty() && !Deprecated.empty() &&
2196 !(Introduced <= Deprecated)) {
2197 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2198 << 1 << PlatformName << Deprecated.getAsString()
2199 << 0 << Introduced.getAsString();
2200 return true;
2201 }
2202
2203 if (!Introduced.empty() && !Obsoleted.empty() &&
2204 !(Introduced <= Obsoleted)) {
2205 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2206 << 2 << PlatformName << Obsoleted.getAsString()
2207 << 0 << Introduced.getAsString();
2208 return true;
2209 }
2210
2211 if (!Deprecated.empty() && !Obsoleted.empty() &&
2212 !(Deprecated <= Obsoleted)) {
2213 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2214 << 2 << PlatformName << Obsoleted.getAsString()
2215 << 1 << Deprecated.getAsString();
2216 return true;
2217 }
2218
2219 return false;
2220}
2221
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002222/// \brief Check whether the two versions match.
2223///
2224/// If either version tuple is empty, then they are assumed to match. If
2225/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2226static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2227 bool BeforeIsOkay) {
2228 if (X.empty() || Y.empty())
2229 return true;
2230
2231 if (X == Y)
2232 return true;
2233
2234 if (BeforeIsOkay && X < Y)
2235 return true;
2236
2237 return false;
2238}
2239
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002240AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002241 IdentifierInfo *Platform,
2242 VersionTuple Introduced,
2243 VersionTuple Deprecated,
2244 VersionTuple Obsoleted,
2245 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002246 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00002247 bool Override,
2248 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002249 VersionTuple MergedIntroduced = Introduced;
2250 VersionTuple MergedDeprecated = Deprecated;
2251 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002252 bool FoundAny = false;
2253
Rafael Espindolac67f2232012-05-10 02:50:16 +00002254 if (D->hasAttrs()) {
2255 AttrVec &Attrs = D->getAttrs();
2256 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2257 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2258 if (!OldAA) {
2259 ++i;
2260 continue;
2261 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002262
Rafael Espindolac67f2232012-05-10 02:50:16 +00002263 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2264 if (OldPlatform != Platform) {
2265 ++i;
2266 continue;
2267 }
2268
2269 FoundAny = true;
2270 VersionTuple OldIntroduced = OldAA->getIntroduced();
2271 VersionTuple OldDeprecated = OldAA->getDeprecated();
2272 VersionTuple OldObsoleted = OldAA->getObsoleted();
2273 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002274
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002275 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2276 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2277 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2278 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00002279 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002280 if (Override) {
2281 int Which = -1;
2282 VersionTuple FirstVersion;
2283 VersionTuple SecondVersion;
2284 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2285 Which = 0;
2286 FirstVersion = OldIntroduced;
2287 SecondVersion = Introduced;
2288 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2289 Which = 1;
2290 FirstVersion = Deprecated;
2291 SecondVersion = OldDeprecated;
2292 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2293 Which = 2;
2294 FirstVersion = Obsoleted;
2295 SecondVersion = OldObsoleted;
2296 }
2297
2298 if (Which == -1) {
2299 Diag(OldAA->getLocation(),
2300 diag::warn_mismatched_availability_override_unavail)
2301 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2302 } else {
2303 Diag(OldAA->getLocation(),
2304 diag::warn_mismatched_availability_override)
2305 << Which
2306 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2307 << FirstVersion.getAsString() << SecondVersion.getAsString();
2308 }
2309 Diag(Range.getBegin(), diag::note_overridden_method);
2310 } else {
2311 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2312 Diag(Range.getBegin(), diag::note_previous_attribute);
2313 }
2314
Rafael Espindolac67f2232012-05-10 02:50:16 +00002315 Attrs.erase(Attrs.begin() + i);
2316 --e;
2317 continue;
2318 }
2319
2320 VersionTuple MergedIntroduced2 = MergedIntroduced;
2321 VersionTuple MergedDeprecated2 = MergedDeprecated;
2322 VersionTuple MergedObsoleted2 = MergedObsoleted;
2323
2324 if (MergedIntroduced2.empty())
2325 MergedIntroduced2 = OldIntroduced;
2326 if (MergedDeprecated2.empty())
2327 MergedDeprecated2 = OldDeprecated;
2328 if (MergedObsoleted2.empty())
2329 MergedObsoleted2 = OldObsoleted;
2330
2331 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2332 MergedIntroduced2, MergedDeprecated2,
2333 MergedObsoleted2)) {
2334 Attrs.erase(Attrs.begin() + i);
2335 --e;
2336 continue;
2337 }
2338
2339 MergedIntroduced = MergedIntroduced2;
2340 MergedDeprecated = MergedDeprecated2;
2341 MergedObsoleted = MergedObsoleted2;
2342 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002343 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002344 }
2345
2346 if (FoundAny &&
2347 MergedIntroduced == Introduced &&
2348 MergedDeprecated == Deprecated &&
2349 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002350 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002351
Ted Kremenekb5445722013-04-06 00:34:27 +00002352 // Only create a new attribute if !Override, but we want to do
2353 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002354 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002355 MergedDeprecated, MergedObsoleted) &&
2356 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002357 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2358 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002359 Obsoleted, IsUnavailable, Message,
2360 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002361 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002362 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002363}
2364
Chandler Carruthedc2c642011-07-02 00:01:44 +00002365static void handleAvailabilityAttr(Sema &S, Decl *D,
2366 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002367 if (!checkAttributeNumArgs(S, Attr, 1))
2368 return;
2369 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00002370 unsigned Index = Attr.getAttributeSpellingListIndex();
2371
Aaron Ballman00e99962013-08-31 01:11:41 +00002372 IdentifierInfo *II = Platform->Ident;
2373 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2374 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2375 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002376
Rafael Espindolac231fab2013-01-08 21:30:32 +00002377 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2378 if (!ND) {
2379 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2380 return;
2381 }
2382
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002383 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2384 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2385 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002386 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002387 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00002388 if (const StringLiteral *SE =
2389 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002390 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002391
Aaron Ballman00e99962013-08-31 01:11:41 +00002392 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002393 Introduced.Version,
2394 Deprecated.Version,
2395 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002396 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00002397 /*Override=*/false,
2398 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002399 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002400 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002401}
2402
John McCalld041a9b2013-02-20 01:54:26 +00002403template <class T>
2404static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2405 typename T::VisibilityType value,
2406 unsigned attrSpellingListIndex) {
2407 T *existingAttr = D->getAttr<T>();
2408 if (existingAttr) {
2409 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2410 if (existingValue == value)
2411 return NULL;
2412 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2413 S.Diag(range.getBegin(), diag::note_previous_attribute);
2414 D->dropAttr<T>();
2415 }
2416 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2417}
2418
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002419VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002420 VisibilityAttr::VisibilityType Vis,
2421 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002422 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2423 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002424}
2425
John McCalld041a9b2013-02-20 01:54:26 +00002426TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2427 TypeVisibilityAttr::VisibilityType Vis,
2428 unsigned AttrSpellingListIndex) {
2429 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2430 AttrSpellingListIndex);
2431}
2432
2433static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2434 bool isTypeVisibility) {
2435 // Visibility attributes don't mean anything on a typedef.
2436 if (isa<TypedefNameDecl>(D)) {
2437 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2438 << Attr.getName();
2439 return;
2440 }
2441
2442 // 'type_visibility' can only go on a type or namespace.
2443 if (isTypeVisibility &&
2444 !(isa<TagDecl>(D) ||
2445 isa<ObjCInterfaceDecl>(D) ||
2446 isa<NamespaceDecl>(D))) {
2447 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2448 << Attr.getName() << ExpectedTypeOrNamespace;
2449 return;
2450 }
2451
Benjamin Kramer70370212013-09-09 15:08:57 +00002452 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002453 StringRef TypeStr;
2454 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002455 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002456 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002457
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002458 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002459 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002460 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002461 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002462 return;
2463 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002464
2465 // Complain about attempts to use protected visibility on targets
2466 // (like Darwin) that don't support it.
2467 if (type == VisibilityAttr::Protected &&
2468 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2469 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2470 type = VisibilityAttr::Default;
2471 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002472
Michael Han99315932013-01-24 16:46:58 +00002473 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002474 clang::Attr *newAttr;
2475 if (isTypeVisibility) {
2476 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2477 (TypeVisibilityAttr::VisibilityType) type,
2478 Index);
2479 } else {
2480 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2481 }
2482 if (newAttr)
2483 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002484}
2485
Chandler Carruthedc2c642011-07-02 00:01:44 +00002486static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2487 const AttributeList &Attr) {
John McCall86bc21f2011-03-02 11:33:24 +00002488 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2489 if (!method) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002490 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002491 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00002492 return;
2493 }
2494
Aaron Ballman00e99962013-08-31 01:11:41 +00002495 if (!Attr.isArgIdent(0)) {
2496 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2497 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002498 return;
2499 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002500
Aaron Ballman682ee422013-09-11 19:47:58 +00002501 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2502 ObjCMethodFamilyAttr::FamilyKind F;
2503 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2504 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2505 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002506 return;
2507 }
2508
Aaron Ballman682ee422013-09-11 19:47:58 +00002509 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCall31168b02011-06-15 23:02:42 +00002510 !method->getResultType()->isObjCObjectPointerType()) {
2511 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2512 << method->getResultType();
2513 // Ignore the attribute.
2514 return;
2515 }
2516
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002517 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman682ee422013-09-11 19:47:58 +00002518 S.Context, F));
John McCall86bc21f2011-03-02 11:33:24 +00002519}
2520
Chandler Carruthedc2c642011-07-02 00:01:44 +00002521static void handleObjCExceptionAttr(Sema &S, Decl *D,
2522 const AttributeList &Attr) {
Chris Lattner677a3582009-02-14 08:09:34 +00002523 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2524 if (OCI == 0) {
Aaron Ballmanf90ccb02013-07-18 14:56:42 +00002525 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2526 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner677a3582009-02-14 08:09:34 +00002527 return;
2528 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002529
Michael Han99315932013-01-24 16:46:58 +00002530 D->addAttr(::new (S.Context)
2531 ObjCExceptionAttr(Attr.getRange(), S.Context,
2532 Attr.getAttributeSpellingListIndex()));
Chris Lattner677a3582009-02-14 08:09:34 +00002533}
2534
Chandler Carruthedc2c642011-07-02 00:01:44 +00002535static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002536 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002537 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002538 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002539 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2540 return;
2541 }
2542 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002543 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2544 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002545 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002546 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2547 return;
2548 }
2549 }
2550 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002551 // It is okay to include this attribute on properties, e.g.:
2552 //
2553 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2554 //
2555 // In this case it follows tradition and suppresses an error in the above
2556 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002557 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002558 }
Michael Han99315932013-01-24 16:46:58 +00002559 D->addAttr(::new (S.Context)
2560 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2561 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002562}
2563
Aaron Ballmanb0dc0742013-11-26 16:14:15 +00002564static void handleOverloadableAttr(Sema &S, Decl *D,
2565 const AttributeList &Attr) {
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002566 if (!isa<FunctionDecl>(D)) {
Aaron Ballmanb0dc0742013-11-26 16:14:15 +00002567 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2568 << Attr.getName() << ExpectedFunction;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002569 return;
2570 }
2571
Michael Han99315932013-01-24 16:46:58 +00002572 D->addAttr(::new (S.Context)
2573 OverloadableAttr(Attr.getRange(), S.Context,
2574 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002575}
2576
Chandler Carruthedc2c642011-07-02 00:01:44 +00002577static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002578 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002579 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002580 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002581 return;
2582 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002583
Aaron Ballman00e99962013-08-31 01:11:41 +00002584 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002585 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002586 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2587 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2588 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002589 return;
2590 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002591
Michael Han99315932013-01-24 16:46:58 +00002592 D->addAttr(::new (S.Context)
2593 BlocksAttr(Attr.getRange(), S.Context, type,
2594 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002595}
2596
Chandler Carruthedc2c642011-07-02 00:01:44 +00002597static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002598 // check the attribute arguments.
2599 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002600 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002601 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002602 }
2603
Aaron Ballman18a78382013-11-21 00:28:23 +00002604 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002605 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002606 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002607 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002608 if (E->isTypeDependent() || E->isValueDependent() ||
2609 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002610 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002611 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002612 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002613 return;
2614 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002615
John McCallb46f2872011-09-09 07:56:05 +00002616 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002617 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2618 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002619 return;
2620 }
John McCallb46f2872011-09-09 07:56:05 +00002621
2622 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002623 }
2624
Aaron Ballman18a78382013-11-21 00:28:23 +00002625 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002626 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002627 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002628 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002629 if (E->isTypeDependent() || E->isValueDependent() ||
2630 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002631 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002632 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002633 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002634 return;
2635 }
2636 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002637
John McCallb46f2872011-09-09 07:56:05 +00002638 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002639 // FIXME: This error message could be improved, it would be nice
2640 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002641 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2642 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002643 return;
2644 }
2645 }
2646
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002647 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002648 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002649 if (isa<FunctionNoProtoType>(FT)) {
2650 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2651 return;
2652 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002653
Chris Lattner9363e312009-03-17 23:03:47 +00002654 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002655 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002656 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002657 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002658 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002659 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002660 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002661 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002662 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002663 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2664 if (!BD->isVariadic()) {
2665 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2666 return;
2667 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002668 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002669 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002670 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002671 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002672 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002673 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002674 int m = Ty->isFunctionPointerType() ? 0 : 1;
2675 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002676 return;
2677 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002678 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002679 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002680 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002681 return;
2682 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002683 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002684 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002685 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002686 return;
2687 }
Michael Han99315932013-01-24 16:46:58 +00002688 D->addAttr(::new (S.Context)
2689 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2690 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002691}
2692
Lubos Lunakedc13882013-07-20 15:05:36 +00002693static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Lubos Lunakedc13882013-07-20 15:05:36 +00002694 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2695 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2696 else
2697 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2698}
2699
Chandler Carruthedc2c642011-07-02 00:01:44 +00002700static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002701 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002702 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002703 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002704 return;
2705 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002706
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002707 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2708 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2709 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002710 return;
2711 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002712 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2713 if (MD->getResultType()->isVoidType()) {
2714 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2715 << Attr.getName() << 1;
2716 return;
2717 }
2718
Michael Han99315932013-01-24 16:46:58 +00002719 D->addAttr(::new (S.Context)
2720 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2721 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002722}
2723
Chandler Carruthedc2c642011-07-02 00:01:44 +00002724static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002725 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002726 if (isa<CXXRecordDecl>(D)) {
2727 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2728 return;
2729 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002730 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2731 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002732 return;
2733 }
2734
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002735 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002736
Michael Han99315932013-01-24 16:46:58 +00002737 nd->addAttr(::new (S.Context)
2738 WeakAttr(Attr.getRange(), S.Context,
2739 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002740}
2741
Chandler Carruthedc2c642011-07-02 00:01:44 +00002742static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002743 // weak_import only applies to variable & function declarations.
2744 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002745 if (!D->canBeWeakImported(isDef)) {
2746 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002747 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2748 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002749 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002750 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002751 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002752 // Nothing to warn about here.
2753 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002754 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002755 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002756
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002757 return;
2758 }
2759
Michael Han99315932013-01-24 16:46:58 +00002760 D->addAttr(::new (S.Context)
2761 WeakImportAttr(Attr.getRange(), S.Context,
2762 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002763}
2764
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002765// Handles reqd_work_group_size and work_group_size_hint.
2766static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002767 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002768 uint32_t WGSize[3];
2769 for (unsigned i = 0; i < 3; ++i)
2770 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002771 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002772
2773 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2774 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2775 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2776 if (!(A->getXDim() == WGSize[0] &&
2777 A->getYDim() == WGSize[1] &&
2778 A->getZDim() == WGSize[2])) {
2779 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2780 Attr.getName();
2781 }
2782 }
2783
2784 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2785 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2786 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2787 if (!(A->getXDim() == WGSize[0] &&
2788 A->getYDim() == WGSize[1] &&
2789 A->getZDim() == WGSize[2])) {
2790 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2791 Attr.getName();
2792 }
2793 }
2794
2795 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2796 D->addAttr(::new (S.Context)
2797 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002798 WGSize[0], WGSize[1], WGSize[2],
2799 Attr.getAttributeSpellingListIndex()));
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002800 else
2801 D->addAttr(::new (S.Context)
2802 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002803 WGSize[0], WGSize[1], WGSize[2],
2804 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002805}
2806
Joey Goulyaba589c2013-03-08 09:42:32 +00002807static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2808 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2809
Aaron Ballman00e99962013-08-31 01:11:41 +00002810 if (!Attr.hasParsedType()) {
2811 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2812 << Attr.getName() << 1;
2813 return;
2814 }
2815
Richard Smithb87c4652013-10-31 21:23:20 +00002816 TypeSourceInfo *ParmTSI = 0;
2817 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2818 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002819
2820 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2821 (ParmType->isBooleanType() ||
2822 !ParmType->isIntegralType(S.getASTContext()))) {
2823 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2824 << ParmType;
2825 return;
2826 }
2827
2828 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2829 D->hasAttr<VecTypeHintAttr>()) {
2830 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
Richard Smithb87c4652013-10-31 21:23:20 +00002831 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002832 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2833 return;
2834 }
2835 }
2836
2837 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Richard Smithb87c4652013-10-31 21:23:20 +00002838 ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002839}
2840
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002841SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002842 StringRef Name,
2843 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002844 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2845 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002846 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002847 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2848 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002849 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002850 }
Michael Han99315932013-01-24 16:46:58 +00002851 return ::new (Context) SectionAttr(Range, Context, Name,
2852 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002853}
2854
Chandler Carruthedc2c642011-07-02 00:01:44 +00002855static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002856 // Make sure that there is a string literal as the sections's single
2857 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002858 StringRef Str;
2859 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002860 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002861 return;
Mike Stump11289f42009-09-09 15:08:12 +00002862
Chris Lattner30ba6742009-08-10 19:03:04 +00002863 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002864 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002865 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002866 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002867 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002868 return;
2869 }
Mike Stump11289f42009-09-09 15:08:12 +00002870
Chris Lattner20aee9b2010-01-12 20:58:53 +00002871 // This attribute cannot be applied to local variables.
2872 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002873 S.Diag(LiteralLoc, diag::err_attribute_section_local_variable);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002874 return;
2875 }
Michael Han99315932013-01-24 16:46:58 +00002876
2877 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002878 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002879 if (NewAttr)
2880 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002881}
2882
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002883
Chandler Carruthedc2c642011-07-02 00:01:44 +00002884static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002885 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002886 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002887 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002888 } else {
Michael Han99315932013-01-24 16:46:58 +00002889 D->addAttr(::new (S.Context)
2890 NoThrowAttr(Attr.getRange(), S.Context,
2891 Attr.getAttributeSpellingListIndex()));
Douglas Gregor88336832011-06-15 05:45:11 +00002892 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002893}
2894
Chandler Carruthedc2c642011-07-02 00:01:44 +00002895static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002896 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002897 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002898 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002899 } else {
Michael Han99315932013-01-24 16:46:58 +00002900 D->addAttr(::new (S.Context)
2901 ConstAttr(Attr.getRange(), S.Context,
2902 Attr.getAttributeSpellingListIndex() ));
Douglas Gregor88336832011-06-15 05:45:11 +00002903 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002904}
2905
Chandler Carruthedc2c642011-07-02 00:01:44 +00002906static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han99315932013-01-24 16:46:58 +00002907 D->addAttr(::new (S.Context)
2908 PureAttr(Attr.getRange(), S.Context,
2909 Attr.getAttributeSpellingListIndex()));
Anders Carlssonb8316282008-10-05 23:32:53 +00002910}
2911
Chandler Carruthedc2c642011-07-02 00:01:44 +00002912static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002913 VarDecl *VD = dyn_cast<VarDecl>(D);
Anders Carlssond277d792009-01-31 01:16:18 +00002914 if (!VD || !VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002915 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002916 return;
2917 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002918
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002919 Expr *E = Attr.getArgAsExpr(0);
2920 SourceLocation Loc = E->getExprLoc();
2921 FunctionDecl *FD = 0;
2922 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002923
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002924 // gcc only allows for simple identifiers. Since we support more than gcc, we
2925 // will warn the user.
2926 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2927 if (DRE->hasQualifier())
2928 S.Diag(Loc, diag::warn_cleanup_ext);
2929 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2930 NI = DRE->getNameInfo();
2931 if (!FD) {
2932 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2933 << NI.getName();
2934 return;
2935 }
2936 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2937 if (ULE->hasExplicitTemplateArgs())
2938 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002939 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2940 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002941 if (!FD) {
2942 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2943 << NI.getName();
2944 if (ULE->getType() == S.Context.OverloadTy)
2945 S.NoteAllOverloadCandidates(ULE);
2946 return;
2947 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002948 } else {
2949 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002950 return;
2951 }
2952
Anders Carlssond277d792009-01-31 01:16:18 +00002953 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002954 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2955 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002956 return;
2957 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002958
Anders Carlsson723f55d2009-02-07 23:16:50 +00002959 // We're currently more strict than GCC about what function types we accept.
2960 // If this ever proves to be a problem it should be easy to fix.
2961 QualType Ty = S.Context.getPointerType(VD->getType());
2962 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002963 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2964 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002965 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2966 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002967 return;
2968 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002969
Michael Han99315932013-01-24 16:46:58 +00002970 D->addAttr(::new (S.Context)
2971 CleanupAttr(Attr.getRange(), S.Context, FD,
2972 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002973}
2974
Mike Stumpd3bb5572009-07-24 19:02:52 +00002975/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002976/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002977static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002978 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002979 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002980 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002981 return;
2982 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002983
Aaron Ballman00e99962013-08-31 01:11:41 +00002984 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002985 uint64_t ArgIdx;
2986 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2987 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002988 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002989
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002990 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002991 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002992
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002993 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2994 if (not_nsstring_type &&
2995 !isCFStringType(Ty, S.Context) &&
2996 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002997 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002998 // FIXME: Should highlight the actual expression that has the wrong type.
2999 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00003000 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003001 << IdxExpr->getSourceRange();
3002 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003003 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003004 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003005 if (!isNSStringType(Ty, S.Context) &&
3006 !isCFStringType(Ty, S.Context) &&
3007 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003008 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003009 // FIXME: Should highlight the actual expression that has the wrong type.
3010 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00003011 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003012 << IdxExpr->getSourceRange();
3013 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003014 }
3015
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00003016 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
3017 // because that has corrected for the implicit this parameter, and is zero-
3018 // based. The attribute expects what the user wrote explicitly.
3019 llvm::APSInt Val;
3020 IdxExpr->EvaluateAsInt(Val, S.Context);
3021
Michael Han99315932013-01-24 16:46:58 +00003022 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00003023 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00003024 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003025}
3026
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003027enum FormatAttrKind {
3028 CFStringFormat,
3029 NSStringFormat,
3030 StrftimeFormat,
3031 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003032 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003033 InvalidFormat
3034};
3035
3036/// getFormatAttrKind - Map from format attribute names to supported format
3037/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003038static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003039 return llvm::StringSwitch<FormatAttrKind>(Format)
3040 // Check for formats that get handled specially.
3041 .Case("NSString", NSStringFormat)
3042 .Case("CFString", CFStringFormat)
3043 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003044
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003045 // Otherwise, check for supported formats.
3046 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3047 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3048 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003049
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003050 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3051 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003052}
3053
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003054/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003055/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003056static void handleInitPriorityAttr(Sema &S, Decl *D,
3057 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003058 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003059 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3060 return;
3061 }
3062
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003063 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003064 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3065 Attr.setInvalid();
3066 return;
3067 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003068 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003069 if (S.Context.getAsArrayType(T))
3070 T = S.Context.getBaseElementType(T);
3071 if (!T->getAs<RecordType>()) {
3072 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3073 Attr.setInvalid();
3074 return;
3075 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003076
3077 Expr *E = Attr.getArgAsExpr(0);
3078 uint32_t prioritynum;
3079 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003080 Attr.setInvalid();
3081 return;
3082 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003083
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003084 if (prioritynum < 101 || prioritynum > 65535) {
3085 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003086 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003087 Attr.setInvalid();
3088 return;
3089 }
Michael Han99315932013-01-24 16:46:58 +00003090 D->addAttr(::new (S.Context)
3091 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3092 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003093}
3094
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003095FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3096 IdentifierInfo *Format, int FormatIdx,
3097 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00003098 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003099 // Check whether we already have an equivalent format attribute.
3100 for (specific_attr_iterator<FormatAttr>
3101 i = D->specific_attr_begin<FormatAttr>(),
3102 e = D->specific_attr_end<FormatAttr>();
3103 i != e ; ++i) {
3104 FormatAttr *f = *i;
3105 if (f->getType() == Format &&
3106 f->getFormatIdx() == FormatIdx &&
3107 f->getFirstArg() == FirstArg) {
3108 // If we don't have a valid location for this attribute, adopt the
3109 // location.
3110 if (f->getLocation().isInvalid())
3111 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003112 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00003113 }
3114 }
3115
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003116 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3117 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003118}
3119
Mike Stumpd3bb5572009-07-24 19:02:52 +00003120/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003121/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003122static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003123 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003124 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00003125 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003126 return;
3127 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003128
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003129 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003130 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003131 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003132 return;
3133 }
3134
Chandler Carruth743682b2010-11-16 08:35:43 +00003135 // In C++ the implicit 'this' function parameter also counts, and they are
3136 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003137 bool HasImplicitThisParam = isInstanceMethod(D);
3138 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003139
Aaron Ballman00e99962013-08-31 01:11:41 +00003140 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
3141 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003142
3143 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003144 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003145 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003146 // If we've modified the string name, we need a new identifier for it.
3147 II = &S.Context.Idents.get(Format);
3148 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003149
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003150 // Check for supported formats.
3151 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00003152
3153 if (Kind == IgnoredFormat)
3154 return;
3155
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003156 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00003157 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman00e99962013-08-31 01:11:41 +00003158 << "format" << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003159 return;
3160 }
3161
3162 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00003163 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003164 uint32_t Idx;
3165 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003166 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003167
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003168 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003169 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003170 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003171 return;
3172 }
3173
3174 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003175 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003176
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003177 if (HasImplicitThisParam) {
3178 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00003179 S.Diag(Attr.getLoc(),
3180 diag::err_format_attribute_implicit_this_format_string)
3181 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003182 return;
3183 }
3184 ArgIdx--;
3185 }
Mike Stump11289f42009-09-09 15:08:12 +00003186
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003187 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003188 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003189
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003190 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003191 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003192 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3193 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00003194 return;
3195 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003196 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003197 // FIXME: do we need to check if the type is NSString*? What are the
3198 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003199 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003200 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003201 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3202 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003203 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003204 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003205 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003206 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003207 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003208 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3209 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003210 return;
3211 }
3212
3213 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00003214 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003215 uint32_t FirstArg;
3216 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003217 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003218
3219 // check if the function is variadic if the 3rd argument non-zero
3220 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003221 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003222 ++NumArgs; // +1 for ...
3223 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003224 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003225 return;
3226 }
3227 }
3228
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003229 // strftime requires FirstArg to be 0 because it doesn't read from any
3230 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003231 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003232 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00003233 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3234 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003235 return;
3236 }
3237 // if 0 it disables parameter checking (to use with e.g. va_list)
3238 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003239 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003240 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003241 return;
3242 }
3243
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003244 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003245 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00003246 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003247 if (NewAttr)
3248 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003249}
3250
Chandler Carruthedc2c642011-07-02 00:01:44 +00003251static void handleTransparentUnionAttr(Sema &S, Decl *D,
3252 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003253 // Try to find the underlying union declaration.
3254 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003255 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003256 if (TD && TD->getUnderlyingType()->isUnionType())
3257 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3258 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003259 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003260
3261 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00003262 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003263 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003264 return;
3265 }
3266
John McCallf937c022011-10-07 06:10:15 +00003267 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003268 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003269 diag::warn_transparent_union_attribute_not_definition);
3270 return;
3271 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003272
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003273 RecordDecl::field_iterator Field = RD->field_begin(),
3274 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003275 if (Field == FieldEnd) {
3276 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3277 return;
3278 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003279
David Blaikie40ed2972012-06-06 20:45:41 +00003280 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003281 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003282 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003283 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003284 diag::warn_transparent_union_attribute_floating)
3285 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003286 return;
3287 }
3288
3289 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3290 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3291 for (; Field != FieldEnd; ++Field) {
3292 QualType FieldType = Field->getType();
3293 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3294 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3295 // Warn if we drop the attribute.
3296 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003297 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003298 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003299 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003300 diag::warn_transparent_union_attribute_field_size_align)
3301 << isSize << Field->getDeclName() << FieldBits;
3302 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003303 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003304 diag::note_transparent_union_first_field_size_align)
3305 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003306 return;
3307 }
3308 }
3309
Michael Han99315932013-01-24 16:46:58 +00003310 RD->addAttr(::new (S.Context)
3311 TransparentUnionAttr(Attr.getRange(), S.Context,
3312 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003313}
3314
Chandler Carruthedc2c642011-07-02 00:01:44 +00003315static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003316 // Make sure that there is a string literal as the annotation's single
3317 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003318 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003319 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003320 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003321
3322 // Don't duplicate annotations that are already set.
3323 for (specific_attr_iterator<AnnotateAttr>
3324 i = D->specific_attr_begin<AnnotateAttr>(),
3325 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003326 if ((*i)->getAnnotation() == Str)
3327 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003328 }
Michael Han99315932013-01-24 16:46:58 +00003329
3330 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003331 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00003332 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003333}
3334
Chandler Carruthedc2c642011-07-02 00:01:44 +00003335static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003336 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003337 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003338 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3339 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003340 return;
3341 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003342
Richard Smith848e1f12013-02-01 08:12:08 +00003343 if (Attr.getNumArgs() == 0) {
3344 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3345 true, 0, Attr.getAttributeSpellingListIndex()));
3346 return;
3347 }
3348
Aaron Ballman00e99962013-08-31 01:11:41 +00003349 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00003350 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3351 S.Diag(Attr.getEllipsisLoc(),
3352 diag::err_pack_expansion_without_parameter_packs);
3353 return;
3354 }
3355
3356 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3357 return;
3358
3359 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3360 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003361}
3362
3363void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003364 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003365 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3366 SourceLocation AttrLoc = AttrRange.getBegin();
3367
Richard Smith1dba27c2013-01-29 09:02:09 +00003368 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003369 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003370 // C++11 [dcl.align]p1:
3371 // An alignment-specifier may be applied to a variable or to a class
3372 // data member, but it shall not be applied to a bit-field, a function
3373 // parameter, the formal parameter of a catch clause, or a variable
3374 // declared with the register storage class specifier. An
3375 // alignment-specifier may also be applied to the declaration of a class
3376 // or enumeration type.
3377 // C11 6.7.5/2:
3378 // An alignment attribute shall not be specified in a declaration of
3379 // a typedef, or a bit-field, or a function, or a parameter, or an
3380 // object declared with the register storage-class specifier.
3381 int DiagKind = -1;
3382 if (isa<ParmVarDecl>(D)) {
3383 DiagKind = 0;
3384 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3385 if (VD->getStorageClass() == SC_Register)
3386 DiagKind = 1;
3387 if (VD->isExceptionVariable())
3388 DiagKind = 2;
3389 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3390 if (FD->isBitField())
3391 DiagKind = 3;
3392 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00003393 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3394 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00003395 << (TmpAttr.isC11() ? ExpectedVariableOrField
3396 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003397 return;
3398 }
3399 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003400 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smithbc8caaf2013-02-22 04:55:39 +00003401 << TmpAttr.isC11() << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003402 return;
3403 }
3404 }
3405
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003406 if (E->isTypeDependent() || E->isValueDependent()) {
3407 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003408 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3409 AA->setPackExpansion(IsPackExpansion);
3410 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003411 return;
3412 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003413
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003414 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00003415 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00003416 ExprResult ICE
3417 = VerifyIntegerConstantExpression(E, &Alignment,
3418 diag::err_aligned_attribute_argument_not_int,
3419 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003420 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003421 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003422
3423 // C++11 [dcl.align]p2:
3424 // -- if the constant expression evaluates to zero, the alignment
3425 // specifier shall have no effect
3426 // C11 6.7.5p6:
3427 // An alignment specification of zero has no effect.
3428 if (!(TmpAttr.isAlignas() && !Alignment) &&
3429 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003430 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3431 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003432 return;
3433 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003434
Richard Smith848e1f12013-02-01 08:12:08 +00003435 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00003436 // We've already verified it's a power of 2, now let's make sure it's
3437 // 8192 or less.
3438 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00003439 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00003440 << E->getSourceRange();
3441 return;
3442 }
3443 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003444
Richard Smith44c247f2013-02-22 08:32:16 +00003445 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3446 ICE.take(), SpellingListIndex);
3447 AA->setPackExpansion(IsPackExpansion);
3448 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003449}
3450
Michael Hanaf02bbe2013-02-01 01:19:17 +00003451void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003452 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003453 // FIXME: Cache the number on the Attr object if non-dependent?
3454 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003455 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3456 SpellingListIndex);
3457 AA->setPackExpansion(IsPackExpansion);
3458 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003459}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003460
Richard Smith848e1f12013-02-01 08:12:08 +00003461void Sema::CheckAlignasUnderalignment(Decl *D) {
3462 assert(D->hasAttrs() && "no attributes on decl");
3463
3464 QualType Ty;
3465 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3466 Ty = VD->getType();
3467 else
3468 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00003469 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003470 return;
3471
3472 // C++11 [dcl.align]p5, C11 6.7.5/4:
3473 // The combined effect of all alignment attributes in a declaration shall
3474 // not specify an alignment that is less strict than the alignment that
3475 // would otherwise be required for the entity being declared.
3476 AlignedAttr *AlignasAttr = 0;
3477 unsigned Align = 0;
3478 for (specific_attr_iterator<AlignedAttr>
3479 I = D->specific_attr_begin<AlignedAttr>(),
3480 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3481 if (I->isAlignmentDependent())
3482 return;
3483 if (I->isAlignas())
3484 AlignasAttr = *I;
3485 Align = std::max(Align, I->getAlignment(Context));
3486 }
3487
3488 if (AlignasAttr && Align) {
3489 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3490 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3491 if (NaturalAlign > RequestedAlign)
3492 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3493 << Ty << (unsigned)NaturalAlign.getQuantity();
3494 }
3495}
3496
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003497/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003498/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003499///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003500/// Despite what would be logical, the mode attribute is a decl attribute, not a
3501/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3502/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003503static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003504 // This attribute isn't documented, but glibc uses it. It changes
3505 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00003506 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00003507 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3508 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003509 return;
3510 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003511
Aaron Ballman00e99962013-08-31 01:11:41 +00003512 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3513 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003514
3515 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003516 if (Str.startswith("__") && Str.endswith("__"))
3517 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003518
3519 unsigned DestWidth = 0;
3520 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003521 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003522 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003523 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003524 switch (Str[0]) {
3525 case 'Q': DestWidth = 8; break;
3526 case 'H': DestWidth = 16; break;
3527 case 'S': DestWidth = 32; break;
3528 case 'D': DestWidth = 64; break;
3529 case 'X': DestWidth = 96; break;
3530 case 'T': DestWidth = 128; break;
3531 }
3532 if (Str[1] == 'F') {
3533 IntegerMode = false;
3534 } else if (Str[1] == 'C') {
3535 IntegerMode = false;
3536 ComplexMode = true;
3537 } else if (Str[1] != 'I') {
3538 DestWidth = 0;
3539 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003540 break;
3541 case 4:
3542 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3543 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003544 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003545 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003546 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003547 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003548 break;
3549 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003550 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003551 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003552 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003553 case 11:
3554 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003555 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003556 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003557 }
3558
3559 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003560 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003561 OldTy = TD->getUnderlyingType();
3562 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3563 OldTy = VD->getType();
3564 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003565 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003566 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003567 return;
3568 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003569
John McCall9dd450b2009-09-21 23:43:11 +00003570 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003571 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3572 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003573 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003574 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3575 } else if (ComplexMode) {
3576 if (!OldTy->isComplexType())
3577 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3578 } else {
3579 if (!OldTy->isFloatingType())
3580 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3581 }
3582
Mike Stump87c57ac2009-05-16 07:39:55 +00003583 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3584 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003585 // FIXME: Make sure floating-point mappings are accurate
3586 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003587 if (!DestWidth) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003588 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003589 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003590 }
3591
3592 QualType NewTy;
3593
3594 if (IntegerMode)
3595 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3596 OldTy->isSignedIntegerType());
3597 else
3598 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3599
3600 if (NewTy.isNull()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003601 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003602 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003603 }
3604
Eli Friedman4735374e2009-03-03 06:41:03 +00003605 if (ComplexMode) {
3606 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003607 }
3608
3609 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003610 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3611 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3612 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003613 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003614
3615 D->addAttr(::new (S.Context)
3616 ModeAttr(Attr.getRange(), S.Context, Name,
3617 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003618}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003619
Chandler Carruthedc2c642011-07-02 00:01:44 +00003620static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003621 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3622 if (!VD->hasGlobalStorage())
3623 S.Diag(Attr.getLoc(),
3624 diag::warn_attribute_requires_functions_or_static_globals)
3625 << Attr.getName();
3626 } else if (!isFunctionOrMethod(D)) {
3627 S.Diag(Attr.getLoc(),
3628 diag::warn_attribute_requires_functions_or_static_globals)
3629 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003630 return;
3631 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003632
Michael Han99315932013-01-24 16:46:58 +00003633 D->addAttr(::new (S.Context)
3634 NoDebugAttr(Attr.getRange(), S.Context,
3635 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003636}
3637
Chandler Carruthedc2c642011-07-02 00:01:44 +00003638static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003639 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00003640 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003641 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00003642 return;
3643 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003644
Michael Han99315932013-01-24 16:46:58 +00003645 D->addAttr(::new (S.Context)
3646 NoInlineAttr(Attr.getRange(), S.Context,
3647 Attr.getAttributeSpellingListIndex()));
Anders Carlsson88097122009-02-19 19:16:48 +00003648}
3649
Chandler Carruthedc2c642011-07-02 00:01:44 +00003650static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3651 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003652 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003653 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003654 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00003655 return;
3656 }
3657
Michael Han99315932013-01-24 16:46:58 +00003658 D->addAttr(::new (S.Context)
3659 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3660 Attr.getAttributeSpellingListIndex()));
Chris Lattner3c77a352010-06-22 00:03:40 +00003661}
3662
Chandler Carruthedc2c642011-07-02 00:01:44 +00003663static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003664 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003665 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003666 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003667 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003668 return;
3669 }
3670
Michael Han99315932013-01-24 16:46:58 +00003671 D->addAttr(::new (S.Context)
3672 CUDAConstantAttr(Attr.getRange(), S.Context,
3673 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003674 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003675 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003676 }
3677}
3678
Chandler Carruthedc2c642011-07-02 00:01:44 +00003679static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003680 if (S.LangOpts.CUDA) {
3681 // check the attribute arguments.
3682 if (Attr.getNumArgs() != 0) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003683 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3684 << Attr.getName() << 0;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003685 return;
3686 }
3687
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003688 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003689 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003690 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003691 return;
3692 }
3693
Michael Han99315932013-01-24 16:46:58 +00003694 D->addAttr(::new (S.Context)
3695 CUDADeviceAttr(Attr.getRange(), S.Context,
3696 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003697 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003698 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003699 }
3700}
3701
Chandler Carruthedc2c642011-07-02 00:01:44 +00003702static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003703 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003704 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003705 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003706 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003707 return;
3708 }
3709
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003710 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003711 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00003712 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie6adc78e2013-02-18 22:06:02 +00003713 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003714 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3715 << FD->getType()
David Blaikie6adc78e2013-02-18 22:06:02 +00003716 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003717 "void");
3718 } else {
3719 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3720 << FD->getType();
3721 }
3722 return;
3723 }
3724
Michael Han99315932013-01-24 16:46:58 +00003725 D->addAttr(::new (S.Context)
3726 CUDAGlobalAttr(Attr.getRange(), S.Context,
3727 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003728 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003729 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003730 }
3731}
3732
Chandler Carruthedc2c642011-07-02 00:01:44 +00003733static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003734 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003735 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003736 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003737 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003738 return;
3739 }
3740
Michael Han99315932013-01-24 16:46:58 +00003741 D->addAttr(::new (S.Context)
3742 CUDAHostAttr(Attr.getRange(), S.Context,
3743 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003744 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003745 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003746 }
3747}
3748
Chandler Carruthedc2c642011-07-02 00:01:44 +00003749static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003750 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003751 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003752 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003753 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003754 return;
3755 }
3756
Michael Han99315932013-01-24 16:46:58 +00003757 D->addAttr(::new (S.Context)
3758 CUDASharedAttr(Attr.getRange(), S.Context,
3759 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003760 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003761 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003762 }
3763}
3764
Chandler Carruthedc2c642011-07-02 00:01:44 +00003765static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003766 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00003767 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003768 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003769 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00003770 return;
3771 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003772
Douglas Gregor35b57532009-10-27 21:01:01 +00003773 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003774 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003775 return;
3776 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003777
Michael Han99315932013-01-24 16:46:58 +00003778 D->addAttr(::new (S.Context)
3779 GNUInlineAttr(Attr.getRange(), S.Context,
3780 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003781}
3782
Chandler Carruthedc2c642011-07-02 00:01:44 +00003783static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003784 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003785
Aaron Ballman02df2e02012-12-09 17:45:41 +00003786 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003787 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003788 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3789 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003790 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003791 return;
3792
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003793 if (!isa<ObjCMethodDecl>(D)) {
3794 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3795 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003796 return;
3797 }
3798
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003799 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003800 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003801 D->addAttr(::new (S.Context)
3802 FastCallAttr(Attr.getRange(), S.Context,
3803 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003804 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003805 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003806 D->addAttr(::new (S.Context)
3807 StdCallAttr(Attr.getRange(), S.Context,
3808 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003809 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003810 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003811 D->addAttr(::new (S.Context)
3812 ThisCallAttr(Attr.getRange(), S.Context,
3813 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003814 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003815 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003816 D->addAttr(::new (S.Context)
3817 CDeclAttr(Attr.getRange(), S.Context,
3818 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003819 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003820 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003821 D->addAttr(::new (S.Context)
3822 PascalAttr(Attr.getRange(), S.Context,
3823 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003824 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003825 case AttributeList::AT_MSABI:
3826 D->addAttr(::new (S.Context)
3827 MSABIAttr(Attr.getRange(), S.Context,
3828 Attr.getAttributeSpellingListIndex()));
3829 return;
3830 case AttributeList::AT_SysVABI:
3831 D->addAttr(::new (S.Context)
3832 SysVABIAttr(Attr.getRange(), S.Context,
3833 Attr.getAttributeSpellingListIndex()));
3834 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003835 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003836 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003837 switch (CC) {
3838 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003839 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003840 break;
3841 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003842 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003843 break;
3844 default:
3845 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003846 }
3847
Michael Han99315932013-01-24 16:46:58 +00003848 D->addAttr(::new (S.Context)
3849 PcsAttr(Attr.getRange(), S.Context, PCS,
3850 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003851 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003852 }
Derek Schuffa2020962012-10-16 22:30:41 +00003853 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003854 D->addAttr(::new (S.Context)
3855 PnaclCallAttr(Attr.getRange(), S.Context,
3856 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003857 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003858 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003859 D->addAttr(::new (S.Context)
3860 IntelOclBiccAttr(Attr.getRange(), S.Context,
3861 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003862 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003863
Abramo Bagnara50099372010-04-30 13:10:51 +00003864 default:
3865 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003866 }
3867}
3868
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003869static void handleOpenCLKernelAttr(Sema &S, Decl *D,
3870 const AttributeList &Attr) {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003871 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003872}
3873
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003874static void handleOpenCLImageAccessAttr(Sema &S, Decl *D,
3875 const AttributeList &Attr) {
3876 uint32_t ArgNum;
3877 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), ArgNum))
Guy Benyeifb36ede2013-03-24 13:58:12 +00003878 return;
Guy Benyeifb36ede2013-03-24 13:58:12 +00003879
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003880 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(Attr.getRange(),
3881 S.Context, ArgNum));
Guy Benyeifb36ede2013-03-24 13:58:12 +00003882}
3883
Aaron Ballman02df2e02012-12-09 17:45:41 +00003884bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3885 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003886 if (attr.isInvalid())
3887 return true;
3888
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003889 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003890 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003891 attr.setInvalid();
3892 return true;
3893 }
3894
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003895 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3896 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003897 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003898 case AttributeList::AT_CDecl: CC = CC_C; break;
3899 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3900 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3901 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3902 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003903 case AttributeList::AT_MSABI:
3904 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3905 CC_X86_64Win64;
3906 break;
3907 case AttributeList::AT_SysVABI:
3908 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3909 CC_C;
3910 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003911 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003912 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003913 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003914 attr.setInvalid();
3915 return true;
3916 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003917 if (StrRef == "aapcs") {
3918 CC = CC_AAPCS;
3919 break;
3920 } else if (StrRef == "aapcs-vfp") {
3921 CC = CC_AAPCS_VFP;
3922 break;
3923 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003924
3925 attr.setInvalid();
3926 Diag(attr.getLoc(), diag::err_invalid_pcs);
3927 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003928 }
Derek Schuffa2020962012-10-16 22:30:41 +00003929 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003930 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003931 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003932 }
3933
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003934 const TargetInfo &TI = Context.getTargetInfo();
3935 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3936 if (A == TargetInfo::CCCR_Warning) {
3937 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003938
3939 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3940 if (FD)
3941 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3942 TargetInfo::CCMT_NonMember;
3943 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003944 }
3945
John McCall3882ace2011-01-05 12:14:39 +00003946 return false;
3947}
3948
Chandler Carruthedc2c642011-07-02 00:01:44 +00003949static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003950 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003951
3952 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003953 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003954 return;
3955
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003956 if (!isa<ObjCMethodDecl>(D)) {
3957 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3958 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003959 return;
3960 }
Eli Friedman7044b762009-03-27 21:06:47 +00003961
Michael Han99315932013-01-24 16:46:58 +00003962 D->addAttr(::new (S.Context)
3963 RegparmAttr(Attr.getRange(), S.Context, numParams,
3964 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00003965}
3966
3967/// Checks a regparm attribute, returning true if it is ill-formed and
3968/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003969bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3970 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003971 return true;
3972
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003973 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003974 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003975 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003976 }
Eli Friedman7044b762009-03-27 21:06:47 +00003977
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003978 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003979 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003980 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003981 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003982 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003983 }
3984
Douglas Gregore8bbc122011-09-02 00:18:52 +00003985 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003986 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003987 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003988 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003989 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003990 }
3991
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003992 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003993 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003994 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003995 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003996 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003997 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003998 }
3999
John McCall3882ace2011-01-05 12:14:39 +00004000 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004001}
4002
Chandler Carruthedc2c642011-07-02 00:01:44 +00004003static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00004004 if (S.LangOpts.CUDA) {
4005 // check the attribute arguments.
4006 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00004007 // FIXME: 0 is not okay.
4008 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004009 return;
4010 }
4011
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004012 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00004013 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00004014 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004015 return;
4016 }
4017
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004018 uint32_t MaxThreads, MinBlocks;
4019 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1) ||
4020 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(1), MinBlocks, 2))
Peter Collingbourne827301e2010-12-12 23:03:07 +00004021 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004022
Michael Han99315932013-01-24 16:46:58 +00004023 D->addAttr(::new (S.Context)
4024 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004025 MaxThreads, MinBlocks,
Michael Han99315932013-01-24 16:46:58 +00004026 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00004027 } else {
4028 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4029 }
4030}
4031
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004032static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4033 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00004034 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00004035 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00004036 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004037 return;
4038 }
Aaron Ballman00e99962013-08-31 01:11:41 +00004039
4040 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004041 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004042
Aaron Ballman00e99962013-08-31 01:11:41 +00004043 StringRef AttrName = Attr.getName()->getName();
4044 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004045
4046 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4047 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4048 << Attr.getName() << ExpectedFunctionOrMethod;
4049 return;
4050 }
4051
4052 uint64_t ArgumentIdx;
4053 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4054 Attr.getLoc(), 2,
Aaron Ballman00e99962013-08-31 01:11:41 +00004055 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004056 return;
4057
4058 uint64_t TypeTagIdx;
4059 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4060 Attr.getLoc(), 3,
Aaron Ballman00e99962013-08-31 01:11:41 +00004061 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004062 return;
4063
4064 bool IsPointer = (AttrName == "pointer_with_type_tag");
4065 if (IsPointer) {
4066 // Ensure that buffer has a pointer type.
4067 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4068 if (!BufferTy->isPointerType()) {
4069 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00004070 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004071 }
4072 }
4073
Michael Han99315932013-01-24 16:46:58 +00004074 D->addAttr(::new (S.Context)
4075 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4076 ArgumentIdx, TypeTagIdx, IsPointer,
4077 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004078}
4079
4080static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4081 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00004082 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00004083 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00004084 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004085 return;
4086 }
Aaron Ballman00e99962013-08-31 01:11:41 +00004087
4088 if (!checkAttributeNumArgs(S, Attr, 1))
4089 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004090
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004091 if (!isa<VarDecl>(D)) {
4092 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4093 << Attr.getName() << ExpectedVariable;
4094 return;
4095 }
4096
Aaron Ballman00e99962013-08-31 01:11:41 +00004097 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00004098 TypeSourceInfo *MatchingCTypeLoc = 0;
4099 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
4100 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004101
Michael Han99315932013-01-24 16:46:58 +00004102 D->addAttr(::new (S.Context)
4103 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00004104 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00004105 Attr.getLayoutCompatible(),
4106 Attr.getMustBeNull(),
4107 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004108}
4109
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004110//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004111// Checker-specific attribute handlers.
4112//===----------------------------------------------------------------------===//
4113
John McCalled433932011-01-25 03:31:58 +00004114static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004115 return type->isDependentType() ||
4116 type->isObjCObjectPointerType() ||
4117 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00004118}
4119static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004120 return type->isDependentType() ||
4121 type->isPointerType() ||
4122 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00004123}
4124
Chandler Carruthedc2c642011-07-02 00:01:44 +00004125static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004126 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00004127 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004128 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004129 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00004130 return;
4131 }
4132
4133 bool typeOK, cf;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004134 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00004135 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4136 cf = false;
4137 } else {
4138 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4139 cf = true;
4140 }
4141
4142 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004143 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004144 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00004145 return;
4146 }
4147
4148 if (cf)
Michael Han99315932013-01-24 16:46:58 +00004149 param->addAttr(::new (S.Context)
4150 CFConsumedAttr(Attr.getRange(), S.Context,
4151 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004152 else
Michael Han99315932013-01-24 16:46:58 +00004153 param->addAttr(::new (S.Context)
4154 NSConsumedAttr(Attr.getRange(), S.Context,
4155 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004156}
4157
Chandler Carruthedc2c642011-07-02 00:01:44 +00004158static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4159 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004160 if (!isa<ObjCMethodDecl>(D)) {
4161 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004162 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00004163 return;
4164 }
4165
Michael Han99315932013-01-24 16:46:58 +00004166 D->addAttr(::new (S.Context)
4167 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4168 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004169}
4170
Chandler Carruthedc2c642011-07-02 00:01:44 +00004171static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4172 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004173
John McCalled433932011-01-25 03:31:58 +00004174 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004175
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004176 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004177 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004178 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004179 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004180 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00004181 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4182 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004183 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004184 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00004185 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004186 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004187 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00004188 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004189 return;
4190 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004191
John McCalled433932011-01-25 03:31:58 +00004192 bool typeOK;
4193 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004194 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004195 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004196 case AttributeList::AT_NSReturnsAutoreleased:
4197 case AttributeList::AT_NSReturnsRetained:
4198 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004199 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4200 cf = false;
4201 break;
4202
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004203 case AttributeList::AT_CFReturnsRetained:
4204 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004205 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4206 cf = true;
4207 break;
4208 }
4209
4210 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004211 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004212 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004213 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004214 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004215
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004216 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004217 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004218 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004219 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00004220 D->addAttr(::new (S.Context)
4221 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4222 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004223 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004224 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004225 D->addAttr(::new (S.Context)
4226 CFReturnsNotRetainedAttr(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_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004230 D->addAttr(::new (S.Context)
4231 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4232 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004233 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004234 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004235 D->addAttr(::new (S.Context)
4236 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4237 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004238 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004239 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004240 D->addAttr(::new (S.Context)
4241 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4242 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004243 return;
4244 };
4245}
4246
John McCallcf166702011-07-22 08:53:00 +00004247static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4248 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00004249 const int EP_ObjCMethod = 1;
4250 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004251
John McCallcf166702011-07-22 08:53:00 +00004252 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004253 QualType resultType;
4254
John McCallcf166702011-07-22 08:53:00 +00004255 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4256
Fariborz Jahaniana53e5d72012-04-21 17:51:44 +00004257 if (!method) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004258 ObjCPropertyDecl *property = dyn_cast<ObjCPropertyDecl>(D);
4259 if (!property) {
4260 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4261 << SourceRange(loc, loc) << attr.getName() << ExpectedMethodOrProperty;
4262 return;
4263 }
4264 resultType = property->getType();
John McCallcf166702011-07-22 08:53:00 +00004265 }
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004266 else
4267 // Check that the method returns a normal pointer.
4268 resultType = method->getResultType();
John McCallcf166702011-07-22 08:53:00 +00004269
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004270 if (!resultType->isReferenceType() &&
4271 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004272 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00004273 << SourceRange(loc)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004274 << attr.getName() << (method ? EP_ObjCMethod : EP_ObjCProperty)
4275 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00004276
4277 // Drop the attribute.
4278 return;
4279 }
4280
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004281 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004282 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4283 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004284}
4285
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004286static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4287 const AttributeList &attr) {
4288 SourceLocation loc = attr.getLoc();
4289 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4290
4291 if (!method) {
4292 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4293 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4294 return;
4295 }
4296 DeclContext *DC = method->getDeclContext();
4297 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4298 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4299 << attr.getName() << 0;
4300 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4301 return;
4302 }
4303 if (method->getMethodFamily() == OMF_dealloc) {
4304 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4305 << attr.getName() << 1;
4306 return;
4307 }
4308
Michael Han99315932013-01-24 16:46:58 +00004309 method->addAttr(::new (S.Context)
4310 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4311 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004312}
4313
John McCall32f5fe12011-09-30 05:12:12 +00004314/// Handle cf_audited_transfer and cf_unknown_transfer.
4315static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4316 if (!isa<FunctionDecl>(D)) {
4317 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004318 << A.getRange() << A.getName() << ExpectedFunction;
John McCall32f5fe12011-09-30 05:12:12 +00004319 return;
4320 }
4321
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004322 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall32f5fe12011-09-30 05:12:12 +00004323
4324 // Check whether there's a conflicting attribute already present.
4325 Attr *Existing;
4326 if (IsAudited) {
4327 Existing = D->getAttr<CFUnknownTransferAttr>();
4328 } else {
4329 Existing = D->getAttr<CFAuditedTransferAttr>();
4330 }
4331 if (Existing) {
4332 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4333 << A.getName()
4334 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4335 << A.getRange() << Existing->getRange();
4336 return;
4337 }
4338
4339 // All clear; add the attribute.
4340 if (IsAudited) {
Michael Han99315932013-01-24 16:46:58 +00004341 D->addAttr(::new (S.Context)
4342 CFAuditedTransferAttr(A.getRange(), S.Context,
4343 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004344 } else {
Michael Han99315932013-01-24 16:46:58 +00004345 D->addAttr(::new (S.Context)
4346 CFUnknownTransferAttr(A.getRange(), S.Context,
4347 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004348 }
4349}
4350
John McCallf1e8b342011-09-29 07:17:38 +00004351static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4352 const AttributeList &Attr) {
4353 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4354 if (!RD || RD->isUnion()) {
4355 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004356 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00004357 }
4358
Aaron Ballman00e99962013-08-31 01:11:41 +00004359 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallf1e8b342011-09-29 07:17:38 +00004360
4361 // In Objective-C, verify that the type names an Objective-C type.
4362 // We don't want to check this outside of ObjC because people sometimes
4363 // do crazy C declarations of Objective-C types.
Aaron Ballman00e99962013-08-31 01:11:41 +00004364 if (Parm && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00004365 // Check for an existing type with this name.
Aaron Ballman00e99962013-08-31 01:11:41 +00004366 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallf1e8b342011-09-29 07:17:38 +00004367 Sema::LookupOrdinaryName);
4368 if (S.LookupName(R, Sc)) {
4369 NamedDecl *Target = R.getFoundDecl();
4370 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4371 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4372 S.Diag(Target->getLocStart(), diag::note_declared_at);
4373 }
4374 }
4375 }
4376
Michael Han99315932013-01-24 16:46:58 +00004377 D->addAttr(::new (S.Context)
Aaron Ballman00e99962013-08-31 01:11:41 +00004378 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han99315932013-01-24 16:46:58 +00004379 Attr.getAttributeSpellingListIndex()));
John McCallf1e8b342011-09-29 07:17:38 +00004380}
4381
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004382static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
4383 const AttributeList &Attr) {
Fariborz Jahaniandb3d8552013-11-19 00:09:48 +00004384 if (!isa<RecordDecl>(D)) {
Fariborz Jahanianf720f862013-11-19 17:42:25 +00004385 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4386 << Attr.getName()
4387 << (S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass
4388 : ExpectedStructOrUnion);
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004389 return;
4390 }
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004391
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00004392 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004393
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004394 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004395 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004396 return;
4397 }
4398
4399 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004400 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004401 Attr.getAttributeSpellingListIndex()));
4402}
4403
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004404static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
4405 const AttributeList &Attr) {
4406 if (!isa<RecordDecl>(D)) {
4407 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4408 << Attr.getName()
4409 << (S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass
4410 : ExpectedStructOrUnion);
4411 return;
4412 }
4413
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00004414 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004415
4416 if (!Parm) {
4417 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
4418 return;
4419 }
4420
4421 D->addAttr(::new (S.Context)
4422 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
4423 Attr.getAttributeSpellingListIndex()));
4424}
4425
Chandler Carruthedc2c642011-07-02 00:01:44 +00004426static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4427 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004428 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004429
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004430 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004431 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004432}
4433
Chandler Carruthedc2c642011-07-02 00:01:44 +00004434static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4435 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004436 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004437 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004438 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004439 return;
4440 }
4441
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004442 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004443 QualType type = vd->getType();
4444
4445 if (!type->isDependentType() &&
4446 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004447 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004448 << type;
4449 return;
4450 }
4451
4452 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4453
4454 // If we have no lifetime yet, check the lifetime we're presumably
4455 // going to infer.
4456 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4457 lifetime = type->getObjCARCImplicitLifetime();
4458
4459 switch (lifetime) {
4460 case Qualifiers::OCL_None:
4461 assert(type->isDependentType() &&
4462 "didn't infer lifetime for non-dependent type?");
4463 break;
4464
4465 case Qualifiers::OCL_Weak: // meaningful
4466 case Qualifiers::OCL_Strong: // meaningful
4467 break;
4468
4469 case Qualifiers::OCL_ExplicitNone:
4470 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004471 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004472 << (lifetime == Qualifiers::OCL_Autoreleasing);
4473 break;
4474 }
4475
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004476 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004477 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4478 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004479}
4480
Francois Picheta83957a2010-12-19 06:50:37 +00004481//===----------------------------------------------------------------------===//
4482// Microsoft specific attribute handlers.
4483//===----------------------------------------------------------------------===//
4484
Reid Kleckner140c4a72013-05-17 14:04:52 +00004485// Check if MS extensions or some other language extensions are enabled. If
4486// not, issue a diagnostic that the given attribute is unused.
4487static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4488 bool OtherExtension = false) {
4489 if (S.LangOpts.MicrosoftExt || OtherExtension)
4490 return true;
4491 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4492 return false;
4493}
4494
Chandler Carruthedc2c642011-07-02 00:01:44 +00004495static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00004496 if (!S.LangOpts.CPlusPlus) {
4497 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4498 << Attr.getName() << AttributeLangSupport::C;
4499 return;
4500 }
4501
Reid Kleckner140c4a72013-05-17 14:04:52 +00004502 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4503 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004504
Aaron Ballman60e705e2013-11-24 20:58:02 +00004505 if (!isa<CXXRecordDecl>(D)) {
4506 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4507 << Attr.getName() << ExpectedClass;
4508 return;
4509 }
4510
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004511 StringRef StrRef;
4512 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00004513 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00004514 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004515
David Majnemer89085342013-08-09 08:56:20 +00004516 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4517 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00004518 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4519 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00004520
Reid Kleckner140c4a72013-05-17 14:04:52 +00004521 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00004522 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004523 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004524 return;
4525 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004526
David Majnemer89085342013-08-09 08:56:20 +00004527 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004528 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00004529 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004530 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00004531 return;
4532 }
David Majnemer89085342013-08-09 08:56:20 +00004533 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004534 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004535 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004536 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004537 }
Francois Picheta83957a2010-12-19 06:50:37 +00004538
David Majnemer89085342013-08-09 08:56:20 +00004539 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4540 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004541}
4542
John McCall8d32c052012-05-22 21:28:12 +00004543static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004544 if (!checkMicrosoftExt(S, Attr))
Nico Weberefa45b22012-11-07 21:31:36 +00004545 return;
Nico Weberefa45b22012-11-07 21:31:36 +00004546
4547 AttributeList::Kind Kind = Attr.getKind();
4548 if (Kind == AttributeList::AT_SingleInheritance)
4549 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004550 ::new (S.Context)
4551 SingleInheritanceAttr(Attr.getRange(), S.Context,
4552 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004553 else if (Kind == AttributeList::AT_MultipleInheritance)
4554 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004555 ::new (S.Context)
4556 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4557 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004558 else if (Kind == AttributeList::AT_VirtualInheritance)
4559 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004560 ::new (S.Context)
4561 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4562 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004563}
4564
4565static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004566 if (!checkMicrosoftExt(S, Attr))
4567 return;
4568
4569 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmancc14f3a2013-11-22 21:49:04 +00004570 if (Kind == AttributeList::AT_Win64)
4571 D->addAttr(
4572 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4573 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004574}
4575
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004576static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004577 if (!checkMicrosoftExt(S, Attr))
4578 return;
4579 D->addAttr(::new (S.Context)
4580 ForceInlineAttr(Attr.getRange(), S.Context,
4581 Attr.getAttributeSpellingListIndex()));
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004582}
4583
Reid Klecknerb144d362013-05-20 14:02:37 +00004584static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4585 if (!checkMicrosoftExt(S, Attr))
4586 return;
4587 // Check linkage after possibly merging declaratinos. See
4588 // checkAttributesAfterMerging().
4589 D->addAttr(::new (S.Context)
4590 SelectAnyAttr(Attr.getRange(), S.Context,
4591 Attr.getAttributeSpellingListIndex()));
4592}
4593
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004594/// Handles semantic checking for features that are common to all attributes,
4595/// such as checking whether a parameter was properly specified, or the correct
4596/// number of arguments were passed, etc.
4597static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4598 const AttributeList &Attr) {
4599 // Several attributes carry different semantics than the parsing requires, so
4600 // those are opted out of the common handling.
4601 //
4602 // We also bail on unknown and ignored attributes because those are handled
4603 // as part of the target-specific handling logic.
4604 if (Attr.hasCustomParsing() ||
4605 Attr.getKind() == AttributeList::UnknownAttribute ||
4606 Attr.getKind() == AttributeList::IgnoredAttribute)
4607 return false;
4608
4609 // If there are no optional arguments, then checking for the argument count
4610 // is trivial.
4611 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
4612 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4613 return true;
4614 return false;
4615}
4616
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004617//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004618// Top Level Sema Entry Points
4619//===----------------------------------------------------------------------===//
4620
Richard Smithf8a75c32013-08-29 00:47:48 +00004621/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4622/// the attribute applies to decls. If the attribute is a type attribute, just
4623/// silently ignore it if a GNU attribute.
4624static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4625 const AttributeList &Attr,
4626 bool IncludeCXX11Attributes) {
4627 if (Attr.isInvalid())
4628 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004629
Richard Smithf8a75c32013-08-29 00:47:48 +00004630 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4631 // instead.
4632 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4633 return;
4634
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004635 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4636 return;
4637
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004638 switch (Attr.getKind()) {
Richard Smith10876ef2013-01-17 01:30:42 +00004639 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4640 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4641 case AttributeList::AT_IBOutletCollection:
4642 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004643 case AttributeList::AT_AddressSpace:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004644 case AttributeList::AT_ObjCGC:
4645 case AttributeList::AT_VectorSize:
4646 case AttributeList::AT_NeonVectorType:
4647 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004648 case AttributeList::AT_Ptr32:
4649 case AttributeList::AT_Ptr64:
4650 case AttributeList::AT_SPtr:
4651 case AttributeList::AT_UPtr:
Mike Stumpd3bb5572009-07-24 19:02:52 +00004652 // Ignore these, these are type attributes, handled by
4653 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004654 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004655 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4656 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4657 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4658 case AttributeList::AT_AlwaysInline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004659 handleAlwaysInlineAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004660 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004661 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00004662 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004663 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4664 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4665 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004666 handleDependencyAttr(S, scope, D, Attr);
4667 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004668 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4669 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4670 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004671 case AttributeList::AT_CXX11NoReturn:
4672 handleCXX11NoReturnAttr(S, D, Attr);
4673 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004674 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004675 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004676 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004677 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4678 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004679 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004680 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004681 case AttributeList::AT_MinSize:
4682 handleMinSizeAttr(S, D, Attr);
4683 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004684 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4685 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4686 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman9a226212013-08-28 23:13:26 +00004687 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4688 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004689 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4690 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004691 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004692 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004693 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4694 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00004695 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004696 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4697 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00004698 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Richard Smith852e9ce2013-11-27 01:46:48 +00004699 case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004700 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4701 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4702 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4703 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4704 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4705 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4706 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004707
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004708 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004709 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004710 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004711 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004712
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004713 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004714 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4715
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004716 case AttributeList::AT_ObjCRequiresSuper:
4717 handleObjCRequiresSuperAttr(S, D, Attr); break;
4718
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004719 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00004720 handleNSBridgedAttr(S, scope, D, Attr); break;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004721
4722 case AttributeList::AT_ObjCBridge:
4723 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004724
4725 case AttributeList::AT_ObjCBridgeMutable:
4726 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00004727
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004728 case AttributeList::AT_CFAuditedTransfer:
4729 case AttributeList::AT_CFUnknownTransfer:
John McCall32f5fe12011-09-30 05:12:12 +00004730 handleCFTransferAttr(S, D, Attr); break;
4731
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004732 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004733 case AttributeList::AT_CFConsumed:
4734 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4735 case AttributeList::AT_NSConsumesSelf:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004736 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004737
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004738 case AttributeList::AT_NSReturnsAutoreleased:
4739 case AttributeList::AT_NSReturnsNotRetained:
4740 case AttributeList::AT_CFReturnsNotRetained:
4741 case AttributeList::AT_NSReturnsRetained:
4742 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004743 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004744
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004745 case AttributeList::AT_WorkGroupSizeHint:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004746 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004747 handleWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00004748
Joey Goulyaba589c2013-03-08 09:42:32 +00004749 case AttributeList::AT_VecTypeHint:
4750 handleVecTypeHint(S, D, Attr); break;
4751
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004752 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004753 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004754
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004755 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4756 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4757 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004758 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004759 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004760 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00004761 handleArcWeakrefUnavailableAttr (S, D, Attr);
4762 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004763 case AttributeList::AT_ObjCRootClass:
Patrick Beardacfbe9e2012-04-06 18:12:22 +00004764 handleObjCRootClassAttr(S, D, Attr);
4765 break;
Ted Kremenek28eace62013-11-23 01:01:34 +00004766 case AttributeList::AT_ObjCSuppressProtocol:
4767 handleObjCSuppresProtocolAttr(S, D, Attr);
4768 break;
4769 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00004770 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00004771 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004772 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4773 case AttributeList::AT_ReturnsTwice:
Rafael Espindola70107f92011-10-03 14:59:42 +00004774 handleReturnsTwiceAttr(S, D, Attr);
4775 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004776 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004777 case AttributeList::AT_Visibility:
4778 handleVisibilityAttr(S, D, Attr, false);
4779 break;
4780 case AttributeList::AT_TypeVisibility:
4781 handleVisibilityAttr(S, D, Attr, true);
4782 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004783 case AttributeList::AT_WarnUnused:
4784 handleWarnUnusedAttr(S, D, Attr);
4785 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004786 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004787 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004788 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4789 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4790 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4791 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004792 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004793 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004794 case AttributeList::AT_ObjCException:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004795 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00004796 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004797 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004798 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004799 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004800 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4801 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4802 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4803 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4804 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4805 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4806 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4807 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4808 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004809 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004810 // Just ignore
4811 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004812 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00004813 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00004814 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004815 case AttributeList::AT_StdCall:
4816 case AttributeList::AT_CDecl:
4817 case AttributeList::AT_FastCall:
4818 case AttributeList::AT_ThisCall:
4819 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004820 case AttributeList::AT_MSABI:
4821 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004822 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004823 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004824 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004825 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004826 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004827 case AttributeList::AT_OpenCLKernel:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004828 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00004829 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004830 case AttributeList::AT_OpenCLImageAccess:
4831 handleOpenCLImageAccessAttr(S, D, Attr);
4832 break;
John McCall8d32c052012-05-22 21:28:12 +00004833
4834 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004835 case AttributeList::AT_MsStruct:
John McCall8d32c052012-05-22 21:28:12 +00004836 handleMsStructAttr(S, D, Attr);
4837 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004838 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004839 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004840 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004841 case AttributeList::AT_SingleInheritance:
4842 case AttributeList::AT_MultipleInheritance:
4843 case AttributeList::AT_VirtualInheritance:
John McCall8d32c052012-05-22 21:28:12 +00004844 handleInheritanceAttr(S, D, Attr);
4845 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004846 case AttributeList::AT_Win64:
John McCall8d32c052012-05-22 21:28:12 +00004847 handlePortabilityAttr(S, D, Attr);
4848 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004849 case AttributeList::AT_ForceInline:
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004850 handleForceInlineAttr(S, D, Attr);
4851 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004852 case AttributeList::AT_SelectAny:
4853 handleSelectAnyAttr(S, D, Attr);
4854 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004855
4856 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004857 case AttributeList::AT_AssertExclusiveLock:
4858 handleAssertExclusiveLockAttr(S, D, Attr);
4859 break;
4860 case AttributeList::AT_AssertSharedLock:
4861 handleAssertSharedLockAttr(S, D, Attr);
4862 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004863 case AttributeList::AT_GuardedVar:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004864 handleGuardedVarAttr(S, D, Attr);
4865 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004866 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004867 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004868 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004869 case AttributeList::AT_ScopedLockable:
Michael Han3be3b442012-07-23 18:48:41 +00004870 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004871 break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004872 case AttributeList::AT_NoSanitizeAddress:
4873 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004874 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004875 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004876 handleNoThreadSafetyAnalysis(S, D, Attr);
4877 break;
4878 case AttributeList::AT_NoSanitizeThread:
4879 handleNoSanitizeThread(S, D, Attr);
4880 break;
4881 case AttributeList::AT_NoSanitizeMemory:
4882 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004883 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004884 case AttributeList::AT_Lockable:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004885 handleLockableAttr(S, D, Attr);
4886 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004887 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004888 handleGuardedByAttr(S, D, Attr);
4889 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004890 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004891 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004892 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004893 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004894 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004895 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004896 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004897 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004898 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004899 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004900 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004901 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004902 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004903 handleLockReturnedAttr(S, D, Attr);
4904 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004905 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004906 handleLocksExcludedAttr(S, D, Attr);
4907 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004908 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004909 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004910 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004911 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004912 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004913 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004914 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004915 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004916 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004917 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004918 handleUnlockFunAttr(S, D, Attr);
4919 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004920 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004921 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004922 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004923 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004924 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004925 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004926
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004927 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004928 case AttributeList::AT_Consumable:
4929 handleConsumableAttr(S, D, Attr);
4930 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004931 case AttributeList::AT_CallableWhen:
4932 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004933 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004934 case AttributeList::AT_ParamTypestate:
4935 handleParamTypestateAttr(S, D, Attr);
4936 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004937 case AttributeList::AT_ReturnTypestate:
4938 handleReturnTypestateAttr(S, D, Attr);
4939 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004940 case AttributeList::AT_SetTypestate:
4941 handleSetTypestateAttr(S, D, Attr);
4942 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004943 case AttributeList::AT_TestTypestate:
4944 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004945 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004946
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004947 // Type safety attributes.
4948 case AttributeList::AT_ArgumentWithTypeTag:
4949 handleArgumentWithTypeTagAttr(S, D, Attr);
4950 break;
4951 case AttributeList::AT_TypeTagForDatatype:
4952 handleTypeTagForDatatypeAttr(S, D, Attr);
4953 break;
4954
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004955 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004956 // Ask target about the attribute.
4957 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4958 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004959 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4960 diag::warn_unhandled_ms_attribute_ignored :
4961 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004962 break;
4963 }
4964}
4965
4966/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4967/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004968void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004969 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004970 bool IncludeCXX11Attributes) {
4971 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004972 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004973
4974 // GCC accepts
4975 // static int a9 __attribute__((weakref));
4976 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004977 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004978 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00004979 cast<NamedDecl>(D)->getNameAsString();
4980 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004981 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004982 }
4983}
4984
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004985// Annotation attributes are the only attributes allowed after an access
4986// specifier.
4987bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4988 const AttributeList *AttrList) {
4989 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004990 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004991 handleAnnotateAttr(*this, ASDecl, *l);
4992 } else {
4993 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4994 return true;
4995 }
4996 }
4997
4998 return false;
4999}
5000
John McCall42856de2011-10-01 05:17:03 +00005001/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5002/// contains any decl attributes that we should warn about.
5003static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5004 for ( ; A; A = A->getNext()) {
5005 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00005006 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00005007 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5008
5009 if (A->getKind() == AttributeList::UnknownAttribute) {
5010 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5011 << A->getName() << A->getRange();
5012 } else {
5013 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5014 << A->getName() << A->getRange();
5015 }
5016 }
5017}
5018
5019/// checkUnusedDeclAttributes - Given a declarator which is not being
5020/// used to build a declaration, complain about any decl attributes
5021/// which might be lying around on it.
5022void Sema::checkUnusedDeclAttributes(Declarator &D) {
5023 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5024 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5025 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5026 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5027}
5028
Ryan Flynn7d470f32009-07-30 03:15:39 +00005029/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00005030/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00005031NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5032 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00005033 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005034 NamedDecl *NewD = 0;
5035 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00005036 FunctionDecl *NewFD;
5037 // FIXME: Missing call to CheckFunctionDeclaration().
5038 // FIXME: Mangling?
5039 // FIXME: Is the qualifier info correct?
5040 // FIXME: Is the DeclContext correct?
5041 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5042 Loc, Loc, DeclarationName(II),
5043 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005044 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00005045 FD->hasPrototype(),
5046 false/*isConstexprSpecified*/);
5047 NewD = NewFD;
5048
5049 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00005050 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00005051
5052 // Fake up parameter variables; they are declared as if this were
5053 // a typedef.
5054 QualType FDTy = FD->getType();
5055 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5056 SmallVector<ParmVarDecl*, 16> Params;
5057 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5058 AE = FT->arg_type_end(); AI != AE; ++AI) {
5059 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5060 Param->setScopeInfo(0, Params.size());
5061 Params.push_back(Param);
5062 }
David Blaikie9c70e042011-09-21 18:16:56 +00005063 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00005064 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005065 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5066 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00005067 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00005068 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005069 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00005070 if (VD->getQualifier()) {
5071 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00005072 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00005073 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005074 }
5075 return NewD;
5076}
5077
James Dennett634962f2012-06-14 21:40:34 +00005078/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00005079/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00005080void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00005081 if (W.getUsed()) return; // only do this once
5082 W.setUsed(true);
5083 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5084 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00005085 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005086 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5087 NDId->getName()));
5088 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00005089 WeakTopLevelDecl.push_back(NewD);
5090 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5091 // to insert Decl at TU scope, sorry.
5092 DeclContext *SavedContext = CurContext;
5093 CurContext = Context.getTranslationUnitDecl();
5094 PushOnScopeChains(NewD, S);
5095 CurContext = SavedContext;
5096 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005097 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005098 }
5099}
5100
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005101void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5102 // It's valid to "forward-declare" #pragma weak, in which case we
5103 // have to do this.
5104 LoadExternalWeakUndeclaredIdentifiers();
5105 if (!WeakUndeclaredIdentifiers.empty()) {
5106 NamedDecl *ND = NULL;
5107 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5108 if (VD->isExternC())
5109 ND = VD;
5110 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5111 if (FD->isExternC())
5112 ND = FD;
5113 if (ND) {
5114 if (IdentifierInfo *Id = ND->getIdentifier()) {
5115 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5116 = WeakUndeclaredIdentifiers.find(Id);
5117 if (I != WeakUndeclaredIdentifiers.end()) {
5118 WeakInfo W = I->second;
5119 DeclApplyPragmaWeak(S, ND, W);
5120 WeakUndeclaredIdentifiers[Id] = W;
5121 }
5122 }
5123 }
5124 }
5125}
5126
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005127/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5128/// it, apply them to D. This is a bit tricky because PD can have attributes
5129/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00005130void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005131 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00005132 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00005133 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005134
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005135 // Walk the declarator structure, applying decl attributes that were in a type
5136 // position to the decl itself. This handles cases like:
5137 // int *__attr__(x)** D;
5138 // when X is a decl attribute.
5139 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5140 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00005141 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005142
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005143 // Finally, apply any attributes on the decl itself.
5144 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00005145 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005146}
John McCall28a6aea2009-11-04 02:18:39 +00005147
John McCall31168b02011-06-15 23:02:42 +00005148/// Is the given declaration allowed to use a forbidden type?
5149static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5150 // Private ivars are always okay. Unfortunately, people don't
5151 // always properly make their ivars private, even in system headers.
5152 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005153 // Function declarations in sys headers will be marked unavailable.
5154 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5155 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005156 return false;
5157
5158 // Require it to be declared in a system header.
5159 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5160}
5161
5162/// Handle a delayed forbidden-type diagnostic.
5163static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5164 Decl *decl) {
5165 if (decl && isForbiddenTypeAllowed(S, decl)) {
5166 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5167 "this system declaration uses an unsupported type"));
5168 return;
5169 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005170 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005171 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005172 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005173 // kind of forbidden type messages on unavailable functions.
5174 if (FD->hasAttr<UnavailableAttr>() &&
5175 diag.getForbiddenTypeDiagnostic() ==
5176 diag::err_arc_array_param_no_ownership) {
5177 diag.Triggered = true;
5178 return;
5179 }
5180 }
John McCall31168b02011-06-15 23:02:42 +00005181
5182 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5183 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5184 diag.Triggered = true;
5185}
5186
John McCall2ec85372012-05-07 06:16:41 +00005187void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5188 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005189 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005190 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005191
John McCall2ec85372012-05-07 06:16:41 +00005192 // When delaying diagnostics to run in the context of a parsed
5193 // declaration, we only want to actually emit anything if parsing
5194 // succeeds.
5195 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005196
John McCall2ec85372012-05-07 06:16:41 +00005197 // We emit all the active diagnostics in this pool or any of its
5198 // parents. In general, we'll get one pool for the decl spec
5199 // and a child pool for each declarator; in a decl group like:
5200 // deprecated_typedef foo, *bar, baz();
5201 // only the declarator pops will be passed decls. This is correct;
5202 // we really do need to consider delayed diagnostics from the decl spec
5203 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00005204 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00005205 do {
John McCall6347b682012-05-07 06:16:58 +00005206 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00005207 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5208 // This const_cast is a bit lame. Really, Triggered should be mutable.
5209 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00005210 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00005211 continue;
5212
John McCallc1465822011-02-14 07:13:47 +00005213 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00005214 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00005215 // Don't bother giving deprecation diagnostics if the decl is invalid.
5216 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00005217 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005218 break;
5219
5220 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00005221 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005222 break;
John McCall31168b02011-06-15 23:02:42 +00005223
5224 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00005225 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00005226 break;
John McCall86121512010-01-27 03:50:35 +00005227 }
5228 }
John McCall2ec85372012-05-07 06:16:41 +00005229 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00005230}
5231
John McCall6347b682012-05-07 06:16:58 +00005232/// Given a set of delayed diagnostics, re-emit them as if they had
5233/// been delayed in the current context instead of in the given pool.
5234/// Essentially, this just moves them to the current pool.
5235void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5236 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5237 assert(curPool && "re-emitting in undelayed context not supported");
5238 curPool->steal(pool);
5239}
5240
John McCall28a6aea2009-11-04 02:18:39 +00005241static bool isDeclDeprecated(Decl *D) {
5242 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005243 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00005244 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00005245 // A category implicitly has the availability of the interface.
5246 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5247 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00005248 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5249 return false;
5250}
5251
Eli Friedman971bfa12012-08-08 21:52:41 +00005252static void
5253DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5254 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005255 const ObjCInterfaceDecl *UnknownObjCClass,
5256 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedman971bfa12012-08-08 21:52:41 +00005257 DeclarationName Name = D->getDeclName();
5258 if (!Message.empty()) {
5259 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5260 S.Diag(D->getLocation(),
5261 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5262 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005263 if (ObjCPropery)
5264 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5265 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005266 } else if (!UnknownObjCClass) {
5267 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5268 S.Diag(D->getLocation(),
5269 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5270 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005271 if (ObjCPropery)
5272 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5273 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005274 } else {
5275 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5276 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5277 }
5278}
5279
John McCallb45a1e72010-08-26 02:13:20 +00005280void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00005281 Decl *Ctx) {
5282 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00005283 return;
5284
John McCall86121512010-01-27 03:50:35 +00005285 DD.Triggered = true;
Eli Friedman971bfa12012-08-08 21:52:41 +00005286 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5287 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005288 DD.getUnknownObjCClass(),
5289 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00005290}
5291
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005292void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00005293 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005294 const ObjCInterfaceDecl *UnknownObjCClass,
5295 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00005296 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00005297 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005298 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5299 UnknownObjCClass,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005300 ObjCProperty,
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005301 Message));
John McCall28a6aea2009-11-04 02:18:39 +00005302 return;
5303 }
5304
5305 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00005306 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00005307 return;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005308 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00005309}