blob: e326a20c87d0517013cfdba789f0b91d8813cda4 [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.
Eli Friedmanc087c3f2012-11-07 00:35:20 +0000966 if (!FD->getType()->isDependentType() &&
967 !FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000968 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +0000969 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000970 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000971 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000972 FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000973 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000974 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000975}
976
Chandler Carruthedc2c642011-07-02 00:01:44 +0000977static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman9ee2d0472012-10-12 23:29:20 +0000978 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
979 RD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +0000980 else
981 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
982}
983
Chandler Carruthedc2c642011-07-02 00:01:44 +0000984static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000985 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +0000986 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000987 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000988
Ted Kremenek1f672822010-02-18 03:08:58 +0000989 // The IBAction attributes only apply to instance methods.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000990 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek1f672822010-02-18 03:08:58 +0000991 if (MD->isInstanceMethod()) {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +0000992 D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context));
Ted Kremenek1f672822010-02-18 03:08:58 +0000993 return;
994 }
995
Ted Kremenekd68ec812011-02-04 06:54:16 +0000996 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek1f672822010-02-18 03:08:58 +0000997}
998
Ted Kremenek7fd17232011-09-29 07:02:25 +0000999static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1000 // The IBOutlet/IBOutletCollection attributes only apply to instance
1001 // variables or properties of Objective-C classes. The outlet must also
1002 // have an object reference type.
1003 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1004 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek5d6044e2011-11-01 18:08:35 +00001005 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001006 << Attr.getName() << VD->getType() << 0;
1007 return false;
1008 }
1009 }
1010 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1011 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001012 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek7fd17232011-09-29 07:02:25 +00001013 << Attr.getName() << PD->getType() << 1;
1014 return false;
1015 }
1016 }
1017 else {
1018 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1019 return false;
1020 }
Douglas Gregor5c3cc422012-03-14 16:55:17 +00001021
Ted Kremenek7fd17232011-09-29 07:02:25 +00001022 return true;
1023}
1024
Chandler Carruthedc2c642011-07-02 00:01:44 +00001025static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek1f672822010-02-18 03:08:58 +00001026 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001027 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek1f672822010-02-18 03:08:58 +00001028 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001029
1030 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek1f672822010-02-18 03:08:58 +00001031 return;
Ted Kremenek1f672822010-02-18 03:08:58 +00001032
Ted Kremenek7fd17232011-09-29 07:02:25 +00001033 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context));
Ted Kremenek8e3704d2008-07-15 22:26:48 +00001034}
1035
Chandler Carruthedc2c642011-07-02 00:01:44 +00001036static void handleIBOutletCollection(Sema &S, Decl *D,
1037 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001038
1039 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001040 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek26bde772010-05-19 17:38:06 +00001041 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1042 return;
1043 }
1044
Ted Kremenek7fd17232011-09-29 07:02:25 +00001045 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek26bde772010-05-19 17:38:06 +00001046 return;
Ted Kremenek7fd17232011-09-29 07:02:25 +00001047
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001048 IdentifierInfo *II = Attr.getParameterName();
1049 if (!II)
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001050 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian798f8322010-08-17 21:39:27 +00001051
John McCallba7bf592010-08-24 05:47:05 +00001052 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001053 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001054 if (!TypeRep) {
1055 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1056 return;
1057 }
John McCallba7bf592010-08-24 05:47:05 +00001058 QualType QT = TypeRep.get();
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001059 // Diagnose use of non-object type in iboutletcollection attribute.
1060 // FIXME. Gnu attribute extension ignores use of builtin types in
1061 // attributes. So, __attribute__((iboutletcollection(char))) will be
1062 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanian2f31b332011-10-18 19:54:31 +00001063 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +00001064 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1065 return;
1066 }
Argyrios Kyrtzidis8db67df2011-09-13 18:41:59 +00001067 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context,
1068 QT, Attr.getParameterLoc()));
Ted Kremenek26bde772010-05-19 17:38:06 +00001069}
1070
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001071static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001072 if (const RecordType *UT = T->getAsUnionType())
1073 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1074 RecordDecl *UD = UT->getDecl();
1075 for (RecordDecl::field_iterator it = UD->field_begin(),
1076 itend = UD->field_end(); it != itend; ++it) {
1077 QualType QT = it->getType();
1078 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1079 T = QT;
1080 return;
1081 }
1082 }
1083 }
1084}
1085
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001086static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopese881ce22012-06-18 16:39:04 +00001087 if (!isFunctionOrMethod(D)) {
1088 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1089 << "alloc_size" << ExpectedFunctionOrMethod;
1090 return;
1091 }
1092
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001093 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1094 return;
1095
1096 // In C++ the implicit 'this' function parameter also counts, and they are
1097 // counted from one.
1098 bool HasImplicitThisParam = isInstanceMethod(D);
1099 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
1100
1101 SmallVector<unsigned, 8> SizeArgs;
1102
1103 for (AttributeList::arg_iterator I = Attr.arg_begin(),
1104 E = Attr.arg_end(); I!=E; ++I) {
1105 // The argument must be an integer constant expression.
1106 Expr *Ex = *I;
1107 llvm::APSInt ArgNum;
1108 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1109 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
1110 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1111 << "alloc_size" << Ex->getSourceRange();
1112 return;
1113 }
1114
1115 uint64_t x = ArgNum.getZExtValue();
1116
1117 if (x < 1 || x > NumArgs) {
1118 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1119 << "alloc_size" << I.getArgNum() << Ex->getSourceRange();
1120 return;
1121 }
1122
1123 --x;
1124 if (HasImplicitThisParam) {
1125 if (x == 0) {
1126 S.Diag(Attr.getLoc(),
1127 diag::err_attribute_invalid_implicit_this_argument)
1128 << "alloc_size" << Ex->getSourceRange();
1129 return;
1130 }
1131 --x;
1132 }
1133
1134 // check if the function argument is of an integer type
1135 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
1136 if (!T->isIntegerType()) {
1137 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1138 << "alloc_size" << Ex->getSourceRange();
1139 return;
1140 }
1141
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001142 SizeArgs.push_back(x);
1143 }
1144
1145 // check if the function returns a pointer
1146 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1147 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1148 << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
1149 }
1150
Nuno Lopese44e93a2012-06-18 16:27:56 +00001151 D->addAttr(::new (S.Context) AllocSizeAttr(Attr.getRange(), S.Context,
1152 SizeArgs.data(), SizeArgs.size()));
Nuno Lopes5c7ad162012-05-24 00:22:00 +00001153}
1154
Chandler Carruthedc2c642011-07-02 00:01:44 +00001155static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001156 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1157 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001158 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001159 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001160 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001161 return;
1162 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001163
Chandler Carruth743682b2010-11-16 08:35:43 +00001164 // In C++ the implicit 'this' function parameter also counts, and they are
1165 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001166 bool HasImplicitThisParam = isInstanceMethod(D);
1167 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001168
1169 // The nonnull attribute only applies to pointers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001170 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001171
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001172 for (AttributeList::arg_iterator I=Attr.arg_begin(),
1173 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001174
1175
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001176 // The argument must be an integer constant expression.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001177 Expr *Ex = *I;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001178 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001179 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
1180 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001181 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1182 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001183 return;
1184 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001185
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001186 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001187
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001188 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001189 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +00001190 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001191 return;
1192 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001193
Ted Kremenek5224e6a2008-07-21 22:09:15 +00001194 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +00001195 if (HasImplicitThisParam) {
1196 if (x == 0) {
1197 S.Diag(Attr.getLoc(),
1198 diag::err_attribute_invalid_implicit_this_argument)
1199 << "nonnull" << Ex->getSourceRange();
1200 return;
1201 }
1202 --x;
1203 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001204
1205 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001206 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001207 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +00001208
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001209 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001210 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +00001211 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +00001212 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001213 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001214 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001215
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001216 NonNullArgs.push_back(x);
1217 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001218
1219 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1220 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001221 if (NonNullArgs.empty()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001222 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
1223 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +00001224 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +00001225 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbar70e3eba2008-10-19 02:04:16 +00001226 NonNullArgs.push_back(I);
Ted Kremenek5fa50522008-11-18 06:52:58 +00001227 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001228
Ted Kremenek22813f42010-10-21 18:49:36 +00001229 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001230 if (NonNullArgs.empty()) {
1231 // Warn the trivial case only if attribute is not coming from a
1232 // macro instantiation.
1233 if (Attr.getLoc().isFileID())
1234 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001235 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +00001236 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001237 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +00001238
1239 unsigned* start = &NonNullArgs[0];
1240 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001241 llvm::array_pod_sort(start, start + size);
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001242 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001243 size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +00001244}
1245
Chandler Carruthedc2c642011-07-02 00:01:44 +00001246static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001247 // This attribute must be applied to a function declaration.
1248 // The first argument to the attribute must be a string,
1249 // the name of the resource, for example "malloc".
1250 // The following arguments must be argument indexes, the arguments must be
1251 // of integer type for Returns, otherwise of pointer type.
1252 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001253 // after being held. free() should be __attribute((ownership_takes)), whereas
1254 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +00001255
1256 if (!AL.getParameterName()) {
1257 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
1258 << AL.getName()->getName() << 1;
1259 return;
1260 }
1261 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001262 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001263 switch (AL.getKind()) {
1264 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001265 K = OwnershipAttr::Takes;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001266 if (AL.getNumArgs() < 1) {
1267 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1268 return;
1269 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001270 break;
1271 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001272 K = OwnershipAttr::Holds;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001273 if (AL.getNumArgs() < 1) {
1274 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1275 return;
1276 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001277 break;
1278 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001279 K = OwnershipAttr::Returns;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001280 if (AL.getNumArgs() > 1) {
1281 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1282 << AL.getNumArgs() + 1;
1283 return;
1284 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001285 break;
1286 default:
1287 // This should never happen given how we are called.
1288 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +00001289 }
1290
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001291 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +00001292 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1293 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001294 return;
1295 }
1296
Chandler Carruth743682b2010-11-16 08:35:43 +00001297 // In C++ the implicit 'this' function parameter also counts, and they are
1298 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001299 bool HasImplicitThisParam = isInstanceMethod(D);
1300 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001301
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001302 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekd21139a2010-07-31 01:52:11 +00001303
1304 // Normalize the argument, __foo__ becomes foo.
1305 if (Module.startswith("__") && Module.endswith("__"))
1306 Module = Module.substr(2, Module.size() - 4);
1307
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001308 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001309
Jordy Rose5af0e3c2010-08-12 08:54:03 +00001310 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1311 ++I) {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001312
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001313 Expr *IdxExpr = *I;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001314 llvm::APSInt ArgNum(32);
1315 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1316 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1317 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1318 << AL.getName()->getName() << IdxExpr->getSourceRange();
1319 continue;
1320 }
1321
1322 unsigned x = (unsigned) ArgNum.getZExtValue();
1323
1324 if (x > NumArgs || x < 1) {
1325 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1326 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1327 continue;
1328 }
1329 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +00001330 if (HasImplicitThisParam) {
1331 if (x == 0) {
1332 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1333 << "ownership" << IdxExpr->getSourceRange();
1334 return;
1335 }
1336 --x;
1337 }
1338
Ted Kremenekd21139a2010-07-31 01:52:11 +00001339 switch (K) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001340 case OwnershipAttr::Takes:
1341 case OwnershipAttr::Holds: {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001342 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001343 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001344 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1345 // FIXME: Should also highlight argument in decl.
1346 S.Diag(AL.getLoc(), diag::err_ownership_type)
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001347 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekd21139a2010-07-31 01:52:11 +00001348 << "pointer"
1349 << IdxExpr->getSourceRange();
1350 continue;
1351 }
1352 break;
1353 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001354 case OwnershipAttr::Returns: {
Ted Kremenekd21139a2010-07-31 01:52:11 +00001355 if (AL.getNumArgs() > 1) {
1356 // Is the function argument an integer type?
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001357 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekd21139a2010-07-31 01:52:11 +00001358 llvm::APSInt ArgNum(32);
1359 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1360 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1361 S.Diag(AL.getLoc(), diag::err_ownership_type)
1362 << "ownership_returns" << "integer"
1363 << IdxExpr->getSourceRange();
1364 return;
1365 }
1366 }
1367 break;
1368 }
Ted Kremenekd21139a2010-07-31 01:52:11 +00001369 } // switch
1370
1371 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001372 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001373 i = D->specific_attr_begin<OwnershipAttr>(),
1374 e = D->specific_attr_end<OwnershipAttr>();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001375 i != e; ++i) {
1376 if ((*i)->getOwnKind() != K) {
1377 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1378 I!=E; ++I) {
1379 if (x == *I) {
1380 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1381 << AL.getName()->getName() << "ownership_*";
Ted Kremenekd21139a2010-07-31 01:52:11 +00001382 }
1383 }
1384 }
1385 }
1386 OwnershipArgs.push_back(x);
1387 }
1388
1389 unsigned* start = OwnershipArgs.data();
1390 unsigned size = OwnershipArgs.size();
1391 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001392
1393 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1394 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1395 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +00001396 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001397
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001398 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001399 start, size));
Ted Kremenekd21139a2010-07-31 01:52:11 +00001400}
1401
John McCall7a198ce2011-02-08 22:35:49 +00001402/// Whether this declaration has internal linkage for the purposes of
1403/// things that want to complain about things not have internal linkage.
1404static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1405 switch (D->getLinkage()) {
1406 case NoLinkage:
1407 case InternalLinkage:
1408 return true;
1409
1410 // Template instantiations that go from external to unique-external
1411 // shouldn't get diagnosed.
1412 case UniqueExternalLinkage:
1413 return true;
1414
1415 case ExternalLinkage:
1416 return false;
1417 }
1418 llvm_unreachable("unknown linkage kind!");
Rafael Espindolac18086a2010-02-23 22:00:30 +00001419}
1420
Chandler Carruthedc2c642011-07-02 00:01:44 +00001421static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001422 // Check the attribute arguments.
1423 if (Attr.getNumArgs() > 1) {
1424 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1425 return;
1426 }
1427
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001428 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall7a198ce2011-02-08 22:35:49 +00001429 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001430 << Attr.getName() << ExpectedVariableOrFunction;
John McCall7a198ce2011-02-08 22:35:49 +00001431 return;
1432 }
1433
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001434 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001435
Rafael Espindolac18086a2010-02-23 22:00:30 +00001436 // gcc rejects
1437 // class c {
1438 // static int a __attribute__((weakref ("v2")));
1439 // static int b() __attribute__((weakref ("f3")));
1440 // };
1441 // and ignores the attributes of
1442 // void f(void) {
1443 // static int a __attribute__((weakref ("v2")));
1444 // }
1445 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001446 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001447 if (!Ctx->isFileContext()) {
1448 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +00001449 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +00001450 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +00001451 }
1452
1453 // The GCC manual says
1454 //
1455 // At present, a declaration to which `weakref' is attached can only
1456 // be `static'.
1457 //
1458 // It also says
1459 //
1460 // Without a TARGET,
1461 // given as an argument to `weakref' or to `alias', `weakref' is
1462 // equivalent to `weak'.
1463 //
1464 // gcc 4.4.1 will accept
1465 // int a7 __attribute__((weakref));
1466 // as
1467 // int a7 __attribute__((weak));
1468 // This looks like a bug in gcc. We reject that for now. We should revisit
1469 // it if this behaviour is actually used.
1470
John McCall7a198ce2011-02-08 22:35:49 +00001471 if (!hasEffectivelyInternalLinkage(nd)) {
1472 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindolac18086a2010-02-23 22:00:30 +00001473 return;
1474 }
1475
1476 // GCC rejects
1477 // static ((alias ("y"), weakref)).
1478 // Should we? How to check that weakref is before or after alias?
1479
1480 if (Attr.getNumArgs() == 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001481 Expr *Arg = Attr.getArg(0);
Rafael Espindolac18086a2010-02-23 22:00:30 +00001482 Arg = Arg->IgnoreParenCasts();
1483 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1484
Douglas Gregorfb65e592011-07-27 05:40:30 +00001485 if (!Str || !Str->isAscii()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00001486 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1487 << "weakref" << 1;
1488 return;
1489 }
1490 // GCC will accept anything as the argument of weakref. Should we
1491 // check for an existing decl?
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001492 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001493 Str->getString()));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001494 }
1495
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001496 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
Rafael Espindolac18086a2010-02-23 22:00:30 +00001497}
1498
Chandler Carruthedc2c642011-07-02 00:01:44 +00001499static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001500 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001501 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001502 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001503 return;
1504 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001505
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001506 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001507 Arg = Arg->IgnoreParenCasts();
1508 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001509
Douglas Gregorfb65e592011-07-27 05:40:30 +00001510 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001511 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001512 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001513 return;
1514 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001515
Douglas Gregore8bbc122011-09-02 00:18:52 +00001516 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +00001517 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1518 return;
1519 }
1520
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001521 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +00001522
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001523 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001524 Str->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001525}
1526
Quentin Colombet4e172062012-11-01 23:55:47 +00001527static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1528 // Check the attribute arguments.
1529 if (!checkAttributeNumArgs(S, Attr, 0))
1530 return;
1531
1532 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1533 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1534 << Attr.getName() << ExpectedFunctionOrMethod;
1535 return;
1536 }
1537
1538 D->addAttr(::new (S.Context) MinSizeAttr(Attr.getRange(), S.Context));
1539}
1540
Benjamin Kramer29c2b432012-05-12 21:10:52 +00001541static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1542 // Check the attribute arguments.
1543 if (!checkAttributeNumArgs(S, Attr, 0))
1544 return;
1545
1546 if (!isa<FunctionDecl>(D)) {
1547 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1548 << Attr.getName() << ExpectedFunction;
1549 return;
1550 }
1551
1552 if (D->hasAttr<HotAttr>()) {
1553 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1554 << Attr.getName() << "hot";
1555 return;
1556 }
1557
1558 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context));
1559}
1560
1561static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1562 // Check the attribute arguments.
1563 if (!checkAttributeNumArgs(S, Attr, 0))
1564 return;
1565
1566 if (!isa<FunctionDecl>(D)) {
1567 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1568 << Attr.getName() << ExpectedFunction;
1569 return;
1570 }
1571
1572 if (D->hasAttr<ColdAttr>()) {
1573 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1574 << Attr.getName() << "cold";
1575 return;
1576 }
1577
1578 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context));
1579}
1580
Chandler Carruthedc2c642011-07-02 00:01:44 +00001581static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001582 // Check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001583 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar03a38442008-10-28 00:17:57 +00001584 return;
Anders Carlsson88097122009-02-19 19:16:48 +00001585
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001586 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001587 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001588 << Attr.getName() << ExpectedFunction;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001589 return;
1590 }
1591
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001592 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001593}
1594
Chandler Carruthedc2c642011-07-02 00:01:44 +00001595static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1596 const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001597 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001598 if (Attr.hasParameterOrArguments()) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001599 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1600 return;
1601 }
1602
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001603 if (!isa<FunctionDecl>(D)) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001604 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001605 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00001606 return;
1607 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001608
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001609 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
Daniel Dunbar03a38442008-10-28 00:17:57 +00001610}
1611
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00001612static void handleTLSModelAttr(Sema &S, Decl *D,
1613 const AttributeList &Attr) {
1614 // Check the attribute arguments.
1615 if (Attr.getNumArgs() != 1) {
1616 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1617 return;
1618 }
1619
1620 Expr *Arg = Attr.getArg(0);
1621 Arg = Arg->IgnoreParenCasts();
1622 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1623
1624 // Check that it is a string.
1625 if (!Str) {
1626 S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
1627 return;
1628 }
1629
1630 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) {
1631 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1632 << Attr.getName() << ExpectedTLSVar;
1633 return;
1634 }
1635
1636 // Check that the value.
1637 StringRef Model = Str->getString();
1638 if (Model != "global-dynamic" && Model != "local-dynamic"
1639 && Model != "initial-exec" && Model != "local-exec") {
1640 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1641 return;
1642 }
1643
1644 D->addAttr(::new (S.Context) TLSModelAttr(Attr.getRange(), S.Context,
1645 Model));
1646}
1647
Chandler Carruthedc2c642011-07-02 00:01:44 +00001648static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +00001649 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001650 if (Attr.hasParameterOrArguments()) {
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001651 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1652 return;
1653 }
Mike Stump11289f42009-09-09 15:08:12 +00001654
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001655 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +00001656 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +00001657 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001658 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
Ted Kremenek08479ae2009-08-15 00:51:46 +00001659 return;
1660 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001661 }
1662
Ted Kremenek08479ae2009-08-15 00:51:46 +00001663 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001664}
1665
Chandler Carruthedc2c642011-07-02 00:01:44 +00001666static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001667 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001668 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001669 return;
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001670
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001671 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
Dan Gohmanbbb7d622010-11-17 00:03:07 +00001672}
1673
Chandler Carruthedc2c642011-07-02 00:01:44 +00001674static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001675 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001676 if (isa<VarDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001677 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
Eric Christopher515d87f2010-12-03 06:58:14 +00001678 else
1679 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001680 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001681}
1682
Chandler Carruthedc2c642011-07-02 00:01:44 +00001683static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth9312c642011-07-11 23:33:05 +00001684 assert(!Attr.isInvalid());
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001685 if (isa<VarDecl>(D))
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001686 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
Eric Christopher515d87f2010-12-03 06:58:14 +00001687 else
1688 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001689 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +00001690}
1691
Chandler Carruthedc2c642011-07-02 00:01:44 +00001692static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001693 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00001694
1695 if (S.CheckNoReturnAttr(attr)) return;
1696
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001697 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +00001698 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001699 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00001700 return;
1701 }
1702
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001703 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
John McCall3882ace2011-01-05 12:14:39 +00001704}
1705
1706bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek1551d552011-04-15 05:49:29 +00001707 if (attr.hasParameterOrArguments()) {
John McCall3882ace2011-01-05 12:14:39 +00001708 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1709 attr.setInvalid();
1710 return true;
1711 }
1712
1713 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001714}
1715
Chandler Carruthedc2c642011-07-02 00:01:44 +00001716static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1717 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +00001718
1719 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1720 // because 'analyzer_noreturn' does not impact the type.
1721
Chandler Carruthfcc48d92011-07-11 23:30:35 +00001722 if(!checkAttributeNumArgs(S, Attr, 0))
1723 return;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001724
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001725 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1726 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +00001727 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1728 && !VD->getType()->isFunctionPointerType())) {
1729 S.Diag(Attr.getLoc(),
1730 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1731 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001732 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +00001733 return;
1734 }
1735 }
1736
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001737 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001738}
1739
John Thompsoncdb847ba2010-08-09 21:53:52 +00001740// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +00001741static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001742/*
1743 Returning a Vector Class in Registers
1744
Eric Christopherbc638a82010-12-01 22:13:54 +00001745 According to the PPU ABI specifications, a class with a single member of
1746 vector type is returned in memory when used as the return value of a function.
1747 This results in inefficient code when implementing vector classes. To return
1748 the value in a single vector register, add the vecreturn attribute to the
1749 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +00001750
1751 Example:
1752
1753 struct Vector
1754 {
1755 __vector float xyzw;
1756 } __attribute__((vecreturn));
1757
1758 Vector Add(Vector lhs, Vector rhs)
1759 {
1760 Vector result;
1761 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1762 return result; // This will be returned in a register
1763 }
1764*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001765 if (!isa<RecordDecl>(D)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001766 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001767 << Attr.getName() << ExpectedClass;
John Thompsoncdb847ba2010-08-09 21:53:52 +00001768 return;
1769 }
1770
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001771 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +00001772 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1773 return;
1774 }
1775
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001776 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +00001777 int count = 0;
1778
1779 if (!isa<CXXRecordDecl>(record)) {
1780 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1781 return;
1782 }
1783
1784 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1785 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1786 return;
1787 }
1788
Eric Christopherbc638a82010-12-01 22:13:54 +00001789 for (RecordDecl::field_iterator iter = record->field_begin();
1790 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +00001791 if ((count == 1) || !iter->getType()->isVectorType()) {
1792 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1793 return;
1794 }
1795 count++;
1796 }
1797
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001798 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
John Thompsoncdb847ba2010-08-09 21:53:52 +00001799}
1800
Chandler Carruthedc2c642011-07-02 00:01:44 +00001801static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001802 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001803 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001804 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001805 return;
1806 }
1807 // FIXME: Actually store the attribute on the declaration
1808}
1809
Chandler Carruthedc2c642011-07-02 00:01:44 +00001810static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek39c59a82008-07-25 04:39:19 +00001811 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001812 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001813 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001814 return;
1815 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001816
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001817 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper429c1342012-06-13 18:31:09 +00001818 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001819 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001820 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +00001821 return;
1822 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001823
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001824 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
Ted Kremenek39c59a82008-07-25 04:39:19 +00001825}
1826
Rafael Espindola70107f92011-10-03 14:59:42 +00001827static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1828 const AttributeList &Attr) {
1829 // check the attribute arguments.
1830 if (Attr.hasParameterOrArguments()) {
1831 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1832 return;
1833 }
1834
1835 if (!isa<FunctionDecl>(D)) {
1836 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1837 << Attr.getName() << ExpectedFunction;
1838 return;
1839 }
1840
1841 D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context));
1842}
1843
Chandler Carruthedc2c642011-07-02 00:01:44 +00001844static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001845 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001846 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001847 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1848 return;
1849 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001850
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001851 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +00001852 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001853 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1854 return;
1855 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001856 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001857 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001858 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001859 return;
1860 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001861
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001862 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
Daniel Dunbarfee07a02009-02-13 19:23:53 +00001863}
1864
Chandler Carruthedc2c642011-07-02 00:01:44 +00001865static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001866 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001867 if (Attr.getNumArgs() > 1) {
1868 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001869 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001870 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001871
1872 int priority = 65535; // FIXME: Do not hardcode such constants.
1873 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001874 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001875 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001876 if (E->isTypeDependent() || E->isValueDependent() ||
1877 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001878 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001879 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001880 return;
1881 }
1882 priority = Idx.getZExtValue();
1883 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001884
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001885 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001886 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001887 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001888 return;
1889 }
1890
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001891 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001892 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001893}
1894
Chandler Carruthedc2c642011-07-02 00:01:44 +00001895static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001896 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001897 if (Attr.getNumArgs() > 1) {
1898 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001899 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001900 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001901
1902 int priority = 65535; // FIXME: Do not hardcode such constants.
1903 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001904 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001905 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001906 if (E->isTypeDependent() || E->isValueDependent() ||
1907 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001908 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001909 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001910 return;
1911 }
1912 priority = Idx.getZExtValue();
1913 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001914
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001915 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001916 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001917 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001918 return;
1919 }
1920
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001921 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001922 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001923}
1924
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001925template <typename AttrTy>
1926static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr,
1927 const char *Name) {
Chris Lattner190aa102011-02-24 05:42:24 +00001928 unsigned NumArgs = Attr.getNumArgs();
1929 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001930 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001931 return;
1932 }
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001933
1934 // Handle the case where the attribute has a text message.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001935 StringRef Str;
Chris Lattner190aa102011-02-24 05:42:24 +00001936 if (NumArgs == 1) {
1937 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanian551063102010-10-06 21:18:44 +00001938 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00001939 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001940 << Name;
Fariborz Jahanian551063102010-10-06 21:18:44 +00001941 return;
1942 }
Chris Lattner190aa102011-02-24 05:42:24 +00001943 Str = SE->getString();
Fariborz Jahanian551063102010-10-06 21:18:44 +00001944 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001945
Benjamin Kramerf435ab42012-05-16 12:19:08 +00001946 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001947}
1948
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00001949static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1950 const AttributeList &Attr) {
1951 unsigned NumArgs = Attr.getNumArgs();
1952 if (NumArgs > 0) {
1953 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1954 return;
1955 }
1956
1957 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00001958 Attr.getRange(), S.Context));
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00001959}
1960
Patrick Beardacfbe9e2012-04-06 18:12:22 +00001961static void handleObjCRootClassAttr(Sema &S, Decl *D,
1962 const AttributeList &Attr) {
1963 if (!isa<ObjCInterfaceDecl>(D)) {
1964 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1965 return;
1966 }
1967
1968 unsigned NumArgs = Attr.getNumArgs();
1969 if (NumArgs > 0) {
1970 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1971 return;
1972 }
1973
1974 D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context));
1975}
1976
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001977static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001978 const AttributeList &Attr) {
Fariborz Jahanian7249e362012-01-03 22:52:32 +00001979 if (!isa<ObjCInterfaceDecl>(D)) {
1980 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1981 return;
1982 }
1983
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001984 unsigned NumArgs = Attr.getNumArgs();
1985 if (NumArgs > 0) {
1986 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1987 return;
1988 }
1989
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001990 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00001991 Attr.getRange(), S.Context));
1992}
1993
Jordy Rose740b0c22012-05-08 03:27:22 +00001994static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
1995 IdentifierInfo *Platform,
1996 VersionTuple Introduced,
1997 VersionTuple Deprecated,
1998 VersionTuple Obsoleted) {
Rafael Espindola2d243bf2012-05-06 19:56:25 +00001999 StringRef PlatformName
2000 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2001 if (PlatformName.empty())
2002 PlatformName = Platform->getName();
2003
2004 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2005 // of these steps are needed).
2006 if (!Introduced.empty() && !Deprecated.empty() &&
2007 !(Introduced <= Deprecated)) {
2008 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2009 << 1 << PlatformName << Deprecated.getAsString()
2010 << 0 << Introduced.getAsString();
2011 return true;
2012 }
2013
2014 if (!Introduced.empty() && !Obsoleted.empty() &&
2015 !(Introduced <= Obsoleted)) {
2016 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2017 << 2 << PlatformName << Obsoleted.getAsString()
2018 << 0 << Introduced.getAsString();
2019 return true;
2020 }
2021
2022 if (!Deprecated.empty() && !Obsoleted.empty() &&
2023 !(Deprecated <= Obsoleted)) {
2024 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2025 << 2 << PlatformName << Obsoleted.getAsString()
2026 << 1 << Deprecated.getAsString();
2027 return true;
2028 }
2029
2030 return false;
2031}
2032
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002033AvailabilityAttr *Sema::mergeAvailabilityAttr(Decl *D, SourceRange Range,
2034 IdentifierInfo *Platform,
2035 VersionTuple Introduced,
2036 VersionTuple Deprecated,
2037 VersionTuple Obsoleted,
2038 bool IsUnavailable,
2039 StringRef Message) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00002040 VersionTuple MergedIntroduced = Introduced;
2041 VersionTuple MergedDeprecated = Deprecated;
2042 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002043 bool FoundAny = false;
2044
Rafael Espindolac67f2232012-05-10 02:50:16 +00002045 if (D->hasAttrs()) {
2046 AttrVec &Attrs = D->getAttrs();
2047 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2048 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2049 if (!OldAA) {
2050 ++i;
2051 continue;
2052 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002053
Rafael Espindolac67f2232012-05-10 02:50:16 +00002054 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2055 if (OldPlatform != Platform) {
2056 ++i;
2057 continue;
2058 }
2059
2060 FoundAny = true;
2061 VersionTuple OldIntroduced = OldAA->getIntroduced();
2062 VersionTuple OldDeprecated = OldAA->getDeprecated();
2063 VersionTuple OldObsoleted = OldAA->getObsoleted();
2064 bool OldIsUnavailable = OldAA->getUnavailable();
2065 StringRef OldMessage = OldAA->getMessage();
2066
2067 if ((!OldIntroduced.empty() && !Introduced.empty() &&
2068 OldIntroduced != Introduced) ||
2069 (!OldDeprecated.empty() && !Deprecated.empty() &&
2070 OldDeprecated != Deprecated) ||
2071 (!OldObsoleted.empty() && !Obsoleted.empty() &&
2072 OldObsoleted != Obsoleted) ||
2073 (OldIsUnavailable != IsUnavailable) ||
2074 (OldMessage != Message)) {
2075 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2076 Diag(Range.getBegin(), diag::note_previous_attribute);
2077 Attrs.erase(Attrs.begin() + i);
2078 --e;
2079 continue;
2080 }
2081
2082 VersionTuple MergedIntroduced2 = MergedIntroduced;
2083 VersionTuple MergedDeprecated2 = MergedDeprecated;
2084 VersionTuple MergedObsoleted2 = MergedObsoleted;
2085
2086 if (MergedIntroduced2.empty())
2087 MergedIntroduced2 = OldIntroduced;
2088 if (MergedDeprecated2.empty())
2089 MergedDeprecated2 = OldDeprecated;
2090 if (MergedObsoleted2.empty())
2091 MergedObsoleted2 = OldObsoleted;
2092
2093 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2094 MergedIntroduced2, MergedDeprecated2,
2095 MergedObsoleted2)) {
2096 Attrs.erase(Attrs.begin() + i);
2097 --e;
2098 continue;
2099 }
2100
2101 MergedIntroduced = MergedIntroduced2;
2102 MergedDeprecated = MergedDeprecated2;
2103 MergedObsoleted = MergedObsoleted2;
2104 ++i;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002105 }
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002106 }
2107
2108 if (FoundAny &&
2109 MergedIntroduced == Introduced &&
2110 MergedDeprecated == Deprecated &&
2111 MergedObsoleted == Obsoleted)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002112 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002113
Rafael Espindolac67f2232012-05-10 02:50:16 +00002114 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002115 MergedDeprecated, MergedObsoleted)) {
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002116 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2117 Introduced, Deprecated,
2118 Obsoleted, IsUnavailable, Message);
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002119 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002120 return NULL;
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002121}
2122
Chandler Carruthedc2c642011-07-02 00:01:44 +00002123static void handleAvailabilityAttr(Sema &S, Decl *D,
2124 const AttributeList &Attr) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002125 IdentifierInfo *Platform = Attr.getParameterName();
2126 SourceLocation PlatformLoc = Attr.getParameterLoc();
2127
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002128 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002129 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2130 << Platform;
2131
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002132 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2133 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2134 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00002135 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00002136 StringRef Str;
2137 const StringLiteral *SE =
2138 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2139 if (SE)
2140 Str = SE->getString();
Rafael Espindola2d243bf2012-05-06 19:56:25 +00002141
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002142 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(D, Attr.getRange(),
2143 Platform,
2144 Introduced.Version,
2145 Deprecated.Version,
2146 Obsoleted.Version,
2147 IsUnavailable, Str);
2148 if (NewAttr)
2149 D->addAttr(NewAttr);
Rafael Espindolac67f2232012-05-10 02:50:16 +00002150}
2151
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002152VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
2153 VisibilityAttr::VisibilityType Vis) {
Rafael Espindolaa6b3cd42012-05-10 03:01:34 +00002154 if (isa<TypedefNameDecl>(D)) {
2155 Diag(Range.getBegin(), diag::warn_attribute_ignored) << "visibility";
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002156 return NULL;
Rafael Espindolaa6b3cd42012-05-10 03:01:34 +00002157 }
Rafael Espindolac67f2232012-05-10 02:50:16 +00002158 VisibilityAttr *ExistingAttr = D->getAttr<VisibilityAttr>();
2159 if (ExistingAttr) {
2160 VisibilityAttr::VisibilityType ExistingVis = ExistingAttr->getVisibility();
2161 if (ExistingVis == Vis)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002162 return NULL;
Rafael Espindolac67f2232012-05-10 02:50:16 +00002163 Diag(ExistingAttr->getLocation(), diag::err_mismatched_visibility);
2164 Diag(Range.getBegin(), diag::note_previous_attribute);
2165 D->dropAttr<VisibilityAttr>();
2166 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002167 return ::new (Context) VisibilityAttr(Range, Context, Vis);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002168}
2169
Chandler Carruthedc2c642011-07-02 00:01:44 +00002170static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002171 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002172 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002173 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002174
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002175 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002176 Arg = Arg->IgnoreParenCasts();
2177 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002178
Douglas Gregorfb65e592011-07-27 05:40:30 +00002179 if (!Str || !Str->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002180 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002181 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002182 return;
2183 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002184
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002185 StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002186 VisibilityAttr::VisibilityType type;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002187
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002188 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002189 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002190 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002191 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00002192 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002193 type = VisibilityAttr::Hidden; // FIXME
John McCalleed64c72012-01-29 01:20:30 +00002194 else if (TypeStr == "protected") {
2195 // Complain about attempts to use protected visibility on targets
2196 // (like Darwin) that don't support it.
2197 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2198 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2199 type = VisibilityAttr::Default;
2200 } else {
2201 type = VisibilityAttr::Protected;
2202 }
2203 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00002204 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002205 return;
2206 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002207
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002208 VisibilityAttr *NewAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type);
2209 if (NewAttr)
2210 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002211}
2212
Chandler Carruthedc2c642011-07-02 00:01:44 +00002213static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2214 const AttributeList &Attr) {
John McCall86bc21f2011-03-02 11:33:24 +00002215 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2216 if (!method) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002217 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002218 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00002219 return;
2220 }
2221
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002222 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2223 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
2224 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCall86bc21f2011-03-02 11:33:24 +00002225 << "objc_method_family" << 1;
2226 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002227 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCall86bc21f2011-03-02 11:33:24 +00002228 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002229 Attr.setInvalid();
John McCall86bc21f2011-03-02 11:33:24 +00002230 return;
2231 }
2232
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002233 StringRef param = Attr.getParameterName()->getName();
John McCall86bc21f2011-03-02 11:33:24 +00002234 ObjCMethodFamilyAttr::FamilyKind family;
2235 if (param == "none")
2236 family = ObjCMethodFamilyAttr::OMF_None;
2237 else if (param == "alloc")
2238 family = ObjCMethodFamilyAttr::OMF_alloc;
2239 else if (param == "copy")
2240 family = ObjCMethodFamilyAttr::OMF_copy;
2241 else if (param == "init")
2242 family = ObjCMethodFamilyAttr::OMF_init;
2243 else if (param == "mutableCopy")
2244 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2245 else if (param == "new")
2246 family = ObjCMethodFamilyAttr::OMF_new;
2247 else {
2248 // Just warn and ignore it. This is future-proof against new
2249 // families being used in system headers.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002250 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCall86bc21f2011-03-02 11:33:24 +00002251 return;
2252 }
2253
John McCall31168b02011-06-15 23:02:42 +00002254 if (family == ObjCMethodFamilyAttr::OMF_init &&
2255 !method->getResultType()->isObjCObjectPointerType()) {
2256 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2257 << method->getResultType();
2258 // Ignore the attribute.
2259 return;
2260 }
2261
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002262 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCall31168b02011-06-15 23:02:42 +00002263 S.Context, family));
John McCall86bc21f2011-03-02 11:33:24 +00002264}
2265
Chandler Carruthedc2c642011-07-02 00:01:44 +00002266static void handleObjCExceptionAttr(Sema &S, Decl *D,
2267 const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002268 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner677a3582009-02-14 08:09:34 +00002269 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002270
Chris Lattner677a3582009-02-14 08:09:34 +00002271 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2272 if (OCI == 0) {
2273 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
2274 return;
2275 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002276
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002277 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner677a3582009-02-14 08:09:34 +00002278}
2279
Chandler Carruthedc2c642011-07-02 00:01:44 +00002280static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002281 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00002282 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002283 return;
2284 }
Richard Smithdda56e42011-04-15 14:24:37 +00002285 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002286 QualType T = TD->getUnderlyingType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002287 if (!T->isCARCBridgableType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002288 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2289 return;
2290 }
2291 }
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002292 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2293 QualType T = PD->getType();
Ted Kremenek7712eef2012-08-29 22:54:47 +00002294 if (!T->isCARCBridgableType()) {
Fariborz Jahanianbebd0ba2012-05-31 23:18:32 +00002295 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2296 return;
2297 }
2298 }
2299 else {
Ted Kremenek05e916b2012-03-01 01:40:32 +00002300 // It is okay to include this attribute on properties, e.g.:
2301 //
2302 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2303 //
2304 // In this case it follows tradition and suppresses an error in the above
2305 // case.
Fariborz Jahaniana45495a2011-11-29 01:48:40 +00002306 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenek05e916b2012-03-01 01:40:32 +00002307 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002308 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00002309}
2310
Mike Stumpd3bb5572009-07-24 19:02:52 +00002311static void
Chandler Carruthedc2c642011-07-02 00:01:44 +00002312handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002313 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00002314 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002315 return;
2316 }
2317
2318 if (!isa<FunctionDecl>(D)) {
2319 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2320 return;
2321 }
2322
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002323 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002324}
2325
Chandler Carruthedc2c642011-07-02 00:01:44 +00002326static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002327 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002328 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002329 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002330 return;
2331 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002332
Steve Naroff3405a732008-09-18 16:44:58 +00002333 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002334 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00002335 return;
2336 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002337
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002338 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00002339 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00002340 type = BlocksAttr::ByRef;
2341 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002342 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002343 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00002344 return;
2345 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002346
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002347 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff3405a732008-09-18 16:44:58 +00002348}
2349
Chandler Carruthedc2c642011-07-02 00:01:44 +00002350static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002351 // check the attribute arguments.
2352 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00002353 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00002354 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002355 }
2356
John McCallb46f2872011-09-09 07:56:05 +00002357 unsigned sentinel = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002358 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002359 Expr *E = Attr.getArg(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00002360 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002361 if (E->isTypeDependent() || E->isValueDependent() ||
2362 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002363 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002364 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002365 return;
2366 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002367
John McCallb46f2872011-09-09 07:56:05 +00002368 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002369 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2370 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002371 return;
2372 }
John McCallb46f2872011-09-09 07:56:05 +00002373
2374 sentinel = Idx.getZExtValue();
Anders Carlssonc181b012008-10-05 18:05:59 +00002375 }
2376
John McCallb46f2872011-09-09 07:56:05 +00002377 unsigned nullPos = 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002378 if (Attr.getNumArgs() > 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002379 Expr *E = Attr.getArg(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00002380 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002381 if (E->isTypeDependent() || E->isValueDependent() ||
2382 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002383 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002384 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002385 return;
2386 }
2387 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002388
John McCallb46f2872011-09-09 07:56:05 +00002389 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002390 // FIXME: This error message could be improved, it would be nice
2391 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00002392 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2393 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00002394 return;
2395 }
2396 }
2397
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002398 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCallb46f2872011-09-09 07:56:05 +00002399 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00002400 if (isa<FunctionNoProtoType>(FT)) {
2401 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2402 return;
2403 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002404
Chris Lattner9363e312009-03-17 23:03:47 +00002405 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002406 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002407 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002408 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002409 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00002410 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002411 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00002412 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002413 }
Eli Friedman5c5e3b72012-01-06 01:23:10 +00002414 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2415 if (!BD->isVariadic()) {
2416 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2417 return;
2418 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002419 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002420 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00002421 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002422 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00002423 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002424 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00002425 int m = Ty->isFunctionPointerType() ? 0 : 1;
2426 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002427 return;
2428 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002429 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002430 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002431 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00002432 return;
2433 }
Anders Carlssonc181b012008-10-05 18:05:59 +00002434 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00002435 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002436 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00002437 return;
2438 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002439 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherbc638a82010-12-01 22:13:54 +00002440 nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +00002441}
2442
Chandler Carruthedc2c642011-07-02 00:01:44 +00002443static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner237f2752009-02-14 07:37:35 +00002444 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002445 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner237f2752009-02-14 07:37:35 +00002446 return;
Chris Lattner237f2752009-02-14 07:37:35 +00002447
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002448 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00002449 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002450 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner237f2752009-02-14 07:37:35 +00002451 return;
2452 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002453
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002454 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2455 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2456 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00002457 return;
2458 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002459 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2460 if (MD->getResultType()->isVoidType()) {
2461 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2462 << Attr.getName() << 1;
2463 return;
2464 }
2465
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002466 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner237f2752009-02-14 07:37:35 +00002467}
2468
Chandler Carruthedc2c642011-07-02 00:01:44 +00002469static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002470 // check the attribute arguments.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002471 if (Attr.hasParameterOrArguments()) {
2472 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002473 return;
2474 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002475
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002476 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian47f9a732011-10-21 22:27:12 +00002477 if (isa<CXXRecordDecl>(D)) {
2478 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2479 return;
2480 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002481 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2482 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00002483 return;
2484 }
2485
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002486 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00002487
2488 // 'weak' only applies to declarations with external linkage.
2489 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002490 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002491 return;
2492 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002493
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002494 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002495}
2496
Chandler Carruthedc2c642011-07-02 00:01:44 +00002497static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002498 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002499 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002500 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002501
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002502
2503 // weak_import only applies to variable & function declarations.
2504 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002505 if (!D->canBeWeakImported(isDef)) {
2506 if (isDef)
2507 S.Diag(Attr.getLoc(),
2508 diag::warn_attribute_weak_import_invalid_on_definition)
2509 << "weak_import" << 2 /*variable and function*/;
Douglas Gregord71149a2011-03-23 13:27:51 +00002510 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00002511 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian3249a1e2011-10-26 23:59:12 +00002512 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregord71149a2011-03-23 13:27:51 +00002513 // Nothing to warn about here.
2514 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00002515 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002516 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002517
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002518 return;
2519 }
2520
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002521 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00002522}
2523
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002524// Handles reqd_work_group_size and work_group_size_hint.
2525static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewyckyb9e4a3a2012-07-24 01:31:55 +00002526 const AttributeList &Attr) {
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002527 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2528 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2529
Nate Begemanf2758702009-06-26 06:32:41 +00002530 // Attribute has 3 arguments.
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002531 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begemanf2758702009-06-26 06:32:41 +00002532
2533 unsigned WGSize[3];
2534 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002535 Expr *E = Attr.getArg(i);
Nate Begemanf2758702009-06-26 06:32:41 +00002536 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002537 if (E->isTypeDependent() || E->isValueDependent() ||
2538 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00002539 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002540 << Attr.getName()->getName() << E->getSourceRange();
Nate Begemanf2758702009-06-26 06:32:41 +00002541 return;
2542 }
2543 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2544 }
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00002545
2546 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2547 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2548 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2549 if (!(A->getXDim() == WGSize[0] &&
2550 A->getYDim() == WGSize[1] &&
2551 A->getZDim() == WGSize[2])) {
2552 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2553 Attr.getName();
2554 }
2555 }
2556
2557 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2558 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2559 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2560 if (!(A->getXDim() == WGSize[0] &&
2561 A->getYDim() == WGSize[1] &&
2562 A->getZDim() == WGSize[2])) {
2563 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2564 Attr.getName();
2565 }
2566 }
2567
2568 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2569 D->addAttr(::new (S.Context)
2570 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
2571 WGSize[0], WGSize[1], WGSize[2]));
2572 else
2573 D->addAttr(::new (S.Context)
2574 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
2575 WGSize[0], WGSize[1], WGSize[2]));
Nate Begemanf2758702009-06-26 06:32:41 +00002576}
2577
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002578SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
2579 StringRef Name) {
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002580 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2581 if (ExistingAttr->getName() == Name)
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002582 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002583 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2584 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002585 return NULL;
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002586 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002587 return ::new (Context) SectionAttr(Range, Context, Name);
Rafael Espindola9869c3a2012-05-13 02:42:42 +00002588}
2589
Chandler Carruthedc2c642011-07-02 00:01:44 +00002590static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00002591 // Attribute has no arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002592 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar648bf782009-02-12 17:28:23 +00002593 return;
Daniel Dunbar648bf782009-02-12 17:28:23 +00002594
2595 // Make sure that there is a string literal as the sections's single
2596 // argument.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002597 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002598 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002599 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002600 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00002601 return;
2602 }
Mike Stump11289f42009-09-09 15:08:12 +00002603
Chris Lattner30ba6742009-08-10 19:03:04 +00002604 // If the target wants to validate the section specifier, make it happen.
Douglas Gregore8bbc122011-09-02 00:18:52 +00002605 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00002606 if (!Error.empty()) {
2607 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2608 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00002609 return;
2610 }
Mike Stump11289f42009-09-09 15:08:12 +00002611
Chris Lattner20aee9b2010-01-12 20:58:53 +00002612 // This attribute cannot be applied to local variables.
2613 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2614 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2615 return;
2616 }
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002617 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
2618 SE->getString());
2619 if (NewAttr)
2620 D->addAttr(NewAttr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00002621}
2622
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002623
Chandler Carruthedc2c642011-07-02 00:01:44 +00002624static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002625 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002626 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002627 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002628 return;
2629 }
Douglas Gregor88336832011-06-15 05:45:11 +00002630
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002631 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002632 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002633 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002634 } else {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002635 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregor88336832011-06-15 05:45:11 +00002636 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002637}
2638
Chandler Carruthedc2c642011-07-02 00:01:44 +00002639static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002640 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002641 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002642 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00002643 return;
2644 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002645
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002646 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00002647 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +00002648 Existing->setRange(Attr.getRange());
Douglas Gregor88336832011-06-15 05:45:11 +00002649 } else {
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002650 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregor88336832011-06-15 05:45:11 +00002651 }
Anders Carlssonb8316282008-10-05 23:32:53 +00002652}
2653
Chandler Carruthedc2c642011-07-02 00:01:44 +00002654static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00002655 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002656 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssonb8316282008-10-05 23:32:53 +00002657 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002658
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002659 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00002660}
2661
Chandler Carruthedc2c642011-07-02 00:01:44 +00002662static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002663 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00002664 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2665 return;
2666 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002667
Anders Carlssond277d792009-01-31 01:16:18 +00002668 if (Attr.getNumArgs() != 0) {
2669 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2670 return;
2671 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002672
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002673 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002674
Anders Carlssond277d792009-01-31 01:16:18 +00002675 if (!VD || !VD->hasLocalStorage()) {
2676 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2677 return;
2678 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002679
Anders Carlssond277d792009-01-31 01:16:18 +00002680 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00002681 // FIXME: Lookup probably isn't looking in the right place
John McCall9f3059a2009-10-09 21:13:30 +00002682 NamedDecl *CleanupDecl
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002683 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2684 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00002685 if (!CleanupDecl) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002686 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00002687 Attr.getParameterName();
2688 return;
2689 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002690
Anders Carlssond277d792009-01-31 01:16:18 +00002691 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2692 if (!FD) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002693 S.Diag(Attr.getParameterLoc(),
2694 diag::err_attribute_cleanup_arg_not_function)
2695 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002696 return;
2697 }
2698
Anders Carlssond277d792009-01-31 01:16:18 +00002699 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002700 S.Diag(Attr.getParameterLoc(),
2701 diag::err_attribute_cleanup_func_must_take_one_arg)
2702 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00002703 return;
2704 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002705
Anders Carlsson723f55d2009-02-07 23:16:50 +00002706 // We're currently more strict than GCC about what function types we accept.
2707 // If this ever proves to be a problem it should be easy to fix.
2708 QualType Ty = S.Context.getPointerType(VD->getType());
2709 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00002710 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2711 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00002712 S.Diag(Attr.getParameterLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00002713 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2714 Attr.getParameterName() << ParamTy << Ty;
2715 return;
2716 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002717
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002718 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Eli Friedmanfa0df832012-02-02 03:46:19 +00002719 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssond277d792009-01-31 01:16:18 +00002720}
2721
Mike Stumpd3bb5572009-07-24 19:02:52 +00002722/// Handle __attribute__((format_arg((idx)))) attribute based on
2723/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002724static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002725 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002726 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00002727
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002728 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002729 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002730 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002731 return;
2732 }
Chandler Carruth743682b2010-11-16 08:35:43 +00002733
2734 // In C++ the implicit 'this' function parameter also counts, and they are
2735 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002736 bool HasImplicitThisParam = isInstanceMethod(D);
2737 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002738 unsigned FirstIdx = 1;
Chandler Carruth743682b2010-11-16 08:35:43 +00002739
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002740 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002741 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002742 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002743 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2744 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002745 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2746 << "format" << 2 << IdxExpr->getSourceRange();
2747 return;
2748 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002749
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002750 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2751 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2752 << "format" << 2 << IdxExpr->getSourceRange();
2753 return;
2754 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002755
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002756 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002757
Chandler Carruth743682b2010-11-16 08:35:43 +00002758 if (HasImplicitThisParam) {
2759 if (ArgIdx == 0) {
2760 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2761 << "format_arg" << IdxExpr->getSourceRange();
2762 return;
2763 }
2764 ArgIdx--;
2765 }
2766
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002767 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002768 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002769
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002770 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2771 if (not_nsstring_type &&
2772 !isCFStringType(Ty, S.Context) &&
2773 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002774 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002775 // FIXME: Should highlight the actual expression that has the wrong type.
2776 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002777 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002778 << IdxExpr->getSourceRange();
2779 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002780 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002781 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002782 if (!isNSStringType(Ty, S.Context) &&
2783 !isCFStringType(Ty, S.Context) &&
2784 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002785 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002786 // FIXME: Should highlight the actual expression that has the wrong type.
2787 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00002788 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002789 << IdxExpr->getSourceRange();
2790 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002791 }
2792
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002793 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth743682b2010-11-16 08:35:43 +00002794 Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002795}
2796
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002797enum FormatAttrKind {
2798 CFStringFormat,
2799 NSStringFormat,
2800 StrftimeFormat,
2801 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00002802 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002803 InvalidFormat
2804};
2805
2806/// getFormatAttrKind - Map from format attribute names to supported format
2807/// types.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002808static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002809 return llvm::StringSwitch<FormatAttrKind>(Format)
2810 // Check for formats that get handled specially.
2811 .Case("NSString", NSStringFormat)
2812 .Case("CFString", CFStringFormat)
2813 .Case("strftime", StrftimeFormat)
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002814
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002815 // Otherwise, check for supported formats.
2816 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2817 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2818 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002819
Benjamin Kramer96a44b62012-05-16 12:44:25 +00002820 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2821 .Default(InvalidFormat);
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002822}
2823
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002824/// Handle __attribute__((init_priority(priority))) attributes based on
2825/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002826static void handleInitPriorityAttr(Sema &S, Decl *D,
2827 const AttributeList &Attr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002828 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002829 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2830 return;
2831 }
2832
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002833 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002834 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2835 Attr.setInvalid();
2836 return;
2837 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002838 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002839 if (S.Context.getAsArrayType(T))
2840 T = S.Context.getBaseElementType(T);
2841 if (!T->getAs<RecordType>()) {
2842 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2843 Attr.setInvalid();
2844 return;
2845 }
2846
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002847 if (Attr.getNumArgs() != 1) {
2848 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2849 Attr.setInvalid();
2850 return;
2851 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002852 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00002853
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002854 llvm::APSInt priority(32);
2855 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2856 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2857 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2858 << "init_priority" << priorityExpr->getSourceRange();
2859 Attr.setInvalid();
2860 return;
2861 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00002862 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002863 if (prioritynum < 101 || prioritynum > 65535) {
2864 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2865 << priorityExpr->getSourceRange();
2866 Attr.setInvalid();
2867 return;
2868 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002869 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00002870 prioritynum));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002871}
2872
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002873FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
2874 int FormatIdx, int FirstArg) {
Rafael Espindola92d49452012-05-11 00:36:07 +00002875 // Check whether we already have an equivalent format attribute.
2876 for (specific_attr_iterator<FormatAttr>
2877 i = D->specific_attr_begin<FormatAttr>(),
2878 e = D->specific_attr_end<FormatAttr>();
2879 i != e ; ++i) {
2880 FormatAttr *f = *i;
2881 if (f->getType() == Format &&
2882 f->getFormatIdx() == FormatIdx &&
2883 f->getFirstArg() == FirstArg) {
2884 // If we don't have a valid location for this attribute, adopt the
2885 // location.
2886 if (f->getLocation().isInvalid())
2887 f->setRange(Range);
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002888 return NULL;
Rafael Espindola92d49452012-05-11 00:36:07 +00002889 }
2890 }
2891
Rafael Espindolae200f1c2012-05-13 03:25:18 +00002892 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
2893 FirstArg);
Rafael Espindola92d49452012-05-11 00:36:07 +00002894}
2895
Mike Stumpd3bb5572009-07-24 19:02:52 +00002896/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2897/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00002898static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002899
Chris Lattner4a927cb2008-06-28 23:36:30 +00002900 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00002901 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002902 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002903 return;
2904 }
2905
Chris Lattner4a927cb2008-06-28 23:36:30 +00002906 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002907 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002908 return;
2909 }
2910
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002911 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002912 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002913 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002914 return;
2915 }
2916
Chandler Carruth743682b2010-11-16 08:35:43 +00002917 // In C++ the implicit 'this' function parameter also counts, and they are
2918 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002919 bool HasImplicitThisParam = isInstanceMethod(D);
2920 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002921 unsigned FirstIdx = 1;
2922
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002923 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002924
2925 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002926 if (Format.startswith("__") && Format.endswith("__"))
2927 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002928
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002929 // Check for supported formats.
2930 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00002931
2932 if (Kind == IgnoredFormat)
2933 return;
2934
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002935 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00002936 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00002937 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002938 return;
2939 }
2940
2941 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002942 Expr *IdxExpr = Attr.getArg(0);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002943 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002944 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2945 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002946 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002947 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002948 return;
2949 }
2950
2951 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00002952 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002953 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002954 return;
2955 }
2956
2957 // FIXME: Do we need to bounds check?
2958 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002959
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002960 if (HasImplicitThisParam) {
2961 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00002962 S.Diag(Attr.getLoc(),
2963 diag::err_format_attribute_implicit_this_format_string)
2964 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00002965 return;
2966 }
2967 ArgIdx--;
2968 }
Mike Stump11289f42009-09-09 15:08:12 +00002969
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002970 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002971 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002972
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002973 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00002974 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00002975 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2976 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00002977 return;
2978 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00002979 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002980 // FIXME: do we need to check if the type is NSString*? What are the
2981 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002982 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002983 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002984 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2985 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002986 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002987 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002988 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002989 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00002990 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00002991 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2992 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002993 return;
2994 }
2995
2996 // check the 3rd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002997 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002998 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002999 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3000 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00003001 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003002 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003003 return;
3004 }
3005
3006 // check if the function is variadic if the 3rd argument non-zero
3007 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003008 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003009 ++NumArgs; // +1 for ...
3010 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003011 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003012 return;
3013 }
3014 }
3015
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003016 // strftime requires FirstArg to be 0 because it doesn't read from any
3017 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00003018 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003019 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00003020 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3021 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003022 return;
3023 }
3024 // if 0 it disables parameter checking (to use with e.g. va_list)
3025 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00003026 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003027 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003028 return;
3029 }
3030
Rafael Espindolae200f1c2012-05-13 03:25:18 +00003031 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3032 Idx.getZExtValue(),
3033 FirstArg.getZExtValue());
3034 if (NewAttr)
3035 D->addAttr(NewAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003036}
3037
Chandler Carruthedc2c642011-07-02 00:01:44 +00003038static void handleTransparentUnionAttr(Sema &S, Decl *D,
3039 const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003040 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003041 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003042 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003043
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003044
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003045 // Try to find the underlying union declaration.
3046 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003047 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003048 if (TD && TD->getUnderlyingType()->isUnionType())
3049 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3050 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003051 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003052
3053 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00003054 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003055 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003056 return;
3057 }
3058
John McCallf937c022011-10-07 06:10:15 +00003059 if (!RD->isCompleteDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003060 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003061 diag::warn_transparent_union_attribute_not_definition);
3062 return;
3063 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003064
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003065 RecordDecl::field_iterator Field = RD->field_begin(),
3066 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003067 if (Field == FieldEnd) {
3068 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3069 return;
3070 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003071
David Blaikie40ed2972012-06-06 20:45:41 +00003072 FieldDecl *FirstField = *Field;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003073 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00003074 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00003075 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00003076 diag::warn_transparent_union_attribute_floating)
3077 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003078 return;
3079 }
3080
3081 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3082 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3083 for (; Field != FieldEnd; ++Field) {
3084 QualType FieldType = Field->getType();
3085 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3086 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3087 // Warn if we drop the attribute.
3088 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003089 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003090 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003091 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003092 diag::warn_transparent_union_attribute_field_size_align)
3093 << isSize << Field->getDeclName() << FieldBits;
3094 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003095 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00003096 diag::note_transparent_union_first_field_size_align)
3097 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00003098 return;
3099 }
3100 }
3101
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003102 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003103}
3104
Chandler Carruthedc2c642011-07-02 00:01:44 +00003105static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003106 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003107 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003108 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003109
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00003110 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00003111 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003112
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003113 // Make sure that there is a string literal as the annotation's single
3114 // argument.
3115 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00003116 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003117 return;
3118 }
Julien Lerouge5a6b6982011-09-09 22:41:49 +00003119
3120 // Don't duplicate annotations that are already set.
3121 for (specific_attr_iterator<AnnotateAttr>
3122 i = D->specific_attr_begin<AnnotateAttr>(),
3123 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3124 if ((*i)->getAnnotation() == SE->getString())
3125 return;
3126 }
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003127 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00003128 SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003129}
3130
Chandler Carruthedc2c642011-07-02 00:01:44 +00003131static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003132 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00003133 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003134 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003135 return;
3136 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003137
Alexis Hunt96d5c762009-11-21 08:43:09 +00003138 //FIXME: The C++0x version of this attribute has more limited applicabilty
3139 // than GNU's, and should error out when it is used to specify a
3140 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003141
Chris Lattner4a927cb2008-06-28 23:36:30 +00003142 if (Attr.getNumArgs() == 0) {
Aaron Ballman478faed2012-06-19 22:09:27 +00003143 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3144 true, 0, Attr.isDeclspecAttribute()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003145 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003146 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003147
Aaron Ballman478faed2012-06-19 22:09:27 +00003148 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0),
3149 Attr.isDeclspecAttribute());
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003150}
3151
Aaron Ballman478faed2012-06-19 22:09:27 +00003152void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
3153 bool isDeclSpec) {
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00003154 // FIXME: Handle pack-expansions here.
3155 if (DiagnoseUnexpandedParameterPack(E))
3156 return;
3157
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003158 if (E->isTypeDependent() || E->isValueDependent()) {
3159 // Save dependent expressions in the AST to be instantiated.
Aaron Ballman478faed2012-06-19 22:09:27 +00003160 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E,
3161 isDeclSpec));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003162 return;
3163 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003164
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003165 SourceLocation AttrLoc = AttrRange.getBegin();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003166 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00003167 llvm::APSInt Alignment(32);
Douglas Gregore2b37442012-05-04 22:38:52 +00003168 ExprResult ICE
3169 = VerifyIntegerConstantExpression(E, &Alignment,
3170 diag::err_aligned_attribute_argument_not_int,
3171 /*AllowFold*/ false);
Richard Smithf4c51d92012-02-04 09:53:13 +00003172 if (ICE.isInvalid())
Chris Lattner4627b742008-06-28 23:50:44 +00003173 return;
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003174 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00003175 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3176 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003177 return;
3178 }
Aaron Ballman478faed2012-06-19 22:09:27 +00003179 if (isDeclSpec) {
3180 // We've already verified it's a power of 2, now let's make sure it's
3181 // 8192 or less.
3182 if (Alignment.getZExtValue() > 8192) {
3183 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
3184 << E->getSourceRange();
3185 return;
3186 }
3187 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00003188
Aaron Ballman478faed2012-06-19 22:09:27 +00003189 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take(),
3190 isDeclSpec));
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003191}
3192
Aaron Ballman478faed2012-06-19 22:09:27 +00003193void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
3194 bool isDeclSpec) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003195 // FIXME: Cache the number on the Attr object if non-dependent?
3196 // FIXME: Perform checking of type validity
Aaron Ballman478faed2012-06-19 22:09:27 +00003197 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3198 isDeclSpec));
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003199 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00003200}
Chris Lattneracbc2d22008-06-27 22:18:37 +00003201
Chandler Carruth3ed22c32011-07-01 23:49:16 +00003202/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00003203/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00003204///
Mike Stumpd3bb5572009-07-24 19:02:52 +00003205/// Despite what would be logical, the mode attribute is a decl attribute, not a
3206/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3207/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003208static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003209 // This attribute isn't documented, but glibc uses it. It changes
3210 // the width of an int or unsigned int to the specified size.
3211
3212 // Check that there aren't any arguments
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003213 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003214 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003215
Chris Lattneracbc2d22008-06-27 22:18:37 +00003216
3217 IdentifierInfo *Name = Attr.getParameterName();
3218 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00003219 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003220 return;
3221 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00003222
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003223 StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003224
3225 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003226 if (Str.startswith("__") && Str.endswith("__"))
3227 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003228
3229 unsigned DestWidth = 0;
3230 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00003231 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00003232 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00003233 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00003234 switch (Str[0]) {
3235 case 'Q': DestWidth = 8; break;
3236 case 'H': DestWidth = 16; break;
3237 case 'S': DestWidth = 32; break;
3238 case 'D': DestWidth = 64; break;
3239 case 'X': DestWidth = 96; break;
3240 case 'T': DestWidth = 128; break;
3241 }
3242 if (Str[1] == 'F') {
3243 IntegerMode = false;
3244 } else if (Str[1] == 'C') {
3245 IntegerMode = false;
3246 ComplexMode = true;
3247 } else if (Str[1] != 'I') {
3248 DestWidth = 0;
3249 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003250 break;
3251 case 4:
3252 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3253 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00003254 if (Str == "word")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003255 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00003256 else if (Str == "byte")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003257 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003258 break;
3259 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00003260 if (Str == "pointer")
Douglas Gregore8bbc122011-09-02 00:18:52 +00003261 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003262 break;
3263 }
3264
3265 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00003266 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00003267 OldTy = TD->getUnderlyingType();
3268 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3269 OldTy = VD->getType();
3270 else {
Chris Lattner3b054132008-11-19 05:08:23 +00003271 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003272 << "mode" << Attr.getRange();
Chris Lattneracbc2d22008-06-27 22:18:37 +00003273 return;
3274 }
Eli Friedman4735374e2009-03-03 06:41:03 +00003275
John McCall9dd450b2009-09-21 23:43:11 +00003276 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003277 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3278 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00003279 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00003280 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3281 } else if (ComplexMode) {
3282 if (!OldTy->isComplexType())
3283 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3284 } else {
3285 if (!OldTy->isFloatingType())
3286 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3287 }
3288
Mike Stump87c57ac2009-05-16 07:39:55 +00003289 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3290 // and friends, at least with glibc.
3291 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3292 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00003293 // FIXME: Make sure floating-point mappings are accurate
3294 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00003295 QualType NewTy;
3296 switch (DestWidth) {
3297 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003298 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003299 return;
3300 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00003301 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003302 return;
3303 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00003304 if (!IntegerMode) {
3305 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3306 return;
3307 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003308 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003309 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003310 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003311 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003312 break;
3313 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00003314 if (!IntegerMode) {
3315 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3316 return;
3317 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00003318 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003319 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003320 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003321 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003322 break;
3323 case 32:
3324 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003325 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003326 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00003327 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003328 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00003329 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003330 break;
3331 case 64:
3332 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00003333 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003334 else if (OldTy->isSignedIntegerType())
Douglas Gregore8bbc122011-09-02 00:18:52 +00003335 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00003336 NewTy = S.Context.LongTy;
3337 else
3338 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003339 else
Douglas Gregore8bbc122011-09-02 00:18:52 +00003340 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruth72343702010-01-26 06:39:24 +00003341 NewTy = S.Context.UnsignedLongTy;
3342 else
3343 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003344 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00003345 case 96:
3346 NewTy = S.Context.LongDoubleTy;
3347 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00003348 case 128:
3349 if (!IntegerMode) {
3350 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3351 return;
3352 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00003353 if (OldTy->isSignedIntegerType())
3354 NewTy = S.Context.Int128Ty;
3355 else
3356 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00003357 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00003358 }
3359
Eli Friedman4735374e2009-03-03 06:41:03 +00003360 if (ComplexMode) {
3361 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00003362 }
3363
3364 // Install the new type.
Richard Smithdda56e42011-04-15 14:24:37 +00003365 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall703a3f82009-10-24 08:00:42 +00003366 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00003367 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00003368 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00003369 cast<ValueDecl>(D)->setType(NewTy);
3370}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003371
Chandler Carruthedc2c642011-07-02 00:01:44 +00003372static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson76187b42009-02-13 06:46:13 +00003373 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003374 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson76187b42009-02-13 06:46:13 +00003375 return;
Anders Carlsson63784f42009-02-13 08:11:52 +00003376
Nick Lewycky08597072012-07-24 01:40:49 +00003377 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3378 if (!VD->hasGlobalStorage())
3379 S.Diag(Attr.getLoc(),
3380 diag::warn_attribute_requires_functions_or_static_globals)
3381 << Attr.getName();
3382 } else if (!isFunctionOrMethod(D)) {
3383 S.Diag(Attr.getLoc(),
3384 diag::warn_attribute_requires_functions_or_static_globals)
3385 << Attr.getName();
Anders Carlsson76187b42009-02-13 06:46:13 +00003386 return;
3387 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003388
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003389 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlsson76187b42009-02-13 06:46:13 +00003390}
3391
Chandler Carruthedc2c642011-07-02 00:01:44 +00003392static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson88097122009-02-19 19:16:48 +00003393 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003394 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson88097122009-02-19 19:16:48 +00003395 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003396
Mike Stumpd3bb5572009-07-24 19:02:52 +00003397
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003398 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00003399 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003400 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00003401 return;
3402 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003403
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003404 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson88097122009-02-19 19:16:48 +00003405}
3406
Chandler Carruthedc2c642011-07-02 00:01:44 +00003407static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3408 const AttributeList &Attr) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003409 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003410 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner3c77a352010-06-22 00:03:40 +00003411 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003412
Chris Lattner3c77a352010-06-22 00:03:40 +00003413
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003414 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00003415 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003416 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00003417 return;
3418 }
3419
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003420 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003421 S.Context));
Chris Lattner3c77a352010-06-22 00:03:40 +00003422}
3423
Chandler Carruthedc2c642011-07-02 00:01:44 +00003424static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003425 if (S.LangOpts.CUDA) {
3426 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00003427 if (Attr.hasParameterOrArguments()) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003428 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3429 return;
3430 }
3431
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003432 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003433 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003434 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003435 return;
3436 }
3437
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003438 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003439 } else {
3440 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3441 }
3442}
3443
Chandler Carruthedc2c642011-07-02 00:01:44 +00003444static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003445 if (S.LangOpts.CUDA) {
3446 // check the attribute arguments.
3447 if (Attr.getNumArgs() != 0) {
3448 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3449 return;
3450 }
3451
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003452 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003453 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003454 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003455 return;
3456 }
3457
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003458 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003459 } else {
3460 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3461 }
3462}
3463
Chandler Carruthedc2c642011-07-02 00:01:44 +00003464static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003465 if (S.LangOpts.CUDA) {
3466 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003467 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003468 return;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003469
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003470 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003471 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003472 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003473 return;
3474 }
3475
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003476 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003477 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00003478 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00003479 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
3480 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3481 << FD->getType()
3482 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
3483 "void");
3484 } else {
3485 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3486 << FD->getType();
3487 }
3488 return;
3489 }
3490
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003491 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003492 } else {
3493 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3494 }
3495}
3496
Chandler Carruthedc2c642011-07-02 00:01:44 +00003497static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003498 if (S.LangOpts.CUDA) {
3499 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003500 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003501 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003502
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003503
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003504 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003505 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003506 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003507 return;
3508 }
3509
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003510 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003511 } else {
3512 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3513 }
3514}
3515
Chandler Carruthedc2c642011-07-02 00:01:44 +00003516static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003517 if (S.LangOpts.CUDA) {
3518 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003519 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003520 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003521
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003522
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003523 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003524 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003525 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003526 return;
3527 }
3528
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003529 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00003530 } else {
3531 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3532 }
3533}
3534
Chandler Carruthedc2c642011-07-02 00:01:44 +00003535static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003536 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00003537 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnereaad6b72009-04-14 16:30:50 +00003538 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003539
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003540 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00003541 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00003542 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003543 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00003544 return;
3545 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003546
Douglas Gregor35b57532009-10-27 21:01:01 +00003547 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00003548 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00003549 return;
3550 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003551
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003552 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattnereaad6b72009-04-14 16:30:50 +00003553}
3554
Chandler Carruthedc2c642011-07-02 00:01:44 +00003555static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003556 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00003557
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003558 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00003559 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3560 CallingConv CC;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003561 if (S.CheckCallingConvAttr(Attr, CC))
John McCall3882ace2011-01-05 12:14:39 +00003562 return;
3563
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003564 if (!isa<ObjCMethodDecl>(D)) {
3565 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3566 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00003567 return;
3568 }
3569
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003570 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003571 case AttributeList::AT_FastCall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003572 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003573 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003574 case AttributeList::AT_StdCall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003575 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003576 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003577 case AttributeList::AT_ThisCall:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003578 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor4d13d102010-08-30 23:30:49 +00003579 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003580 case AttributeList::AT_CDecl:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003581 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00003582 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003583 case AttributeList::AT_Pascal:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003584 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik335e16b2010-09-03 01:29:35 +00003585 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003586 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003587 PcsAttr::PCSType PCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003588 switch (CC) {
3589 case CC_AAPCS:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003590 PCS = PcsAttr::AAPCS;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003591 break;
3592 case CC_AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003593 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer25885f42012-08-14 13:24:39 +00003594 break;
3595 default:
3596 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003597 }
3598
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003599 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Derek Schuffa2020962012-10-16 22:30:41 +00003600 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003601 }
Derek Schuffa2020962012-10-16 22:30:41 +00003602 case AttributeList::AT_PnaclCall:
3603 D->addAttr(::new (S.Context) PnaclCallAttr(Attr.getRange(), S.Context));
3604 return;
3605
Abramo Bagnara50099372010-04-30 13:10:51 +00003606 default:
3607 llvm_unreachable("unexpected attribute kind");
Abramo Bagnara50099372010-04-30 13:10:51 +00003608 }
3609}
3610
Chandler Carruthedc2c642011-07-02 00:01:44 +00003611static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth9312c642011-07-11 23:33:05 +00003612 assert(!Attr.isInvalid());
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003613 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003614}
3615
John McCall3882ace2011-01-05 12:14:39 +00003616bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
3617 if (attr.isInvalid())
3618 return true;
3619
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003620 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
3621 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
3622 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs;
John McCall3882ace2011-01-05 12:14:39 +00003623 attr.setInvalid();
3624 return true;
3625 }
3626
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003627 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3628 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00003629 switch (attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003630 case AttributeList::AT_CDecl: CC = CC_C; break;
3631 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3632 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3633 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3634 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
3635 case AttributeList::AT_Pcs: {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003636 Expr *Arg = attr.getArg(0);
3637 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00003638 if (!Str || !Str->isAscii()) {
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003639 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3640 << "pcs" << 1;
3641 attr.setInvalid();
3642 return true;
3643 }
3644
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003645 StringRef StrRef = Str->getString();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003646 if (StrRef == "aapcs") {
3647 CC = CC_AAPCS;
3648 break;
3649 } else if (StrRef == "aapcs-vfp") {
3650 CC = CC_AAPCS_VFP;
3651 break;
3652 }
Benjamin Kramer833fb9f2012-08-14 13:13:47 +00003653
3654 attr.setInvalid();
3655 Diag(attr.getLoc(), diag::err_invalid_pcs);
3656 return true;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003657 }
Derek Schuffa2020962012-10-16 22:30:41 +00003658 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
David Blaikie8a40f702012-01-17 06:56:22 +00003659 default: llvm_unreachable("unexpected attribute kind");
John McCall3882ace2011-01-05 12:14:39 +00003660 }
3661
Aaron Ballmane91c6be2012-10-02 14:26:08 +00003662 const TargetInfo &TI = Context.getTargetInfo();
3663 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3664 if (A == TargetInfo::CCCR_Warning) {
3665 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
3666 CC = TI.getDefaultCallingConv();
3667 }
3668
John McCall3882ace2011-01-05 12:14:39 +00003669 return false;
3670}
3671
Chandler Carruthedc2c642011-07-02 00:01:44 +00003672static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003673 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00003674
3675 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003676 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00003677 return;
3678
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003679 if (!isa<ObjCMethodDecl>(D)) {
3680 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3681 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003682 return;
3683 }
Eli Friedman7044b762009-03-27 21:06:47 +00003684
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003685 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall3882ace2011-01-05 12:14:39 +00003686}
3687
3688/// Checks a regparm attribute, returning true if it is ill-formed and
3689/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003690bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3691 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00003692 return true;
3693
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003694 if (Attr.getNumArgs() != 1) {
3695 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3696 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003697 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003698 }
Eli Friedman7044b762009-03-27 21:06:47 +00003699
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003700 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman7044b762009-03-27 21:06:47 +00003701 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00003702 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall3882ace2011-01-05 12:14:39 +00003703 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003704 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman7044b762009-03-27 21:06:47 +00003705 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003706 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003707 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003708 }
3709
Douglas Gregore8bbc122011-09-02 00:18:52 +00003710 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003711 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00003712 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003713 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003714 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003715 }
3716
John McCall3882ace2011-01-05 12:14:39 +00003717 numParams = NumParams.getZExtValue();
Douglas Gregore8bbc122011-09-02 00:18:52 +00003718 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003719 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregore8bbc122011-09-02 00:18:52 +00003720 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003721 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00003722 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00003723 }
3724
John McCall3882ace2011-01-05 12:14:39 +00003725 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00003726}
3727
Chandler Carruthedc2c642011-07-02 00:01:44 +00003728static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00003729 if (S.LangOpts.CUDA) {
3730 // check the attribute arguments.
3731 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00003732 // FIXME: 0 is not okay.
3733 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003734 return;
3735 }
3736
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003737 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00003738 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00003739 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00003740 return;
3741 }
3742
3743 Expr *MaxThreadsExpr = Attr.getArg(0);
3744 llvm::APSInt MaxThreads(32);
3745 if (MaxThreadsExpr->isTypeDependent() ||
3746 MaxThreadsExpr->isValueDependent() ||
3747 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3748 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3749 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3750 return;
3751 }
3752
3753 llvm::APSInt MinBlocks(32);
3754 if (Attr.getNumArgs() > 1) {
3755 Expr *MinBlocksExpr = Attr.getArg(1);
3756 if (MinBlocksExpr->isTypeDependent() ||
3757 MinBlocksExpr->isValueDependent() ||
3758 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3759 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3760 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3761 return;
3762 }
3763 }
3764
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003765 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne827301e2010-12-12 23:03:07 +00003766 MaxThreads.getZExtValue(),
3767 MinBlocks.getZExtValue()));
3768 } else {
3769 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3770 }
3771}
3772
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003773static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
3774 const AttributeList &Attr) {
3775 StringRef AttrName = Attr.getName()->getName();
3776 if (!Attr.getParameterName()) {
3777 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
3778 << Attr.getName() << /* arg num = */ 1;
3779 return;
3780 }
3781
3782 if (Attr.getNumArgs() != 2) {
3783 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3784 << /* required args = */ 3;
3785 return;
3786 }
3787
3788 IdentifierInfo *ArgumentKind = Attr.getParameterName();
3789
3790 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
3791 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
3792 << Attr.getName() << ExpectedFunctionOrMethod;
3793 return;
3794 }
3795
3796 uint64_t ArgumentIdx;
3797 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3798 Attr.getLoc(), 2,
3799 Attr.getArg(0), ArgumentIdx))
3800 return;
3801
3802 uint64_t TypeTagIdx;
3803 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
3804 Attr.getLoc(), 3,
3805 Attr.getArg(1), TypeTagIdx))
3806 return;
3807
3808 bool IsPointer = (AttrName == "pointer_with_type_tag");
3809 if (IsPointer) {
3810 // Ensure that buffer has a pointer type.
3811 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
3812 if (!BufferTy->isPointerType()) {
3813 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
3814 << AttrName;
3815 }
3816 }
3817
3818 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(Attr.getRange(),
3819 S.Context,
3820 ArgumentKind,
3821 ArgumentIdx,
3822 TypeTagIdx,
3823 IsPointer));
3824}
3825
3826static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
3827 const AttributeList &Attr) {
3828 IdentifierInfo *PointerKind = Attr.getParameterName();
3829 if (!PointerKind) {
3830 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier)
3831 << "type_tag_for_datatype" << 1;
3832 return;
3833 }
3834
3835 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
3836
3837 D->addAttr(::new (S.Context) TypeTagForDatatypeAttr(
3838 Attr.getRange(),
3839 S.Context,
3840 PointerKind,
3841 MatchingCType,
3842 Attr.getLayoutCompatible(),
3843 Attr.getMustBeNull()));
3844}
3845
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003846//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003847// Checker-specific attribute handlers.
3848//===----------------------------------------------------------------------===//
3849
John McCalled433932011-01-25 03:31:58 +00003850static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003851 return type->isDependentType() ||
3852 type->isObjCObjectPointerType() ||
3853 S.Context.isObjCNSObjectType(type);
John McCalled433932011-01-25 03:31:58 +00003854}
3855static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregorf892c7f2011-10-09 22:26:49 +00003856 return type->isDependentType() ||
3857 type->isPointerType() ||
3858 isValidSubjectOfNSAttribute(S, type);
John McCalled433932011-01-25 03:31:58 +00003859}
3860
Chandler Carruthedc2c642011-07-02 00:01:44 +00003861static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003862 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00003863 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003864 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003865 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00003866 return;
3867 }
3868
3869 bool typeOK, cf;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003870 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCalled433932011-01-25 03:31:58 +00003871 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3872 cf = false;
3873 } else {
3874 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3875 cf = true;
3876 }
3877
3878 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003879 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003880 << Attr.getRange() << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00003881 return;
3882 }
3883
3884 if (cf)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003885 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003886 else
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003887 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003888}
3889
Chandler Carruthedc2c642011-07-02 00:01:44 +00003890static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3891 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003892 if (!isa<ObjCMethodDecl>(D)) {
3893 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003894 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00003895 return;
3896 }
3897
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003898 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCalled433932011-01-25 03:31:58 +00003899}
3900
Chandler Carruthedc2c642011-07-02 00:01:44 +00003901static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3902 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003903
John McCalled433932011-01-25 03:31:58 +00003904 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003905
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003906 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003907 returnType = MD->getResultType();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003908 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003909 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCall31168b02011-06-15 23:02:42 +00003910 return; // ignore: was handled as a type attribute
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +00003911 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3912 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003913 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00003914 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00003915 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003916 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003917 << Attr.getRange() << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00003918 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003919 return;
3920 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003921
John McCalled433932011-01-25 03:31:58 +00003922 bool typeOK;
3923 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003924 switch (Attr.getKind()) {
David Blaikie8a40f702012-01-17 06:56:22 +00003925 default: llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003926 case AttributeList::AT_NSReturnsAutoreleased:
3927 case AttributeList::AT_NSReturnsRetained:
3928 case AttributeList::AT_NSReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003929 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3930 cf = false;
3931 break;
3932
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003933 case AttributeList::AT_CFReturnsRetained:
3934 case AttributeList::AT_CFReturnsNotRetained:
John McCalled433932011-01-25 03:31:58 +00003935 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3936 cf = true;
3937 break;
3938 }
3939
3940 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003941 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003942 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003943 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00003944 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00003945
Chandler Carruthff4c4f02011-07-01 23:49:12 +00003946 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003947 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003948 llvm_unreachable("invalid ownership attribute");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003949 case AttributeList::AT_NSReturnsAutoreleased:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003950 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCalled433932011-01-25 03:31:58 +00003951 S.Context));
3952 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003953 case AttributeList::AT_CFReturnsNotRetained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003954 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003955 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003956 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003957 case AttributeList::AT_NSReturnsNotRetained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003958 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003959 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00003960 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003961 case AttributeList::AT_CFReturnsRetained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003962 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003963 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003964 return;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003965 case AttributeList::AT_NSReturnsRetained:
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003966 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherbc638a82010-12-01 22:13:54 +00003967 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003968 return;
3969 };
3970}
3971
John McCallcf166702011-07-22 08:53:00 +00003972static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3973 const AttributeList &attr) {
3974 SourceLocation loc = attr.getLoc();
3975
3976 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3977
Fariborz Jahaniana53e5d72012-04-21 17:51:44 +00003978 if (!method) {
Fariborz Jahanian344d65c2012-04-20 22:00:46 +00003979 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00003980 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCallcf166702011-07-22 08:53:00 +00003981 return;
3982 }
3983
3984 // Check that the method returns a normal pointer.
3985 QualType resultType = method->getResultType();
Fariborz Jahanian044a5be2011-09-30 20:50:23 +00003986
3987 if (!resultType->isReferenceType() &&
3988 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCallcf166702011-07-22 08:53:00 +00003989 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3990 << SourceRange(loc)
3991 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3992
3993 // Drop the attribute.
3994 return;
3995 }
3996
3997 method->addAttr(
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003998 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCallcf166702011-07-22 08:53:00 +00003999}
4000
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004001static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4002 const AttributeList &attr) {
4003 SourceLocation loc = attr.getLoc();
4004 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4005
4006 if (!method) {
4007 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4008 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4009 return;
4010 }
4011 DeclContext *DC = method->getDeclContext();
4012 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4013 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4014 << attr.getName() << 0;
4015 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4016 return;
4017 }
4018 if (method->getMethodFamily() == OMF_dealloc) {
4019 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4020 << attr.getName() << 1;
4021 return;
4022 }
4023
4024 method->addAttr(
4025 ::new (S.Context) ObjCRequiresSuperAttr(attr.getRange(), S.Context));
4026}
4027
John McCall32f5fe12011-09-30 05:12:12 +00004028/// Handle cf_audited_transfer and cf_unknown_transfer.
4029static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4030 if (!isa<FunctionDecl>(D)) {
4031 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004032 << A.getRange() << A.getName() << ExpectedFunction;
John McCall32f5fe12011-09-30 05:12:12 +00004033 return;
4034 }
4035
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004036 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall32f5fe12011-09-30 05:12:12 +00004037
4038 // Check whether there's a conflicting attribute already present.
4039 Attr *Existing;
4040 if (IsAudited) {
4041 Existing = D->getAttr<CFUnknownTransferAttr>();
4042 } else {
4043 Existing = D->getAttr<CFAuditedTransferAttr>();
4044 }
4045 if (Existing) {
4046 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4047 << A.getName()
4048 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4049 << A.getRange() << Existing->getRange();
4050 return;
4051 }
4052
4053 // All clear; add the attribute.
4054 if (IsAudited) {
4055 D->addAttr(
4056 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
4057 } else {
4058 D->addAttr(
4059 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
4060 }
4061}
4062
John McCallf1e8b342011-09-29 07:17:38 +00004063static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4064 const AttributeList &Attr) {
4065 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4066 if (!RD || RD->isUnion()) {
4067 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004068 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallf1e8b342011-09-29 07:17:38 +00004069 }
4070
4071 IdentifierInfo *ParmName = Attr.getParameterName();
4072
4073 // In Objective-C, verify that the type names an Objective-C type.
4074 // We don't want to check this outside of ObjC because people sometimes
4075 // do crazy C declarations of Objective-C types.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004076 if (ParmName && S.getLangOpts().ObjC1) {
John McCallf1e8b342011-09-29 07:17:38 +00004077 // Check for an existing type with this name.
4078 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4079 Sema::LookupOrdinaryName);
4080 if (S.LookupName(R, Sc)) {
4081 NamedDecl *Target = R.getFoundDecl();
4082 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4083 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4084 S.Diag(Target->getLocStart(), diag::note_declared_at);
4085 }
4086 }
4087 }
4088
4089 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
4090 ParmName));
4091}
4092
Chandler Carruthedc2c642011-07-02 00:01:44 +00004093static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4094 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004095 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00004096
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004097 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004098 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004099}
4100
Chandler Carruthedc2c642011-07-02 00:01:44 +00004101static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4102 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004103 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004104 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004105 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCall31168b02011-06-15 23:02:42 +00004106 return;
4107 }
4108
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004109 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00004110 QualType type = vd->getType();
4111
4112 if (!type->isDependentType() &&
4113 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004114 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00004115 << type;
4116 return;
4117 }
4118
4119 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4120
4121 // If we have no lifetime yet, check the lifetime we're presumably
4122 // going to infer.
4123 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4124 lifetime = type->getObjCARCImplicitLifetime();
4125
4126 switch (lifetime) {
4127 case Qualifiers::OCL_None:
4128 assert(type->isDependentType() &&
4129 "didn't infer lifetime for non-dependent type?");
4130 break;
4131
4132 case Qualifiers::OCL_Weak: // meaningful
4133 case Qualifiers::OCL_Strong: // meaningful
4134 break;
4135
4136 case Qualifiers::OCL_ExplicitNone:
4137 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004138 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00004139 << (lifetime == Qualifiers::OCL_Autoreleasing);
4140 break;
4141 }
4142
Chandler Carruthff4c4f02011-07-01 23:49:12 +00004143 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004144 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCall31168b02011-06-15 23:02:42 +00004145}
4146
Francois Picheta83957a2010-12-19 06:50:37 +00004147//===----------------------------------------------------------------------===//
4148// Microsoft specific attribute handlers.
4149//===----------------------------------------------------------------------===//
4150
Chandler Carruthedc2c642011-07-02 00:01:44 +00004151static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet0706d202011-09-17 17:15:52 +00004152 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Picheta83957a2010-12-19 06:50:37 +00004153 // check the attribute arguments.
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004154 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Picheta83957a2010-12-19 06:50:37 +00004155 return;
Chandler Carruthfcc48d92011-07-11 23:30:35 +00004156
Francois Picheta83957a2010-12-19 06:50:37 +00004157 Expr *Arg = Attr.getArg(0);
4158 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregorfb65e592011-07-27 05:40:30 +00004159 if (!Str || !Str->isAscii()) {
Francois Pichet7da11662010-12-20 01:41:49 +00004160 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
4161 << "uuid" << 1;
4162 return;
4163 }
4164
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004165 StringRef StrRef = Str->getString();
Francois Pichet7da11662010-12-20 01:41:49 +00004166
4167 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
4168 StrRef.back() == '}';
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004169
Francois Pichet7da11662010-12-20 01:41:49 +00004170 // Validate GUID length.
4171 if (IsCurly && StrRef.size() != 38) {
4172 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4173 return;
4174 }
4175 if (!IsCurly && StrRef.size() != 36) {
4176 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4177 return;
4178 }
4179
Douglas Gregor5c3cc422012-03-14 16:55:17 +00004180 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichet7da11662010-12-20 01:41:49 +00004181 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004182 StringRef::iterator I = StrRef.begin();
Anders Carlsson19588aa2011-01-23 21:07:30 +00004183 if (IsCurly) // Skip the optional '{'
4184 ++I;
4185
4186 for (int i = 0; i < 36; ++i) {
Francois Pichet7da11662010-12-20 01:41:49 +00004187 if (i == 8 || i == 13 || i == 18 || i == 23) {
4188 if (*I != '-') {
4189 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4190 return;
4191 }
4192 } else if (!isxdigit(*I)) {
4193 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4194 return;
4195 }
4196 I++;
4197 }
Francois Picheta83957a2010-12-19 06:50:37 +00004198
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004199 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Picheta83957a2010-12-19 06:50:37 +00004200 Str->getString()));
Francois Pichet7da11662010-12-20 01:41:49 +00004201 } else
Francois Picheta83957a2010-12-19 06:50:37 +00004202 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davis163855f2010-02-16 18:27:26 +00004203}
4204
John McCall8d32c052012-05-22 21:28:12 +00004205static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nico Weberefa45b22012-11-07 21:31:36 +00004206 if (!S.LangOpts.MicrosoftExt) {
John McCall8d32c052012-05-22 21:28:12 +00004207 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Nico Weberefa45b22012-11-07 21:31:36 +00004208 return;
4209 }
4210
4211 AttributeList::Kind Kind = Attr.getKind();
4212 if (Kind == AttributeList::AT_SingleInheritance)
4213 D->addAttr(
4214 ::new (S.Context) SingleInheritanceAttr(Attr.getRange(), S.Context));
4215 else if (Kind == AttributeList::AT_MultipleInheritance)
4216 D->addAttr(
4217 ::new (S.Context) MultipleInheritanceAttr(Attr.getRange(), S.Context));
4218 else if (Kind == AttributeList::AT_VirtualInheritance)
4219 D->addAttr(
4220 ::new (S.Context) VirtualInheritanceAttr(Attr.getRange(), S.Context));
John McCall8d32c052012-05-22 21:28:12 +00004221}
4222
4223static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4224 if (S.LangOpts.MicrosoftExt) {
4225 AttributeList::Kind Kind = Attr.getKind();
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004226 if (Kind == AttributeList::AT_Ptr32)
John McCall8d32c052012-05-22 21:28:12 +00004227 D->addAttr(
4228 ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context));
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004229 else if (Kind == AttributeList::AT_Ptr64)
John McCall8d32c052012-05-22 21:28:12 +00004230 D->addAttr(
4231 ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context));
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004232 else if (Kind == AttributeList::AT_Win64)
John McCall8d32c052012-05-22 21:28:12 +00004233 D->addAttr(
4234 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context));
4235 } else
4236 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4237}
4238
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004239static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4240 if (S.LangOpts.MicrosoftExt)
4241 D->addAttr(::new (S.Context) ForceInlineAttr(Attr.getRange(), S.Context));
4242 else
4243 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4244}
4245
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004246//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004247// Top Level Sema Entry Points
4248//===----------------------------------------------------------------------===//
4249
Chandler Carruthedc2c642011-07-02 00:01:44 +00004250static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4251 const AttributeList &Attr) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00004252 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004253 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4254 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
4255 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00004256 default:
4257 break;
4258 }
4259}
Abramo Bagnara50099372010-04-30 13:10:51 +00004260
Chandler Carruthedc2c642011-07-02 00:01:44 +00004261static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
4262 const AttributeList &Attr) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004263 switch (Attr.getKind()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004264 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4265 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4266 case AttributeList::AT_IBOutletCollection:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004267 handleIBOutletCollection(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004268 case AttributeList::AT_AddressSpace:
4269 case AttributeList::AT_OpenCLImageAccess:
4270 case AttributeList::AT_ObjCGC:
4271 case AttributeList::AT_VectorSize:
4272 case AttributeList::AT_NeonVectorType:
4273 case AttributeList::AT_NeonPolyVectorType:
Mike Stumpd3bb5572009-07-24 19:02:52 +00004274 // Ignore these, these are type attributes, handled by
4275 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004276 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004277 case AttributeList::AT_CUDADevice:
4278 case AttributeList::AT_CUDAHost:
4279 case AttributeList::AT_Overloadable:
Peter Collingbourneb331b262011-01-21 02:08:45 +00004280 // Ignore, this is a non-inheritable attribute, handled
4281 // by ProcessNonInheritableDeclAttr.
4282 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004283 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4284 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4285 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4286 case AttributeList::AT_AlwaysInline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004287 handleAlwaysInlineAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004288 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004289 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborgd3b01bc2012-06-23 11:51:46 +00004290 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004291 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4292 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4293 case AttributeList::AT_CarriesDependency:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004294 handleDependencyAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004295 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4296 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4297 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
4298 case AttributeList::AT_Deprecated:
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004299 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated");
4300 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004301 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4302 case AttributeList::AT_ExtVectorType:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004303 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004304 break;
Quentin Colombet4e172062012-11-01 23:55:47 +00004305 case AttributeList::AT_MinSize:
4306 handleMinSizeAttr(S, D, Attr);
4307 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004308 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4309 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4310 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
4311 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4312 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004313 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00004314 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004315 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
4316 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4317 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
4318 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4319 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00004320 case AttributeList::AT_ownership_returns:
4321 case AttributeList::AT_ownership_takes:
4322 case AttributeList::AT_ownership_holds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004323 handleOwnershipAttr (S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004324 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4325 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4326 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4327 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4328 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4329 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4330 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004331
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004332 case AttributeList::AT_ObjCOwnership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004333 handleObjCOwnershipAttr(S, D, Attr); break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004334 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004335 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00004336
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004337 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCallcf166702011-07-22 08:53:00 +00004338 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4339
Fariborz Jahanian566fff02012-09-07 23:46:23 +00004340 case AttributeList::AT_ObjCRequiresSuper:
4341 handleObjCRequiresSuperAttr(S, D, Attr); break;
4342
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004343 case AttributeList::AT_NSBridged:
John McCallf1e8b342011-09-29 07:17:38 +00004344 handleNSBridgedAttr(S, scope, D, Attr); break;
4345
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004346 case AttributeList::AT_CFAuditedTransfer:
4347 case AttributeList::AT_CFUnknownTransfer:
John McCall32f5fe12011-09-30 05:12:12 +00004348 handleCFTransferAttr(S, D, Attr); break;
4349
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004350 // Checker-specific.
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004351 case AttributeList::AT_CFConsumed:
4352 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4353 case AttributeList::AT_NSConsumesSelf:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004354 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00004355
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004356 case AttributeList::AT_NSReturnsAutoreleased:
4357 case AttributeList::AT_NSReturnsNotRetained:
4358 case AttributeList::AT_CFReturnsNotRetained:
4359 case AttributeList::AT_NSReturnsRetained:
4360 case AttributeList::AT_CFReturnsRetained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004361 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00004362
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004363 case AttributeList::AT_WorkGroupSizeHint:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004364 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattnerbcffcdf2012-07-09 22:06:01 +00004365 handleWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00004366
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004367 case AttributeList::AT_InitPriority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004368 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00004369
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004370 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4371 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4372 case AttributeList::AT_Unavailable:
Benjamin Kramerf435ab42012-05-16 12:19:08 +00004373 handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable");
4374 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004375 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00004376 handleArcWeakrefUnavailableAttr (S, D, Attr);
4377 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004378 case AttributeList::AT_ObjCRootClass:
Patrick Beardacfbe9e2012-04-06 18:12:22 +00004379 handleObjCRootClassAttr(S, D, Attr);
4380 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004381 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00004382 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahanian9d4d20a2012-01-03 18:45:41 +00004383 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004384 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4385 case AttributeList::AT_ReturnsTwice:
Rafael Espindola70107f92011-10-03 14:59:42 +00004386 handleReturnsTwiceAttr(S, D, Attr);
4387 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004388 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
4389 case AttributeList::AT_Visibility: handleVisibilityAttr (S, D, Attr); break;
4390 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00004391 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004392 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4393 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4394 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4395 case AttributeList::AT_TransparentUnion:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004396 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004397 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004398 case AttributeList::AT_ObjCException:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004399 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00004400 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004401 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004402 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00004403 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004404 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4405 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4406 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4407 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4408 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4409 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4410 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4411 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4412 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00004413 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00004414 // Just ignore
4415 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004416 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00004417 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00004418 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004419 case AttributeList::AT_StdCall:
4420 case AttributeList::AT_CDecl:
4421 case AttributeList::AT_FastCall:
4422 case AttributeList::AT_ThisCall:
4423 case AttributeList::AT_Pascal:
4424 case AttributeList::AT_Pcs:
Derek Schuffa2020962012-10-16 22:30:41 +00004425 case AttributeList::AT_PnaclCall:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004426 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00004427 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004428 case AttributeList::AT_OpenCLKernel:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004429 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00004430 break;
John McCall8d32c052012-05-22 21:28:12 +00004431
4432 // Microsoft attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004433 case AttributeList::AT_MsStruct:
John McCall8d32c052012-05-22 21:28:12 +00004434 handleMsStructAttr(S, D, Attr);
4435 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004436 case AttributeList::AT_Uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00004437 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00004438 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004439 case AttributeList::AT_SingleInheritance:
4440 case AttributeList::AT_MultipleInheritance:
4441 case AttributeList::AT_VirtualInheritance:
John McCall8d32c052012-05-22 21:28:12 +00004442 handleInheritanceAttr(S, D, Attr);
4443 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004444 case AttributeList::AT_Win64:
4445 case AttributeList::AT_Ptr32:
4446 case AttributeList::AT_Ptr64:
John McCall8d32c052012-05-22 21:28:12 +00004447 handlePortabilityAttr(S, D, Attr);
4448 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004449 case AttributeList::AT_ForceInline:
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00004450 handleForceInlineAttr(S, D, Attr);
4451 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004452
4453 // Thread safety attributes:
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004454 case AttributeList::AT_GuardedVar:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004455 handleGuardedVarAttr(S, D, Attr);
4456 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004457 case AttributeList::AT_PtGuardedVar:
Michael Han3be3b442012-07-23 18:48:41 +00004458 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004459 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004460 case AttributeList::AT_ScopedLockable:
Michael Han3be3b442012-07-23 18:48:41 +00004461 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004462 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004463 case AttributeList::AT_NoAddressSafetyAnalysis:
Kostya Serebryany588d6ab2012-01-24 19:25:38 +00004464 handleNoAddressSafetyAttr(S, D, Attr);
4465 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004466 case AttributeList::AT_NoThreadSafetyAnalysis:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004467 handleNoThreadSafetyAttr(S, D, Attr);
4468 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004469 case AttributeList::AT_Lockable:
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004470 handleLockableAttr(S, D, Attr);
4471 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004472 case AttributeList::AT_GuardedBy:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004473 handleGuardedByAttr(S, D, Attr);
4474 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004475 case AttributeList::AT_PtGuardedBy:
Michael Han3be3b442012-07-23 18:48:41 +00004476 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004477 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004478 case AttributeList::AT_ExclusiveLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004479 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004480 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004481 case AttributeList::AT_ExclusiveLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004482 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004483 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004484 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004485 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004486 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004487 case AttributeList::AT_LockReturned:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004488 handleLockReturnedAttr(S, D, Attr);
4489 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004490 case AttributeList::AT_LocksExcluded:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004491 handleLocksExcludedAttr(S, D, Attr);
4492 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004493 case AttributeList::AT_SharedLockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004494 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004495 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004496 case AttributeList::AT_SharedLocksRequired:
Michael Han3be3b442012-07-23 18:48:41 +00004497 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004498 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004499 case AttributeList::AT_SharedTrylockFunction:
Michael Han3be3b442012-07-23 18:48:41 +00004500 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004501 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004502 case AttributeList::AT_UnlockFunction:
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004503 handleUnlockFunAttr(S, D, Attr);
4504 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004505 case AttributeList::AT_AcquiredBefore:
Michael Han3be3b442012-07-23 18:48:41 +00004506 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004507 break;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004508 case AttributeList::AT_AcquiredAfter:
Michael Han3be3b442012-07-23 18:48:41 +00004509 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowski63fa6672011-07-28 20:12:35 +00004510 break;
Caitlin Sadowskiaac4d212011-07-28 17:21:07 +00004511
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00004512 // Type safety attributes.
4513 case AttributeList::AT_ArgumentWithTypeTag:
4514 handleArgumentWithTypeTagAttr(S, D, Attr);
4515 break;
4516 case AttributeList::AT_TypeTagForDatatype:
4517 handleTypeTagForDatatypeAttr(S, D, Attr);
4518 break;
4519
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004520 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004521 // Ask target about the attribute.
4522 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4523 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballman478faed2012-06-19 22:09:27 +00004524 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4525 diag::warn_unhandled_ms_attribute_ignored :
4526 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004527 break;
4528 }
4529}
4530
Peter Collingbourneb331b262011-01-21 02:08:45 +00004531/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4532/// the attribute applies to decls. If the attribute is a type attribute, just
4533/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
4534/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruthedc2c642011-07-02 00:01:44 +00004535static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4536 const AttributeList &Attr,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004537 bool NonInheritable, bool Inheritable) {
4538 if (Attr.isInvalid())
4539 return;
4540
Aaron Ballman478faed2012-06-19 22:09:27 +00004541 // Type attributes are still treated as declaration attributes by
4542 // ParseMicrosoftTypeAttributes and ParseBorlandTypeAttributes. We don't
4543 // want to process them, however, because we will simply warn about ignoring
4544 // them. So instead, we will bail out early.
4545 if (Attr.isMSTypespecAttribute())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004546 return;
4547
4548 if (NonInheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00004549 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00004550
4551 if (Inheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00004552 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00004553}
4554
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004555/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4556/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00004557void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00004558 const AttributeList *AttrList,
4559 bool NonInheritable, bool Inheritable) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004560 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Rafael Espindolac67f2232012-05-10 02:50:16 +00004561 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola3c9d9472012-05-07 23:58:18 +00004562 }
Rafael Espindolac18086a2010-02-23 22:00:30 +00004563
4564 // GCC accepts
4565 // static int a9 __attribute__((weakref));
4566 // but that looks really pointless. We reject it.
Peter Collingbourneb331b262011-01-21 02:08:45 +00004567 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00004568 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekd21139a2010-07-31 01:52:11 +00004569 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindolac18086a2010-02-23 22:00:30 +00004570 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00004571 }
4572}
4573
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004574// Annotation attributes are the only attributes allowed after an access
4575// specifier.
4576bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4577 const AttributeList *AttrList) {
4578 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00004579 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00004580 handleAnnotateAttr(*this, ASDecl, *l);
4581 } else {
4582 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4583 return true;
4584 }
4585 }
4586
4587 return false;
4588}
4589
John McCall42856de2011-10-01 05:17:03 +00004590/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4591/// contains any decl attributes that we should warn about.
4592static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4593 for ( ; A; A = A->getNext()) {
4594 // Only warn if the attribute is an unignored, non-type attribute.
4595 if (A->isUsedAsTypeAttr()) continue;
4596 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4597
4598 if (A->getKind() == AttributeList::UnknownAttribute) {
4599 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4600 << A->getName() << A->getRange();
4601 } else {
4602 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4603 << A->getName() << A->getRange();
4604 }
4605 }
4606}
4607
4608/// checkUnusedDeclAttributes - Given a declarator which is not being
4609/// used to build a declaration, complain about any decl attributes
4610/// which might be lying around on it.
4611void Sema::checkUnusedDeclAttributes(Declarator &D) {
4612 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4613 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4614 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4615 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4616}
4617
Ryan Flynn7d470f32009-07-30 03:15:39 +00004618/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett634962f2012-06-14 21:40:34 +00004619/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedmance3e2c82011-09-07 04:05:06 +00004620NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4621 SourceLocation Loc) {
Ryan Flynnd963a492009-07-31 02:52:19 +00004622 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004623 NamedDecl *NewD = 0;
4624 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedmance3e2c82011-09-07 04:05:06 +00004625 FunctionDecl *NewFD;
4626 // FIXME: Missing call to CheckFunctionDeclaration().
4627 // FIXME: Mangling?
4628 // FIXME: Is the qualifier info correct?
4629 // FIXME: Is the DeclContext correct?
4630 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4631 Loc, Loc, DeclarationName(II),
4632 FD->getType(), FD->getTypeSourceInfo(),
4633 SC_None, SC_None,
4634 false/*isInlineSpecified*/,
4635 FD->hasPrototype(),
4636 false/*isConstexprSpecified*/);
4637 NewD = NewFD;
4638
4639 if (FD->getQualifier())
Douglas Gregor14454802011-02-25 02:25:35 +00004640 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedmance3e2c82011-09-07 04:05:06 +00004641
4642 // Fake up parameter variables; they are declared as if this were
4643 // a typedef.
4644 QualType FDTy = FD->getType();
4645 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4646 SmallVector<ParmVarDecl*, 16> Params;
4647 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4648 AE = FT->arg_type_end(); AI != AE; ++AI) {
4649 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4650 Param->setScopeInfo(0, Params.size());
4651 Params.push_back(Param);
4652 }
David Blaikie9c70e042011-09-21 18:16:56 +00004653 NewFD->setParams(Params);
John McCall3e11ebe2010-03-15 10:12:16 +00004654 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004655 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4656 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004657 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00004658 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00004659 VD->getStorageClass(),
4660 VD->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00004661 if (VD->getQualifier()) {
4662 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00004663 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00004664 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004665 }
4666 return NewD;
4667}
4668
James Dennett634962f2012-06-14 21:40:34 +00004669/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynn7d470f32009-07-30 03:15:39 +00004670/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00004671void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00004672 if (W.getUsed()) return; // only do this once
4673 W.setUsed(true);
4674 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4675 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedmance3e2c82011-09-07 04:05:06 +00004676 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004677 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
4678 NDId->getName()));
4679 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00004680 WeakTopLevelDecl.push_back(NewD);
4681 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
4682 // to insert Decl at TU scope, sorry.
4683 DeclContext *SavedContext = CurContext;
4684 CurContext = Context.getTranslationUnitDecl();
4685 PushOnScopeChains(NewD, S);
4686 CurContext = SavedContext;
4687 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004688 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00004689 }
4690}
4691
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004692/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
4693/// it, apply them to D. This is a bit tricky because PD can have attributes
4694/// specified in many different places, and we need to find and apply them all.
Peter Collingbourneb331b262011-01-21 02:08:45 +00004695void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
4696 bool NonInheritable, bool Inheritable) {
John McCall6fe02402010-10-27 00:59:00 +00004697 // It's valid to "forward-declare" #pragma weak, in which case we
4698 // have to do this.
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00004699 if (Inheritable) {
4700 LoadExternalWeakUndeclaredIdentifiers();
4701 if (!WeakUndeclaredIdentifiers.empty()) {
4702 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
4703 if (IdentifierInfo *Id = ND->getIdentifier()) {
4704 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
4705 = WeakUndeclaredIdentifiers.find(Id);
4706 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
4707 WeakInfo W = I->second;
4708 DeclApplyPragmaWeak(S, ND, W);
4709 WeakUndeclaredIdentifiers[Id] = W;
4710 }
John McCall6fe02402010-10-27 00:59:00 +00004711 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00004712 }
4713 }
4714 }
4715
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004716 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00004717 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004718 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004719
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004720 // Walk the declarator structure, applying decl attributes that were in a type
4721 // position to the decl itself. This handles cases like:
4722 // int *__attr__(x)** D;
4723 // when X is a decl attribute.
4724 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
4725 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004726 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00004727
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004728 // Finally, apply any attributes on the decl itself.
4729 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourneb331b262011-01-21 02:08:45 +00004730 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00004731}
John McCall28a6aea2009-11-04 02:18:39 +00004732
John McCall31168b02011-06-15 23:02:42 +00004733/// Is the given declaration allowed to use a forbidden type?
4734static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4735 // Private ivars are always okay. Unfortunately, people don't
4736 // always properly make their ivars private, even in system headers.
4737 // Plus we need to make fields okay, too.
Fariborz Jahanian6d5d6a22011-09-26 21:23:35 +00004738 // Function declarations in sys headers will be marked unavailable.
4739 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4740 !isa<FunctionDecl>(decl))
John McCall31168b02011-06-15 23:02:42 +00004741 return false;
4742
4743 // Require it to be declared in a system header.
4744 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4745}
4746
4747/// Handle a delayed forbidden-type diagnostic.
4748static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4749 Decl *decl) {
4750 if (decl && isForbiddenTypeAllowed(S, decl)) {
4751 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4752 "this system declaration uses an unsupported type"));
4753 return;
4754 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00004755 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004756 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer474261a2012-06-02 10:20:41 +00004757 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanianed1933b2011-10-03 22:11:57 +00004758 // kind of forbidden type messages on unavailable functions.
4759 if (FD->hasAttr<UnavailableAttr>() &&
4760 diag.getForbiddenTypeDiagnostic() ==
4761 diag::err_arc_array_param_no_ownership) {
4762 diag.Triggered = true;
4763 return;
4764 }
4765 }
John McCall31168b02011-06-15 23:02:42 +00004766
4767 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4768 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4769 diag.Triggered = true;
4770}
4771
John McCall2ec85372012-05-07 06:16:41 +00004772void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
4773 assert(DelayedDiagnostics.getCurrentPool());
John McCall6347b682012-05-07 06:16:58 +00004774 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall2ec85372012-05-07 06:16:41 +00004775 DelayedDiagnostics.popWithoutEmitting(state);
John McCallc1465822011-02-14 07:13:47 +00004776
John McCall2ec85372012-05-07 06:16:41 +00004777 // When delaying diagnostics to run in the context of a parsed
4778 // declaration, we only want to actually emit anything if parsing
4779 // succeeds.
4780 if (!decl) return;
John McCallc1465822011-02-14 07:13:47 +00004781
John McCall2ec85372012-05-07 06:16:41 +00004782 // We emit all the active diagnostics in this pool or any of its
4783 // parents. In general, we'll get one pool for the decl spec
4784 // and a child pool for each declarator; in a decl group like:
4785 // deprecated_typedef foo, *bar, baz();
4786 // only the declarator pops will be passed decls. This is correct;
4787 // we really do need to consider delayed diagnostics from the decl spec
4788 // for each of the different declarations.
John McCall6347b682012-05-07 06:16:58 +00004789 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall2ec85372012-05-07 06:16:41 +00004790 do {
John McCall6347b682012-05-07 06:16:58 +00004791 for (DelayedDiagnosticPool::pool_iterator
John McCall2ec85372012-05-07 06:16:41 +00004792 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
4793 // This const_cast is a bit lame. Really, Triggered should be mutable.
4794 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCallc1465822011-02-14 07:13:47 +00004795 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00004796 continue;
4797
John McCallc1465822011-02-14 07:13:47 +00004798 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00004799 case DelayedDiagnostic::Deprecation:
John McCall18a962b2012-01-26 20:04:03 +00004800 // Don't bother giving deprecation diagnostics if the decl is invalid.
4801 if (!decl->isInvalidDecl())
John McCall2ec85372012-05-07 06:16:41 +00004802 HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004803 break;
4804
4805 case DelayedDiagnostic::Access:
John McCall2ec85372012-05-07 06:16:41 +00004806 HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00004807 break;
John McCall31168b02011-06-15 23:02:42 +00004808
4809 case DelayedDiagnostic::ForbiddenType:
John McCall2ec85372012-05-07 06:16:41 +00004810 handleDelayedForbiddenType(*this, diag, decl);
John McCall31168b02011-06-15 23:02:42 +00004811 break;
John McCall86121512010-01-27 03:50:35 +00004812 }
4813 }
John McCall2ec85372012-05-07 06:16:41 +00004814 } while ((pool = pool->getParent()));
John McCall28a6aea2009-11-04 02:18:39 +00004815}
4816
John McCall6347b682012-05-07 06:16:58 +00004817/// Given a set of delayed diagnostics, re-emit them as if they had
4818/// been delayed in the current context instead of in the given pool.
4819/// Essentially, this just moves them to the current pool.
4820void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
4821 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
4822 assert(curPool && "re-emitting in undelayed context not supported");
4823 curPool->steal(pool);
4824}
4825
John McCall28a6aea2009-11-04 02:18:39 +00004826static bool isDeclDeprecated(Decl *D) {
4827 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004828 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00004829 return true;
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00004830 // A category implicitly has the availability of the interface.
4831 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4832 return CatD->getClassInterface()->isDeprecated();
John McCall28a6aea2009-11-04 02:18:39 +00004833 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4834 return false;
4835}
4836
Eli Friedman971bfa12012-08-08 21:52:41 +00004837static void
4838DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
4839 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004840 const ObjCInterfaceDecl *UnknownObjCClass,
4841 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedman971bfa12012-08-08 21:52:41 +00004842 DeclarationName Name = D->getDeclName();
4843 if (!Message.empty()) {
4844 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
4845 S.Diag(D->getLocation(),
4846 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4847 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004848 if (ObjCPropery)
4849 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4850 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00004851 } else if (!UnknownObjCClass) {
4852 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
4853 S.Diag(D->getLocation(),
4854 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
4855 : diag::note_previous_decl) << Name;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004856 if (ObjCPropery)
4857 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
4858 << ObjCPropery->getDeclName() << 0;
Eli Friedman971bfa12012-08-08 21:52:41 +00004859 } else {
4860 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
4861 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4862 }
4863}
4864
John McCallb45a1e72010-08-26 02:13:20 +00004865void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00004866 Decl *Ctx) {
4867 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00004868 return;
4869
John McCall86121512010-01-27 03:50:35 +00004870 DD.Triggered = true;
Eli Friedman971bfa12012-08-08 21:52:41 +00004871 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
4872 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004873 DD.getUnknownObjCClass(),
4874 DD.getObjCProperty());
John McCall28a6aea2009-11-04 02:18:39 +00004875}
4876
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004877void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00004878 SourceLocation Loc,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004879 const ObjCInterfaceDecl *UnknownObjCClass,
4880 const ObjCPropertyDecl *ObjCProperty) {
John McCall28a6aea2009-11-04 02:18:39 +00004881 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00004882 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004883 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4884 UnknownObjCClass,
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004885 ObjCProperty,
Fariborz Jahanian7923ef42012-03-02 21:50:02 +00004886 Message));
John McCall28a6aea2009-11-04 02:18:39 +00004887 return;
4888 }
4889
4890 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00004891 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall28a6aea2009-11-04 02:18:39 +00004892 return;
Fariborz Jahanian974c9482012-09-21 20:46:37 +00004893 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall28a6aea2009-11-04 02:18:39 +00004894}