blob: 6db30bea9d58ef0ed8a04790a2fc00f564b92974 [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"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000020#include "clang/AST/DeclObjC.h"
21#include "clang/AST/Expr.h"
John McCall31168b02011-06-15 23:02:42 +000022#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000023#include "clang/Basic/TargetInfo.h"
John McCall8b0666c2010-08-20 18:27:03 +000024#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000025#include "clang/Sema/DelayedDiagnostic.h"
John McCallf1e8b342011-09-29 07:17:38 +000026#include "clang/Sema/Lookup.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000027#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000028using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000029using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000030
John McCall5fca7ea2011-03-02 12:29:23 +000031/// These constants match the enumerated choices of
32/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000033enum AttributeDeclKind {
John McCall5fca7ea2011-03-02 12:29:23 +000034 ExpectedFunction,
35 ExpectedUnion,
36 ExpectedVariableOrFunction,
37 ExpectedFunctionOrMethod,
38 ExpectedParameter,
John McCall5fca7ea2011-03-02 12:29:23 +000039 ExpectedFunctionMethodOrBlock,
John McCall5fca7ea2011-03-02 12:29:23 +000040 ExpectedFunctionMethodOrParameter,
41 ExpectedClass,
John McCall5fca7ea2011-03-02 12:29:23 +000042 ExpectedVariable,
43 ExpectedMethod,
Caitlin Sadowski63fa6672011-07-28 20:12:35 +000044 ExpectedVariableFunctionOrLabel,
Douglas Gregor5c3cc422012-03-14 16:55:17 +000045 ExpectedFieldOrGlobalVar,
Hans Wennborgd3b01bc2012-06-23 11:51:46 +000046 ExpectedStruct,
47 ExpectedTLSVar
John McCall5fca7ea2011-03-02 12:29:23 +000048};
49
Chris Lattner58418ff2008-06-29 00:16:31 +000050//===----------------------------------------------------------------------===//
51// Helper functions
52//===----------------------------------------------------------------------===//
53
Chandler Carruthff4c4f02011-07-01 23:49:12 +000054static const FunctionType *getFunctionType(const Decl *D,
Ted Kremenek527042b2009-08-14 20:49:40 +000055 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000056 QualType Ty;
Chandler Carruthff4c4f02011-07-01 23:49:12 +000057 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000058 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000059 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000060 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000061 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000062 Ty = decl->getUnderlyingType();
63 else
64 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000065
Chris Lattner2c6fcf52008-06-26 18:38:35 +000066 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000067 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000068 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000069 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000070
John McCall9dd450b2009-09-21 23:43:11 +000071 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000072}
73
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000074// FIXME: We should provide an abstraction around a method or function
75// to provide the following bits of information.
76
Nuno Lopes518e3702009-12-20 23:11:08 +000077/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000078/// type (function or function-typed variable).
Chandler Carruthff4c4f02011-07-01 23:49:12 +000079static bool isFunction(const Decl *D) {
80 return getFunctionType(D, false) != NULL;
Ted Kremenek527042b2009-08-14 20:49:40 +000081}
82
83/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000084/// type (function or function-typed variable) or an Objective-C
85/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000086static bool isFunctionOrMethod(const Decl *D) {
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +000087 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000088}
89
Fariborz Jahanian4447e172009-05-15 23:15:03 +000090/// isFunctionOrMethodOrBlock - Return true if the given decl has function
91/// type (function or function-typed variable) or an Objective-C
92/// method or a block.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000093static bool isFunctionOrMethodOrBlock(const Decl *D) {
94 if (isFunctionOrMethod(D))
Fariborz Jahanian4447e172009-05-15 23:15:03 +000095 return true;
96 // check for block is more involved.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000097 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000098 QualType Ty = V->getType();
99 return Ty->isBlockPointerType();
100 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000101 return isa<BlockDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000102}
103
John McCall3882ace2011-01-05 12:14:39 +0000104/// Return true if the given decl has a declarator that should have
105/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000106static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +0000107 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000108 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
109 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +0000110}
111
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000112/// hasFunctionProto - Return true if the given decl has a argument
113/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000114/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000115static bool hasFunctionProto(const Decl *D) {
116 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000117 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000118 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000119 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000120 return true;
121 }
122}
123
124/// getFunctionOrMethodNumArgs - Return number of function or method
125/// arguments. It is an error to call this on a K&R function (use
126/// hasFunctionProto first).
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000127static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
128 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000129 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000130 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000131 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000132 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000133}
134
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000135static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
136 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000137 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000138 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000139 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000140
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000141 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000142}
143
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000144static QualType getFunctionOrMethodResultType(const Decl *D) {
145 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000146 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000147 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000148}
149
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000150static bool isFunctionOrMethodVariadic(const Decl *D) {
151 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000152 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000153 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000154 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000155 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000156 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000157 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000158 }
159}
160
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000161static bool isInstanceMethod(const Decl *D) {
162 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000163 return MethodDecl->isInstance();
164 return false;
165}
166
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000167static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000168 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000169 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000170 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000171
John McCall96fa4842010-05-17 21:00:27 +0000172 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
173 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000174 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000175
John McCall96fa4842010-05-17 21:00:27 +0000176 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000177
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000178 // FIXME: Should we walk the chain of classes?
179 return ClsName == &Ctx.Idents.get("NSString") ||
180 ClsName == &Ctx.Idents.get("NSMutableString");
181}
182
Daniel Dunbar980c6692008-09-26 03:32:58 +0000183static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000184 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000185 if (!PT)
186 return false;
187
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000188 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000189 if (!RT)
190 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000191
Daniel Dunbar980c6692008-09-26 03:32:58 +0000192 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000193 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000194 return false;
195
196 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
197}
198
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000199/// \brief Check if the attribute has exactly as many args as Num. May
200/// output an error.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000201static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
202 unsigned int Num) {
203 if (Attr.getNumArgs() != Num) {
204 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
205 return false;
206 }
207
208 return true;
209}
210
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000211
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000212/// \brief Check if the attribute has at least as many args as Num. May
213/// output an error.
214static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
215 unsigned int Num) {
216 if (Attr.getNumArgs() < Num) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000217 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
218 return false;
219 }
220
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000221 return true;
222}
223
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000224/// \brief Check if IdxExpr is a valid argument index for a function or
225/// instance method D. May output an error.
226///
227/// \returns true if IdxExpr is a valid index.
228static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
229 StringRef AttrName,
230 SourceLocation AttrLoc,
231 unsigned AttrArgNum,
232 const Expr *IdxExpr,
233 uint64_t &Idx)
234{
235 assert(isFunctionOrMethod(D) && hasFunctionProto(D));
236
237 // In C++ the implicit 'this' function parameter also counts.
238 // Parameters are counted from one.
239 const bool HasImplicitThisParam = isInstanceMethod(D);
240 const unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
241 const unsigned FirstIdx = 1;
242
243 llvm::APSInt IdxInt;
244 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
245 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
246 S.Diag(AttrLoc, diag::err_attribute_argument_n_not_int)
247 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
248 return false;
249 }
250
251 Idx = IdxInt.getLimitedValue();
252 if (Idx < FirstIdx || (!isFunctionOrMethodVariadic(D) && Idx > NumArgs)) {
253 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
254 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
255 return false;
256 }
257 Idx--; // Convert to zero-based.
258 if (HasImplicitThisParam) {
259 if (Idx == 0) {
260 S.Diag(AttrLoc,
261 diag::err_attribute_invalid_implicit_this_argument)
262 << AttrName << IdxExpr->getSourceRange();
263 return false;
264 }
265 --Idx;
266 }
267
268 return true;
269}
270
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000271///
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000272/// \brief Check if passed in Decl is a field or potentially shared global var
273/// \return true if the Decl is a field or potentially shared global variable
274///
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000275static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000276 if (isa<FieldDecl>(D))
277 return true;
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000278 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000279 return (vd->hasGlobalStorage() && !(vd->isThreadSpecified()));
280
281 return false;
282}
283
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000284/// \brief Check if the passed-in expression is of type int or bool.
285static bool isIntOrBool(Expr *Exp) {
286 QualType QT = Exp->getType();
287 return QT->isBooleanType() || QT->isIntegerType();
288}
289
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000290
291// Check to see if the type is a smart pointer of some kind. We assume
292// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000293static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
294 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
295 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
296 if (Res1.first == Res1.second)
297 return false;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000298
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000299 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
300 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
301 if (Res2.first == Res2.second)
302 return false;
303
304 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000305}
306
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000307/// \brief Check if passed in Decl is a pointer type.
308/// Note that this function may produce an error message.
309/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000310static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
311 const AttributeList &Attr) {
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000312 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000313 QualType QT = vd->getType();
Benjamin Kramer3c05b7c2011-08-02 04:50:49 +0000314 if (QT->isAnyPointerType())
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000315 return true;
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000316
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000317 if (const RecordType *RT = QT->getAs<RecordType>()) {
318 // If it's an incomplete type, it could be a smart pointer; skip it.
319 // (We don't want to force template instantiation if we can avoid it,
320 // since that would alter the order in which templates are instantiated.)
321 if (RT->isIncompleteType())
322 return true;
323
324 if (threadSafetyCheckIsSmartPointer(S, RT))
325 return true;
326 }
DeLesley Hutchinse09be232012-04-23 18:39:55 +0000327
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000328 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000329 << Attr.getName()->getName() << QT;
330 } else {
331 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
332 << Attr.getName();
333 }
334 return false;
335}
336
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000337/// \brief Checks that the passed in QualType either is of RecordType or points
338/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000339static const RecordType *getRecordType(QualType QT) {
340 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000341 return RT;
Benjamin Kramer56b675f2011-08-19 04:18:11 +0000342
343 // Now check if we point to record type.
344 if (const PointerType *PT = QT->getAs<PointerType>())
345 return PT->getPointeeType()->getAs<RecordType>();
346
347 return 0;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000348}
349
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000350
Jordy Rose740b0c22012-05-08 03:27:22 +0000351static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
352 CXXBasePath &Path, void *Unused) {
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000353 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
354 if (RT->getDecl()->getAttr<LockableAttr>())
355 return true;
356 return false;
357}
358
359
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000360/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000361/// resolves to a lockable object.
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000362static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
363 QualType Ty) {
364 const RecordType *RT = getRecordType(Ty);
Michael Hana9171bc2012-08-03 17:40:43 +0000365
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000366 // Warn if could not get record type for this argument.
Benjamin Kramer2667afa2011-09-03 03:30:59 +0000367 if (!RT) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000368 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000369 << Attr.getName() << Ty.getAsString();
370 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000371 }
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000372
Michael Hana9171bc2012-08-03 17:40:43 +0000373 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins3509f292012-02-16 17:15:51 +0000374 if (RT->isIncompleteType())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000375 return;
DeLesley Hutchins90ff4682012-05-02 22:18:42 +0000376
377 // Allow smart pointers to be used as lockable objects.
378 // FIXME -- Check the type that the smart pointer points to.
379 if (threadSafetyCheckIsSmartPointer(S, RT))
380 return;
381
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000382 // Check if the type is lockable.
383 RecordDecl *RD = RT->getDecl();
384 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000385 return;
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000386
387 // Else check if any base classes are lockable.
388 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
389 CXXBasePaths BPaths(false, false);
390 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
391 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000392 }
DeLesley Hutchins5ff430c2012-05-04 16:28:38 +0000393
394 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
395 << Attr.getName() << Ty.getAsString();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000396}
397
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000398/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000399/// from Sidx, resolve to a lockable object.
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000400/// \param Sidx The attribute argument index to start checking with.
401/// \param ParamIdxOk Whether an argument can be indexing into a function
402/// parameter list.
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000403static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000404 const AttributeList &Attr,
405 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000406 int Sidx = 0,
407 bool ParamIdxOk = false) {
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000408 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000409 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000410
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000411 if (ArgExp->isTypeDependent()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000412 // FIXME -- need to check this again on template instantiation
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000413 Args.push_back(ArgExp);
414 continue;
415 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000416
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000417 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000418 if (StrLit->getLength() == 0 ||
419 StrLit->getString() == StringRef("*")) {
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000420 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchinsa5a00e82012-09-07 17:34:53 +0000421 // Treat "*" as the universal lock.
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000422 Args.push_back(ArgExp);
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000423 continue;
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000424 }
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000425
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000426 // We allow constant strings to be used as a placeholder for expressions
427 // that are not valid C++ syntax, but warn that they are ignored.
428 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
429 Attr.getName();
DeLesley Hutchins3c3d57b2012-08-31 21:57:32 +0000430 Args.push_back(ArgExp);
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000431 continue;
432 }
433
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000434 QualType ArgTy = ArgExp->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000435
DeLesley Hutchins70b5e8e2012-04-23 16:45:01 +0000436 // A pointer to member expression of the form &MyClass::mu is treated
437 // specially -- we need to look at the type of the member.
438 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
439 if (UOp->getOpcode() == UO_AddrOf)
440 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
441 if (DRE->getDecl()->isCXXInstanceMember())
442 ArgTy = DRE->getDecl()->getType();
443
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000444 // First see if we can just cast to record type, or point to record type.
445 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000446
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000447 // Now check if we index into a record type function param.
448 if(!RT && ParamIdxOk) {
449 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000450 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
451 if(FD && IL) {
452 unsigned int NumParams = FD->getNumParams();
453 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000454 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
455 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
456 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000457 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
458 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000459 continue;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000460 }
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000461 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000462 }
463 }
464
DeLesley Hutchins481d5ab2012-04-06 20:02:30 +0000465 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000466
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000467 Args.push_back(ArgExp);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000468 }
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000469}
470
Chris Lattner58418ff2008-06-29 00:16:31 +0000471//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000472// Attribute Implementations
473//===----------------------------------------------------------------------===//
474
Daniel Dunbar032db472008-07-31 22:40:48 +0000475// FIXME: All this manual attribute parsing code is gross. At the
476// least add some helper functions to check most argument patterns (#
477// and types of args).
478
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000479enum ThreadAttributeDeclKind {
480 ThreadExpectedFieldOrGlobalVar,
481 ThreadExpectedFunctionOrMethod,
482 ThreadExpectedClassOrStruct
483};
484
Michael Hana9171bc2012-08-03 17:40:43 +0000485static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000486 const AttributeList &Attr) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000487 assert(!Attr.isInvalid());
488
489 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Han3be3b442012-07-23 18:48:41 +0000490 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000491
492 // D must be either a member field or global (potentially shared) variable.
493 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000494 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
495 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000496 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000497 }
498
Michael Han3be3b442012-07-23 18:48:41 +0000499 return true;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000500}
501
Michael Han3be3b442012-07-23 18:48:41 +0000502static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
503 if (!checkGuardedVarAttrCommon(S, D, Attr))
504 return;
Michael Hana9171bc2012-08-03 17:40:43 +0000505
Michael Han3be3b442012-07-23 18:48:41 +0000506 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context));
507}
508
Michael Hana9171bc2012-08-03 17:40:43 +0000509static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000510 const AttributeList &Attr) {
511 if (!checkGuardedVarAttrCommon(S, D, Attr))
512 return;
513
514 if (!threadSafetyCheckIsPointer(S, D, Attr))
515 return;
516
517 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context));
518}
519
Michael Hana9171bc2012-08-03 17:40:43 +0000520static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
521 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000522 Expr* &Arg) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000523 assert(!Attr.isInvalid());
524
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000525 if (!checkAttributeNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000526 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000527
528 // D must be either a member field or global (potentially shared) variable.
529 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000530 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
531 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000532 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000533 }
534
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000535 SmallVector<Expr*, 1> Args;
536 // check that all arguments are lockable objects
537 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
538 unsigned Size = Args.size();
539 if (Size != 1)
Michael Han3be3b442012-07-23 18:48:41 +0000540 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000541
Michael Han3be3b442012-07-23 18:48:41 +0000542 Arg = Args[0];
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000543
Michael Han3be3b442012-07-23 18:48:41 +0000544 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000545}
546
Michael Han3be3b442012-07-23 18:48:41 +0000547static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
548 Expr *Arg = 0;
549 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
550 return;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000551
Michael Han3be3b442012-07-23 18:48:41 +0000552 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
553}
554
Michael Hana9171bc2012-08-03 17:40:43 +0000555static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000556 const AttributeList &Attr) {
557 Expr *Arg = 0;
558 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
559 return;
560
561 if (!threadSafetyCheckIsPointer(S, D, Attr))
562 return;
563
564 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
565 S.Context, Arg));
566}
567
Michael Hana9171bc2012-08-03 17:40:43 +0000568static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000569 const AttributeList &Attr) {
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000570 assert(!Attr.isInvalid());
571
572 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Han3be3b442012-07-23 18:48:41 +0000573 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000574
Caitlin Sadowski086fb952011-09-16 00:35:54 +0000575 // FIXME: Lockable structs for C code.
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000576 if (!isa<CXXRecordDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000577 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
578 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Han3be3b442012-07-23 18:48:41 +0000579 return false;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000580 }
581
Michael Han3be3b442012-07-23 18:48:41 +0000582 return true;
583}
584
585static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
586 if (!checkLockableAttrCommon(S, D, Attr))
587 return;
588
589 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
590}
591
Michael Hana9171bc2012-08-03 17:40:43 +0000592static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000593 const AttributeList &Attr) {
594 if (!checkLockableAttrCommon(S, D, Attr))
595 return;
596
597 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000598}
599
600static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
601 const AttributeList &Attr) {
602 assert(!Attr.isInvalid());
603
604 if (!checkAttributeNumArgs(S, Attr, 0))
605 return;
606
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000607 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000608 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
609 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000610 return;
611 }
612
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000613 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +0000614 S.Context));
615}
616
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000617static void handleNoAddressSafetyAttr(Sema &S, Decl *D,
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000618 const AttributeList &Attr) {
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000619 assert(!Attr.isInvalid());
620
621 if (!checkAttributeNumArgs(S, Attr, 0))
622 return;
623
624 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
625 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
626 << Attr.getName() << ExpectedFunctionOrMethod;
627 return;
628 }
629
630 D->addAttr(::new (S.Context) NoAddressSafetyAnalysisAttr(Attr.getRange(),
Nick Lewyckycfb45172012-07-24 01:37:23 +0000631 S.Context));
Kostya Serebryany588d6ab2012-01-24 19:25:38 +0000632}
633
Michael Hana9171bc2012-08-03 17:40:43 +0000634static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
635 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000636 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000637 assert(!Attr.isInvalid());
638
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000639 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000640 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000641
642 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000643 ValueDecl *VD = dyn_cast<ValueDecl>(D);
644 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000645 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
646 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Han3be3b442012-07-23 18:48:41 +0000647 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000648 }
649
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000650 // Check that this attribute only applies to lockable types.
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000651 QualType QT = VD->getType();
652 if (!QT->isDependentType()) {
653 const RecordType *RT = getRecordType(QT);
654 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000655 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Han3be3b442012-07-23 18:48:41 +0000656 << Attr.getName();
657 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000658 }
659 }
660
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000661 // Check that all arguments are lockable objects.
662 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Han3be3b442012-07-23 18:48:41 +0000663 if (Args.size() == 0)
664 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000665
Michael Han3be3b442012-07-23 18:48:41 +0000666 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000667}
668
Michael Hana9171bc2012-08-03 17:40:43 +0000669static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000670 const AttributeList &Attr) {
671 SmallVector<Expr*, 1> Args;
672 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
673 return;
674
675 Expr **StartArg = &Args[0];
676 D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +0000677 StartArg, Args.size()));
Michael Han3be3b442012-07-23 18:48:41 +0000678}
679
Michael Hana9171bc2012-08-03 17:40:43 +0000680static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000681 const AttributeList &Attr) {
682 SmallVector<Expr*, 1> Args;
683 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
684 return;
685
686 Expr **StartArg = &Args[0];
687 D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +0000688 StartArg, Args.size()));
Michael Han3be3b442012-07-23 18:48:41 +0000689}
690
Michael Hana9171bc2012-08-03 17:40:43 +0000691static bool checkLockFunAttrCommon(Sema &S, Decl *D,
692 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000693 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000694 assert(!Attr.isInvalid());
695
696 // zero or more arguments ok
697
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000698 // check that the attribute is applied to a function
699 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000700 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
701 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000702 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000703 }
704
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000705 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000706 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000707
Michael Han3be3b442012-07-23 18:48:41 +0000708 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000709}
710
Michael Hana9171bc2012-08-03 17:40:43 +0000711static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000712 const AttributeList &Attr) {
713 SmallVector<Expr*, 1> Args;
714 if (!checkLockFunAttrCommon(S, D, Attr, Args))
715 return;
716
717 unsigned Size = Args.size();
718 Expr **StartArg = Size == 0 ? 0 : &Args[0];
719 D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(),
Michael Hana9171bc2012-08-03 17:40:43 +0000720 S.Context,
Michael Han3be3b442012-07-23 18:48:41 +0000721 StartArg, Size));
722}
723
Michael Hana9171bc2012-08-03 17:40:43 +0000724static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000725 const AttributeList &Attr) {
726 SmallVector<Expr*, 1> Args;
727 if (!checkLockFunAttrCommon(S, D, Attr, Args))
728 return;
729
730 unsigned Size = Args.size();
731 Expr **StartArg = Size == 0 ? 0 : &Args[0];
732 D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(),
Michael Hana9171bc2012-08-03 17:40:43 +0000733 S.Context,
Michael Han3be3b442012-07-23 18:48:41 +0000734 StartArg, Size));
735}
736
Michael Hana9171bc2012-08-03 17:40:43 +0000737static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
738 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000739 SmallVector<Expr*, 2> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000740 assert(!Attr.isInvalid());
741
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000742 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000743 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000744
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000745 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000746 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
747 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000748 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000749 }
750
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000751 if (!isIntOrBool(Attr.getArg(0))) {
752 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
Michael Han3be3b442012-07-23 18:48:41 +0000753 << Attr.getName();
754 return false;
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000755 }
756
757 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000758 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000759
Michael Han3be3b442012-07-23 18:48:41 +0000760 return true;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000761}
762
Michael Hana9171bc2012-08-03 17:40:43 +0000763static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000764 const AttributeList &Attr) {
765 SmallVector<Expr*, 2> Args;
766 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
767 return;
768
769 unsigned Size = Args.size();
770 Expr **StartArg = Size == 0 ? 0 : &Args[0];
771 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(),
Michael Hana9171bc2012-08-03 17:40:43 +0000772 S.Context,
773 Attr.getArg(0),
Michael Han3be3b442012-07-23 18:48:41 +0000774 StartArg, Size));
775}
776
Michael Hana9171bc2012-08-03 17:40:43 +0000777static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000778 const AttributeList &Attr) {
779 SmallVector<Expr*, 2> Args;
780 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
781 return;
782
783 unsigned Size = Args.size();
784 Expr **StartArg = Size == 0 ? 0 : &Args[0];
785 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(),
Michael Hana9171bc2012-08-03 17:40:43 +0000786 S.Context,
787 Attr.getArg(0),
Michael Han3be3b442012-07-23 18:48:41 +0000788 StartArg, Size));
789}
790
Michael Hana9171bc2012-08-03 17:40:43 +0000791static bool checkLocksRequiredCommon(Sema &S, Decl *D,
792 const AttributeList &Attr,
Michael Han3be3b442012-07-23 18:48:41 +0000793 SmallVector<Expr*, 1> &Args) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000794 assert(!Attr.isInvalid());
795
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000796 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Han3be3b442012-07-23 18:48:41 +0000797 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000798
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000799 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000800 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
801 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Han3be3b442012-07-23 18:48:41 +0000802 return false;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000803 }
804
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000805 // check that all arguments are lockable objects
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000806 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Michael Han3be3b442012-07-23 18:48:41 +0000807 if (Args.size() == 0)
808 return false;
Michael Hana9171bc2012-08-03 17:40:43 +0000809
Michael Han3be3b442012-07-23 18:48:41 +0000810 return true;
811}
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000812
Michael Hana9171bc2012-08-03 17:40:43 +0000813static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000814 const AttributeList &Attr) {
815 SmallVector<Expr*, 1> Args;
816 if (!checkLocksRequiredCommon(S, D, Attr, Args))
817 return;
818
819 Expr **StartArg = &Args[0];
820 D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(),
Michael Hana9171bc2012-08-03 17:40:43 +0000821 S.Context,
822 StartArg,
Michael Han3be3b442012-07-23 18:48:41 +0000823 Args.size()));
824}
825
Michael Hana9171bc2012-08-03 17:40:43 +0000826static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Han3be3b442012-07-23 18:48:41 +0000827 const AttributeList &Attr) {
828 SmallVector<Expr*, 1> Args;
829 if (!checkLocksRequiredCommon(S, D, Attr, Args))
830 return;
831
832 Expr **StartArg = &Args[0];
833 D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(),
Michael Hana9171bc2012-08-03 17:40:43 +0000834 S.Context,
835 StartArg,
Michael Han3be3b442012-07-23 18:48:41 +0000836 Args.size()));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000837}
838
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000839static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000840 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000841 assert(!Attr.isInvalid());
842
843 // zero or more arguments ok
844
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000845 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000846 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
847 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000848 return;
849 }
850
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000851 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000852 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000853 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000854 unsigned Size = Args.size();
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000855 Expr **StartArg = Size == 0 ? 0 : &Args[0];
856
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000857 D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000858 StartArg, Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000859}
860
861static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000862 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000863 assert(!Attr.isInvalid());
864
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000865 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000866 return;
867
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000868 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000869 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
870 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000871 return;
872 }
873
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000874 // check that the argument is lockable object
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000875 SmallVector<Expr*, 1> Args;
876 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
877 unsigned Size = Args.size();
878 if (Size == 0)
879 return;
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000880
DeLesley Hutchinsd96b46a2012-05-02 17:38:37 +0000881 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context,
882 Args[0]));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000883}
884
885static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000886 const AttributeList &Attr) {
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000887 assert(!Attr.isInvalid());
888
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000889 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000890 return;
891
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000892 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins312e7422012-06-19 23:25:19 +0000893 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
894 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000895 return;
896 }
897
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000898 // check that all arguments are lockable objects
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000899 SmallVector<Expr*, 1> Args;
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000900 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000901 unsigned Size = Args.size();
DeLesley Hutchins8d11c792012-04-19 16:10:44 +0000902 if (Size == 0)
903 return;
904 Expr **StartArg = &Args[0];
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000905
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000906 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context,
Caitlin Sadowskiafbbd8e2011-08-23 18:46:34 +0000907 StartArg, Size));
Caitlin Sadowski63fa6672011-07-28 20:12:35 +0000908}
909
910
Chandler Carruthedc2c642011-07-02 00:01:44 +0000911static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
912 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000913 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000914 if (tDecl == 0) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000915 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000916 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000917 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000918
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000919 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor758a8692009-06-17 21:51:59 +0000920
921 Expr *sizeExpr;
922
923 // Special case where the argument is a template id.
924 if (Attr.getParameterName()) {
John McCalle66edc12009-11-24 19:00:30 +0000925 CXXScopeSpec SS;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000926 SourceLocation TemplateKWLoc;
John McCalle66edc12009-11-24 19:00:30 +0000927 UnqualifiedId id;
928 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Michael Hana9171bc2012-08-03 17:40:43 +0000929
Abramo Bagnara7945c982012-01-27 09:46:47 +0000930 ExprResult Size = S.ActOnIdExpression(scope, SS, TemplateKWLoc, id,
931 false, false);
Douglas Gregor39c02722011-06-15 16:02:29 +0000932 if (Size.isInvalid())
933 return;
Michael Hana9171bc2012-08-03 17:40:43 +0000934
Douglas Gregor39c02722011-06-15 16:02:29 +0000935 sizeExpr = Size.get();
Douglas Gregor758a8692009-06-17 21:51:59 +0000936 } else {
937 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000938 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor758a8692009-06-17 21:51:59 +0000939 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000940
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000941 sizeExpr = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000942 }
Douglas Gregor758a8692009-06-17 21:51:59 +0000943
944 // Instantiate/Install the vector type, and let Sema build the type for us.
945 // This will run the reguired checks.
John McCallb268a282010-08-23 23:25:46 +0000946 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor758a8692009-06-17 21:51:59 +0000947 if (!T.isNull()) {
John McCall703a3f82009-10-24 08:00:42 +0000948 // FIXME: preserve the old source info.
John McCallbcd03502009-12-07 02:54:59 +0000949 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpd3bb5572009-07-24 19:02:52 +0000950
Douglas Gregor758a8692009-06-17 21:51:59 +0000951 // Remember this typedef decl, we will need it later for diagnostics.
952 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000953 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000954}
955
Chandler Carruthedc2c642011-07-02 00:01:44 +0000956static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000957 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000958 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000959 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000960
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000961 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000962 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000963 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000964 // If the alignment is less than or equal to 8 bits, the packed attribute
965 // has no effect.
966 if (!FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000967 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +0000968 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000969 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000970 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000971 FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000972 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000973 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000974}
975
Chandler Carruthedc2c642011-07-02 00:01:44 +0000976static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman9ee2d0472012-10-12 23:29:20 +0000977 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
978 RD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +0000979 else
980 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
981}
982
Chandler Carruthedc2c642011-07-02 00:01:44 +0000983static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000984 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000985 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000986 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000987
Ted Kremenek1f672822010-02-18 03:08:58 +0000988 // The IBAction attributes only apply to instance methods.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000989 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek1f672822010-02-18 03:08:58 +0000990 if (MD->isInstanceMethod()) {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000991 D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context));
Ted Kremenek1f672822010-02-18 03:08:58 +0000992 return;
993 }
994
Ted Kremenekd68ec812011-02-04 06:54:16 +0000995 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek1f672822010-02-18 03:08:58 +0000996}
997
Ted Kremenek7fd17232011-09-29 07:02:25 +0000998static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
999 // The IBOutlet/IBOutletCollection attributes only apply to instance
1000 // variables or properties of Objective-C classes. The outlet must also
1001 // have an object reference type.
1002 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1003 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001004 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001005 << Attr.getName() << VD->getType() << 0;
1006 return false;
1007 }
1008 }
1009 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1010 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001011 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001012 << Attr.getName() << PD->getType() << 1;
1013 return false;
1014 }
1015 }
1016 else {
1017 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1018 return false;
1019 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001020
Ted Kremenek7fd17232011-09-29 07:02:25 +00001021 return true;
1022}
1023
Chandler Carruthedc2c642011-07-02 00:01:44 +00001024static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek1f672822010-02-18 03:08:58 +00001025 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001026 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek1f672822010-02-18 03:08:58 +00001027 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001028
1029 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001030 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001031
Ted Kremenek7fd17232011-09-29 07:02:25 +00001032 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001033}
1034
Chandler Carruthedc2c642011-07-02 00:01:44 +00001035static void handleIBOutletCollection(Sema &S, Decl *D,
1036 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001037
1038 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001039 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001040 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1041 return;
1042 }
1043
Ted Kremenek7fd17232011-09-29 07:02:25 +00001044 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001045 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001046
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001047 IdentifierInfo *II = Attr.getParameterName();
1048 if (!II)
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001049 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian798f8322010-08-17 21:39:27 +00001050
John McCallba7bf592010-08-24 05:47:05 +00001051 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001052 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001053 if (!TypeRep) {
1054 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1055 return;
1056 }
John McCallba7bf592010-08-24 05:47:05 +00001057 QualType QT = TypeRep.get();
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001058 // Diagnose use of non-object type in iboutletcollection attribute.
1059 // FIXME. Gnu attribute extension ignores use of builtin types in
1060 // attributes. So, __attribute__((iboutletcollection(char))) will be
1061 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001062 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001063 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1064 return;
1065 }
Argyrios Kyrtzidis8db67df2011-09-13 18:41:59 +00001066 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context,
1067 QT, Attr.getParameterLoc()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001068}
1069
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001070static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001071 if (const RecordType *UT = T->getAsUnionType())
1072 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1073 RecordDecl *UD = UT->getDecl();
1074 for (RecordDecl::field_iterator it = UD->field_begin(),
1075 itend = UD->field_end(); it != itend; ++it) {
1076 QualType QT = it->getType();
1077 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1078 T = QT;
1079 return;
1080 }
1081 }
1082 }
1083}
1084
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001085static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopese881ce22012-06-18 16:39:04 +00001086 if (!isFunctionOrMethod(D)) {
1087 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1088 << "alloc_size" << ExpectedFunctionOrMethod;
1089 return;
1090 }
1091
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001092 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1093 return;
1094
1095 // In C++ the implicit 'this' function parameter also counts, and they are
1096 // counted from one.
1097 bool HasImplicitThisParam = isInstanceMethod(D);
1098 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
1099
1100 SmallVector<unsigned, 8> SizeArgs;
1101
1102 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1103 E = Attr.arg_end(); I!=E; ++I) {
1104 // The argument must be an integer constant expression.
1105 Expr *Ex = *I;
1106 llvm::APSInt ArgNum;
1107 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1108 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
1109 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1110 << "alloc_size" << Ex->getSourceRange();
1111 return;
1112 }
1113
1114 uint64_t x = ArgNum.getZExtValue();
1115
1116 if (x < 1 || x > NumArgs) {
1117 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1118 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
1119 return;
1120 }
1121
1122 --x;
1123 if (HasImplicitThisParam) {
1124 if (x == 0) {
1125 S.Diag(Attr.getLoc(),
1126 diag::err_attribute_invalid_implicit_this_argument)
1127 << "alloc_size" << Ex->getSourceRange();
1128 return;
1129 }
1130 --x;
1131 }
1132
1133 // check if the function argument is of an integer type
1134 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
1135 if (!T->isIntegerType()) {
1136 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1137 << "alloc_size" << Ex->getSourceRange();
1138 return;
1139 }
1140
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001141 SizeArgs.push_back(x);
1142 }
1143
1144 // check if the function returns a pointer
1145 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1146 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1147 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
1148 }
1149
Nuno Lopese44e93a2012-06-18 16:27:56 +00001150 D->addAttr(::new (S.Context) AllocSizeAttr(Attr.getRange(), S.Context,
1151 SizeArgs.data(), SizeArgs.size()));
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001152}
1153
Chandler Carruthedc2c642011-07-02 00:01:44 +00001154static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001155 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1156 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001157 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001158 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001159 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001160 return;
1161 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001162
Chandler Carruth743682b2010-11-16 08:35:43 +00001163 // In C++ the implicit 'this' function parameter also counts, and they are
1164 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001165 bool HasImplicitThisParam = isInstanceMethod(D);
1166 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001167
1168 // The nonnull attribute only applies to pointers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001169 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001170
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001171 for (AttributeList::arg_iterator I=Attr.arg_begin(),
1172 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001173
1174
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001175 // The argument must be an integer constant expression.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001176 Expr *Ex = *I;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001177 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001178 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1179 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001180 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1181 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001182 return;
1183 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001184
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001185 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001186
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001187 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001188 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +00001189 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001190 return;
1191 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001192
Ted Kremenek5224e6a2008-07-21 22:09:15 +00001193 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +00001194 if (HasImplicitThisParam) {
1195 if (x == 0) {
1196 S.Diag(Attr.getLoc(),
1197 diag::err_attribute_invalid_implicit_this_argument)
1198 << "nonnull" << Ex->getSourceRange();
1199 return;
1200 }
1201 --x;
1202 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001203
1204 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001205 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001206 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001207
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001208 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001209 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +00001210 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +00001211 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001212 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001213 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001214
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001215 NonNullArgs.push_back(x);
1216 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001217
1218 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1219 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001220 if (NonNullArgs.empty()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001221 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
1222 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001223 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001224 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbar70e3eba2008-10-19 02:04:16 +00001225 NonNullArgs.push_back(I);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001226 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001227
Ted Kremenek22813f42010-10-21 18:49:36 +00001228 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001229 if (NonNullArgs.empty()) {
1230 // Warn the trivial case only if attribute is not coming from a
1231 // macro instantiation.
1232 if (Attr.getLoc().isFileID())
1233 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001234 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001235 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001236 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001237
1238 unsigned* start = &NonNullArgs[0];
1239 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001240 llvm::array_pod_sort(start, start + size);
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001241 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001242 size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001243}
1244
Chandler Carruthedc2c642011-07-02 00:01:44 +00001245static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001246 // This attribute must be applied to a function declaration.
1247 // The first argument to the attribute must be a string,
1248 // the name of the resource, for example "malloc".
1249 // The following arguments must be argument indexes, the arguments must be
1250 // of integer type for Returns, otherwise of pointer type.
1251 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001252 // after being held. free() should be __attribute((ownership_takes)), whereas
1253 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001254
1255 if (!AL.getParameterName()) {
1256 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1257 << AL.getName()->getName() << 1;
1258 return;
1259 }
1260 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001261 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001262 switch (AL.getKind()) {
1263 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001264 K = OwnershipAttr::Takes;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001265 if (AL.getNumArgs() < 1) {
1266 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1267 return;
1268 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001269 break;
1270 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001271 K = OwnershipAttr::Holds;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001272 if (AL.getNumArgs() < 1) {
1273 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1274 return;
1275 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001276 break;
1277 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001278 K = OwnershipAttr::Returns;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001279 if (AL.getNumArgs() > 1) {
1280 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1281 << AL.getNumArgs() + 1;
1282 return;
1283 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001284 break;
1285 default:
1286 // This should never happen given how we are called.
1287 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +00001288 }
1289
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001290 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001291 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1292 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001293 return;
1294 }
1295
Chandler Carruth743682b2010-11-16 08:35:43 +00001296 // In C++ the implicit 'this' function parameter also counts, and they are
1297 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001298 bool HasImplicitThisParam = isInstanceMethod(D);
1299 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001300
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001301 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001302
1303 // Normalize the argument, __foo__ becomes foo.
1304 if (Module.startswith("__") && Module.endswith("__"))
1305 Module = Module.substr(2, Module.size() - 4);
1306
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001307 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001308
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001309 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1310 ++I) {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001311
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001312 Expr *IdxExpr = *I;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001313 llvm::APSInt ArgNum(32);
1314 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1315 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1316 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1317 << AL.getName()->getName() << IdxExpr->getSourceRange();
1318 continue;
1319 }
1320
1321 unsigned x = (unsigned) ArgNum.getZExtValue();
1322
1323 if (x > NumArgs || x < 1) {
1324 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1325 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1326 continue;
1327 }
1328 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +00001329 if (HasImplicitThisParam) {
1330 if (x == 0) {
1331 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1332 << "ownership" << IdxExpr->getSourceRange();
1333 return;
1334 }
1335 --x;
1336 }
1337
Ted Kremenekd21139a2010-07-31 01:52:11 +00001338 switch (K) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001339 case OwnershipAttr::Takes:
1340 case OwnershipAttr::Holds: {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001341 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001342 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001343 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1344 // FIXME: Should also highlight argument in decl.
1345 S.Diag(AL.getLoc(), diag::err_ownership_type)
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001346 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekd21139a2010-07-31 01:52:11 +00001347 << "pointer"
1348 << IdxExpr->getSourceRange();
1349 continue;
1350 }
1351 break;
1352 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001353 case OwnershipAttr::Returns: {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001354 if (AL.getNumArgs() > 1) {
1355 // Is the function argument an integer type?
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001356 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001357 llvm::APSInt ArgNum(32);
1358 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1359 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1360 S.Diag(AL.getLoc(), diag::err_ownership_type)
1361 << "ownership_returns" << "integer"
1362 << IdxExpr->getSourceRange();
1363 return;
1364 }
1365 }
1366 break;
1367 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001368 } // switch
1369
1370 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001371 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001372 i = D->specific_attr_begin<OwnershipAttr>(),
1373 e = D->specific_attr_end<OwnershipAttr>();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001374 i != e; ++i) {
1375 if ((*i)->getOwnKind() != K) {
1376 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1377 I!=E; ++I) {
1378 if (x == *I) {
1379 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1380 << AL.getName()->getName() << "ownership_*";
Ted Kremenekd21139a2010-07-31 01:52:11 +00001381 }
1382 }
1383 }
1384 }
1385 OwnershipArgs.push_back(x);
1386 }
1387
1388 unsigned* start = OwnershipArgs.data();
1389 unsigned size = OwnershipArgs.size();
1390 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001391
1392 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1393 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1394 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001395 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001396
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001397 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001398 start, size));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001399}
1400
John McCall7a198ce2011-02-08 22:35:49 +00001401/// Whether this declaration has internal linkage for the purposes of
1402/// things that want to complain about things not have internal linkage.
1403static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1404 switch (D->getLinkage()) {
1405 case NoLinkage:
1406 case InternalLinkage:
1407 return true;
1408
1409 // Template instantiations that go from external to unique-external
1410 // shouldn't get diagnosed.
1411 case UniqueExternalLinkage:
1412 return true;
1413
1414 case ExternalLinkage:
1415 return false;
1416 }
1417 llvm_unreachable("unknown linkage kind!");
Rafael Espindolac18086a2010-02-23 22:00:30 +00001418}
1419
Chandler Carruthedc2c642011-07-02 00:01:44 +00001420static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001421 // Check the attribute arguments.
1422 if (Attr.getNumArgs() > 1) {
1423 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1424 return;
1425 }
1426
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001427 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall7a198ce2011-02-08 22:35:49 +00001428 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001429 << Attr.getName() << ExpectedVariableOrFunction;
John McCall7a198ce2011-02-08 22:35:49 +00001430 return;
1431 }
1432
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001433 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001434
Rafael Espindolac18086a2010-02-23 22:00:30 +00001435 // gcc rejects
1436 // class c {
1437 // static int a __attribute__((weakref ("v2")));
1438 // static int b() __attribute__((weakref ("f3")));
1439 // };
1440 // and ignores the attributes of
1441 // void f(void) {
1442 // static int a __attribute__((weakref ("v2")));
1443 // }
1444 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001445 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001446 if (!Ctx->isFileContext()) {
1447 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +00001448 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +00001449 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001450 }
1451
1452 // The GCC manual says
1453 //
1454 // At present, a declaration to which `weakref' is attached can only
1455 // be `static'.
1456 //
1457 // It also says
1458 //
1459 // Without a TARGET,
1460 // given as an argument to `weakref' or to `alias', `weakref' is
1461 // equivalent to `weak'.
1462 //
1463 // gcc 4.4.1 will accept
1464 // int a7 __attribute__((weakref));
1465 // as
1466 // int a7 __attribute__((weak));
1467 // This looks like a bug in gcc. We reject that for now. We should revisit
1468 // it if this behaviour is actually used.
1469
John McCall7a198ce2011-02-08 22:35:49 +00001470 if (!hasEffectivelyInternalLinkage(nd)) {
1471 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindolac18086a2010-02-23 22:00:30 +00001472 return;
1473 }
1474
1475 // GCC rejects
1476 // static ((alias ("y"), weakref)).
1477 // Should we? How to check that weakref is before or after alias?
1478
1479 if (Attr.getNumArgs() == 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001480 Expr *Arg = Attr.getArg(0);
Rafael Espindolac18086a2010-02-23 22:00:30 +00001481 Arg = Arg->IgnoreParenCasts();
1482 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1483
Douglas Gregorfb65e592011-07-27 05:40:30 +00001484 if (!Str || !Str->isAscii()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001485 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1486 << "weakref" << 1;
1487 return;
1488 }
1489 // GCC will accept anything as the argument of weakref. Should we
1490 // check for an existing decl?
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001491 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001492 Str->getString()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001493 }
1494
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001495 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001496}
1497
Chandler Carruthedc2c642011-07-02 00:01:44 +00001498static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001499 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001500 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001501 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001502 return;
1503 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001504
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001505 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001506 Arg = Arg->IgnoreParenCasts();
1507 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001508
Douglas Gregorfb65e592011-07-27 05:40:30 +00001509 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001510 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001511 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001512 return;
1513 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001514
Douglas Gregore8bbc122011-09-02 00:18:52 +00001515 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001516 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1517 return;
1518 }
1519
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001520 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001521
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001522 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001523 Str->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001524}
1525
Quentin Colombet4e172062012-11-01 23:55:47 +00001526static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1527 // Check the attribute arguments.
1528 if (!checkAttributeNumArgs(S, Attr, 0))
1529 return;
1530
1531 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1532 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1533 << Attr.getName() << ExpectedFunctionOrMethod;
1534 return;
1535 }
1536
1537 D->addAttr(::new (S.Context) MinSizeAttr(Attr.getRange(), S.Context));
1538}
1539
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001540static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1541 // Check the attribute arguments.
1542 if (!checkAttributeNumArgs(S, Attr, 0))
1543 return;
1544
1545 if (!isa<FunctionDecl>(D)) {
1546 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1547 << Attr.getName() << ExpectedFunction;
1548 return;
1549 }
1550
1551 if (D->hasAttr<HotAttr>()) {
1552 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1553 << Attr.getName() << "hot";
1554 return;
1555 }
1556
1557 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context));
1558}
1559
1560static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1561 // Check the attribute arguments.
1562 if (!checkAttributeNumArgs(S, Attr, 0))
1563 return;
1564
1565 if (!isa<FunctionDecl>(D)) {
1566 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1567 << Attr.getName() << ExpectedFunction;
1568 return;
1569 }
1570
1571 if (D->hasAttr<ColdAttr>()) {
1572 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1573 << Attr.getName() << "cold";
1574 return;
1575 }
1576
1577 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context));
1578}
1579
Chandler Carruthedc2c642011-07-02 00:01:44 +00001580static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001581 // Check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001582 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar03a38442008-10-28 00:17:57 +00001583 return;
Anders Carlsson88097122009-02-19 19:16:48 +00001584
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001585 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001586 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001587 << Attr.getName() << ExpectedFunction;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001588 return;
1589 }
1590
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001591 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001592}
1593
Chandler Carruthedc2c642011-07-02 00:01:44 +00001594static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1595 const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001596 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001597 if (Attr.hasParameterOrArguments()) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001598 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1599 return;
1600 }
1601
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001602 if (!isa<FunctionDecl>(D)) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001603 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001604 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00001605 return;
1606 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001607
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001608 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
Daniel Dunbar03a38442008-10-28 00:17:57 +00001609}
1610
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001611static void handleTLSModelAttr(Sema &S, Decl *D,
1612 const AttributeList &Attr) {
1613 // Check the attribute arguments.
1614 if (Attr.getNumArgs() != 1) {
1615 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1616 return;
1617 }
1618
1619 Expr *Arg = Attr.getArg(0);
1620 Arg = Arg->IgnoreParenCasts();
1621 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1622
1623 // Check that it is a string.
1624 if (!Str) {
1625 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1626 return;
1627 }
1628
1629 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) {
1630 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1631 << Attr.getName() << ExpectedTLSVar;
1632 return;
1633 }
1634
1635 // Check that the value.
1636 StringRef Model = Str->getString();
1637 if (Model != "global-dynamic" && Model != "local-dynamic"
1638 && Model != "initial-exec" && Model != "local-exec") {
1639 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1640 return;
1641 }
1642
1643 D->addAttr(::new (S.Context) TLSModelAttr(Attr.getRange(), S.Context,
1644 Model));
1645}
1646
Chandler Carruthedc2c642011-07-02 00:01:44 +00001647static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001648 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001649 if (Attr.hasParameterOrArguments()) {
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001650 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1651 return;
1652 }
Mike Stump11289f42009-09-09 15:08:12 +00001653
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001654 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001655 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001656 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001657 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001658 return;
1659 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001660 }
1661
Ted Kremenek08479ae2009-08-15 00:51:46 +00001662 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001663}
1664
Chandler Carruthedc2c642011-07-02 00:01:44 +00001665static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001666 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001667 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001668 return;
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001669
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001670 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001671}
1672
Chandler Carruthedc2c642011-07-02 00:01:44 +00001673static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001674 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001675 if (isa<VarDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001676 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
Eric Christopher515d87f2010-12-03 06:58:14 +00001677 else
1678 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001679 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001680}
1681
Chandler Carruthedc2c642011-07-02 00:01:44 +00001682static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001683 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001684 if (isa<VarDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001685 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
Eric Christopher515d87f2010-12-03 06:58:14 +00001686 else
1687 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001688 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001689}
1690
Chandler Carruthedc2c642011-07-02 00:01:44 +00001691static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001692 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001693
1694 if (S.CheckNoReturnAttr(attr)) return;
1695
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001696 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001697 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001698 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001699 return;
1700 }
1701
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001702 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
John McCall3882ace2011-01-05 12:14:39 +00001703}
1704
1705bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek1551d552011-04-15 05:49:29 +00001706 if (attr.hasParameterOrArguments()) {
John McCall3882ace2011-01-05 12:14:39 +00001707 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1708 attr.setInvalid();
1709 return true;
1710 }
1711
1712 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001713}
1714
Chandler Carruthedc2c642011-07-02 00:01:44 +00001715static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1716 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001717
1718 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1719 // because 'analyzer_noreturn' does not impact the type.
1720
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001721 if(!checkAttributeNumArgs(S, Attr, 0))
1722 return;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001723
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001724 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1725 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001726 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1727 && !VD->getType()->isFunctionPointerType())) {
1728 S.Diag(Attr.getLoc(),
1729 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1730 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001731 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001732 return;
1733 }
1734 }
1735
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001736 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001737}
1738
John Thompsoncdb847ba2010-08-09 21:53:52 +00001739// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001740static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001741/*
1742 Returning a Vector Class in Registers
1743
Eric Christopherbc638a82010-12-01 22:13:54 +00001744 According to the PPU ABI specifications, a class with a single member of
1745 vector type is returned in memory when used as the return value of a function.
1746 This results in inefficient code when implementing vector classes. To return
1747 the value in a single vector register, add the vecreturn attribute to the
1748 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001749
1750 Example:
1751
1752 struct Vector
1753 {
1754 __vector float xyzw;
1755 } __attribute__((vecreturn));
1756
1757 Vector Add(Vector lhs, Vector rhs)
1758 {
1759 Vector result;
1760 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1761 return result; // This will be returned in a register
1762 }
1763*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001764 if (!isa<RecordDecl>(D)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001765 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001766 << Attr.getName() << ExpectedClass;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001767 return;
1768 }
1769
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001770 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001771 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1772 return;
1773 }
1774
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001775 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001776 int count = 0;
1777
1778 if (!isa<CXXRecordDecl>(record)) {
1779 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1780 return;
1781 }
1782
1783 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1784 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1785 return;
1786 }
1787
Eric Christopherbc638a82010-12-01 22:13:54 +00001788 for (RecordDecl::field_iterator iter = record->field_begin();
1789 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001790 if ((count == 1) || !iter->getType()->isVectorType()) {
1791 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1792 return;
1793 }
1794 count++;
1795 }
1796
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001797 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001798}
1799
Chandler Carruthedc2c642011-07-02 00:01:44 +00001800static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001801 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001802 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001803 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001804 return;
1805 }
1806 // FIXME: Actually store the attribute on the declaration
1807}
1808
Chandler Carruthedc2c642011-07-02 00:01:44 +00001809static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek39c59a82008-07-25 04:39:19 +00001810 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001811 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001812 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001813 return;
1814 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001815
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001816 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00001817 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001818 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001819 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001820 return;
1821 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001822
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001823 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001824}
1825
Rafael Espindola70107f92011-10-03 14:59:42 +00001826static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1827 const AttributeList &Attr) {
1828 // check the attribute arguments.
1829 if (Attr.hasParameterOrArguments()) {
1830 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1831 return;
1832 }
1833
1834 if (!isa<FunctionDecl>(D)) {
1835 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1836 << Attr.getName() << ExpectedFunction;
1837 return;
1838 }
1839
1840 D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context));
1841}
1842
Chandler Carruthedc2c642011-07-02 00:01:44 +00001843static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001844 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001845 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001846 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1847 return;
1848 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001849
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001850 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +00001851 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001852 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1853 return;
1854 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001855 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001856 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001857 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001858 return;
1859 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001860
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001861 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001862}
1863
Chandler Carruthedc2c642011-07-02 00:01:44 +00001864static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001865 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001866 if (Attr.getNumArgs() > 1) {
1867 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001868 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001869 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001870
1871 int priority = 65535; // FIXME: Do not hardcode such constants.
1872 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001873 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001874 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001875 if (E->isTypeDependent() || E->isValueDependent() ||
1876 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001877 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001878 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001879 return;
1880 }
1881 priority = Idx.getZExtValue();
1882 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001883
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001884 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001885 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001886 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001887 return;
1888 }
1889
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001890 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001891 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001892}
1893
Chandler Carruthedc2c642011-07-02 00:01:44 +00001894static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001895 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001896 if (Attr.getNumArgs() > 1) {
1897 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001898 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001899 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001900
1901 int priority = 65535; // FIXME: Do not hardcode such constants.
1902 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001903 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001904 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001905 if (E->isTypeDependent() || E->isValueDependent() ||
1906 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001907 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001908 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001909 return;
1910 }
1911 priority = Idx.getZExtValue();
1912 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001913
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001914 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001915 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001916 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001917 return;
1918 }
1919
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001920 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001921 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001922}
1923
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001924template <typename AttrTy>
1925static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr,
1926 const char *Name) {
Chris Lattner190aa102011-02-24 05:42:24 +00001927 unsigned NumArgs = Attr.getNumArgs();
1928 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001929 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001930 return;
1931 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001932
1933 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001934 StringRef Str;
Chris Lattner190aa102011-02-24 05:42:24 +00001935 if (NumArgs == 1) {
1936 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanian551063102010-10-06 21:18:44 +00001937 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00001938 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001939 << Name;
Fariborz Jahanian551063102010-10-06 21:18:44 +00001940 return;
1941 }
Chris Lattner190aa102011-02-24 05:42:24 +00001942 Str = SE->getString();
Fariborz Jahanian551063102010-10-06 21:18:44 +00001943 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001944
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001945 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001946}
1947
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00001948static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1949 const AttributeList &Attr) {
1950 unsigned NumArgs = Attr.getNumArgs();
1951 if (NumArgs > 0) {
1952 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1953 return;
1954 }
1955
1956 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001957 Attr.getRange(), S.Context));
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00001958}
1959
Patrick Beardacfbe9e2012-04-06 18:12:22 +00001960static void handleObjCRootClassAttr(Sema &S, Decl *D,
1961 const AttributeList &Attr) {
1962 if (!isa<ObjCInterfaceDecl>(D)) {
1963 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1964 return;
1965 }
1966
1967 unsigned NumArgs = Attr.getNumArgs();
1968 if (NumArgs > 0) {
1969 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1970 return;
1971 }
1972
1973 D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context));
1974}
1975
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001976static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001977 const AttributeList &Attr) {
Fariborz Jahanian7249e362012-01-03 22:52:32 +00001978 if (!isa<ObjCInterfaceDecl>(D)) {
1979 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1980 return;
1981 }
1982
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001983 unsigned NumArgs = Attr.getNumArgs();
1984 if (NumArgs > 0) {
1985 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1986 return;
1987 }
1988
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001989 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001990 Attr.getRange(), S.Context));
1991}
1992
Jordy Rose740b0c22012-05-08 03:27:22 +00001993static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1994 IdentifierInfo *Platform,
1995 VersionTuple Introduced,
1996 VersionTuple Deprecated,
1997 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001998 StringRef PlatformName
1999 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2000 if (PlatformName.empty())
2001 PlatformName = Platform->getName();
2002
2003 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2004 // of these steps are needed).
2005 if (!Introduced.empty() && !Deprecated.empty() &&
2006 !(Introduced <= Deprecated)) {
2007 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2008 << 1 << PlatformName << Deprecated.getAsString()
2009 << 0 << Introduced.getAsString();
2010 return true;
2011 }
2012
2013 if (!Introduced.empty() && !Obsoleted.empty() &&
2014 !(Introduced <= Obsoleted)) {
2015 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2016 << 2 << PlatformName << Obsoleted.getAsString()
2017 << 0 << Introduced.getAsString();
2018 return true;
2019 }
2020
2021 if (!Deprecated.empty() && !Obsoleted.empty() &&
2022 !(Deprecated <= Obsoleted)) {
2023 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2024 << 2 << PlatformName << Obsoleted.getAsString()
2025 << 1 << Deprecated.getAsString();
2026 return true;
2027 }
2028
2029 return false;
2030}
2031
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002032AvailabilityAttr *Sema::mergeAvailabilityAttr(Decl *D, SourceRange Range,
2033 IdentifierInfo *Platform,
2034 VersionTuple Introduced,
2035 VersionTuple Deprecated,
2036 VersionTuple Obsoleted,
2037 bool IsUnavailable,
2038 StringRef Message) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002039 VersionTuple MergedIntroduced = Introduced;
2040 VersionTuple MergedDeprecated = Deprecated;
2041 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002042 bool FoundAny = false;
2043
Rafael Espindolac67f2232012-05-10 02:50:16 +00002044 if (D->hasAttrs()) {
2045 AttrVec &Attrs = D->getAttrs();
2046 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2047 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2048 if (!OldAA) {
2049 ++i;
2050 continue;
2051 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002052
Rafael Espindolac67f2232012-05-10 02:50:16 +00002053 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2054 if (OldPlatform != Platform) {
2055 ++i;
2056 continue;
2057 }
2058
2059 FoundAny = true;
2060 VersionTuple OldIntroduced = OldAA->getIntroduced();
2061 VersionTuple OldDeprecated = OldAA->getDeprecated();
2062 VersionTuple OldObsoleted = OldAA->getObsoleted();
2063 bool OldIsUnavailable = OldAA->getUnavailable();
2064 StringRef OldMessage = OldAA->getMessage();
2065
2066 if ((!OldIntroduced.empty() && !Introduced.empty() &&
2067 OldIntroduced != Introduced) ||
2068 (!OldDeprecated.empty() && !Deprecated.empty() &&
2069 OldDeprecated != Deprecated) ||
2070 (!OldObsoleted.empty() && !Obsoleted.empty() &&
2071 OldObsoleted != Obsoleted) ||
2072 (OldIsUnavailable != IsUnavailable) ||
2073 (OldMessage != Message)) {
2074 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2075 Diag(Range.getBegin(), diag::note_previous_attribute);
2076 Attrs.erase(Attrs.begin() + i);
2077 --e;
2078 continue;
2079 }
2080
2081 VersionTuple MergedIntroduced2 = MergedIntroduced;
2082 VersionTuple MergedDeprecated2 = MergedDeprecated;
2083 VersionTuple MergedObsoleted2 = MergedObsoleted;
2084
2085 if (MergedIntroduced2.empty())
2086 MergedIntroduced2 = OldIntroduced;
2087 if (MergedDeprecated2.empty())
2088 MergedDeprecated2 = OldDeprecated;
2089 if (MergedObsoleted2.empty())
2090 MergedObsoleted2 = OldObsoleted;
2091
2092 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2093 MergedIntroduced2, MergedDeprecated2,
2094 MergedObsoleted2)) {
2095 Attrs.erase(Attrs.begin() + i);
2096 --e;
2097 continue;
2098 }
2099
2100 MergedIntroduced = MergedIntroduced2;
2101 MergedDeprecated = MergedDeprecated2;
2102 MergedObsoleted = MergedObsoleted2;
2103 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002104 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002105 }
2106
2107 if (FoundAny &&
2108 MergedIntroduced == Introduced &&
2109 MergedDeprecated == Deprecated &&
2110 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002111 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002112
Rafael Espindolac67f2232012-05-10 02:50:16 +00002113 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002114 MergedDeprecated, MergedObsoleted)) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002115 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2116 Introduced, Deprecated,
2117 Obsoleted, IsUnavailable, Message);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002118 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002119 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002120}
2121
Chandler Carruthedc2c642011-07-02 00:01:44 +00002122static void handleAvailabilityAttr(Sema &S, Decl *D,
2123 const AttributeList &Attr) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002124 IdentifierInfo *Platform = Attr.getParameterName();
2125 SourceLocation PlatformLoc = Attr.getParameterLoc();
2126
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002127 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002128 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2129 << Platform;
2130
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002131 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2132 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2133 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002134 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002135 StringRef Str;
2136 const StringLiteral *SE =
2137 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2138 if (SE)
2139 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002140
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002141 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(D, Attr.getRange(),
2142 Platform,
2143 Introduced.Version,
2144 Deprecated.Version,
2145 Obsoleted.Version,
2146 IsUnavailable, Str);
2147 if (NewAttr)
2148 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002149}
2150
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002151VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
2152 VisibilityAttr::VisibilityType Vis) {
Rafael Espindolaa6b3cd42012-05-10 03:01:34 +00002153 if (isa<TypedefNameDecl>(D)) {
2154 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility";
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002155 return NULL;
Rafael Espindolaa6b3cd42012-05-10 03:01:34 +00002156 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002157 VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>();
2158 if (ExistingAttr) {
2159 VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility();
2160 if (ExistingVis == Vis)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002161 return NULL;
Rafael Espindolac67f2232012-05-10 02:50:16 +00002162 Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility);
2163 Diag(Range.getBegin(), diag::note_previous_attribute);
2164 D->dropAttr<VisibilityAttr>();
2165 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002166 return ::new (Context) VisibilityAttr(Range, Context, Vis);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002167}
2168
Chandler Carruthedc2c642011-07-02 00:01:44 +00002169static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002170 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002171 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002172 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002173
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002174 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002175 Arg = Arg->IgnoreParenCasts();
2176 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002177
Douglas Gregorfb65e592011-07-27 05:40:30 +00002178 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002179 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002180 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002181 return;
2182 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002183
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002184 StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002185 VisibilityAttr::VisibilityType type;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002186
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002187 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002188 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002189 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002190 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002191 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002192 type = VisibilityAttr::Hidden; // FIXME
John McCalleed64c72012-01-29 01:20:30 +00002193 else if (TypeStr == "protected") {
2194 // Complain about attempts to use protected visibility on targets
2195 // (like Darwin) that don't support it.
2196 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2197 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2198 type = VisibilityAttr::Default;
2199 } else {
2200 type = VisibilityAttr::Protected;
2201 }
2202 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00002203 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002204 return;
2205 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002206
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002207 VisibilityAttr *NewAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type);
2208 if (NewAttr)
2209 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002210}
2211
Chandler Carruthedc2c642011-07-02 00:01:44 +00002212static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2213 const AttributeList &Attr) {
John McCall86bc21f2011-03-02 11:33:24 +00002214 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2215 if (!method) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002216 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002217 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00002218 return;
2219 }
2220
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002221 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2222 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2223 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCall86bc21f2011-03-02 11:33:24 +00002224 << "objc_method_family" << 1;
2225 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002226 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCall86bc21f2011-03-02 11:33:24 +00002227 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002228 Attr.setInvalid();
John McCall86bc21f2011-03-02 11:33:24 +00002229 return;
2230 }
2231
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002232 StringRef param = Attr.getParameterName()->getName();
John McCall86bc21f2011-03-02 11:33:24 +00002233 ObjCMethodFamilyAttr::FamilyKind family;
2234 if (param == "none")
2235 family = ObjCMethodFamilyAttr::OMF_None;
2236 else if (param == "alloc")
2237 family = ObjCMethodFamilyAttr::OMF_alloc;
2238 else if (param == "copy")
2239 family = ObjCMethodFamilyAttr::OMF_copy;
2240 else if (param == "init")
2241 family = ObjCMethodFamilyAttr::OMF_init;
2242 else if (param == "mutableCopy")
2243 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2244 else if (param == "new")
2245 family = ObjCMethodFamilyAttr::OMF_new;
2246 else {
2247 // Just warn and ignore it. This is future-proof against new
2248 // families being used in system headers.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002249 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCall86bc21f2011-03-02 11:33:24 +00002250 return;
2251 }
2252
John McCall31168b02011-06-15 23:02:42 +00002253 if (family == ObjCMethodFamilyAttr::OMF_init &&
2254 !method->getResultType()->isObjCObjectPointerType()) {
2255 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2256 << method->getResultType();
2257 // Ignore the attribute.
2258 return;
2259 }
2260
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002261 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCall31168b02011-06-15 23:02:42 +00002262 S.Context, family));
John McCall86bc21f2011-03-02 11:33:24 +00002263}
2264
Chandler Carruthedc2c642011-07-02 00:01:44 +00002265static void handleObjCExceptionAttr(Sema &S, Decl *D,
2266 const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002267 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner677a3582009-02-14 08:09:34 +00002268 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002269
Chris Lattner677a3582009-02-14 08:09:34 +00002270 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2271 if (OCI == 0) {
2272 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2273 return;
2274 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002275
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002276 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner677a3582009-02-14 08:09:34 +00002277}
2278
Chandler Carruthedc2c642011-07-02 00:01:44 +00002279static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002280 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00002281 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002282 return;
2283 }
Richard Smithdda56e42011-04-15 14:24:37 +00002284 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002285 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002286 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002287 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2288 return;
2289 }
2290 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002291 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2292 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002293 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002294 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2295 return;
2296 }
2297 }
2298 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002299 // It is okay to include this attribute on properties, e.g.:
2300 //
2301 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2302 //
2303 // In this case it follows tradition and suppresses an error in the above
2304 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002305 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002306 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002307 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002308}
2309
Mike Stumpd3bb5572009-07-24 19:02:52 +00002310static void
Chandler Carruthedc2c642011-07-02 00:01:44 +00002311handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002312 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00002313 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002314 return;
2315 }
2316
2317 if (!isa<FunctionDecl>(D)) {
2318 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2319 return;
2320 }
2321
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002322 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002323}
2324
Chandler Carruthedc2c642011-07-02 00:01:44 +00002325static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002326 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002327 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002328 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002329 return;
2330 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002331
Steve Naroff3405a732008-09-18 16:44:58 +00002332 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002333 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002334 return;
2335 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002336
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002337 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00002338 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00002339 type = BlocksAttr::ByRef;
2340 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002341 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002342 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00002343 return;
2344 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002345
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002346 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff3405a732008-09-18 16:44:58 +00002347}
2348
Chandler Carruthedc2c642011-07-02 00:01:44 +00002349static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002350 // check the attribute arguments.
2351 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002352 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002353 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002354 }
2355
John McCallb46f2872011-09-09 07:56:05 +00002356 unsigned sentinel = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002357 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002358 Expr *E = Attr.getArg(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002359 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002360 if (E->isTypeDependent() || E->isValueDependent() ||
2361 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002362 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002363 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002364 return;
2365 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002366
John McCallb46f2872011-09-09 07:56:05 +00002367 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002368 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2369 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002370 return;
2371 }
John McCallb46f2872011-09-09 07:56:05 +00002372
2373 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002374 }
2375
John McCallb46f2872011-09-09 07:56:05 +00002376 unsigned nullPos = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002377 if (Attr.getNumArgs() > 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002378 Expr *E = Attr.getArg(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002379 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002380 if (E->isTypeDependent() || E->isValueDependent() ||
2381 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002382 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002383 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002384 return;
2385 }
2386 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002387
John McCallb46f2872011-09-09 07:56:05 +00002388 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002389 // FIXME: This error message could be improved, it would be nice
2390 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002391 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2392 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002393 return;
2394 }
2395 }
2396
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002397 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002398 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002399 if (isa<FunctionNoProtoType>(FT)) {
2400 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2401 return;
2402 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002403
Chris Lattner9363e312009-03-17 23:03:47 +00002404 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002405 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002406 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002407 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002408 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002409 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002410 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002411 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002412 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002413 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2414 if (!BD->isVariadic()) {
2415 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2416 return;
2417 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002418 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002419 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002420 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002421 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002422 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002423 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002424 int m = Ty->isFunctionPointerType() ? 0 : 1;
2425 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002426 return;
2427 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002428 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002429 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002430 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002431 return;
2432 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002433 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002434 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002435 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002436 return;
2437 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002438 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherbc638a82010-12-01 22:13:54 +00002439 nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +00002440}
2441
Chandler Carruthedc2c642011-07-02 00:01:44 +00002442static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner237f2752009-02-14 07:37:35 +00002443 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002444 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner237f2752009-02-14 07:37:35 +00002445 return;
Chris Lattner237f2752009-02-14 07:37:35 +00002446
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002447 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002448 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002449 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner237f2752009-02-14 07:37:35 +00002450 return;
2451 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002452
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002453 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2454 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2455 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002456 return;
2457 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002458 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2459 if (MD->getResultType()->isVoidType()) {
2460 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2461 << Attr.getName() << 1;
2462 return;
2463 }
2464
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002465 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner237f2752009-02-14 07:37:35 +00002466}
2467
Chandler Carruthedc2c642011-07-02 00:01:44 +00002468static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002469 // check the attribute arguments.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002470 if (Attr.hasParameterOrArguments()) {
2471 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002472 return;
2473 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002474
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002475 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002476 if (isa<CXXRecordDecl>(D)) {
2477 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2478 return;
2479 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002480 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2481 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002482 return;
2483 }
2484
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002485 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002486
2487 // 'weak' only applies to declarations with external linkage.
2488 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002489 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002490 return;
2491 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002492
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002493 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002494}
2495
Chandler Carruthedc2c642011-07-02 00:01:44 +00002496static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002497 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002498 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002499 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002500
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002501
2502 // weak_import only applies to variable & function declarations.
2503 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002504 if (!D->canBeWeakImported(isDef)) {
2505 if (isDef)
2506 S.Diag(Attr.getLoc(),
2507 diag::warn_attribute_weak_import_invalid_on_definition)
2508 << "weak_import" << 2 /*variable and function*/;
Douglas Gregord71149a2011-03-23 13:27:51 +00002509 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002510 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002511 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002512 // Nothing to warn about here.
2513 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002514 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002515 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002516
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002517 return;
2518 }
2519
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002520 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002521}
2522
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002523// Handles reqd_work_group_size and work_group_size_hint.
2524static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002525 const AttributeList &Attr) {
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002526 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2527 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2528
Nate Begemanf2758702009-06-26 06:32:41 +00002529 // Attribute has 3 arguments.
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002530 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begemanf2758702009-06-26 06:32:41 +00002531
2532 unsigned WGSize[3];
2533 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002534 Expr *E = Attr.getArg(i);
Nate Begemanf2758702009-06-26 06:32:41 +00002535 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002536 if (E->isTypeDependent() || E->isValueDependent() ||
2537 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00002538 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002539 << Attr.getName()->getName() << E->getSourceRange();
Nate Begemanf2758702009-06-26 06:32:41 +00002540 return;
2541 }
2542 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2543 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002544
2545 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2546 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2547 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2548 if (!(A->getXDim() == WGSize[0] &&
2549 A->getYDim() == WGSize[1] &&
2550 A->getZDim() == WGSize[2])) {
2551 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2552 Attr.getName();
2553 }
2554 }
2555
2556 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2557 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2558 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2559 if (!(A->getXDim() == WGSize[0] &&
2560 A->getYDim() == WGSize[1] &&
2561 A->getZDim() == WGSize[2])) {
2562 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2563 Attr.getName();
2564 }
2565 }
2566
2567 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2568 D->addAttr(::new (S.Context)
2569 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
2570 WGSize[0], WGSize[1], WGSize[2]));
2571 else
2572 D->addAttr(::new (S.Context)
2573 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
2574 WGSize[0], WGSize[1], WGSize[2]));
Nate Begemanf2758702009-06-26 06:32:41 +00002575}
2576
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002577SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
2578 StringRef Name) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002579 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2580 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002581 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002582 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2583 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002584 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002585 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002586 return ::new (Context) SectionAttr(Range, Context, Name);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002587}
2588
Chandler Carruthedc2c642011-07-02 00:01:44 +00002589static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002590 // Attribute has no arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002591 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002592 return;
Daniel Dunbar648bf782009-02-12 17:28:23 +00002593
2594 // Make sure that there is a string literal as the sections's single
2595 // argument.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002596 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002597 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002598 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002599 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00002600 return;
2601 }
Mike Stump11289f42009-09-09 15:08:12 +00002602
Chris Lattner30ba6742009-08-10 19:03:04 +00002603 // If the target wants to validate the section specifier, make it happen.
Douglas Gregore8bbc122011-09-02 00:18:52 +00002604 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00002605 if (!Error.empty()) {
2606 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2607 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002608 return;
2609 }
Mike Stump11289f42009-09-09 15:08:12 +00002610
Chris Lattner20aee9b2010-01-12 20:58:53 +00002611 // This attribute cannot be applied to local variables.
2612 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2613 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2614 return;
2615 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002616 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
2617 SE->getString());
2618 if (NewAttr)
2619 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002620}
2621
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002622
Chandler Carruthedc2c642011-07-02 00:01:44 +00002623static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002624 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002625 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002626 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002627 return;
2628 }
Douglas Gregor88336832011-06-15 05:45:11 +00002629
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002630 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002631 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002632 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002633 } else {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002634 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregor88336832011-06-15 05:45:11 +00002635 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002636}
2637
Chandler Carruthedc2c642011-07-02 00:01:44 +00002638static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002639 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002640 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002641 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00002642 return;
2643 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002644
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002645 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002646 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002647 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002648 } else {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002649 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregor88336832011-06-15 05:45:11 +00002650 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002651}
2652
Chandler Carruthedc2c642011-07-02 00:01:44 +00002653static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002654 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002655 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssonb8316282008-10-05 23:32:53 +00002656 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002657
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002658 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00002659}
2660
Chandler Carruthedc2c642011-07-02 00:01:44 +00002661static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002662 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00002663 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2664 return;
2665 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002666
Anders Carlssond277d792009-01-31 01:16:18 +00002667 if (Attr.getNumArgs() != 0) {
2668 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2669 return;
2670 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002671
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002672 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002673
Anders Carlssond277d792009-01-31 01:16:18 +00002674 if (!VD || !VD->hasLocalStorage()) {
2675 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2676 return;
2677 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002678
Anders Carlssond277d792009-01-31 01:16:18 +00002679 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00002680 // FIXME: Lookup probably isn't looking in the right place
John McCall9f3059a2009-10-09 21:13:30 +00002681 NamedDecl *CleanupDecl
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002682 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2683 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00002684 if (!CleanupDecl) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002685 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00002686 Attr.getParameterName();
2687 return;
2688 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002689
Anders Carlssond277d792009-01-31 01:16:18 +00002690 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2691 if (!FD) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002692 S.Diag(Attr.getParameterLoc(),
2693 diag::err_attribute_cleanup_arg_not_function)
2694 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002695 return;
2696 }
2697
Anders Carlssond277d792009-01-31 01:16:18 +00002698 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002699 S.Diag(Attr.getParameterLoc(),
2700 diag::err_attribute_cleanup_func_must_take_one_arg)
2701 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002702 return;
2703 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002704
Anders Carlsson723f55d2009-02-07 23:16:50 +00002705 // We're currently more strict than GCC about what function types we accept.
2706 // If this ever proves to be a problem it should be easy to fix.
2707 QualType Ty = S.Context.getPointerType(VD->getType());
2708 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002709 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2710 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002711 S.Diag(Attr.getParameterLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00002712 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2713 Attr.getParameterName() << ParamTy << Ty;
2714 return;
2715 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002716
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002717 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Eli Friedmanfa0df832012-02-02 03:46:19 +00002718 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssond277d792009-01-31 01:16:18 +00002719}
2720
Mike Stumpd3bb5572009-07-24 19:02:52 +00002721/// Handle __attribute__((format_arg((idx)))) attribute based on
2722/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002723static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002724 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002725 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002726
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002727 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002728 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002729 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002730 return;
2731 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002732
2733 // In C++ the implicit 'this' function parameter also counts, and they are
2734 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002735 bool HasImplicitThisParam = isInstanceMethod(D);
2736 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002737 unsigned FirstIdx = 1;
Chandler Carruth743682b2010-11-16 08:35:43 +00002738
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002739 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002740 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002741 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002742 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2743 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002744 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2745 << "format" << 2 << IdxExpr->getSourceRange();
2746 return;
2747 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002748
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002749 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2750 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2751 << "format" << 2 << IdxExpr->getSourceRange();
2752 return;
2753 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002754
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002755 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002756
Chandler Carruth743682b2010-11-16 08:35:43 +00002757 if (HasImplicitThisParam) {
2758 if (ArgIdx == 0) {
2759 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2760 << "format_arg" << IdxExpr->getSourceRange();
2761 return;
2762 }
2763 ArgIdx--;
2764 }
2765
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002766 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002767 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002768
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002769 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2770 if (not_nsstring_type &&
2771 !isCFStringType(Ty, S.Context) &&
2772 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002773 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002774 // FIXME: Should highlight the actual expression that has the wrong type.
2775 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002776 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002777 << IdxExpr->getSourceRange();
2778 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002779 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002780 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002781 if (!isNSStringType(Ty, S.Context) &&
2782 !isCFStringType(Ty, S.Context) &&
2783 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002784 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002785 // FIXME: Should highlight the actual expression that has the wrong type.
2786 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002787 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002788 << IdxExpr->getSourceRange();
2789 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002790 }
2791
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002792 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth743682b2010-11-16 08:35:43 +00002793 Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002794}
2795
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002796enum FormatAttrKind {
2797 CFStringFormat,
2798 NSStringFormat,
2799 StrftimeFormat,
2800 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002801 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002802 InvalidFormat
2803};
2804
2805/// getFormatAttrKind - Map from format attribute names to supported format
2806/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002807static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002808 return llvm::StringSwitch<FormatAttrKind>(Format)
2809 // Check for formats that get handled specially.
2810 .Case("NSString", NSStringFormat)
2811 .Case("CFString", CFStringFormat)
2812 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002813
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002814 // Otherwise, check for supported formats.
2815 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2816 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2817 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002818
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002819 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2820 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002821}
2822
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002823/// Handle __attribute__((init_priority(priority))) attributes based on
2824/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002825static void handleInitPriorityAttr(Sema &S, Decl *D,
2826 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002827 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002828 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2829 return;
2830 }
2831
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002832 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002833 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2834 Attr.setInvalid();
2835 return;
2836 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002837 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002838 if (S.Context.getAsArrayType(T))
2839 T = S.Context.getBaseElementType(T);
2840 if (!T->getAs<RecordType>()) {
2841 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2842 Attr.setInvalid();
2843 return;
2844 }
2845
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002846 if (Attr.getNumArgs() != 1) {
2847 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2848 Attr.setInvalid();
2849 return;
2850 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002851 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002852
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002853 llvm::APSInt priority(32);
2854 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2855 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2856 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2857 << "init_priority" << priorityExpr->getSourceRange();
2858 Attr.setInvalid();
2859 return;
2860 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00002861 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002862 if (prioritynum < 101 || prioritynum > 65535) {
2863 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2864 << priorityExpr->getSourceRange();
2865 Attr.setInvalid();
2866 return;
2867 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002868 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00002869 prioritynum));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002870}
2871
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002872FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
2873 int FormatIdx, int FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002874 // Check whether we already have an equivalent format attribute.
2875 for (specific_attr_iterator<FormatAttr>
2876 i = D->specific_attr_begin<FormatAttr>(),
2877 e = D->specific_attr_end<FormatAttr>();
2878 i != e ; ++i) {
2879 FormatAttr *f = *i;
2880 if (f->getType() == Format &&
2881 f->getFormatIdx() == FormatIdx &&
2882 f->getFirstArg() == FirstArg) {
2883 // If we don't have a valid location for this attribute, adopt the
2884 // location.
2885 if (f->getLocation().isInvalid())
2886 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002887 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002888 }
2889 }
2890
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002891 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2892 FirstArg);
Rafael Espindola92d49452012-05-11 00:36:07 +00002893}
2894
Mike Stumpd3bb5572009-07-24 19:02:52 +00002895/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2896/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002897static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002898
Chris Lattner4a927cb2008-06-28 23:36:30 +00002899 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002900 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002901 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002902 return;
2903 }
2904
Chris Lattner4a927cb2008-06-28 23:36:30 +00002905 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002906 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002907 return;
2908 }
2909
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002910 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002911 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002912 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002913 return;
2914 }
2915
Chandler Carruth743682b2010-11-16 08:35:43 +00002916 // In C++ the implicit 'this' function parameter also counts, and they are
2917 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002918 bool HasImplicitThisParam = isInstanceMethod(D);
2919 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002920 unsigned FirstIdx = 1;
2921
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002922 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002923
2924 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002925 if (Format.startswith("__") && Format.endswith("__"))
2926 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002927
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002928 // Check for supported formats.
2929 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002930
2931 if (Kind == IgnoredFormat)
2932 return;
2933
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002934 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002935 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00002936 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002937 return;
2938 }
2939
2940 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002941 Expr *IdxExpr = Attr.getArg(0);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002942 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002943 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2944 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002945 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002946 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002947 return;
2948 }
2949
2950 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002951 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002952 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002953 return;
2954 }
2955
2956 // FIXME: Do we need to bounds check?
2957 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002958
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002959 if (HasImplicitThisParam) {
2960 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002961 S.Diag(Attr.getLoc(),
2962 diag::err_format_attribute_implicit_this_format_string)
2963 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002964 return;
2965 }
2966 ArgIdx--;
2967 }
Mike Stump11289f42009-09-09 15:08:12 +00002968
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002969 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002970 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002971
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002972 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002973 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002974 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2975 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002976 return;
2977 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002978 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002979 // FIXME: do we need to check if the type is NSString*? What are the
2980 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002981 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002982 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002983 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2984 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002985 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002986 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002987 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002988 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002989 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002990 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2991 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002992 return;
2993 }
2994
2995 // check the 3rd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002996 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002997 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002998 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
2999 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003000 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003001 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003002 return;
3003 }
3004
3005 // check if the function is variadic if the 3rd argument non-zero
3006 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003007 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003008 ++NumArgs; // +1 for ...
3009 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003010 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003011 return;
3012 }
3013 }
3014
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003015 // strftime requires FirstArg to be 0 because it doesn't read from any
3016 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003017 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003018 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00003019 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3020 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003021 return;
3022 }
3023 // if 0 it disables parameter checking (to use with e.g. va_list)
3024 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003025 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003026 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003027 return;
3028 }
3029
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003030 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3031 Idx.getZExtValue(),
3032 FirstArg.getZExtValue());
3033 if (NewAttr)
3034 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003035}
3036
Chandler Carruthedc2c642011-07-02 00:01:44 +00003037static void handleTransparentUnionAttr(Sema &S, Decl *D,
3038 const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003039 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003040 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003041 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003042
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003043
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003044 // Try to find the underlying union declaration.
3045 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003046 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003047 if (TD && TD->getUnderlyingType()->isUnionType())
3048 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3049 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003050 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003051
3052 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00003053 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003054 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003055 return;
3056 }
3057
John McCallf937c022011-10-07 06:10:15 +00003058 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003059 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003060 diag::warn_transparent_union_attribute_not_definition);
3061 return;
3062 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003063
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003064 RecordDecl::field_iterator Field = RD->field_begin(),
3065 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003066 if (Field == FieldEnd) {
3067 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3068 return;
3069 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003070
David Blaikie40ed2972012-06-06 20:45:41 +00003071 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003072 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003073 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003074 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003075 diag::warn_transparent_union_attribute_floating)
3076 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003077 return;
3078 }
3079
3080 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3081 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3082 for (; Field != FieldEnd; ++Field) {
3083 QualType FieldType = Field->getType();
3084 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3085 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3086 // Warn if we drop the attribute.
3087 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003088 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003089 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003090 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003091 diag::warn_transparent_union_attribute_field_size_align)
3092 << isSize << Field->getDeclName() << FieldBits;
3093 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003094 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003095 diag::note_transparent_union_first_field_size_align)
3096 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003097 return;
3098 }
3099 }
3100
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003101 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003102}
3103
Chandler Carruthedc2c642011-07-02 00:01:44 +00003104static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003105 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003106 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003107 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003108
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003109 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00003110 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003111
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003112 // Make sure that there is a string literal as the annotation's single
3113 // argument.
3114 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00003115 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003116 return;
3117 }
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003118
3119 // Don't duplicate annotations that are already set.
3120 for (specific_attr_iterator<AnnotateAttr>
3121 i = D->specific_attr_begin<AnnotateAttr>(),
3122 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3123 if ((*i)->getAnnotation() == SE->getString())
3124 return;
3125 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003126 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00003127 SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003128}
3129
Chandler Carruthedc2c642011-07-02 00:01:44 +00003130static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003131 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003132 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003133 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003134 return;
3135 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003136
Alexis Hunt96d5c762009-11-21 08:43:09 +00003137 //FIXME: The C++0x version of this attribute has more limited applicabilty
3138 // than GNU's, and should error out when it is used to specify a
3139 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003140
Chris Lattner4a927cb2008-06-28 23:36:30 +00003141 if (Attr.getNumArgs() == 0) {
Aaron Ballman478faed2012-06-19 22:09:27 +00003142 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3143 true, 0, Attr.isDeclspecAttribute()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003144 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003145 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003146
Aaron Ballman478faed2012-06-19 22:09:27 +00003147 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0),
3148 Attr.isDeclspecAttribute());
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003149}
3150
Aaron Ballman478faed2012-06-19 22:09:27 +00003151void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
3152 bool isDeclSpec) {
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00003153 // FIXME: Handle pack-expansions here.
3154 if (DiagnoseUnexpandedParameterPack(E))
3155 return;
3156
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003157 if (E->isTypeDependent() || E->isValueDependent()) {
3158 // Save dependent expressions in the AST to be instantiated.
Aaron Ballman478faed2012-06-19 22:09:27 +00003159 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E,
3160 isDeclSpec));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003161 return;
3162 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003163
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003164 SourceLocation AttrLoc = AttrRange.getBegin();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003165 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00003166 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00003167 ExprResult ICE
3168 = VerifyIntegerConstantExpression(E, &Alignment,
3169 diag::err_aligned_attribute_argument_not_int,
3170 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003171 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003172 return;
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003173 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003174 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3175 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003176 return;
3177 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003178 if (isDeclSpec) {
3179 // We've already verified it's a power of 2, now let's make sure it's
3180 // 8192 or less.
3181 if (Alignment.getZExtValue() > 8192) {
3182 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
3183 << E->getSourceRange();
3184 return;
3185 }
3186 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003187
Aaron Ballman478faed2012-06-19 22:09:27 +00003188 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take(),
3189 isDeclSpec));
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003190}
3191
Aaron Ballman478faed2012-06-19 22:09:27 +00003192void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
3193 bool isDeclSpec) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003194 // FIXME: Cache the number on the Attr object if non-dependent?
3195 // FIXME: Perform checking of type validity
Aaron Ballman478faed2012-06-19 22:09:27 +00003196 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3197 isDeclSpec));
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003198 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003199}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003200
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003201/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003202/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003203///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003204/// Despite what would be logical, the mode attribute is a decl attribute, not a
3205/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3206/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003207static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003208 // This attribute isn't documented, but glibc uses it. It changes
3209 // the width of an int or unsigned int to the specified size.
3210
3211 // Check that there aren't any arguments
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003212 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003213 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003214
Chris Lattneracbc2d22008-06-27 22:18:37 +00003215
3216 IdentifierInfo *Name = Attr.getParameterName();
3217 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00003218 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003219 return;
3220 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00003221
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003222 StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003223
3224 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003225 if (Str.startswith("__") && Str.endswith("__"))
3226 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003227
3228 unsigned DestWidth = 0;
3229 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003230 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003231 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003232 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003233 switch (Str[0]) {
3234 case 'Q': DestWidth = 8; break;
3235 case 'H': DestWidth = 16; break;
3236 case 'S': DestWidth = 32; break;
3237 case 'D': DestWidth = 64; break;
3238 case 'X': DestWidth = 96; break;
3239 case 'T': DestWidth = 128; break;
3240 }
3241 if (Str[1] == 'F') {
3242 IntegerMode = false;
3243 } else if (Str[1] == 'C') {
3244 IntegerMode = false;
3245 ComplexMode = true;
3246 } else if (Str[1] != 'I') {
3247 DestWidth = 0;
3248 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003249 break;
3250 case 4:
3251 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3252 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003253 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003254 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003255 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003256 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003257 break;
3258 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003259 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003260 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003261 break;
3262 }
3263
3264 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003265 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003266 OldTy = TD->getUnderlyingType();
3267 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3268 OldTy = VD->getType();
3269 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003270 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003271 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003272 return;
3273 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003274
John McCall9dd450b2009-09-21 23:43:11 +00003275 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003276 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3277 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003278 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003279 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3280 } else if (ComplexMode) {
3281 if (!OldTy->isComplexType())
3282 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3283 } else {
3284 if (!OldTy->isFloatingType())
3285 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3286 }
3287
Mike Stump87c57ac2009-05-16 07:39:55 +00003288 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3289 // and friends, at least with glibc.
3290 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3291 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003292 // FIXME: Make sure floating-point mappings are accurate
3293 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00003294 QualType NewTy;
3295 switch (DestWidth) {
3296 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003297 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003298 return;
3299 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003300 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003301 return;
3302 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00003303 if (!IntegerMode) {
3304 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3305 return;
3306 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003307 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003308 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003309 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003310 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003311 break;
3312 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00003313 if (!IntegerMode) {
3314 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3315 return;
3316 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003317 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003318 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003319 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003320 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003321 break;
3322 case 32:
3323 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003324 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003325 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003326 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003327 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003328 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003329 break;
3330 case 64:
3331 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003332 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003333 else if (OldTy->isSignedIntegerType())
Douglas Gregore8bbc122011-09-02 00:18:52 +00003334 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00003335 NewTy = S.Context.LongTy;
3336 else
3337 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003338 else
Douglas Gregore8bbc122011-09-02 00:18:52 +00003339 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00003340 NewTy = S.Context.UnsignedLongTy;
3341 else
3342 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003343 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003344 case 96:
3345 NewTy = S.Context.LongDoubleTy;
3346 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00003347 case 128:
3348 if (!IntegerMode) {
3349 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3350 return;
3351 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00003352 if (OldTy->isSignedIntegerType())
3353 NewTy = S.Context.Int128Ty;
3354 else
3355 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00003356 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003357 }
3358
Eli Friedman4735374e2009-03-03 06:41:03 +00003359 if (ComplexMode) {
3360 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003361 }
3362
3363 // Install the new type.
Richard Smithdda56e42011-04-15 14:24:37 +00003364 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall703a3f82009-10-24 08:00:42 +00003365 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00003366 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00003367 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003368 cast<ValueDecl>(D)->setType(NewTy);
3369}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003370
Chandler Carruthedc2c642011-07-02 00:01:44 +00003371static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson76187b42009-02-13 06:46:13 +00003372 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003373 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson76187b42009-02-13 06:46:13 +00003374 return;
Anders Carlsson63784f42009-02-13 08:11:52 +00003375
Nick Lewycky08597072012-07-24 01:40:49 +00003376 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3377 if (!VD->hasGlobalStorage())
3378 S.Diag(Attr.getLoc(),
3379 diag::warn_attribute_requires_functions_or_static_globals)
3380 << Attr.getName();
3381 } else if (!isFunctionOrMethod(D)) {
3382 S.Diag(Attr.getLoc(),
3383 diag::warn_attribute_requires_functions_or_static_globals)
3384 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003385 return;
3386 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003387
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003388 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlsson76187b42009-02-13 06:46:13 +00003389}
3390
Chandler Carruthedc2c642011-07-02 00:01:44 +00003391static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson88097122009-02-19 19:16:48 +00003392 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003393 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson88097122009-02-19 19:16:48 +00003394 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003395
Mike Stumpd3bb5572009-07-24 19:02:52 +00003396
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003397 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00003398 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003399 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00003400 return;
3401 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003402
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003403 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson88097122009-02-19 19:16:48 +00003404}
3405
Chandler Carruthedc2c642011-07-02 00:01:44 +00003406static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3407 const AttributeList &Attr) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003408 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003409 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner3c77a352010-06-22 00:03:40 +00003410 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003411
Chris Lattner3c77a352010-06-22 00:03:40 +00003412
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003413 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003414 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003415 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00003416 return;
3417 }
3418
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003419 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003420 S.Context));
Chris Lattner3c77a352010-06-22 00:03:40 +00003421}
3422
Chandler Carruthedc2c642011-07-02 00:01:44 +00003423static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003424 if (S.LangOpts.CUDA) {
3425 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00003426 if (Attr.hasParameterOrArguments()) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003427 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3428 return;
3429 }
3430
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003431 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003432 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003433 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003434 return;
3435 }
3436
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003437 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003438 } else {
3439 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3440 }
3441}
3442
Chandler Carruthedc2c642011-07-02 00:01:44 +00003443static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003444 if (S.LangOpts.CUDA) {
3445 // check the attribute arguments.
3446 if (Attr.getNumArgs() != 0) {
3447 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3448 return;
3449 }
3450
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003451 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003452 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003453 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003454 return;
3455 }
3456
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003457 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003458 } else {
3459 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3460 }
3461}
3462
Chandler Carruthedc2c642011-07-02 00:01:44 +00003463static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003464 if (S.LangOpts.CUDA) {
3465 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003466 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003467 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003468
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003469 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003470 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003471 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003472 return;
3473 }
3474
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003475 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003476 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00003477 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003478 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
3479 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3480 << FD->getType()
3481 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
3482 "void");
3483 } else {
3484 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3485 << FD->getType();
3486 }
3487 return;
3488 }
3489
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003490 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003491 } else {
3492 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3493 }
3494}
3495
Chandler Carruthedc2c642011-07-02 00:01:44 +00003496static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003497 if (S.LangOpts.CUDA) {
3498 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003499 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003500 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003501
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003502
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003503 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003504 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003505 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003506 return;
3507 }
3508
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003509 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003510 } else {
3511 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3512 }
3513}
3514
Chandler Carruthedc2c642011-07-02 00:01:44 +00003515static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003516 if (S.LangOpts.CUDA) {
3517 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003518 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003519 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003520
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003521
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003522 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003523 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003524 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003525 return;
3526 }
3527
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003528 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003529 } else {
3530 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3531 }
3532}
3533
Chandler Carruthedc2c642011-07-02 00:01:44 +00003534static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003535 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003536 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnereaad6b72009-04-14 16:30:50 +00003537 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003538
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003539 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00003540 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003541 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003542 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00003543 return;
3544 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003545
Douglas Gregor35b57532009-10-27 21:01:01 +00003546 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003547 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003548 return;
3549 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003550
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003551 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003552}
3553
Chandler Carruthedc2c642011-07-02 00:01:44 +00003554static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003555 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003556
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003557 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003558 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3559 CallingConv CC;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003560 if (S.CheckCallingConvAttr(Attr, CC))
John McCall3882ace2011-01-05 12:14:39 +00003561 return;
3562
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003563 if (!isa<ObjCMethodDecl>(D)) {
3564 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3565 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003566 return;
3567 }
3568
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003569 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003570 case AttributeList::AT_FastCall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003571 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003572 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003573 case AttributeList::AT_StdCall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003574 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003575 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003576 case AttributeList::AT_ThisCall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003577 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003578 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003579 case AttributeList::AT_CDecl:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003580 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003581 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003582 case AttributeList::AT_Pascal:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003583 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003584 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003585 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003586 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003587 switch (CC) {
3588 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003589 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003590 break;
3591 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003592 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003593 break;
3594 default:
3595 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003596 }
3597
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003598 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Derek Schuffa2020962012-10-16 22:30:41 +00003599 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003600 }
Derek Schuffa2020962012-10-16 22:30:41 +00003601 case AttributeList::AT_PnaclCall:
3602 D->addAttr(::new (S.Context) PnaclCallAttr(Attr.getRange(), S.Context));
3603 return;
3604
Abramo Bagnara50099372010-04-30 13:10:51 +00003605 default:
3606 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003607 }
3608}
3609
Chandler Carruthedc2c642011-07-02 00:01:44 +00003610static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth9312c642011-07-11 23:33:05 +00003611 assert(!Attr.isInvalid());
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003612 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003613}
3614
John McCall3882ace2011-01-05 12:14:39 +00003615bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
3616 if (attr.isInvalid())
3617 return true;
3618
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003619 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
3620 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
3621 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall3882ace2011-01-05 12:14:39 +00003622 attr.setInvalid();
3623 return true;
3624 }
3625
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003626 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3627 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003628 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003629 case AttributeList::AT_CDecl: CC = CC_C; break;
3630 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3631 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3632 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3633 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
3634 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003635 Expr *Arg = attr.getArg(0);
3636 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00003637 if (!Str || !Str->isAscii()) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003638 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3639 << "pcs" << 1;
3640 attr.setInvalid();
3641 return true;
3642 }
3643
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003644 StringRef StrRef = Str->getString();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003645 if (StrRef == "aapcs") {
3646 CC = CC_AAPCS;
3647 break;
3648 } else if (StrRef == "aapcs-vfp") {
3649 CC = CC_AAPCS_VFP;
3650 break;
3651 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003652
3653 attr.setInvalid();
3654 Diag(attr.getLoc(), diag::err_invalid_pcs);
3655 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003656 }
Derek Schuffa2020962012-10-16 22:30:41 +00003657 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003658 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003659 }
3660
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003661 const TargetInfo &TI = Context.getTargetInfo();
3662 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3663 if (A == TargetInfo::CCCR_Warning) {
3664 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
3665 CC = TI.getDefaultCallingConv();
3666 }
3667
John McCall3882ace2011-01-05 12:14:39 +00003668 return false;
3669}
3670
Chandler Carruthedc2c642011-07-02 00:01:44 +00003671static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003672 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003673
3674 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003675 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003676 return;
3677
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003678 if (!isa<ObjCMethodDecl>(D)) {
3679 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3680 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003681 return;
3682 }
Eli Friedman7044b762009-03-27 21:06:47 +00003683
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003684 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall3882ace2011-01-05 12:14:39 +00003685}
3686
3687/// Checks a regparm attribute, returning true if it is ill-formed and
3688/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003689bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3690 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003691 return true;
3692
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003693 if (Attr.getNumArgs() != 1) {
3694 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3695 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003696 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003697 }
Eli Friedman7044b762009-03-27 21:06:47 +00003698
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003699 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman7044b762009-03-27 21:06:47 +00003700 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003701 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall3882ace2011-01-05 12:14:39 +00003702 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003703 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman7044b762009-03-27 21:06:47 +00003704 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003705 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003706 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003707 }
3708
Douglas Gregore8bbc122011-09-02 00:18:52 +00003709 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003710 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003711 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003712 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003713 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003714 }
3715
John McCall3882ace2011-01-05 12:14:39 +00003716 numParams = NumParams.getZExtValue();
Douglas Gregore8bbc122011-09-02 00:18:52 +00003717 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003718 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003719 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003720 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003721 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003722 }
3723
John McCall3882ace2011-01-05 12:14:39 +00003724 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003725}
3726
Chandler Carruthedc2c642011-07-02 00:01:44 +00003727static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00003728 if (S.LangOpts.CUDA) {
3729 // check the attribute arguments.
3730 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00003731 // FIXME: 0 is not okay.
3732 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003733 return;
3734 }
3735
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003736 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00003737 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003738 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003739 return;
3740 }
3741
3742 Expr *MaxThreadsExpr = Attr.getArg(0);
3743 llvm::APSInt MaxThreads(32);
3744 if (MaxThreadsExpr->isTypeDependent() ||
3745 MaxThreadsExpr->isValueDependent() ||
3746 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3747 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3748 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3749 return;
3750 }
3751
3752 llvm::APSInt MinBlocks(32);
3753 if (Attr.getNumArgs() > 1) {
3754 Expr *MinBlocksExpr = Attr.getArg(1);
3755 if (MinBlocksExpr->isTypeDependent() ||
3756 MinBlocksExpr->isValueDependent() ||
3757 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3758 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3759 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3760 return;
3761 }
3762 }
3763
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003764 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne827301e2010-12-12 23:03:07 +00003765 MaxThreads.getZExtValue(),
3766 MinBlocks.getZExtValue()));
3767 } else {
3768 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3769 }
3770}
3771
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003772static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3773 const AttributeList &Attr) {
3774 StringRef AttrName = Attr.getName()->getName();
3775 if (!Attr.getParameterName()) {
3776 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
3777 << Attr.getName() << /* arg num = */ 1;
3778 return;
3779 }
3780
3781 if (Attr.getNumArgs() != 2) {
3782 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3783 << /* required args = */ 3;
3784 return;
3785 }
3786
3787 IdentifierInfo *ArgumentKind = Attr.getParameterName();
3788
3789 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3790 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3791 << Attr.getName() << ExpectedFunctionOrMethod;
3792 return;
3793 }
3794
3795 uint64_t ArgumentIdx;
3796 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3797 Attr.getLoc(), 2,
3798 Attr.getArg(0), ArgumentIdx))
3799 return;
3800
3801 uint64_t TypeTagIdx;
3802 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3803 Attr.getLoc(), 3,
3804 Attr.getArg(1), TypeTagIdx))
3805 return;
3806
3807 bool IsPointer = (AttrName == "pointer_with_type_tag");
3808 if (IsPointer) {
3809 // Ensure that buffer has a pointer type.
3810 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3811 if (!BufferTy->isPointerType()) {
3812 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
3813 << AttrName;
3814 }
3815 }
3816
3817 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(Attr.getRange(),
3818 S.Context,
3819 ArgumentKind,
3820 ArgumentIdx,
3821 TypeTagIdx,
3822 IsPointer));
3823}
3824
3825static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3826 const AttributeList &Attr) {
3827 IdentifierInfo *PointerKind = Attr.getParameterName();
3828 if (!PointerKind) {
3829 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
3830 << "type_tag_for_datatype" << 1;
3831 return;
3832 }
3833
3834 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
3835
3836 D->addAttr(::new (S.Context) TypeTagForDatatypeAttr(
3837 Attr.getRange(),
3838 S.Context,
3839 PointerKind,
3840 MatchingCType,
3841 Attr.getLayoutCompatible(),
3842 Attr.getMustBeNull()));
3843}
3844
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003845//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003846// Checker-specific attribute handlers.
3847//===----------------------------------------------------------------------===//
3848
John McCalled433932011-01-25 03:31:58 +00003849static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003850 return type->isDependentType() ||
3851 type->isObjCObjectPointerType() ||
3852 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003853}
3854static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003855 return type->isDependentType() ||
3856 type->isPointerType() ||
3857 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003858}
3859
Chandler Carruthedc2c642011-07-02 00:01:44 +00003860static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003861 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003862 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003863 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003864 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00003865 return;
3866 }
3867
3868 bool typeOK, cf;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003869 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003870 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3871 cf = false;
3872 } else {
3873 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3874 cf = true;
3875 }
3876
3877 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003878 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003879 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003880 return;
3881 }
3882
3883 if (cf)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003884 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003885 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003886 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003887}
3888
Chandler Carruthedc2c642011-07-02 00:01:44 +00003889static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3890 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003891 if (!isa<ObjCMethodDecl>(D)) {
3892 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003893 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00003894 return;
3895 }
3896
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003897 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003898}
3899
Chandler Carruthedc2c642011-07-02 00:01:44 +00003900static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3901 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003902
John McCalled433932011-01-25 03:31:58 +00003903 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003904
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003905 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003906 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003907 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003908 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003909 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003910 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3911 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003912 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003913 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003914 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003915 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003916 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003917 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003918 return;
3919 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003920
John McCalled433932011-01-25 03:31:58 +00003921 bool typeOK;
3922 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003923 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003924 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003925 case AttributeList::AT_NSReturnsAutoreleased:
3926 case AttributeList::AT_NSReturnsRetained:
3927 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003928 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3929 cf = false;
3930 break;
3931
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003932 case AttributeList::AT_CFReturnsRetained:
3933 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003934 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3935 cf = true;
3936 break;
3937 }
3938
3939 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003940 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003941 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003942 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003943 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003944
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003945 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003946 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003947 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003948 case AttributeList::AT_NSReturnsAutoreleased:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003949 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCalled433932011-01-25 03:31:58 +00003950 S.Context));
3951 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003952 case AttributeList::AT_CFReturnsNotRetained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003953 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003954 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003955 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003956 case AttributeList::AT_NSReturnsNotRetained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003957 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003958 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003959 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003960 case AttributeList::AT_CFReturnsRetained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003961 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003962 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003963 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003964 case AttributeList::AT_NSReturnsRetained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003965 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003966 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003967 return;
3968 };
3969}
3970
John McCallcf166702011-07-22 08:53:00 +00003971static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3972 const AttributeList &attr) {
3973 SourceLocation loc = attr.getLoc();
3974
3975 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3976
Fariborz Jahaniana53e5d72012-04-21 17:51:44 +00003977 if (!method) {
Fariborz Jahanian344d65c2012-04-20 22:00:46 +00003978 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003979 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCallcf166702011-07-22 08:53:00 +00003980 return;
3981 }
3982
3983 // Check that the method returns a normal pointer.
3984 QualType resultType = method->getResultType();
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003985
3986 if (!resultType->isReferenceType() &&
3987 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCallcf166702011-07-22 08:53:00 +00003988 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3989 << SourceRange(loc)
3990 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3991
3992 // Drop the attribute.
3993 return;
3994 }
3995
3996 method->addAttr(
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003997 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCallcf166702011-07-22 08:53:00 +00003998}
3999
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004000static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4001 const AttributeList &attr) {
4002 SourceLocation loc = attr.getLoc();
4003 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4004
4005 if (!method) {
4006 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4007 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4008 return;
4009 }
4010 DeclContext *DC = method->getDeclContext();
4011 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4012 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4013 << attr.getName() << 0;
4014 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4015 return;
4016 }
4017 if (method->getMethodFamily() == OMF_dealloc) {
4018 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4019 << attr.getName() << 1;
4020 return;
4021 }
4022
4023 method->addAttr(
4024 ::new (S.Context) ObjCRequiresSuperAttr(attr.getRange(), S.Context));
4025}
4026
John McCall32f5fe12011-09-30 05:12:12 +00004027/// Handle cf_audited_transfer and cf_unknown_transfer.
4028static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4029 if (!isa<FunctionDecl>(D)) {
4030 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004031 << A.getRange() << A.getName() << ExpectedFunction;
John McCall32f5fe12011-09-30 05:12:12 +00004032 return;
4033 }
4034
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004035 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall32f5fe12011-09-30 05:12:12 +00004036
4037 // Check whether there's a conflicting attribute already present.
4038 Attr *Existing;
4039 if (IsAudited) {
4040 Existing = D->getAttr<CFUnknownTransferAttr>();
4041 } else {
4042 Existing = D->getAttr<CFAuditedTransferAttr>();
4043 }
4044 if (Existing) {
4045 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4046 << A.getName()
4047 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4048 << A.getRange() << Existing->getRange();
4049 return;
4050 }
4051
4052 // All clear; add the attribute.
4053 if (IsAudited) {
4054 D->addAttr(
4055 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
4056 } else {
4057 D->addAttr(
4058 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
4059 }
4060}
4061
John McCallf1e8b342011-09-29 07:17:38 +00004062static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4063 const AttributeList &Attr) {
4064 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4065 if (!RD || RD->isUnion()) {
4066 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004067 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00004068 }
4069
4070 IdentifierInfo *ParmName = Attr.getParameterName();
4071
4072 // In Objective-C, verify that the type names an Objective-C type.
4073 // We don't want to check this outside of ObjC because people sometimes
4074 // do crazy C declarations of Objective-C types.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004075 if (ParmName && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00004076 // Check for an existing type with this name.
4077 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4078 Sema::LookupOrdinaryName);
4079 if (S.LookupName(R, Sc)) {
4080 NamedDecl *Target = R.getFoundDecl();
4081 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4082 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4083 S.Diag(Target->getLocStart(), diag::note_declared_at);
4084 }
4085 }
4086 }
4087
4088 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
4089 ParmName));
4090}
4091
Chandler Carruthedc2c642011-07-02 00:01:44 +00004092static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4093 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004094 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004095
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004096 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004097 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004098}
4099
Chandler Carruthedc2c642011-07-02 00:01:44 +00004100static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4101 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004102 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004103 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004104 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004105 return;
4106 }
4107
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004108 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004109 QualType type = vd->getType();
4110
4111 if (!type->isDependentType() &&
4112 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004113 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004114 << type;
4115 return;
4116 }
4117
4118 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4119
4120 // If we have no lifetime yet, check the lifetime we're presumably
4121 // going to infer.
4122 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4123 lifetime = type->getObjCARCImplicitLifetime();
4124
4125 switch (lifetime) {
4126 case Qualifiers::OCL_None:
4127 assert(type->isDependentType() &&
4128 "didn't infer lifetime for non-dependent type?");
4129 break;
4130
4131 case Qualifiers::OCL_Weak: // meaningful
4132 case Qualifiers::OCL_Strong: // meaningful
4133 break;
4134
4135 case Qualifiers::OCL_ExplicitNone:
4136 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004137 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004138 << (lifetime == Qualifiers::OCL_Autoreleasing);
4139 break;
4140 }
4141
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004142 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004143 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCall31168b02011-06-15 23:02:42 +00004144}
4145
Francois Picheta83957a2010-12-19 06:50:37 +00004146//===----------------------------------------------------------------------===//
4147// Microsoft specific attribute handlers.
4148//===----------------------------------------------------------------------===//
4149
Chandler Carruthedc2c642011-07-02 00:01:44 +00004150static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet0706d202011-09-17 17:15:52 +00004151 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Picheta83957a2010-12-19 06:50:37 +00004152 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004153 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Picheta83957a2010-12-19 06:50:37 +00004154 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004155
Francois Picheta83957a2010-12-19 06:50:37 +00004156 Expr *Arg = Attr.getArg(0);
4157 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00004158 if (!Str || !Str->isAscii()) {
Francois Pichet7da11662010-12-20 01:41:49 +00004159 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
4160 << "uuid" << 1;
4161 return;
4162 }
4163
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004164 StringRef StrRef = Str->getString();
Francois Pichet7da11662010-12-20 01:41:49 +00004165
4166 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4167 StrRef.back() == '}';
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004168
Francois Pichet7da11662010-12-20 01:41:49 +00004169 // Validate GUID length.
4170 if (IsCurly && StrRef.size() != 38) {
4171 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4172 return;
4173 }
4174 if (!IsCurly && StrRef.size() != 36) {
4175 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4176 return;
4177 }
4178
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004179 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichet7da11662010-12-20 01:41:49 +00004180 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004181 StringRef::iterator I = StrRef.begin();
Anders Carlsson19588aa2011-01-23 21:07:30 +00004182 if (IsCurly) // Skip the optional '{'
4183 ++I;
4184
4185 for (int i = 0; i < 36; ++i) {
Francois Pichet7da11662010-12-20 01:41:49 +00004186 if (i == 8 || i == 13 || i == 18 || i == 23) {
4187 if (*I != '-') {
4188 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4189 return;
4190 }
4191 } else if (!isxdigit(*I)) {
4192 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4193 return;
4194 }
4195 I++;
4196 }
Francois Picheta83957a2010-12-19 06:50:37 +00004197
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004198 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Picheta83957a2010-12-19 06:50:37 +00004199 Str->getString()));
Francois Pichet7da11662010-12-20 01:41:49 +00004200 } else
Francois Picheta83957a2010-12-19 06:50:37 +00004201 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davis163855f2010-02-16 18:27:26 +00004202}
4203
John McCall8d32c052012-05-22 21:28:12 +00004204static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Joao Matose30771f2012-09-04 17:18:12 +00004205 if (S.LangOpts.MicrosoftExt) {
4206 AttributeList::Kind Kind = Attr.getKind();
4207 if (Kind == AttributeList::AT_SingleInheritance)
4208 D->addAttr(
4209 ::new (S.Context) SingleInheritanceAttr(Attr.getRange(), S.Context));
4210 else if (Kind == AttributeList::AT_MultipleInheritance)
4211 D->addAttr(
4212 ::new (S.Context) MultipleInheritanceAttr(Attr.getRange(), S.Context));
4213 else if (Kind == AttributeList::AT_VirtualInheritance)
4214 D->addAttr(
4215 ::new (S.Context) VirtualInheritanceAttr(Attr.getRange(), S.Context));
4216 } else
John McCall8d32c052012-05-22 21:28:12 +00004217 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4218}
4219
4220static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4221 if (S.LangOpts.MicrosoftExt) {
4222 AttributeList::Kind Kind = Attr.getKind();
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004223 if (Kind == AttributeList::AT_Ptr32)
John McCall8d32c052012-05-22 21:28:12 +00004224 D->addAttr(
4225 ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context));
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004226 else if (Kind == AttributeList::AT_Ptr64)
John McCall8d32c052012-05-22 21:28:12 +00004227 D->addAttr(
4228 ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context));
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004229 else if (Kind == AttributeList::AT_Win64)
John McCall8d32c052012-05-22 21:28:12 +00004230 D->addAttr(
4231 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context));
4232 } else
4233 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4234}
4235
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004236static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4237 if (S.LangOpts.MicrosoftExt)
4238 D->addAttr(::new (S.Context) ForceInlineAttr(Attr.getRange(), S.Context));
4239 else
4240 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4241}
4242
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004243//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004244// Top Level Sema Entry Points
4245//===----------------------------------------------------------------------===//
4246
Chandler Carruthedc2c642011-07-02 00:01:44 +00004247static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4248 const AttributeList &Attr) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00004249 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004250 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4251 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4252 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00004253 default:
4254 break;
4255 }
4256}
Abramo Bagnara50099372010-04-30 13:10:51 +00004257
Chandler Carruthedc2c642011-07-02 00:01:44 +00004258static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4259 const AttributeList &Attr) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004260 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004261 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4262 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4263 case AttributeList::AT_IBOutletCollection:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004264 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004265 case AttributeList::AT_AddressSpace:
4266 case AttributeList::AT_OpenCLImageAccess:
4267 case AttributeList::AT_ObjCGC:
4268 case AttributeList::AT_VectorSize:
4269 case AttributeList::AT_NeonVectorType:
4270 case AttributeList::AT_NeonPolyVectorType:
Mike Stumpd3bb5572009-07-24 19:02:52 +00004271 // Ignore these, these are type attributes, handled by
4272 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004273 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004274 case AttributeList::AT_CUDADevice:
4275 case AttributeList::AT_CUDAHost:
4276 case AttributeList::AT_Overloadable:
Peter Collingbourneb331b262011-01-21 02:08:45 +00004277 // Ignore, this is a non-inheritable attribute, handled
4278 // by ProcessNonInheritableDeclAttr.
4279 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004280 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4281 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4282 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4283 case AttributeList::AT_AlwaysInline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004284 handleAlwaysInlineAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004285 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004286 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00004287 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004288 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4289 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4290 case AttributeList::AT_CarriesDependency:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004291 handleDependencyAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004292 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4293 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4294 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
4295 case AttributeList::AT_Deprecated:
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004296 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated");
4297 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004298 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4299 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004300 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004301 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004302 case AttributeList::AT_MinSize:
4303 handleMinSizeAttr(S, D, Attr);
4304 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004305 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4306 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4307 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4308 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4309 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004310 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004311 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004312 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4313 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4314 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4315 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4316 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00004317 case AttributeList::AT_ownership_returns:
4318 case AttributeList::AT_ownership_takes:
4319 case AttributeList::AT_ownership_holds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004320 handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004321 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4322 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4323 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4324 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4325 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4326 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4327 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004328
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004329 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004330 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004331 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004332 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004333
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004334 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004335 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4336
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004337 case AttributeList::AT_ObjCRequiresSuper:
4338 handleObjCRequiresSuperAttr(S, D, Attr); break;
4339
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004340 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00004341 handleNSBridgedAttr(S, scope, D, Attr); break;
4342
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004343 case AttributeList::AT_CFAuditedTransfer:
4344 case AttributeList::AT_CFUnknownTransfer:
John McCall32f5fe12011-09-30 05:12:12 +00004345 handleCFTransferAttr(S, D, Attr); break;
4346
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004347 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004348 case AttributeList::AT_CFConsumed:
4349 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4350 case AttributeList::AT_NSConsumesSelf:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004351 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004352
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004353 case AttributeList::AT_NSReturnsAutoreleased:
4354 case AttributeList::AT_NSReturnsNotRetained:
4355 case AttributeList::AT_CFReturnsNotRetained:
4356 case AttributeList::AT_NSReturnsRetained:
4357 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004358 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004359
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004360 case AttributeList::AT_WorkGroupSizeHint:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004361 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004362 handleWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00004363
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004364 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004365 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004366
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004367 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4368 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4369 case AttributeList::AT_Unavailable:
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004370 handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable");
4371 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004372 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00004373 handleArcWeakrefUnavailableAttr (S, D, Attr);
4374 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004375 case AttributeList::AT_ObjCRootClass:
Patrick Beardacfbe9e2012-04-06 18:12:22 +00004376 handleObjCRootClassAttr(S, D, Attr);
4377 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004378 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00004379 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00004380 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004381 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4382 case AttributeList::AT_ReturnsTwice:
Rafael Espindola70107f92011-10-03 14:59:42 +00004383 handleReturnsTwiceAttr(S, D, Attr);
4384 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004385 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
4386 case AttributeList::AT_Visibility: handleVisibilityAttr (S, D, Attr); break;
4387 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004388 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004389 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4390 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4391 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4392 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004393 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004394 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004395 case AttributeList::AT_ObjCException:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004396 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00004397 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004398 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004399 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004400 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004401 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4402 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4403 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4404 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4405 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4406 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4407 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4408 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4409 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004410 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004411 // Just ignore
4412 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004413 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00004414 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00004415 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004416 case AttributeList::AT_StdCall:
4417 case AttributeList::AT_CDecl:
4418 case AttributeList::AT_FastCall:
4419 case AttributeList::AT_ThisCall:
4420 case AttributeList::AT_Pascal:
4421 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004422 case AttributeList::AT_PnaclCall:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004423 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004424 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004425 case AttributeList::AT_OpenCLKernel:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004426 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00004427 break;
John McCall8d32c052012-05-22 21:28:12 +00004428
4429 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004430 case AttributeList::AT_MsStruct:
John McCall8d32c052012-05-22 21:28:12 +00004431 handleMsStructAttr(S, D, Attr);
4432 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004433 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004434 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004435 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004436 case AttributeList::AT_SingleInheritance:
4437 case AttributeList::AT_MultipleInheritance:
4438 case AttributeList::AT_VirtualInheritance:
John McCall8d32c052012-05-22 21:28:12 +00004439 handleInheritanceAttr(S, D, Attr);
4440 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004441 case AttributeList::AT_Win64:
4442 case AttributeList::AT_Ptr32:
4443 case AttributeList::AT_Ptr64:
John McCall8d32c052012-05-22 21:28:12 +00004444 handlePortabilityAttr(S, D, Attr);
4445 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004446 case AttributeList::AT_ForceInline:
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004447 handleForceInlineAttr(S, D, Attr);
4448 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004449
4450 // Thread safety attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004451 case AttributeList::AT_GuardedVar:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004452 handleGuardedVarAttr(S, D, Attr);
4453 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004454 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004455 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004456 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004457 case AttributeList::AT_ScopedLockable:
Michael Han3be3b442012-07-23 18:48:41 +00004458 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004459 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004460 case AttributeList::AT_NoAddressSafetyAnalysis:
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004461 handleNoAddressSafetyAttr(S, D, Attr);
4462 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004463 case AttributeList::AT_NoThreadSafetyAnalysis:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004464 handleNoThreadSafetyAttr(S, D, Attr);
4465 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004466 case AttributeList::AT_Lockable:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004467 handleLockableAttr(S, D, Attr);
4468 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004469 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004470 handleGuardedByAttr(S, D, Attr);
4471 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004472 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004473 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004474 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004475 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004476 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004477 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004478 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004479 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004480 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004481 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004482 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004483 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004484 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004485 handleLockReturnedAttr(S, D, Attr);
4486 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004487 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004488 handleLocksExcludedAttr(S, D, Attr);
4489 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004490 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004491 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004492 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004493 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004494 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004495 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004496 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004497 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004498 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004499 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004500 handleUnlockFunAttr(S, D, Attr);
4501 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004502 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004503 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004504 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004505 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004506 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004507 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004508
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004509 // Type safety attributes.
4510 case AttributeList::AT_ArgumentWithTypeTag:
4511 handleArgumentWithTypeTagAttr(S, D, Attr);
4512 break;
4513 case AttributeList::AT_TypeTagForDatatype:
4514 handleTypeTagForDatatypeAttr(S, D, Attr);
4515 break;
4516
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004517 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004518 // Ask target about the attribute.
4519 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4520 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004521 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4522 diag::warn_unhandled_ms_attribute_ignored :
4523 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004524 break;
4525 }
4526}
4527
Peter Collingbourneb331b262011-01-21 02:08:45 +00004528/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4529/// the attribute applies to decls. If the attribute is a type attribute, just
4530/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
4531/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruthedc2c642011-07-02 00:01:44 +00004532static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4533 const AttributeList &Attr,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004534 bool NonInheritable, bool Inheritable) {
4535 if (Attr.isInvalid())
4536 return;
4537
Aaron Ballman478faed2012-06-19 22:09:27 +00004538 // Type attributes are still treated as declaration attributes by
4539 // ParseMicrosoftTypeAttributes and ParseBorlandTypeAttributes. We don't
4540 // want to process them, however, because we will simply warn about ignoring
4541 // them. So instead, we will bail out early.
4542 if (Attr.isMSTypespecAttribute())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004543 return;
4544
4545 if (NonInheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00004546 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00004547
4548 if (Inheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00004549 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00004550}
4551
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004552/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4553/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004554void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004555 const AttributeList *AttrList,
4556 bool NonInheritable, bool Inheritable) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004557 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00004558 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola3c9d9472012-05-07 23:58:18 +00004559 }
Rafael Espindolac18086a2010-02-23 22:00:30 +00004560
4561 // GCC accepts
4562 // static int a9 __attribute__((weakref));
4563 // but that looks really pointless. We reject it.
Peter Collingbourneb331b262011-01-21 02:08:45 +00004564 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004565 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekd21139a2010-07-31 01:52:11 +00004566 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004567 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004568 }
4569}
4570
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004571// Annotation attributes are the only attributes allowed after an access
4572// specifier.
4573bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4574 const AttributeList *AttrList) {
4575 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004576 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004577 handleAnnotateAttr(*this, ASDecl, *l);
4578 } else {
4579 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4580 return true;
4581 }
4582 }
4583
4584 return false;
4585}
4586
John McCall42856de2011-10-01 05:17:03 +00004587/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4588/// contains any decl attributes that we should warn about.
4589static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4590 for ( ; A; A = A->getNext()) {
4591 // Only warn if the attribute is an unignored, non-type attribute.
4592 if (A->isUsedAsTypeAttr()) continue;
4593 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4594
4595 if (A->getKind() == AttributeList::UnknownAttribute) {
4596 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4597 << A->getName() << A->getRange();
4598 } else {
4599 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4600 << A->getName() << A->getRange();
4601 }
4602 }
4603}
4604
4605/// checkUnusedDeclAttributes - Given a declarator which is not being
4606/// used to build a declaration, complain about any decl attributes
4607/// which might be lying around on it.
4608void Sema::checkUnusedDeclAttributes(Declarator &D) {
4609 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4610 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4611 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4612 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4613}
4614
Ryan Flynn7d470f32009-07-30 03:15:39 +00004615/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004616/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004617NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4618 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004619 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004620 NamedDecl *NewD = 0;
4621 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004622 FunctionDecl *NewFD;
4623 // FIXME: Missing call to CheckFunctionDeclaration().
4624 // FIXME: Mangling?
4625 // FIXME: Is the qualifier info correct?
4626 // FIXME: Is the DeclContext correct?
4627 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4628 Loc, Loc, DeclarationName(II),
4629 FD->getType(), FD->getTypeSourceInfo(),
4630 SC_None, SC_None,
4631 false/*isInlineSpecified*/,
4632 FD->hasPrototype(),
4633 false/*isConstexprSpecified*/);
4634 NewD = NewFD;
4635
4636 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004637 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004638
4639 // Fake up parameter variables; they are declared as if this were
4640 // a typedef.
4641 QualType FDTy = FD->getType();
4642 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4643 SmallVector<ParmVarDecl*, 16> Params;
4644 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4645 AE = FT->arg_type_end(); AI != AE; ++AI) {
4646 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4647 Param->setScopeInfo(0, Params.size());
4648 Params.push_back(Param);
4649 }
David Blaikie9c70e042011-09-21 18:16:56 +00004650 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004651 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004652 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4653 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004654 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004655 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00004656 VD->getStorageClass(),
4657 VD->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00004658 if (VD->getQualifier()) {
4659 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004660 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004661 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004662 }
4663 return NewD;
4664}
4665
James Dennett634962f2012-06-14 21:40:34 +00004666/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004667/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004668void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004669 if (W.getUsed()) return; // only do this once
4670 W.setUsed(true);
4671 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4672 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004673 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004674 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4675 NDId->getName()));
4676 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00004677 WeakTopLevelDecl.push_back(NewD);
4678 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4679 // to insert Decl at TU scope, sorry.
4680 DeclContext *SavedContext = CurContext;
4681 CurContext = Context.getTranslationUnitDecl();
4682 PushOnScopeChains(NewD, S);
4683 CurContext = SavedContext;
4684 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004685 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004686 }
4687}
4688
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004689/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4690/// it, apply them to D. This is a bit tricky because PD can have attributes
4691/// specified in many different places, and we need to find and apply them all.
Peter Collingbourneb331b262011-01-21 02:08:45 +00004692void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
4693 bool NonInheritable, bool Inheritable) {
John McCall6fe02402010-10-27 00:59:00 +00004694 // It's valid to "forward-declare" #pragma weak, in which case we
4695 // have to do this.
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00004696 if (Inheritable) {
4697 LoadExternalWeakUndeclaredIdentifiers();
4698 if (!WeakUndeclaredIdentifiers.empty()) {
4699 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
4700 if (IdentifierInfo *Id = ND->getIdentifier()) {
4701 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4702 = WeakUndeclaredIdentifiers.find(Id);
4703 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
4704 WeakInfo W = I->second;
4705 DeclApplyPragmaWeak(S, ND, W);
4706 WeakUndeclaredIdentifiers[Id] = W;
4707 }
John McCall6fe02402010-10-27 00:59:00 +00004708 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004709 }
4710 }
4711 }
4712
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004713 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004714 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004715 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004716
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004717 // Walk the declarator structure, applying decl attributes that were in a type
4718 // position to the decl itself. This handles cases like:
4719 // int *__attr__(x)** D;
4720 // when X is a decl attribute.
4721 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4722 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004723 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004724
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004725 // Finally, apply any attributes on the decl itself.
4726 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004727 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004728}
John McCall28a6aea2009-11-04 02:18:39 +00004729
John McCall31168b02011-06-15 23:02:42 +00004730/// Is the given declaration allowed to use a forbidden type?
4731static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4732 // Private ivars are always okay. Unfortunately, people don't
4733 // always properly make their ivars private, even in system headers.
4734 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004735 // Function declarations in sys headers will be marked unavailable.
4736 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4737 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004738 return false;
4739
4740 // Require it to be declared in a system header.
4741 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4742}
4743
4744/// Handle a delayed forbidden-type diagnostic.
4745static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4746 Decl *decl) {
4747 if (decl && isForbiddenTypeAllowed(S, decl)) {
4748 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4749 "this system declaration uses an unsupported type"));
4750 return;
4751 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004752 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004753 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004754 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004755 // kind of forbidden type messages on unavailable functions.
4756 if (FD->hasAttr<UnavailableAttr>() &&
4757 diag.getForbiddenTypeDiagnostic() ==
4758 diag::err_arc_array_param_no_ownership) {
4759 diag.Triggered = true;
4760 return;
4761 }
4762 }
John McCall31168b02011-06-15 23:02:42 +00004763
4764 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4765 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4766 diag.Triggered = true;
4767}
4768
John McCall2ec85372012-05-07 06:16:41 +00004769void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4770 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004771 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004772 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004773
John McCall2ec85372012-05-07 06:16:41 +00004774 // When delaying diagnostics to run in the context of a parsed
4775 // declaration, we only want to actually emit anything if parsing
4776 // succeeds.
4777 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004778
John McCall2ec85372012-05-07 06:16:41 +00004779 // We emit all the active diagnostics in this pool or any of its
4780 // parents. In general, we'll get one pool for the decl spec
4781 // and a child pool for each declarator; in a decl group like:
4782 // deprecated_typedef foo, *bar, baz();
4783 // only the declarator pops will be passed decls. This is correct;
4784 // we really do need to consider delayed diagnostics from the decl spec
4785 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004786 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004787 do {
John McCall6347b682012-05-07 06:16:58 +00004788 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004789 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4790 // This const_cast is a bit lame. Really, Triggered should be mutable.
4791 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004792 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004793 continue;
4794
John McCallc1465822011-02-14 07:13:47 +00004795 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004796 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00004797 // Don't bother giving deprecation diagnostics if the decl is invalid.
4798 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00004799 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004800 break;
4801
4802 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004803 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004804 break;
John McCall31168b02011-06-15 23:02:42 +00004805
4806 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004807 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004808 break;
John McCall86121512010-01-27 03:50:35 +00004809 }
4810 }
John McCall2ec85372012-05-07 06:16:41 +00004811 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004812}
4813
John McCall6347b682012-05-07 06:16:58 +00004814/// Given a set of delayed diagnostics, re-emit them as if they had
4815/// been delayed in the current context instead of in the given pool.
4816/// Essentially, this just moves them to the current pool.
4817void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4818 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4819 assert(curPool && "re-emitting in undelayed context not supported");
4820 curPool->steal(pool);
4821}
4822
John McCall28a6aea2009-11-04 02:18:39 +00004823static bool isDeclDeprecated(Decl *D) {
4824 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004825 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004826 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004827 // A category implicitly has the availability of the interface.
4828 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4829 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004830 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4831 return false;
4832}
4833
Eli Friedman971bfa12012-08-08 21:52:41 +00004834static void
4835DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
4836 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004837 const ObjCInterfaceDecl *UnknownObjCClass,
4838 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedman971bfa12012-08-08 21:52:41 +00004839 DeclarationName Name = D->getDeclName();
4840 if (!Message.empty()) {
4841 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
4842 S.Diag(D->getLocation(),
4843 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4844 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004845 if (ObjCPropery)
4846 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4847 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00004848 } else if (!UnknownObjCClass) {
4849 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
4850 S.Diag(D->getLocation(),
4851 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4852 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004853 if (ObjCPropery)
4854 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4855 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00004856 } else {
4857 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
4858 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4859 }
4860}
4861
John McCallb45a1e72010-08-26 02:13:20 +00004862void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00004863 Decl *Ctx) {
4864 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00004865 return;
4866
John McCall86121512010-01-27 03:50:35 +00004867 DD.Triggered = true;
Eli Friedman971bfa12012-08-08 21:52:41 +00004868 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
4869 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004870 DD.getUnknownObjCClass(),
4871 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004872}
4873
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004874void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004875 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004876 const ObjCInterfaceDecl *UnknownObjCClass,
4877 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004878 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004879 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004880 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4881 UnknownObjCClass,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004882 ObjCProperty,
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004883 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004884 return;
4885 }
4886
4887 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00004888 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00004889 return;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004890 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004891}