blob: 4c8d098158eceab3aee17bf800370b273e896201 [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"
Jordan Rosea7d03842013-02-08 22:30:41 +000022#include "clang/Basic/CharInfo.h"
John McCall31168b02011-06-15 23:02:42 +000023#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000024#include "clang/Basic/TargetInfo.h"
John McCall8b0666c2010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000026#include "clang/Sema/DelayedDiagnostic.h"
John McCallf1e8b342011-09-29 07:17:38 +000027#include "clang/Sema/Lookup.h"
Richard Smithe233fbf2013-01-28 22:42:45 +000028#include "clang/Sema/Scope.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000029#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000030using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000031using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000032
John McCall5fca7ea2011-03-02 12:29:23 +000033/// These constants match the enumerated choices of
34/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000035enum AttributeDeclKind {
John McCall5fca7ea2011-03-02 12:29:23 +000036 ExpectedFunction,
37 ExpectedUnion,
38 ExpectedVariableOrFunction,
39 ExpectedFunctionOrMethod,
40 ExpectedParameter,
John McCall5fca7ea2011-03-02 12:29:23 +000041 ExpectedFunctionMethodOrBlock,
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +000042 ExpectedFunctionMethodOrClass,
John McCall5fca7ea2011-03-02 12:29:23 +000043 ExpectedFunctionMethodOrParameter,
44 ExpectedClass,
John McCall5fca7ea2011-03-02 12:29:23 +000045 ExpectedVariable,
46 ExpectedMethod,
Caitlin Sadowski63fa6672011-07-28 20:12:35 +000047 ExpectedVariableFunctionOrLabel,
Douglas Gregor5c3cc422012-03-14 16:55:17 +000048 ExpectedFieldOrGlobalVar,
Hans Wennborgd3b01bc2012-06-23 11:51:46 +000049 ExpectedStruct,
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +000050 ExpectedVariableFunctionOrTag,
Richard Smith9eaab4b2013-02-01 08:25:07 +000051 ExpectedTLSVar,
52 ExpectedVariableOrField,
53 ExpectedVariableFieldOrTag
John McCall5fca7ea2011-03-02 12:29:23 +000054};
55
Chris Lattner58418ff2008-06-29 00:16:31 +000056//===----------------------------------------------------------------------===//
57// Helper functions
58//===----------------------------------------------------------------------===//
59
Chandler Carruthff4c4f02011-07-01 23:49:12 +000060static const FunctionType *getFunctionType(const Decl *D,
Ted Kremenek527042b2009-08-14 20:49:40 +000061 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000062 QualType Ty;
Chandler Carruthff4c4f02011-07-01 23:49:12 +000063 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000064 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000065 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000066 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000067 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000068 Ty = decl->getUnderlyingType();
69 else
70 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000071
Chris Lattner2c6fcf52008-06-26 18:38:35 +000072 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000073 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000074 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000075 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000076
John McCall9dd450b2009-09-21 23:43:11 +000077 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000078}
79
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000080// FIXME: We should provide an abstraction around a method or function
81// to provide the following bits of information.
82
Nuno Lopes518e3702009-12-20 23:11:08 +000083/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000084/// type (function or function-typed variable).
Chandler Carruthff4c4f02011-07-01 23:49:12 +000085static bool isFunction(const Decl *D) {
86 return getFunctionType(D, false) != NULL;
Ted Kremenek527042b2009-08-14 20:49:40 +000087}
88
89/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000090/// type (function or function-typed variable) or an Objective-C
91/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000092static bool isFunctionOrMethod(const Decl *D) {
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +000093 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000094}
95
Fariborz Jahanian4447e172009-05-15 23:15:03 +000096/// isFunctionOrMethodOrBlock - Return true if the given decl has function
97/// type (function or function-typed variable) or an Objective-C
98/// method or a block.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000099static bool isFunctionOrMethodOrBlock(const Decl *D) {
100 if (isFunctionOrMethod(D))
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000101 return true;
102 // check for block is more involved.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000103 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000104 QualType Ty = V->getType();
105 return Ty->isBlockPointerType();
106 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000107 return isa<BlockDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000108}
109
John McCall3882ace2011-01-05 12:14:39 +0000110/// Return true if the given decl has a declarator that should have
111/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000112static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +0000113 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000114 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
115 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +0000116}
117
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000118/// hasFunctionProto - Return true if the given decl has a argument
119/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000120/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000121static bool hasFunctionProto(const Decl *D) {
122 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000123 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000124 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000125 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000126 return true;
127 }
128}
129
130/// getFunctionOrMethodNumArgs - Return number of function or method
131/// arguments. It is an error to call this on a K&R function (use
132/// hasFunctionProto first).
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000133static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
134 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000135 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000136 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000137 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000138 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000139}
140
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000141static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
142 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000143 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000144 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000145 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000146
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000147 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000148}
149
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000150static QualType getFunctionOrMethodResultType(const Decl *D) {
151 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000152 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000153 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000154}
155
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000156static bool isFunctionOrMethodVariadic(const Decl *D) {
157 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000158 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000159 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000160 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000161 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000162 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000163 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000164 }
165}
166
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000167static bool isInstanceMethod(const Decl *D) {
168 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000169 return MethodDecl->isInstance();
170 return false;
171}
172
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000173static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000174 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000175 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000176 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000177
John McCall96fa4842010-05-17 21:00:27 +0000178 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
179 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000180 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000181
John McCall96fa4842010-05-17 21:00:27 +0000182 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000183
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000184 // FIXME: Should we walk the chain of classes?
185 return ClsName == &Ctx.Idents.get("NSString") ||
186 ClsName == &Ctx.Idents.get("NSMutableString");
187}
188
Daniel Dunbar980c6692008-09-26 03:32:58 +0000189static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000190 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000191 if (!PT)
192 return false;
193
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000194 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000195 if (!RT)
196 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000197
Daniel Dunbar980c6692008-09-26 03:32:58 +0000198 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000199 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000200 return false;
201
202 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
203}
204
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000205/// \brief Check if the attribute has exactly as many args as Num. May
206/// output an error.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000207static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
208 unsigned int Num) {
209 if (Attr.getNumArgs() != Num) {
210 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
211 return false;
212 }
213
214 return true;
215}
216
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000217
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000218/// \brief Check if the attribute has at least as many args as Num. May
219/// output an error.
220static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
221 unsigned int Num) {
222 if (Attr.getNumArgs() < Num) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000223 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
224 return false;
225 }
226
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000227 return true;
228}
229
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000230/// \brief Check if IdxExpr is a valid argument index for a function or
231/// instance method D. May output an error.
232///
233/// \returns true if IdxExpr is a valid index.
234static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
235 StringRef AttrName,
236 SourceLocation AttrLoc,
237 unsigned AttrArgNum,
238 const Expr *IdxExpr,
239 uint64_t &Idx)
240{
241 assert(isFunctionOrMethod(D) && hasFunctionProto(D));
242
243 // In C++ the implicit 'this' function parameter also counts.
244 // Parameters are counted from one.
245 const bool HasImplicitThisParam = isInstanceMethod(D);
246 const unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
247 const unsigned FirstIdx = 1;
248
249 llvm::APSInt IdxInt;
250 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
251 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
252 S.Diag(AttrLoc, diag::err_attribute_argument_n_not_int)
253 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
254 return false;
255 }
256
257 Idx = IdxInt.getLimitedValue();
258 if (Idx < FirstIdx || (!isFunctionOrMethodVariadic(D) && Idx > NumArgs)) {
259 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
260 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
261 return false;
262 }
263 Idx--; // Convert to zero-based.
264 if (HasImplicitThisParam) {
265 if (Idx == 0) {
266 S.Diag(AttrLoc,
267 diag::err_attribute_invalid_implicit_this_argument)
268 << AttrName << IdxExpr->getSourceRange();
269 return false;
270 }
271 --Idx;
272 }
273
274 return true;
275}
276
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000277///
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000278/// \brief Check if passed in Decl is a field or potentially shared global var
279/// \return true if the Decl is a field or potentially shared global variable
280///
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000281static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000282 if (isa<FieldDecl>(D))
283 return true;
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000284 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000285 return (vd->hasGlobalStorage() && !(vd->isThreadSpecified()));
286
287 return false;
288}
289
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000290/// \brief Check if the passed-in expression is of type int or bool.
291static bool isIntOrBool(Expr *Exp) {
292 QualType QT = Exp->getType();
293 return QT->isBooleanType() || QT->isIntegerType();
294}
295
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000296
297// Check to see if the type is a smart pointer of some kind. We assume
298// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000299static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
300 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
301 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikieff7d47a2012-12-19 00:45:41 +0000302 if (Res1.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000303 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000304
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000305 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
306 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikieff7d47a2012-12-19 00:45:41 +0000307 if (Res2.empty())
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000308 return false;
309
310 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000311}
312
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000313/// \brief Check if passed in Decl is a pointer type.
314/// Note that this function may produce an error message.
315/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000316static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
317 const AttributeList &Attr) {
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000318 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000319 QualType QT = vd->getType();
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000320 if (QT->isAnyPointerType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000321 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000322
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000323 if (const RecordType *RT = QT->getAs<RecordType>()) {
324 // If it's an incomplete type, it could be a smart pointer; skip it.
325 // (We don't want to force template instantiation if we can avoid it,
326 // since that would alter the order in which templates are instantiated.)
327 if (RT->isIncompleteType())
328 return true;
329
330 if (threadSafetyCheckIsSmartPointer(S, RT))
331 return true;
332 }
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000333
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000334 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000335 << Attr.getName()->getName() << QT;
336 } else {
337 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
338 << Attr.getName();
339 }
340 return false;
341}
342
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000343/// \brief Checks that the passed in QualType either is of RecordType or points
344/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000345static const RecordType *getRecordType(QualType QT) {
346 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000347 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000348
349 // Now check if we point to record type.
350 if (const PointerType *PT = QT->getAs<PointerType>())
351 return PT->getPointeeType()->getAs<RecordType>();
352
353 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000354}
355
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000356
Jordy Rose740b0c22012-05-08 03:27:22 +0000357static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
358 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000359 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
360 if (RT->getDecl()->getAttr<LockableAttr>())
361 return true;
362 return false;
363}
364
365
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000366/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000367/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000368static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
369 QualType Ty) {
370 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000371
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000372 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000373 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000374 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000375 << Attr.getName() << Ty.getAsString();
376 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000377 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000378
Michael Hana9171bc2012-08-03 17:40:43 +0000379 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000380 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000381 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000382
383 // Allow smart pointers to be used as lockable objects.
384 // FIXME -- Check the type that the smart pointer points to.
385 if (threadSafetyCheckIsSmartPointer(S, RT))
386 return;
387
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000388 // Check if the type is lockable.
389 RecordDecl *RD = RT->getDecl();
390 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000391 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000392
393 // Else check if any base classes are lockable.
394 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
395 CXXBasePaths BPaths(false, false);
396 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
397 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000398 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000399
400 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
401 << Attr.getName() << Ty.getAsString();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000402}
403
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000404/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000405/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000406/// \param Sidx The attribute argument index to start checking with.
407/// \param ParamIdxOk Whether an argument can be indexing into a function
408/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000409static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000410 const AttributeList &Attr,
411 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000412 int Sidx = 0,
413 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000414 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000415 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000416
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000417 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000418 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000419 Args.push_back(ArgExp);
420 continue;
421 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000422
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000423 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000424 if (StrLit->getLength() == 0 ||
425 StrLit->getString() == StringRef("*")) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000426 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000427 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000428 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000429 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000430 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000431
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000432 // We allow constant strings to be used as a placeholder for expressions
433 // that are not valid C++ syntax, but warn that they are ignored.
434 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
435 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000436 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000437 continue;
438 }
439
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000440 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000441
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000442 // A pointer to member expression of the form &MyClass::mu is treated
443 // specially -- we need to look at the type of the member.
444 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
445 if (UOp->getOpcode() == UO_AddrOf)
446 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
447 if (DRE->getDecl()->isCXXInstanceMember())
448 ArgTy = DRE->getDecl()->getType();
449
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000450 // First see if we can just cast to record type, or point to record type.
451 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000452
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000453 // Now check if we index into a record type function param.
454 if(!RT && ParamIdxOk) {
455 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000456 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
457 if(FD && IL) {
458 unsigned int NumParams = FD->getNumParams();
459 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000460 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
461 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
462 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000463 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
464 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000465 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000466 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000467 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000468 }
469 }
470
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000471 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000472
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000473 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000474 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000475}
476
Chris Lattner58418ff2008-06-29 00:16:31 +0000477//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000478// Attribute Implementations
479//===----------------------------------------------------------------------===//
480
Daniel Dunbar032db472008-07-31 22:40:48 +0000481// FIXME: All this manual attribute parsing code is gross. At the
482// least add some helper functions to check most argument patterns (#
483// and types of args).
484
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000485enum ThreadAttributeDeclKind {
486 ThreadExpectedFieldOrGlobalVar,
487 ThreadExpectedFunctionOrMethod,
488 ThreadExpectedClassOrStruct
489};
490
Michael Hana9171bc2012-08-03 17:40:43 +0000491static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000492 const AttributeList &Attr) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000493 assert(!Attr.isInvalid());
494
495 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Han3be3b442012-07-23 18:48:41 +0000496 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000497
498 // D must be either a member field or global (potentially shared) variable.
499 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000500 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
501 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000502 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000503 }
504
Michael Han3be3b442012-07-23 18:48:41 +0000505 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000506}
507
Michael Han3be3b442012-07-23 18:48:41 +0000508static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
509 if (!checkGuardedVarAttrCommon(S, D, Attr))
510 return;
Michael Hana9171bc2012-08-03 17:40:43 +0000511
Michael Han99315932013-01-24 16:46:58 +0000512 D->addAttr(::new (S.Context)
513 GuardedVarAttr(Attr.getRange(), S.Context,
514 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000515}
516
Michael Hana9171bc2012-08-03 17:40:43 +0000517static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han99315932013-01-24 16:46:58 +0000518 const AttributeList &Attr) {
Michael Han3be3b442012-07-23 18:48:41 +0000519 if (!checkGuardedVarAttrCommon(S, D, Attr))
520 return;
521
522 if (!threadSafetyCheckIsPointer(S, D, Attr))
523 return;
524
Michael Han99315932013-01-24 16:46:58 +0000525 D->addAttr(::new (S.Context)
526 PtGuardedVarAttr(Attr.getRange(), S.Context,
527 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000528}
529
Michael Hana9171bc2012-08-03 17:40:43 +0000530static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
531 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000532 Expr* &Arg) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000533 assert(!Attr.isInvalid());
534
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000535 if (!checkAttributeNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000536 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000537
538 // D must be either a member field or global (potentially shared) variable.
539 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000540 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
541 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000542 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000543 }
544
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000545 SmallVector<Expr*, 1> Args;
546 // check that all arguments are lockable objects
547 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
548 unsigned Size = Args.size();
549 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000550 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000551
Michael Han3be3b442012-07-23 18:48:41 +0000552 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000553
Michael Han3be3b442012-07-23 18:48:41 +0000554 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000555}
556
Michael Han3be3b442012-07-23 18:48:41 +0000557static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
558 Expr *Arg = 0;
559 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
560 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000561
Michael Han3be3b442012-07-23 18:48:41 +0000562 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
563}
564
Michael Hana9171bc2012-08-03 17:40:43 +0000565static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000566 const AttributeList &Attr) {
567 Expr *Arg = 0;
568 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
569 return;
570
571 if (!threadSafetyCheckIsPointer(S, D, Attr))
572 return;
573
574 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
575 S.Context, Arg));
576}
577
Michael Hana9171bc2012-08-03 17:40:43 +0000578static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000579 const AttributeList &Attr) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000580 assert(!Attr.isInvalid());
581
582 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Han3be3b442012-07-23 18:48:41 +0000583 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000584
Caitlin Sadowski086fb952011-09-16 00:35:54 +0000585 // FIXME: Lockable structs for C code.
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000586 if (!isa<CXXRecordDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000587 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
588 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Han3be3b442012-07-23 18:48:41 +0000589 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000590 }
591
Michael Han3be3b442012-07-23 18:48:41 +0000592 return true;
593}
594
595static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
596 if (!checkLockableAttrCommon(S, D, Attr))
597 return;
598
599 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
600}
601
Michael Hana9171bc2012-08-03 17:40:43 +0000602static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000603 const AttributeList &Attr) {
604 if (!checkLockableAttrCommon(S, D, Attr))
605 return;
606
Michael Han99315932013-01-24 16:46:58 +0000607 D->addAttr(::new (S.Context)
608 ScopedLockableAttr(Attr.getRange(), S.Context,
609 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000610}
611
612static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
613 const AttributeList &Attr) {
614 assert(!Attr.isInvalid());
615
616 if (!checkAttributeNumArgs(S, Attr, 0))
617 return;
618
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000619 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000620 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
621 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000622 return;
623 }
624
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000625 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000626 S.Context));
627}
628
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000629static void handleNoAddressSafetyAttr(Sema &S, Decl *D,
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000630 const AttributeList &Attr) {
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000631 assert(!Attr.isInvalid());
632
633 if (!checkAttributeNumArgs(S, Attr, 0))
634 return;
635
636 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
637 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
638 << Attr.getName() << ExpectedFunctionOrMethod;
639 return;
640 }
641
Michael Han99315932013-01-24 16:46:58 +0000642 D->addAttr(::new (S.Context)
643 NoAddressSafetyAnalysisAttr(Attr.getRange(), S.Context,
644 Attr.getAttributeSpellingListIndex()));
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000645}
646
Michael Hana9171bc2012-08-03 17:40:43 +0000647static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
648 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000649 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000650 assert(!Attr.isInvalid());
651
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000652 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000653 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000654
655 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000656 ValueDecl *VD = dyn_cast<ValueDecl>(D);
657 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000658 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
659 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000660 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000661 }
662
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000663 // Check that this attribute only applies to lockable types.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000664 QualType QT = VD->getType();
665 if (!QT->isDependentType()) {
666 const RecordType *RT = getRecordType(QT);
667 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000668 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000669 << Attr.getName();
670 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000671 }
672 }
673
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000674 // Check that all arguments are lockable objects.
675 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Han3be3b442012-07-23 18:48:41 +0000676 if (Args.size() == 0)
677 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000678
Michael Han3be3b442012-07-23 18:48:41 +0000679 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000680}
681
Michael Hana9171bc2012-08-03 17:40:43 +0000682static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000683 const AttributeList &Attr) {
684 SmallVector<Expr*, 1> Args;
685 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
686 return;
687
688 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000689 D->addAttr(::new (S.Context)
690 AcquiredAfterAttr(Attr.getRange(), S.Context,
691 StartArg, Args.size(),
692 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000693}
694
Michael Hana9171bc2012-08-03 17:40:43 +0000695static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000696 const AttributeList &Attr) {
697 SmallVector<Expr*, 1> Args;
698 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
699 return;
700
701 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000702 D->addAttr(::new (S.Context)
703 AcquiredBeforeAttr(Attr.getRange(), S.Context,
704 StartArg, Args.size(),
705 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000706}
707
Michael Hana9171bc2012-08-03 17:40:43 +0000708static bool checkLockFunAttrCommon(Sema &S, Decl *D,
709 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000710 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000711 assert(!Attr.isInvalid());
712
713 // zero or more arguments ok
714
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000715 // check that the attribute is applied to a function
716 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000717 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
718 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000719 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000720 }
721
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000722 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000723 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000724
Michael Han3be3b442012-07-23 18:48:41 +0000725 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000726}
727
Michael Hana9171bc2012-08-03 17:40:43 +0000728static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000729 const AttributeList &Attr) {
730 SmallVector<Expr*, 1> Args;
731 if (!checkLockFunAttrCommon(S, D, Attr, Args))
732 return;
733
734 unsigned Size = Args.size();
735 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000736 D->addAttr(::new (S.Context)
737 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
738 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000739}
740
Michael Hana9171bc2012-08-03 17:40:43 +0000741static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000742 const AttributeList &Attr) {
743 SmallVector<Expr*, 1> Args;
744 if (!checkLockFunAttrCommon(S, D, Attr, Args))
745 return;
746
747 unsigned Size = Args.size();
748 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000749 D->addAttr(::new (S.Context)
750 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
751 StartArg, Size,
752 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000753}
754
Michael Hana9171bc2012-08-03 17:40:43 +0000755static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
756 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000757 SmallVector<Expr*, 2> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000758 assert(!Attr.isInvalid());
759
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000760 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000761 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000762
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000763 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000764 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
765 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000766 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000767 }
768
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000769 if (!isIntOrBool(Attr.getArg(0))) {
770 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
Michael Han3be3b442012-07-23 18:48:41 +0000771 << Attr.getName();
772 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000773 }
774
775 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000776 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000777
Michael Han3be3b442012-07-23 18:48:41 +0000778 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000779}
780
Michael Hana9171bc2012-08-03 17:40:43 +0000781static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000782 const AttributeList &Attr) {
783 SmallVector<Expr*, 2> Args;
784 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
785 return;
786
787 unsigned Size = Args.size();
788 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000789 D->addAttr(::new (S.Context)
790 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
791 Attr.getArg(0), StartArg, Size,
792 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000793}
794
Michael Hana9171bc2012-08-03 17:40:43 +0000795static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000796 const AttributeList &Attr) {
797 SmallVector<Expr*, 2> Args;
798 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
799 return;
800
801 unsigned Size = Args.size();
802 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000803 D->addAttr(::new (S.Context)
804 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
805 Attr.getArg(0), StartArg, Size,
806 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000807}
808
Michael Hana9171bc2012-08-03 17:40:43 +0000809static bool checkLocksRequiredCommon(Sema &S, Decl *D,
810 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000811 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000812 assert(!Attr.isInvalid());
813
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000814 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000815 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000816
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000817 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000818 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
819 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000820 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000821 }
822
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000823 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000824 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Han3be3b442012-07-23 18:48:41 +0000825 if (Args.size() == 0)
826 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000827
Michael Han3be3b442012-07-23 18:48:41 +0000828 return true;
829}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000830
Michael Hana9171bc2012-08-03 17:40:43 +0000831static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000832 const AttributeList &Attr) {
833 SmallVector<Expr*, 1> Args;
834 if (!checkLocksRequiredCommon(S, D, Attr, Args))
835 return;
836
837 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000838 D->addAttr(::new (S.Context)
839 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
840 StartArg, Args.size(),
841 Attr.getAttributeSpellingListIndex()));
Michael Han3be3b442012-07-23 18:48:41 +0000842}
843
Michael Hana9171bc2012-08-03 17:40:43 +0000844static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000845 const AttributeList &Attr) {
846 SmallVector<Expr*, 1> Args;
847 if (!checkLocksRequiredCommon(S, D, Attr, Args))
848 return;
849
850 Expr **StartArg = &Args[0];
Michael Han99315932013-01-24 16:46:58 +0000851 D->addAttr(::new (S.Context)
852 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
853 StartArg, Args.size(),
854 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000855}
856
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000857static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000858 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000859 assert(!Attr.isInvalid());
860
861 // zero or more arguments ok
862
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000863 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000864 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
865 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000866 return;
867 }
868
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000869 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000870 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000871 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000872 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000873 Expr **StartArg = Size == 0 ? 0 : &Args[0];
874
Michael Han99315932013-01-24 16:46:58 +0000875 D->addAttr(::new (S.Context)
876 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
877 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000878}
879
880static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000881 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000882 assert(!Attr.isInvalid());
883
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000884 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000885 return;
886
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000887 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000888 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
889 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000890 return;
891 }
892
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000893 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000894 SmallVector<Expr*, 1> Args;
895 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
896 unsigned Size = Args.size();
897 if (Size == 0)
898 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000899
Michael Han99315932013-01-24 16:46:58 +0000900 D->addAttr(::new (S.Context)
901 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
902 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000903}
904
905static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000906 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000907 assert(!Attr.isInvalid());
908
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000909 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000910 return;
911
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000912 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000913 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
914 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000915 return;
916 }
917
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000918 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000919 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000920 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000921 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000922 if (Size == 0)
923 return;
924 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000925
Michael Han99315932013-01-24 16:46:58 +0000926 D->addAttr(::new (S.Context)
927 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
928 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000929}
930
931
Chandler Carruthedc2c642011-07-02 00:01:44 +0000932static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
933 const AttributeList &Attr) {
Richard Smith1f5a4322013-01-13 02:11:23 +0000934 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
935 if (TD == 0) {
936 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
937 // and type-ids.
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000938 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000939 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000940 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000941
Richard Smith1f5a4322013-01-13 02:11:23 +0000942 // Remember this typedef decl, we will need it later for diagnostics.
943 S.ExtVectorDecls.push_back(TD);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000944}
945
Chandler Carruthedc2c642011-07-02 00:01:44 +0000946static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000947 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000948 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000949 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000950
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000951 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000952 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000953 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000954 // If the alignment is less than or equal to 8 bits, the packed attribute
955 // has no effect.
Eli Friedmanc087c3f2012-11-07 00:35:20 +0000956 if (!FD->getType()->isDependentType() &&
957 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000958 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +0000959 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000960 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000961 else
Michael Han99315932013-01-24 16:46:58 +0000962 FD->addAttr(::new (S.Context)
963 PackedAttr(Attr.getRange(), S.Context,
964 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000965 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000966 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000967}
968
Chandler Carruthedc2c642011-07-02 00:01:44 +0000969static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman9ee2d0472012-10-12 23:29:20 +0000970 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han99315932013-01-24 16:46:58 +0000971 RD->addAttr(::new (S.Context)
972 MsStructAttr(Attr.getRange(), S.Context,
973 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +0000974 else
975 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
976}
977
Chandler Carruthedc2c642011-07-02 00:01:44 +0000978static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000979 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000980 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000981 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000982
Ted Kremenek1f672822010-02-18 03:08:58 +0000983 // The IBAction attributes only apply to instance methods.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000984 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek1f672822010-02-18 03:08:58 +0000985 if (MD->isInstanceMethod()) {
Michael Han99315932013-01-24 16:46:58 +0000986 D->addAttr(::new (S.Context)
987 IBActionAttr(Attr.getRange(), S.Context,
988 Attr.getAttributeSpellingListIndex()));
Ted Kremenek1f672822010-02-18 03:08:58 +0000989 return;
990 }
991
Ted Kremenekd68ec812011-02-04 06:54:16 +0000992 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek1f672822010-02-18 03:08:58 +0000993}
994
Ted Kremenek7fd17232011-09-29 07:02:25 +0000995static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
996 // The IBOutlet/IBOutletCollection attributes only apply to instance
997 // variables or properties of Objective-C classes. The outlet must also
998 // have an object reference type.
999 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1000 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001001 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001002 << Attr.getName() << VD->getType() << 0;
1003 return false;
1004 }
1005 }
1006 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1007 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001008 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001009 << Attr.getName() << PD->getType() << 1;
1010 return false;
1011 }
1012 }
1013 else {
1014 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1015 return false;
1016 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001017
Ted Kremenek7fd17232011-09-29 07:02:25 +00001018 return true;
1019}
1020
Chandler Carruthedc2c642011-07-02 00:01:44 +00001021static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek1f672822010-02-18 03:08:58 +00001022 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001023 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek1f672822010-02-18 03:08:58 +00001024 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001025
1026 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001027 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001028
Michael Han99315932013-01-24 16:46:58 +00001029 D->addAttr(::new (S.Context)
1030 IBOutletAttr(Attr.getRange(), S.Context,
1031 Attr.getAttributeSpellingListIndex()));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001032}
1033
Chandler Carruthedc2c642011-07-02 00:01:44 +00001034static void handleIBOutletCollection(Sema &S, Decl *D,
1035 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001036
1037 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001038 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001039 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1040 return;
1041 }
1042
Ted Kremenek7fd17232011-09-29 07:02:25 +00001043 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001044 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001045
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001046 IdentifierInfo *II = Attr.getParameterName();
1047 if (!II)
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001048 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian798f8322010-08-17 21:39:27 +00001049
John McCallba7bf592010-08-24 05:47:05 +00001050 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001051 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001052 if (!TypeRep) {
1053 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1054 return;
1055 }
John McCallba7bf592010-08-24 05:47:05 +00001056 QualType QT = TypeRep.get();
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001057 // Diagnose use of non-object type in iboutletcollection attribute.
1058 // FIXME. Gnu attribute extension ignores use of builtin types in
1059 // attributes. So, __attribute__((iboutletcollection(char))) will be
1060 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001061 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001062 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1063 return;
1064 }
Michael Han99315932013-01-24 16:46:58 +00001065 D->addAttr(::new (S.Context)
1066 IBOutletCollectionAttr(Attr.getRange(),S.Context,
1067 QT, Attr.getParameterLoc(),
1068 Attr.getAttributeSpellingListIndex()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001069}
1070
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001071static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001072 if (const RecordType *UT = T->getAsUnionType())
1073 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1074 RecordDecl *UD = UT->getDecl();
1075 for (RecordDecl::field_iterator it = UD->field_begin(),
1076 itend = UD->field_end(); it != itend; ++it) {
1077 QualType QT = it->getType();
1078 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1079 T = QT;
1080 return;
1081 }
1082 }
1083 }
1084}
1085
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001086static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopese881ce22012-06-18 16:39:04 +00001087 if (!isFunctionOrMethod(D)) {
1088 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1089 << "alloc_size" << ExpectedFunctionOrMethod;
1090 return;
1091 }
1092
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001093 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1094 return;
1095
1096 // In C++ the implicit 'this' function parameter also counts, and they are
1097 // counted from one.
1098 bool HasImplicitThisParam = isInstanceMethod(D);
1099 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
1100
1101 SmallVector<unsigned, 8> SizeArgs;
1102
1103 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1104 E = Attr.arg_end(); I!=E; ++I) {
1105 // The argument must be an integer constant expression.
1106 Expr *Ex = *I;
1107 llvm::APSInt ArgNum;
1108 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1109 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
1110 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1111 << "alloc_size" << Ex->getSourceRange();
1112 return;
1113 }
1114
1115 uint64_t x = ArgNum.getZExtValue();
1116
1117 if (x < 1 || x > NumArgs) {
1118 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1119 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
1120 return;
1121 }
1122
1123 --x;
1124 if (HasImplicitThisParam) {
1125 if (x == 0) {
1126 S.Diag(Attr.getLoc(),
1127 diag::err_attribute_invalid_implicit_this_argument)
1128 << "alloc_size" << Ex->getSourceRange();
1129 return;
1130 }
1131 --x;
1132 }
1133
1134 // check if the function argument is of an integer type
1135 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
1136 if (!T->isIntegerType()) {
1137 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1138 << "alloc_size" << Ex->getSourceRange();
1139 return;
1140 }
1141
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001142 SizeArgs.push_back(x);
1143 }
1144
1145 // check if the function returns a pointer
1146 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1147 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1148 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
1149 }
1150
Michael Han99315932013-01-24 16:46:58 +00001151 D->addAttr(::new (S.Context)
1152 AllocSizeAttr(Attr.getRange(), S.Context,
1153 SizeArgs.data(), SizeArgs.size(),
1154 Attr.getAttributeSpellingListIndex()));
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001155}
1156
Chandler Carruthedc2c642011-07-02 00:01:44 +00001157static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001158 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1159 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001160 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001161 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001162 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001163 return;
1164 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001165
Chandler Carruth743682b2010-11-16 08:35:43 +00001166 // In C++ the implicit 'this' function parameter also counts, and they are
1167 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001168 bool HasImplicitThisParam = isInstanceMethod(D);
Nick Lewyckye1121512013-01-24 01:12:16 +00001169 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001170
1171 // The nonnull attribute only applies to pointers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001172 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001173
Nick Lewyckye1121512013-01-24 01:12:16 +00001174 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1175 E = Attr.arg_end(); I != E; ++I) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001176 // The argument must be an integer constant expression.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001177 Expr *Ex = *I;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001178 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001179 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1180 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001181 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1182 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001183 return;
1184 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001185
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001186 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001187
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001188 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001189 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +00001190 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001191 return;
1192 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001193
Ted Kremenek5224e6a2008-07-21 22:09:15 +00001194 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +00001195 if (HasImplicitThisParam) {
1196 if (x == 0) {
1197 S.Diag(Attr.getLoc(),
1198 diag::err_attribute_invalid_implicit_this_argument)
1199 << "nonnull" << Ex->getSourceRange();
1200 return;
1201 }
1202 --x;
1203 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001204
1205 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001206 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001207 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001208
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001209 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001210 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +00001211 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +00001212 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001213 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001214 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001215
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001216 NonNullArgs.push_back(x);
1217 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001218
1219 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1220 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001221 if (NonNullArgs.empty()) {
Nick Lewyckye1121512013-01-24 01:12:16 +00001222 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1223 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001224 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001225 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewyckye1121512013-01-24 01:12:16 +00001226 NonNullArgs.push_back(i);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001227 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001228
Ted Kremenek22813f42010-10-21 18:49:36 +00001229 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001230 if (NonNullArgs.empty()) {
1231 // Warn the trivial case only if attribute is not coming from a
1232 // macro instantiation.
1233 if (Attr.getLoc().isFileID())
1234 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001235 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001236 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001237 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001238
Nick Lewyckye1121512013-01-24 01:12:16 +00001239 unsigned *start = &NonNullArgs[0];
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001240 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001241 llvm::array_pod_sort(start, start + size);
Michael Han99315932013-01-24 16:46:58 +00001242 D->addAttr(::new (S.Context)
1243 NonNullAttr(Attr.getRange(), S.Context, start, size,
1244 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001245}
1246
Chandler Carruthedc2c642011-07-02 00:01:44 +00001247static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001248 // This attribute must be applied to a function declaration.
1249 // The first argument to the attribute must be a string,
1250 // the name of the resource, for example "malloc".
1251 // The following arguments must be argument indexes, the arguments must be
1252 // of integer type for Returns, otherwise of pointer type.
1253 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001254 // after being held. free() should be __attribute((ownership_takes)), whereas
1255 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001256
1257 if (!AL.getParameterName()) {
1258 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1259 << AL.getName()->getName() << 1;
1260 return;
1261 }
1262 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001263 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001264 switch (AL.getKind()) {
1265 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001266 K = OwnershipAttr::Takes;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001267 if (AL.getNumArgs() < 1) {
1268 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1269 return;
1270 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001271 break;
1272 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001273 K = OwnershipAttr::Holds;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001274 if (AL.getNumArgs() < 1) {
1275 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1276 return;
1277 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001278 break;
1279 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001280 K = OwnershipAttr::Returns;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001281 if (AL.getNumArgs() > 1) {
1282 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1283 << AL.getNumArgs() + 1;
1284 return;
1285 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001286 break;
1287 default:
1288 // This should never happen given how we are called.
1289 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +00001290 }
1291
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001292 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001293 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1294 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001295 return;
1296 }
1297
Chandler Carruth743682b2010-11-16 08:35:43 +00001298 // In C++ the implicit 'this' function parameter also counts, and they are
1299 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001300 bool HasImplicitThisParam = isInstanceMethod(D);
1301 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001302
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001303 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001304
1305 // Normalize the argument, __foo__ becomes foo.
1306 if (Module.startswith("__") && Module.endswith("__"))
1307 Module = Module.substr(2, Module.size() - 4);
1308
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001309 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001310
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001311 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1312 ++I) {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001313
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001314 Expr *IdxExpr = *I;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001315 llvm::APSInt ArgNum(32);
1316 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1317 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1318 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1319 << AL.getName()->getName() << IdxExpr->getSourceRange();
1320 continue;
1321 }
1322
1323 unsigned x = (unsigned) ArgNum.getZExtValue();
1324
1325 if (x > NumArgs || x < 1) {
1326 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1327 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1328 continue;
1329 }
1330 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +00001331 if (HasImplicitThisParam) {
1332 if (x == 0) {
1333 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1334 << "ownership" << IdxExpr->getSourceRange();
1335 return;
1336 }
1337 --x;
1338 }
1339
Ted Kremenekd21139a2010-07-31 01:52:11 +00001340 switch (K) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001341 case OwnershipAttr::Takes:
1342 case OwnershipAttr::Holds: {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001343 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001344 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001345 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1346 // FIXME: Should also highlight argument in decl.
1347 S.Diag(AL.getLoc(), diag::err_ownership_type)
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001348 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekd21139a2010-07-31 01:52:11 +00001349 << "pointer"
1350 << IdxExpr->getSourceRange();
1351 continue;
1352 }
1353 break;
1354 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001355 case OwnershipAttr::Returns: {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001356 if (AL.getNumArgs() > 1) {
1357 // Is the function argument an integer type?
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001358 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001359 llvm::APSInt ArgNum(32);
1360 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1361 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1362 S.Diag(AL.getLoc(), diag::err_ownership_type)
1363 << "ownership_returns" << "integer"
1364 << IdxExpr->getSourceRange();
1365 return;
1366 }
1367 }
1368 break;
1369 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001370 } // switch
1371
1372 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001373 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001374 i = D->specific_attr_begin<OwnershipAttr>(),
1375 e = D->specific_attr_end<OwnershipAttr>();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001376 i != e; ++i) {
1377 if ((*i)->getOwnKind() != K) {
1378 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1379 I!=E; ++I) {
1380 if (x == *I) {
1381 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1382 << AL.getName()->getName() << "ownership_*";
Ted Kremenekd21139a2010-07-31 01:52:11 +00001383 }
1384 }
1385 }
1386 }
1387 OwnershipArgs.push_back(x);
1388 }
1389
1390 unsigned* start = OwnershipArgs.data();
1391 unsigned size = OwnershipArgs.size();
1392 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001393
1394 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1395 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1396 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001397 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001398
Michael Han99315932013-01-24 16:46:58 +00001399 D->addAttr(::new (S.Context)
1400 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1401 AL.getAttributeSpellingListIndex()));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001402}
1403
Chandler Carruthedc2c642011-07-02 00:01:44 +00001404static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001405 // Check the attribute arguments.
1406 if (Attr.getNumArgs() > 1) {
1407 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1408 return;
1409 }
1410
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001411 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall7a198ce2011-02-08 22:35:49 +00001412 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001413 << Attr.getName() << ExpectedVariableOrFunction;
John McCall7a198ce2011-02-08 22:35:49 +00001414 return;
1415 }
1416
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001417 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001418
Rafael Espindolac18086a2010-02-23 22:00:30 +00001419 // gcc rejects
1420 // class c {
1421 // static int a __attribute__((weakref ("v2")));
1422 // static int b() __attribute__((weakref ("f3")));
1423 // };
1424 // and ignores the attributes of
1425 // void f(void) {
1426 // static int a __attribute__((weakref ("v2")));
1427 // }
1428 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001429 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001430 if (!Ctx->isFileContext()) {
1431 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +00001432 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +00001433 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001434 }
1435
1436 // The GCC manual says
1437 //
1438 // At present, a declaration to which `weakref' is attached can only
1439 // be `static'.
1440 //
1441 // It also says
1442 //
1443 // Without a TARGET,
1444 // given as an argument to `weakref' or to `alias', `weakref' is
1445 // equivalent to `weak'.
1446 //
1447 // gcc 4.4.1 will accept
1448 // int a7 __attribute__((weakref));
1449 // as
1450 // int a7 __attribute__((weak));
1451 // This looks like a bug in gcc. We reject that for now. We should revisit
1452 // it if this behaviour is actually used.
1453
Rafael Espindolac18086a2010-02-23 22:00:30 +00001454 // GCC rejects
1455 // static ((alias ("y"), weakref)).
1456 // Should we? How to check that weakref is before or after alias?
1457
1458 if (Attr.getNumArgs() == 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001459 Expr *Arg = Attr.getArg(0);
Rafael Espindolac18086a2010-02-23 22:00:30 +00001460 Arg = Arg->IgnoreParenCasts();
1461 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1462
Douglas Gregorfb65e592011-07-27 05:40:30 +00001463 if (!Str || !Str->isAscii()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001464 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1465 << "weakref" << 1;
1466 return;
1467 }
1468 // GCC will accept anything as the argument of weakref. Should we
1469 // check for an existing decl?
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001470 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001471 Str->getString()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001472 }
1473
Michael Han99315932013-01-24 16:46:58 +00001474 D->addAttr(::new (S.Context)
1475 WeakRefAttr(Attr.getRange(), S.Context,
1476 Attr.getAttributeSpellingListIndex()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001477}
1478
Chandler Carruthedc2c642011-07-02 00:01:44 +00001479static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001480 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001481 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001482 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001483 return;
1484 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001485
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001486 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001487 Arg = Arg->IgnoreParenCasts();
1488 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001489
Douglas Gregorfb65e592011-07-27 05:40:30 +00001490 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001491 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001492 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001493 return;
1494 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001495
Douglas Gregore8bbc122011-09-02 00:18:52 +00001496 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001497 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1498 return;
1499 }
1500
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001501 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001502
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001503 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00001504 Str->getString(),
1505 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001506}
1507
Quentin Colombet4e172062012-11-01 23:55:47 +00001508static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1509 // Check the attribute arguments.
1510 if (!checkAttributeNumArgs(S, Attr, 0))
1511 return;
1512
1513 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1514 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1515 << Attr.getName() << ExpectedFunctionOrMethod;
1516 return;
1517 }
1518
Michael Han99315932013-01-24 16:46:58 +00001519 D->addAttr(::new (S.Context)
1520 MinSizeAttr(Attr.getRange(), S.Context,
1521 Attr.getAttributeSpellingListIndex()));
Quentin Colombet4e172062012-11-01 23:55:47 +00001522}
1523
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001524static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1525 // Check the attribute arguments.
1526 if (!checkAttributeNumArgs(S, Attr, 0))
1527 return;
1528
1529 if (!isa<FunctionDecl>(D)) {
1530 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1531 << Attr.getName() << ExpectedFunction;
1532 return;
1533 }
1534
1535 if (D->hasAttr<HotAttr>()) {
1536 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1537 << Attr.getName() << "hot";
1538 return;
1539 }
1540
Michael Han99315932013-01-24 16:46:58 +00001541 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1542 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001543}
1544
1545static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1546 // Check the attribute arguments.
1547 if (!checkAttributeNumArgs(S, Attr, 0))
1548 return;
1549
1550 if (!isa<FunctionDecl>(D)) {
1551 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1552 << Attr.getName() << ExpectedFunction;
1553 return;
1554 }
1555
1556 if (D->hasAttr<ColdAttr>()) {
1557 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1558 << Attr.getName() << "cold";
1559 return;
1560 }
1561
Michael Han99315932013-01-24 16:46:58 +00001562 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1563 Attr.getAttributeSpellingListIndex()));
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001564}
1565
Chandler Carruthedc2c642011-07-02 00:01:44 +00001566static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001567 // Check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001568 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar03a38442008-10-28 00:17:57 +00001569 return;
Anders Carlsson88097122009-02-19 19:16:48 +00001570
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001571 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001572 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001573 << Attr.getName() << ExpectedFunction;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001574 return;
1575 }
1576
Michael Han99315932013-01-24 16:46:58 +00001577 D->addAttr(::new (S.Context)
1578 NakedAttr(Attr.getRange(), S.Context,
1579 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001580}
1581
Chandler Carruthedc2c642011-07-02 00:01:44 +00001582static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1583 const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001584 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001585 if (Attr.hasParameterOrArguments()) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001586 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1587 return;
1588 }
1589
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001590 if (!isa<FunctionDecl>(D)) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001591 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001592 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00001593 return;
1594 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001595
Michael Han99315932013-01-24 16:46:58 +00001596 D->addAttr(::new (S.Context)
1597 AlwaysInlineAttr(Attr.getRange(), S.Context,
1598 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar03a38442008-10-28 00:17:57 +00001599}
1600
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001601static void handleTLSModelAttr(Sema &S, Decl *D,
1602 const AttributeList &Attr) {
1603 // Check the attribute arguments.
1604 if (Attr.getNumArgs() != 1) {
1605 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1606 return;
1607 }
1608
1609 Expr *Arg = Attr.getArg(0);
1610 Arg = Arg->IgnoreParenCasts();
1611 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1612
1613 // Check that it is a string.
1614 if (!Str) {
1615 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1616 return;
1617 }
1618
1619 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) {
1620 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1621 << Attr.getName() << ExpectedTLSVar;
1622 return;
1623 }
1624
1625 // Check that the value.
1626 StringRef Model = Str->getString();
1627 if (Model != "global-dynamic" && Model != "local-dynamic"
1628 && Model != "initial-exec" && Model != "local-exec") {
1629 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1630 return;
1631 }
1632
Michael Han99315932013-01-24 16:46:58 +00001633 D->addAttr(::new (S.Context)
1634 TLSModelAttr(Attr.getRange(), S.Context, Model,
1635 Attr.getAttributeSpellingListIndex()));
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001636}
1637
Chandler Carruthedc2c642011-07-02 00:01:44 +00001638static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001639 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001640 if (Attr.hasParameterOrArguments()) {
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001641 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1642 return;
1643 }
Mike Stump11289f42009-09-09 15:08:12 +00001644
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001645 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001646 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001647 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han99315932013-01-24 16:46:58 +00001648 D->addAttr(::new (S.Context)
1649 MallocAttr(Attr.getRange(), S.Context,
1650 Attr.getAttributeSpellingListIndex()));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001651 return;
1652 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001653 }
1654
Ted Kremenek08479ae2009-08-15 00:51:46 +00001655 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001656}
1657
Chandler Carruthedc2c642011-07-02 00:01:44 +00001658static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001659 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001660 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001661 return;
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001662
Michael Han99315932013-01-24 16:46:58 +00001663 D->addAttr(::new (S.Context)
1664 MayAliasAttr(Attr.getRange(), S.Context,
1665 Attr.getAttributeSpellingListIndex()));
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001666}
1667
Chandler Carruthedc2c642011-07-02 00:01:44 +00001668static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001669 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001670 if (isa<VarDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001671 D->addAttr(::new (S.Context)
1672 NoCommonAttr(Attr.getRange(), S.Context,
1673 Attr.getAttributeSpellingListIndex()));
Eric Christopher515d87f2010-12-03 06:58:14 +00001674 else
1675 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001676 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001677}
1678
Chandler Carruthedc2c642011-07-02 00:01:44 +00001679static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001680 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001681 if (isa<VarDecl>(D))
Michael Han99315932013-01-24 16:46:58 +00001682 D->addAttr(::new (S.Context)
1683 CommonAttr(Attr.getRange(), S.Context,
1684 Attr.getAttributeSpellingListIndex()));
Eric Christopher515d87f2010-12-03 06:58:14 +00001685 else
1686 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001687 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001688}
1689
Chandler Carruthedc2c642011-07-02 00:01:44 +00001690static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001691 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001692
1693 if (S.CheckNoReturnAttr(attr)) return;
1694
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001695 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001696 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001697 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001698 return;
1699 }
1700
Michael Han99315932013-01-24 16:46:58 +00001701 D->addAttr(::new (S.Context)
1702 NoReturnAttr(attr.getRange(), S.Context,
1703 attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00001704}
1705
1706bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek1551d552011-04-15 05:49:29 +00001707 if (attr.hasParameterOrArguments()) {
John McCall3882ace2011-01-05 12:14:39 +00001708 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1709 attr.setInvalid();
1710 return true;
1711 }
1712
1713 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001714}
1715
Chandler Carruthedc2c642011-07-02 00:01:44 +00001716static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1717 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001718
1719 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1720 // because 'analyzer_noreturn' does not impact the type.
1721
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001722 if(!checkAttributeNumArgs(S, Attr, 0))
1723 return;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001724
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001725 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1726 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001727 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1728 && !VD->getType()->isFunctionPointerType())) {
1729 S.Diag(Attr.getLoc(),
Richard Smith89645bc2013-01-02 12:01:23 +00001730 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenek5295ce82010-08-19 00:51:58 +00001731 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001732 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001733 return;
1734 }
1735 }
1736
Michael Han99315932013-01-24 16:46:58 +00001737 D->addAttr(::new (S.Context)
1738 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1739 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001740}
1741
Richard Smith10876ef2013-01-17 01:30:42 +00001742static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1743 const AttributeList &Attr) {
1744 // C++11 [dcl.attr.noreturn]p1:
1745 // The attribute may be applied to the declarator-id in a function
1746 // declaration.
1747 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1748 if (!FD) {
1749 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1750 << Attr.getName() << ExpectedFunctionOrMethod;
1751 return;
1752 }
1753
Michael Han99315932013-01-24 16:46:58 +00001754 D->addAttr(::new (S.Context)
1755 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1756 Attr.getAttributeSpellingListIndex()));
Richard Smith10876ef2013-01-17 01:30:42 +00001757}
1758
John Thompsoncdb847ba2010-08-09 21:53:52 +00001759// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001760static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001761/*
1762 Returning a Vector Class in Registers
1763
Eric Christopherbc638a82010-12-01 22:13:54 +00001764 According to the PPU ABI specifications, a class with a single member of
1765 vector type is returned in memory when used as the return value of a function.
1766 This results in inefficient code when implementing vector classes. To return
1767 the value in a single vector register, add the vecreturn attribute to the
1768 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001769
1770 Example:
1771
1772 struct Vector
1773 {
1774 __vector float xyzw;
1775 } __attribute__((vecreturn));
1776
1777 Vector Add(Vector lhs, Vector rhs)
1778 {
1779 Vector result;
1780 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1781 return result; // This will be returned in a register
1782 }
1783*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001784 if (!isa<RecordDecl>(D)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001785 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001786 << Attr.getName() << ExpectedClass;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001787 return;
1788 }
1789
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001790 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001791 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1792 return;
1793 }
1794
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001795 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001796 int count = 0;
1797
1798 if (!isa<CXXRecordDecl>(record)) {
1799 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1800 return;
1801 }
1802
1803 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1804 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1805 return;
1806 }
1807
Eric Christopherbc638a82010-12-01 22:13:54 +00001808 for (RecordDecl::field_iterator iter = record->field_begin();
1809 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001810 if ((count == 1) || !iter->getType()->isVectorType()) {
1811 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1812 return;
1813 }
1814 count++;
1815 }
1816
Michael Han99315932013-01-24 16:46:58 +00001817 D->addAttr(::new (S.Context)
1818 VecReturnAttr(Attr.getRange(), S.Context,
1819 Attr.getAttributeSpellingListIndex()));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001820}
1821
Richard Smithe233fbf2013-01-28 22:42:45 +00001822static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1823 const AttributeList &Attr) {
1824 if (isa<ParmVarDecl>(D)) {
1825 // [[carries_dependency]] can only be applied to a parameter if it is a
1826 // parameter of a function declaration or lambda.
1827 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1828 S.Diag(Attr.getLoc(),
1829 diag::err_carries_dependency_param_not_function_decl);
1830 return;
1831 }
1832 } else if (!isa<FunctionDecl>(D)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001833 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001834 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001835 return;
1836 }
Richard Smithe233fbf2013-01-28 22:42:45 +00001837
1838 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1839 Attr.getRange(), S.Context,
1840 Attr.getAttributeSpellingListIndex()));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001841}
1842
Chandler Carruthedc2c642011-07-02 00:01:44 +00001843static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek39c59a82008-07-25 04:39:19 +00001844 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001845 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001846 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001847 return;
1848 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001849
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001850 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00001851 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001852 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001853 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001854 return;
1855 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001856
Michael Han99315932013-01-24 16:46:58 +00001857 D->addAttr(::new (S.Context)
1858 UnusedAttr(Attr.getRange(), S.Context,
1859 Attr.getAttributeSpellingListIndex()));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001860}
1861
Rafael Espindola70107f92011-10-03 14:59:42 +00001862static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1863 const AttributeList &Attr) {
1864 // check the attribute arguments.
1865 if (Attr.hasParameterOrArguments()) {
1866 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1867 return;
1868 }
1869
1870 if (!isa<FunctionDecl>(D)) {
1871 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1872 << Attr.getName() << ExpectedFunction;
1873 return;
1874 }
1875
Michael Han99315932013-01-24 16:46:58 +00001876 D->addAttr(::new (S.Context)
1877 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1878 Attr.getAttributeSpellingListIndex()));
Rafael Espindola70107f92011-10-03 14:59:42 +00001879}
1880
Chandler Carruthedc2c642011-07-02 00:01:44 +00001881static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001882 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001883 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001884 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1885 return;
1886 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001887
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001888 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +00001889 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001890 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1891 return;
1892 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001893 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001894 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001895 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001896 return;
1897 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001898
Michael Han99315932013-01-24 16:46:58 +00001899 D->addAttr(::new (S.Context)
1900 UsedAttr(Attr.getRange(), S.Context,
1901 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001902}
1903
Chandler Carruthedc2c642011-07-02 00:01:44 +00001904static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001905 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001906 if (Attr.getNumArgs() > 1) {
1907 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001908 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001909 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001910
1911 int priority = 65535; // FIXME: Do not hardcode such constants.
1912 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001913 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001914 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001915 if (E->isTypeDependent() || E->isValueDependent() ||
1916 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001917 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001918 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001919 return;
1920 }
1921 priority = Idx.getZExtValue();
1922 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001923
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001924 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001925 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001926 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001927 return;
1928 }
1929
Michael Han99315932013-01-24 16:46:58 +00001930 D->addAttr(::new (S.Context)
1931 ConstructorAttr(Attr.getRange(), S.Context, priority,
1932 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001933}
1934
Chandler Carruthedc2c642011-07-02 00:01:44 +00001935static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001936 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001937 if (Attr.getNumArgs() > 1) {
1938 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001939 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001940 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001941
1942 int priority = 65535; // FIXME: Do not hardcode such constants.
1943 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001944 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001945 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001946 if (E->isTypeDependent() || E->isValueDependent() ||
1947 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001948 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001949 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001950 return;
1951 }
1952 priority = Idx.getZExtValue();
1953 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001954
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001955 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001956 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001957 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001958 return;
1959 }
1960
Michael Han99315932013-01-24 16:46:58 +00001961 D->addAttr(::new (S.Context)
1962 DestructorAttr(Attr.getRange(), S.Context, priority,
1963 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar032db472008-07-31 22:40:48 +00001964}
1965
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001966template <typename AttrTy>
1967static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr,
1968 const char *Name) {
Chris Lattner190aa102011-02-24 05:42:24 +00001969 unsigned NumArgs = Attr.getNumArgs();
1970 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001971 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001972 return;
1973 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001974
1975 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001976 StringRef Str;
Chris Lattner190aa102011-02-24 05:42:24 +00001977 if (NumArgs == 1) {
1978 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanian551063102010-10-06 21:18:44 +00001979 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00001980 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001981 << Name;
Fariborz Jahanian551063102010-10-06 21:18:44 +00001982 return;
1983 }
Chris Lattner190aa102011-02-24 05:42:24 +00001984 Str = SE->getString();
Fariborz Jahanian551063102010-10-06 21:18:44 +00001985 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001986
Michael Han99315932013-01-24 16:46:58 +00001987 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
1988 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001989}
1990
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00001991static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1992 const AttributeList &Attr) {
1993 unsigned NumArgs = Attr.getNumArgs();
1994 if (NumArgs > 0) {
1995 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1996 return;
1997 }
1998
Michael Han99315932013-01-24 16:46:58 +00001999 D->addAttr(::new (S.Context)
2000 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2001 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00002002}
2003
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002004static void handleObjCRootClassAttr(Sema &S, Decl *D,
2005 const AttributeList &Attr) {
2006 if (!isa<ObjCInterfaceDecl>(D)) {
2007 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2008 return;
2009 }
2010
2011 unsigned NumArgs = Attr.getNumArgs();
2012 if (NumArgs > 0) {
2013 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2014 return;
2015 }
2016
Michael Han99315932013-01-24 16:46:58 +00002017 D->addAttr(::new (S.Context)
2018 ObjCRootClassAttr(Attr.getRange(), S.Context,
2019 Attr.getAttributeSpellingListIndex()));
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002020}
2021
Michael Han99315932013-01-24 16:46:58 +00002022static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2023 const AttributeList &Attr) {
Fariborz Jahanian7249e362012-01-03 22:52:32 +00002024 if (!isa<ObjCInterfaceDecl>(D)) {
2025 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2026 return;
2027 }
2028
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00002029 unsigned NumArgs = Attr.getNumArgs();
2030 if (NumArgs > 0) {
2031 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
2032 return;
2033 }
2034
Michael Han99315932013-01-24 16:46:58 +00002035 D->addAttr(::new (S.Context)
2036 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2037 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00002038}
2039
Jordy Rose740b0c22012-05-08 03:27:22 +00002040static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2041 IdentifierInfo *Platform,
2042 VersionTuple Introduced,
2043 VersionTuple Deprecated,
2044 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002045 StringRef PlatformName
2046 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2047 if (PlatformName.empty())
2048 PlatformName = Platform->getName();
2049
2050 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2051 // of these steps are needed).
2052 if (!Introduced.empty() && !Deprecated.empty() &&
2053 !(Introduced <= Deprecated)) {
2054 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2055 << 1 << PlatformName << Deprecated.getAsString()
2056 << 0 << Introduced.getAsString();
2057 return true;
2058 }
2059
2060 if (!Introduced.empty() && !Obsoleted.empty() &&
2061 !(Introduced <= Obsoleted)) {
2062 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2063 << 2 << PlatformName << Obsoleted.getAsString()
2064 << 0 << Introduced.getAsString();
2065 return true;
2066 }
2067
2068 if (!Deprecated.empty() && !Obsoleted.empty() &&
2069 !(Deprecated <= Obsoleted)) {
2070 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2071 << 2 << PlatformName << Obsoleted.getAsString()
2072 << 1 << Deprecated.getAsString();
2073 return true;
2074 }
2075
2076 return false;
2077}
2078
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002079/// \brief Check whether the two versions match.
2080///
2081/// If either version tuple is empty, then they are assumed to match. If
2082/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2083static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2084 bool BeforeIsOkay) {
2085 if (X.empty() || Y.empty())
2086 return true;
2087
2088 if (X == Y)
2089 return true;
2090
2091 if (BeforeIsOkay && X < Y)
2092 return true;
2093
2094 return false;
2095}
2096
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002097AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002098 IdentifierInfo *Platform,
2099 VersionTuple Introduced,
2100 VersionTuple Deprecated,
2101 VersionTuple Obsoleted,
2102 bool IsUnavailable,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002103 StringRef Message,
Michael Han99315932013-01-24 16:46:58 +00002104 bool Override,
2105 unsigned AttrSpellingListIndex) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002106 VersionTuple MergedIntroduced = Introduced;
2107 VersionTuple MergedDeprecated = Deprecated;
2108 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002109 bool FoundAny = false;
2110
Rafael Espindolac67f2232012-05-10 02:50:16 +00002111 if (D->hasAttrs()) {
2112 AttrVec &Attrs = D->getAttrs();
2113 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2114 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2115 if (!OldAA) {
2116 ++i;
2117 continue;
2118 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002119
Rafael Espindolac67f2232012-05-10 02:50:16 +00002120 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2121 if (OldPlatform != Platform) {
2122 ++i;
2123 continue;
2124 }
2125
2126 FoundAny = true;
2127 VersionTuple OldIntroduced = OldAA->getIntroduced();
2128 VersionTuple OldDeprecated = OldAA->getDeprecated();
2129 VersionTuple OldObsoleted = OldAA->getObsoleted();
2130 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindolac67f2232012-05-10 02:50:16 +00002131
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002132 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2133 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2134 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2135 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor43dc0c72013-01-16 00:54:48 +00002136 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002137 if (Override) {
2138 int Which = -1;
2139 VersionTuple FirstVersion;
2140 VersionTuple SecondVersion;
2141 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2142 Which = 0;
2143 FirstVersion = OldIntroduced;
2144 SecondVersion = Introduced;
2145 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2146 Which = 1;
2147 FirstVersion = Deprecated;
2148 SecondVersion = OldDeprecated;
2149 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2150 Which = 2;
2151 FirstVersion = Obsoleted;
2152 SecondVersion = OldObsoleted;
2153 }
2154
2155 if (Which == -1) {
2156 Diag(OldAA->getLocation(),
2157 diag::warn_mismatched_availability_override_unavail)
2158 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2159 } else {
2160 Diag(OldAA->getLocation(),
2161 diag::warn_mismatched_availability_override)
2162 << Which
2163 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2164 << FirstVersion.getAsString() << SecondVersion.getAsString();
2165 }
2166 Diag(Range.getBegin(), diag::note_overridden_method);
2167 } else {
2168 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2169 Diag(Range.getBegin(), diag::note_previous_attribute);
2170 }
2171
Rafael Espindolac67f2232012-05-10 02:50:16 +00002172 Attrs.erase(Attrs.begin() + i);
2173 --e;
2174 continue;
2175 }
2176
2177 VersionTuple MergedIntroduced2 = MergedIntroduced;
2178 VersionTuple MergedDeprecated2 = MergedDeprecated;
2179 VersionTuple MergedObsoleted2 = MergedObsoleted;
2180
2181 if (MergedIntroduced2.empty())
2182 MergedIntroduced2 = OldIntroduced;
2183 if (MergedDeprecated2.empty())
2184 MergedDeprecated2 = OldDeprecated;
2185 if (MergedObsoleted2.empty())
2186 MergedObsoleted2 = OldObsoleted;
2187
2188 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2189 MergedIntroduced2, MergedDeprecated2,
2190 MergedObsoleted2)) {
2191 Attrs.erase(Attrs.begin() + i);
2192 --e;
2193 continue;
2194 }
2195
2196 MergedIntroduced = MergedIntroduced2;
2197 MergedDeprecated = MergedDeprecated2;
2198 MergedObsoleted = MergedObsoleted2;
2199 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002200 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002201 }
2202
2203 if (FoundAny &&
2204 MergedIntroduced == Introduced &&
2205 MergedDeprecated == Deprecated &&
2206 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002207 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002208
Rafael Espindolac67f2232012-05-10 02:50:16 +00002209 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002210 MergedDeprecated, MergedObsoleted)) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002211 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2212 Introduced, Deprecated,
Michael Han99315932013-01-24 16:46:58 +00002213 Obsoleted, IsUnavailable, Message,
2214 AttrSpellingListIndex);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002215 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002216 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002217}
2218
Chandler Carruthedc2c642011-07-02 00:01:44 +00002219static void handleAvailabilityAttr(Sema &S, Decl *D,
2220 const AttributeList &Attr) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002221 IdentifierInfo *Platform = Attr.getParameterName();
2222 SourceLocation PlatformLoc = Attr.getParameterLoc();
Michael Han99315932013-01-24 16:46:58 +00002223 unsigned Index = Attr.getAttributeSpellingListIndex();
2224
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002225 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002226 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2227 << Platform;
2228
Rafael Espindolac231fab2013-01-08 21:30:32 +00002229 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2230 if (!ND) {
2231 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2232 return;
2233 }
2234
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002235 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2236 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2237 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002238 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002239 StringRef Str;
2240 const StringLiteral *SE =
2241 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2242 if (SE)
2243 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002244
Rafael Espindolaa3aea432013-01-08 22:04:34 +00002245 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(),
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002246 Platform,
2247 Introduced.Version,
2248 Deprecated.Version,
2249 Obsoleted.Version,
Douglas Gregor66a8ca02013-01-15 22:43:08 +00002250 IsUnavailable, Str,
Michael Han99315932013-01-24 16:46:58 +00002251 /*Override=*/false,
2252 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002253 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002254 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002255}
2256
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002257VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002258 VisibilityAttr::VisibilityType Vis,
2259 unsigned AttrSpellingListIndex) {
Rafael Espindolaa6b3cd42012-05-10 03:01:34 +00002260 if (isa<TypedefNameDecl>(D)) {
2261 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility";
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002262 return NULL;
Rafael Espindolaa6b3cd42012-05-10 03:01:34 +00002263 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002264 VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>();
2265 if (ExistingAttr) {
2266 VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility();
2267 if (ExistingVis == Vis)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002268 return NULL;
Rafael Espindolac67f2232012-05-10 02:50:16 +00002269 Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility);
2270 Diag(Range.getBegin(), diag::note_previous_attribute);
2271 D->dropAttr<VisibilityAttr>();
2272 }
Michael Han99315932013-01-24 16:46:58 +00002273 return ::new (Context) VisibilityAttr(Range, Context, Vis,
2274 AttrSpellingListIndex);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002275}
2276
Chandler Carruthedc2c642011-07-02 00:01:44 +00002277static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002278 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002279 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002280 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002281
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002282 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002283 Arg = Arg->IgnoreParenCasts();
2284 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002285
Douglas Gregorfb65e592011-07-27 05:40:30 +00002286 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002287 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002288 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002289 return;
2290 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002291
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002292 StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002293 VisibilityAttr::VisibilityType type;
Michael Han99315932013-01-24 16:46:58 +00002294
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002295 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002296 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002297 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002298 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002299 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002300 type = VisibilityAttr::Hidden; // FIXME
John McCalleed64c72012-01-29 01:20:30 +00002301 else if (TypeStr == "protected") {
2302 // Complain about attempts to use protected visibility on targets
2303 // (like Darwin) that don't support it.
2304 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2305 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2306 type = VisibilityAttr::Default;
2307 } else {
2308 type = VisibilityAttr::Protected;
2309 }
2310 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00002311 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002312 return;
2313 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002314
Michael Han99315932013-01-24 16:46:58 +00002315 unsigned Index = Attr.getAttributeSpellingListIndex();
2316 VisibilityAttr *NewAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type,
2317 Index);
Rafael Espindola19de5612013-01-12 06:42:30 +00002318 if (NewAttr)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002319 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002320}
2321
Chandler Carruthedc2c642011-07-02 00:01:44 +00002322static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2323 const AttributeList &Attr) {
John McCall86bc21f2011-03-02 11:33:24 +00002324 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2325 if (!method) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002326 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002327 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00002328 return;
2329 }
2330
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002331 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2332 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2333 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCall86bc21f2011-03-02 11:33:24 +00002334 << "objc_method_family" << 1;
2335 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002336 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCall86bc21f2011-03-02 11:33:24 +00002337 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002338 Attr.setInvalid();
John McCall86bc21f2011-03-02 11:33:24 +00002339 return;
2340 }
2341
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002342 StringRef param = Attr.getParameterName()->getName();
John McCall86bc21f2011-03-02 11:33:24 +00002343 ObjCMethodFamilyAttr::FamilyKind family;
2344 if (param == "none")
2345 family = ObjCMethodFamilyAttr::OMF_None;
2346 else if (param == "alloc")
2347 family = ObjCMethodFamilyAttr::OMF_alloc;
2348 else if (param == "copy")
2349 family = ObjCMethodFamilyAttr::OMF_copy;
2350 else if (param == "init")
2351 family = ObjCMethodFamilyAttr::OMF_init;
2352 else if (param == "mutableCopy")
2353 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2354 else if (param == "new")
2355 family = ObjCMethodFamilyAttr::OMF_new;
2356 else {
2357 // Just warn and ignore it. This is future-proof against new
2358 // families being used in system headers.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002359 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCall86bc21f2011-03-02 11:33:24 +00002360 return;
2361 }
2362
John McCall31168b02011-06-15 23:02:42 +00002363 if (family == ObjCMethodFamilyAttr::OMF_init &&
2364 !method->getResultType()->isObjCObjectPointerType()) {
2365 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2366 << method->getResultType();
2367 // Ignore the attribute.
2368 return;
2369 }
2370
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002371 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCall31168b02011-06-15 23:02:42 +00002372 S.Context, family));
John McCall86bc21f2011-03-02 11:33:24 +00002373}
2374
Chandler Carruthedc2c642011-07-02 00:01:44 +00002375static void handleObjCExceptionAttr(Sema &S, Decl *D,
2376 const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002377 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner677a3582009-02-14 08:09:34 +00002378 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002379
Chris Lattner677a3582009-02-14 08:09:34 +00002380 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2381 if (OCI == 0) {
2382 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2383 return;
2384 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002385
Michael Han99315932013-01-24 16:46:58 +00002386 D->addAttr(::new (S.Context)
2387 ObjCExceptionAttr(Attr.getRange(), S.Context,
2388 Attr.getAttributeSpellingListIndex()));
Chris Lattner677a3582009-02-14 08:09:34 +00002389}
2390
Chandler Carruthedc2c642011-07-02 00:01:44 +00002391static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002392 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00002393 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002394 return;
2395 }
Richard Smithdda56e42011-04-15 14:24:37 +00002396 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002397 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002398 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002399 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2400 return;
2401 }
2402 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002403 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2404 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002405 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002406 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2407 return;
2408 }
2409 }
2410 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002411 // It is okay to include this attribute on properties, e.g.:
2412 //
2413 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2414 //
2415 // In this case it follows tradition and suppresses an error in the above
2416 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002417 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002418 }
Michael Han99315932013-01-24 16:46:58 +00002419 D->addAttr(::new (S.Context)
2420 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2421 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002422}
2423
Mike Stumpd3bb5572009-07-24 19:02:52 +00002424static void
Chandler Carruthedc2c642011-07-02 00:01:44 +00002425handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002426 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00002427 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002428 return;
2429 }
2430
2431 if (!isa<FunctionDecl>(D)) {
2432 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2433 return;
2434 }
2435
Michael Han99315932013-01-24 16:46:58 +00002436 D->addAttr(::new (S.Context)
2437 OverloadableAttr(Attr.getRange(), S.Context,
2438 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002439}
2440
Chandler Carruthedc2c642011-07-02 00:01:44 +00002441static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002442 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002443 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002444 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002445 return;
2446 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002447
Steve Naroff3405a732008-09-18 16:44:58 +00002448 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002449 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002450 return;
2451 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002452
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002453 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00002454 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00002455 type = BlocksAttr::ByRef;
2456 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002457 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002458 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00002459 return;
2460 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002461
Michael Han99315932013-01-24 16:46:58 +00002462 D->addAttr(::new (S.Context)
2463 BlocksAttr(Attr.getRange(), S.Context, type,
2464 Attr.getAttributeSpellingListIndex()));
Steve Naroff3405a732008-09-18 16:44:58 +00002465}
2466
Chandler Carruthedc2c642011-07-02 00:01:44 +00002467static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002468 // check the attribute arguments.
2469 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002470 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002471 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002472 }
2473
John McCallb46f2872011-09-09 07:56:05 +00002474 unsigned sentinel = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002475 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002476 Expr *E = Attr.getArg(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002477 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002478 if (E->isTypeDependent() || E->isValueDependent() ||
2479 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002480 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002481 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002482 return;
2483 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002484
John McCallb46f2872011-09-09 07:56:05 +00002485 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002486 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2487 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002488 return;
2489 }
John McCallb46f2872011-09-09 07:56:05 +00002490
2491 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002492 }
2493
John McCallb46f2872011-09-09 07:56:05 +00002494 unsigned nullPos = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002495 if (Attr.getNumArgs() > 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002496 Expr *E = Attr.getArg(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002497 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002498 if (E->isTypeDependent() || E->isValueDependent() ||
2499 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002500 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002501 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002502 return;
2503 }
2504 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002505
John McCallb46f2872011-09-09 07:56:05 +00002506 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002507 // FIXME: This error message could be improved, it would be nice
2508 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002509 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2510 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002511 return;
2512 }
2513 }
2514
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002515 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002516 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002517 if (isa<FunctionNoProtoType>(FT)) {
2518 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2519 return;
2520 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002521
Chris Lattner9363e312009-03-17 23:03:47 +00002522 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002523 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002524 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002525 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002526 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002527 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002528 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002529 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002530 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002531 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2532 if (!BD->isVariadic()) {
2533 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2534 return;
2535 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002536 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002537 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002538 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002539 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002540 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002541 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002542 int m = Ty->isFunctionPointerType() ? 0 : 1;
2543 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002544 return;
2545 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002546 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002547 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002548 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002549 return;
2550 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002551 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002552 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002553 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002554 return;
2555 }
Michael Han99315932013-01-24 16:46:58 +00002556 D->addAttr(::new (S.Context)
2557 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2558 Attr.getAttributeSpellingListIndex()));
Anders Carlssonc181b012008-10-05 18:05:59 +00002559}
2560
Chandler Carruthedc2c642011-07-02 00:01:44 +00002561static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner237f2752009-02-14 07:37:35 +00002562 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002563 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner237f2752009-02-14 07:37:35 +00002564 return;
Chris Lattner237f2752009-02-14 07:37:35 +00002565
Kaelyn Uhrain8681f9d2012-11-12 23:48:05 +00002566 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002567 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhrain3d699e02012-11-13 00:18:47 +00002568 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner237f2752009-02-14 07:37:35 +00002569 return;
2570 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002571
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002572 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2573 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2574 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002575 return;
2576 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002577 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2578 if (MD->getResultType()->isVoidType()) {
2579 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2580 << Attr.getName() << 1;
2581 return;
2582 }
2583
Michael Han99315932013-01-24 16:46:58 +00002584 D->addAttr(::new (S.Context)
2585 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2586 Attr.getAttributeSpellingListIndex()));
Chris Lattner237f2752009-02-14 07:37:35 +00002587}
2588
Chandler Carruthedc2c642011-07-02 00:01:44 +00002589static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002590 // check the attribute arguments.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002591 if (Attr.hasParameterOrArguments()) {
2592 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002593 return;
2594 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002595
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002596 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002597 if (isa<CXXRecordDecl>(D)) {
2598 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2599 return;
2600 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002601 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2602 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002603 return;
2604 }
2605
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002606 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002607
Michael Han99315932013-01-24 16:46:58 +00002608 nd->addAttr(::new (S.Context)
2609 WeakAttr(Attr.getRange(), S.Context,
2610 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002611}
2612
Chandler Carruthedc2c642011-07-02 00:01:44 +00002613static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002614 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002615 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002616 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002617
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002618
2619 // weak_import only applies to variable & function declarations.
2620 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002621 if (!D->canBeWeakImported(isDef)) {
2622 if (isDef)
2623 S.Diag(Attr.getLoc(),
2624 diag::warn_attribute_weak_import_invalid_on_definition)
2625 << "weak_import" << 2 /*variable and function*/;
Douglas Gregord71149a2011-03-23 13:27:51 +00002626 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002627 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002628 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002629 // Nothing to warn about here.
2630 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002631 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002632 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002633
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002634 return;
2635 }
2636
Michael Han99315932013-01-24 16:46:58 +00002637 D->addAttr(::new (S.Context)
2638 WeakImportAttr(Attr.getRange(), S.Context,
2639 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002640}
2641
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002642// Handles reqd_work_group_size and work_group_size_hint.
2643static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002644 const AttributeList &Attr) {
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002645 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2646 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2647
Nate Begemanf2758702009-06-26 06:32:41 +00002648 // Attribute has 3 arguments.
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002649 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begemanf2758702009-06-26 06:32:41 +00002650
2651 unsigned WGSize[3];
2652 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002653 Expr *E = Attr.getArg(i);
Nate Begemanf2758702009-06-26 06:32:41 +00002654 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002655 if (E->isTypeDependent() || E->isValueDependent() ||
2656 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00002657 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002658 << Attr.getName()->getName() << E->getSourceRange();
Nate Begemanf2758702009-06-26 06:32:41 +00002659 return;
2660 }
2661 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2662 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002663
2664 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2665 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2666 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2667 if (!(A->getXDim() == WGSize[0] &&
2668 A->getYDim() == WGSize[1] &&
2669 A->getZDim() == WGSize[2])) {
2670 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2671 Attr.getName();
2672 }
2673 }
2674
2675 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2676 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2677 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2678 if (!(A->getXDim() == WGSize[0] &&
2679 A->getYDim() == WGSize[1] &&
2680 A->getZDim() == WGSize[2])) {
2681 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2682 Attr.getName();
2683 }
2684 }
2685
2686 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2687 D->addAttr(::new (S.Context)
2688 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002689 WGSize[0], WGSize[1], WGSize[2],
2690 Attr.getAttributeSpellingListIndex()));
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002691 else
2692 D->addAttr(::new (S.Context)
2693 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han99315932013-01-24 16:46:58 +00002694 WGSize[0], WGSize[1], WGSize[2],
2695 Attr.getAttributeSpellingListIndex()));
Nate Begemanf2758702009-06-26 06:32:41 +00002696}
2697
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002698SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han99315932013-01-24 16:46:58 +00002699 StringRef Name,
2700 unsigned AttrSpellingListIndex) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002701 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2702 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002703 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002704 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2705 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002706 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002707 }
Michael Han99315932013-01-24 16:46:58 +00002708 return ::new (Context) SectionAttr(Range, Context, Name,
2709 AttrSpellingListIndex);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002710}
2711
Chandler Carruthedc2c642011-07-02 00:01:44 +00002712static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002713 // Attribute has no arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002714 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002715 return;
Daniel Dunbar648bf782009-02-12 17:28:23 +00002716
2717 // Make sure that there is a string literal as the sections's single
2718 // argument.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002719 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002720 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002721 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002722 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00002723 return;
2724 }
Mike Stump11289f42009-09-09 15:08:12 +00002725
Chris Lattner30ba6742009-08-10 19:03:04 +00002726 // If the target wants to validate the section specifier, make it happen.
Douglas Gregore8bbc122011-09-02 00:18:52 +00002727 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00002728 if (!Error.empty()) {
2729 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2730 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002731 return;
2732 }
Mike Stump11289f42009-09-09 15:08:12 +00002733
Chris Lattner20aee9b2010-01-12 20:58:53 +00002734 // This attribute cannot be applied to local variables.
2735 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2736 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2737 return;
2738 }
Michael Han99315932013-01-24 16:46:58 +00002739
2740 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002741 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han99315932013-01-24 16:46:58 +00002742 SE->getString(), Index);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002743 if (NewAttr)
2744 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002745}
2746
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002747
Chandler Carruthedc2c642011-07-02 00:01:44 +00002748static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002749 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002750 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002751 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002752 return;
2753 }
Douglas Gregor88336832011-06-15 05:45:11 +00002754
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002755 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002756 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002757 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002758 } else {
Michael Han99315932013-01-24 16:46:58 +00002759 D->addAttr(::new (S.Context)
2760 NoThrowAttr(Attr.getRange(), S.Context,
2761 Attr.getAttributeSpellingListIndex()));
Douglas Gregor88336832011-06-15 05:45:11 +00002762 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002763}
2764
Chandler Carruthedc2c642011-07-02 00:01:44 +00002765static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002766 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002767 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002768 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00002769 return;
2770 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002771
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002772 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002773 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002774 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002775 } else {
Michael Han99315932013-01-24 16:46:58 +00002776 D->addAttr(::new (S.Context)
2777 ConstAttr(Attr.getRange(), S.Context,
2778 Attr.getAttributeSpellingListIndex() ));
Douglas Gregor88336832011-06-15 05:45:11 +00002779 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002780}
2781
Chandler Carruthedc2c642011-07-02 00:01:44 +00002782static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002783 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002784 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssonb8316282008-10-05 23:32:53 +00002785 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002786
Michael Han99315932013-01-24 16:46:58 +00002787 D->addAttr(::new (S.Context)
2788 PureAttr(Attr.getRange(), S.Context,
2789 Attr.getAttributeSpellingListIndex()));
Anders Carlssonb8316282008-10-05 23:32:53 +00002790}
2791
Chandler Carruthedc2c642011-07-02 00:01:44 +00002792static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002793 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00002794 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2795 return;
2796 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002797
Anders Carlssond277d792009-01-31 01:16:18 +00002798 if (Attr.getNumArgs() != 0) {
2799 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2800 return;
2801 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002802
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002803 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002804
Anders Carlssond277d792009-01-31 01:16:18 +00002805 if (!VD || !VD->hasLocalStorage()) {
2806 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2807 return;
2808 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002809
Anders Carlssond277d792009-01-31 01:16:18 +00002810 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00002811 // FIXME: Lookup probably isn't looking in the right place
John McCall9f3059a2009-10-09 21:13:30 +00002812 NamedDecl *CleanupDecl
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002813 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2814 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00002815 if (!CleanupDecl) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002816 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00002817 Attr.getParameterName();
2818 return;
2819 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002820
Anders Carlssond277d792009-01-31 01:16:18 +00002821 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2822 if (!FD) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002823 S.Diag(Attr.getParameterLoc(),
2824 diag::err_attribute_cleanup_arg_not_function)
2825 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002826 return;
2827 }
2828
Anders Carlssond277d792009-01-31 01:16:18 +00002829 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002830 S.Diag(Attr.getParameterLoc(),
2831 diag::err_attribute_cleanup_func_must_take_one_arg)
2832 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002833 return;
2834 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002835
Anders Carlsson723f55d2009-02-07 23:16:50 +00002836 // We're currently more strict than GCC about what function types we accept.
2837 // If this ever proves to be a problem it should be easy to fix.
2838 QualType Ty = S.Context.getPointerType(VD->getType());
2839 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002840 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2841 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002842 S.Diag(Attr.getParameterLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00002843 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2844 Attr.getParameterName() << ParamTy << Ty;
2845 return;
2846 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002847
Michael Han99315932013-01-24 16:46:58 +00002848 D->addAttr(::new (S.Context)
2849 CleanupAttr(Attr.getRange(), S.Context, FD,
2850 Attr.getAttributeSpellingListIndex()));
Eli Friedmanfa0df832012-02-02 03:46:19 +00002851 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssond277d792009-01-31 01:16:18 +00002852}
2853
Mike Stumpd3bb5572009-07-24 19:02:52 +00002854/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendling44426052012-12-20 19:22:21 +00002855/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002856static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002857 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002858 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002859
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002860 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002861 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002862 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002863 return;
2864 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002865
2866 // In C++ the implicit 'this' function parameter also counts, and they are
2867 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002868 bool HasImplicitThisParam = isInstanceMethod(D);
2869 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002870 unsigned FirstIdx = 1;
Chandler Carruth743682b2010-11-16 08:35:43 +00002871
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002872 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002873 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002874 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002875 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2876 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002877 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2878 << "format" << 2 << IdxExpr->getSourceRange();
2879 return;
2880 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002881
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002882 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2883 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2884 << "format" << 2 << IdxExpr->getSourceRange();
2885 return;
2886 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002887
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002888 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002889
Chandler Carruth743682b2010-11-16 08:35:43 +00002890 if (HasImplicitThisParam) {
2891 if (ArgIdx == 0) {
2892 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2893 << "format_arg" << IdxExpr->getSourceRange();
2894 return;
2895 }
2896 ArgIdx--;
2897 }
2898
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002899 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002900 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002901
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002902 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2903 if (not_nsstring_type &&
2904 !isCFStringType(Ty, S.Context) &&
2905 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002906 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002907 // FIXME: Should highlight the actual expression that has the wrong type.
2908 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002909 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002910 << IdxExpr->getSourceRange();
2911 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002912 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002913 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002914 if (!isNSStringType(Ty, S.Context) &&
2915 !isCFStringType(Ty, S.Context) &&
2916 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002917 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002918 // FIXME: Should highlight the actual expression that has the wrong type.
2919 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002920 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002921 << IdxExpr->getSourceRange();
2922 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002923 }
2924
Michael Han99315932013-01-24 16:46:58 +00002925 D->addAttr(::new (S.Context)
2926 FormatArgAttr(Attr.getRange(), S.Context, Idx.getZExtValue(),
2927 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002928}
2929
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002930enum FormatAttrKind {
2931 CFStringFormat,
2932 NSStringFormat,
2933 StrftimeFormat,
2934 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002935 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002936 InvalidFormat
2937};
2938
2939/// getFormatAttrKind - Map from format attribute names to supported format
2940/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002941static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002942 return llvm::StringSwitch<FormatAttrKind>(Format)
2943 // Check for formats that get handled specially.
2944 .Case("NSString", NSStringFormat)
2945 .Case("CFString", CFStringFormat)
2946 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002947
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002948 // Otherwise, check for supported formats.
2949 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2950 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2951 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002952
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002953 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2954 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002955}
2956
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002957/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00002958/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002959static void handleInitPriorityAttr(Sema &S, Decl *D,
2960 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002961 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002962 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2963 return;
2964 }
2965
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002966 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002967 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2968 Attr.setInvalid();
2969 return;
2970 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002971 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002972 if (S.Context.getAsArrayType(T))
2973 T = S.Context.getBaseElementType(T);
2974 if (!T->getAs<RecordType>()) {
2975 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2976 Attr.setInvalid();
2977 return;
2978 }
2979
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002980 if (Attr.getNumArgs() != 1) {
2981 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2982 Attr.setInvalid();
2983 return;
2984 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002985 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002986
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002987 llvm::APSInt priority(32);
2988 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2989 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2990 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2991 << "init_priority" << priorityExpr->getSourceRange();
2992 Attr.setInvalid();
2993 return;
2994 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00002995 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002996 if (prioritynum < 101 || prioritynum > 65535) {
2997 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2998 << priorityExpr->getSourceRange();
2999 Attr.setInvalid();
3000 return;
3001 }
Michael Han99315932013-01-24 16:46:58 +00003002 D->addAttr(::new (S.Context)
3003 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3004 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003005}
3006
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003007FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
Michael Han99315932013-01-24 16:46:58 +00003008 int FormatIdx, int FirstArg,
3009 unsigned AttrSpellingListIndex) {
Rafael Espindola92d49452012-05-11 00:36:07 +00003010 // Check whether we already have an equivalent format attribute.
3011 for (specific_attr_iterator<FormatAttr>
3012 i = D->specific_attr_begin<FormatAttr>(),
3013 e = D->specific_attr_end<FormatAttr>();
3014 i != e ; ++i) {
3015 FormatAttr *f = *i;
3016 if (f->getType() == Format &&
3017 f->getFormatIdx() == FormatIdx &&
3018 f->getFirstArg() == FirstArg) {
3019 // If we don't have a valid location for this attribute, adopt the
3020 // location.
3021 if (f->getLocation().isInvalid())
3022 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003023 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00003024 }
3025 }
3026
Michael Han99315932013-01-24 16:46:58 +00003027 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg,
3028 AttrSpellingListIndex);
Rafael Espindola92d49452012-05-11 00:36:07 +00003029}
3030
Mike Stumpd3bb5572009-07-24 19:02:52 +00003031/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendling44426052012-12-20 19:22:21 +00003032/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00003033static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003034
Chris Lattner4a927cb2008-06-28 23:36:30 +00003035 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00003036 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003037 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003038 return;
3039 }
3040
Chris Lattner4a927cb2008-06-28 23:36:30 +00003041 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003042 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003043 return;
3044 }
3045
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003046 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003047 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003048 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003049 return;
3050 }
3051
Chandler Carruth743682b2010-11-16 08:35:43 +00003052 // In C++ the implicit 'this' function parameter also counts, and they are
3053 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003054 bool HasImplicitThisParam = isInstanceMethod(D);
3055 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003056 unsigned FirstIdx = 1;
3057
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003058 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003059
3060 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003061 if (Format.startswith("__") && Format.endswith("__"))
3062 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003063
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003064 // Check for supported formats.
3065 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00003066
3067 if (Kind == IgnoredFormat)
3068 return;
3069
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003070 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00003071 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00003072 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003073 return;
3074 }
3075
3076 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003077 Expr *IdxExpr = Attr.getArg(0);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003078 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003079 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3080 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003081 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003082 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003083 return;
3084 }
3085
3086 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003087 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003088 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003089 return;
3090 }
3091
3092 // FIXME: Do we need to bounds check?
3093 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003094
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003095 if (HasImplicitThisParam) {
3096 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00003097 S.Diag(Attr.getLoc(),
3098 diag::err_format_attribute_implicit_this_format_string)
3099 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00003100 return;
3101 }
3102 ArgIdx--;
3103 }
Mike Stump11289f42009-09-09 15:08:12 +00003104
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003105 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003106 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003107
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003108 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00003109 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003110 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3111 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00003112 return;
3113 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003114 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003115 // FIXME: do we need to check if the type is NSString*? What are the
3116 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003117 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003118 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003119 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3120 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003121 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003122 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003123 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003124 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003125 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00003126 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3127 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003128 return;
3129 }
3130
3131 // check the 3rd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003132 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003133 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003134 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3135 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003136 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003137 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003138 return;
3139 }
3140
3141 // check if the function is variadic if the 3rd argument non-zero
3142 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003143 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003144 ++NumArgs; // +1 for ...
3145 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003146 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003147 return;
3148 }
3149 }
3150
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003151 // strftime requires FirstArg to be 0 because it doesn't read from any
3152 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003153 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003154 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00003155 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3156 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003157 return;
3158 }
3159 // if 0 it disables parameter checking (to use with e.g. va_list)
3160 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003161 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003162 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003163 return;
3164 }
3165
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003166 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3167 Idx.getZExtValue(),
Michael Han99315932013-01-24 16:46:58 +00003168 FirstArg.getZExtValue(),
3169 Attr.getAttributeSpellingListIndex());
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003170 if (NewAttr)
3171 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003172}
3173
Chandler Carruthedc2c642011-07-02 00:01:44 +00003174static void handleTransparentUnionAttr(Sema &S, Decl *D,
3175 const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003176 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003177 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003178 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003179
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003180
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003181 // Try to find the underlying union declaration.
3182 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003183 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003184 if (TD && TD->getUnderlyingType()->isUnionType())
3185 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3186 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003187 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003188
3189 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00003190 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003191 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003192 return;
3193 }
3194
John McCallf937c022011-10-07 06:10:15 +00003195 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003196 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003197 diag::warn_transparent_union_attribute_not_definition);
3198 return;
3199 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003200
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003201 RecordDecl::field_iterator Field = RD->field_begin(),
3202 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003203 if (Field == FieldEnd) {
3204 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3205 return;
3206 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003207
David Blaikie40ed2972012-06-06 20:45:41 +00003208 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003209 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003210 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003211 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003212 diag::warn_transparent_union_attribute_floating)
3213 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003214 return;
3215 }
3216
3217 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3218 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3219 for (; Field != FieldEnd; ++Field) {
3220 QualType FieldType = Field->getType();
3221 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3222 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3223 // Warn if we drop the attribute.
3224 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003225 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003226 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003227 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003228 diag::warn_transparent_union_attribute_field_size_align)
3229 << isSize << Field->getDeclName() << FieldBits;
3230 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003231 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003232 diag::note_transparent_union_first_field_size_align)
3233 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003234 return;
3235 }
3236 }
3237
Michael Han99315932013-01-24 16:46:58 +00003238 RD->addAttr(::new (S.Context)
3239 TransparentUnionAttr(Attr.getRange(), S.Context,
3240 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003241}
3242
Chandler Carruthedc2c642011-07-02 00:01:44 +00003243static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003244 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003245 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003246 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003247
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003248 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00003249 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003250
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003251 // Make sure that there is a string literal as the annotation's single
3252 // argument.
3253 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00003254 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003255 return;
3256 }
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003257
3258 // Don't duplicate annotations that are already set.
3259 for (specific_attr_iterator<AnnotateAttr>
3260 i = D->specific_attr_begin<AnnotateAttr>(),
3261 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3262 if ((*i)->getAnnotation() == SE->getString())
3263 return;
3264 }
Michael Han99315932013-01-24 16:46:58 +00003265
3266 D->addAttr(::new (S.Context)
3267 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3268 Attr.getAttributeSpellingListIndex()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003269}
3270
Chandler Carruthedc2c642011-07-02 00:01:44 +00003271static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003272 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003273 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003274 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003275 return;
3276 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003277
Richard Smith848e1f12013-02-01 08:12:08 +00003278 // FIXME: The C++11 version of this attribute should error out when it is
3279 // used to specify a weaker alignment, rather than being silently
3280 // ignored. This constraint cannot be applied until we have seen
3281 // all the attributes which apply to the variable.
3282
3283 if (Attr.getNumArgs() == 0) {
3284 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3285 true, 0, Attr.getAttributeSpellingListIndex()));
3286 return;
3287 }
3288
3289 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0),
3290 Attr.getAttributeSpellingListIndex());
3291}
3292
3293void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
3294 unsigned SpellingListIndex) {
3295 // FIXME: Handle pack-expansions here.
3296 if (DiagnoseUnexpandedParameterPack(E))
3297 return;
3298
3299 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3300 SourceLocation AttrLoc = AttrRange.getBegin();
3301
Richard Smith1dba27c2013-01-29 09:02:09 +00003302 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smith848e1f12013-02-01 08:12:08 +00003303 if (TmpAttr.isAlignas()) {
Richard Smith1dba27c2013-01-29 09:02:09 +00003304 // C++11 [dcl.align]p1:
3305 // An alignment-specifier may be applied to a variable or to a class
3306 // data member, but it shall not be applied to a bit-field, a function
3307 // parameter, the formal parameter of a catch clause, or a variable
3308 // declared with the register storage class specifier. An
3309 // alignment-specifier may also be applied to the declaration of a class
3310 // or enumeration type.
3311 // C11 6.7.5/2:
3312 // An alignment attribute shall not be specified in a declaration of
3313 // a typedef, or a bit-field, or a function, or a parameter, or an
3314 // object declared with the register storage-class specifier.
3315 int DiagKind = -1;
3316 if (isa<ParmVarDecl>(D)) {
3317 DiagKind = 0;
3318 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3319 if (VD->getStorageClass() == SC_Register)
3320 DiagKind = 1;
3321 if (VD->isExceptionVariable())
3322 DiagKind = 2;
3323 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3324 if (FD->isBitField())
3325 DiagKind = 3;
3326 } else if (!isa<TagDecl>(D)) {
Richard Smith848e1f12013-02-01 08:12:08 +00003327 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3328 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith9eaab4b2013-02-01 08:25:07 +00003329 << (TmpAttr.isC11() ? ExpectedVariableOrField
3330 : ExpectedVariableFieldOrTag);
Richard Smith1dba27c2013-01-29 09:02:09 +00003331 return;
3332 }
3333 if (DiagKind != -1) {
Richard Smith848e1f12013-02-01 08:12:08 +00003334 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
3335 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
3336 << DiagKind;
Richard Smith1dba27c2013-01-29 09:02:09 +00003337 return;
3338 }
3339 }
3340
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003341 if (E->isTypeDependent() || E->isValueDependent()) {
3342 // Save dependent expressions in the AST to be instantiated.
Richard Smith848e1f12013-02-01 08:12:08 +00003343 D->addAttr(::new (Context) AlignedAttr(TmpAttr));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003344 return;
3345 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003346
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003347 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00003348 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00003349 ExprResult ICE
3350 = VerifyIntegerConstantExpression(E, &Alignment,
3351 diag::err_aligned_attribute_argument_not_int,
3352 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003353 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003354 return;
Richard Smith848e1f12013-02-01 08:12:08 +00003355
3356 // C++11 [dcl.align]p2:
3357 // -- if the constant expression evaluates to zero, the alignment
3358 // specifier shall have no effect
3359 // C11 6.7.5p6:
3360 // An alignment specification of zero has no effect.
3361 if (!(TmpAttr.isAlignas() && !Alignment) &&
3362 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003363 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3364 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003365 return;
3366 }
Michael Hanaf02bbe2013-02-01 01:19:17 +00003367
Richard Smith848e1f12013-02-01 08:12:08 +00003368 if (TmpAttr.isDeclspec()) {
Aaron Ballman478faed2012-06-19 22:09:27 +00003369 // We've already verified it's a power of 2, now let's make sure it's
3370 // 8192 or less.
3371 if (Alignment.getZExtValue() > 8192) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00003372 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballman478faed2012-06-19 22:09:27 +00003373 << E->getSourceRange();
3374 return;
3375 }
3376 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003377
Richard Smith848e1f12013-02-01 08:12:08 +00003378 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true,
3379 ICE.take(), SpellingListIndex));
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003380}
3381
Michael Hanaf02bbe2013-02-01 01:19:17 +00003382void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
3383 unsigned SpellingListIndex) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003384 // FIXME: Cache the number on the Attr object if non-dependent?
3385 // FIXME: Perform checking of type validity
Michael Hanaf02bbe2013-02-01 01:19:17 +00003386 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3387 SpellingListIndex));
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003388 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003389}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003390
Richard Smith848e1f12013-02-01 08:12:08 +00003391void Sema::CheckAlignasUnderalignment(Decl *D) {
3392 assert(D->hasAttrs() && "no attributes on decl");
3393
3394 QualType Ty;
3395 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3396 Ty = VD->getType();
3397 else
3398 Ty = Context.getTagDeclType(cast<TagDecl>(D));
3399 if (Ty->isDependentType())
3400 return;
3401
3402 // C++11 [dcl.align]p5, C11 6.7.5/4:
3403 // The combined effect of all alignment attributes in a declaration shall
3404 // not specify an alignment that is less strict than the alignment that
3405 // would otherwise be required for the entity being declared.
3406 AlignedAttr *AlignasAttr = 0;
3407 unsigned Align = 0;
3408 for (specific_attr_iterator<AlignedAttr>
3409 I = D->specific_attr_begin<AlignedAttr>(),
3410 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3411 if (I->isAlignmentDependent())
3412 return;
3413 if (I->isAlignas())
3414 AlignasAttr = *I;
3415 Align = std::max(Align, I->getAlignment(Context));
3416 }
3417
3418 if (AlignasAttr && Align) {
3419 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3420 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3421 if (NaturalAlign > RequestedAlign)
3422 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3423 << Ty << (unsigned)NaturalAlign.getQuantity();
3424 }
3425}
3426
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003427/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003428/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003429///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003430/// Despite what would be logical, the mode attribute is a decl attribute, not a
3431/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3432/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003433static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003434 // This attribute isn't documented, but glibc uses it. It changes
3435 // the width of an int or unsigned int to the specified size.
3436
3437 // Check that there aren't any arguments
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003438 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003439 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003440
Chris Lattneracbc2d22008-06-27 22:18:37 +00003441
3442 IdentifierInfo *Name = Attr.getParameterName();
3443 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00003444 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003445 return;
3446 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00003447
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003448 StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003449
3450 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003451 if (Str.startswith("__") && Str.endswith("__"))
3452 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003453
3454 unsigned DestWidth = 0;
3455 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003456 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003457 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003458 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003459 switch (Str[0]) {
3460 case 'Q': DestWidth = 8; break;
3461 case 'H': DestWidth = 16; break;
3462 case 'S': DestWidth = 32; break;
3463 case 'D': DestWidth = 64; break;
3464 case 'X': DestWidth = 96; break;
3465 case 'T': DestWidth = 128; break;
3466 }
3467 if (Str[1] == 'F') {
3468 IntegerMode = false;
3469 } else if (Str[1] == 'C') {
3470 IntegerMode = false;
3471 ComplexMode = true;
3472 } else if (Str[1] != 'I') {
3473 DestWidth = 0;
3474 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003475 break;
3476 case 4:
3477 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3478 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003479 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003480 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003481 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003482 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003483 break;
3484 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003485 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003486 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003487 break;
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003488 case 11:
3489 if (Str == "unwind_word")
Rafael Espindola03705972013-01-07 20:01:57 +00003490 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindolaf0dafd32013-01-07 19:58:54 +00003491 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003492 }
3493
3494 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003495 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003496 OldTy = TD->getUnderlyingType();
3497 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3498 OldTy = VD->getType();
3499 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003500 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003501 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003502 return;
3503 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003504
John McCall9dd450b2009-09-21 23:43:11 +00003505 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003506 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3507 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003508 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003509 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3510 } else if (ComplexMode) {
3511 if (!OldTy->isComplexType())
3512 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3513 } else {
3514 if (!OldTy->isFloatingType())
3515 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3516 }
3517
Mike Stump87c57ac2009-05-16 07:39:55 +00003518 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3519 // and friends, at least with glibc.
3520 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3521 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003522 // FIXME: Make sure floating-point mappings are accurate
3523 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00003524 QualType NewTy;
3525 switch (DestWidth) {
3526 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003527 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003528 return;
3529 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003530 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003531 return;
3532 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00003533 if (!IntegerMode) {
3534 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3535 return;
3536 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003537 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003538 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003539 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003540 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003541 break;
3542 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00003543 if (!IntegerMode) {
3544 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3545 return;
3546 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003547 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003548 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003549 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003550 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003551 break;
3552 case 32:
3553 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003554 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003555 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003556 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003557 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003558 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003559 break;
3560 case 64:
3561 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003562 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003563 else if (OldTy->isSignedIntegerType())
Douglas Gregore8bbc122011-09-02 00:18:52 +00003564 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00003565 NewTy = S.Context.LongTy;
3566 else
3567 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003568 else
Douglas Gregore8bbc122011-09-02 00:18:52 +00003569 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00003570 NewTy = S.Context.UnsignedLongTy;
3571 else
3572 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003573 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003574 case 96:
3575 NewTy = S.Context.LongDoubleTy;
3576 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00003577 case 128:
3578 if (!IntegerMode) {
3579 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3580 return;
3581 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00003582 if (OldTy->isSignedIntegerType())
3583 NewTy = S.Context.Int128Ty;
3584 else
3585 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00003586 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003587 }
3588
Eli Friedman4735374e2009-03-03 06:41:03 +00003589 if (ComplexMode) {
3590 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003591 }
3592
3593 // Install the new type.
Richard Smithdda56e42011-04-15 14:24:37 +00003594 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall703a3f82009-10-24 08:00:42 +00003595 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00003596 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00003597 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003598 cast<ValueDecl>(D)->setType(NewTy);
3599}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003600
Chandler Carruthedc2c642011-07-02 00:01:44 +00003601static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson76187b42009-02-13 06:46:13 +00003602 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003603 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson76187b42009-02-13 06:46:13 +00003604 return;
Anders Carlsson63784f42009-02-13 08:11:52 +00003605
Nick Lewycky08597072012-07-24 01:40:49 +00003606 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3607 if (!VD->hasGlobalStorage())
3608 S.Diag(Attr.getLoc(),
3609 diag::warn_attribute_requires_functions_or_static_globals)
3610 << Attr.getName();
3611 } else if (!isFunctionOrMethod(D)) {
3612 S.Diag(Attr.getLoc(),
3613 diag::warn_attribute_requires_functions_or_static_globals)
3614 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003615 return;
3616 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003617
Michael Han99315932013-01-24 16:46:58 +00003618 D->addAttr(::new (S.Context)
3619 NoDebugAttr(Attr.getRange(), S.Context,
3620 Attr.getAttributeSpellingListIndex()));
Anders Carlsson76187b42009-02-13 06:46:13 +00003621}
3622
Chandler Carruthedc2c642011-07-02 00:01:44 +00003623static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson88097122009-02-19 19:16:48 +00003624 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003625 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson88097122009-02-19 19:16:48 +00003626 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003627
Mike Stumpd3bb5572009-07-24 19:02:52 +00003628
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003629 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00003630 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003631 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +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 NoInlineAttr(Attr.getRange(), S.Context,
3637 Attr.getAttributeSpellingListIndex()));
Anders Carlsson88097122009-02-19 19:16:48 +00003638}
3639
Chandler Carruthedc2c642011-07-02 00:01:44 +00003640static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3641 const AttributeList &Attr) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003642 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003643 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner3c77a352010-06-22 00:03:40 +00003644 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003645
Chris Lattner3c77a352010-06-22 00:03:40 +00003646
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003647 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003648 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003649 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00003650 return;
3651 }
3652
Michael Han99315932013-01-24 16:46:58 +00003653 D->addAttr(::new (S.Context)
3654 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3655 Attr.getAttributeSpellingListIndex()));
Chris Lattner3c77a352010-06-22 00:03:40 +00003656}
3657
Chandler Carruthedc2c642011-07-02 00:01:44 +00003658static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003659 if (S.LangOpts.CUDA) {
3660 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00003661 if (Attr.hasParameterOrArguments()) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003662 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3663 return;
3664 }
3665
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003666 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003667 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003668 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003669 return;
3670 }
3671
Michael Han99315932013-01-24 16:46:58 +00003672 D->addAttr(::new (S.Context)
3673 CUDAConstantAttr(Attr.getRange(), S.Context,
3674 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003675 } else {
3676 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3677 }
3678}
3679
Chandler Carruthedc2c642011-07-02 00:01:44 +00003680static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003681 if (S.LangOpts.CUDA) {
3682 // check the attribute arguments.
3683 if (Attr.getNumArgs() != 0) {
3684 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3685 return;
3686 }
3687
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003688 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003689 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003690 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003691 return;
3692 }
3693
Michael Han99315932013-01-24 16:46:58 +00003694 D->addAttr(::new (S.Context)
3695 CUDADeviceAttr(Attr.getRange(), S.Context,
3696 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003697 } else {
3698 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3699 }
3700}
3701
Chandler Carruthedc2c642011-07-02 00:01:44 +00003702static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003703 if (S.LangOpts.CUDA) {
3704 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003705 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003706 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003707
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003708 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003709 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003710 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003711 return;
3712 }
3713
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003714 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003715 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00003716 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003717 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
3718 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3719 << FD->getType()
3720 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
3721 "void");
3722 } else {
3723 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3724 << FD->getType();
3725 }
3726 return;
3727 }
3728
Michael Han99315932013-01-24 16:46:58 +00003729 D->addAttr(::new (S.Context)
3730 CUDAGlobalAttr(Attr.getRange(), S.Context,
3731 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003732 } else {
3733 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3734 }
3735}
3736
Chandler Carruthedc2c642011-07-02 00:01:44 +00003737static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003738 if (S.LangOpts.CUDA) {
3739 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003740 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003741 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003742
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003743
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003744 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003745 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003746 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003747 return;
3748 }
3749
Michael Han99315932013-01-24 16:46:58 +00003750 D->addAttr(::new (S.Context)
3751 CUDAHostAttr(Attr.getRange(), S.Context,
3752 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003753 } else {
3754 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3755 }
3756}
3757
Chandler Carruthedc2c642011-07-02 00:01:44 +00003758static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003759 if (S.LangOpts.CUDA) {
3760 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003761 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003762 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003763
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003764 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003765 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003766 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003767 return;
3768 }
3769
Michael Han99315932013-01-24 16:46:58 +00003770 D->addAttr(::new (S.Context)
3771 CUDASharedAttr(Attr.getRange(), S.Context,
3772 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003773 } else {
3774 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3775 }
3776}
3777
Chandler Carruthedc2c642011-07-02 00:01:44 +00003778static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003779 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003780 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnereaad6b72009-04-14 16:30:50 +00003781 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003782
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003783 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00003784 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003785 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003786 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00003787 return;
3788 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003789
Douglas Gregor35b57532009-10-27 21:01:01 +00003790 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003791 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003792 return;
3793 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003794
Michael Han99315932013-01-24 16:46:58 +00003795 D->addAttr(::new (S.Context)
3796 GNUInlineAttr(Attr.getRange(), S.Context,
3797 Attr.getAttributeSpellingListIndex()));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003798}
3799
Chandler Carruthedc2c642011-07-02 00:01:44 +00003800static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003801 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003802
Aaron Ballman02df2e02012-12-09 17:45:41 +00003803 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003804 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003805 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3806 CallingConv CC;
Aaron Ballman02df2e02012-12-09 17:45:41 +00003807 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall3882ace2011-01-05 12:14:39 +00003808 return;
3809
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003810 if (!isa<ObjCMethodDecl>(D)) {
3811 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3812 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003813 return;
3814 }
3815
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003816 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003817 case AttributeList::AT_FastCall:
Michael Han99315932013-01-24 16:46:58 +00003818 D->addAttr(::new (S.Context)
3819 FastCallAttr(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_StdCall:
Michael Han99315932013-01-24 16:46:58 +00003823 D->addAttr(::new (S.Context)
3824 StdCallAttr(Attr.getRange(), S.Context,
3825 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003826 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003827 case AttributeList::AT_ThisCall:
Michael Han99315932013-01-24 16:46:58 +00003828 D->addAttr(::new (S.Context)
3829 ThisCallAttr(Attr.getRange(), S.Context,
3830 Attr.getAttributeSpellingListIndex()));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003831 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003832 case AttributeList::AT_CDecl:
Michael Han99315932013-01-24 16:46:58 +00003833 D->addAttr(::new (S.Context)
3834 CDeclAttr(Attr.getRange(), S.Context,
3835 Attr.getAttributeSpellingListIndex()));
Abramo Bagnara50099372010-04-30 13:10:51 +00003836 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003837 case AttributeList::AT_Pascal:
Michael Han99315932013-01-24 16:46:58 +00003838 D->addAttr(::new (S.Context)
3839 PascalAttr(Attr.getRange(), S.Context,
3840 Attr.getAttributeSpellingListIndex()));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003841 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003842 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003843 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003844 switch (CC) {
3845 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003846 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003847 break;
3848 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003849 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003850 break;
3851 default:
3852 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003853 }
3854
Michael Han99315932013-01-24 16:46:58 +00003855 D->addAttr(::new (S.Context)
3856 PcsAttr(Attr.getRange(), S.Context, PCS,
3857 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003858 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003859 }
Derek Schuffa2020962012-10-16 22:30:41 +00003860 case AttributeList::AT_PnaclCall:
Michael Han99315932013-01-24 16:46:58 +00003861 D->addAttr(::new (S.Context)
3862 PnaclCallAttr(Attr.getRange(), S.Context,
3863 Attr.getAttributeSpellingListIndex()));
Derek Schuffa2020962012-10-16 22:30:41 +00003864 return;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003865 case AttributeList::AT_IntelOclBicc:
Michael Han99315932013-01-24 16:46:58 +00003866 D->addAttr(::new (S.Context)
3867 IntelOclBiccAttr(Attr.getRange(), S.Context,
3868 Attr.getAttributeSpellingListIndex()));
Guy Benyeif0a014b2012-12-25 08:53:55 +00003869 return;
Derek Schuffa2020962012-10-16 22:30:41 +00003870
Abramo Bagnara50099372010-04-30 13:10:51 +00003871 default:
3872 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003873 }
3874}
3875
Chandler Carruthedc2c642011-07-02 00:01:44 +00003876static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth9312c642011-07-11 23:33:05 +00003877 assert(!Attr.isInvalid());
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003878 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003879}
3880
Aaron Ballman02df2e02012-12-09 17:45:41 +00003881bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3882 const FunctionDecl *FD) {
John McCall3882ace2011-01-05 12:14:39 +00003883 if (attr.isInvalid())
3884 return true;
3885
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003886 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
3887 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
3888 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall3882ace2011-01-05 12:14:39 +00003889 attr.setInvalid();
3890 return true;
3891 }
3892
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003893 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3894 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003895 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003896 case AttributeList::AT_CDecl: CC = CC_C; break;
3897 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3898 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3899 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3900 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
3901 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003902 Expr *Arg = attr.getArg(0);
3903 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00003904 if (!Str || !Str->isAscii()) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003905 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3906 << "pcs" << 1;
3907 attr.setInvalid();
3908 return true;
3909 }
3910
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003911 StringRef StrRef = Str->getString();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003912 if (StrRef == "aapcs") {
3913 CC = CC_AAPCS;
3914 break;
3915 } else if (StrRef == "aapcs-vfp") {
3916 CC = CC_AAPCS_VFP;
3917 break;
3918 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003919
3920 attr.setInvalid();
3921 Diag(attr.getLoc(), diag::err_invalid_pcs);
3922 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003923 }
Derek Schuffa2020962012-10-16 22:30:41 +00003924 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyeif0a014b2012-12-25 08:53:55 +00003925 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003926 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003927 }
3928
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003929 const TargetInfo &TI = Context.getTargetInfo();
3930 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3931 if (A == TargetInfo::CCCR_Warning) {
3932 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballman02df2e02012-12-09 17:45:41 +00003933
3934 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3935 if (FD)
3936 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3937 TargetInfo::CCMT_NonMember;
3938 CC = TI.getDefaultCallingConv(MT);
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003939 }
3940
John McCall3882ace2011-01-05 12:14:39 +00003941 return false;
3942}
3943
Chandler Carruthedc2c642011-07-02 00:01:44 +00003944static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003945 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003946
3947 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003948 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003949 return;
3950
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003951 if (!isa<ObjCMethodDecl>(D)) {
3952 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3953 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003954 return;
3955 }
Eli Friedman7044b762009-03-27 21:06:47 +00003956
Michael Han99315932013-01-24 16:46:58 +00003957 D->addAttr(::new (S.Context)
3958 RegparmAttr(Attr.getRange(), S.Context, numParams,
3959 Attr.getAttributeSpellingListIndex()));
John McCall3882ace2011-01-05 12:14:39 +00003960}
3961
3962/// Checks a regparm attribute, returning true if it is ill-formed and
3963/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003964bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3965 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003966 return true;
3967
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003968 if (Attr.getNumArgs() != 1) {
3969 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3970 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003971 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003972 }
Eli Friedman7044b762009-03-27 21:06:47 +00003973
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003974 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman7044b762009-03-27 21:06:47 +00003975 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003976 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall3882ace2011-01-05 12:14:39 +00003977 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003978 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman7044b762009-03-27 21:06:47 +00003979 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003980 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003981 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003982 }
3983
Douglas Gregore8bbc122011-09-02 00:18:52 +00003984 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003985 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003986 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003987 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003988 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003989 }
3990
John McCall3882ace2011-01-05 12:14:39 +00003991 numParams = NumParams.getZExtValue();
Douglas Gregore8bbc122011-09-02 00:18:52 +00003992 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003993 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003994 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003995 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003996 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003997 }
3998
John McCall3882ace2011-01-05 12:14:39 +00003999 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00004000}
4001
Chandler Carruthedc2c642011-07-02 00:01:44 +00004002static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00004003 if (S.LangOpts.CUDA) {
4004 // check the attribute arguments.
4005 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00004006 // FIXME: 0 is not okay.
4007 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004008 return;
4009 }
4010
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004011 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00004012 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00004013 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00004014 return;
4015 }
4016
4017 Expr *MaxThreadsExpr = Attr.getArg(0);
4018 llvm::APSInt MaxThreads(32);
4019 if (MaxThreadsExpr->isTypeDependent() ||
4020 MaxThreadsExpr->isValueDependent() ||
4021 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
4022 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
4023 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
4024 return;
4025 }
4026
4027 llvm::APSInt MinBlocks(32);
4028 if (Attr.getNumArgs() > 1) {
4029 Expr *MinBlocksExpr = Attr.getArg(1);
4030 if (MinBlocksExpr->isTypeDependent() ||
4031 MinBlocksExpr->isValueDependent() ||
4032 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
4033 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
4034 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
4035 return;
4036 }
4037 }
4038
Michael Han99315932013-01-24 16:46:58 +00004039 D->addAttr(::new (S.Context)
4040 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4041 MaxThreads.getZExtValue(),
4042 MinBlocks.getZExtValue(),
4043 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne827301e2010-12-12 23:03:07 +00004044 } else {
4045 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4046 }
4047}
4048
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004049static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4050 const AttributeList &Attr) {
4051 StringRef AttrName = Attr.getName()->getName();
4052 if (!Attr.getParameterName()) {
4053 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
4054 << Attr.getName() << /* arg num = */ 1;
4055 return;
4056 }
4057
4058 if (Attr.getNumArgs() != 2) {
4059 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4060 << /* required args = */ 3;
4061 return;
4062 }
4063
4064 IdentifierInfo *ArgumentKind = Attr.getParameterName();
4065
4066 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4067 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4068 << Attr.getName() << ExpectedFunctionOrMethod;
4069 return;
4070 }
4071
4072 uint64_t ArgumentIdx;
4073 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4074 Attr.getLoc(), 2,
4075 Attr.getArg(0), ArgumentIdx))
4076 return;
4077
4078 uint64_t TypeTagIdx;
4079 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4080 Attr.getLoc(), 3,
4081 Attr.getArg(1), TypeTagIdx))
4082 return;
4083
4084 bool IsPointer = (AttrName == "pointer_with_type_tag");
4085 if (IsPointer) {
4086 // Ensure that buffer has a pointer type.
4087 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4088 if (!BufferTy->isPointerType()) {
4089 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
4090 << AttrName;
4091 }
4092 }
4093
Michael Han99315932013-01-24 16:46:58 +00004094 D->addAttr(::new (S.Context)
4095 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4096 ArgumentIdx, TypeTagIdx, IsPointer,
4097 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004098}
4099
4100static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4101 const AttributeList &Attr) {
4102 IdentifierInfo *PointerKind = Attr.getParameterName();
4103 if (!PointerKind) {
4104 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
4105 << "type_tag_for_datatype" << 1;
4106 return;
4107 }
4108
4109 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4110
Michael Han99315932013-01-24 16:46:58 +00004111 D->addAttr(::new (S.Context)
4112 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4113 MatchingCType,
4114 Attr.getLayoutCompatible(),
4115 Attr.getMustBeNull(),
4116 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004117}
4118
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004119//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004120// Checker-specific attribute handlers.
4121//===----------------------------------------------------------------------===//
4122
John McCalled433932011-01-25 03:31:58 +00004123static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004124 return type->isDependentType() ||
4125 type->isObjCObjectPointerType() ||
4126 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00004127}
4128static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00004129 return type->isDependentType() ||
4130 type->isPointerType() ||
4131 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00004132}
4133
Chandler Carruthedc2c642011-07-02 00:01:44 +00004134static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004135 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00004136 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004137 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004138 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00004139 return;
4140 }
4141
4142 bool typeOK, cf;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004143 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00004144 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4145 cf = false;
4146 } else {
4147 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4148 cf = true;
4149 }
4150
4151 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004152 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004153 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00004154 return;
4155 }
4156
4157 if (cf)
Michael Han99315932013-01-24 16:46:58 +00004158 param->addAttr(::new (S.Context)
4159 CFConsumedAttr(Attr.getRange(), S.Context,
4160 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004161 else
Michael Han99315932013-01-24 16:46:58 +00004162 param->addAttr(::new (S.Context)
4163 NSConsumedAttr(Attr.getRange(), S.Context,
4164 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004165}
4166
Chandler Carruthedc2c642011-07-02 00:01:44 +00004167static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4168 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004169 if (!isa<ObjCMethodDecl>(D)) {
4170 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004171 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00004172 return;
4173 }
4174
Michael Han99315932013-01-24 16:46:58 +00004175 D->addAttr(::new (S.Context)
4176 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4177 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004178}
4179
Chandler Carruthedc2c642011-07-02 00:01:44 +00004180static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4181 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004182
John McCalled433932011-01-25 03:31:58 +00004183 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004184
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004185 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004186 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004187 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004188 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00004189 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00004190 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4191 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004192 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00004193 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00004194 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004195 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004196 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00004197 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004198 return;
4199 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004200
John McCalled433932011-01-25 03:31:58 +00004201 bool typeOK;
4202 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004203 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00004204 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004205 case AttributeList::AT_NSReturnsAutoreleased:
4206 case AttributeList::AT_NSReturnsRetained:
4207 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004208 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4209 cf = false;
4210 break;
4211
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004212 case AttributeList::AT_CFReturnsRetained:
4213 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00004214 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4215 cf = true;
4216 break;
4217 }
4218
4219 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004220 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004221 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004222 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00004223 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00004224
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004225 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004226 default:
David Blaikie83d382b2011-09-23 05:06:16 +00004227 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004228 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han99315932013-01-24 16:46:58 +00004229 D->addAttr(::new (S.Context)
4230 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4231 Attr.getAttributeSpellingListIndex()));
John McCalled433932011-01-25 03:31:58 +00004232 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004233 case AttributeList::AT_CFReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004234 D->addAttr(::new (S.Context)
4235 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4236 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004237 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004238 case AttributeList::AT_NSReturnsNotRetained:
Michael Han99315932013-01-24 16:46:58 +00004239 D->addAttr(::new (S.Context)
4240 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4241 Attr.getAttributeSpellingListIndex()));
Ted Kremenekd9c66632010-02-18 00:05:45 +00004242 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004243 case AttributeList::AT_CFReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004244 D->addAttr(::new (S.Context)
4245 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4246 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004247 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004248 case AttributeList::AT_NSReturnsRetained:
Michael Han99315932013-01-24 16:46:58 +00004249 D->addAttr(::new (S.Context)
4250 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4251 Attr.getAttributeSpellingListIndex()));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004252 return;
4253 };
4254}
4255
John McCallcf166702011-07-22 08:53:00 +00004256static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4257 const AttributeList &attr) {
4258 SourceLocation loc = attr.getLoc();
4259
4260 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4261
Fariborz Jahaniana53e5d72012-04-21 17:51:44 +00004262 if (!method) {
Fariborz Jahanian344d65c2012-04-20 22:00:46 +00004263 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004264 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCallcf166702011-07-22 08:53:00 +00004265 return;
4266 }
4267
4268 // Check that the method returns a normal pointer.
4269 QualType resultType = method->getResultType();
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00004270
4271 if (!resultType->isReferenceType() &&
4272 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCallcf166702011-07-22 08:53:00 +00004273 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4274 << SourceRange(loc)
4275 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4276
4277 // Drop the attribute.
4278 return;
4279 }
4280
Michael Han99315932013-01-24 16:46:58 +00004281 method->addAttr(::new (S.Context)
4282 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4283 attr.getAttributeSpellingListIndex()));
John McCallcf166702011-07-22 08:53:00 +00004284}
4285
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004286static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4287 const AttributeList &attr) {
4288 SourceLocation loc = attr.getLoc();
4289 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4290
4291 if (!method) {
4292 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4293 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4294 return;
4295 }
4296 DeclContext *DC = method->getDeclContext();
4297 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4298 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4299 << attr.getName() << 0;
4300 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4301 return;
4302 }
4303 if (method->getMethodFamily() == OMF_dealloc) {
4304 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4305 << attr.getName() << 1;
4306 return;
4307 }
4308
Michael Han99315932013-01-24 16:46:58 +00004309 method->addAttr(::new (S.Context)
4310 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4311 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004312}
4313
John McCall32f5fe12011-09-30 05:12:12 +00004314/// Handle cf_audited_transfer and cf_unknown_transfer.
4315static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4316 if (!isa<FunctionDecl>(D)) {
4317 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004318 << A.getRange() << A.getName() << ExpectedFunction;
John McCall32f5fe12011-09-30 05:12:12 +00004319 return;
4320 }
4321
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004322 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall32f5fe12011-09-30 05:12:12 +00004323
4324 // Check whether there's a conflicting attribute already present.
4325 Attr *Existing;
4326 if (IsAudited) {
4327 Existing = D->getAttr<CFUnknownTransferAttr>();
4328 } else {
4329 Existing = D->getAttr<CFAuditedTransferAttr>();
4330 }
4331 if (Existing) {
4332 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4333 << A.getName()
4334 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4335 << A.getRange() << Existing->getRange();
4336 return;
4337 }
4338
4339 // All clear; add the attribute.
4340 if (IsAudited) {
Michael Han99315932013-01-24 16:46:58 +00004341 D->addAttr(::new (S.Context)
4342 CFAuditedTransferAttr(A.getRange(), S.Context,
4343 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004344 } else {
Michael Han99315932013-01-24 16:46:58 +00004345 D->addAttr(::new (S.Context)
4346 CFUnknownTransferAttr(A.getRange(), S.Context,
4347 A.getAttributeSpellingListIndex()));
John McCall32f5fe12011-09-30 05:12:12 +00004348 }
4349}
4350
John McCallf1e8b342011-09-29 07:17:38 +00004351static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4352 const AttributeList &Attr) {
4353 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4354 if (!RD || RD->isUnion()) {
4355 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004356 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00004357 }
4358
4359 IdentifierInfo *ParmName = Attr.getParameterName();
4360
4361 // In Objective-C, verify that the type names an Objective-C type.
4362 // We don't want to check this outside of ObjC because people sometimes
4363 // do crazy C declarations of Objective-C types.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004364 if (ParmName && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00004365 // Check for an existing type with this name.
4366 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4367 Sema::LookupOrdinaryName);
4368 if (S.LookupName(R, Sc)) {
4369 NamedDecl *Target = R.getFoundDecl();
4370 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4371 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4372 S.Diag(Target->getLocStart(), diag::note_declared_at);
4373 }
4374 }
4375 }
4376
Michael Han99315932013-01-24 16:46:58 +00004377 D->addAttr(::new (S.Context)
4378 NSBridgedAttr(Attr.getRange(), S.Context, ParmName,
4379 Attr.getAttributeSpellingListIndex()));
John McCallf1e8b342011-09-29 07:17:38 +00004380}
4381
Chandler Carruthedc2c642011-07-02 00:01:44 +00004382static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4383 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004384 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004385
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004386 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004387 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004388}
4389
Chandler Carruthedc2c642011-07-02 00:01:44 +00004390static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4391 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004392 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004393 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004394 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004395 return;
4396 }
4397
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004398 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004399 QualType type = vd->getType();
4400
4401 if (!type->isDependentType() &&
4402 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004403 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004404 << type;
4405 return;
4406 }
4407
4408 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4409
4410 // If we have no lifetime yet, check the lifetime we're presumably
4411 // going to infer.
4412 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4413 lifetime = type->getObjCARCImplicitLifetime();
4414
4415 switch (lifetime) {
4416 case Qualifiers::OCL_None:
4417 assert(type->isDependentType() &&
4418 "didn't infer lifetime for non-dependent type?");
4419 break;
4420
4421 case Qualifiers::OCL_Weak: // meaningful
4422 case Qualifiers::OCL_Strong: // meaningful
4423 break;
4424
4425 case Qualifiers::OCL_ExplicitNone:
4426 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004427 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004428 << (lifetime == Qualifiers::OCL_Autoreleasing);
4429 break;
4430 }
4431
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004432 D->addAttr(::new (S.Context)
Michael Han99315932013-01-24 16:46:58 +00004433 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4434 Attr.getAttributeSpellingListIndex()));
John McCall31168b02011-06-15 23:02:42 +00004435}
4436
Francois Picheta83957a2010-12-19 06:50:37 +00004437//===----------------------------------------------------------------------===//
4438// Microsoft specific attribute handlers.
4439//===----------------------------------------------------------------------===//
4440
Chandler Carruthedc2c642011-07-02 00:01:44 +00004441static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet0706d202011-09-17 17:15:52 +00004442 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Picheta83957a2010-12-19 06:50:37 +00004443 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004444 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Picheta83957a2010-12-19 06:50:37 +00004445 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004446
Francois Picheta83957a2010-12-19 06:50:37 +00004447 Expr *Arg = Attr.getArg(0);
4448 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00004449 if (!Str || !Str->isAscii()) {
Francois Pichet7da11662010-12-20 01:41:49 +00004450 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
4451 << "uuid" << 1;
4452 return;
4453 }
4454
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004455 StringRef StrRef = Str->getString();
Francois Pichet7da11662010-12-20 01:41:49 +00004456
4457 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4458 StrRef.back() == '}';
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004459
Francois Pichet7da11662010-12-20 01:41:49 +00004460 // Validate GUID length.
4461 if (IsCurly && StrRef.size() != 38) {
4462 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4463 return;
4464 }
4465 if (!IsCurly && StrRef.size() != 36) {
4466 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4467 return;
4468 }
4469
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004470 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichet7da11662010-12-20 01:41:49 +00004471 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004472 StringRef::iterator I = StrRef.begin();
Anders Carlsson19588aa2011-01-23 21:07:30 +00004473 if (IsCurly) // Skip the optional '{'
4474 ++I;
4475
4476 for (int i = 0; i < 36; ++i) {
Francois Pichet7da11662010-12-20 01:41:49 +00004477 if (i == 8 || i == 13 || i == 18 || i == 23) {
4478 if (*I != '-') {
4479 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4480 return;
4481 }
Jordan Rosea7d03842013-02-08 22:30:41 +00004482 } else if (!isHexDigit(*I)) {
Francois Pichet7da11662010-12-20 01:41:49 +00004483 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4484 return;
4485 }
4486 I++;
4487 }
Francois Picheta83957a2010-12-19 06:50:37 +00004488
Michael Han99315932013-01-24 16:46:58 +00004489 D->addAttr(::new (S.Context)
4490 UuidAttr(Attr.getRange(), S.Context, Str->getString(),
4491 Attr.getAttributeSpellingListIndex()));
Francois Pichet7da11662010-12-20 01:41:49 +00004492 } else
Francois Picheta83957a2010-12-19 06:50:37 +00004493 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davis163855f2010-02-16 18:27:26 +00004494}
4495
John McCall8d32c052012-05-22 21:28:12 +00004496static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nico Weberefa45b22012-11-07 21:31:36 +00004497 if (!S.LangOpts.MicrosoftExt) {
John McCall8d32c052012-05-22 21:28:12 +00004498 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Nico Weberefa45b22012-11-07 21:31:36 +00004499 return;
4500 }
4501
4502 AttributeList::Kind Kind = Attr.getKind();
4503 if (Kind == AttributeList::AT_SingleInheritance)
4504 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004505 ::new (S.Context)
4506 SingleInheritanceAttr(Attr.getRange(), S.Context,
4507 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004508 else if (Kind == AttributeList::AT_MultipleInheritance)
4509 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004510 ::new (S.Context)
4511 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4512 Attr.getAttributeSpellingListIndex()));
Nico Weberefa45b22012-11-07 21:31:36 +00004513 else if (Kind == AttributeList::AT_VirtualInheritance)
4514 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004515 ::new (S.Context)
4516 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4517 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004518}
4519
4520static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4521 if (S.LangOpts.MicrosoftExt) {
4522 AttributeList::Kind Kind = Attr.getKind();
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004523 if (Kind == AttributeList::AT_Ptr32)
John McCall8d32c052012-05-22 21:28:12 +00004524 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004525 ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context,
4526 Attr.getAttributeSpellingListIndex()));
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004527 else if (Kind == AttributeList::AT_Ptr64)
John McCall8d32c052012-05-22 21:28:12 +00004528 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004529 ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context,
4530 Attr.getAttributeSpellingListIndex()));
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004531 else if (Kind == AttributeList::AT_Win64)
John McCall8d32c052012-05-22 21:28:12 +00004532 D->addAttr(
Michael Han99315932013-01-24 16:46:58 +00004533 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4534 Attr.getAttributeSpellingListIndex()));
John McCall8d32c052012-05-22 21:28:12 +00004535 } else
4536 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4537}
4538
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004539static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4540 if (S.LangOpts.MicrosoftExt)
Michael Han99315932013-01-24 16:46:58 +00004541 D->addAttr(::new (S.Context)
4542 ForceInlineAttr(Attr.getRange(), S.Context,
4543 Attr.getAttributeSpellingListIndex()));
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004544 else
4545 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4546}
4547
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004548//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004549// Top Level Sema Entry Points
4550//===----------------------------------------------------------------------===//
4551
Chandler Carruthedc2c642011-07-02 00:01:44 +00004552static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4553 const AttributeList &Attr) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00004554 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004555 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4556 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4557 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00004558 default:
4559 break;
4560 }
4561}
Abramo Bagnara50099372010-04-30 13:10:51 +00004562
Chandler Carruthedc2c642011-07-02 00:01:44 +00004563static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4564 const AttributeList &Attr) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004565 switch (Attr.getKind()) {
Richard Smith10876ef2013-01-17 01:30:42 +00004566 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4567 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4568 case AttributeList::AT_IBOutletCollection:
4569 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004570 case AttributeList::AT_AddressSpace:
4571 case AttributeList::AT_OpenCLImageAccess:
4572 case AttributeList::AT_ObjCGC:
4573 case AttributeList::AT_VectorSize:
4574 case AttributeList::AT_NeonVectorType:
4575 case AttributeList::AT_NeonPolyVectorType:
Mike Stumpd3bb5572009-07-24 19:02:52 +00004576 // Ignore these, these are type attributes, handled by
4577 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004578 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004579 case AttributeList::AT_CUDADevice:
4580 case AttributeList::AT_CUDAHost:
4581 case AttributeList::AT_Overloadable:
Peter Collingbourneb331b262011-01-21 02:08:45 +00004582 // Ignore, this is a non-inheritable attribute, handled
4583 // by ProcessNonInheritableDeclAttr.
4584 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004585 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4586 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4587 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4588 case AttributeList::AT_AlwaysInline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004589 handleAlwaysInlineAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004590 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004591 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00004592 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004593 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4594 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4595 case AttributeList::AT_CarriesDependency:
Richard Smithe233fbf2013-01-28 22:42:45 +00004596 handleDependencyAttr(S, scope, D, Attr);
4597 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004598 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4599 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4600 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smith10876ef2013-01-17 01:30:42 +00004601 case AttributeList::AT_CXX11NoReturn:
4602 handleCXX11NoReturnAttr(S, D, Attr);
4603 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004604 case AttributeList::AT_Deprecated:
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004605 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated");
4606 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004607 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4608 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004609 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004610 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004611 case AttributeList::AT_MinSize:
4612 handleMinSizeAttr(S, D, Attr);
4613 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004614 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4615 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4616 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4617 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4618 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004619 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004620 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004621 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4622 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4623 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4624 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4625 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00004626 case AttributeList::AT_ownership_returns:
4627 case AttributeList::AT_ownership_takes:
4628 case AttributeList::AT_ownership_holds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004629 handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004630 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4631 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4632 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4633 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4634 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4635 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4636 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004637
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004638 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004639 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004640 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004641 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004642
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004643 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004644 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4645
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004646 case AttributeList::AT_ObjCRequiresSuper:
4647 handleObjCRequiresSuperAttr(S, D, Attr); break;
4648
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004649 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00004650 handleNSBridgedAttr(S, scope, D, Attr); break;
4651
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004652 case AttributeList::AT_CFAuditedTransfer:
4653 case AttributeList::AT_CFUnknownTransfer:
John McCall32f5fe12011-09-30 05:12:12 +00004654 handleCFTransferAttr(S, D, Attr); break;
4655
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004656 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004657 case AttributeList::AT_CFConsumed:
4658 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4659 case AttributeList::AT_NSConsumesSelf:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004660 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004661
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004662 case AttributeList::AT_NSReturnsAutoreleased:
4663 case AttributeList::AT_NSReturnsNotRetained:
4664 case AttributeList::AT_CFReturnsNotRetained:
4665 case AttributeList::AT_NSReturnsRetained:
4666 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004667 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004668
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004669 case AttributeList::AT_WorkGroupSizeHint:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004670 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004671 handleWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00004672
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004673 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004674 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004675
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004676 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4677 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4678 case AttributeList::AT_Unavailable:
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004679 handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable");
4680 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004681 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00004682 handleArcWeakrefUnavailableAttr (S, D, Attr);
4683 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004684 case AttributeList::AT_ObjCRootClass:
Patrick Beardacfbe9e2012-04-06 18:12:22 +00004685 handleObjCRootClassAttr(S, D, Attr);
4686 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004687 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00004688 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00004689 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004690 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4691 case AttributeList::AT_ReturnsTwice:
Rafael Espindola70107f92011-10-03 14:59:42 +00004692 handleReturnsTwiceAttr(S, D, Attr);
4693 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004694 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
4695 case AttributeList::AT_Visibility: handleVisibilityAttr (S, D, Attr); break;
4696 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004697 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004698 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4699 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4700 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4701 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004702 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004703 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004704 case AttributeList::AT_ObjCException:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004705 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00004706 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004707 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004708 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004709 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004710 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4711 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4712 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4713 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4714 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4715 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4716 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4717 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4718 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004719 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004720 // Just ignore
4721 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004722 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00004723 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00004724 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004725 case AttributeList::AT_StdCall:
4726 case AttributeList::AT_CDecl:
4727 case AttributeList::AT_FastCall:
4728 case AttributeList::AT_ThisCall:
4729 case AttributeList::AT_Pascal:
4730 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004731 case AttributeList::AT_PnaclCall:
Guy Benyeif0a014b2012-12-25 08:53:55 +00004732 case AttributeList::AT_IntelOclBicc:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004733 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004734 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004735 case AttributeList::AT_OpenCLKernel:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004736 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00004737 break;
John McCall8d32c052012-05-22 21:28:12 +00004738
4739 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004740 case AttributeList::AT_MsStruct:
John McCall8d32c052012-05-22 21:28:12 +00004741 handleMsStructAttr(S, D, Attr);
4742 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004743 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004744 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004745 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004746 case AttributeList::AT_SingleInheritance:
4747 case AttributeList::AT_MultipleInheritance:
4748 case AttributeList::AT_VirtualInheritance:
John McCall8d32c052012-05-22 21:28:12 +00004749 handleInheritanceAttr(S, D, Attr);
4750 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004751 case AttributeList::AT_Win64:
4752 case AttributeList::AT_Ptr32:
4753 case AttributeList::AT_Ptr64:
John McCall8d32c052012-05-22 21:28:12 +00004754 handlePortabilityAttr(S, D, Attr);
4755 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004756 case AttributeList::AT_ForceInline:
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004757 handleForceInlineAttr(S, D, Attr);
4758 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004759
4760 // Thread safety attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004761 case AttributeList::AT_GuardedVar:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004762 handleGuardedVarAttr(S, D, Attr);
4763 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004764 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004765 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004766 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004767 case AttributeList::AT_ScopedLockable:
Michael Han3be3b442012-07-23 18:48:41 +00004768 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004769 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004770 case AttributeList::AT_NoAddressSafetyAnalysis:
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004771 handleNoAddressSafetyAttr(S, D, Attr);
4772 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004773 case AttributeList::AT_NoThreadSafetyAnalysis:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004774 handleNoThreadSafetyAttr(S, D, Attr);
4775 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004776 case AttributeList::AT_Lockable:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004777 handleLockableAttr(S, D, Attr);
4778 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004779 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004780 handleGuardedByAttr(S, D, Attr);
4781 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004782 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004783 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004784 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004785 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004786 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004787 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004788 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004789 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004790 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004791 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004792 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004793 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004794 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004795 handleLockReturnedAttr(S, D, Attr);
4796 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004797 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004798 handleLocksExcludedAttr(S, D, Attr);
4799 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004800 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004801 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004802 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004803 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004804 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004805 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004806 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004807 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004808 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004809 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004810 handleUnlockFunAttr(S, D, Attr);
4811 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004812 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004813 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004814 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004815 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004816 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004817 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004818
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004819 // Type safety attributes.
4820 case AttributeList::AT_ArgumentWithTypeTag:
4821 handleArgumentWithTypeTagAttr(S, D, Attr);
4822 break;
4823 case AttributeList::AT_TypeTagForDatatype:
4824 handleTypeTagForDatatypeAttr(S, D, Attr);
4825 break;
4826
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004827 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004828 // Ask target about the attribute.
4829 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4830 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004831 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4832 diag::warn_unhandled_ms_attribute_ignored :
4833 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004834 break;
4835 }
4836}
4837
Peter Collingbourneb331b262011-01-21 02:08:45 +00004838/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4839/// the attribute applies to decls. If the attribute is a type attribute, just
Richard Smith10876ef2013-01-17 01:30:42 +00004840/// silently ignore it if a GNU attribute.
Chandler Carruthedc2c642011-07-02 00:01:44 +00004841static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4842 const AttributeList &Attr,
Richard Smith10876ef2013-01-17 01:30:42 +00004843 bool NonInheritable, bool Inheritable,
4844 bool IncludeCXX11Attributes) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00004845 if (Attr.isInvalid())
4846 return;
4847
Richard Smith10876ef2013-01-17 01:30:42 +00004848 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4849 // instead.
4850 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4851 return;
4852
Peter Collingbourneb331b262011-01-21 02:08:45 +00004853 if (NonInheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00004854 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00004855
4856 if (Inheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00004857 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00004858}
4859
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004860/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4861/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004862void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004863 const AttributeList *AttrList,
Richard Smith10876ef2013-01-17 01:30:42 +00004864 bool NonInheritable, bool Inheritable,
4865 bool IncludeCXX11Attributes) {
4866 for (const AttributeList* l = AttrList; l; l = l->getNext())
4867 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable,
4868 IncludeCXX11Attributes);
Rafael Espindolac18086a2010-02-23 22:00:30 +00004869
4870 // GCC accepts
4871 // static int a9 __attribute__((weakref));
4872 // but that looks really pointless. We reject it.
Peter Collingbourneb331b262011-01-21 02:08:45 +00004873 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004874 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindolab3069002013-01-16 23:49:06 +00004875 cast<NamedDecl>(D)->getNameAsString();
4876 D->dropAttr<WeakRefAttr>();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004877 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004878 }
4879}
4880
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004881// Annotation attributes are the only attributes allowed after an access
4882// specifier.
4883bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4884 const AttributeList *AttrList) {
4885 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004886 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004887 handleAnnotateAttr(*this, ASDecl, *l);
4888 } else {
4889 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4890 return true;
4891 }
4892 }
4893
4894 return false;
4895}
4896
John McCall42856de2011-10-01 05:17:03 +00004897/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4898/// contains any decl attributes that we should warn about.
4899static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4900 for ( ; A; A = A->getNext()) {
4901 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smith810ad3e2013-01-29 10:02:16 +00004902 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCall42856de2011-10-01 05:17:03 +00004903 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4904
4905 if (A->getKind() == AttributeList::UnknownAttribute) {
4906 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4907 << A->getName() << A->getRange();
4908 } else {
4909 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4910 << A->getName() << A->getRange();
4911 }
4912 }
4913}
4914
4915/// checkUnusedDeclAttributes - Given a declarator which is not being
4916/// used to build a declaration, complain about any decl attributes
4917/// which might be lying around on it.
4918void Sema::checkUnusedDeclAttributes(Declarator &D) {
4919 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4920 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4921 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4922 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4923}
4924
Ryan Flynn7d470f32009-07-30 03:15:39 +00004925/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004926/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004927NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4928 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004929 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004930 NamedDecl *NewD = 0;
4931 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004932 FunctionDecl *NewFD;
4933 // FIXME: Missing call to CheckFunctionDeclaration().
4934 // FIXME: Mangling?
4935 // FIXME: Is the qualifier info correct?
4936 // FIXME: Is the DeclContext correct?
4937 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4938 Loc, Loc, DeclarationName(II),
4939 FD->getType(), FD->getTypeSourceInfo(),
4940 SC_None, SC_None,
4941 false/*isInlineSpecified*/,
4942 FD->hasPrototype(),
4943 false/*isConstexprSpecified*/);
4944 NewD = NewFD;
4945
4946 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004947 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004948
4949 // Fake up parameter variables; they are declared as if this were
4950 // a typedef.
4951 QualType FDTy = FD->getType();
4952 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4953 SmallVector<ParmVarDecl*, 16> Params;
4954 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4955 AE = FT->arg_type_end(); AI != AE; ++AI) {
4956 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4957 Param->setScopeInfo(0, Params.size());
4958 Params.push_back(Param);
4959 }
David Blaikie9c70e042011-09-21 18:16:56 +00004960 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004961 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004962 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4963 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004964 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004965 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00004966 VD->getStorageClass(),
4967 VD->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00004968 if (VD->getQualifier()) {
4969 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004970 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004971 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004972 }
4973 return NewD;
4974}
4975
James Dennett634962f2012-06-14 21:40:34 +00004976/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004977/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004978void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004979 if (W.getUsed()) return; // only do this once
4980 W.setUsed(true);
4981 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4982 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004983 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004984 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4985 NDId->getName()));
4986 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00004987 WeakTopLevelDecl.push_back(NewD);
4988 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4989 // to insert Decl at TU scope, sorry.
4990 DeclContext *SavedContext = CurContext;
4991 CurContext = Context.getTranslationUnitDecl();
4992 PushOnScopeChains(NewD, S);
4993 CurContext = SavedContext;
4994 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004995 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004996 }
4997}
4998
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004999/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5000/// it, apply them to D. This is a bit tricky because PD can have attributes
5001/// specified in many different places, and we need to find and apply them all.
Peter Collingbourneb331b262011-01-21 02:08:45 +00005002void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
5003 bool NonInheritable, bool Inheritable) {
John McCall6fe02402010-10-27 00:59:00 +00005004 // It's valid to "forward-declare" #pragma weak, in which case we
5005 // have to do this.
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00005006 if (Inheritable) {
5007 LoadExternalWeakUndeclaredIdentifiers();
5008 if (!WeakUndeclaredIdentifiers.empty()) {
5009 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
5010 if (IdentifierInfo *Id = ND->getIdentifier()) {
5011 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5012 = WeakUndeclaredIdentifiers.find(Id);
5013 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
5014 WeakInfo W = I->second;
5015 DeclApplyPragmaWeak(S, ND, W);
5016 WeakUndeclaredIdentifiers[Id] = W;
5017 }
John McCall6fe02402010-10-27 00:59:00 +00005018 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00005019 }
5020 }
5021 }
5022
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005023 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00005024 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourneb331b262011-01-21 02:08:45 +00005025 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005026
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005027 // Walk the declarator structure, applying decl attributes that were in a type
5028 // position to the decl itself. This handles cases like:
5029 // int *__attr__(x)** D;
5030 // when X is a decl attribute.
5031 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5032 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smith10876ef2013-01-17 01:30:42 +00005033 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable,
5034 /*IncludeCXX11Attributes=*/false);
Mike Stumpd3bb5572009-07-24 19:02:52 +00005035
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005036 // Finally, apply any attributes on the decl itself.
5037 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourneb331b262011-01-21 02:08:45 +00005038 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00005039}
John McCall28a6aea2009-11-04 02:18:39 +00005040
John McCall31168b02011-06-15 23:02:42 +00005041/// Is the given declaration allowed to use a forbidden type?
5042static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5043 // Private ivars are always okay. Unfortunately, people don't
5044 // always properly make their ivars private, even in system headers.
5045 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00005046 // Function declarations in sys headers will be marked unavailable.
5047 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5048 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00005049 return false;
5050
5051 // Require it to be declared in a system header.
5052 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5053}
5054
5055/// Handle a delayed forbidden-type diagnostic.
5056static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5057 Decl *decl) {
5058 if (decl && isForbiddenTypeAllowed(S, decl)) {
5059 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5060 "this system declaration uses an unsupported type"));
5061 return;
5062 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00005063 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005064 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00005065 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00005066 // kind of forbidden type messages on unavailable functions.
5067 if (FD->hasAttr<UnavailableAttr>() &&
5068 diag.getForbiddenTypeDiagnostic() ==
5069 diag::err_arc_array_param_no_ownership) {
5070 diag.Triggered = true;
5071 return;
5072 }
5073 }
John McCall31168b02011-06-15 23:02:42 +00005074
5075 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5076 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5077 diag.Triggered = true;
5078}
5079
John McCall2ec85372012-05-07 06:16:41 +00005080void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5081 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00005082 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00005083 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00005084
John McCall2ec85372012-05-07 06:16:41 +00005085 // When delaying diagnostics to run in the context of a parsed
5086 // declaration, we only want to actually emit anything if parsing
5087 // succeeds.
5088 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00005089
John McCall2ec85372012-05-07 06:16:41 +00005090 // We emit all the active diagnostics in this pool or any of its
5091 // parents. In general, we'll get one pool for the decl spec
5092 // and a child pool for each declarator; in a decl group like:
5093 // deprecated_typedef foo, *bar, baz();
5094 // only the declarator pops will be passed decls. This is correct;
5095 // we really do need to consider delayed diagnostics from the decl spec
5096 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00005097 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00005098 do {
John McCall6347b682012-05-07 06:16:58 +00005099 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00005100 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5101 // This const_cast is a bit lame. Really, Triggered should be mutable.
5102 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00005103 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00005104 continue;
5105
John McCallc1465822011-02-14 07:13:47 +00005106 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00005107 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00005108 // Don't bother giving deprecation diagnostics if the decl is invalid.
5109 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00005110 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005111 break;
5112
5113 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00005114 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00005115 break;
John McCall31168b02011-06-15 23:02:42 +00005116
5117 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00005118 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00005119 break;
John McCall86121512010-01-27 03:50:35 +00005120 }
5121 }
John McCall2ec85372012-05-07 06:16:41 +00005122 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00005123}
5124
John McCall6347b682012-05-07 06:16:58 +00005125/// Given a set of delayed diagnostics, re-emit them as if they had
5126/// been delayed in the current context instead of in the given pool.
5127/// Essentially, this just moves them to the current pool.
5128void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5129 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5130 assert(curPool && "re-emitting in undelayed context not supported");
5131 curPool->steal(pool);
5132}
5133
John McCall28a6aea2009-11-04 02:18:39 +00005134static bool isDeclDeprecated(Decl *D) {
5135 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005136 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00005137 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00005138 // A category implicitly has the availability of the interface.
5139 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5140 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00005141 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5142 return false;
5143}
5144
Eli Friedman971bfa12012-08-08 21:52:41 +00005145static void
5146DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5147 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005148 const ObjCInterfaceDecl *UnknownObjCClass,
5149 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedman971bfa12012-08-08 21:52:41 +00005150 DeclarationName Name = D->getDeclName();
5151 if (!Message.empty()) {
5152 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5153 S.Diag(D->getLocation(),
5154 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5155 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005156 if (ObjCPropery)
5157 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5158 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005159 } else if (!UnknownObjCClass) {
5160 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5161 S.Diag(D->getLocation(),
5162 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5163 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005164 if (ObjCPropery)
5165 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5166 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00005167 } else {
5168 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5169 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5170 }
5171}
5172
John McCallb45a1e72010-08-26 02:13:20 +00005173void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00005174 Decl *Ctx) {
5175 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00005176 return;
5177
John McCall86121512010-01-27 03:50:35 +00005178 DD.Triggered = true;
Eli Friedman971bfa12012-08-08 21:52:41 +00005179 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5180 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005181 DD.getUnknownObjCClass(),
5182 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00005183}
5184
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005185void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00005186 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005187 const ObjCInterfaceDecl *UnknownObjCClass,
5188 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00005189 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00005190 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005191 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5192 UnknownObjCClass,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005193 ObjCProperty,
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00005194 Message));
John McCall28a6aea2009-11-04 02:18:39 +00005195 return;
5196 }
5197
5198 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00005199 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00005200 return;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00005201 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00005202}