blob: 24009768f46b16d824b4d62cf611a9d4b869bce4 [file] [log] [blame]
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
John McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +000017#include "clang/AST/CXXInheritance.h"
John McCall28a0cf72010-08-25 07:42:41 +000018#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000019#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000021#include "clang/AST/Expr.h"
Rafael Espindoladb77c4a2013-02-26 19:13:56 +000022#include "clang/AST/Mangle.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000023#include "clang/Basic/CharInfo.h"
John McCall31168b02011-06-15 23:02:42 +000024#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000025#include "clang/Basic/TargetInfo.h"
Benjamin Kramer6ee15622013-09-13 15:35:43 +000026#include "clang/Lex/Preprocessor.h"
John McCall8b0666c2010-08-20 18:27:03 +000027#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000028#include "clang/Sema/DelayedDiagnostic.h"
John McCallf1e8b342011-09-29 07:17:38 +000029#include "clang/Sema/Lookup.h"
Richard Smithe233fbf2013-01-28 22:42:45 +000030#include "clang/Sema/Scope.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000031#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000032using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000033using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000034
John McCall5fca7ea2011-03-02 12:29:23 +000035/// These constants match the enumerated choices of
36/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000037enum AttributeDeclKind {
John McCall5fca7ea2011-03-02 12:29:23 +000038 ExpectedFunction,
39 ExpectedUnion,
40 ExpectedVariableOrFunction,
41 ExpectedFunctionOrMethod,
42 ExpectedParameter,
John McCall5fca7ea2011-03-02 12:29:23 +000043 ExpectedFunctionMethodOrBlock,
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +000044 ExpectedFunctionMethodOrClass,
John McCall5fca7ea2011-03-02 12:29:23 +000045 ExpectedFunctionMethodOrParameter,
46 ExpectedClass,
John McCall5fca7ea2011-03-02 12:29:23 +000047 ExpectedVariable,
48 ExpectedMethod,
Caitlin Sadowski63fa6672011-07-28 20:12:35 +000049 ExpectedVariableFunctionOrLabel,
Douglas Gregor5c3cc422012-03-14 16:55:17 +000050 ExpectedFieldOrGlobalVar,
Hans Wennborgd3b01bc2012-06-23 11:51:46 +000051 ExpectedStruct,
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +000052 ExpectedVariableFunctionOrTag,
Richard Smith9eaab4b2013-02-01 08:25:07 +000053 ExpectedTLSVar,
54 ExpectedVariableOrField,
John McCalld041a9b2013-02-20 01:54:26 +000055 ExpectedVariableFieldOrTag,
Aaron Ballmanf90ccb02013-07-18 14:56:42 +000056 ExpectedTypeOrNamespace,
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +000057 ExpectedObjectiveCInterface,
Fariborz Jahanianf720f862013-11-19 17:42:25 +000058 ExpectedMethodOrProperty,
59 ExpectedStructOrUnion,
Aaron Ballmandbb634f2013-11-20 01:35:23 +000060 ExpectedStructOrUnionOrClass,
61 ExpectedType
John McCall5fca7ea2011-03-02 12:29:23 +000062};
63
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000064namespace AttributeLangSupport {
NAKAMURA Takumi01d27f92013-11-25 00:52:29 +000065 enum LANG {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +000066 C,
67 Cpp,
68 ObjC
69 };
70}
71
Chris Lattner58418ff2008-06-29 00:16:31 +000072//===----------------------------------------------------------------------===//
73// Helper functions
74//===----------------------------------------------------------------------===//
75
Chandler Carruthff4c4f02011-07-01 23:49:12 +000076static const FunctionType *getFunctionType(const Decl *D,
Ted Kremenek527042b2009-08-14 20:49:40 +000077 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000078 QualType Ty;
Chandler Carruthff4c4f02011-07-01 23:49:12 +000079 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000080 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000081 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000082 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000083 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000084 Ty = decl->getUnderlyingType();
85 else
86 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000087
Chris Lattner2c6fcf52008-06-26 18:38:35 +000088 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000089 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000090 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000091 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000092
John McCall9dd450b2009-09-21 23:43:11 +000093 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000094}
95
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000096// FIXME: We should provide an abstraction around a method or function
97// to provide the following bits of information.
98
Nuno Lopes518e3702009-12-20 23:11:08 +000099/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +0000100/// type (function or function-typed variable).
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000101static bool isFunction(const Decl *D) {
102 return getFunctionType(D, false) != NULL;
Ted Kremenek527042b2009-08-14 20:49:40 +0000103}
104
105/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000106/// type (function or function-typed variable) or an Objective-C
107/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000108static bool isFunctionOrMethod(const Decl *D) {
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +0000109 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000110}
111
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000112/// isFunctionOrMethodOrBlock - Return true if the given decl has function
113/// type (function or function-typed variable) or an Objective-C
114/// method or a block.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000115static bool isFunctionOrMethodOrBlock(const Decl *D) {
116 if (isFunctionOrMethod(D))
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000117 return true;
118 // check for block is more involved.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000119 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000120 QualType Ty = V->getType();
121 return Ty->isBlockPointerType();
122 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000123 return isa<BlockDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000124}
125
John McCall3882ace2011-01-05 12:14:39 +0000126/// Return true if the given decl has a declarator that should have
127/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000128static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +0000129 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000130 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
131 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +0000132}
133
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000134/// hasFunctionProto - Return true if the given decl has a argument
135/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000136/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000137static bool hasFunctionProto(const Decl *D) {
138 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000139 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000140 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000141 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000142 return true;
143 }
144}
145
146/// getFunctionOrMethodNumArgs - Return number of function or method
147/// arguments. It is an error to call this on a K&R function (use
148/// hasFunctionProto first).
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000149static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
150 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000151 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000152 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000153 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000154 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000155}
156
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000157static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
158 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000159 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000160 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000161 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000162
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000163 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000164}
165
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000166static QualType getFunctionOrMethodResultType(const Decl *D) {
167 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000168 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000169 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000170}
171
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000172static bool isFunctionOrMethodVariadic(const Decl *D) {
173 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000174 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000175 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000176 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000177 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000178 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000179 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000180 }
181}
182
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000183static bool isInstanceMethod(const Decl *D) {
184 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000185 return MethodDecl->isInstance();
186 return false;
187}
188
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000189static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000190 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000191 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000192 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000193
John McCall96fa4842010-05-17 21:00:27 +0000194 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
195 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000196 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000197
John McCall96fa4842010-05-17 21:00:27 +0000198 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000199
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000200 // FIXME: Should we walk the chain of classes?
201 return ClsName == &Ctx.Idents.get("NSString") ||
202 ClsName == &Ctx.Idents.get("NSMutableString");
203}
204
Daniel Dunbar980c6692008-09-26 03:32:58 +0000205static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000206 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000207 if (!PT)
208 return false;
209
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000210 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000211 if (!RT)
212 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000213
Daniel Dunbar980c6692008-09-26 03:32:58 +0000214 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000215 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000216 return false;
217
218 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
219}
220
Richard Smithb87c4652013-10-31 21:23:20 +0000221static unsigned getNumAttributeArgs(const AttributeList &Attr) {
222 // FIXME: Include the type in the argument list.
223 return Attr.getNumArgs() + Attr.hasParsedType();
224}
225
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000226/// \brief Check if the attribute has exactly as many args as Num. May
227/// output an error.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000228static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000229 unsigned Num) {
230 if (getNumAttributeArgs(Attr) != Num) {
Aaron Ballmanb7243382013-07-23 19:30:11 +0000231 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
232 << Attr.getName() << Num;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000233 return false;
234 }
235
236 return true;
237}
238
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000239
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000240/// \brief Check if the attribute has at least as many args as Num. May
241/// output an error.
242static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
Richard Smithb87c4652013-10-31 21:23:20 +0000243 unsigned Num) {
244 if (getNumAttributeArgs(Attr) < Num) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000245 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
246 return false;
247 }
248
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000249 return true;
250}
251
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +0000252/// \brief If Expr is a valid integer constant, get the value of the integer
253/// expression and return success or failure. May output an error.
254static bool checkUInt32Argument(Sema &S, const AttributeList &Attr,
255 const Expr *Expr, uint32_t &Val,
256 unsigned Idx = UINT_MAX) {
257 llvm::APSInt I(32);
258 if (Expr->isTypeDependent() || Expr->isValueDependent() ||
259 !Expr->isIntegerConstantExpr(I, S.Context)) {
260 if (Idx != UINT_MAX)
261 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
262 << Attr.getName() << Idx << AANT_ArgumentIntegerConstant
263 << Expr->getSourceRange();
264 else
265 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
266 << Attr.getName() << AANT_ArgumentIntegerConstant
267 << Expr->getSourceRange();
268 return false;
269 }
270 Val = (uint32_t)I.getZExtValue();
271 return true;
272}
273
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000274/// \brief Check if IdxExpr is a valid argument index for a function or
275/// instance method D. May output an error.
276///
277/// \returns true if IdxExpr is a valid index.
278static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
279 StringRef AttrName,
280 SourceLocation AttrLoc,
281 unsigned AttrArgNum,
282 const Expr *IdxExpr,
283 uint64_t &Idx)
284{
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000285 assert(isFunctionOrMethod(D));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000286
287 // In C++ the implicit 'this' function parameter also counts.
288 // Parameters are counted from one.
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000289 bool HP = hasFunctionProto(D);
290 bool HasImplicitThisParam = isInstanceMethod(D);
291 bool IV = HP && isFunctionOrMethodVariadic(D);
292 unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) +
293 HasImplicitThisParam;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000294
295 llvm::APSInt IdxInt;
296 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
297 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +0000298 std::string Name = std::string("'") + AttrName.str() + std::string("'");
299 S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str()
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000300 << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000301 return false;
302 }
303
304 Idx = IdxInt.getLimitedValue();
Aaron Ballmanbe50eb82013-07-30 00:48:57 +0000305 if (Idx < 1 || (!IV && Idx > NumArgs)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000306 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
307 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
308 return false;
309 }
310 Idx--; // Convert to zero-based.
311 if (HasImplicitThisParam) {
312 if (Idx == 0) {
313 S.Diag(AttrLoc,
314 diag::err_attribute_invalid_implicit_this_argument)
315 << AttrName << IdxExpr->getSourceRange();
316 return false;
317 }
318 --Idx;
319 }
320
321 return true;
322}
323
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000324/// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
325/// If not emit an error and return false. If the argument is an identifier it
326/// will emit an error with a fixit hint and treat it as if it was a string
327/// literal.
Tim Northover6a6b63b2013-10-01 14:34:18 +0000328bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr,
329 unsigned ArgNum, StringRef &Str,
Tim Northovera484bc02013-10-01 14:34:25 +0000330 SourceLocation *ArgLocation) {
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000331 // Look for identifiers. If we have one emit a hint to fix it to a literal.
332 if (Attr.isArgIdent(ArgNum)) {
333 IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum);
Tim Northover6a6b63b2013-10-01 14:34:18 +0000334 Diag(Loc->Loc, diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000335 << Attr.getName() << AANT_ArgumentString
336 << FixItHint::CreateInsertion(Loc->Loc, "\"")
Tim Northover6a6b63b2013-10-01 14:34:18 +0000337 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\"");
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000338 Str = Loc->Ident->getName();
339 if (ArgLocation)
340 *ArgLocation = Loc->Loc;
341 return true;
342 }
343
344 // Now check for an actual string literal.
345 Expr *ArgExpr = Attr.getArgAsExpr(ArgNum);
346 StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
347 if (ArgLocation)
348 *ArgLocation = ArgExpr->getLocStart();
349
350 if (!Literal || !Literal->isAscii()) {
Tim Northover6a6b63b2013-10-01 14:34:18 +0000351 Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
Aaron Ballman3b1dde62013-09-13 19:35:18 +0000352 << Attr.getName() << AANT_ArgumentString;
353 return false;
354 }
355
356 Str = Literal->getString();
357 return true;
358}
359
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000360///
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000361/// \brief Check if passed in Decl is a field or potentially shared global var
362/// \return true if the Decl is a field or potentially shared global variable
363///
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000364static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000365 if (isa<FieldDecl>(D))
366 return true;
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000367 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Richard Smithfd3834f2013-04-13 02:43:54 +0000368 return vd->hasGlobalStorage() && !vd->getTLSKind();
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000369
370 return false;
371}
372
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000373/// \brief Check if the passed-in expression is of type int or bool.
374static bool isIntOrBool(Expr *Exp) {
375 QualType QT = Exp->getType();
376 return QT->isBooleanType() || QT->isIntegerType();
377}
378
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000379
380// Check to see if the type is a smart pointer of some kind. We assume
381// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000382static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
383 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
384 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000385 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000386 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000387
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000388 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
389 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000390 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000391 return false;
392
393 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000394}
395
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000396/// \brief Check if passed in Decl is a pointer type.
397/// Note that this function may produce an error message.
398/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000399static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
400 const AttributeList &Attr) {
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000401 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000402 QualType QT = vd->getType();
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000403 if (QT->isAnyPointerType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000404 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000405
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000406 if (const RecordType *RT = QT->getAs<RecordType>()) {
407 // If it's an incomplete type, it could be a smart pointer; skip it.
408 // (We don't want to force template instantiation if we can avoid it,
409 // since that would alter the order in which templates are instantiated.)
410 if (RT->isIncompleteType())
411 return true;
412
413 if (threadSafetyCheckIsSmartPointer(S, RT))
414 return true;
415 }
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000416
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000417 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000418 << Attr.getName()->getName() << QT;
419 } else {
420 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
421 << Attr.getName();
422 }
423 return false;
424}
425
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000426/// \brief Checks that the passed in QualType either is of RecordType or points
427/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000428static const RecordType *getRecordType(QualType QT) {
429 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000430 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000431
432 // Now check if we point to record type.
433 if (const PointerType *PT = QT->getAs<PointerType>())
434 return PT->getPointeeType()->getAs<RecordType>();
435
436 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000437}
438
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000439
Jordy Rose740b0c22012-05-08 03:27:22 +0000440static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
441 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000442 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
443 if (RT->getDecl()->getAttr<LockableAttr>())
444 return true;
445 return false;
446}
447
448
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000449/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000450/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000451static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
452 QualType Ty) {
453 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000454
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000455 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000456 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000457 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000458 << Attr.getName() << Ty.getAsString();
459 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000460 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000461
Michael Hana9171bc2012-08-03 17:40:43 +0000462 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000463 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000464 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000465
466 // Allow smart pointers to be used as lockable objects.
467 // FIXME -- Check the type that the smart pointer points to.
468 if (threadSafetyCheckIsSmartPointer(S, RT))
469 return;
470
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000471 // Check if the type is lockable.
472 RecordDecl *RD = RT->getDecl();
473 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000474 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000475
476 // Else check if any base classes are lockable.
477 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
478 CXXBasePaths BPaths(false, false);
479 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
480 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000481 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000482
483 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
484 << Attr.getName() << Ty.getAsString();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000485}
486
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000487/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000488/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000489/// \param Sidx The attribute argument index to start checking with.
490/// \param ParamIdxOk Whether an argument can be indexing into a function
491/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000492static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000493 const AttributeList &Attr,
494 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000495 int Sidx = 0,
496 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000497 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000498 Expr *ArgExp = Attr.getArgAsExpr(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000499
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000500 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000501 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000502 Args.push_back(ArgExp);
503 continue;
504 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000505
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000506 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000507 if (StrLit->getLength() == 0 ||
Benjamin Kramerca9fe142013-09-13 16:30:12 +0000508 (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000509 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000510 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000511 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000512 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000513 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000514
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000515 // We allow constant strings to be used as a placeholder for expressions
516 // that are not valid C++ syntax, but warn that they are ignored.
517 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
518 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000519 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000520 continue;
521 }
522
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000523 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000524
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000525 // A pointer to member expression of the form &MyClass::mu is treated
526 // specially -- we need to look at the type of the member.
527 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
528 if (UOp->getOpcode() == UO_AddrOf)
529 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
530 if (DRE->getDecl()->isCXXInstanceMember())
531 ArgTy = DRE->getDecl()->getType();
532
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000533 // First see if we can just cast to record type, or point to record type.
534 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000535
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000536 // Now check if we index into a record type function param.
537 if(!RT && ParamIdxOk) {
538 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000539 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
540 if(FD && IL) {
541 unsigned int NumParams = FD->getNumParams();
542 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000543 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
544 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
545 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000546 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
547 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000548 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000549 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000550 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000551 }
552 }
553
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000554 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000555
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000556 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000557 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000558}
559
Chris Lattner58418ff2008-06-29 00:16:31 +0000560//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000561// Attribute Implementations
562//===----------------------------------------------------------------------===//
563
Daniel Dunbar032db472008-07-31 22:40:48 +0000564// FIXME: All this manual attribute parsing code is gross. At the
565// least add some helper functions to check most argument patterns (#
566// and types of args).
567
Michael Hana9171bc2012-08-03 17:40:43 +0000568static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000569 const AttributeList &Attr) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000570 // D must be either a member field or global (potentially shared) variable.
571 if (!mayBeSharedVariable(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000572 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
573 << Attr.getName() << ExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000574 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000575 }
576
Michael Han3be3b442012-07-23 18:48:41 +0000577 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000578}
579
Michael Han3be3b442012-07-23 18:48:41 +0000580static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
581 if (!checkGuardedVarAttrCommon(S, D, Attr))
582 return;
Michael Hana9171bc2012-08-03 17:40:43 +0000583
Michael Han99315932013-01-24 16:46:58 +0000584 D->addAttr(::new (S.Context)
585 GuardedVarAttr(Attr.getRange(), S.Context,
586 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000587}
588
Michael Hana9171bc2012-08-03 17:40:43 +0000589static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000590 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000591 if (!checkGuardedVarAttrCommon(S, D, Attr))
592 return;
593
594 if (!threadSafetyCheckIsPointer(S, D, Attr))
595 return;
596
Michael Han99315932013-01-24 16:46:58 +0000597 D->addAttr(::new (S.Context)
598 PtGuardedVarAttr(Attr.getRange(), S.Context,
599 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000600}
601
Michael Hana9171bc2012-08-03 17:40:43 +0000602static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
603 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000604 Expr* &Arg) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000605 // D must be either a member field or global (potentially shared) variable.
606 if (!mayBeSharedVariable(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000607 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
608 << Attr.getName() << ExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000609 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000610 }
611
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000612 SmallVector<Expr*, 1> Args;
613 // check that all arguments are lockable objects
614 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
615 unsigned Size = Args.size();
616 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000617 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000618
Michael Han3be3b442012-07-23 18:48:41 +0000619 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000620
Michael Han3be3b442012-07-23 18:48:41 +0000621 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000622}
623
Michael Han3be3b442012-07-23 18:48:41 +0000624static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
625 Expr *Arg = 0;
626 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
627 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000628
Michael Han3be3b442012-07-23 18:48:41 +0000629 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
630}
631
Michael Hana9171bc2012-08-03 17:40:43 +0000632static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000633 const AttributeList &Attr) {
634 Expr *Arg = 0;
635 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
636 return;
637
638 if (!threadSafetyCheckIsPointer(S, D, Attr))
639 return;
640
641 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
642 S.Context, Arg));
643}
644
Michael Hana9171bc2012-08-03 17:40:43 +0000645static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000646 const AttributeList &Attr) {
Caitlin Sadowski086fb952011-09-16 00:35:54 +0000647 // FIXME: Lockable structs for C code.
David Blaikie021221d2013-07-29 18:24:03 +0000648 if (!isa<RecordDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000649 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
650 << Attr.getName() << ExpectedStructOrUnionOrClass;
Michael Han3be3b442012-07-23 18:48:41 +0000651 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000652 }
653
Michael Han3be3b442012-07-23 18:48:41 +0000654 return true;
655}
656
657static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
658 if (!checkLockableAttrCommon(S, D, Attr))
659 return;
660
661 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
662}
663
Michael Hana9171bc2012-08-03 17:40:43 +0000664static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000665 const AttributeList &Attr) {
666 if (!checkLockableAttrCommon(S, D, Attr))
667 return;
668
Michael Han99315932013-01-24 16:46:58 +0000669 D->addAttr(::new (S.Context)
670 ScopedLockableAttr(Attr.getRange(), S.Context,
671 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000672}
673
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000674static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D,
675 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000676 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000677 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
678 << Attr.getName() << ExpectedFunctionOrMethod;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000679 return;
680 }
681
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000682 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000683 S.Context));
684}
685
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000686static void handleNoSanitizeAddressAttr(Sema &S, Decl *D,
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000687 const AttributeList &Attr) {
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000688 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000689 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000690 << Attr.getName() << ExpectedFunctionOrMethod;
691 return;
692 }
693
Michael Han99315932013-01-24 16:46:58 +0000694 D->addAttr(::new (S.Context)
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000695 NoSanitizeAddressAttr(Attr.getRange(), S.Context,
696 Attr.getAttributeSpellingListIndex()));
697}
698
699static void handleNoSanitizeMemory(Sema &S, Decl *D,
700 const AttributeList &Attr) {
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000701 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
702 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
703 << Attr.getName() << ExpectedFunctionOrMethod;
704 return;
705 }
706
707 D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(),
708 S.Context));
709}
710
711static void handleNoSanitizeThread(Sema &S, Decl *D,
712 const AttributeList &Attr) {
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000713 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
714 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
715 << Attr.getName() << ExpectedFunctionOrMethod;
716 return;
717 }
718
719 D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(),
720 S.Context));
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000721}
722
Michael Hana9171bc2012-08-03 17:40:43 +0000723static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
724 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000725 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000726 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000727 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000728
729 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000730 ValueDecl *VD = dyn_cast<ValueDecl>(D);
731 if (!VD || !mayBeSharedVariable(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000732 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
733 << Attr.getName() << ExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000734 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000735 }
736
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000737 // Check that this attribute only applies to lockable types.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000738 QualType QT = VD->getType();
739 if (!QT->isDependentType()) {
740 const RecordType *RT = getRecordType(QT);
741 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000742 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000743 << Attr.getName();
744 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000745 }
746 }
747
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000748 // Check that all arguments are lockable objects.
749 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000750 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000751 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000752
Michael Han3be3b442012-07-23 18:48:41 +0000753 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000754}
755
Michael Hana9171bc2012-08-03 17:40:43 +0000756static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000757 const AttributeList &Attr) {
758 SmallVector<Expr*, 1> Args;
759 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
760 return;
761
762 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000763 D->addAttr(::new (S.Context)
764 AcquiredAfterAttr(Attr.getRange(), S.Context,
765 StartArg, Args.size(),
766 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000767}
768
Michael Hana9171bc2012-08-03 17:40:43 +0000769static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000770 const AttributeList &Attr) {
771 SmallVector<Expr*, 1> Args;
772 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
773 return;
774
775 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000776 D->addAttr(::new (S.Context)
777 AcquiredBeforeAttr(Attr.getRange(), S.Context,
778 StartArg, Args.size(),
779 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000780}
781
Michael Hana9171bc2012-08-03 17:40:43 +0000782static bool checkLockFunAttrCommon(Sema &S, Decl *D,
783 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000784 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000785 // zero or more arguments ok
786
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000787 // check that the attribute is applied to a function
788 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000789 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
790 << Attr.getName() << ExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000791 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000792 }
793
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000794 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000795 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000796
Michael Han3be3b442012-07-23 18:48:41 +0000797 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000798}
799
Michael Hana9171bc2012-08-03 17:40:43 +0000800static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000801 const AttributeList &Attr) {
802 SmallVector<Expr*, 1> Args;
803 if (!checkLockFunAttrCommon(S, D, Attr, Args))
804 return;
805
806 unsigned Size = Args.size();
807 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000808 D->addAttr(::new (S.Context)
809 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
810 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000811}
812
Michael Hana9171bc2012-08-03 17:40:43 +0000813static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000814 const AttributeList &Attr) {
815 SmallVector<Expr*, 1> Args;
816 if (!checkLockFunAttrCommon(S, D, Attr, Args))
817 return;
818
819 unsigned Size = Args.size();
820 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000821 D->addAttr(::new (S.Context)
822 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
823 StartArg, Size,
824 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000825}
826
DeLesley Hutchinsb6824312013-05-17 23:02:59 +0000827static void handleAssertSharedLockAttr(Sema &S, Decl *D,
828 const AttributeList &Attr) {
829 SmallVector<Expr*, 1> Args;
830 if (!checkLockFunAttrCommon(S, D, Attr, Args))
831 return;
832
833 unsigned Size = Args.size();
834 Expr **StartArg = Size == 0 ? 0 : &Args[0];
835 D->addAttr(::new (S.Context)
836 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
837 Attr.getAttributeSpellingListIndex()));
838}
839
840static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
841 const AttributeList &Attr) {
842 SmallVector<Expr*, 1> Args;
843 if (!checkLockFunAttrCommon(S, D, Attr, Args))
844 return;
845
846 unsigned Size = Args.size();
847 Expr **StartArg = Size == 0 ? 0 : &Args[0];
848 D->addAttr(::new (S.Context)
849 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
850 StartArg, Size,
851 Attr.getAttributeSpellingListIndex()));
852}
853
854
Michael Hana9171bc2012-08-03 17:40:43 +0000855static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
856 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000857 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000858 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000859 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000860
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000861 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000862 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
863 << Attr.getName() << ExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000864 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000865 }
866
Aaron Ballman00e99962013-08-31 01:11:41 +0000867 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman29982272013-07-23 14:03:57 +0000868 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000869 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Han3be3b442012-07-23 18:48:41 +0000870 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000871 }
872
873 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000874 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000875
Michael Han3be3b442012-07-23 18:48:41 +0000876 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000877}
878
Michael Hana9171bc2012-08-03 17:40:43 +0000879static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000880 const AttributeList &Attr) {
881 SmallVector<Expr*, 2> Args;
882 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
883 return;
884
Michael Han99315932013-01-24 16:46:58 +0000885 D->addAttr(::new (S.Context)
886 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000887 Attr.getArgAsExpr(0),
888 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000889 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000890}
891
Michael Hana9171bc2012-08-03 17:40:43 +0000892static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000893 const AttributeList &Attr) {
894 SmallVector<Expr*, 2> Args;
895 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
896 return;
897
Michael Han99315932013-01-24 16:46:58 +0000898 D->addAttr(::new (S.Context)
899 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman00e99962013-08-31 01:11:41 +0000900 Attr.getArgAsExpr(0),
901 Args.data(), Args.size(),
Michael Han99315932013-01-24 16:46:58 +0000902 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000903}
904
Michael Hana9171bc2012-08-03 17:40:43 +0000905static bool checkLocksRequiredCommon(Sema &S, Decl *D,
906 const AttributeList &Attr,
Craig Topper5603df42013-07-05 19:34:19 +0000907 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000908 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000909 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000910
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000911 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000912 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
913 << Attr.getName() << ExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000914 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000915 }
916
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000917 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000918 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko8f8930f2013-05-03 15:05:50 +0000919 if (Args.empty())
Michael Han3be3b442012-07-23 18:48:41 +0000920 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000921
Michael Han3be3b442012-07-23 18:48:41 +0000922 return true;
923}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000924
Michael Hana9171bc2012-08-03 17:40:43 +0000925static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000926 const AttributeList &Attr) {
927 SmallVector<Expr*, 1> Args;
928 if (!checkLocksRequiredCommon(S, D, Attr, Args))
929 return;
930
931 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000932 D->addAttr(::new (S.Context)
933 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
934 StartArg, Args.size(),
935 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000936}
937
Michael Hana9171bc2012-08-03 17:40:43 +0000938static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000939 const AttributeList &Attr) {
940 SmallVector<Expr*, 1> Args;
941 if (!checkLocksRequiredCommon(S, D, Attr, Args))
942 return;
943
944 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000945 D->addAttr(::new (S.Context)
946 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
947 StartArg, Args.size(),
948 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000949}
950
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000951static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000952 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000953 // zero or more arguments ok
954
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000955 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000956 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
957 << Attr.getName() << ExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000958 return;
959 }
960
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000961 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000962 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000963 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000964 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000965 Expr **StartArg = Size == 0 ? 0 : &Args[0];
966
Michael Han99315932013-01-24 16:46:58 +0000967 D->addAttr(::new (S.Context)
968 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
969 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000970}
971
972static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000973 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000974 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000975 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
976 << Attr.getName() << ExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000977 return;
978 }
979
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000980 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000981 SmallVector<Expr*, 1> Args;
982 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
983 unsigned Size = Args.size();
984 if (Size == 0)
985 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000986
Michael Han99315932013-01-24 16:46:58 +0000987 D->addAttr(::new (S.Context)
988 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
989 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000990}
991
992static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000993 const AttributeList &Attr) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000994 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000995 return;
996
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000997 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Aaron Ballman07e27642013-11-20 21:41:42 +0000998 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
999 << Attr.getName() << ExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00001000 return;
1001 }
1002
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001003 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +00001004 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +00001005 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +00001006 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +00001007 if (Size == 0)
1008 return;
1009 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +00001010
Michael Han99315932013-01-24 16:46:58 +00001011 D->addAttr(::new (S.Context)
1012 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
1013 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00001014}
1015
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001016static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikie16f76d22013-09-06 01:28:43 +00001017 ConsumableAttr::ConsumedState DefaultState;
1018
1019 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +00001020 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1021 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1022 DefaultState)) {
1023 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
1024 << Attr.getName() << IL->Ident;
David Blaikie16f76d22013-09-06 01:28:43 +00001025 return;
1026 }
David Blaikie16f76d22013-09-06 01:28:43 +00001027 } else {
1028 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1029 << Attr.getName() << AANT_ArgumentIdentifier;
1030 return;
1031 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001032
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001033 if (!isa<CXXRecordDecl>(D)) {
1034 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1035 Attr.getName() << ExpectedClass;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001036 return;
1037 }
1038
1039 D->addAttr(::new (S.Context)
David Blaikie16f76d22013-09-06 01:28:43 +00001040 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001041 Attr.getAttributeSpellingListIndex()));
1042}
1043
1044static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
1045 const AttributeList &Attr) {
1046 ASTContext &CurrContext = S.getASTContext();
1047 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
1048
1049 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1050 if (!RD->hasAttr<ConsumableAttr>()) {
1051 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
1052 RD->getNameAsString();
1053
1054 return false;
1055 }
1056 }
1057
1058 return true;
1059}
1060
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001061
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001062static void handleCallableWhenAttr(Sema &S, Decl *D,
1063 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001064 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1065 return;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001066
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001067 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001068 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1069 Attr.getName() << ExpectedMethod;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001070 return;
1071 }
1072
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00001073 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1074 return;
1075
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001076 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
1077 for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) {
1078 CallableWhenAttr::ConsumedState CallableState;
1079
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001080 StringRef StateString;
1081 SourceLocation Loc;
1082 if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc))
1083 return;
1084
1085 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
DeLesley Hutchins69391772013-10-17 23:23:53 +00001086 CallableState)) {
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001087 S.Diag(Loc, diag::warn_attribute_type_not_supported)
1088 << Attr.getName() << StateString;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001089 return;
1090 }
Aaron Ballman4c9b7dc2013-10-05 22:45:34 +00001091
1092 States.push_back(CallableState);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001093 }
1094
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001095 D->addAttr(::new (S.Context)
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001096 CallableWhenAttr(Attr.getRange(), S.Context, States.data(),
1097 States.size(), Attr.getAttributeSpellingListIndex()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001098}
1099
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001100
DeLesley Hutchins69391772013-10-17 23:23:53 +00001101static void handleParamTypestateAttr(Sema &S, Decl *D,
1102 const AttributeList &Attr) {
1103 if (!checkAttributeNumArgs(S, Attr, 1)) return;
1104
1105 if (!isa<ParmVarDecl>(D)) {
1106 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1107 Attr.getName() << ExpectedParameter;
1108 return;
1109 }
1110
1111 ParamTypestateAttr::ConsumedState ParamState;
1112
1113 if (Attr.isArgIdent(0)) {
1114 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1115 StringRef StateString = Ident->Ident->getName();
1116
1117 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
1118 ParamState)) {
1119 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1120 << Attr.getName() << StateString;
1121 return;
1122 }
1123 } else {
1124 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1125 Attr.getName() << AANT_ArgumentIdentifier;
1126 return;
1127 }
1128
1129 // FIXME: This check is currently being done in the analysis. It can be
1130 // enabled here only after the parser propagates attributes at
1131 // template specialization definition, not declaration.
1132 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
1133 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1134 //
1135 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1136 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1137 // ReturnType.getAsString();
1138 // return;
1139 //}
1140
1141 D->addAttr(::new (S.Context)
1142 ParamTypestateAttr(Attr.getRange(), S.Context, ParamState,
1143 Attr.getAttributeSpellingListIndex()));
1144}
1145
1146
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001147static void handleReturnTypestateAttr(Sema &S, Decl *D,
1148 const AttributeList &Attr) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001149 if (!checkAttributeNumArgs(S, Attr, 1)) return;
1150
1151 if (!(isa<FunctionDecl>(D) || isa<ParmVarDecl>(D))) {
1152 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1153 Attr.getName() << ExpectedFunctionMethodOrParameter;
1154 return;
1155 }
1156
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001157 ReturnTypestateAttr::ConsumedState ReturnState;
1158
1159 if (Attr.isArgIdent(0)) {
Aaron Ballman682ee422013-09-11 19:47:58 +00001160 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1161 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1162 ReturnState)) {
1163 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
1164 << Attr.getName() << IL->Ident;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001165 return;
1166 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001167 } else {
1168 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1169 Attr.getName() << AANT_ArgumentIdentifier;
1170 return;
1171 }
1172
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001173 // FIXME: This check is currently being done in the analysis. It can be
1174 // enabled here only after the parser propagates attributes at
1175 // template specialization definition, not declaration.
1176 //QualType ReturnType;
1177 //
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001178 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1179 // ReturnType = Param->getType();
1180 //
1181 //} else if (const CXXConstructorDecl *Constructor =
1182 // dyn_cast<CXXConstructorDecl>(D)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00001183 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1184 //
1185 //} else {
1186 //
1187 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1188 //}
1189 //
1190 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1191 //
1192 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1193 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1194 // ReturnType.getAsString();
1195 // return;
1196 //}
1197
1198 D->addAttr(::new (S.Context)
1199 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1200 Attr.getAttributeSpellingListIndex()));
1201}
1202
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001203
1204static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001205 if (!checkAttributeNumArgs(S, Attr, 1))
1206 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001207
1208 if (!isa<CXXMethodDecl>(D)) {
1209 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1210 Attr.getName() << ExpectedMethod;
1211 return;
1212 }
1213
1214 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1215 return;
1216
1217 SetTypestateAttr::ConsumedState NewState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001218 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001219 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1220 StringRef Param = Ident->Ident->getName();
1221 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1222 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1223 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001224 return;
1225 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001226 } else {
1227 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1228 Attr.getName() << AANT_ArgumentIdentifier;
1229 return;
1230 }
1231
1232 D->addAttr(::new (S.Context)
1233 SetTypestateAttr(Attr.getRange(), S.Context, NewState,
1234 Attr.getAttributeSpellingListIndex()));
1235}
1236
Chris Wailes9385f9f2013-10-29 20:28:41 +00001237static void handleTestTypestateAttr(Sema &S, Decl *D,
1238 const AttributeList &Attr) {
Aaron Ballmandbd586f2013-10-14 23:26:04 +00001239 if (!checkAttributeNumArgs(S, Attr, 1))
1240 return;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001241
1242 if (!isa<CXXMethodDecl>(D)) {
1243 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1244 Attr.getName() << ExpectedMethod;
1245 return;
1246 }
1247
1248 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1249 return;
1250
Chris Wailes9385f9f2013-10-29 20:28:41 +00001251 TestTypestateAttr::ConsumedState TestState;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001252 if (Attr.isArgIdent(0)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001253 IdentifierLoc *Ident = Attr.getArgAsIdent(0);
1254 StringRef Param = Ident->Ident->getName();
Chris Wailes9385f9f2013-10-29 20:28:41 +00001255 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
Aaron Ballman91c98e12013-10-14 23:22:37 +00001256 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1257 << Attr.getName() << Param;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001258 return;
1259 }
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001260 } else {
1261 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1262 Attr.getName() << AANT_ArgumentIdentifier;
1263 return;
1264 }
1265
1266 D->addAttr(::new (S.Context)
Chris Wailes9385f9f2013-10-29 20:28:41 +00001267 TestTypestateAttr(Attr.getRange(), S.Context, TestState,
DeLesley Hutchins33a29342013-10-11 23:03:26 +00001268 Attr.getAttributeSpellingListIndex()));
1269}
1270
Chandler Carruthedc2c642011-07-02 00:01:44 +00001271static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1272 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +00001273 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1274 if (TD == 0) {
1275 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1276 // and type-ids.
Aaron Ballmandbb634f2013-11-20 01:35:23 +00001277 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) <<
1278 Attr.getName() << ExpectedType;
Chris Lattner4a927cb2008-06-28 23:36:30 +00001279 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001280 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001281
Richard Smith1f5a4322013-01-13 02:11:23 +00001282 // Remember this typedef decl, we will need it later for diagnostics.
1283 S.ExtVectorDecls.push_back(TD);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001284}
1285
Chandler Carruthedc2c642011-07-02 00:01:44 +00001286static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001287 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001288 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001289 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001290 // If the alignment is less than or equal to 8 bits, the packed attribute
1291 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +00001292 if (!FD->getType()->isDependentType() &&
1293 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001294 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +00001295 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001296 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001297 else
Michael Han99315932013-01-24 16:46:58 +00001298 FD->addAttr(::new (S.Context)
1299 PackedAttr(Attr.getRange(), S.Context,
1300 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001301 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001302 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001303}
1304
Chandler Carruthedc2c642011-07-02 00:01:44 +00001305static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman9ee2d0472012-10-12 23:29:20 +00001306 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001307 RD->addAttr(::new (S.Context)
1308 MsStructAttr(Attr.getRange(), S.Context,
1309 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +00001310 else
Aaron Ballmanb80f94b2013-11-20 22:22:04 +00001311 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1312 << Attr.getName() << ExpectedStructOrUnionOrClass;
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +00001313}
1314
Chandler Carruthedc2c642011-07-02 00:01:44 +00001315static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek1f672822010-02-18 03:08:58 +00001316 // The IBAction attributes only apply to instance methods.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001317 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek1f672822010-02-18 03:08:58 +00001318 if (MD->isInstanceMethod()) {
Michael Han99315932013-01-24 16:46:58 +00001319 D->addAttr(::new (S.Context)
1320 IBActionAttr(Attr.getRange(), S.Context,
1321 Attr.getAttributeSpellingListIndex()));
Ted Kremenek1f672822010-02-18 03:08:58 +00001322 return;
1323 }
1324
Ted Kremenekd68ec812011-02-04 06:54:16 +00001325 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek1f672822010-02-18 03:08:58 +00001326}
1327
Ted Kremenek7fd17232011-09-29 07:02:25 +00001328static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1329 // The IBOutlet/IBOutletCollection attributes only apply to instance
1330 // variables or properties of Objective-C classes. The outlet must also
1331 // have an object reference type.
1332 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1333 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001334 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001335 << Attr.getName() << VD->getType() << 0;
1336 return false;
1337 }
1338 }
1339 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1340 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001341 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001342 << Attr.getName() << PD->getType() << 1;
1343 return false;
1344 }
1345 }
1346 else {
1347 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1348 return false;
1349 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001350
Ted Kremenek7fd17232011-09-29 07:02:25 +00001351 return true;
1352}
1353
Chandler Carruthedc2c642011-07-02 00:01:44 +00001354static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek7fd17232011-09-29 07:02:25 +00001355 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001356 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001357
Michael Han99315932013-01-24 16:46:58 +00001358 D->addAttr(::new (S.Context)
1359 IBOutletAttr(Attr.getRange(), S.Context,
1360 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001361}
1362
Chandler Carruthedc2c642011-07-02 00:01:44 +00001363static void handleIBOutletCollection(Sema &S, Decl *D,
1364 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001365
1366 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman00e99962013-08-31 01:11:41 +00001367 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001368 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1369 << Attr.getName() << 1;
Ted Kremenek26bde772010-05-19 17:38:06 +00001370 return;
1371 }
1372
Ted Kremenek7fd17232011-09-29 07:02:25 +00001373 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001374 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001375
Richard Smithb1f9a282013-10-31 01:56:18 +00001376 ParsedType PT;
1377
1378 if (Attr.hasParsedType())
1379 PT = Attr.getTypeArg();
1380 else {
1381 PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(),
1382 S.getScopeForContext(D->getDeclContext()->getParent()));
1383 if (!PT) {
1384 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1385 return;
1386 }
Aaron Ballman00e99962013-08-31 01:11:41 +00001387 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001388
Richard Smithb87c4652013-10-31 21:23:20 +00001389 TypeSourceInfo *QTLoc = 0;
1390 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1391 if (!QTLoc)
1392 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc());
Richard Smithb1f9a282013-10-31 01:56:18 +00001393
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001394 // Diagnose use of non-object type in iboutletcollection attribute.
1395 // FIXME. Gnu attribute extension ignores use of builtin types in
1396 // attributes. So, __attribute__((iboutletcollection(char))) will be
1397 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001398 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Richard Smithb1f9a282013-10-31 01:56:18 +00001399 S.Diag(Attr.getLoc(),
1400 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1401 : diag::err_iboutletcollection_type) << QT;
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001402 return;
1403 }
Richard Smithb1f9a282013-10-31 01:56:18 +00001404
Michael Han99315932013-01-24 16:46:58 +00001405 D->addAttr(::new (S.Context)
Richard Smithb87c4652013-10-31 21:23:20 +00001406 IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc,
Michael Han99315932013-01-24 16:46:58 +00001407 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001408}
1409
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001410static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001411 if (const RecordType *UT = T->getAsUnionType())
1412 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1413 RecordDecl *UD = UT->getDecl();
1414 for (RecordDecl::field_iterator it = UD->field_begin(),
1415 itend = UD->field_end(); it != itend; ++it) {
1416 QualType QT = it->getType();
1417 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1418 T = QT;
1419 return;
1420 }
1421 }
1422 }
1423}
1424
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001425static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopese881ce22012-06-18 16:39:04 +00001426 if (!isFunctionOrMethod(D)) {
1427 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001428 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopese881ce22012-06-18 16:39:04 +00001429 return;
1430 }
1431
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001432 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1433 return;
1434
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001435 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001436 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001437 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001438 uint64_t Idx;
1439 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1440 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001441 return;
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001442
1443 // check if the function argument is of an integer type
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001444 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001445 if (!T->isIntegerType()) {
Aaron Ballman9d695092013-07-30 14:10:17 +00001446 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1447 << Attr.getName() << AANT_ArgumentIntegerConstant
1448 << Ex->getSourceRange();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001449 return;
1450 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001451 SizeArgs.push_back(Idx);
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001452 }
1453
1454 // check if the function returns a pointer
1455 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1456 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001457 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001458 }
1459
Michael Han99315932013-01-24 16:46:58 +00001460 D->addAttr(::new (S.Context)
1461 AllocSizeAttr(Attr.getRange(), S.Context,
1462 SizeArgs.data(), SizeArgs.size(),
1463 Attr.getAttributeSpellingListIndex()));
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001464}
1465
Chandler Carruthedc2c642011-07-02 00:01:44 +00001466static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001467 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1468 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001469 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001470 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001471 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001472 return;
1473 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001474
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001475 SmallVector<unsigned, 8> NonNullArgs;
1476 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001477 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001478 uint64_t Idx;
1479 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1480 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001481 return;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001482
1483 // Is the function argument a pointer type?
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001484 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001485 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001486
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001487 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001488 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +00001489 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +00001490 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001491 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001492 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001493
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001494 NonNullArgs.push_back(Idx);
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001495 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001496
1497 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1498 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001499 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001500 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1501 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001502 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001503 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001504 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001505 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001506
Ted Kremenek22813f42010-10-21 18:49:36 +00001507 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001508 if (NonNullArgs.empty()) {
1509 // Warn the trivial case only if attribute is not coming from a
1510 // macro instantiation.
1511 if (Attr.getLoc().isFileID())
1512 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001513 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001514 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001515 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001516
Nick Lewyckye1121512013-01-24 01:12:16 +00001517 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001518 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001519 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001520 D->addAttr(::new (S.Context)
1521 NonNullAttr(Attr.getRange(), S.Context, start, size,
1522 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001523}
1524
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001525static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) {
1526 switch (K) {
1527 case OwnershipAttr::Holds: return "'ownership_holds'";
1528 case OwnershipAttr::Takes: return "'ownership_takes'";
1529 case OwnershipAttr::Returns: return "'ownership_returns'";
1530 }
1531 llvm_unreachable("unknown ownership");
1532}
1533
Chandler Carruthedc2c642011-07-02 00:01:44 +00001534static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001535 // This attribute must be applied to a function declaration. The first
1536 // argument to the attribute must be an identifier, the name of the resource,
1537 // for example: malloc. The following arguments must be argument indexes, the
1538 // arguments must be of integer type for Returns, otherwise of pointer type.
Ted Kremenekd21139a2010-07-31 01:52:11 +00001539 // The difference between Holds and Takes is that a pointer may still be used
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001540 // after being held. free() should be __attribute((ownership_takes)), whereas
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001541 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001542
Aaron Ballman00e99962013-08-31 01:11:41 +00001543 if (!AL.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00001544 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001545 << AL.getName() << 1 << AANT_ArgumentIdentifier;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001546 return;
1547 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001548
Ted Kremenekd21139a2010-07-31 01:52:11 +00001549 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001550 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001551 switch (AL.getKind()) {
1552 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001553 K = OwnershipAttr::Takes;
Aaron Ballman00e99962013-08-31 01:11:41 +00001554 if (AL.getNumArgs() < 2) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001555 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001556 return;
1557 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001558 break;
1559 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001560 K = OwnershipAttr::Holds;
Aaron Ballman00e99962013-08-31 01:11:41 +00001561 if (AL.getNumArgs() < 2) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001562 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001563 return;
1564 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001565 break;
1566 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001567 K = OwnershipAttr::Returns;
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001568
Aaron Ballman00e99962013-08-31 01:11:41 +00001569 if (AL.getNumArgs() > 2) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001570 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001571 return;
1572 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001573 break;
1574 default:
1575 // This should never happen given how we are called.
1576 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +00001577 }
1578
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001579 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001580 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1581 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001582 return;
1583 }
1584
Aaron Ballman00e99962013-08-31 01:11:41 +00001585 StringRef Module = AL.getArgAsIdent(0)->Ident->getName();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001586
1587 // Normalize the argument, __foo__ becomes foo.
1588 if (Module.startswith("__") && Module.endswith("__"))
1589 Module = Module.substr(2, Module.size() - 4);
1590
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001591 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman00e99962013-08-31 01:11:41 +00001592 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1593 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001594 uint64_t Idx;
1595 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
Aaron Ballman00e99962013-08-31 01:11:41 +00001596 AL.getLoc(), i, Ex, Idx))
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001597 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00001598
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001599 // Is the function argument a pointer type?
1600 QualType T = getFunctionOrMethodArgType(D, Idx);
1601 int Err = -1; // No error
Ted Kremenekd21139a2010-07-31 01:52:11 +00001602 switch (K) {
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001603 case OwnershipAttr::Takes:
1604 case OwnershipAttr::Holds:
1605 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1606 Err = 0;
1607 break;
1608 case OwnershipAttr::Returns:
1609 if (!T->isIntegerType())
1610 Err = 1;
1611 break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001612 }
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001613 if (-1 != Err) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001614 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001615 << Ex->getSourceRange();
1616 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001617 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001618
1619 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001620 for (specific_attr_iterator<OwnershipAttr>
Aaron Ballman6e2dd7b2013-09-16 18:11:41 +00001621 i = D->specific_attr_begin<OwnershipAttr>(),
1622 e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) {
1623 if ((*i)->getOwnKind() != K && (*i)->args_end() !=
1624 std::find((*i)->args_begin(), (*i)->args_end(), Idx)) {
1625 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1626 << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind());
1627 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001628 }
1629 }
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00001630 OwnershipArgs.push_back(Idx);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001631 }
1632
1633 unsigned* start = OwnershipArgs.data();
1634 unsigned size = OwnershipArgs.size();
1635 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001636
Michael Han99315932013-01-24 16:46:58 +00001637 D->addAttr(::new (S.Context)
1638 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1639 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001640}
1641
Chandler Carruthedc2c642011-07-02 00:01:44 +00001642static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001643 // Check the attribute arguments.
1644 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00001645 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1646 << Attr.getName() << 1;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001647 return;
1648 }
1649
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001650 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall7a198ce2011-02-08 22:35:49 +00001651 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001652 << Attr.getName() << ExpectedVariableOrFunction;
John McCall7a198ce2011-02-08 22:35:49 +00001653 return;
1654 }
1655
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001656 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001657
Rafael Espindolac18086a2010-02-23 22:00:30 +00001658 // gcc rejects
1659 // class c {
1660 // static int a __attribute__((weakref ("v2")));
1661 // static int b() __attribute__((weakref ("f3")));
1662 // };
1663 // and ignores the attributes of
1664 // void f(void) {
1665 // static int a __attribute__((weakref ("v2")));
1666 // }
1667 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001668 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001669 if (!Ctx->isFileContext()) {
1670 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +00001671 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +00001672 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001673 }
1674
1675 // The GCC manual says
1676 //
1677 // At present, a declaration to which `weakref' is attached can only
1678 // be `static'.
1679 //
1680 // It also says
1681 //
1682 // Without a TARGET,
1683 // given as an argument to `weakref' or to `alias', `weakref' is
1684 // equivalent to `weak'.
1685 //
1686 // gcc 4.4.1 will accept
1687 // int a7 __attribute__((weakref));
1688 // as
1689 // int a7 __attribute__((weak));
1690 // This looks like a bug in gcc. We reject that for now. We should revisit
1691 // it if this behaviour is actually used.
1692
Rafael Espindolac18086a2010-02-23 22:00:30 +00001693 // GCC rejects
1694 // static ((alias ("y"), weakref)).
1695 // Should we? How to check that weakref is before or after alias?
1696
Aaron Ballmanfebff0c2013-09-09 23:40:31 +00001697 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1698 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1699 // StringRef parameter it was given anyway.
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001700 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001701 if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Rafael Espindolac18086a2010-02-23 22:00:30 +00001702 // GCC will accept anything as the argument of weakref. Should we
1703 // check for an existing decl?
Aaron Ballman3b1dde62013-09-13 19:35:18 +00001704 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
1705 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001706
Michael Han99315932013-01-24 16:46:58 +00001707 D->addAttr(::new (S.Context)
1708 WeakRefAttr(Attr.getRange(), S.Context,
1709 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001710}
1711
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001712static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1713 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00001714 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001715 return;
1716
Douglas Gregore8bbc122011-09-02 00:18:52 +00001717 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001718 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1719 return;
1720 }
1721
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001722 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001723
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001724 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00001725 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001726}
1727
Quentin Colombet4e172062012-11-01 23:55:47 +00001728static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Quentin Colombet4e172062012-11-01 23:55:47 +00001729 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1730 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1731 << Attr.getName() << ExpectedFunctionOrMethod;
1732 return;
1733 }
1734
Michael Han99315932013-01-24 16:46:58 +00001735 D->addAttr(::new (S.Context)
1736 MinSizeAttr(Attr.getRange(), S.Context,
1737 Attr.getAttributeSpellingListIndex()));
Quentin Colombet4e172062012-11-01 23:55:47 +00001738}
1739
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001740static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001741 if (!isa<FunctionDecl>(D)) {
1742 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1743 << Attr.getName() << ExpectedFunction;
1744 return;
1745 }
1746
1747 if (D->hasAttr<HotAttr>()) {
1748 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1749 << Attr.getName() << "hot";
1750 return;
1751 }
1752
Michael Han99315932013-01-24 16:46:58 +00001753 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1754 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001755}
1756
1757static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001758 if (!isa<FunctionDecl>(D)) {
1759 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1760 << Attr.getName() << ExpectedFunction;
1761 return;
1762 }
1763
1764 if (D->hasAttr<ColdAttr>()) {
1765 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1766 << Attr.getName() << "cold";
1767 return;
1768 }
1769
Michael Han99315932013-01-24 16:46:58 +00001770 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1771 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001772}
1773
Chandler Carruthedc2c642011-07-02 00:01:44 +00001774static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001775 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001776 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001777 << Attr.getName() << ExpectedFunction;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001778 return;
1779 }
1780
Michael Han99315932013-01-24 16:46:58 +00001781 D->addAttr(::new (S.Context)
1782 NakedAttr(Attr.getRange(), S.Context,
1783 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001784}
1785
Chandler Carruthedc2c642011-07-02 00:01:44 +00001786static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1787 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001788 if (!isa<FunctionDecl>(D)) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001789 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001790 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00001791 return;
1792 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001793
Michael Han99315932013-01-24 16:46:58 +00001794 D->addAttr(::new (S.Context)
1795 AlwaysInlineAttr(Attr.getRange(), S.Context,
1796 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar03a38442008-10-28 00:17:57 +00001797}
1798
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001799static void handleTLSModelAttr(Sema &S, Decl *D,
1800 const AttributeList &Attr) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001801 StringRef Model;
1802 SourceLocation LiteralLoc;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001803 // Check that it is a string.
Tim Northover6a6b63b2013-10-01 14:34:18 +00001804 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc))
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001805 return;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001806
Richard Smithfd3834f2013-04-13 02:43:54 +00001807 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001808 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1809 << Attr.getName() << ExpectedTLSVar;
1810 return;
1811 }
1812
1813 // Check that the value.
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001814 if (Model != "global-dynamic" && Model != "local-dynamic"
1815 && Model != "initial-exec" && Model != "local-exec") {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00001816 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001817 return;
1818 }
1819
Michael Han99315932013-01-24 16:46:58 +00001820 D->addAttr(::new (S.Context)
1821 TLSModelAttr(Attr.getRange(), S.Context, Model,
1822 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001823}
1824
Chandler Carruthedc2c642011-07-02 00:01:44 +00001825static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001826 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001827 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001828 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001829 D->addAttr(::new (S.Context)
1830 MallocAttr(Attr.getRange(), S.Context,
1831 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001832 return;
1833 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001834 }
1835
Ted Kremenek08479ae2009-08-15 00:51:46 +00001836 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001837}
1838
Chandler Carruthedc2c642011-07-02 00:01:44 +00001839static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han99315932013-01-24 16:46:58 +00001840 D->addAttr(::new (S.Context)
1841 MayAliasAttr(Attr.getRange(), S.Context,
1842 Attr.getAttributeSpellingListIndex()));
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001843}
1844
Chandler Carruthedc2c642011-07-02 00:01:44 +00001845static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001846 if (isa<VarDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001847 D->addAttr(::new (S.Context)
1848 NoCommonAttr(Attr.getRange(), S.Context,
1849 Attr.getAttributeSpellingListIndex()));
Eric Christopher515d87f2010-12-03 06:58:14 +00001850 else
1851 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001852 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001853}
1854
Chandler Carruthedc2c642011-07-02 00:01:44 +00001855static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001856 if (S.LangOpts.CPlusPlus) {
Aaron Ballman3db89662013-11-24 21:48:06 +00001857 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
1858 << Attr.getName() << AttributeLangSupport::Cpp;
Eli Friedman6fc7ad12013-06-20 22:55:04 +00001859 return;
1860 }
1861
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001862 if (isa<VarDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001863 D->addAttr(::new (S.Context)
1864 CommonAttr(Attr.getRange(), S.Context,
1865 Attr.getAttributeSpellingListIndex()));
Eric Christopher515d87f2010-12-03 06:58:14 +00001866 else
1867 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001868 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001869}
1870
Chandler Carruthedc2c642011-07-02 00:01:44 +00001871static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001872 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001873
1874 if (S.CheckNoReturnAttr(attr)) return;
1875
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001876 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001877 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001878 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001879 return;
1880 }
1881
Michael Han99315932013-01-24 16:46:58 +00001882 D->addAttr(::new (S.Context)
1883 NoReturnAttr(attr.getRange(), S.Context,
1884 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001885}
1886
1887bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001888 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall3882ace2011-01-05 12:14:39 +00001889 attr.setInvalid();
1890 return true;
1891 }
1892
1893 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001894}
1895
Chandler Carruthedc2c642011-07-02 00:01:44 +00001896static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1897 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001898
1899 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1900 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001901 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1902 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001903 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1904 && !VD->getType()->isFunctionPointerType())) {
1905 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001906 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001907 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001908 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001909 return;
1910 }
1911 }
1912
Michael Han99315932013-01-24 16:46:58 +00001913 D->addAttr(::new (S.Context)
1914 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1915 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001916}
1917
Richard Smith10876ef2013-01-17 01:30:42 +00001918static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1919 const AttributeList &Attr) {
1920 // C++11 [dcl.attr.noreturn]p1:
1921 // The attribute may be applied to the declarator-id in a function
1922 // declaration.
1923 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1924 if (!FD) {
1925 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1926 << Attr.getName() << ExpectedFunctionOrMethod;
1927 return;
1928 }
1929
Michael Han99315932013-01-24 16:46:58 +00001930 D->addAttr(::new (S.Context)
1931 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1932 Attr.getAttributeSpellingListIndex()));
Richard Smith10876ef2013-01-17 01:30:42 +00001933}
1934
John Thompsoncdb847ba2010-08-09 21:53:52 +00001935// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001936static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001937/*
1938 Returning a Vector Class in Registers
1939
Eric Christopherbc638a82010-12-01 22:13:54 +00001940 According to the PPU ABI specifications, a class with a single member of
1941 vector type is returned in memory when used as the return value of a function.
1942 This results in inefficient code when implementing vector classes. To return
1943 the value in a single vector register, add the vecreturn attribute to the
1944 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001945
1946 Example:
1947
1948 struct Vector
1949 {
1950 __vector float xyzw;
1951 } __attribute__((vecreturn));
1952
1953 Vector Add(Vector lhs, Vector rhs)
1954 {
1955 Vector result;
1956 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1957 return result; // This will be returned in a register
1958 }
1959*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001960 if (!isa<RecordDecl>(D)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001961 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001962 << Attr.getName() << ExpectedClass;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001963 return;
1964 }
1965
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001966 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001967 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1968 return;
1969 }
1970
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001971 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001972 int count = 0;
1973
1974 if (!isa<CXXRecordDecl>(record)) {
1975 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1976 return;
1977 }
1978
1979 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1980 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1981 return;
1982 }
1983
Eric Christopherbc638a82010-12-01 22:13:54 +00001984 for (RecordDecl::field_iterator iter = record->field_begin();
1985 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001986 if ((count == 1) || !iter->getType()->isVectorType()) {
1987 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1988 return;
1989 }
1990 count++;
1991 }
1992
Michael Han99315932013-01-24 16:46:58 +00001993 D->addAttr(::new (S.Context)
1994 VecReturnAttr(Attr.getRange(), S.Context,
1995 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001996}
1997
Richard Smithe233fbf2013-01-28 22:42:45 +00001998static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1999 const AttributeList &Attr) {
2000 if (isa<ParmVarDecl>(D)) {
2001 // [[carries_dependency]] can only be applied to a parameter if it is a
2002 // parameter of a function declaration or lambda.
2003 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
2004 S.Diag(Attr.getLoc(),
2005 diag::err_carries_dependency_param_not_function_decl);
2006 return;
2007 }
2008 } else if (!isa<FunctionDecl>(D)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002009 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002010 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002011 return;
2012 }
Richard Smithe233fbf2013-01-28 22:42:45 +00002013
2014 D->addAttr(::new (S.Context) CarriesDependencyAttr(
2015 Attr.getRange(), S.Context,
2016 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002017}
2018
Chandler Carruthedc2c642011-07-02 00:01:44 +00002019static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002020 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00002021 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002022 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002023 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00002024 return;
2025 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002026
Michael Han99315932013-01-24 16:46:58 +00002027 D->addAttr(::new (S.Context)
2028 UnusedAttr(Attr.getRange(), S.Context,
2029 Attr.getAttributeSpellingListIndex()));
Ted Kremenek39c59a82008-07-25 04:39:19 +00002030}
2031
Rafael Espindola70107f92011-10-03 14:59:42 +00002032static void handleReturnsTwiceAttr(Sema &S, Decl *D,
2033 const AttributeList &Attr) {
Rafael Espindola70107f92011-10-03 14:59:42 +00002034 if (!isa<FunctionDecl>(D)) {
2035 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2036 << Attr.getName() << ExpectedFunction;
2037 return;
2038 }
2039
Michael Han99315932013-01-24 16:46:58 +00002040 D->addAttr(::new (S.Context)
2041 ReturnsTwiceAttr(Attr.getRange(), S.Context,
2042 Attr.getAttributeSpellingListIndex()));
Rafael Espindola70107f92011-10-03 14:59:42 +00002043}
2044
Chandler Carruthedc2c642011-07-02 00:01:44 +00002045static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002046 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola87198cd2013-08-16 23:18:50 +00002047 if (VD->hasLocalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002048 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
2049 return;
2050 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002051 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002052 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002053 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002054 return;
2055 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002056
Michael Han99315932013-01-24 16:46:58 +00002057 D->addAttr(::new (S.Context)
2058 UsedAttr(Attr.getRange(), S.Context,
2059 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00002060}
2061
Chandler Carruthedc2c642011-07-02 00:01:44 +00002062static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00002063 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00002064 if (Attr.getNumArgs() > 1) {
2065 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00002066 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002067 }
Daniel Dunbar032db472008-07-31 22:40:48 +00002068
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002069 uint32_t priority = ConstructorAttr::DefaultPriority;
2070 if (Attr.getNumArgs() > 0 &&
2071 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
2072 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002073
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002074 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002075 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002076 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00002077 return;
2078 }
2079
Michael Han99315932013-01-24 16:46:58 +00002080 D->addAttr(::new (S.Context)
2081 ConstructorAttr(Attr.getRange(), S.Context, priority,
2082 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002083}
2084
Chandler Carruthedc2c642011-07-02 00:01:44 +00002085static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00002086 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00002087 if (Attr.getNumArgs() > 1) {
2088 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00002089 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002090 }
Daniel Dunbar032db472008-07-31 22:40:48 +00002091
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002092 uint32_t priority = ConstructorAttr::DefaultPriority;
2093 if (Attr.getNumArgs() > 0 &&
2094 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority))
2095 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002096
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002097 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002098 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002099 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00002100 return;
2101 }
2102
Michael Han99315932013-01-24 16:46:58 +00002103 D->addAttr(::new (S.Context)
2104 DestructorAttr(Attr.getRange(), S.Context, priority,
2105 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00002106}
2107
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002108template <typename AttrTy>
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00002109static void handleAttrWithMessage(Sema &S, Decl *D,
2110 const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00002111 unsigned NumArgs = Attr.getNumArgs();
2112 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00002113 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002114 return;
2115 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00002116
2117 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002118 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002119 if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002120 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002121
Michael Han99315932013-01-24 16:46:58 +00002122 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2123 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00002124}
2125
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00002126static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2127 const AttributeList &Attr) {
Aaron Ballmanf3614532013-11-24 20:36:50 +00002128 if (!isa<ObjCInterfaceDecl>(D)) {
2129 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2130 << Attr.getName() << ExpectedObjectiveCInterface;
2131 return;
2132 }
Michael Han99315932013-01-24 16:46:58 +00002133 D->addAttr(::new (S.Context)
2134 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2135 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00002136}
2137
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002138static void handleObjCRootClassAttr(Sema &S, Decl *D,
2139 const AttributeList &Attr) {
2140 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballmanf90ccb02013-07-18 14:56:42 +00002141 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2142 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002143 return;
2144 }
2145
Michael Han99315932013-01-24 16:46:58 +00002146 D->addAttr(::new (S.Context)
2147 ObjCRootClassAttr(Attr.getRange(), S.Context,
2148 Attr.getAttributeSpellingListIndex()));
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002149}
2150
Ted Kremenek28eace62013-11-23 01:01:34 +00002151static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
2152 const AttributeList &Attr) {
2153 if (!isa<ObjCInterfaceDecl>(D)) {
2154 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2155 << Attr.getName() << ExpectedObjectiveCInterface;
2156 return;
2157 }
2158
Ted Kremenek7559b472013-11-23 22:29:11 +00002159 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek28eace62013-11-23 01:01:34 +00002160
2161 if (!Parm) {
2162 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 1;
2163 return;
2164 }
2165
2166 D->addAttr(::new (S.Context)
2167 ObjCSuppressProtocolAttr(Attr.getRange(), S.Context, Parm->Ident,
2168 Attr.getAttributeSpellingListIndex()));
2169}
2170
2171
Michael Han99315932013-01-24 16:46:58 +00002172static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2173 const AttributeList &Attr) {
Fariborz Jahanian7249e362012-01-03 22:52:32 +00002174 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballmanf3614532013-11-24 20:36:50 +00002175 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2176 << Attr.getName() << ExpectedObjectiveCInterface;
Fariborz Jahanian7249e362012-01-03 22:52:32 +00002177 return;
2178 }
2179
Michael Han99315932013-01-24 16:46:58 +00002180 D->addAttr(::new (S.Context)
2181 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2182 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00002183}
2184
Jordy Rose740b0c22012-05-08 03:27:22 +00002185static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2186 IdentifierInfo *Platform,
2187 VersionTuple Introduced,
2188 VersionTuple Deprecated,
2189 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002190 StringRef PlatformName
2191 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2192 if (PlatformName.empty())
2193 PlatformName = Platform->getName();
2194
2195 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2196 // of these steps are needed).
2197 if (!Introduced.empty() && !Deprecated.empty() &&
2198 !(Introduced <= Deprecated)) {
2199 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2200 << 1 << PlatformName << Deprecated.getAsString()
2201 << 0 << Introduced.getAsString();
2202 return true;
2203 }
2204
2205 if (!Introduced.empty() && !Obsoleted.empty() &&
2206 !(Introduced <= Obsoleted)) {
2207 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2208 << 2 << PlatformName << Obsoleted.getAsString()
2209 << 0 << Introduced.getAsString();
2210 return true;
2211 }
2212
2213 if (!Deprecated.empty() && !Obsoleted.empty() &&
2214 !(Deprecated <= Obsoleted)) {
2215 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2216 << 2 << PlatformName << Obsoleted.getAsString()
2217 << 1 << Deprecated.getAsString();
2218 return true;
2219 }
2220
2221 return false;
2222}
2223
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002224/// \brief Check whether the two versions match.
2225///
2226/// If either version tuple is empty, then they are assumed to match. If
2227/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2228static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2229 bool BeforeIsOkay) {
2230 if (X.empty() || Y.empty())
2231 return true;
2232
2233 if (X == Y)
2234 return true;
2235
2236 if (BeforeIsOkay && X < Y)
2237 return true;
2238
2239 return false;
2240}
2241
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002242AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002243 IdentifierInfo *Platform,
2244 VersionTuple Introduced,
2245 VersionTuple Deprecated,
2246 VersionTuple Obsoleted,
2247 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002248 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00002249 bool Override,
2250 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002251 VersionTuple MergedIntroduced = Introduced;
2252 VersionTuple MergedDeprecated = Deprecated;
2253 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002254 bool FoundAny = false;
2255
Rafael Espindolac67f2232012-05-10 02:50:16 +00002256 if (D->hasAttrs()) {
2257 AttrVec &Attrs = D->getAttrs();
2258 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2259 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2260 if (!OldAA) {
2261 ++i;
2262 continue;
2263 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002264
Rafael Espindolac67f2232012-05-10 02:50:16 +00002265 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2266 if (OldPlatform != Platform) {
2267 ++i;
2268 continue;
2269 }
2270
2271 FoundAny = true;
2272 VersionTuple OldIntroduced = OldAA->getIntroduced();
2273 VersionTuple OldDeprecated = OldAA->getDeprecated();
2274 VersionTuple OldObsoleted = OldAA->getObsoleted();
2275 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002276
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002277 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2278 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2279 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2280 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00002281 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002282 if (Override) {
2283 int Which = -1;
2284 VersionTuple FirstVersion;
2285 VersionTuple SecondVersion;
2286 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2287 Which = 0;
2288 FirstVersion = OldIntroduced;
2289 SecondVersion = Introduced;
2290 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2291 Which = 1;
2292 FirstVersion = Deprecated;
2293 SecondVersion = OldDeprecated;
2294 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2295 Which = 2;
2296 FirstVersion = Obsoleted;
2297 SecondVersion = OldObsoleted;
2298 }
2299
2300 if (Which == -1) {
2301 Diag(OldAA->getLocation(),
2302 diag::warn_mismatched_availability_override_unavail)
2303 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2304 } else {
2305 Diag(OldAA->getLocation(),
2306 diag::warn_mismatched_availability_override)
2307 << Which
2308 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2309 << FirstVersion.getAsString() << SecondVersion.getAsString();
2310 }
2311 Diag(Range.getBegin(), diag::note_overridden_method);
2312 } else {
2313 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2314 Diag(Range.getBegin(), diag::note_previous_attribute);
2315 }
2316
Rafael Espindolac67f2232012-05-10 02:50:16 +00002317 Attrs.erase(Attrs.begin() + i);
2318 --e;
2319 continue;
2320 }
2321
2322 VersionTuple MergedIntroduced2 = MergedIntroduced;
2323 VersionTuple MergedDeprecated2 = MergedDeprecated;
2324 VersionTuple MergedObsoleted2 = MergedObsoleted;
2325
2326 if (MergedIntroduced2.empty())
2327 MergedIntroduced2 = OldIntroduced;
2328 if (MergedDeprecated2.empty())
2329 MergedDeprecated2 = OldDeprecated;
2330 if (MergedObsoleted2.empty())
2331 MergedObsoleted2 = OldObsoleted;
2332
2333 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2334 MergedIntroduced2, MergedDeprecated2,
2335 MergedObsoleted2)) {
2336 Attrs.erase(Attrs.begin() + i);
2337 --e;
2338 continue;
2339 }
2340
2341 MergedIntroduced = MergedIntroduced2;
2342 MergedDeprecated = MergedDeprecated2;
2343 MergedObsoleted = MergedObsoleted2;
2344 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002345 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002346 }
2347
2348 if (FoundAny &&
2349 MergedIntroduced == Introduced &&
2350 MergedDeprecated == Deprecated &&
2351 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002352 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002353
Ted Kremenekb5445722013-04-06 00:34:27 +00002354 // Only create a new attribute if !Override, but we want to do
2355 // the checking.
Rafael Espindolac67f2232012-05-10 02:50:16 +00002356 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekb5445722013-04-06 00:34:27 +00002357 MergedDeprecated, MergedObsoleted) &&
2358 !Override) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002359 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2360 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002361 Obsoleted, IsUnavailable, Message,
2362 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002363 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002364 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002365}
2366
Chandler Carruthedc2c642011-07-02 00:01:44 +00002367static void handleAvailabilityAttr(Sema &S, Decl *D,
2368 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002369 if (!checkAttributeNumArgs(S, Attr, 1))
2370 return;
2371 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han99315932013-01-24 16:46:58 +00002372 unsigned Index = Attr.getAttributeSpellingListIndex();
2373
Aaron Ballman00e99962013-08-31 01:11:41 +00002374 IdentifierInfo *II = Platform->Ident;
2375 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2376 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2377 << Platform->Ident;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002378
Rafael Espindolac231fab2013-01-08 21:30:32 +00002379 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2380 if (!ND) {
2381 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2382 return;
2383 }
2384
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002385 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2386 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2387 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002388 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002389 StringRef Str;
Benjamin Kramera9dfa922013-09-13 17:31:48 +00002390 if (const StringLiteral *SE =
2391 dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr()))
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002392 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002393
Aaron Ballman00e99962013-08-31 01:11:41 +00002394 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002395 Introduced.Version,
2396 Deprecated.Version,
2397 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002398 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00002399 /*Override=*/false,
2400 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002401 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002402 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002403}
2404
John McCalld041a9b2013-02-20 01:54:26 +00002405template <class T>
2406static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2407 typename T::VisibilityType value,
2408 unsigned attrSpellingListIndex) {
2409 T *existingAttr = D->getAttr<T>();
2410 if (existingAttr) {
2411 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2412 if (existingValue == value)
2413 return NULL;
2414 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2415 S.Diag(range.getBegin(), diag::note_previous_attribute);
2416 D->dropAttr<T>();
2417 }
2418 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2419}
2420
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002421VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002422 VisibilityAttr::VisibilityType Vis,
2423 unsigned AttrSpellingListIndex) {
John McCalld041a9b2013-02-20 01:54:26 +00002424 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2425 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002426}
2427
John McCalld041a9b2013-02-20 01:54:26 +00002428TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2429 TypeVisibilityAttr::VisibilityType Vis,
2430 unsigned AttrSpellingListIndex) {
2431 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2432 AttrSpellingListIndex);
2433}
2434
2435static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2436 bool isTypeVisibility) {
2437 // Visibility attributes don't mean anything on a typedef.
2438 if (isa<TypedefNameDecl>(D)) {
2439 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2440 << Attr.getName();
2441 return;
2442 }
2443
2444 // 'type_visibility' can only go on a type or namespace.
2445 if (isTypeVisibility &&
2446 !(isa<TagDecl>(D) ||
2447 isa<ObjCInterfaceDecl>(D) ||
2448 isa<NamespaceDecl>(D))) {
2449 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2450 << Attr.getName() << ExpectedTypeOrNamespace;
2451 return;
2452 }
2453
Benjamin Kramer70370212013-09-09 15:08:57 +00002454 // Check that the argument is a string literal.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002455 StringRef TypeStr;
2456 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002457 if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002458 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002459
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002460 VisibilityAttr::VisibilityType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002461 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002462 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
Aaron Ballman682ee422013-09-11 19:47:58 +00002463 << Attr.getName() << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002464 return;
2465 }
Aaron Ballman682ee422013-09-11 19:47:58 +00002466
2467 // Complain about attempts to use protected visibility on targets
2468 // (like Darwin) that don't support it.
2469 if (type == VisibilityAttr::Protected &&
2470 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2471 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2472 type = VisibilityAttr::Default;
2473 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002474
Michael Han99315932013-01-24 16:46:58 +00002475 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld041a9b2013-02-20 01:54:26 +00002476 clang::Attr *newAttr;
2477 if (isTypeVisibility) {
2478 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2479 (TypeVisibilityAttr::VisibilityType) type,
2480 Index);
2481 } else {
2482 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2483 }
2484 if (newAttr)
2485 D->addAttr(newAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002486}
2487
Chandler Carruthedc2c642011-07-02 00:01:44 +00002488static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2489 const AttributeList &Attr) {
John McCall86bc21f2011-03-02 11:33:24 +00002490 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2491 if (!method) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002492 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002493 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00002494 return;
2495 }
2496
Aaron Ballman00e99962013-08-31 01:11:41 +00002497 if (!Attr.isArgIdent(0)) {
2498 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2499 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCall86bc21f2011-03-02 11:33:24 +00002500 return;
2501 }
Aaron Ballman00e99962013-08-31 01:11:41 +00002502
Aaron Ballman682ee422013-09-11 19:47:58 +00002503 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2504 ObjCMethodFamilyAttr::FamilyKind F;
2505 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2506 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2507 << IL->Ident;
John McCall86bc21f2011-03-02 11:33:24 +00002508 return;
2509 }
2510
Aaron Ballman682ee422013-09-11 19:47:58 +00002511 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCall31168b02011-06-15 23:02:42 +00002512 !method->getResultType()->isObjCObjectPointerType()) {
2513 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2514 << method->getResultType();
2515 // Ignore the attribute.
2516 return;
2517 }
2518
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002519 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballman682ee422013-09-11 19:47:58 +00002520 S.Context, F));
John McCall86bc21f2011-03-02 11:33:24 +00002521}
2522
Chandler Carruthedc2c642011-07-02 00:01:44 +00002523static void handleObjCExceptionAttr(Sema &S, Decl *D,
2524 const AttributeList &Attr) {
Chris Lattner677a3582009-02-14 08:09:34 +00002525 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2526 if (OCI == 0) {
Aaron Ballmanf90ccb02013-07-18 14:56:42 +00002527 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2528 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner677a3582009-02-14 08:09:34 +00002529 return;
2530 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002531
Michael Han99315932013-01-24 16:46:58 +00002532 D->addAttr(::new (S.Context)
2533 ObjCExceptionAttr(Attr.getRange(), S.Context,
2534 Attr.getAttributeSpellingListIndex()));
Chris Lattner677a3582009-02-14 08:09:34 +00002535}
2536
Chandler Carruthedc2c642011-07-02 00:01:44 +00002537static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smithdda56e42011-04-15 14:24:37 +00002538 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002539 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002540 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002541 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2542 return;
2543 }
2544 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002545 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2546 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002547 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002548 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2549 return;
2550 }
2551 }
2552 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002553 // It is okay to include this attribute on properties, e.g.:
2554 //
2555 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2556 //
2557 // In this case it follows tradition and suppresses an error in the above
2558 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002559 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002560 }
Michael Han99315932013-01-24 16:46:58 +00002561 D->addAttr(::new (S.Context)
2562 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2563 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002564}
2565
Aaron Ballmanb0dc0742013-11-26 16:14:15 +00002566static void handleOverloadableAttr(Sema &S, Decl *D,
2567 const AttributeList &Attr) {
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002568 if (!isa<FunctionDecl>(D)) {
Aaron Ballmanb0dc0742013-11-26 16:14:15 +00002569 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2570 << Attr.getName() << ExpectedFunction;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002571 return;
2572 }
2573
Michael Han99315932013-01-24 16:46:58 +00002574 D->addAttr(::new (S.Context)
2575 OverloadableAttr(Attr.getRange(), S.Context,
2576 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002577}
2578
Chandler Carruthedc2c642011-07-02 00:01:44 +00002579static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002580 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002581 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00002582 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff3405a732008-09-18 16:44:58 +00002583 return;
2584 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002585
Aaron Ballman00e99962013-08-31 01:11:41 +00002586 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002587 BlocksAttr::BlockType type;
Aaron Ballman682ee422013-09-11 19:47:58 +00002588 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2589 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2590 << Attr.getName() << II;
Steve Naroff3405a732008-09-18 16:44:58 +00002591 return;
2592 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002593
Michael Han99315932013-01-24 16:46:58 +00002594 D->addAttr(::new (S.Context)
2595 BlocksAttr(Attr.getRange(), S.Context, type,
2596 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002597}
2598
Chandler Carruthedc2c642011-07-02 00:01:44 +00002599static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002600 // check the attribute arguments.
2601 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002602 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002603 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002604 }
2605
Aaron Ballman18a78382013-11-21 00:28:23 +00002606 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
Anders Carlssonc181b012008-10-05 18:05:59 +00002607 if (Attr.getNumArgs() > 0) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002608 Expr *E = Attr.getArgAsExpr(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002609 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002610 if (E->isTypeDependent() || E->isValueDependent() ||
2611 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002612 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002613 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002614 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002615 return;
2616 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002617
John McCallb46f2872011-09-09 07:56:05 +00002618 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002619 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2620 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002621 return;
2622 }
John McCallb46f2872011-09-09 07:56:05 +00002623
2624 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002625 }
2626
Aaron Ballman18a78382013-11-21 00:28:23 +00002627 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
Anders Carlssonc181b012008-10-05 18:05:59 +00002628 if (Attr.getNumArgs() > 1) {
Aaron Ballman00e99962013-08-31 01:11:41 +00002629 Expr *E = Attr.getArgAsExpr(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002630 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002631 if (E->isTypeDependent() || E->isValueDependent() ||
2632 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman29982272013-07-23 14:03:57 +00002633 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00002634 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman29982272013-07-23 14:03:57 +00002635 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002636 return;
2637 }
2638 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002639
John McCallb46f2872011-09-09 07:56:05 +00002640 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002641 // FIXME: This error message could be improved, it would be nice
2642 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002643 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2644 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002645 return;
2646 }
2647 }
2648
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002649 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002650 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002651 if (isa<FunctionNoProtoType>(FT)) {
2652 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2653 return;
2654 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002655
Chris Lattner9363e312009-03-17 23:03:47 +00002656 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002657 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002658 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002659 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002660 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002661 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002662 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002663 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002664 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002665 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2666 if (!BD->isVariadic()) {
2667 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2668 return;
2669 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002670 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002671 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002672 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002673 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002674 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002675 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002676 int m = Ty->isFunctionPointerType() ? 0 : 1;
2677 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002678 return;
2679 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002680 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002681 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002682 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002683 return;
2684 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002685 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002686 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002687 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002688 return;
2689 }
Michael Han99315932013-01-24 16:46:58 +00002690 D->addAttr(::new (S.Context)
2691 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2692 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002693}
2694
Lubos Lunakedc13882013-07-20 15:05:36 +00002695static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Lubos Lunakedc13882013-07-20 15:05:36 +00002696 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2697 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2698 else
2699 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2700}
2701
Chandler Carruthedc2c642011-07-02 00:01:44 +00002702static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002703 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002704 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002705 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002706 return;
2707 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002708
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002709 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2710 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2711 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002712 return;
2713 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002714 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2715 if (MD->getResultType()->isVoidType()) {
2716 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2717 << Attr.getName() << 1;
2718 return;
2719 }
2720
Michael Han99315932013-01-24 16:46:58 +00002721 D->addAttr(::new (S.Context)
2722 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2723 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002724}
2725
Chandler Carruthedc2c642011-07-02 00:01:44 +00002726static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002727 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002728 if (isa<CXXRecordDecl>(D)) {
2729 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2730 return;
2731 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002732 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2733 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002734 return;
2735 }
2736
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002737 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002738
Michael Han99315932013-01-24 16:46:58 +00002739 nd->addAttr(::new (S.Context)
2740 WeakAttr(Attr.getRange(), S.Context,
2741 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002742}
2743
Chandler Carruthedc2c642011-07-02 00:01:44 +00002744static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002745 // weak_import only applies to variable & function declarations.
2746 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002747 if (!D->canBeWeakImported(isDef)) {
2748 if (isDef)
Reid Kleckner52d598e2013-05-20 21:53:29 +00002749 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2750 << "weak_import";
Douglas Gregord71149a2011-03-23 13:27:51 +00002751 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002752 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002753 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002754 // Nothing to warn about here.
2755 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002756 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002757 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002758
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002759 return;
2760 }
2761
Michael Han99315932013-01-24 16:46:58 +00002762 D->addAttr(::new (S.Context)
2763 WeakImportAttr(Attr.getRange(), S.Context,
2764 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002765}
2766
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002767// Handles reqd_work_group_size and work_group_size_hint.
2768static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002769 const AttributeList &Attr) {
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00002770 uint32_t WGSize[3];
2771 for (unsigned i = 0; i < 3; ++i)
2772 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i))
Nate Begemanf2758702009-06-26 06:32:41 +00002773 return;
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002774
2775 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2776 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2777 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2778 if (!(A->getXDim() == WGSize[0] &&
2779 A->getYDim() == WGSize[1] &&
2780 A->getZDim() == WGSize[2])) {
2781 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2782 Attr.getName();
2783 }
2784 }
2785
2786 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2787 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2788 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2789 if (!(A->getXDim() == WGSize[0] &&
2790 A->getYDim() == WGSize[1] &&
2791 A->getZDim() == WGSize[2])) {
2792 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2793 Attr.getName();
2794 }
2795 }
2796
2797 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2798 D->addAttr(::new (S.Context)
2799 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002800 WGSize[0], WGSize[1], WGSize[2],
2801 Attr.getAttributeSpellingListIndex()));
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002802 else
2803 D->addAttr(::new (S.Context)
2804 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002805 WGSize[0], WGSize[1], WGSize[2],
2806 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002807}
2808
Joey Goulyaba589c2013-03-08 09:42:32 +00002809static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2810 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2811
Aaron Ballman00e99962013-08-31 01:11:41 +00002812 if (!Attr.hasParsedType()) {
2813 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2814 << Attr.getName() << 1;
2815 return;
2816 }
2817
Richard Smithb87c4652013-10-31 21:23:20 +00002818 TypeSourceInfo *ParmTSI = 0;
2819 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI);
2820 assert(ParmTSI && "no type source info for attribute argument");
Joey Goulyaba589c2013-03-08 09:42:32 +00002821
2822 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2823 (ParmType->isBooleanType() ||
2824 !ParmType->isIntegralType(S.getASTContext()))) {
2825 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2826 << ParmType;
2827 return;
2828 }
2829
2830 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2831 D->hasAttr<VecTypeHintAttr>()) {
2832 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
Richard Smithb87c4652013-10-31 21:23:20 +00002833 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
Joey Goulyaba589c2013-03-08 09:42:32 +00002834 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2835 return;
2836 }
2837 }
2838
2839 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
Richard Smithb87c4652013-10-31 21:23:20 +00002840 ParmTSI));
Joey Goulyaba589c2013-03-08 09:42:32 +00002841}
2842
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002843SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002844 StringRef Name,
2845 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002846 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2847 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002848 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002849 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2850 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002851 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002852 }
Michael Han99315932013-01-24 16:46:58 +00002853 return ::new (Context) SectionAttr(Range, Context, Name,
2854 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002855}
2856
Chandler Carruthedc2c642011-07-02 00:01:44 +00002857static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002858 // Make sure that there is a string literal as the sections's single
2859 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002860 StringRef Str;
2861 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00002862 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002863 return;
Mike Stump11289f42009-09-09 15:08:12 +00002864
Chris Lattner30ba6742009-08-10 19:03:04 +00002865 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002866 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002867 if (!Error.empty()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002868 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
Chris Lattner20aee9b2010-01-12 20:58:53 +00002869 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002870 return;
2871 }
Mike Stump11289f42009-09-09 15:08:12 +00002872
Chris Lattner20aee9b2010-01-12 20:58:53 +00002873 // This attribute cannot be applied to local variables.
2874 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002875 S.Diag(LiteralLoc, diag::err_attribute_section_local_variable);
Chris Lattner20aee9b2010-01-12 20:58:53 +00002876 return;
2877 }
Michael Han99315932013-01-24 16:46:58 +00002878
2879 unsigned Index = Attr.getAttributeSpellingListIndex();
Benjamin Kramer6ee15622013-09-13 15:35:43 +00002880 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002881 if (NewAttr)
2882 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002883}
2884
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002885
Chandler Carruthedc2c642011-07-02 00:01:44 +00002886static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002887 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002888 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002889 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002890 } else {
Michael Han99315932013-01-24 16:46:58 +00002891 D->addAttr(::new (S.Context)
2892 NoThrowAttr(Attr.getRange(), S.Context,
2893 Attr.getAttributeSpellingListIndex()));
Douglas Gregor88336832011-06-15 05:45:11 +00002894 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002895}
2896
Chandler Carruthedc2c642011-07-02 00:01:44 +00002897static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002898 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002899 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002900 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002901 } else {
Michael Han99315932013-01-24 16:46:58 +00002902 D->addAttr(::new (S.Context)
2903 ConstAttr(Attr.getRange(), S.Context,
2904 Attr.getAttributeSpellingListIndex() ));
Douglas Gregor88336832011-06-15 05:45:11 +00002905 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002906}
2907
Chandler Carruthedc2c642011-07-02 00:01:44 +00002908static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han99315932013-01-24 16:46:58 +00002909 D->addAttr(::new (S.Context)
2910 PureAttr(Attr.getRange(), S.Context,
2911 Attr.getAttributeSpellingListIndex()));
Anders Carlssonb8316282008-10-05 23:32:53 +00002912}
2913
Chandler Carruthedc2c642011-07-02 00:01:44 +00002914static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002915 VarDecl *VD = dyn_cast<VarDecl>(D);
Anders Carlssond277d792009-01-31 01:16:18 +00002916 if (!VD || !VD->hasLocalStorage()) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002917 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002918 return;
2919 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002920
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002921 Expr *E = Attr.getArgAsExpr(0);
2922 SourceLocation Loc = E->getExprLoc();
2923 FunctionDecl *FD = 0;
2924 DeclarationNameInfo NI;
Aaron Ballman00e99962013-08-31 01:11:41 +00002925
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002926 // gcc only allows for simple identifiers. Since we support more than gcc, we
2927 // will warn the user.
2928 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2929 if (DRE->hasQualifier())
2930 S.Diag(Loc, diag::warn_cleanup_ext);
2931 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2932 NI = DRE->getNameInfo();
2933 if (!FD) {
2934 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2935 << NI.getName();
2936 return;
2937 }
2938 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2939 if (ULE->hasExplicitTemplateArgs())
2940 S.Diag(Loc, diag::warn_cleanup_ext);
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002941 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2942 NI = ULE->getNameInfo();
Alp Toker67b47ac2013-10-20 18:48:56 +00002943 if (!FD) {
2944 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
2945 << NI.getName();
2946 if (ULE->getType() == S.Context.OverloadTy)
2947 S.NoteAllOverloadCandidates(ULE);
2948 return;
2949 }
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002950 } else {
2951 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssond277d792009-01-31 01:16:18 +00002952 return;
2953 }
2954
Anders Carlssond277d792009-01-31 01:16:18 +00002955 if (FD->getNumParams() != 1) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002956 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2957 << NI.getName();
Anders Carlssond277d792009-01-31 01:16:18 +00002958 return;
2959 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002960
Anders Carlsson723f55d2009-02-07 23:16:50 +00002961 // We're currently more strict than GCC about what function types we accept.
2962 // If this ever proves to be a problem it should be easy to fix.
2963 QualType Ty = S.Context.getPointerType(VD->getType());
2964 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002965 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2966 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanc12aaff2013-09-11 01:37:41 +00002967 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2968 << NI.getName() << ParamTy << Ty;
Anders Carlsson723f55d2009-02-07 23:16:50 +00002969 return;
2970 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002971
Michael Han99315932013-01-24 16:46:58 +00002972 D->addAttr(::new (S.Context)
2973 CleanupAttr(Attr.getRange(), S.Context, FD,
2974 Attr.getAttributeSpellingListIndex()));
Anders Carlssond277d792009-01-31 01:16:18 +00002975}
2976
Mike Stumpd3bb5572009-07-24 19:02:52 +00002977/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002978/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002979static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002980 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002981 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002982 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002983 return;
2984 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002985
Aaron Ballman00e99962013-08-31 01:11:41 +00002986 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00002987 uint64_t ArgIdx;
2988 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2989 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002990 return;
Chandler Carruth743682b2010-11-16 08:35:43 +00002991
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002992 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002993 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002994
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002995 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2996 if (not_nsstring_type &&
2997 !isCFStringType(Ty, S.Context) &&
2998 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002999 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003000 // FIXME: Should highlight the actual expression that has the wrong type.
3001 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00003002 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003003 << IdxExpr->getSourceRange();
3004 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003005 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003006 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003007 if (!isNSStringType(Ty, S.Context) &&
3008 !isCFStringType(Ty, S.Context) &&
3009 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003010 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003011 // FIXME: Should highlight the actual expression that has the wrong type.
3012 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00003013 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003014 << IdxExpr->getSourceRange();
3015 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003016 }
3017
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00003018 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
3019 // because that has corrected for the implicit this parameter, and is zero-
3020 // based. The attribute expects what the user wrote explicitly.
3021 llvm::APSInt Val;
3022 IdxExpr->EvaluateAsInt(Val, S.Context);
3023
Michael Han99315932013-01-24 16:46:58 +00003024 D->addAttr(::new (S.Context)
Aaron Ballmanbe50eb82013-07-30 00:48:57 +00003025 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00003026 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00003027}
3028
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003029enum FormatAttrKind {
3030 CFStringFormat,
3031 NSStringFormat,
3032 StrftimeFormat,
3033 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00003034 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003035 InvalidFormat
3036};
3037
3038/// getFormatAttrKind - Map from format attribute names to supported format
3039/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003040static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003041 return llvm::StringSwitch<FormatAttrKind>(Format)
3042 // Check for formats that get handled specially.
3043 .Case("NSString", NSStringFormat)
3044 .Case("CFString", CFStringFormat)
3045 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003046
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003047 // Otherwise, check for supported formats.
3048 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3049 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3050 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003051
Benjamin Kramer96a44b62012-05-16 12:44:25 +00003052 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3053 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003054}
3055
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003056/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003057/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003058static void handleInitPriorityAttr(Sema &S, Decl *D,
3059 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003060 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003061 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3062 return;
3063 }
3064
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003065 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003066 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3067 Attr.setInvalid();
3068 return;
3069 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003070 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00003071 if (S.Context.getAsArrayType(T))
3072 T = S.Context.getBaseElementType(T);
3073 if (!T->getAs<RecordType>()) {
3074 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3075 Attr.setInvalid();
3076 return;
3077 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003078
3079 Expr *E = Attr.getArgAsExpr(0);
3080 uint32_t prioritynum;
3081 if (!checkUInt32Argument(S, Attr, E, prioritynum)) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003082 Attr.setInvalid();
3083 return;
3084 }
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003085
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003086 if (prioritynum < 101 || prioritynum > 65535) {
3087 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003088 << E->getSourceRange();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003089 Attr.setInvalid();
3090 return;
3091 }
Michael Han99315932013-01-24 16:46:58 +00003092 D->addAttr(::new (S.Context)
3093 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3094 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003095}
3096
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003097FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3098 IdentifierInfo *Format, int FormatIdx,
3099 int FirstArg,
Michael Han99315932013-01-24 16:46:58 +00003100 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003101 // Check whether we already have an equivalent format attribute.
3102 for (specific_attr_iterator<FormatAttr>
3103 i = D->specific_attr_begin<FormatAttr>(),
3104 e = D->specific_attr_end<FormatAttr>();
3105 i != e ; ++i) {
3106 FormatAttr *f = *i;
3107 if (f->getType() == Format &&
3108 f->getFormatIdx() == FormatIdx &&
3109 f->getFirstArg() == FirstArg) {
3110 // If we don't have a valid location for this attribute, adopt the
3111 // location.
3112 if (f->getLocation().isInvalid())
3113 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003114 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00003115 }
3116 }
3117
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003118 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3119 FirstArg, AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003120}
3121
Mike Stumpd3bb5572009-07-24 19:02:52 +00003122/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003123/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003124static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00003125 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00003126 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman00e99962013-08-31 01:11:41 +00003127 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003128 return;
3129 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003130
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003131 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003132 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003133 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003134 return;
3135 }
3136
Chandler Carruth743682b2010-11-16 08:35:43 +00003137 // In C++ the implicit 'this' function parameter also counts, and they are
3138 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003139 bool HasImplicitThisParam = isInstanceMethod(D);
3140 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003141
Aaron Ballman00e99962013-08-31 01:11:41 +00003142 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
3143 StringRef Format = II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003144
3145 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003146 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003147 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003148 // If we've modified the string name, we need a new identifier for it.
3149 II = &S.Context.Idents.get(Format);
3150 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003151
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003152 // Check for supported formats.
3153 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00003154
3155 if (Kind == IgnoredFormat)
3156 return;
3157
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003158 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00003159 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman00e99962013-08-31 01:11:41 +00003160 << "format" << II->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003161 return;
3162 }
3163
3164 // checks for the 2nd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00003165 Expr *IdxExpr = Attr.getArgAsExpr(1);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003166 uint32_t Idx;
3167 if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003168 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003169
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003170 if (Idx < 1 || Idx > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003171 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003172 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003173 return;
3174 }
3175
3176 // FIXME: Do we need to bounds check?
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003177 unsigned ArgIdx = Idx - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003178
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003179 if (HasImplicitThisParam) {
3180 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00003181 S.Diag(Attr.getLoc(),
3182 diag::err_format_attribute_implicit_this_format_string)
3183 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003184 return;
3185 }
3186 ArgIdx--;
3187 }
Mike Stump11289f42009-09-09 15:08:12 +00003188
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003189 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003190 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003191
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003192 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003193 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003194 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3195 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00003196 return;
3197 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003198 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003199 // FIXME: do we need to check if the type is NSString*? What are the
3200 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003201 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003202 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003203 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3204 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003205 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003206 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003207 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003208 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003209 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003210 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3211 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003212 return;
3213 }
3214
3215 // check the 3rd argument
Aaron Ballman00e99962013-08-31 01:11:41 +00003216 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003217 uint32_t FirstArg;
3218 if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003219 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003220
3221 // check if the function is variadic if the 3rd argument non-zero
3222 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003223 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003224 ++NumArgs; // +1 for ...
3225 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003226 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003227 return;
3228 }
3229 }
3230
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003231 // strftime requires FirstArg to be 0 because it doesn't read from any
3232 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003233 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003234 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00003235 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3236 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003237 return;
3238 }
3239 // if 0 it disables parameter checking (to use with e.g. va_list)
3240 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003241 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003242 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003243 return;
3244 }
3245
Aaron Ballmanf58070b2013-09-03 21:02:22 +00003246 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003247 Idx, FirstArg,
Michael Han99315932013-01-24 16:46:58 +00003248 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003249 if (NewAttr)
3250 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003251}
3252
Chandler Carruthedc2c642011-07-02 00:01:44 +00003253static void handleTransparentUnionAttr(Sema &S, Decl *D,
3254 const AttributeList &Attr) {
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003255 // Try to find the underlying union declaration.
3256 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003257 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003258 if (TD && TD->getUnderlyingType()->isUnionType())
3259 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3260 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003261 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003262
3263 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00003264 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003265 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003266 return;
3267 }
3268
John McCallf937c022011-10-07 06:10:15 +00003269 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003270 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003271 diag::warn_transparent_union_attribute_not_definition);
3272 return;
3273 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003274
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003275 RecordDecl::field_iterator Field = RD->field_begin(),
3276 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003277 if (Field == FieldEnd) {
3278 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3279 return;
3280 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003281
David Blaikie40ed2972012-06-06 20:45:41 +00003282 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003283 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003284 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003285 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003286 diag::warn_transparent_union_attribute_floating)
3287 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003288 return;
3289 }
3290
3291 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3292 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3293 for (; Field != FieldEnd; ++Field) {
3294 QualType FieldType = Field->getType();
3295 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3296 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3297 // Warn if we drop the attribute.
3298 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003299 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003300 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003301 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003302 diag::warn_transparent_union_attribute_field_size_align)
3303 << isSize << Field->getDeclName() << FieldBits;
3304 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003305 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003306 diag::note_transparent_union_first_field_size_align)
3307 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003308 return;
3309 }
3310 }
3311
Michael Han99315932013-01-24 16:46:58 +00003312 RD->addAttr(::new (S.Context)
3313 TransparentUnionAttr(Attr.getRange(), S.Context,
3314 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003315}
3316
Chandler Carruthedc2c642011-07-02 00:01:44 +00003317static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003318 // Make sure that there is a string literal as the annotation's single
3319 // argument.
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003320 StringRef Str;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003321 if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003322 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003323
3324 // Don't duplicate annotations that are already set.
3325 for (specific_attr_iterator<AnnotateAttr>
3326 i = D->specific_attr_begin<AnnotateAttr>(),
3327 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003328 if ((*i)->getAnnotation() == Str)
3329 return;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003330 }
Michael Han99315932013-01-24 16:46:58 +00003331
3332 D->addAttr(::new (S.Context)
Benjamin Kramer6ee15622013-09-13 15:35:43 +00003333 AnnotateAttr(Attr.getRange(), S.Context, Str,
Michael Han99315932013-01-24 16:46:58 +00003334 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003335}
3336
Chandler Carruthedc2c642011-07-02 00:01:44 +00003337static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003338 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003339 if (Attr.getNumArgs() > 1) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003340 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3341 << Attr.getName() << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003342 return;
3343 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003344
Richard Smith848e1f12013-02-01 08:12:08 +00003345 if (Attr.getNumArgs() == 0) {
3346 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3347 true, 0, Attr.getAttributeSpellingListIndex()));
3348 return;
3349 }
3350
Aaron Ballman00e99962013-08-31 01:11:41 +00003351 Expr *E = Attr.getArgAsExpr(0);
Richard Smith44c247f2013-02-22 08:32:16 +00003352 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3353 S.Diag(Attr.getEllipsisLoc(),
3354 diag::err_pack_expansion_without_parameter_packs);
3355 return;
3356 }
3357
3358 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3359 return;
3360
3361 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3362 Attr.isPackExpansion());
Richard Smith848e1f12013-02-01 08:12:08 +00003363}
3364
3365void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smith44c247f2013-02-22 08:32:16 +00003366 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smith848e1f12013-02-01 08:12:08 +00003367 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3368 SourceLocation AttrLoc = AttrRange.getBegin();
3369
Richard Smith1dba27c2013-01-29 09:02:09 +00003370 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003371 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003372 // C++11 [dcl.align]p1:
3373 // An alignment-specifier may be applied to a variable or to a class
3374 // data member, but it shall not be applied to a bit-field, a function
3375 // parameter, the formal parameter of a catch clause, or a variable
3376 // declared with the register storage class specifier. An
3377 // alignment-specifier may also be applied to the declaration of a class
3378 // or enumeration type.
3379 // C11 6.7.5/2:
3380 // An alignment attribute shall not be specified in a declaration of
3381 // a typedef, or a bit-field, or a function, or a parameter, or an
3382 // object declared with the register storage-class specifier.
3383 int DiagKind = -1;
3384 if (isa<ParmVarDecl>(D)) {
3385 DiagKind = 0;
3386 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3387 if (VD->getStorageClass() == SC_Register)
3388 DiagKind = 1;
3389 if (VD->isExceptionVariable())
3390 DiagKind = 2;
3391 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3392 if (FD->isBitField())
3393 DiagKind = 3;
3394 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00003395 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3396 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00003397 << (TmpAttr.isC11() ? ExpectedVariableOrField
3398 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003399 return;
3400 }
3401 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003402 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smithbc8caaf2013-02-22 04:55:39 +00003403 << TmpAttr.isC11() << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003404 return;
3405 }
3406 }
3407
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003408 if (E->isTypeDependent() || E->isValueDependent()) {
3409 // Save dependent expressions in the AST to be instantiated.
Richard Smith44c247f2013-02-22 08:32:16 +00003410 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3411 AA->setPackExpansion(IsPackExpansion);
3412 D->addAttr(AA);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003413 return;
3414 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003415
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003416 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00003417 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00003418 ExprResult ICE
3419 = VerifyIntegerConstantExpression(E, &Alignment,
3420 diag::err_aligned_attribute_argument_not_int,
3421 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003422 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003423 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003424
3425 // C++11 [dcl.align]p2:
3426 // -- if the constant expression evaluates to zero, the alignment
3427 // specifier shall have no effect
3428 // C11 6.7.5p6:
3429 // An alignment specification of zero has no effect.
3430 if (!(TmpAttr.isAlignas() && !Alignment) &&
3431 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003432 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3433 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003434 return;
3435 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003436
Richard Smith848e1f12013-02-01 08:12:08 +00003437 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00003438 // We've already verified it's a power of 2, now let's make sure it's
3439 // 8192 or less.
3440 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00003441 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00003442 << E->getSourceRange();
3443 return;
3444 }
3445 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003446
Richard Smith44c247f2013-02-22 08:32:16 +00003447 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3448 ICE.take(), SpellingListIndex);
3449 AA->setPackExpansion(IsPackExpansion);
3450 D->addAttr(AA);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003451}
3452
Michael Hanaf02bbe2013-02-01 01:19:17 +00003453void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smith44c247f2013-02-22 08:32:16 +00003454 unsigned SpellingListIndex, bool IsPackExpansion) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003455 // FIXME: Cache the number on the Attr object if non-dependent?
3456 // FIXME: Perform checking of type validity
Richard Smith44c247f2013-02-22 08:32:16 +00003457 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3458 SpellingListIndex);
3459 AA->setPackExpansion(IsPackExpansion);
3460 D->addAttr(AA);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003461}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003462
Richard Smith848e1f12013-02-01 08:12:08 +00003463void Sema::CheckAlignasUnderalignment(Decl *D) {
3464 assert(D->hasAttrs() && "no attributes on decl");
3465
3466 QualType Ty;
3467 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3468 Ty = VD->getType();
3469 else
3470 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith3653b7e2013-02-22 09:21:42 +00003471 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smith848e1f12013-02-01 08:12:08 +00003472 return;
3473
3474 // C++11 [dcl.align]p5, C11 6.7.5/4:
3475 // The combined effect of all alignment attributes in a declaration shall
3476 // not specify an alignment that is less strict than the alignment that
3477 // would otherwise be required for the entity being declared.
3478 AlignedAttr *AlignasAttr = 0;
3479 unsigned Align = 0;
3480 for (specific_attr_iterator<AlignedAttr>
3481 I = D->specific_attr_begin<AlignedAttr>(),
3482 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3483 if (I->isAlignmentDependent())
3484 return;
3485 if (I->isAlignas())
3486 AlignasAttr = *I;
3487 Align = std::max(Align, I->getAlignment(Context));
3488 }
3489
3490 if (AlignasAttr && Align) {
3491 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3492 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3493 if (NaturalAlign > RequestedAlign)
3494 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3495 << Ty << (unsigned)NaturalAlign.getQuantity();
3496 }
3497}
3498
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003499/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003500/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003501///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003502/// Despite what would be logical, the mode attribute is a decl attribute, not a
3503/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3504/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003505static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003506 // This attribute isn't documented, but glibc uses it. It changes
3507 // the width of an int or unsigned int to the specified size.
Aaron Ballman00e99962013-08-31 01:11:41 +00003508 if (!Attr.isArgIdent(0)) {
Aaron Ballman9744ffd2013-07-30 14:29:12 +00003509 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3510 << AANT_ArgumentIdentifier;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003511 return;
3512 }
Aaron Ballman00e99962013-08-31 01:11:41 +00003513
Aaron Ballman00e99962013-08-31 01:11:41 +00003514 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3515 StringRef Str = Name->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003516
3517 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003518 if (Str.startswith("__") && Str.endswith("__"))
3519 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003520
3521 unsigned DestWidth = 0;
3522 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003523 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003524 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003525 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003526 switch (Str[0]) {
3527 case 'Q': DestWidth = 8; break;
3528 case 'H': DestWidth = 16; break;
3529 case 'S': DestWidth = 32; break;
3530 case 'D': DestWidth = 64; break;
3531 case 'X': DestWidth = 96; break;
3532 case 'T': DestWidth = 128; break;
3533 }
3534 if (Str[1] == 'F') {
3535 IntegerMode = false;
3536 } else if (Str[1] == 'C') {
3537 IntegerMode = false;
3538 ComplexMode = true;
3539 } else if (Str[1] != 'I') {
3540 DestWidth = 0;
3541 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003542 break;
3543 case 4:
3544 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3545 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003546 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003547 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003548 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003549 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003550 break;
3551 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003552 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003553 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003554 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003555 case 11:
3556 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003557 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003558 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003559 }
3560
3561 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003562 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003563 OldTy = TD->getUnderlyingType();
3564 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3565 OldTy = VD->getType();
3566 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003567 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003568 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003569 return;
3570 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003571
John McCall9dd450b2009-09-21 23:43:11 +00003572 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003573 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3574 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003575 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003576 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3577 } else if (ComplexMode) {
3578 if (!OldTy->isComplexType())
3579 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3580 } else {
3581 if (!OldTy->isFloatingType())
3582 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3583 }
3584
Mike Stump87c57ac2009-05-16 07:39:55 +00003585 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3586 // and friends, at least with glibc.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003587 // FIXME: Make sure floating-point mappings are accurate
3588 // FIXME: Support XF and TF types
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003589 if (!DestWidth) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003590 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003591 return;
Stepan Dyatkovskiyb88c30f2013-09-18 09:08:52 +00003592 }
3593
3594 QualType NewTy;
3595
3596 if (IntegerMode)
3597 NewTy = S.Context.getIntTypeForBitwidth(DestWidth,
3598 OldTy->isSignedIntegerType());
3599 else
3600 NewTy = S.Context.getRealTypeForBitwidth(DestWidth);
3601
3602 if (NewTy.isNull()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003603 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003604 return;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003605 }
3606
Eli Friedman4735374e2009-03-03 06:41:03 +00003607 if (ComplexMode) {
3608 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003609 }
3610
3611 // Install the new type.
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003612 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3613 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3614 else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003615 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00003616
3617 D->addAttr(::new (S.Context)
3618 ModeAttr(Attr.getRange(), S.Context, Name,
3619 Attr.getAttributeSpellingListIndex()));
Chris Lattneracbc2d22008-06-27 22:18:37 +00003620}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003621
Chandler Carruthedc2c642011-07-02 00:01:44 +00003622static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky08597072012-07-24 01:40:49 +00003623 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3624 if (!VD->hasGlobalStorage())
3625 S.Diag(Attr.getLoc(),
3626 diag::warn_attribute_requires_functions_or_static_globals)
3627 << Attr.getName();
3628 } else if (!isFunctionOrMethod(D)) {
3629 S.Diag(Attr.getLoc(),
3630 diag::warn_attribute_requires_functions_or_static_globals)
3631 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003632 return;
3633 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003634
Michael Han99315932013-01-24 16:46:58 +00003635 D->addAttr(::new (S.Context)
3636 NoDebugAttr(Attr.getRange(), S.Context,
3637 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003638}
3639
Chandler Carruthedc2c642011-07-02 00:01:44 +00003640static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003641 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00003642 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003643 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00003644 return;
3645 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003646
Michael Han99315932013-01-24 16:46:58 +00003647 D->addAttr(::new (S.Context)
3648 NoInlineAttr(Attr.getRange(), S.Context,
3649 Attr.getAttributeSpellingListIndex()));
Anders Carlsson88097122009-02-19 19:16:48 +00003650}
3651
Chandler Carruthedc2c642011-07-02 00:01:44 +00003652static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3653 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003654 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003655 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003656 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00003657 return;
3658 }
3659
Michael Han99315932013-01-24 16:46:58 +00003660 D->addAttr(::new (S.Context)
3661 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3662 Attr.getAttributeSpellingListIndex()));
Chris Lattner3c77a352010-06-22 00:03:40 +00003663}
3664
Chandler Carruthedc2c642011-07-02 00:01:44 +00003665static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003666 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003667 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003668 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003669 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003670 return;
3671 }
3672
Michael Han99315932013-01-24 16:46:58 +00003673 D->addAttr(::new (S.Context)
3674 CUDAConstantAttr(Attr.getRange(), S.Context,
3675 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003676 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003677 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003678 }
3679}
3680
Chandler Carruthedc2c642011-07-02 00:01:44 +00003681static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003682 if (S.LangOpts.CUDA) {
3683 // check the attribute arguments.
3684 if (Attr.getNumArgs() != 0) {
Aaron Ballmanb7243382013-07-23 19:30:11 +00003685 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3686 << Attr.getName() << 0;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003687 return;
3688 }
3689
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003690 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003691 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003692 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003693 return;
3694 }
3695
Michael Han99315932013-01-24 16:46:58 +00003696 D->addAttr(::new (S.Context)
3697 CUDADeviceAttr(Attr.getRange(), S.Context,
3698 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003699 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003700 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003701 }
3702}
3703
Chandler Carruthedc2c642011-07-02 00:01:44 +00003704static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003705 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003706 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003707 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003708 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003709 return;
3710 }
3711
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003712 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003713 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00003714 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie6adc78e2013-02-18 22:06:02 +00003715 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003716 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3717 << FD->getType()
David Blaikie6adc78e2013-02-18 22:06:02 +00003718 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003719 "void");
3720 } else {
3721 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3722 << FD->getType();
3723 }
3724 return;
3725 }
3726
Michael Han99315932013-01-24 16:46:58 +00003727 D->addAttr(::new (S.Context)
3728 CUDAGlobalAttr(Attr.getRange(), S.Context,
3729 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003730 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003731 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003732 }
3733}
3734
Chandler Carruthedc2c642011-07-02 00:01:44 +00003735static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003736 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003737 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003738 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003739 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003740 return;
3741 }
3742
Michael Han99315932013-01-24 16:46:58 +00003743 D->addAttr(::new (S.Context)
3744 CUDAHostAttr(Attr.getRange(), S.Context,
3745 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003746 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003747 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003748 }
3749}
3750
Chandler Carruthedc2c642011-07-02 00:01:44 +00003751static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003752 if (S.LangOpts.CUDA) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003753 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003754 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003755 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003756 return;
3757 }
3758
Michael Han99315932013-01-24 16:46:58 +00003759 D->addAttr(::new (S.Context)
3760 CUDASharedAttr(Attr.getRange(), S.Context,
3761 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003762 } else {
Aaron Ballmanecf81c02013-11-19 22:18:24 +00003763 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003764 }
3765}
3766
Chandler Carruthedc2c642011-07-02 00:01:44 +00003767static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003768 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00003769 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003770 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003771 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00003772 return;
3773 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003774
Douglas Gregor35b57532009-10-27 21:01:01 +00003775 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003776 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003777 return;
3778 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003779
Michael Han99315932013-01-24 16:46:58 +00003780 D->addAttr(::new (S.Context)
3781 GNUInlineAttr(Attr.getRange(), S.Context,
3782 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003783}
3784
Chandler Carruthedc2c642011-07-02 00:01:44 +00003785static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003786 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003787
Aaron Ballman02df2e02012-12-09 17:45:41 +00003788 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003789 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003790 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3791 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003792 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003793 return;
3794
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003795 if (!isa<ObjCMethodDecl>(D)) {
3796 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3797 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003798 return;
3799 }
3800
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003801 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003802 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003803 D->addAttr(::new (S.Context)
3804 FastCallAttr(Attr.getRange(), S.Context,
3805 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003806 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003807 case AttributeList::AT_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003808 D->addAttr(::new (S.Context)
3809 StdCallAttr(Attr.getRange(), S.Context,
3810 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003811 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003812 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003813 D->addAttr(::new (S.Context)
3814 ThisCallAttr(Attr.getRange(), S.Context,
3815 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003816 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003817 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003818 D->addAttr(::new (S.Context)
3819 CDeclAttr(Attr.getRange(), S.Context,
3820 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003821 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003822 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003823 D->addAttr(::new (S.Context)
3824 PascalAttr(Attr.getRange(), S.Context,
3825 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003826 return;
Charles Davisb5a214e2013-08-30 04:39:01 +00003827 case AttributeList::AT_MSABI:
3828 D->addAttr(::new (S.Context)
3829 MSABIAttr(Attr.getRange(), S.Context,
3830 Attr.getAttributeSpellingListIndex()));
3831 return;
3832 case AttributeList::AT_SysVABI:
3833 D->addAttr(::new (S.Context)
3834 SysVABIAttr(Attr.getRange(), S.Context,
3835 Attr.getAttributeSpellingListIndex()));
3836 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003837 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003838 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003839 switch (CC) {
3840 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003841 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003842 break;
3843 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003844 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003845 break;
3846 default:
3847 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003848 }
3849
Michael Han99315932013-01-24 16:46:58 +00003850 D->addAttr(::new (S.Context)
3851 PcsAttr(Attr.getRange(), S.Context, PCS,
3852 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003853 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003854 }
Derek Schuffa2020962012-10-16 22:30:41 +00003855 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003856 D->addAttr(::new (S.Context)
3857 PnaclCallAttr(Attr.getRange(), S.Context,
3858 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003859 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003860 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003861 D->addAttr(::new (S.Context)
3862 IntelOclBiccAttr(Attr.getRange(), S.Context,
3863 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003864 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003865
Abramo Bagnara50099372010-04-30 13:10:51 +00003866 default:
3867 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003868 }
3869}
3870
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003871static void handleOpenCLKernelAttr(Sema &S, Decl *D,
3872 const AttributeList &Attr) {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003873 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003874}
3875
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003876static void handleOpenCLImageAccessAttr(Sema &S, Decl *D,
3877 const AttributeList &Attr) {
3878 uint32_t ArgNum;
3879 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), ArgNum))
Guy Benyeifb36ede2013-03-24 13:58:12 +00003880 return;
Guy Benyeifb36ede2013-03-24 13:58:12 +00003881
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003882 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(Attr.getRange(),
3883 S.Context, ArgNum));
Guy Benyeifb36ede2013-03-24 13:58:12 +00003884}
3885
Aaron Ballman02df2e02012-12-09 17:45:41 +00003886bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3887 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003888 if (attr.isInvalid())
3889 return true;
3890
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003891 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman00e99962013-08-31 01:11:41 +00003892 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall3882ace2011-01-05 12:14:39 +00003893 attr.setInvalid();
3894 return true;
3895 }
3896
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003897 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3898 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003899 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003900 case AttributeList::AT_CDecl: CC = CC_C; break;
3901 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3902 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3903 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3904 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davisb5a214e2013-08-30 04:39:01 +00003905 case AttributeList::AT_MSABI:
3906 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3907 CC_X86_64Win64;
3908 break;
3909 case AttributeList::AT_SysVABI:
3910 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3911 CC_C;
3912 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003913 case AttributeList::AT_Pcs: {
Aaron Ballmand6600a52013-09-13 17:48:25 +00003914 StringRef StrRef;
Tim Northover6a6b63b2013-10-01 14:34:18 +00003915 if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003916 attr.setInvalid();
3917 return true;
3918 }
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003919 if (StrRef == "aapcs") {
3920 CC = CC_AAPCS;
3921 break;
3922 } else if (StrRef == "aapcs-vfp") {
3923 CC = CC_AAPCS_VFP;
3924 break;
3925 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003926
3927 attr.setInvalid();
3928 Diag(attr.getLoc(), diag::err_invalid_pcs);
3929 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003930 }
Derek Schuffa2020962012-10-16 22:30:41 +00003931 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003932 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003933 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003934 }
3935
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003936 const TargetInfo &TI = Context.getTargetInfo();
3937 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3938 if (A == TargetInfo::CCCR_Warning) {
3939 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003940
3941 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3942 if (FD)
3943 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3944 TargetInfo::CCMT_NonMember;
3945 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003946 }
3947
John McCall3882ace2011-01-05 12:14:39 +00003948 return false;
3949}
3950
Chandler Carruthedc2c642011-07-02 00:01:44 +00003951static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003952 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003953
3954 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003955 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003956 return;
3957
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003958 if (!isa<ObjCMethodDecl>(D)) {
3959 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3960 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003961 return;
3962 }
Eli Friedman7044b762009-03-27 21:06:47 +00003963
Michael Han99315932013-01-24 16:46:58 +00003964 D->addAttr(::new (S.Context)
3965 RegparmAttr(Attr.getRange(), S.Context, numParams,
3966 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00003967}
3968
3969/// Checks a regparm attribute, returning true if it is ill-formed and
3970/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003971bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3972 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003973 return true;
3974
Aaron Ballmanc2cbc662013-07-18 18:01:48 +00003975 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003976 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003977 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003978 }
Eli Friedman7044b762009-03-27 21:06:47 +00003979
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003980 uint32_t NP;
Aaron Ballman00e99962013-08-31 01:11:41 +00003981 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003982 if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003983 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003984 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003985 }
3986
Douglas Gregore8bbc122011-09-02 00:18:52 +00003987 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003988 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003989 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003990 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003991 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003992 }
3993
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00003994 numParams = NP;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003995 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003996 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003997 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003998 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003999 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00004000 }
4001
John McCall3882ace2011-01-05 12:14:39 +00004002 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004003}
4004
Chandler Carruthedc2c642011-07-02 00:01:44 +00004005static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00004006 if (S.LangOpts.CUDA) {
4007 // check the attribute arguments.
4008 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00004009 // FIXME: 0 is not okay.
4010 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004011 return;
4012 }
4013
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004014 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00004015 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00004016 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004017 return;
4018 }
4019
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004020 uint32_t MaxThreads, MinBlocks;
4021 if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1) ||
4022 !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(1), MinBlocks, 2))
Peter Collingbourne827301e2010-12-12 23:03:07 +00004023 return;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004024
Michael Han99315932013-01-24 16:46:58 +00004025 D->addAttr(::new (S.Context)
4026 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Aaron Ballmanf22ef5a2013-11-21 01:50:40 +00004027 MaxThreads, MinBlocks,
Michael Han99315932013-01-24 16:46:58 +00004028 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00004029 } else {
4030 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4031 }
4032}
4033
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004034static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4035 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00004036 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00004037 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00004038 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004039 return;
4040 }
Aaron Ballman00e99962013-08-31 01:11:41 +00004041
4042 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004043 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004044
Aaron Ballman00e99962013-08-31 01:11:41 +00004045 StringRef AttrName = Attr.getName()->getName();
4046 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004047
4048 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4049 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4050 << Attr.getName() << ExpectedFunctionOrMethod;
4051 return;
4052 }
4053
4054 uint64_t ArgumentIdx;
4055 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4056 Attr.getLoc(), 2,
Aaron Ballman00e99962013-08-31 01:11:41 +00004057 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004058 return;
4059
4060 uint64_t TypeTagIdx;
4061 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4062 Attr.getLoc(), 3,
Aaron Ballman00e99962013-08-31 01:11:41 +00004063 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004064 return;
4065
4066 bool IsPointer = (AttrName == "pointer_with_type_tag");
4067 if (IsPointer) {
4068 // Ensure that buffer has a pointer type.
4069 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4070 if (!BufferTy->isPointerType()) {
4071 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballman317a77f2013-05-22 23:25:32 +00004072 << Attr.getName();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004073 }
4074 }
4075
Michael Han99315932013-01-24 16:46:58 +00004076 D->addAttr(::new (S.Context)
4077 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4078 ArgumentIdx, TypeTagIdx, IsPointer,
4079 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004080}
4081
4082static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4083 const AttributeList &Attr) {
Aaron Ballman00e99962013-08-31 01:11:41 +00004084 if (!Attr.isArgIdent(0)) {
Aaron Ballman29982272013-07-23 14:03:57 +00004085 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3bf758c2013-07-30 01:31:03 +00004086 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004087 return;
4088 }
Aaron Ballman00e99962013-08-31 01:11:41 +00004089
4090 if (!checkAttributeNumArgs(S, Attr, 1))
4091 return;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004092
Aaron Ballman90f8c6f2013-11-25 18:50:49 +00004093 if (!isa<VarDecl>(D)) {
4094 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4095 << Attr.getName() << ExpectedVariable;
4096 return;
4097 }
4098
Aaron Ballman00e99962013-08-31 01:11:41 +00004099 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Richard Smithb87c4652013-10-31 21:23:20 +00004100 TypeSourceInfo *MatchingCTypeLoc = 0;
4101 S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc);
4102 assert(MatchingCTypeLoc && "no type source info for attribute argument");
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004103
Michael Han99315932013-01-24 16:46:58 +00004104 D->addAttr(::new (S.Context)
4105 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
Richard Smithb87c4652013-10-31 21:23:20 +00004106 MatchingCTypeLoc,
Michael Han99315932013-01-24 16:46:58 +00004107 Attr.getLayoutCompatible(),
4108 Attr.getMustBeNull(),
4109 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004110}
4111
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004112//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004113// Checker-specific attribute handlers.
4114//===----------------------------------------------------------------------===//
4115
John McCalled433932011-01-25 03:31:58 +00004116static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004117 return type->isDependentType() ||
4118 type->isObjCObjectPointerType() ||
4119 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00004120}
4121static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004122 return type->isDependentType() ||
4123 type->isPointerType() ||
4124 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00004125}
4126
Chandler Carruthedc2c642011-07-02 00:01:44 +00004127static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004128 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00004129 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004130 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004131 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00004132 return;
4133 }
4134
4135 bool typeOK, cf;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004136 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00004137 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4138 cf = false;
4139 } else {
4140 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4141 cf = true;
4142 }
4143
4144 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004145 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004146 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00004147 return;
4148 }
4149
4150 if (cf)
Michael Han99315932013-01-24 16:46:58 +00004151 param->addAttr(::new (S.Context)
4152 CFConsumedAttr(Attr.getRange(), S.Context,
4153 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004154 else
Michael Han99315932013-01-24 16:46:58 +00004155 param->addAttr(::new (S.Context)
4156 NSConsumedAttr(Attr.getRange(), S.Context,
4157 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004158}
4159
Chandler Carruthedc2c642011-07-02 00:01:44 +00004160static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4161 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004162 if (!isa<ObjCMethodDecl>(D)) {
4163 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004164 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00004165 return;
4166 }
4167
Michael Han99315932013-01-24 16:46:58 +00004168 D->addAttr(::new (S.Context)
4169 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4170 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004171}
4172
Chandler Carruthedc2c642011-07-02 00:01:44 +00004173static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4174 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004175
John McCalled433932011-01-25 03:31:58 +00004176 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004177
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004178 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004179 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004180 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004181 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004182 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00004183 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4184 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004185 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004186 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00004187 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004188 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004189 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00004190 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004191 return;
4192 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004193
John McCalled433932011-01-25 03:31:58 +00004194 bool typeOK;
4195 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004196 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004197 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004198 case AttributeList::AT_NSReturnsAutoreleased:
4199 case AttributeList::AT_NSReturnsRetained:
4200 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004201 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4202 cf = false;
4203 break;
4204
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004205 case AttributeList::AT_CFReturnsRetained:
4206 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004207 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4208 cf = true;
4209 break;
4210 }
4211
4212 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004213 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004214 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004215 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004216 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004217
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004218 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004219 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004220 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004221 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00004222 D->addAttr(::new (S.Context)
4223 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4224 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004225 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004226 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004227 D->addAttr(::new (S.Context)
4228 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4229 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004230 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004231 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004232 D->addAttr(::new (S.Context)
4233 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4234 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004235 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004236 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004237 D->addAttr(::new (S.Context)
4238 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4239 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004240 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004241 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004242 D->addAttr(::new (S.Context)
4243 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4244 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004245 return;
4246 };
4247}
4248
John McCallcf166702011-07-22 08:53:00 +00004249static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4250 const AttributeList &attr) {
Fariborz Jahanian8bf05562013-09-19 17:52:50 +00004251 const int EP_ObjCMethod = 1;
4252 const int EP_ObjCProperty = 2;
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004253
John McCallcf166702011-07-22 08:53:00 +00004254 SourceLocation loc = attr.getLoc();
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004255 QualType resultType;
4256
John McCallcf166702011-07-22 08:53:00 +00004257 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4258
Fariborz Jahaniana53e5d72012-04-21 17:51:44 +00004259 if (!method) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004260 ObjCPropertyDecl *property = dyn_cast<ObjCPropertyDecl>(D);
4261 if (!property) {
4262 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4263 << SourceRange(loc, loc) << attr.getName() << ExpectedMethodOrProperty;
4264 return;
4265 }
4266 resultType = property->getType();
John McCallcf166702011-07-22 08:53:00 +00004267 }
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004268 else
4269 // Check that the method returns a normal pointer.
4270 resultType = method->getResultType();
John McCallcf166702011-07-22 08:53:00 +00004271
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004272 if (!resultType->isReferenceType() &&
4273 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004274 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallcf166702011-07-22 08:53:00 +00004275 << SourceRange(loc)
Fariborz Jahanian5c005832013-09-19 17:18:55 +00004276 << attr.getName() << (method ? EP_ObjCMethod : EP_ObjCProperty)
4277 << /*non-retainable pointer*/ 2;
John McCallcf166702011-07-22 08:53:00 +00004278
4279 // Drop the attribute.
4280 return;
4281 }
4282
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00004283 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004284 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4285 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004286}
4287
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004288static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4289 const AttributeList &attr) {
4290 SourceLocation loc = attr.getLoc();
4291 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4292
4293 if (!method) {
4294 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4295 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4296 return;
4297 }
4298 DeclContext *DC = method->getDeclContext();
4299 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4300 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4301 << attr.getName() << 0;
4302 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4303 return;
4304 }
4305 if (method->getMethodFamily() == OMF_dealloc) {
4306 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4307 << attr.getName() << 1;
4308 return;
4309 }
4310
Michael Han99315932013-01-24 16:46:58 +00004311 method->addAttr(::new (S.Context)
4312 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4313 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004314}
4315
John McCall32f5fe12011-09-30 05:12:12 +00004316/// Handle cf_audited_transfer and cf_unknown_transfer.
4317static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4318 if (!isa<FunctionDecl>(D)) {
4319 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004320 << A.getRange() << A.getName() << ExpectedFunction;
John McCall32f5fe12011-09-30 05:12:12 +00004321 return;
4322 }
4323
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004324 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall32f5fe12011-09-30 05:12:12 +00004325
4326 // Check whether there's a conflicting attribute already present.
4327 Attr *Existing;
4328 if (IsAudited) {
4329 Existing = D->getAttr<CFUnknownTransferAttr>();
4330 } else {
4331 Existing = D->getAttr<CFAuditedTransferAttr>();
4332 }
4333 if (Existing) {
4334 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4335 << A.getName()
4336 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4337 << A.getRange() << Existing->getRange();
4338 return;
4339 }
4340
4341 // All clear; add the attribute.
4342 if (IsAudited) {
Michael Han99315932013-01-24 16:46:58 +00004343 D->addAttr(::new (S.Context)
4344 CFAuditedTransferAttr(A.getRange(), S.Context,
4345 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004346 } else {
Michael Han99315932013-01-24 16:46:58 +00004347 D->addAttr(::new (S.Context)
4348 CFUnknownTransferAttr(A.getRange(), S.Context,
4349 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004350 }
4351}
4352
John McCallf1e8b342011-09-29 07:17:38 +00004353static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4354 const AttributeList &Attr) {
4355 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4356 if (!RD || RD->isUnion()) {
4357 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004358 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00004359 }
4360
Aaron Ballman00e99962013-08-31 01:11:41 +00004361 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallf1e8b342011-09-29 07:17:38 +00004362
4363 // In Objective-C, verify that the type names an Objective-C type.
4364 // We don't want to check this outside of ObjC because people sometimes
4365 // do crazy C declarations of Objective-C types.
Aaron Ballman00e99962013-08-31 01:11:41 +00004366 if (Parm && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00004367 // Check for an existing type with this name.
Aaron Ballman00e99962013-08-31 01:11:41 +00004368 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallf1e8b342011-09-29 07:17:38 +00004369 Sema::LookupOrdinaryName);
4370 if (S.LookupName(R, Sc)) {
4371 NamedDecl *Target = R.getFoundDecl();
4372 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4373 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4374 S.Diag(Target->getLocStart(), diag::note_declared_at);
4375 }
4376 }
4377 }
4378
Michael Han99315932013-01-24 16:46:58 +00004379 D->addAttr(::new (S.Context)
Aaron Ballman00e99962013-08-31 01:11:41 +00004380 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han99315932013-01-24 16:46:58 +00004381 Attr.getAttributeSpellingListIndex()));
John McCallf1e8b342011-09-29 07:17:38 +00004382}
4383
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004384static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
4385 const AttributeList &Attr) {
Fariborz Jahaniandb3d8552013-11-19 00:09:48 +00004386 if (!isa<RecordDecl>(D)) {
Fariborz Jahanianf720f862013-11-19 17:42:25 +00004387 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4388 << Attr.getName()
4389 << (S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass
4390 : ExpectedStructOrUnion);
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004391 return;
4392 }
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004393
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00004394 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004395
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004396 if (!Parm) {
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004397 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004398 return;
4399 }
4400
4401 D->addAttr(::new (S.Context)
Ted Kremenek2d3379e2013-11-21 07:20:34 +00004402 ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident,
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004403 Attr.getAttributeSpellingListIndex()));
4404}
4405
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004406static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D,
4407 const AttributeList &Attr) {
4408 if (!isa<RecordDecl>(D)) {
4409 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4410 << Attr.getName()
4411 << (S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass
4412 : ExpectedStructOrUnion);
4413 return;
4414 }
4415
Fariborz Jahanian2651ac52013-11-22 00:02:22 +00004416 IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004417
4418 if (!Parm) {
4419 S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0;
4420 return;
4421 }
4422
4423 D->addAttr(::new (S.Context)
4424 ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident,
4425 Attr.getAttributeSpellingListIndex()));
4426}
4427
Chandler Carruthedc2c642011-07-02 00:01:44 +00004428static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4429 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004430 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004431
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004432 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004433 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004434}
4435
Chandler Carruthedc2c642011-07-02 00:01:44 +00004436static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4437 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004438 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004439 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004440 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004441 return;
4442 }
4443
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004444 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004445 QualType type = vd->getType();
4446
4447 if (!type->isDependentType() &&
4448 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004449 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004450 << type;
4451 return;
4452 }
4453
4454 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4455
4456 // If we have no lifetime yet, check the lifetime we're presumably
4457 // going to infer.
4458 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4459 lifetime = type->getObjCARCImplicitLifetime();
4460
4461 switch (lifetime) {
4462 case Qualifiers::OCL_None:
4463 assert(type->isDependentType() &&
4464 "didn't infer lifetime for non-dependent type?");
4465 break;
4466
4467 case Qualifiers::OCL_Weak: // meaningful
4468 case Qualifiers::OCL_Strong: // meaningful
4469 break;
4470
4471 case Qualifiers::OCL_ExplicitNone:
4472 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004473 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004474 << (lifetime == Qualifiers::OCL_Autoreleasing);
4475 break;
4476 }
4477
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004478 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004479 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4480 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004481}
4482
Francois Picheta83957a2010-12-19 06:50:37 +00004483//===----------------------------------------------------------------------===//
4484// Microsoft specific attribute handlers.
4485//===----------------------------------------------------------------------===//
4486
Reid Kleckner140c4a72013-05-17 14:04:52 +00004487// Check if MS extensions or some other language extensions are enabled. If
4488// not, issue a diagnostic that the given attribute is unused.
4489static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4490 bool OtherExtension = false) {
4491 if (S.LangOpts.MicrosoftExt || OtherExtension)
4492 return true;
4493 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4494 return false;
4495}
4496
Chandler Carruthedc2c642011-07-02 00:01:44 +00004497static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballmandf8fe4c2013-11-24 21:35:16 +00004498 if (!S.LangOpts.CPlusPlus) {
4499 S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
4500 << Attr.getName() << AttributeLangSupport::C;
4501 return;
4502 }
4503
Reid Kleckner140c4a72013-05-17 14:04:52 +00004504 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4505 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004506
Aaron Ballman60e705e2013-11-24 20:58:02 +00004507 if (!isa<CXXRecordDecl>(D)) {
4508 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4509 << Attr.getName() << ExpectedClass;
4510 return;
4511 }
4512
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004513 StringRef StrRef;
4514 SourceLocation LiteralLoc;
Tim Northover6a6b63b2013-10-01 14:34:18 +00004515 if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc))
Reid Kleckner140c4a72013-05-17 14:04:52 +00004516 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004517
David Majnemer89085342013-08-09 08:56:20 +00004518 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4519 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
David Majnemer89085342013-08-09 08:56:20 +00004520 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4521 StrRef = StrRef.drop_front().drop_back();
Francois Pichet7da11662010-12-20 01:41:49 +00004522
Reid Kleckner140c4a72013-05-17 14:04:52 +00004523 // Validate GUID length.
David Majnemer89085342013-08-09 08:56:20 +00004524 if (StrRef.size() != 36) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004525 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004526 return;
4527 }
Anders Carlsson19588aa2011-01-23 21:07:30 +00004528
David Majnemer89085342013-08-09 08:56:20 +00004529 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004530 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemer89085342013-08-09 08:56:20 +00004531 if (StrRef[i] != '-') {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004532 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Francois Pichet7da11662010-12-20 01:41:49 +00004533 return;
4534 }
David Majnemer89085342013-08-09 08:56:20 +00004535 } else if (!isHexDigit(StrRef[i])) {
Benjamin Kramer6ee15622013-09-13 15:35:43 +00004536 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
Reid Kleckner140c4a72013-05-17 14:04:52 +00004537 return;
Francois Pichet7da11662010-12-20 01:41:49 +00004538 }
Reid Kleckner140c4a72013-05-17 14:04:52 +00004539 }
Francois Picheta83957a2010-12-19 06:50:37 +00004540
David Majnemer89085342013-08-09 08:56:20 +00004541 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4542 Attr.getAttributeSpellingListIndex()));
Charles Davis163855f2010-02-16 18:27:26 +00004543}
4544
John McCall8d32c052012-05-22 21:28:12 +00004545static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004546 if (!checkMicrosoftExt(S, Attr))
Nico Weberefa45b22012-11-07 21:31:36 +00004547 return;
Nico Weberefa45b22012-11-07 21:31:36 +00004548
4549 AttributeList::Kind Kind = Attr.getKind();
4550 if (Kind == AttributeList::AT_SingleInheritance)
4551 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004552 ::new (S.Context)
4553 SingleInheritanceAttr(Attr.getRange(), S.Context,
4554 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004555 else if (Kind == AttributeList::AT_MultipleInheritance)
4556 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004557 ::new (S.Context)
4558 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4559 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004560 else if (Kind == AttributeList::AT_VirtualInheritance)
4561 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004562 ::new (S.Context)
4563 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4564 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004565}
4566
4567static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004568 if (!checkMicrosoftExt(S, Attr))
4569 return;
4570
4571 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmancc14f3a2013-11-22 21:49:04 +00004572 if (Kind == AttributeList::AT_Win64)
4573 D->addAttr(
4574 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4575 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004576}
4577
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004578static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner140c4a72013-05-17 14:04:52 +00004579 if (!checkMicrosoftExt(S, Attr))
4580 return;
4581 D->addAttr(::new (S.Context)
4582 ForceInlineAttr(Attr.getRange(), S.Context,
4583 Attr.getAttributeSpellingListIndex()));
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004584}
4585
Reid Klecknerb144d362013-05-20 14:02:37 +00004586static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4587 if (!checkMicrosoftExt(S, Attr))
4588 return;
4589 // Check linkage after possibly merging declaratinos. See
4590 // checkAttributesAfterMerging().
4591 D->addAttr(::new (S.Context)
4592 SelectAnyAttr(Attr.getRange(), S.Context,
4593 Attr.getAttributeSpellingListIndex()));
4594}
4595
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004596/// Handles semantic checking for features that are common to all attributes,
4597/// such as checking whether a parameter was properly specified, or the correct
4598/// number of arguments were passed, etc.
4599static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4600 const AttributeList &Attr) {
4601 // Several attributes carry different semantics than the parsing requires, so
4602 // those are opted out of the common handling.
4603 //
4604 // We also bail on unknown and ignored attributes because those are handled
4605 // as part of the target-specific handling logic.
4606 if (Attr.hasCustomParsing() ||
4607 Attr.getKind() == AttributeList::UnknownAttribute ||
4608 Attr.getKind() == AttributeList::IgnoredAttribute)
4609 return false;
4610
4611 // If there are no optional arguments, then checking for the argument count
4612 // is trivial.
4613 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
4614 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4615 return true;
4616 return false;
4617}
4618
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004619//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004620// Top Level Sema Entry Points
4621//===----------------------------------------------------------------------===//
4622
Richard Smithf8a75c32013-08-29 00:47:48 +00004623/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4624/// the attribute applies to decls. If the attribute is a type attribute, just
4625/// silently ignore it if a GNU attribute.
4626static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4627 const AttributeList &Attr,
4628 bool IncludeCXX11Attributes) {
4629 if (Attr.isInvalid())
4630 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00004631
Richard Smithf8a75c32013-08-29 00:47:48 +00004632 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4633 // instead.
4634 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4635 return;
4636
Aaron Ballman8ee40b72013-09-09 23:33:17 +00004637 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4638 return;
4639
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004640 switch (Attr.getKind()) {
Richard Smith10876ef2013-01-17 01:30:42 +00004641 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4642 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4643 case AttributeList::AT_IBOutletCollection:
4644 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004645 case AttributeList::AT_AddressSpace:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004646 case AttributeList::AT_ObjCGC:
4647 case AttributeList::AT_VectorSize:
4648 case AttributeList::AT_NeonVectorType:
4649 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004650 case AttributeList::AT_Ptr32:
4651 case AttributeList::AT_Ptr64:
4652 case AttributeList::AT_SPtr:
4653 case AttributeList::AT_UPtr:
Mike Stumpd3bb5572009-07-24 19:02:52 +00004654 // Ignore these, these are type attributes, handled by
4655 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004656 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004657 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4658 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4659 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4660 case AttributeList::AT_AlwaysInline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004661 handleAlwaysInlineAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004662 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004663 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00004664 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004665 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4666 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4667 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004668 handleDependencyAttr(S, scope, D, Attr);
4669 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004670 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4671 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4672 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004673 case AttributeList::AT_CXX11NoReturn:
4674 handleCXX11NoReturnAttr(S, D, Attr);
4675 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004676 case AttributeList::AT_Deprecated:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004677 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004678 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004679 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4680 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004681 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004682 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004683 case AttributeList::AT_MinSize:
4684 handleMinSizeAttr(S, D, Attr);
4685 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004686 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4687 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4688 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman9a226212013-08-28 23:13:26 +00004689 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4690 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004691 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4692 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004693 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004694 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004695 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4696 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00004697 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004698 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4699 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Richard Smithf8a75c32013-08-29 00:47:48 +00004700 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00004701 case AttributeList::AT_ownership_returns:
4702 case AttributeList::AT_ownership_takes:
4703 case AttributeList::AT_ownership_holds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004704 handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004705 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4706 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4707 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4708 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4709 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4710 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4711 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004712
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004713 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004714 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004715 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004716 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004717
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004718 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004719 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4720
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004721 case AttributeList::AT_ObjCRequiresSuper:
4722 handleObjCRequiresSuperAttr(S, D, Attr); break;
4723
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004724 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00004725 handleNSBridgedAttr(S, scope, D, Attr); break;
Fariborz Jahanian0a0a3972013-11-13 23:59:17 +00004726
4727 case AttributeList::AT_ObjCBridge:
4728 handleObjCBridgeAttr(S, scope, D, Attr); break;
Fariborz Jahanian87c77912013-11-21 20:50:32 +00004729
4730 case AttributeList::AT_ObjCBridgeMutable:
4731 handleObjCBridgeMutableAttr(S, scope, D, Attr); break;
John McCallf1e8b342011-09-29 07:17:38 +00004732
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004733 case AttributeList::AT_CFAuditedTransfer:
4734 case AttributeList::AT_CFUnknownTransfer:
John McCall32f5fe12011-09-30 05:12:12 +00004735 handleCFTransferAttr(S, D, Attr); break;
4736
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004737 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004738 case AttributeList::AT_CFConsumed:
4739 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4740 case AttributeList::AT_NSConsumesSelf:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004741 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004742
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004743 case AttributeList::AT_NSReturnsAutoreleased:
4744 case AttributeList::AT_NSReturnsNotRetained:
4745 case AttributeList::AT_CFReturnsNotRetained:
4746 case AttributeList::AT_NSReturnsRetained:
4747 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004748 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004749
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004750 case AttributeList::AT_WorkGroupSizeHint:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004751 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004752 handleWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00004753
Joey Goulyaba589c2013-03-08 09:42:32 +00004754 case AttributeList::AT_VecTypeHint:
4755 handleVecTypeHint(S, D, Attr); break;
4756
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004757 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004758 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004759
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004760 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4761 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4762 case AttributeList::AT_Unavailable:
Aaron Ballman8b8ebdd2013-07-18 13:13:52 +00004763 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004764 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004765 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00004766 handleArcWeakrefUnavailableAttr (S, D, Attr);
4767 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004768 case AttributeList::AT_ObjCRootClass:
Patrick Beardacfbe9e2012-04-06 18:12:22 +00004769 handleObjCRootClassAttr(S, D, Attr);
4770 break;
Ted Kremenek28eace62013-11-23 01:01:34 +00004771 case AttributeList::AT_ObjCSuppressProtocol:
4772 handleObjCSuppresProtocolAttr(S, D, Attr);
4773 break;
4774 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00004775 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00004776 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004777 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4778 case AttributeList::AT_ReturnsTwice:
Rafael Espindola70107f92011-10-03 14:59:42 +00004779 handleReturnsTwiceAttr(S, D, Attr);
4780 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004781 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld041a9b2013-02-20 01:54:26 +00004782 case AttributeList::AT_Visibility:
4783 handleVisibilityAttr(S, D, Attr, false);
4784 break;
4785 case AttributeList::AT_TypeVisibility:
4786 handleVisibilityAttr(S, D, Attr, true);
4787 break;
Lubos Lunakedc13882013-07-20 15:05:36 +00004788 case AttributeList::AT_WarnUnused:
4789 handleWarnUnusedAttr(S, D, Attr);
4790 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004791 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004792 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004793 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4794 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4795 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4796 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004797 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004798 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004799 case AttributeList::AT_ObjCException:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004800 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00004801 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004802 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004803 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004804 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004805 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4806 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4807 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4808 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4809 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4810 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4811 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4812 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4813 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004814 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004815 // Just ignore
4816 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004817 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00004818 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00004819 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004820 case AttributeList::AT_StdCall:
4821 case AttributeList::AT_CDecl:
4822 case AttributeList::AT_FastCall:
4823 case AttributeList::AT_ThisCall:
4824 case AttributeList::AT_Pascal:
Charles Davisb5a214e2013-08-30 04:39:01 +00004825 case AttributeList::AT_MSABI:
4826 case AttributeList::AT_SysVABI:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004827 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004828 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004829 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004830 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004831 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004832 case AttributeList::AT_OpenCLKernel:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004833 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00004834 break;
Guy Benyeifb36ede2013-03-24 13:58:12 +00004835 case AttributeList::AT_OpenCLImageAccess:
4836 handleOpenCLImageAccessAttr(S, D, Attr);
4837 break;
John McCall8d32c052012-05-22 21:28:12 +00004838
4839 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004840 case AttributeList::AT_MsStruct:
John McCall8d32c052012-05-22 21:28:12 +00004841 handleMsStructAttr(S, D, Attr);
4842 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004843 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004844 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004845 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004846 case AttributeList::AT_SingleInheritance:
4847 case AttributeList::AT_MultipleInheritance:
4848 case AttributeList::AT_VirtualInheritance:
John McCall8d32c052012-05-22 21:28:12 +00004849 handleInheritanceAttr(S, D, Attr);
4850 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004851 case AttributeList::AT_Win64:
John McCall8d32c052012-05-22 21:28:12 +00004852 handlePortabilityAttr(S, D, Attr);
4853 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004854 case AttributeList::AT_ForceInline:
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004855 handleForceInlineAttr(S, D, Attr);
4856 break;
Reid Klecknerb144d362013-05-20 14:02:37 +00004857 case AttributeList::AT_SelectAny:
4858 handleSelectAnyAttr(S, D, Attr);
4859 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004860
4861 // Thread safety attributes:
DeLesley Hutchinsb6824312013-05-17 23:02:59 +00004862 case AttributeList::AT_AssertExclusiveLock:
4863 handleAssertExclusiveLockAttr(S, D, Attr);
4864 break;
4865 case AttributeList::AT_AssertSharedLock:
4866 handleAssertSharedLockAttr(S, D, Attr);
4867 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004868 case AttributeList::AT_GuardedVar:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004869 handleGuardedVarAttr(S, D, Attr);
4870 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004871 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004872 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004873 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004874 case AttributeList::AT_ScopedLockable:
Michael Han3be3b442012-07-23 18:48:41 +00004875 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004876 break;
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004877 case AttributeList::AT_NoSanitizeAddress:
4878 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004879 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004880 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany4c0fc992013-02-26 06:58:27 +00004881 handleNoThreadSafetyAnalysis(S, D, Attr);
4882 break;
4883 case AttributeList::AT_NoSanitizeThread:
4884 handleNoSanitizeThread(S, D, Attr);
4885 break;
4886 case AttributeList::AT_NoSanitizeMemory:
4887 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004888 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004889 case AttributeList::AT_Lockable:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004890 handleLockableAttr(S, D, Attr);
4891 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004892 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004893 handleGuardedByAttr(S, D, Attr);
4894 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004895 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004896 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004897 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004898 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004899 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004900 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004901 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004902 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004903 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004904 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004905 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004906 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004907 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004908 handleLockReturnedAttr(S, D, Attr);
4909 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004910 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004911 handleLocksExcludedAttr(S, D, Attr);
4912 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004913 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004914 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004915 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004916 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004917 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004918 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004919 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004920 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004921 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004922 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004923 handleUnlockFunAttr(S, D, Attr);
4924 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004925 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004926 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004927 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004928 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004929 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004930 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004931
DeLesley Hutchins8d41d992013-10-11 22:30:48 +00004932 // Consumed analysis attributes.
DeLesley Hutchins5a715c42013-08-30 22:56:34 +00004933 case AttributeList::AT_Consumable:
4934 handleConsumableAttr(S, D, Attr);
4935 break;
DeLesley Hutchins210791a2013-10-04 21:28:06 +00004936 case AttributeList::AT_CallableWhen:
4937 handleCallableWhenAttr(S, D, Attr);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004938 break;
DeLesley Hutchins69391772013-10-17 23:23:53 +00004939 case AttributeList::AT_ParamTypestate:
4940 handleParamTypestateAttr(S, D, Attr);
4941 break;
DeLesley Hutchinsfc368252013-09-03 20:11:38 +00004942 case AttributeList::AT_ReturnTypestate:
4943 handleReturnTypestateAttr(S, D, Attr);
4944 break;
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004945 case AttributeList::AT_SetTypestate:
4946 handleSetTypestateAttr(S, D, Attr);
4947 break;
Chris Wailes9385f9f2013-10-29 20:28:41 +00004948 case AttributeList::AT_TestTypestate:
4949 handleTestTypestateAttr(S, D, Attr);
DeLesley Hutchins33a29342013-10-11 23:03:26 +00004950 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00004951
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004952 // Type safety attributes.
4953 case AttributeList::AT_ArgumentWithTypeTag:
4954 handleArgumentWithTypeTagAttr(S, D, Attr);
4955 break;
4956 case AttributeList::AT_TypeTagForDatatype:
4957 handleTypeTagForDatatypeAttr(S, D, Attr);
4958 break;
4959
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004960 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004961 // Ask target about the attribute.
4962 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4963 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004964 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4965 diag::warn_unhandled_ms_attribute_ignored :
4966 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004967 break;
4968 }
4969}
4970
4971/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4972/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004973void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004974 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004975 bool IncludeCXX11Attributes) {
4976 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smithf8a75c32013-08-29 00:47:48 +00004977 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004978
4979 // GCC accepts
4980 // static int a9 __attribute__((weakref));
4981 // but that looks really pointless. We reject it.
Richard Smithf8a75c32013-08-29 00:47:48 +00004982 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004983 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00004984 cast<NamedDecl>(D)->getNameAsString();
4985 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004986 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004987 }
4988}
4989
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004990// Annotation attributes are the only attributes allowed after an access
4991// specifier.
4992bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4993 const AttributeList *AttrList) {
4994 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004995 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004996 handleAnnotateAttr(*this, ASDecl, *l);
4997 } else {
4998 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4999 return true;
5000 }
5001 }
5002
5003 return false;
5004}
5005
John McCall42856de2011-10-01 05:17:03 +00005006/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5007/// contains any decl attributes that we should warn about.
5008static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5009 for ( ; A; A = A->getNext()) {
5010 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00005011 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00005012 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5013
5014 if (A->getKind() == AttributeList::UnknownAttribute) {
5015 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5016 << A->getName() << A->getRange();
5017 } else {
5018 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5019 << A->getName() << A->getRange();
5020 }
5021 }
5022}
5023
5024/// checkUnusedDeclAttributes - Given a declarator which is not being
5025/// used to build a declaration, complain about any decl attributes
5026/// which might be lying around on it.
5027void Sema::checkUnusedDeclAttributes(Declarator &D) {
5028 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5029 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5030 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5031 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5032}
5033
Ryan Flynn7d470f32009-07-30 03:15:39 +00005034/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00005035/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00005036NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5037 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00005038 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005039 NamedDecl *NewD = 0;
5040 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00005041 FunctionDecl *NewFD;
5042 // FIXME: Missing call to CheckFunctionDeclaration().
5043 // FIXME: Mangling?
5044 // FIXME: Is the qualifier info correct?
5045 // FIXME: Is the DeclContext correct?
5046 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5047 Loc, Loc, DeclarationName(II),
5048 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005049 SC_None, false/*isInlineSpecified*/,
Eli Friedmance3e2c82011-09-07 04:05:06 +00005050 FD->hasPrototype(),
5051 false/*isConstexprSpecified*/);
5052 NewD = NewFD;
5053
5054 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00005055 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00005056
5057 // Fake up parameter variables; they are declared as if this were
5058 // a typedef.
5059 QualType FDTy = FD->getType();
5060 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5061 SmallVector<ParmVarDecl*, 16> Params;
5062 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5063 AE = FT->arg_type_end(); AI != AE; ++AI) {
5064 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5065 Param->setScopeInfo(0, Params.size());
5066 Params.push_back(Param);
5067 }
David Blaikie9c70e042011-09-21 18:16:56 +00005068 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00005069 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005070 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5071 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00005072 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00005073 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00005074 VD->getStorageClass());
John McCall3e11ebe2010-03-15 10:12:16 +00005075 if (VD->getQualifier()) {
5076 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00005077 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00005078 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005079 }
5080 return NewD;
5081}
5082
James Dennett634962f2012-06-14 21:40:34 +00005083/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00005084/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00005085void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00005086 if (W.getUsed()) return; // only do this once
5087 W.setUsed(true);
5088 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5089 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00005090 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005091 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5092 NDId->getName()));
5093 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00005094 WeakTopLevelDecl.push_back(NewD);
5095 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5096 // to insert Decl at TU scope, sorry.
5097 DeclContext *SavedContext = CurContext;
5098 CurContext = Context.getTranslationUnitDecl();
5099 PushOnScopeChains(NewD, S);
5100 CurContext = SavedContext;
5101 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00005102 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00005103 }
5104}
5105
Rafael Espindolade6a39f2013-03-02 21:41:48 +00005106void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5107 // It's valid to "forward-declare" #pragma weak, in which case we
5108 // have to do this.
5109 LoadExternalWeakUndeclaredIdentifiers();
5110 if (!WeakUndeclaredIdentifiers.empty()) {
5111 NamedDecl *ND = NULL;
5112 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5113 if (VD->isExternC())
5114 ND = VD;
5115 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5116 if (FD->isExternC())
5117 ND = FD;
5118 if (ND) {
5119 if (IdentifierInfo *Id = ND->getIdentifier()) {
5120 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5121 = WeakUndeclaredIdentifiers.find(Id);
5122 if (I != WeakUndeclaredIdentifiers.end()) {
5123 WeakInfo W = I->second;
5124 DeclApplyPragmaWeak(S, ND, W);
5125 WeakUndeclaredIdentifiers[Id] = W;
5126 }
5127 }
5128 }
5129 }
5130}
5131
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005132/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5133/// it, apply them to D. This is a bit tricky because PD can have attributes
5134/// specified in many different places, and we need to find and apply them all.
Richard Smithf8a75c32013-08-29 00:47:48 +00005135void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005136 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00005137 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smithf8a75c32013-08-29 00:47:48 +00005138 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005139
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005140 // Walk the declarator structure, applying decl attributes that were in a type
5141 // position to the decl itself. This handles cases like:
5142 // int *__attr__(x)** D;
5143 // when X is a decl attribute.
5144 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5145 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smithf8a75c32013-08-29 00:47:48 +00005146 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005147
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005148 // Finally, apply any attributes on the decl itself.
5149 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smithf8a75c32013-08-29 00:47:48 +00005150 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005151}
John McCall28a6aea2009-11-04 02:18:39 +00005152
John McCall31168b02011-06-15 23:02:42 +00005153/// Is the given declaration allowed to use a forbidden type?
5154static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5155 // Private ivars are always okay. Unfortunately, people don't
5156 // always properly make their ivars private, even in system headers.
5157 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005158 // Function declarations in sys headers will be marked unavailable.
5159 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5160 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005161 return false;
5162
5163 // Require it to be declared in a system header.
5164 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5165}
5166
5167/// Handle a delayed forbidden-type diagnostic.
5168static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5169 Decl *decl) {
5170 if (decl && isForbiddenTypeAllowed(S, decl)) {
5171 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5172 "this system declaration uses an unsupported type"));
5173 return;
5174 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005175 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005176 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005177 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005178 // kind of forbidden type messages on unavailable functions.
5179 if (FD->hasAttr<UnavailableAttr>() &&
5180 diag.getForbiddenTypeDiagnostic() ==
5181 diag::err_arc_array_param_no_ownership) {
5182 diag.Triggered = true;
5183 return;
5184 }
5185 }
John McCall31168b02011-06-15 23:02:42 +00005186
5187 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5188 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5189 diag.Triggered = true;
5190}
5191
John McCall2ec85372012-05-07 06:16:41 +00005192void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5193 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005194 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005195 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005196
John McCall2ec85372012-05-07 06:16:41 +00005197 // When delaying diagnostics to run in the context of a parsed
5198 // declaration, we only want to actually emit anything if parsing
5199 // succeeds.
5200 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005201
John McCall2ec85372012-05-07 06:16:41 +00005202 // We emit all the active diagnostics in this pool or any of its
5203 // parents. In general, we'll get one pool for the decl spec
5204 // and a child pool for each declarator; in a decl group like:
5205 // deprecated_typedef foo, *bar, baz();
5206 // only the declarator pops will be passed decls. This is correct;
5207 // we really do need to consider delayed diagnostics from the decl spec
5208 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00005209 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00005210 do {
John McCall6347b682012-05-07 06:16:58 +00005211 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00005212 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5213 // This const_cast is a bit lame. Really, Triggered should be mutable.
5214 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00005215 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00005216 continue;
5217
John McCallc1465822011-02-14 07:13:47 +00005218 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00005219 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00005220 // Don't bother giving deprecation diagnostics if the decl is invalid.
5221 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00005222 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005223 break;
5224
5225 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00005226 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005227 break;
John McCall31168b02011-06-15 23:02:42 +00005228
5229 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00005230 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00005231 break;
John McCall86121512010-01-27 03:50:35 +00005232 }
5233 }
John McCall2ec85372012-05-07 06:16:41 +00005234 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00005235}
5236
John McCall6347b682012-05-07 06:16:58 +00005237/// Given a set of delayed diagnostics, re-emit them as if they had
5238/// been delayed in the current context instead of in the given pool.
5239/// Essentially, this just moves them to the current pool.
5240void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5241 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5242 assert(curPool && "re-emitting in undelayed context not supported");
5243 curPool->steal(pool);
5244}
5245
John McCall28a6aea2009-11-04 02:18:39 +00005246static bool isDeclDeprecated(Decl *D) {
5247 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005248 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00005249 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00005250 // A category implicitly has the availability of the interface.
5251 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5252 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00005253 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5254 return false;
5255}
5256
Eli Friedman971bfa12012-08-08 21:52:41 +00005257static void
5258DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5259 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005260 const ObjCInterfaceDecl *UnknownObjCClass,
5261 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedman971bfa12012-08-08 21:52:41 +00005262 DeclarationName Name = D->getDeclName();
5263 if (!Message.empty()) {
5264 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5265 S.Diag(D->getLocation(),
5266 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5267 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005268 if (ObjCPropery)
5269 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5270 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005271 } else if (!UnknownObjCClass) {
5272 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5273 S.Diag(D->getLocation(),
5274 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5275 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005276 if (ObjCPropery)
5277 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5278 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005279 } else {
5280 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5281 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5282 }
5283}
5284
John McCallb45a1e72010-08-26 02:13:20 +00005285void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00005286 Decl *Ctx) {
5287 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00005288 return;
5289
John McCall86121512010-01-27 03:50:35 +00005290 DD.Triggered = true;
Eli Friedman971bfa12012-08-08 21:52:41 +00005291 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5292 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005293 DD.getUnknownObjCClass(),
5294 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00005295}
5296
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005297void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00005298 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005299 const ObjCInterfaceDecl *UnknownObjCClass,
5300 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00005301 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00005302 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005303 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5304 UnknownObjCClass,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005305 ObjCProperty,
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005306 Message));
John McCall28a6aea2009-11-04 02:18:39 +00005307 return;
5308 }
5309
5310 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00005311 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00005312 return;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005313 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00005314}