blob: b9e4ef06330819eb6ab907f22dc09d3313951123 [file] [log] [blame]
Chris Lattner6b6b5372008-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 McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +000017#include "clang/AST/CXXInheritance.h"
John McCall384aff82010-08-25 07:42:41 +000018#include "clang/AST/DeclCXX.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000019#include "clang/AST/DeclObjC.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000021#include "clang/AST/Expr.h"
Rafael Espindolad7a60ad2013-02-26 19:13:56 +000022#include "clang/AST/Mangle.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000023#include "clang/Basic/CharInfo.h"
John McCallf85e1932011-06-15 23:02:42 +000024#include "clang/Basic/SourceManager.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000025#include "clang/Basic/TargetInfo.h"
John McCall19510852010-08-20 18:27:03 +000026#include "clang/Sema/DeclSpec.h"
John McCall9c3087b2010-08-26 02:13:20 +000027#include "clang/Sema/DelayedDiagnostic.h"
John McCallfe98da02011-09-29 07:17:38 +000028#include "clang/Sema/Lookup.h"
Richard Smith3a2b7a12013-01-28 22:42:45 +000029#include "clang/Sema/Scope.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000030#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000031using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000032using namespace sema;
Chris Lattner6b6b5372008-06-26 18:38:35 +000033
John McCall883cc2c2011-03-02 12:29:23 +000034/// These constants match the enumerated choices of
35/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000036enum AttributeDeclKind {
John McCall883cc2c2011-03-02 12:29:23 +000037 ExpectedFunction,
38 ExpectedUnion,
39 ExpectedVariableOrFunction,
40 ExpectedFunctionOrMethod,
41 ExpectedParameter,
John McCall883cc2c2011-03-02 12:29:23 +000042 ExpectedFunctionMethodOrBlock,
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +000043 ExpectedFunctionMethodOrClass,
John McCall883cc2c2011-03-02 12:29:23 +000044 ExpectedFunctionMethodOrParameter,
45 ExpectedClass,
John McCall883cc2c2011-03-02 12:29:23 +000046 ExpectedVariable,
47 ExpectedMethod,
Caitlin Sadowskidb33e142011-07-28 20:12:35 +000048 ExpectedVariableFunctionOrLabel,
Douglas Gregorf6b8b582012-03-14 16:55:17 +000049 ExpectedFieldOrGlobalVar,
Hans Wennborg5e2d5de2012-06-23 11:51:46 +000050 ExpectedStruct,
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +000051 ExpectedVariableFunctionOrTag,
Richard Smith5f838aa2013-02-01 08:25:07 +000052 ExpectedTLSVar,
53 ExpectedVariableOrField,
John McCalld4c3d662013-02-20 01:54:26 +000054 ExpectedVariableFieldOrTag,
Aaron Ballman37a89532013-07-18 14:56:42 +000055 ExpectedTypeOrNamespace,
56 ExpectedObjectiveCInterface
John McCall883cc2c2011-03-02 12:29:23 +000057};
58
Chris Lattnere5c5ee12008-06-29 00:16:31 +000059//===----------------------------------------------------------------------===//
60// Helper functions
61//===----------------------------------------------------------------------===//
62
Chandler Carruth87c44602011-07-01 23:49:12 +000063static const FunctionType *getFunctionType(const Decl *D,
Ted Kremeneka18d7d82009-08-14 20:49:40 +000064 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000065 QualType Ty;
Chandler Carruth87c44602011-07-01 23:49:12 +000066 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000067 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000068 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000069 Ty = decl->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +000070 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner6b6b5372008-06-26 18:38:35 +000071 Ty = decl->getUnderlyingType();
72 else
73 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000074
Chris Lattner6b6b5372008-06-26 18:38:35 +000075 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000076 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000077 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000078 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000079
John McCall183700f2009-09-21 23:43:11 +000080 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000081}
82
Daniel Dunbar35682492008-09-26 04:12:28 +000083// FIXME: We should provide an abstraction around a method or function
84// to provide the following bits of information.
85
Nuno Lopesd20254f2009-12-20 23:11:08 +000086/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000087/// type (function or function-typed variable).
Chandler Carruth87c44602011-07-01 23:49:12 +000088static bool isFunction(const Decl *D) {
89 return getFunctionType(D, false) != NULL;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000090}
91
92/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000093/// type (function or function-typed variable) or an Objective-C
94/// method.
Chandler Carruth87c44602011-07-01 23:49:12 +000095static bool isFunctionOrMethod(const Decl *D) {
Nick Lewycky4ae89bc2012-07-24 01:31:55 +000096 return isFunction(D) || isa<ObjCMethodDecl>(D);
Daniel Dunbar35682492008-09-26 04:12:28 +000097}
98
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000099/// isFunctionOrMethodOrBlock - Return true if the given decl has function
100/// type (function or function-typed variable) or an Objective-C
101/// method or a block.
Chandler Carruth87c44602011-07-01 23:49:12 +0000102static bool isFunctionOrMethodOrBlock(const Decl *D) {
103 if (isFunctionOrMethod(D))
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000104 return true;
105 // check for block is more involved.
Chandler Carruth87c44602011-07-01 23:49:12 +0000106 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000107 QualType Ty = V->getType();
108 return Ty->isBlockPointerType();
109 }
Chandler Carruth87c44602011-07-01 23:49:12 +0000110 return isa<BlockDecl>(D);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000111}
112
John McCall711c52b2011-01-05 12:14:39 +0000113/// Return true if the given decl has a declarator that should have
114/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruth87c44602011-07-01 23:49:12 +0000115static bool hasDeclarator(const Decl *D) {
John McCallf85e1932011-06-15 23:02:42 +0000116 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruth87c44602011-07-01 23:49:12 +0000117 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
118 isa<ObjCPropertyDecl>(D);
John McCall711c52b2011-01-05 12:14:39 +0000119}
120
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000121/// hasFunctionProto - Return true if the given decl has a argument
122/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000123/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruth87c44602011-07-01 23:49:12 +0000124static bool hasFunctionProto(const Decl *D) {
125 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000126 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000127 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000128 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000129 return true;
130 }
131}
132
133/// getFunctionOrMethodNumArgs - Return number of function or method
134/// arguments. It is an error to call this on a K&R function (use
135/// hasFunctionProto first).
Chandler Carruth87c44602011-07-01 23:49:12 +0000136static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
137 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000138 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruth87c44602011-07-01 23:49:12 +0000139 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000140 return BD->getNumParams();
Chandler Carruth87c44602011-07-01 23:49:12 +0000141 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000142}
143
Chandler Carruth87c44602011-07-01 23:49:12 +0000144static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
145 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregor72564e72009-02-26 23:50:07 +0000146 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruth87c44602011-07-01 23:49:12 +0000147 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000148 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000149
Chandler Carruth87c44602011-07-01 23:49:12 +0000150 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000151}
152
Chandler Carruth87c44602011-07-01 23:49:12 +0000153static QualType getFunctionOrMethodResultType(const Decl *D) {
154 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000155 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +0000156 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000157}
158
Chandler Carruth87c44602011-07-01 23:49:12 +0000159static bool isFunctionOrMethodVariadic(const Decl *D) {
160 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000161 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000162 return proto->isVariadic();
Chandler Carruth87c44602011-07-01 23:49:12 +0000163 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000164 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000165 else {
Chandler Carruth87c44602011-07-01 23:49:12 +0000166 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbar35682492008-09-26 04:12:28 +0000167 }
168}
169
Chandler Carruth87c44602011-07-01 23:49:12 +0000170static bool isInstanceMethod(const Decl *D) {
171 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000172 return MethodDecl->isInstance();
173 return false;
174}
175
Chris Lattner6b6b5372008-06-26 18:38:35 +0000176static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000177 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000178 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000179 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000180
John McCall506b57e2010-05-17 21:00:27 +0000181 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
182 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000183 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000184
John McCall506b57e2010-05-17 21:00:27 +0000185 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000186
Chris Lattner6b6b5372008-06-26 18:38:35 +0000187 // FIXME: Should we walk the chain of classes?
188 return ClsName == &Ctx.Idents.get("NSString") ||
189 ClsName == &Ctx.Idents.get("NSMutableString");
190}
191
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000192static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000193 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000194 if (!PT)
195 return false;
196
Ted Kremenek6217b802009-07-29 21:53:49 +0000197 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000198 if (!RT)
199 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000200
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000201 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000202 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000203 return false;
204
205 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
206}
207
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000208/// \brief Check if the attribute has exactly as many args as Num. May
209/// output an error.
Chandler Carruth1731e202011-07-11 23:30:35 +0000210static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
211 unsigned int Num) {
212 if (Attr.getNumArgs() != Num) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +0000213 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
214 << Attr.getName() << Num;
Chandler Carruth1731e202011-07-11 23:30:35 +0000215 return false;
216 }
217
218 return true;
219}
220
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000221
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000222/// \brief Check if the attribute has at least as many args as Num. May
223/// output an error.
224static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
225 unsigned int Num) {
226 if (Attr.getNumArgs() < Num) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000227 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
228 return false;
229 }
230
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000231 return true;
232}
233
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000234/// \brief Check if IdxExpr is a valid argument index for a function or
235/// instance method D. May output an error.
236///
237/// \returns true if IdxExpr is a valid index.
238static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D,
239 StringRef AttrName,
240 SourceLocation AttrLoc,
241 unsigned AttrArgNum,
242 const Expr *IdxExpr,
243 uint64_t &Idx)
244{
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000245 assert(isFunctionOrMethod(D));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000246
247 // In C++ the implicit 'this' function parameter also counts.
248 // Parameters are counted from one.
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000249 bool HP = hasFunctionProto(D);
250 bool HasImplicitThisParam = isInstanceMethod(D);
251 bool IV = HP && isFunctionOrMethodVariadic(D);
252 unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) +
253 HasImplicitThisParam;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000254
255 llvm::APSInt IdxInt;
256 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
257 !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000258 std::string Name = std::string("'") + AttrName.str() + std::string("'");
259 S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str()
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000260 << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000261 return false;
262 }
263
264 Idx = IdxInt.getLimitedValue();
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +0000265 if (Idx < 1 || (!IV && Idx > NumArgs)) {
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000266 S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds)
267 << AttrName << AttrArgNum << IdxExpr->getSourceRange();
268 return false;
269 }
270 Idx--; // Convert to zero-based.
271 if (HasImplicitThisParam) {
272 if (Idx == 0) {
273 S.Diag(AttrLoc,
274 diag::err_attribute_invalid_implicit_this_argument)
275 << AttrName << IdxExpr->getSourceRange();
276 return false;
277 }
278 --Idx;
279 }
280
281 return true;
282}
283
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000284///
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000285/// \brief Check if passed in Decl is a field or potentially shared global var
286/// \return true if the Decl is a field or potentially shared global variable
287///
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000288static bool mayBeSharedVariable(const Decl *D) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000289 if (isa<FieldDecl>(D))
290 return true;
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000291 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
Richard Smith38afbc72013-04-13 02:43:54 +0000292 return vd->hasGlobalStorage() && !vd->getTLSKind();
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000293
294 return false;
295}
296
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000297/// \brief Check if the passed-in expression is of type int or bool.
298static bool isIntOrBool(Expr *Exp) {
299 QualType QT = Exp->getType();
300 return QT->isBooleanType() || QT->isIntegerType();
301}
302
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000303
304// Check to see if the type is a smart pointer of some kind. We assume
305// it's a smart pointer if it defines both operator-> and operator*.
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000306static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
307 DeclContextLookupConstResult Res1 = RT->getDecl()->lookup(
308 S.Context.DeclarationNames.getCXXOperatorName(OO_Star));
David Blaikie3bc93e32012-12-19 00:45:41 +0000309 if (Res1.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000310 return false;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000311
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000312 DeclContextLookupConstResult Res2 = RT->getDecl()->lookup(
313 S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow));
David Blaikie3bc93e32012-12-19 00:45:41 +0000314 if (Res2.empty())
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000315 return false;
316
317 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000318}
319
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000320/// \brief Check if passed in Decl is a pointer type.
321/// Note that this function may produce an error message.
322/// \return true if the Decl is a pointer type; false otherwise
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000323static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
324 const AttributeList &Attr) {
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000325 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000326 QualType QT = vd->getType();
Benjamin Kramer39997fc2011-08-02 04:50:49 +0000327 if (QT->isAnyPointerType())
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000328 return true;
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000329
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000330 if (const RecordType *RT = QT->getAs<RecordType>()) {
331 // If it's an incomplete type, it could be a smart pointer; skip it.
332 // (We don't want to force template instantiation if we can avoid it,
333 // since that would alter the order in which templates are instantiated.)
334 if (RT->isIncompleteType())
335 return true;
336
337 if (threadSafetyCheckIsSmartPointer(S, RT))
338 return true;
339 }
DeLesley Hutchinsaed9ea32012-04-23 18:39:55 +0000340
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000341 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer)
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000342 << Attr.getName()->getName() << QT;
343 } else {
344 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
345 << Attr.getName();
346 }
347 return false;
348}
349
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000350/// \brief Checks that the passed in QualType either is of RecordType or points
351/// to RecordType. Returns the relevant RecordType, null if it does not exit.
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000352static const RecordType *getRecordType(QualType QT) {
353 if (const RecordType *RT = QT->getAs<RecordType>())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000354 return RT;
Benjamin Kramer7d23b4a2011-08-19 04:18:11 +0000355
356 // Now check if we point to record type.
357 if (const PointerType *PT = QT->getAs<PointerType>())
358 return PT->getPointeeType()->getAs<RecordType>();
359
360 return 0;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000361}
362
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000363
Jordy Rosefad5de92012-05-08 03:27:22 +0000364static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier,
365 CXXBasePath &Path, void *Unused) {
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000366 const RecordType *RT = Specifier->getType()->getAs<RecordType>();
367 if (RT->getDecl()->getAttr<LockableAttr>())
368 return true;
369 return false;
370}
371
372
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000373/// \brief Thread Safety Analysis: Checks that the passed in RecordType
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000374/// resolves to a lockable object.
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000375static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
376 QualType Ty) {
377 const RecordType *RT = getRecordType(Ty);
Michael Hanf1aae3b2012-08-03 17:40:43 +0000378
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000379 // Warn if could not get record type for this argument.
Benjamin Kramerd77ba892011-09-03 03:30:59 +0000380 if (!RT) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000381 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class)
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000382 << Attr.getName() << Ty.getAsString();
383 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000384 }
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000385
Michael Hanf1aae3b2012-08-03 17:40:43 +0000386 // Don't check for lockable if the class hasn't been defined yet.
DeLesley Hutchins634b2932012-02-16 17:15:51 +0000387 if (RT->isIncompleteType())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000388 return;
DeLesley Hutchins60f20242012-05-02 22:18:42 +0000389
390 // Allow smart pointers to be used as lockable objects.
391 // FIXME -- Check the type that the smart pointer points to.
392 if (threadSafetyCheckIsSmartPointer(S, RT))
393 return;
394
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000395 // Check if the type is lockable.
396 RecordDecl *RD = RT->getDecl();
397 if (RD->getAttr<LockableAttr>())
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000398 return;
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000399
400 // Else check if any base classes are lockable.
401 if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
402 CXXBasePaths BPaths(false, false);
403 if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths))
404 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000405 }
DeLesley Hutchinsbbba25f2012-05-04 16:28:38 +0000406
407 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
408 << Attr.getName() << Ty.getAsString();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000409}
410
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000411/// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000412/// from Sidx, resolve to a lockable object.
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000413/// \param Sidx The attribute argument index to start checking with.
414/// \param ParamIdxOk Whether an argument can be indexing into a function
415/// parameter list.
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000416static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000417 const AttributeList &Attr,
418 SmallVectorImpl<Expr*> &Args,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000419 int Sidx = 0,
420 bool ParamIdxOk = false) {
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000421 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000422 Expr *ArgExp = Attr.getArg(Idx);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000423
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000424 if (ArgExp->isTypeDependent()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000425 // FIXME -- need to check this again on template instantiation
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000426 Args.push_back(ArgExp);
427 continue;
428 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000429
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000430 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000431 if (StrLit->getLength() == 0 ||
432 StrLit->getString() == StringRef("*")) {
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000433 // Pass empty strings to the analyzer without warnings.
DeLesley Hutchins0b4db3e2012-09-07 17:34:53 +0000434 // Treat "*" as the universal lock.
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000435 Args.push_back(ArgExp);
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000436 continue;
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000437 }
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000438
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000439 // We allow constant strings to be used as a placeholder for expressions
440 // that are not valid C++ syntax, but warn that they are ignored.
441 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
442 Attr.getName();
DeLesley Hutchins4e4c1572012-08-31 21:57:32 +0000443 Args.push_back(ArgExp);
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000444 continue;
445 }
446
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000447 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000448
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000449 // A pointer to member expression of the form &MyClass::mu is treated
450 // specially -- we need to look at the type of the member.
451 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
452 if (UOp->getOpcode() == UO_AddrOf)
453 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
454 if (DRE->getDecl()->isCXXInstanceMember())
455 ArgTy = DRE->getDecl()->getType();
456
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000457 // First see if we can just cast to record type, or point to record type.
458 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000459
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000460 // Now check if we index into a record type function param.
461 if(!RT && ParamIdxOk) {
462 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000463 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
464 if(FD && IL) {
465 unsigned int NumParams = FD->getNumParams();
466 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000467 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
468 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
469 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000470 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
471 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000472 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000473 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000474 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000475 }
476 }
477
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000478 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000479
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000480 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000481 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000482}
483
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000484//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000485// Attribute Implementations
486//===----------------------------------------------------------------------===//
487
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000488// FIXME: All this manual attribute parsing code is gross. At the
489// least add some helper functions to check most argument patterns (#
490// and types of args).
491
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000492enum ThreadAttributeDeclKind {
493 ThreadExpectedFieldOrGlobalVar,
494 ThreadExpectedFunctionOrMethod,
495 ThreadExpectedClassOrStruct
496};
497
Michael Hanf1aae3b2012-08-03 17:40:43 +0000498static bool checkGuardedVarAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000499 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000500 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000501 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000502
503 // D must be either a member field or global (potentially shared) variable.
504 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000505 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
506 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000507 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000508 }
509
Michael Handc691572012-07-23 18:48:41 +0000510 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000511}
512
Michael Handc691572012-07-23 18:48:41 +0000513static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
514 if (!checkGuardedVarAttrCommon(S, D, Attr))
515 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000516
Michael Han51d8c522013-01-24 16:46:58 +0000517 D->addAttr(::new (S.Context)
518 GuardedVarAttr(Attr.getRange(), S.Context,
519 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000520}
521
Michael Hanf1aae3b2012-08-03 17:40:43 +0000522static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han51d8c522013-01-24 16:46:58 +0000523 const AttributeList &Attr) {
Michael Handc691572012-07-23 18:48:41 +0000524 if (!checkGuardedVarAttrCommon(S, D, Attr))
525 return;
526
527 if (!threadSafetyCheckIsPointer(S, D, Attr))
528 return;
529
Michael Han51d8c522013-01-24 16:46:58 +0000530 D->addAttr(::new (S.Context)
531 PtGuardedVarAttr(Attr.getRange(), S.Context,
532 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000533}
534
Michael Hanf1aae3b2012-08-03 17:40:43 +0000535static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
536 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000537 Expr* &Arg) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000538 if (!checkAttributeNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000539 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000540
541 // D must be either a member field or global (potentially shared) variable.
542 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000543 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
544 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000545 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000546 }
547
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000548 SmallVector<Expr*, 1> Args;
549 // check that all arguments are lockable objects
550 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
551 unsigned Size = Args.size();
552 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000553 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000554
Michael Handc691572012-07-23 18:48:41 +0000555 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000556
Michael Handc691572012-07-23 18:48:41 +0000557 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000558}
559
Michael Handc691572012-07-23 18:48:41 +0000560static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
561 Expr *Arg = 0;
562 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
563 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000564
Michael Handc691572012-07-23 18:48:41 +0000565 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
566}
567
Michael Hanf1aae3b2012-08-03 17:40:43 +0000568static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000569 const AttributeList &Attr) {
570 Expr *Arg = 0;
571 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
572 return;
573
574 if (!threadSafetyCheckIsPointer(S, D, Attr))
575 return;
576
577 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
578 S.Context, Arg));
579}
580
Michael Hanf1aae3b2012-08-03 17:40:43 +0000581static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000582 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000583 if (!checkAttributeNumArgs(S, Attr, 0))
Michael Handc691572012-07-23 18:48:41 +0000584 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000585
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000586 // FIXME: Lockable structs for C code.
David Blaikie88c4b5e2013-07-29 18:24:03 +0000587 if (!isa<RecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000588 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
589 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000590 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000591 }
592
Michael Handc691572012-07-23 18:48:41 +0000593 return true;
594}
595
596static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
597 if (!checkLockableAttrCommon(S, D, Attr))
598 return;
599
600 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
601}
602
Michael Hanf1aae3b2012-08-03 17:40:43 +0000603static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000604 const AttributeList &Attr) {
605 if (!checkLockableAttrCommon(S, D, Attr))
606 return;
607
Michael Han51d8c522013-01-24 16:46:58 +0000608 D->addAttr(::new (S.Context)
609 ScopedLockableAttr(Attr.getRange(), S.Context,
610 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000611}
612
Kostya Serebryany85aee962013-02-26 06:58:27 +0000613static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D,
614 const AttributeList &Attr) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000615 if (!checkAttributeNumArgs(S, Attr, 0))
616 return;
617
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000618 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000619 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
620 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000621 return;
622 }
623
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000624 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000625 S.Context));
626}
627
Kostya Serebryany85aee962013-02-26 06:58:27 +0000628static void handleNoSanitizeAddressAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000629 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000630 if (!checkAttributeNumArgs(S, Attr, 0))
631 return;
632
633 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000634 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
Kostya Serebryany71efba02012-01-24 19:25:38 +0000635 << Attr.getName() << ExpectedFunctionOrMethod;
636 return;
637 }
638
Michael Han51d8c522013-01-24 16:46:58 +0000639 D->addAttr(::new (S.Context)
Kostya Serebryany85aee962013-02-26 06:58:27 +0000640 NoSanitizeAddressAttr(Attr.getRange(), S.Context,
641 Attr.getAttributeSpellingListIndex()));
642}
643
644static void handleNoSanitizeMemory(Sema &S, Decl *D,
645 const AttributeList &Attr) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000646 if (!checkAttributeNumArgs(S, Attr, 0))
647 return;
648
649 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
650 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
651 << Attr.getName() << ExpectedFunctionOrMethod;
652 return;
653 }
654
655 D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(),
656 S.Context));
657}
658
659static void handleNoSanitizeThread(Sema &S, Decl *D,
660 const AttributeList &Attr) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000661 if (!checkAttributeNumArgs(S, Attr, 0))
662 return;
663
664 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
665 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
666 << Attr.getName() << ExpectedFunctionOrMethod;
667 return;
668 }
669
670 D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(),
671 S.Context));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000672}
673
Michael Hanf1aae3b2012-08-03 17:40:43 +0000674static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
675 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000676 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000677 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000678 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000679
680 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000681 ValueDecl *VD = dyn_cast<ValueDecl>(D);
682 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000683 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
684 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000685 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000686 }
687
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000688 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000689 QualType QT = VD->getType();
690 if (!QT->isDependentType()) {
691 const RecordType *RT = getRecordType(QT);
692 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000693 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000694 << Attr.getName();
695 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000696 }
697 }
698
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000699 // Check that all arguments are lockable objects.
700 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000701 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000702 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000703
Michael Handc691572012-07-23 18:48:41 +0000704 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000705}
706
Michael Hanf1aae3b2012-08-03 17:40:43 +0000707static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000708 const AttributeList &Attr) {
709 SmallVector<Expr*, 1> Args;
710 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
711 return;
712
713 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000714 D->addAttr(::new (S.Context)
715 AcquiredAfterAttr(Attr.getRange(), S.Context,
716 StartArg, Args.size(),
717 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000718}
719
Michael Hanf1aae3b2012-08-03 17:40:43 +0000720static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000721 const AttributeList &Attr) {
722 SmallVector<Expr*, 1> Args;
723 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
724 return;
725
726 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000727 D->addAttr(::new (S.Context)
728 AcquiredBeforeAttr(Attr.getRange(), S.Context,
729 StartArg, Args.size(),
730 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000731}
732
Michael Hanf1aae3b2012-08-03 17:40:43 +0000733static bool checkLockFunAttrCommon(Sema &S, Decl *D,
734 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000735 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000736 // zero or more arguments ok
737
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000738 // check that the attribute is applied to a function
739 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000740 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
741 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000742 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000743 }
744
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000745 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000746 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000747
Michael Handc691572012-07-23 18:48:41 +0000748 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000749}
750
Michael Hanf1aae3b2012-08-03 17:40:43 +0000751static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000752 const AttributeList &Attr) {
753 SmallVector<Expr*, 1> Args;
754 if (!checkLockFunAttrCommon(S, D, Attr, Args))
755 return;
756
757 unsigned Size = Args.size();
758 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000759 D->addAttr(::new (S.Context)
760 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
761 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000762}
763
Michael Hanf1aae3b2012-08-03 17:40:43 +0000764static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000765 const AttributeList &Attr) {
766 SmallVector<Expr*, 1> Args;
767 if (!checkLockFunAttrCommon(S, D, Attr, Args))
768 return;
769
770 unsigned Size = Args.size();
771 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000772 D->addAttr(::new (S.Context)
773 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
774 StartArg, Size,
775 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000776}
777
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +0000778static void handleAssertSharedLockAttr(Sema &S, Decl *D,
779 const AttributeList &Attr) {
780 SmallVector<Expr*, 1> Args;
781 if (!checkLockFunAttrCommon(S, D, Attr, Args))
782 return;
783
784 unsigned Size = Args.size();
785 Expr **StartArg = Size == 0 ? 0 : &Args[0];
786 D->addAttr(::new (S.Context)
787 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
788 Attr.getAttributeSpellingListIndex()));
789}
790
791static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
792 const AttributeList &Attr) {
793 SmallVector<Expr*, 1> Args;
794 if (!checkLockFunAttrCommon(S, D, Attr, Args))
795 return;
796
797 unsigned Size = Args.size();
798 Expr **StartArg = Size == 0 ? 0 : &Args[0];
799 D->addAttr(::new (S.Context)
800 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
801 StartArg, Size,
802 Attr.getAttributeSpellingListIndex()));
803}
804
805
Michael Hanf1aae3b2012-08-03 17:40:43 +0000806static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
807 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000808 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000809 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000810 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000811
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000812 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000813 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
814 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000815 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000816 }
817
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000818 if (!isIntOrBool(Attr.getArg(0))) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000819 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000820 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Handc691572012-07-23 18:48:41 +0000821 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000822 }
823
824 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000825 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000826
Michael Handc691572012-07-23 18:48:41 +0000827 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000828}
829
Michael Hanf1aae3b2012-08-03 17:40:43 +0000830static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000831 const AttributeList &Attr) {
832 SmallVector<Expr*, 2> Args;
833 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
834 return;
835
836 unsigned Size = Args.size();
837 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000838 D->addAttr(::new (S.Context)
839 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
840 Attr.getArg(0), StartArg, Size,
841 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000842}
843
Michael Hanf1aae3b2012-08-03 17:40:43 +0000844static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000845 const AttributeList &Attr) {
846 SmallVector<Expr*, 2> Args;
847 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
848 return;
849
850 unsigned Size = Args.size();
851 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000852 D->addAttr(::new (S.Context)
853 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
854 Attr.getArg(0), StartArg, Size,
855 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000856}
857
Michael Hanf1aae3b2012-08-03 17:40:43 +0000858static bool checkLocksRequiredCommon(Sema &S, Decl *D,
859 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000860 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000861 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000862 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000863
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000864 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000865 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
866 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000867 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000868 }
869
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000870 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000871 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000872 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000873 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000874
Michael Handc691572012-07-23 18:48:41 +0000875 return true;
876}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000877
Michael Hanf1aae3b2012-08-03 17:40:43 +0000878static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000879 const AttributeList &Attr) {
880 SmallVector<Expr*, 1> Args;
881 if (!checkLocksRequiredCommon(S, D, Attr, Args))
882 return;
883
884 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000885 D->addAttr(::new (S.Context)
886 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
887 StartArg, Args.size(),
888 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000889}
890
Michael Hanf1aae3b2012-08-03 17:40:43 +0000891static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000892 const AttributeList &Attr) {
893 SmallVector<Expr*, 1> Args;
894 if (!checkLocksRequiredCommon(S, D, Attr, Args))
895 return;
896
897 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000898 D->addAttr(::new (S.Context)
899 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
900 StartArg, Args.size(),
901 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000902}
903
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000904static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000905 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000906 // zero or more arguments ok
907
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000908 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000909 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
910 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000911 return;
912 }
913
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000914 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000915 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000916 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000917 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000918 Expr **StartArg = Size == 0 ? 0 : &Args[0];
919
Michael Han51d8c522013-01-24 16:46:58 +0000920 D->addAttr(::new (S.Context)
921 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
922 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000923}
924
925static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000926 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000927 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000928 return;
929
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000930 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000931 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
932 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000933 return;
934 }
935
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000936 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000937 SmallVector<Expr*, 1> Args;
938 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
939 unsigned Size = Args.size();
940 if (Size == 0)
941 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000942
Michael Han51d8c522013-01-24 16:46:58 +0000943 D->addAttr(::new (S.Context)
944 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
945 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000946}
947
948static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000949 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000950 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000951 return;
952
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000953 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000954 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
955 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000956 return;
957 }
958
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000959 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000960 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000961 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000962 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000963 if (Size == 0)
964 return;
965 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000966
Michael Han51d8c522013-01-24 16:46:58 +0000967 D->addAttr(::new (S.Context)
968 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
969 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000970}
971
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +0000972static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +0000973 if (!checkAttributeNumArgs(S, Attr, 0)) return;
974
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +0000975 if (!isa<CXXRecordDecl>(D)) {
976 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
977 Attr.getName() << ExpectedClass;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +0000978 return;
979 }
980
981 D->addAttr(::new (S.Context)
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +0000982 ConsumableAttr(Attr.getRange(), S.Context,
983 Attr.getAttributeSpellingListIndex()));
984}
985
986static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
987 const AttributeList &Attr) {
988 ASTContext &CurrContext = S.getASTContext();
989 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
990
991 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
992 if (!RD->hasAttr<ConsumableAttr>()) {
993 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
994 RD->getNameAsString();
995
996 return false;
997 }
998 }
999
1000 return true;
1001}
1002
1003static void handleConsumesAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1004 if (!checkAttributeNumArgs(S, Attr, 0)) return;
1005
1006 if (!isa<CXXMethodDecl>(D)) {
1007 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1008 Attr.getName() << ExpectedMethod;
1009 return;
1010 }
1011
1012 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1013 return;
1014
1015 D->addAttr(::new (S.Context)
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001016 ConsumesAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001017 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001018}
1019
1020static void handleCallableWhenUnconsumedAttr(Sema &S, Decl *D,
1021 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001022 if (!checkAttributeNumArgs(S, Attr, 0)) return;
1023
1024 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001025 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1026 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001027 return;
1028 }
1029
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001030 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1031 return;
1032
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001033 D->addAttr(::new (S.Context)
1034 CallableWhenUnconsumedAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001035 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001036}
1037
1038static void handleTestsConsumedAttr(Sema &S, Decl *D,
1039 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001040 if (!checkAttributeNumArgs(S, Attr, 0)) return;
1041
1042 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001043 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1044 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001045 return;
1046 }
1047
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001048 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1049 return;
1050
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001051 D->addAttr(::new (S.Context)
1052 TestsConsumedAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001053 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001054}
1055
1056static void handleTestsUnconsumedAttr(Sema &S, Decl *D,
1057 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001058 if (!checkAttributeNumArgs(S, Attr, 0)) return;
1059
1060 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001061 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1062 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001063 return;
1064 }
1065
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001066 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1067 return;
1068
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001069 D->addAttr(::new (S.Context)
1070 TestsUnconsumedAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001071 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001072}
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00001073
Chandler Carruth1b03c872011-07-02 00:01:44 +00001074static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1075 const AttributeList &Attr) {
Richard Smitha4fa9002013-01-13 02:11:23 +00001076 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1077 if (TD == 0) {
1078 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1079 // and type-ids.
Chris Lattner803d0802008-06-29 00:43:07 +00001080 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +00001081 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001082 }
Mike Stumpbf916502009-07-24 19:02:52 +00001083
Richard Smitha4fa9002013-01-13 02:11:23 +00001084 // Remember this typedef decl, we will need it later for diagnostics.
1085 S.ExtVectorDecls.push_back(TD);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001086}
1087
Chandler Carruth1b03c872011-07-02 00:01:44 +00001088static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001089 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001090 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001091 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001092
Chandler Carruth87c44602011-07-01 23:49:12 +00001093 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001094 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +00001095 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001096 // If the alignment is less than or equal to 8 bits, the packed attribute
1097 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +00001098 if (!FD->getType()->isDependentType() &&
1099 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +00001100 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001101 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001102 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001103 else
Michael Han51d8c522013-01-24 16:46:58 +00001104 FD->addAttr(::new (S.Context)
1105 PackedAttr(Attr.getRange(), S.Context,
1106 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001107 } else
Chris Lattner3c73c412008-11-19 08:23:25 +00001108 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001109}
1110
Chandler Carruth1b03c872011-07-02 00:01:44 +00001111static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +00001112 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001113 RD->addAttr(::new (S.Context)
1114 MsStructAttr(Attr.getRange(), S.Context,
1115 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +00001116 else
1117 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1118}
1119
Chandler Carruth1b03c872011-07-02 00:01:44 +00001120static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +00001121 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001122 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +00001123 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001124
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001125 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +00001126 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001127 if (MD->isInstanceMethod()) {
Michael Han51d8c522013-01-24 16:46:58 +00001128 D->addAttr(::new (S.Context)
1129 IBActionAttr(Attr.getRange(), S.Context,
1130 Attr.getAttributeSpellingListIndex()));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001131 return;
1132 }
1133
Ted Kremenek4ee2bb12011-02-04 06:54:16 +00001134 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001135}
1136
Ted Kremenek2f041d02011-09-29 07:02:25 +00001137static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1138 // The IBOutlet/IBOutletCollection attributes only apply to instance
1139 // variables or properties of Objective-C classes. The outlet must also
1140 // have an object reference type.
1141 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1142 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001143 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001144 << Attr.getName() << VD->getType() << 0;
1145 return false;
1146 }
1147 }
1148 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1149 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001150 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001151 << Attr.getName() << PD->getType() << 1;
1152 return false;
1153 }
1154 }
1155 else {
1156 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1157 return false;
1158 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001159
Ted Kremenek2f041d02011-09-29 07:02:25 +00001160 return true;
1161}
1162
Chandler Carruth1b03c872011-07-02 00:01:44 +00001163static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001164 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001165 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001166 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001167
1168 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001169 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001170
Michael Han51d8c522013-01-24 16:46:58 +00001171 D->addAttr(::new (S.Context)
1172 IBOutletAttr(Attr.getRange(), S.Context,
1173 Attr.getAttributeSpellingListIndex()));
Ted Kremenek96329d42008-07-15 22:26:48 +00001174}
1175
Chandler Carruth1b03c872011-07-02 00:01:44 +00001176static void handleIBOutletCollection(Sema &S, Decl *D,
1177 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001178
1179 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001180 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001181 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1182 << Attr.getName() << 1;
Ted Kremenek857e9182010-05-19 17:38:06 +00001183 return;
1184 }
1185
Ted Kremenek2f041d02011-09-29 07:02:25 +00001186 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001187 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001188
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001189 IdentifierInfo *II = Attr.getParameterName();
1190 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001191 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001192
John McCallb3d87482010-08-24 05:47:05 +00001193 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001194 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001195 if (!TypeRep) {
1196 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1197 return;
1198 }
John McCallb3d87482010-08-24 05:47:05 +00001199 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001200 // Diagnose use of non-object type in iboutletcollection attribute.
1201 // FIXME. Gnu attribute extension ignores use of builtin types in
1202 // attributes. So, __attribute__((iboutletcollection(char))) will be
1203 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001204 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001205 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1206 return;
1207 }
Michael Han51d8c522013-01-24 16:46:58 +00001208 D->addAttr(::new (S.Context)
1209 IBOutletCollectionAttr(Attr.getRange(),S.Context,
1210 QT, Attr.getParameterLoc(),
1211 Attr.getAttributeSpellingListIndex()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001212}
1213
Chandler Carruthd309c812011-07-01 23:49:16 +00001214static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001215 if (const RecordType *UT = T->getAsUnionType())
1216 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1217 RecordDecl *UD = UT->getDecl();
1218 for (RecordDecl::field_iterator it = UD->field_begin(),
1219 itend = UD->field_end(); it != itend; ++it) {
1220 QualType QT = it->getType();
1221 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1222 T = QT;
1223 return;
1224 }
1225 }
1226 }
1227}
1228
Nuno Lopes587de5b2012-05-24 00:22:00 +00001229static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001230 if (!isFunctionOrMethod(D)) {
1231 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001232 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopes174930d2012-06-18 16:39:04 +00001233 return;
1234 }
1235
Nuno Lopes587de5b2012-05-24 00:22:00 +00001236 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1237 return;
1238
Nuno Lopes587de5b2012-05-24 00:22:00 +00001239 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001240 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
1241 Expr *Ex = Attr.getArg(i);
1242 uint64_t Idx;
1243 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1244 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes587de5b2012-05-24 00:22:00 +00001245 return;
Nuno Lopes587de5b2012-05-24 00:22:00 +00001246
1247 // check if the function argument is of an integer type
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001248 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001249 if (!T->isIntegerType()) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00001250 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1251 << Attr.getName() << AANT_ArgumentIntegerConstant
1252 << Ex->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001253 return;
1254 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001255 SizeArgs.push_back(Idx);
Nuno Lopes587de5b2012-05-24 00:22:00 +00001256 }
1257
1258 // check if the function returns a pointer
1259 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1260 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001261 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001262 }
1263
Michael Han51d8c522013-01-24 16:46:58 +00001264 D->addAttr(::new (S.Context)
1265 AllocSizeAttr(Attr.getRange(), S.Context,
1266 SizeArgs.data(), SizeArgs.size(),
1267 Attr.getAttributeSpellingListIndex()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001268}
1269
Chandler Carruth1b03c872011-07-02 00:01:44 +00001270static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001271 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1272 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001273 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001274 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001275 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001276 return;
1277 }
Mike Stumpbf916502009-07-24 19:02:52 +00001278
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001279 SmallVector<unsigned, 8> NonNullArgs;
1280 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
1281 Expr *Ex = Attr.getArg(i);
1282 uint64_t Idx;
1283 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1284 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001285 return;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001286
1287 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001288 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001289 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001290
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001291 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001292 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001293 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001294 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001295 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001296 }
Mike Stumpbf916502009-07-24 19:02:52 +00001297
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001298 NonNullArgs.push_back(Idx);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001299 }
Mike Stumpbf916502009-07-24 19:02:52 +00001300
1301 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1302 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001303 if (NonNullArgs.empty()) {
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001304 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1305 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001306 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001307 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001308 NonNullArgs.push_back(i);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001309 }
Mike Stumpbf916502009-07-24 19:02:52 +00001310
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001311 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001312 if (NonNullArgs.empty()) {
1313 // Warn the trivial case only if attribute is not coming from a
1314 // macro instantiation.
1315 if (Attr.getLoc().isFileID())
1316 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001317 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001318 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001319 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001320
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001321 unsigned *start = &NonNullArgs[0];
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001322 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001323 llvm::array_pod_sort(start, start + size);
Michael Han51d8c522013-01-24 16:46:58 +00001324 D->addAttr(::new (S.Context)
1325 NonNullAttr(Attr.getRange(), S.Context, start, size,
1326 Attr.getAttributeSpellingListIndex()));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001327}
1328
Chandler Carruth1b03c872011-07-02 00:01:44 +00001329static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001330 // This attribute must be applied to a function declaration.
1331 // The first argument to the attribute must be a string,
1332 // the name of the resource, for example "malloc".
1333 // The following arguments must be argument indexes, the arguments must be
1334 // of integer type for Returns, otherwise of pointer type.
1335 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001336 // after being held. free() should be __attribute((ownership_takes)), whereas
1337 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001338
1339 if (!AL.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001340 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001341 << AL.getName()->getName() << 1 << AANT_ArgumentString;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001342 return;
1343 }
1344 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001345 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001346 switch (AL.getKind()) {
1347 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001348 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001349 if (AL.getNumArgs() < 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001350 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1351 << AL.getName() << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001352 return;
1353 }
Jordy Rose2a479922010-08-12 08:54:03 +00001354 break;
1355 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001356 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001357 if (AL.getNumArgs() < 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001358 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1359 << AL.getName() << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001360 return;
1361 }
Jordy Rose2a479922010-08-12 08:54:03 +00001362 break;
1363 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001364 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001365 if (AL.getNumArgs() > 1) {
1366 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001367 << AL.getName() << AL.getNumArgs() + 1;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001368 return;
1369 }
Jordy Rose2a479922010-08-12 08:54:03 +00001370 break;
1371 default:
1372 // This should never happen given how we are called.
1373 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001374 }
1375
Chandler Carruth87c44602011-07-01 23:49:12 +00001376 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001377 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1378 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001379 return;
1380 }
1381
Chris Lattner5f9e2722011-07-23 10:55:15 +00001382 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001383
1384 // Normalize the argument, __foo__ becomes foo.
1385 if (Module.startswith("__") && Module.endswith("__"))
1386 Module = Module.substr(2, Module.size() - 4);
1387
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001388
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001389 SmallVector<unsigned, 8> OwnershipArgs;
1390 for (unsigned i = 0; i < AL.getNumArgs(); ++i) {
1391 Expr *Ex = AL.getArg(i);
1392 uint64_t Idx;
1393 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
1394 AL.getLoc(), i + 1, Ex, Idx))
1395 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001396
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001397 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001398 case OwnershipAttr::Takes:
1399 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001400 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001401 QualType T = getFunctionOrMethodArgType(D, Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001402 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1403 // FIXME: Should also highlight argument in decl.
1404 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001405 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001406 << "pointer"
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001407 << Ex->getSourceRange();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001408 continue;
1409 }
1410 break;
1411 }
Sean Huntcf807c42010-08-18 23:23:40 +00001412 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001413 if (AL.getNumArgs() > 1) {
1414 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001415 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001416 llvm::APSInt ArgNum(32);
1417 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1418 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1419 S.Diag(AL.getLoc(), diag::err_ownership_type)
1420 << "ownership_returns" << "integer"
1421 << IdxExpr->getSourceRange();
1422 return;
1423 }
1424 }
1425 break;
1426 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001427 } // switch
1428
1429 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001430 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001431 i = D->specific_attr_begin<OwnershipAttr>(),
1432 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001433 i != e; ++i) {
1434 if ((*i)->getOwnKind() != K) {
1435 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1436 I!=E; ++I) {
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001437 if (Idx == *I) {
Sean Huntcf807c42010-08-18 23:23:40 +00001438 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1439 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001440 }
1441 }
1442 }
1443 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001444 OwnershipArgs.push_back(Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001445 }
1446
1447 unsigned* start = OwnershipArgs.data();
1448 unsigned size = OwnershipArgs.size();
1449 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001450
1451 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001452 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1453 << AL.getName() << 2;
Sean Huntcf807c42010-08-18 23:23:40 +00001454 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001455 }
Sean Huntcf807c42010-08-18 23:23:40 +00001456
Michael Han51d8c522013-01-24 16:46:58 +00001457 D->addAttr(::new (S.Context)
1458 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1459 AL.getAttributeSpellingListIndex()));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001460}
1461
Chandler Carruth1b03c872011-07-02 00:01:44 +00001462static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001463 // Check the attribute arguments.
1464 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001465 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1466 << Attr.getName() << 1;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001467 return;
1468 }
1469
Chandler Carruth87c44602011-07-01 23:49:12 +00001470 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001471 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001472 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001473 return;
1474 }
1475
Chandler Carruth87c44602011-07-01 23:49:12 +00001476 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001477
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001478 // gcc rejects
1479 // class c {
1480 // static int a __attribute__((weakref ("v2")));
1481 // static int b() __attribute__((weakref ("f3")));
1482 // };
1483 // and ignores the attributes of
1484 // void f(void) {
1485 // static int a __attribute__((weakref ("v2")));
1486 // }
1487 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001488 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001489 if (!Ctx->isFileContext()) {
1490 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001491 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001492 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001493 }
1494
1495 // The GCC manual says
1496 //
1497 // At present, a declaration to which `weakref' is attached can only
1498 // be `static'.
1499 //
1500 // It also says
1501 //
1502 // Without a TARGET,
1503 // given as an argument to `weakref' or to `alias', `weakref' is
1504 // equivalent to `weak'.
1505 //
1506 // gcc 4.4.1 will accept
1507 // int a7 __attribute__((weakref));
1508 // as
1509 // int a7 __attribute__((weak));
1510 // This looks like a bug in gcc. We reject that for now. We should revisit
1511 // it if this behaviour is actually used.
1512
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001513 // GCC rejects
1514 // static ((alias ("y"), weakref)).
1515 // Should we? How to check that weakref is before or after alias?
1516
1517 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001518 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001519 Arg = Arg->IgnoreParenCasts();
1520 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1521
Douglas Gregor5cee1192011-07-27 05:40:30 +00001522 if (!Str || !Str->isAscii()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001523 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001524 << Attr.getName() << 1 << AANT_ArgumentString;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001525 return;
1526 }
1527 // GCC will accept anything as the argument of weakref. Should we
1528 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001529 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001530 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001531 }
1532
Michael Han51d8c522013-01-24 16:46:58 +00001533 D->addAttr(::new (S.Context)
1534 WeakRefAttr(Attr.getRange(), S.Context,
1535 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001536}
1537
Chandler Carruth1b03c872011-07-02 00:01:44 +00001538static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001539 // check the attribute arguments.
Aaron Ballmanffa9d572013-07-18 18:01:48 +00001540 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001541 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001542
Peter Collingbourne7a730022010-11-23 20:45:58 +00001543 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001544 Arg = Arg->IgnoreParenCasts();
1545 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001546
Douglas Gregor5cee1192011-07-27 05:40:30 +00001547 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001548 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1549 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001550 return;
1551 }
Mike Stumpbf916502009-07-24 19:02:52 +00001552
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001553 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001554 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1555 return;
1556 }
1557
Chris Lattner6b6b5372008-06-26 18:38:35 +00001558 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001559
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001560 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00001561 Str->getString(),
1562 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001563}
1564
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001565static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1566 // Check the attribute arguments.
1567 if (!checkAttributeNumArgs(S, Attr, 0))
1568 return;
1569
1570 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1571 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1572 << Attr.getName() << ExpectedFunctionOrMethod;
1573 return;
1574 }
1575
Michael Han51d8c522013-01-24 16:46:58 +00001576 D->addAttr(::new (S.Context)
1577 MinSizeAttr(Attr.getRange(), S.Context,
1578 Attr.getAttributeSpellingListIndex()));
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001579}
1580
Benjamin Krameree409a92012-05-12 21:10:52 +00001581static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1582 // Check the attribute arguments.
1583 if (!checkAttributeNumArgs(S, Attr, 0))
1584 return;
1585
1586 if (!isa<FunctionDecl>(D)) {
1587 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1588 << Attr.getName() << ExpectedFunction;
1589 return;
1590 }
1591
1592 if (D->hasAttr<HotAttr>()) {
1593 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1594 << Attr.getName() << "hot";
1595 return;
1596 }
1597
Michael Han51d8c522013-01-24 16:46:58 +00001598 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1599 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001600}
1601
1602static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1603 // Check the attribute arguments.
1604 if (!checkAttributeNumArgs(S, Attr, 0))
1605 return;
1606
1607 if (!isa<FunctionDecl>(D)) {
1608 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1609 << Attr.getName() << ExpectedFunction;
1610 return;
1611 }
1612
1613 if (D->hasAttr<ColdAttr>()) {
1614 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1615 << Attr.getName() << "cold";
1616 return;
1617 }
1618
Michael Han51d8c522013-01-24 16:46:58 +00001619 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1620 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001621}
1622
Chandler Carruth1b03c872011-07-02 00:01:44 +00001623static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001624 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001625 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001626 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001627
Chandler Carruth87c44602011-07-01 23:49:12 +00001628 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001629 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001630 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001631 return;
1632 }
1633
Michael Han51d8c522013-01-24 16:46:58 +00001634 D->addAttr(::new (S.Context)
1635 NakedAttr(Attr.getRange(), S.Context,
1636 Attr.getAttributeSpellingListIndex()));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001637}
1638
Chandler Carruth1b03c872011-07-02 00:01:44 +00001639static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1640 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001641 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001642 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001643 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1644 << Attr.getName() << 0;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001645 return;
1646 }
1647
Chandler Carruth87c44602011-07-01 23:49:12 +00001648 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001649 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001650 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001651 return;
1652 }
Mike Stumpbf916502009-07-24 19:02:52 +00001653
Michael Han51d8c522013-01-24 16:46:58 +00001654 D->addAttr(::new (S.Context)
1655 AlwaysInlineAttr(Attr.getRange(), S.Context,
1656 Attr.getAttributeSpellingListIndex()));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001657}
1658
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001659static void handleTLSModelAttr(Sema &S, Decl *D,
1660 const AttributeList &Attr) {
1661 // Check the attribute arguments.
Aaron Ballmanffa9d572013-07-18 18:01:48 +00001662 if (!checkAttributeNumArgs(S, Attr, 1))
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001663 return;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001664
1665 Expr *Arg = Attr.getArg(0);
1666 Arg = Arg->IgnoreParenCasts();
1667 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1668
1669 // Check that it is a string.
1670 if (!Str) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001671 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1672 << Attr.getName() << AANT_ArgumentString;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001673 return;
1674 }
1675
Richard Smith38afbc72013-04-13 02:43:54 +00001676 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001677 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1678 << Attr.getName() << ExpectedTLSVar;
1679 return;
1680 }
1681
1682 // Check that the value.
1683 StringRef Model = Str->getString();
1684 if (Model != "global-dynamic" && Model != "local-dynamic"
1685 && Model != "initial-exec" && Model != "local-exec") {
1686 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1687 return;
1688 }
1689
Michael Han51d8c522013-01-24 16:46:58 +00001690 D->addAttr(::new (S.Context)
1691 TLSModelAttr(Attr.getRange(), S.Context, Model,
1692 Attr.getAttributeSpellingListIndex()));
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001693}
1694
Chandler Carruth1b03c872011-07-02 00:01:44 +00001695static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001696 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001697 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001698 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1699 << Attr.getName() << 0;
Ryan Flynn76168e22009-08-09 20:07:29 +00001700 return;
1701 }
Mike Stump1eb44332009-09-09 15:08:12 +00001702
Chandler Carruth87c44602011-07-01 23:49:12 +00001703 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001704 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001705 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han51d8c522013-01-24 16:46:58 +00001706 D->addAttr(::new (S.Context)
1707 MallocAttr(Attr.getRange(), S.Context,
1708 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001709 return;
1710 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001711 }
1712
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001713 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001714}
1715
Chandler Carruth1b03c872011-07-02 00:01:44 +00001716static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001717 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001718 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001719 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001720
Michael Han51d8c522013-01-24 16:46:58 +00001721 D->addAttr(::new (S.Context)
1722 MayAliasAttr(Attr.getRange(), S.Context,
1723 Attr.getAttributeSpellingListIndex()));
Dan Gohman34c26302010-11-17 00:03:07 +00001724}
1725
Chandler Carruth1b03c872011-07-02 00:01:44 +00001726static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001727 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001728 D->addAttr(::new (S.Context)
1729 NoCommonAttr(Attr.getRange(), S.Context,
1730 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001731 else
1732 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001733 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001734}
1735
Chandler Carruth1b03c872011-07-02 00:01:44 +00001736static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman3e1aca22013-06-20 22:55:04 +00001737 if (S.LangOpts.CPlusPlus) {
1738 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1739 return;
1740 }
1741
Chandler Carruth87c44602011-07-01 23:49:12 +00001742 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001743 D->addAttr(::new (S.Context)
1744 CommonAttr(Attr.getRange(), S.Context,
1745 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001746 else
1747 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001748 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001749}
1750
Chandler Carruth1b03c872011-07-02 00:01:44 +00001751static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001752 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001753
1754 if (S.CheckNoReturnAttr(attr)) return;
1755
Chandler Carruth87c44602011-07-01 23:49:12 +00001756 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001757 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001758 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001759 return;
1760 }
1761
Michael Han51d8c522013-01-24 16:46:58 +00001762 D->addAttr(::new (S.Context)
1763 NoReturnAttr(attr.getRange(), S.Context,
1764 attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00001765}
1766
1767bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001768 if (attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001769 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1770 << attr.getName() << 0;
John McCall711c52b2011-01-05 12:14:39 +00001771 attr.setInvalid();
1772 return true;
1773 }
1774
1775 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001776}
1777
Chandler Carruth1b03c872011-07-02 00:01:44 +00001778static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1779 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001780
1781 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1782 // because 'analyzer_noreturn' does not impact the type.
1783
Chandler Carruth1731e202011-07-11 23:30:35 +00001784 if(!checkAttributeNumArgs(S, Attr, 0))
1785 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001786
Chandler Carruth87c44602011-07-01 23:49:12 +00001787 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1788 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001789 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1790 && !VD->getType()->isFunctionPointerType())) {
1791 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001792 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001793 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001794 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001795 return;
1796 }
1797 }
1798
Michael Han51d8c522013-01-24 16:46:58 +00001799 D->addAttr(::new (S.Context)
1800 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1801 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001802}
1803
Richard Smithcd8ab512013-01-17 01:30:42 +00001804static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1805 const AttributeList &Attr) {
1806 // C++11 [dcl.attr.noreturn]p1:
1807 // The attribute may be applied to the declarator-id in a function
1808 // declaration.
1809 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1810 if (!FD) {
1811 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1812 << Attr.getName() << ExpectedFunctionOrMethod;
1813 return;
1814 }
1815
Michael Han51d8c522013-01-24 16:46:58 +00001816 D->addAttr(::new (S.Context)
1817 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1818 Attr.getAttributeSpellingListIndex()));
Richard Smithcd8ab512013-01-17 01:30:42 +00001819}
1820
John Thompson35cc9622010-08-09 21:53:52 +00001821// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001822static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001823/*
1824 Returning a Vector Class in Registers
1825
Eric Christopherf48f3672010-12-01 22:13:54 +00001826 According to the PPU ABI specifications, a class with a single member of
1827 vector type is returned in memory when used as the return value of a function.
1828 This results in inefficient code when implementing vector classes. To return
1829 the value in a single vector register, add the vecreturn attribute to the
1830 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001831
1832 Example:
1833
1834 struct Vector
1835 {
1836 __vector float xyzw;
1837 } __attribute__((vecreturn));
1838
1839 Vector Add(Vector lhs, Vector rhs)
1840 {
1841 Vector result;
1842 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1843 return result; // This will be returned in a register
1844 }
1845*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001846 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001847 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001848 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001849 return;
1850 }
1851
Chandler Carruth87c44602011-07-01 23:49:12 +00001852 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001853 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1854 return;
1855 }
1856
Chandler Carruth87c44602011-07-01 23:49:12 +00001857 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001858 int count = 0;
1859
1860 if (!isa<CXXRecordDecl>(record)) {
1861 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1862 return;
1863 }
1864
1865 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1866 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1867 return;
1868 }
1869
Eric Christopherf48f3672010-12-01 22:13:54 +00001870 for (RecordDecl::field_iterator iter = record->field_begin();
1871 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001872 if ((count == 1) || !iter->getType()->isVectorType()) {
1873 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1874 return;
1875 }
1876 count++;
1877 }
1878
Michael Han51d8c522013-01-24 16:46:58 +00001879 D->addAttr(::new (S.Context)
1880 VecReturnAttr(Attr.getRange(), S.Context,
1881 Attr.getAttributeSpellingListIndex()));
John Thompson35cc9622010-08-09 21:53:52 +00001882}
1883
Richard Smith3a2b7a12013-01-28 22:42:45 +00001884static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1885 const AttributeList &Attr) {
1886 if (isa<ParmVarDecl>(D)) {
1887 // [[carries_dependency]] can only be applied to a parameter if it is a
1888 // parameter of a function declaration or lambda.
1889 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1890 S.Diag(Attr.getLoc(),
1891 diag::err_carries_dependency_param_not_function_decl);
1892 return;
1893 }
1894 } else if (!isa<FunctionDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001895 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001896 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001897 return;
1898 }
Richard Smith3a2b7a12013-01-28 22:42:45 +00001899
1900 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1901 Attr.getRange(), S.Context,
1902 Attr.getAttributeSpellingListIndex()));
Sean Huntbbd37c62009-11-21 08:43:09 +00001903}
1904
Chandler Carruth1b03c872011-07-02 00:01:44 +00001905static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001906 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001907 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001908 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1909 << Attr.getName() << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001910 return;
1911 }
Mike Stumpbf916502009-07-24 19:02:52 +00001912
Chandler Carruth87c44602011-07-01 23:49:12 +00001913 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001914 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001915 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001916 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001917 return;
1918 }
Mike Stumpbf916502009-07-24 19:02:52 +00001919
Michael Han51d8c522013-01-24 16:46:58 +00001920 D->addAttr(::new (S.Context)
1921 UnusedAttr(Attr.getRange(), S.Context,
1922 Attr.getAttributeSpellingListIndex()));
Ted Kremenek73798892008-07-25 04:39:19 +00001923}
1924
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001925static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1926 const AttributeList &Attr) {
1927 // check the attribute arguments.
1928 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001929 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1930 << Attr.getName() << 0;
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001931 return;
1932 }
1933
1934 if (!isa<FunctionDecl>(D)) {
1935 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1936 << Attr.getName() << ExpectedFunction;
1937 return;
1938 }
1939
Michael Han51d8c522013-01-24 16:46:58 +00001940 D->addAttr(::new (S.Context)
1941 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1942 Attr.getAttributeSpellingListIndex()));
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001943}
1944
Chandler Carruth1b03c872011-07-02 00:01:44 +00001945static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001946 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001947 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001948 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1949 << Attr.getName() << 0;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001950 return;
1951 }
Mike Stumpbf916502009-07-24 19:02:52 +00001952
Chandler Carruth87c44602011-07-01 23:49:12 +00001953 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola29535ba2013-08-16 23:18:50 +00001954 if (VD->hasLocalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001955 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1956 return;
1957 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001958 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001959 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001960 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001961 return;
1962 }
Mike Stumpbf916502009-07-24 19:02:52 +00001963
Michael Han51d8c522013-01-24 16:46:58 +00001964 D->addAttr(::new (S.Context)
1965 UsedAttr(Attr.getRange(), S.Context,
1966 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001967}
1968
Chandler Carruth1b03c872011-07-02 00:01:44 +00001969static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001970 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001971 if (Attr.getNumArgs() > 1) {
1972 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001973 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001974 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001975
1976 int priority = 65535; // FIXME: Do not hardcode such constants.
1977 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001978 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001979 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001980 if (E->isTypeDependent() || E->isValueDependent() ||
1981 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001982 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001983 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00001984 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001985 return;
1986 }
1987 priority = Idx.getZExtValue();
1988 }
Mike Stumpbf916502009-07-24 19:02:52 +00001989
Chandler Carruth87c44602011-07-01 23:49:12 +00001990 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001991 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001992 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001993 return;
1994 }
1995
Michael Han51d8c522013-01-24 16:46:58 +00001996 D->addAttr(::new (S.Context)
1997 ConstructorAttr(Attr.getRange(), S.Context, priority,
1998 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001999}
2000
Chandler Carruth1b03c872011-07-02 00:01:44 +00002001static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002002 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00002003 if (Attr.getNumArgs() > 1) {
2004 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002005 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002006 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002007
2008 int priority = 65535; // FIXME: Do not hardcode such constants.
2009 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002010 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002011 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002012 if (E->isTypeDependent() || E->isValueDependent() ||
2013 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002014 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002015 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002016 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002017 return;
2018 }
2019 priority = Idx.getZExtValue();
2020 }
Mike Stumpbf916502009-07-24 19:02:52 +00002021
Chandler Carruth87c44602011-07-01 23:49:12 +00002022 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002023 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002024 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002025 return;
2026 }
2027
Michael Han51d8c522013-01-24 16:46:58 +00002028 D->addAttr(::new (S.Context)
2029 DestructorAttr(Attr.getRange(), S.Context, priority,
2030 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002031}
2032
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002033template <typename AttrTy>
Aaron Ballman2dbdef22013-07-18 13:13:52 +00002034static void handleAttrWithMessage(Sema &S, Decl *D,
2035 const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00002036 unsigned NumArgs = Attr.getNumArgs();
2037 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00002038 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002039 return;
2040 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002041
2042 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002043 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00002044 if (NumArgs == 1) {
2045 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002046 if (!SE) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002047 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_argument_type)
2048 << Attr.getName() << AANT_ArgumentString;
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002049 return;
2050 }
Chris Lattner951bbb22011-02-24 05:42:24 +00002051 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002052 }
Mike Stumpbf916502009-07-24 19:02:52 +00002053
Michael Han51d8c522013-01-24 16:46:58 +00002054 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2055 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00002056}
2057
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002058static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2059 const AttributeList &Attr) {
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002060 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002061 return;
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002062
Michael Han51d8c522013-01-24 16:46:58 +00002063 D->addAttr(::new (S.Context)
2064 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2065 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002066}
2067
Patrick Beardb2f68202012-04-06 18:12:22 +00002068static void handleObjCRootClassAttr(Sema &S, Decl *D,
2069 const AttributeList &Attr) {
2070 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002071 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2072 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardb2f68202012-04-06 18:12:22 +00002073 return;
2074 }
2075
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002076 if (!checkAttributeNumArgs(S, Attr, 0))
Patrick Beardb2f68202012-04-06 18:12:22 +00002077 return;
Patrick Beardb2f68202012-04-06 18:12:22 +00002078
Michael Han51d8c522013-01-24 16:46:58 +00002079 D->addAttr(::new (S.Context)
2080 ObjCRootClassAttr(Attr.getRange(), S.Context,
2081 Attr.getAttributeSpellingListIndex()));
Patrick Beardb2f68202012-04-06 18:12:22 +00002082}
2083
Michael Han51d8c522013-01-24 16:46:58 +00002084static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2085 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00002086 if (!isa<ObjCInterfaceDecl>(D)) {
2087 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2088 return;
2089 }
2090
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002091 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002092 return;
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002093
Michael Han51d8c522013-01-24 16:46:58 +00002094 D->addAttr(::new (S.Context)
2095 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2096 Attr.getAttributeSpellingListIndex()));
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002097}
2098
Jordy Rosefad5de92012-05-08 03:27:22 +00002099static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2100 IdentifierInfo *Platform,
2101 VersionTuple Introduced,
2102 VersionTuple Deprecated,
2103 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002104 StringRef PlatformName
2105 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2106 if (PlatformName.empty())
2107 PlatformName = Platform->getName();
2108
2109 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2110 // of these steps are needed).
2111 if (!Introduced.empty() && !Deprecated.empty() &&
2112 !(Introduced <= Deprecated)) {
2113 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2114 << 1 << PlatformName << Deprecated.getAsString()
2115 << 0 << Introduced.getAsString();
2116 return true;
2117 }
2118
2119 if (!Introduced.empty() && !Obsoleted.empty() &&
2120 !(Introduced <= Obsoleted)) {
2121 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2122 << 2 << PlatformName << Obsoleted.getAsString()
2123 << 0 << Introduced.getAsString();
2124 return true;
2125 }
2126
2127 if (!Deprecated.empty() && !Obsoleted.empty() &&
2128 !(Deprecated <= Obsoleted)) {
2129 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2130 << 2 << PlatformName << Obsoleted.getAsString()
2131 << 1 << Deprecated.getAsString();
2132 return true;
2133 }
2134
2135 return false;
2136}
2137
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002138/// \brief Check whether the two versions match.
2139///
2140/// If either version tuple is empty, then they are assumed to match. If
2141/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2142static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2143 bool BeforeIsOkay) {
2144 if (X.empty() || Y.empty())
2145 return true;
2146
2147 if (X == Y)
2148 return true;
2149
2150 if (BeforeIsOkay && X < Y)
2151 return true;
2152
2153 return false;
2154}
2155
Rafael Espindola51be6e32013-01-08 22:04:34 +00002156AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002157 IdentifierInfo *Platform,
2158 VersionTuple Introduced,
2159 VersionTuple Deprecated,
2160 VersionTuple Obsoleted,
2161 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002162 StringRef Message,
Michael Han51d8c522013-01-24 16:46:58 +00002163 bool Override,
2164 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002165 VersionTuple MergedIntroduced = Introduced;
2166 VersionTuple MergedDeprecated = Deprecated;
2167 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002168 bool FoundAny = false;
2169
Rafael Espindola98ae8342012-05-10 02:50:16 +00002170 if (D->hasAttrs()) {
2171 AttrVec &Attrs = D->getAttrs();
2172 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2173 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2174 if (!OldAA) {
2175 ++i;
2176 continue;
2177 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002178
Rafael Espindola98ae8342012-05-10 02:50:16 +00002179 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2180 if (OldPlatform != Platform) {
2181 ++i;
2182 continue;
2183 }
2184
2185 FoundAny = true;
2186 VersionTuple OldIntroduced = OldAA->getIntroduced();
2187 VersionTuple OldDeprecated = OldAA->getDeprecated();
2188 VersionTuple OldObsoleted = OldAA->getObsoleted();
2189 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002190
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002191 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2192 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2193 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2194 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002195 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002196 if (Override) {
2197 int Which = -1;
2198 VersionTuple FirstVersion;
2199 VersionTuple SecondVersion;
2200 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2201 Which = 0;
2202 FirstVersion = OldIntroduced;
2203 SecondVersion = Introduced;
2204 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2205 Which = 1;
2206 FirstVersion = Deprecated;
2207 SecondVersion = OldDeprecated;
2208 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2209 Which = 2;
2210 FirstVersion = Obsoleted;
2211 SecondVersion = OldObsoleted;
2212 }
2213
2214 if (Which == -1) {
2215 Diag(OldAA->getLocation(),
2216 diag::warn_mismatched_availability_override_unavail)
2217 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2218 } else {
2219 Diag(OldAA->getLocation(),
2220 diag::warn_mismatched_availability_override)
2221 << Which
2222 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2223 << FirstVersion.getAsString() << SecondVersion.getAsString();
2224 }
2225 Diag(Range.getBegin(), diag::note_overridden_method);
2226 } else {
2227 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2228 Diag(Range.getBegin(), diag::note_previous_attribute);
2229 }
2230
Rafael Espindola98ae8342012-05-10 02:50:16 +00002231 Attrs.erase(Attrs.begin() + i);
2232 --e;
2233 continue;
2234 }
2235
2236 VersionTuple MergedIntroduced2 = MergedIntroduced;
2237 VersionTuple MergedDeprecated2 = MergedDeprecated;
2238 VersionTuple MergedObsoleted2 = MergedObsoleted;
2239
2240 if (MergedIntroduced2.empty())
2241 MergedIntroduced2 = OldIntroduced;
2242 if (MergedDeprecated2.empty())
2243 MergedDeprecated2 = OldDeprecated;
2244 if (MergedObsoleted2.empty())
2245 MergedObsoleted2 = OldObsoleted;
2246
2247 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2248 MergedIntroduced2, MergedDeprecated2,
2249 MergedObsoleted2)) {
2250 Attrs.erase(Attrs.begin() + i);
2251 --e;
2252 continue;
2253 }
2254
2255 MergedIntroduced = MergedIntroduced2;
2256 MergedDeprecated = MergedDeprecated2;
2257 MergedObsoleted = MergedObsoleted2;
2258 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002259 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002260 }
2261
2262 if (FoundAny &&
2263 MergedIntroduced == Introduced &&
2264 MergedDeprecated == Deprecated &&
2265 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002266 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002267
Ted Kremenekcb344392013-04-06 00:34:27 +00002268 // Only create a new attribute if !Override, but we want to do
2269 // the checking.
Rafael Espindola98ae8342012-05-10 02:50:16 +00002270 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekcb344392013-04-06 00:34:27 +00002271 MergedDeprecated, MergedObsoleted) &&
2272 !Override) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002273 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2274 Introduced, Deprecated,
Michael Han51d8c522013-01-24 16:46:58 +00002275 Obsoleted, IsUnavailable, Message,
2276 AttrSpellingListIndex);
Rafael Espindola3b294362012-05-06 19:56:25 +00002277 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002278 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002279}
2280
Chandler Carruth1b03c872011-07-02 00:01:44 +00002281static void handleAvailabilityAttr(Sema &S, Decl *D,
2282 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002283 IdentifierInfo *Platform = Attr.getParameterName();
2284 SourceLocation PlatformLoc = Attr.getParameterLoc();
Michael Han51d8c522013-01-24 16:46:58 +00002285 unsigned Index = Attr.getAttributeSpellingListIndex();
2286
Rafael Espindola3b294362012-05-06 19:56:25 +00002287 if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002288 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
2289 << Platform;
2290
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002291 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2292 if (!ND) {
2293 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2294 return;
2295 }
2296
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002297 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2298 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2299 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002300 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002301 StringRef Str;
2302 const StringLiteral *SE =
2303 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2304 if (SE)
2305 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002306
Rafael Espindola51be6e32013-01-08 22:04:34 +00002307 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(),
Rafael Espindola599f1b72012-05-13 03:25:18 +00002308 Platform,
2309 Introduced.Version,
2310 Deprecated.Version,
2311 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002312 IsUnavailable, Str,
Michael Han51d8c522013-01-24 16:46:58 +00002313 /*Override=*/false,
2314 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002315 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002316 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002317}
2318
John McCalld4c3d662013-02-20 01:54:26 +00002319template <class T>
2320static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2321 typename T::VisibilityType value,
2322 unsigned attrSpellingListIndex) {
2323 T *existingAttr = D->getAttr<T>();
2324 if (existingAttr) {
2325 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2326 if (existingValue == value)
2327 return NULL;
2328 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2329 S.Diag(range.getBegin(), diag::note_previous_attribute);
2330 D->dropAttr<T>();
2331 }
2332 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2333}
2334
Rafael Espindola599f1b72012-05-13 03:25:18 +00002335VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002336 VisibilityAttr::VisibilityType Vis,
2337 unsigned AttrSpellingListIndex) {
John McCalld4c3d662013-02-20 01:54:26 +00002338 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2339 AttrSpellingListIndex);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002340}
2341
John McCalld4c3d662013-02-20 01:54:26 +00002342TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2343 TypeVisibilityAttr::VisibilityType Vis,
2344 unsigned AttrSpellingListIndex) {
2345 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2346 AttrSpellingListIndex);
2347}
2348
2349static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2350 bool isTypeVisibility) {
2351 // Visibility attributes don't mean anything on a typedef.
2352 if (isa<TypedefNameDecl>(D)) {
2353 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2354 << Attr.getName();
2355 return;
2356 }
2357
2358 // 'type_visibility' can only go on a type or namespace.
2359 if (isTypeVisibility &&
2360 !(isa<TagDecl>(D) ||
2361 isa<ObjCInterfaceDecl>(D) ||
2362 isa<NamespaceDecl>(D))) {
2363 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2364 << Attr.getName() << ExpectedTypeOrNamespace;
2365 return;
2366 }
2367
Chris Lattner6b6b5372008-06-26 18:38:35 +00002368 // check the attribute arguments.
John McCalld4c3d662013-02-20 01:54:26 +00002369 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002370 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002371
Peter Collingbourne7a730022010-11-23 20:45:58 +00002372 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002373 Arg = Arg->IgnoreParenCasts();
2374 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002375
Douglas Gregor5cee1192011-07-27 05:40:30 +00002376 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002377 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2378 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002379 return;
2380 }
Mike Stumpbf916502009-07-24 19:02:52 +00002381
Chris Lattner5f9e2722011-07-23 10:55:15 +00002382 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002383 VisibilityAttr::VisibilityType type;
Michael Han51d8c522013-01-24 16:46:58 +00002384
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002385 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002386 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002387 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002388 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002389 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002390 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002391 else if (TypeStr == "protected") {
2392 // Complain about attempts to use protected visibility on targets
2393 // (like Darwin) that don't support it.
2394 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2395 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2396 type = VisibilityAttr::Default;
2397 } else {
2398 type = VisibilityAttr::Protected;
2399 }
2400 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002401 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002402 return;
2403 }
Mike Stumpbf916502009-07-24 19:02:52 +00002404
Michael Han51d8c522013-01-24 16:46:58 +00002405 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld4c3d662013-02-20 01:54:26 +00002406 clang::Attr *newAttr;
2407 if (isTypeVisibility) {
2408 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2409 (TypeVisibilityAttr::VisibilityType) type,
2410 Index);
2411 } else {
2412 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2413 }
2414 if (newAttr)
2415 D->addAttr(newAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002416}
2417
Chandler Carruth1b03c872011-07-02 00:01:44 +00002418static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2419 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002420 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2421 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002422 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002423 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002424 return;
2425 }
2426
Chandler Carruth87c44602011-07-01 23:49:12 +00002427 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
2428 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002429 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002430 << Attr.getName() << 1 << AANT_ArgumentString;
John McCalld5313b02011-03-02 11:33:24 +00002431 } else {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002432 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2433 << Attr.getName() << 0;
John McCalld5313b02011-03-02 11:33:24 +00002434 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002435 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00002436 return;
2437 }
2438
Chris Lattner5f9e2722011-07-23 10:55:15 +00002439 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00002440 ObjCMethodFamilyAttr::FamilyKind family;
2441 if (param == "none")
2442 family = ObjCMethodFamilyAttr::OMF_None;
2443 else if (param == "alloc")
2444 family = ObjCMethodFamilyAttr::OMF_alloc;
2445 else if (param == "copy")
2446 family = ObjCMethodFamilyAttr::OMF_copy;
2447 else if (param == "init")
2448 family = ObjCMethodFamilyAttr::OMF_init;
2449 else if (param == "mutableCopy")
2450 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2451 else if (param == "new")
2452 family = ObjCMethodFamilyAttr::OMF_new;
2453 else {
2454 // Just warn and ignore it. This is future-proof against new
2455 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00002456 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002457 return;
2458 }
2459
John McCallf85e1932011-06-15 23:02:42 +00002460 if (family == ObjCMethodFamilyAttr::OMF_init &&
2461 !method->getResultType()->isObjCObjectPointerType()) {
2462 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2463 << method->getResultType();
2464 // Ignore the attribute.
2465 return;
2466 }
2467
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002468 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002469 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002470}
2471
Chandler Carruth1b03c872011-07-02 00:01:44 +00002472static void handleObjCExceptionAttr(Sema &S, Decl *D,
2473 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002474 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002475 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002476
Chris Lattner0db29ec2009-02-14 08:09:34 +00002477 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2478 if (OCI == 0) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002479 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2480 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002481 return;
2482 }
Mike Stumpbf916502009-07-24 19:02:52 +00002483
Michael Han51d8c522013-01-24 16:46:58 +00002484 D->addAttr(::new (S.Context)
2485 ObjCExceptionAttr(Attr.getRange(), S.Context,
2486 Attr.getAttributeSpellingListIndex()));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002487}
2488
Chandler Carruth1b03c872011-07-02 00:01:44 +00002489static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman081c8832013-07-23 12:13:14 +00002490 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002491 return;
Richard Smith162e1c12011-04-15 14:24:37 +00002492 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002493 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002494 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002495 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2496 return;
2497 }
2498 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002499 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2500 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002501 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002502 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2503 return;
2504 }
2505 }
2506 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002507 // It is okay to include this attribute on properties, e.g.:
2508 //
2509 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2510 //
2511 // In this case it follows tradition and suppresses an error in the above
2512 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002513 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002514 }
Michael Han51d8c522013-01-24 16:46:58 +00002515 D->addAttr(::new (S.Context)
2516 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2517 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002518}
2519
Mike Stumpbf916502009-07-24 19:02:52 +00002520static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002521handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman081c8832013-07-23 12:13:14 +00002522 if (!checkAttributeNumArgs(S, Attr, 0))
Douglas Gregorf9201e02009-02-11 23:02:49 +00002523 return;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002524
2525 if (!isa<FunctionDecl>(D)) {
2526 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2527 return;
2528 }
2529
Michael Han51d8c522013-01-24 16:46:58 +00002530 D->addAttr(::new (S.Context)
2531 OverloadableAttr(Attr.getRange(), S.Context,
2532 Attr.getAttributeSpellingListIndex()));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002533}
2534
Chandler Carruth1b03c872011-07-02 00:01:44 +00002535static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002536 if (!Attr.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002537 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002538 << Attr.getName() << 1 << AANT_ArgumentString;
Steve Naroff9eae5762008-09-18 16:44:58 +00002539 return;
2540 }
Mike Stumpbf916502009-07-24 19:02:52 +00002541
Steve Naroff9eae5762008-09-18 16:44:58 +00002542 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002543 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2544 << Attr.getName() << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00002545 return;
2546 }
Mike Stumpbf916502009-07-24 19:02:52 +00002547
Sean Huntcf807c42010-08-18 23:23:40 +00002548 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00002549 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002550 type = BlocksAttr::ByRef;
2551 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002552 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00002553 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00002554 return;
2555 }
Mike Stumpbf916502009-07-24 19:02:52 +00002556
Michael Han51d8c522013-01-24 16:46:58 +00002557 D->addAttr(::new (S.Context)
2558 BlocksAttr(Attr.getRange(), S.Context, type,
2559 Attr.getAttributeSpellingListIndex()));
Steve Naroff9eae5762008-09-18 16:44:58 +00002560}
2561
Chandler Carruth1b03c872011-07-02 00:01:44 +00002562static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002563 // check the attribute arguments.
2564 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002565 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002566 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002567 }
2568
John McCall3323fad2011-09-09 07:56:05 +00002569 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002570 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002571 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002572 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002573 if (E->isTypeDependent() || E->isValueDependent() ||
2574 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002575 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002576 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002577 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002578 return;
2579 }
Mike Stumpbf916502009-07-24 19:02:52 +00002580
John McCall3323fad2011-09-09 07:56:05 +00002581 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002582 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2583 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002584 return;
2585 }
John McCall3323fad2011-09-09 07:56:05 +00002586
2587 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002588 }
2589
John McCall3323fad2011-09-09 07:56:05 +00002590 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002591 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002592 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002593 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002594 if (E->isTypeDependent() || E->isValueDependent() ||
2595 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002596 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002597 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002598 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002599 return;
2600 }
2601 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002602
John McCall3323fad2011-09-09 07:56:05 +00002603 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002604 // FIXME: This error message could be improved, it would be nice
2605 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002606 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2607 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002608 return;
2609 }
2610 }
2611
Chandler Carruth87c44602011-07-01 23:49:12 +00002612 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002613 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002614 if (isa<FunctionNoProtoType>(FT)) {
2615 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2616 return;
2617 }
Mike Stumpbf916502009-07-24 19:02:52 +00002618
Chris Lattner897cd902009-03-17 23:03:47 +00002619 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002620 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002621 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002622 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002623 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002624 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002625 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002626 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002627 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002628 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2629 if (!BD->isVariadic()) {
2630 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2631 return;
2632 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002633 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002634 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002635 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002636 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002637 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002638 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002639 int m = Ty->isFunctionPointerType() ? 0 : 1;
2640 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002641 return;
2642 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002643 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002644 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002645 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002646 return;
2647 }
Anders Carlsson77091822008-10-05 18:05:59 +00002648 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002649 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002650 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002651 return;
2652 }
Michael Han51d8c522013-01-24 16:46:58 +00002653 D->addAttr(::new (S.Context)
2654 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2655 Attr.getAttributeSpellingListIndex()));
Anders Carlsson77091822008-10-05 18:05:59 +00002656}
2657
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002658static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2659 // Check the attribute arguments.
2660 if (!checkAttributeNumArgs(S, Attr, 0))
2661 return;
2662
2663 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2664 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2665 else
2666 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2667}
2668
Chandler Carruth1b03c872011-07-02 00:01:44 +00002669static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002670 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002671 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002672 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002673
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002674 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002675 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002676 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002677 return;
2678 }
Mike Stumpbf916502009-07-24 19:02:52 +00002679
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002680 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2681 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2682 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002683 return;
2684 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002685 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2686 if (MD->getResultType()->isVoidType()) {
2687 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2688 << Attr.getName() << 1;
2689 return;
2690 }
2691
Michael Han51d8c522013-01-24 16:46:58 +00002692 D->addAttr(::new (S.Context)
2693 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2694 Attr.getAttributeSpellingListIndex()));
Chris Lattner026dc962009-02-14 07:37:35 +00002695}
2696
Chandler Carruth1b03c872011-07-02 00:01:44 +00002697static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002698 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00002699 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002700 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2701 << Attr.getName() << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002702 return;
2703 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002704
Chandler Carruth87c44602011-07-01 23:49:12 +00002705 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002706 if (isa<CXXRecordDecl>(D)) {
2707 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2708 return;
2709 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002710 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2711 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002712 return;
2713 }
2714
Chandler Carruth87c44602011-07-01 23:49:12 +00002715 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002716
Michael Han51d8c522013-01-24 16:46:58 +00002717 nd->addAttr(::new (S.Context)
2718 WeakAttr(Attr.getRange(), S.Context,
2719 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002720}
2721
Chandler Carruth1b03c872011-07-02 00:01:44 +00002722static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002723 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002724 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002725 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002726
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002727
2728 // weak_import only applies to variable & function declarations.
2729 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002730 if (!D->canBeWeakImported(isDef)) {
2731 if (isDef)
Reid Klecknerbeba3e82013-05-20 21:53:29 +00002732 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2733 << "weak_import";
Douglas Gregordef86312011-03-23 13:27:51 +00002734 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002735 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002736 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002737 // Nothing to warn about here.
2738 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002739 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002740 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002741
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002742 return;
2743 }
2744
Michael Han51d8c522013-01-24 16:46:58 +00002745 D->addAttr(::new (S.Context)
2746 WeakImportAttr(Attr.getRange(), S.Context,
2747 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002748}
2749
Tanya Lattner0df579e2012-07-09 22:06:01 +00002750// Handles reqd_work_group_size and work_group_size_hint.
2751static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002752 const AttributeList &Attr) {
Tanya Lattner0df579e2012-07-09 22:06:01 +00002753 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2754 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2755
Nate Begeman6f3d8382009-06-26 06:32:41 +00002756 // Attribute has 3 arguments.
Tanya Lattner0df579e2012-07-09 22:06:01 +00002757 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002758
2759 unsigned WGSize[3];
2760 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002761 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002762 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002763 if (E->isTypeDependent() || E->isValueDependent() ||
2764 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00002765 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2766 << Attr.getName() << AANT_ArgumentIntegerConstant
2767 << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002768 return;
2769 }
2770 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2771 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002772
2773 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2774 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2775 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2776 if (!(A->getXDim() == WGSize[0] &&
2777 A->getYDim() == WGSize[1] &&
2778 A->getZDim() == WGSize[2])) {
2779 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2780 Attr.getName();
2781 }
2782 }
2783
2784 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2785 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2786 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2787 if (!(A->getXDim() == WGSize[0] &&
2788 A->getYDim() == WGSize[1] &&
2789 A->getZDim() == WGSize[2])) {
2790 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2791 Attr.getName();
2792 }
2793 }
2794
2795 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2796 D->addAttr(::new (S.Context)
2797 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002798 WGSize[0], WGSize[1], WGSize[2],
2799 Attr.getAttributeSpellingListIndex()));
Tanya Lattner0df579e2012-07-09 22:06:01 +00002800 else
2801 D->addAttr(::new (S.Context)
2802 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002803 WGSize[0], WGSize[1], WGSize[2],
2804 Attr.getAttributeSpellingListIndex()));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002805}
2806
Joey Gouly37453b92013-03-08 09:42:32 +00002807static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2808 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2809
2810 // Attribute has 1 argument.
2811 if (!checkAttributeNumArgs(S, Attr, 1))
2812 return;
2813
2814 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg());
2815
2816 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2817 (ParmType->isBooleanType() ||
2818 !ParmType->isIntegralType(S.getASTContext()))) {
2819 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2820 << ParmType;
2821 return;
2822 }
2823
2824 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2825 D->hasAttr<VecTypeHintAttr>()) {
2826 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
2827 if (A->getTypeHint() != ParmType) {
2828 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2829 return;
2830 }
2831 }
2832
2833 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
2834 ParmType, Attr.getLoc()));
2835}
2836
Joey Gouly96cead52013-03-14 09:54:43 +00002837static void handleEndianAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2838 if (!dyn_cast<VarDecl>(D))
2839 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << "endian"
2840 << 9;
2841 StringRef EndianType = Attr.getParameterName()->getName();
2842 if (EndianType != "host" && EndianType != "device")
2843 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_endian) << EndianType;
2844}
2845
Rafael Espindola599f1b72012-05-13 03:25:18 +00002846SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002847 StringRef Name,
2848 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002849 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2850 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002851 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002852 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2853 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002854 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002855 }
Michael Han51d8c522013-01-24 16:46:58 +00002856 return ::new (Context) SectionAttr(Range, Context, Name,
2857 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002858}
2859
Chandler Carruth1b03c872011-07-02 00:01:44 +00002860static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002861 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002862 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002863 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002864
2865 // Make sure that there is a string literal as the sections's single
2866 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002867 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002868 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002869 if (!SE) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002870 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
2871 << Attr.getName() << AANT_ArgumentString;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002872 return;
2873 }
Mike Stump1eb44332009-09-09 15:08:12 +00002874
Chris Lattner797c3c42009-08-10 19:03:04 +00002875 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002876 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002877 if (!Error.empty()) {
2878 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2879 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002880 return;
2881 }
Mike Stump1eb44332009-09-09 15:08:12 +00002882
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002883 // This attribute cannot be applied to local variables.
2884 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2885 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2886 return;
2887 }
Michael Han51d8c522013-01-24 16:46:58 +00002888
2889 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindola599f1b72012-05-13 03:25:18 +00002890 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han51d8c522013-01-24 16:46:58 +00002891 SE->getString(), Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002892 if (NewAttr)
2893 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002894}
2895
Chris Lattner6b6b5372008-06-26 18:38:35 +00002896
Chandler Carruth1b03c872011-07-02 00:01:44 +00002897static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002898 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002899 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002900 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2901 << Attr.getName() << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002902 return;
2903 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002904
Chandler Carruth87c44602011-07-01 23:49:12 +00002905 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002906 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002907 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002908 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002909 D->addAttr(::new (S.Context)
2910 NoThrowAttr(Attr.getRange(), S.Context,
2911 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002912 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002913}
2914
Chandler Carruth1b03c872011-07-02 00:01:44 +00002915static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002916 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002917 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002918 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2919 << Attr.getName() << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002920 return;
2921 }
Mike Stumpbf916502009-07-24 19:02:52 +00002922
Chandler Carruth87c44602011-07-01 23:49:12 +00002923 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002924 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002925 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002926 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002927 D->addAttr(::new (S.Context)
2928 ConstAttr(Attr.getRange(), S.Context,
2929 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002930 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002931}
2932
Chandler Carruth1b03c872011-07-02 00:01:44 +00002933static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002934 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002935 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002936 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002937
Michael Han51d8c522013-01-24 16:46:58 +00002938 D->addAttr(::new (S.Context)
2939 PureAttr(Attr.getRange(), S.Context,
2940 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002941}
2942
Chandler Carruth1b03c872011-07-02 00:01:44 +00002943static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002944 if (!Attr.getParameterName()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002945 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2946 << Attr.getName() << 1;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002947 return;
2948 }
Mike Stumpbf916502009-07-24 19:02:52 +00002949
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002950 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002951 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2952 << Attr.getName() << 1;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002953 return;
2954 }
Mike Stumpbf916502009-07-24 19:02:52 +00002955
Chandler Carruth87c44602011-07-01 23:49:12 +00002956 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002957
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002958 if (!VD || !VD->hasLocalStorage()) {
2959 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2960 return;
2961 }
Mike Stumpbf916502009-07-24 19:02:52 +00002962
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002963 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002964 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002965 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002966 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2967 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002968 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002969 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002970 Attr.getParameterName();
2971 return;
2972 }
Mike Stumpbf916502009-07-24 19:02:52 +00002973
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002974 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2975 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002976 S.Diag(Attr.getParameterLoc(),
2977 diag::err_attribute_cleanup_arg_not_function)
2978 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002979 return;
2980 }
2981
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002982 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002983 S.Diag(Attr.getParameterLoc(),
2984 diag::err_attribute_cleanup_func_must_take_one_arg)
2985 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002986 return;
2987 }
Mike Stumpbf916502009-07-24 19:02:52 +00002988
Anders Carlsson89941c12009-02-07 23:16:50 +00002989 // We're currently more strict than GCC about what function types we accept.
2990 // If this ever proves to be a problem it should be easy to fix.
2991 QualType Ty = S.Context.getPointerType(VD->getType());
2992 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002993 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2994 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002995 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002996 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2997 Attr.getParameterName() << ParamTy << Ty;
2998 return;
2999 }
Mike Stumpbf916502009-07-24 19:02:52 +00003000
Michael Han51d8c522013-01-24 16:46:58 +00003001 D->addAttr(::new (S.Context)
3002 CleanupAttr(Attr.getRange(), S.Context, FD,
3003 Attr.getAttributeSpellingListIndex()));
Eli Friedman5f2987c2012-02-02 03:46:19 +00003004 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Nick Lewycky3c86a5c2013-02-12 08:08:54 +00003005 S.DiagnoseUseOfDecl(FD, Attr.getParameterLoc());
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003006}
3007
Mike Stumpbf916502009-07-24 19:02:52 +00003008/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003009/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003010static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00003011 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003012 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003013
Chandler Carruth87c44602011-07-01 23:49:12 +00003014 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003015 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003016 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003017 return;
3018 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003019
Peter Collingbourne7a730022010-11-23 20:45:58 +00003020 Expr *IdxExpr = Attr.getArg(0);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00003021 uint64_t ArgIdx;
3022 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
3023 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003024 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003025
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003026 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003027 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00003028
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003029 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
3030 if (not_nsstring_type &&
3031 !isCFStringType(Ty, S.Context) &&
3032 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003033 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003034 // FIXME: Should highlight the actual expression that has the wrong type.
3035 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00003036 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003037 << IdxExpr->getSourceRange();
3038 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003039 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003040 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003041 if (!isNSStringType(Ty, S.Context) &&
3042 !isCFStringType(Ty, S.Context) &&
3043 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003044 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003045 // FIXME: Should highlight the actual expression that has the wrong type.
3046 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00003047 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003048 << IdxExpr->getSourceRange();
3049 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003050 }
3051
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00003052 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
3053 // because that has corrected for the implicit this parameter, and is zero-
3054 // based. The attribute expects what the user wrote explicitly.
3055 llvm::APSInt Val;
3056 IdxExpr->EvaluateAsInt(Val, S.Context);
3057
Michael Han51d8c522013-01-24 16:46:58 +00003058 D->addAttr(::new (S.Context)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00003059 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003060 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003061}
3062
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003063enum FormatAttrKind {
3064 CFStringFormat,
3065 NSStringFormat,
3066 StrftimeFormat,
3067 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00003068 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003069 InvalidFormat
3070};
3071
3072/// getFormatAttrKind - Map from format attribute names to supported format
3073/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003074static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003075 return llvm::StringSwitch<FormatAttrKind>(Format)
3076 // Check for formats that get handled specially.
3077 .Case("NSString", NSStringFormat)
3078 .Case("CFString", CFStringFormat)
3079 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003080
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003081 // Otherwise, check for supported formats.
3082 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3083 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3084 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003085
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003086 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3087 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003088}
3089
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003090/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003091/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003092static void handleInitPriorityAttr(Sema &S, Decl *D,
3093 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003094 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003095 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3096 return;
3097 }
3098
Chandler Carruth87c44602011-07-01 23:49:12 +00003099 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003100 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3101 Attr.setInvalid();
3102 return;
3103 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003104 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003105 if (S.Context.getAsArrayType(T))
3106 T = S.Context.getBaseElementType(T);
3107 if (!T->getAs<RecordType>()) {
3108 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3109 Attr.setInvalid();
3110 return;
3111 }
3112
Aaron Ballmanffa9d572013-07-18 18:01:48 +00003113 if (!checkAttributeNumArgs(S, Attr, 1)) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003114 Attr.setInvalid();
3115 return;
3116 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00003117 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003118
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003119 llvm::APSInt priority(32);
3120 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3121 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003122 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3123 << Attr.getName() << AANT_ArgumentIntegerConstant
3124 << priorityExpr->getSourceRange();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003125 Attr.setInvalid();
3126 return;
3127 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00003128 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003129 if (prioritynum < 101 || prioritynum > 65535) {
3130 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3131 << priorityExpr->getSourceRange();
3132 Attr.setInvalid();
3133 return;
3134 }
Michael Han51d8c522013-01-24 16:46:58 +00003135 D->addAttr(::new (S.Context)
3136 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3137 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003138}
3139
Rafael Espindola599f1b72012-05-13 03:25:18 +00003140FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
Michael Han51d8c522013-01-24 16:46:58 +00003141 int FormatIdx, int FirstArg,
3142 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003143 // Check whether we already have an equivalent format attribute.
3144 for (specific_attr_iterator<FormatAttr>
3145 i = D->specific_attr_begin<FormatAttr>(),
3146 e = D->specific_attr_end<FormatAttr>();
3147 i != e ; ++i) {
3148 FormatAttr *f = *i;
3149 if (f->getType() == Format &&
3150 f->getFormatIdx() == FormatIdx &&
3151 f->getFirstArg() == FirstArg) {
3152 // If we don't have a valid location for this attribute, adopt the
3153 // location.
3154 if (f->getLocation().isInvalid())
3155 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00003156 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003157 }
3158 }
3159
Michael Han51d8c522013-01-24 16:46:58 +00003160 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg,
3161 AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003162}
3163
Mike Stumpbf916502009-07-24 19:02:52 +00003164/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003165/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003166static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003167
Chris Lattner545dd342008-06-28 23:36:30 +00003168 if (!Attr.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003169 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003170 << Attr.getName() << 1 << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003171 return;
3172 }
3173
Chris Lattner545dd342008-06-28 23:36:30 +00003174 if (Attr.getNumArgs() != 2) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003175 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3176 << Attr.getName() << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003177 return;
3178 }
3179
Chandler Carruth87c44602011-07-01 23:49:12 +00003180 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003181 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003182 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003183 return;
3184 }
3185
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003186 // In C++ the implicit 'this' function parameter also counts, and they are
3187 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003188 bool HasImplicitThisParam = isInstanceMethod(D);
3189 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003190 unsigned FirstIdx = 1;
3191
Chris Lattner5f9e2722011-07-23 10:55:15 +00003192 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003193
3194 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003195 if (Format.startswith("__") && Format.endswith("__"))
3196 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003197
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003198 // Check for supported formats.
3199 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003200
3201 if (Kind == IgnoredFormat)
3202 return;
3203
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003204 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003205 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003206 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003207 return;
3208 }
3209
3210 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003211 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00003212 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003213 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3214 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003215 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003216 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003217 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003218 return;
3219 }
3220
3221 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003222 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003223 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003224 return;
3225 }
3226
3227 // FIXME: Do we need to bounds check?
3228 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003229
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003230 if (HasImplicitThisParam) {
3231 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003232 S.Diag(Attr.getLoc(),
3233 diag::err_format_attribute_implicit_this_format_string)
3234 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003235 return;
3236 }
3237 ArgIdx--;
3238 }
Mike Stump1eb44332009-09-09 15:08:12 +00003239
Chris Lattner6b6b5372008-06-26 18:38:35 +00003240 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003241 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003242
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003243 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003244 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003245 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3246 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003247 return;
3248 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003249 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003250 // FIXME: do we need to check if the type is NSString*? What are the
3251 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003252 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003253 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003254 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3255 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003256 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003257 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003258 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003259 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003260 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003261 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3262 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003263 return;
3264 }
3265
3266 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00003267 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003268 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003269 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3270 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003271 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003272 << Attr.getName() << 3 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003273 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003274 return;
3275 }
3276
3277 // check if the function is variadic if the 3rd argument non-zero
3278 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003279 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003280 ++NumArgs; // +1 for ...
3281 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003282 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003283 return;
3284 }
3285 }
3286
Chris Lattner3c73c412008-11-19 08:23:25 +00003287 // strftime requires FirstArg to be 0 because it doesn't read from any
3288 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003289 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003290 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003291 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3292 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003293 return;
3294 }
3295 // if 0 it disables parameter checking (to use with e.g. va_list)
3296 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003297 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003298 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003299 return;
3300 }
3301
Rafael Espindola599f1b72012-05-13 03:25:18 +00003302 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3303 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003304 FirstArg.getZExtValue(),
3305 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003306 if (NewAttr)
3307 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003308}
3309
Chandler Carruth1b03c872011-07-02 00:01:44 +00003310static void handleTransparentUnionAttr(Sema &S, Decl *D,
3311 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003312 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003313 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003314 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003315
Chris Lattner6b6b5372008-06-26 18:38:35 +00003316
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003317 // Try to find the underlying union declaration.
3318 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003319 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003320 if (TD && TD->getUnderlyingType()->isUnionType())
3321 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3322 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003323 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003324
3325 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003326 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003327 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003328 return;
3329 }
3330
John McCall5e1cdac2011-10-07 06:10:15 +00003331 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003332 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003333 diag::warn_transparent_union_attribute_not_definition);
3334 return;
3335 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003336
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003337 RecordDecl::field_iterator Field = RD->field_begin(),
3338 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003339 if (Field == FieldEnd) {
3340 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3341 return;
3342 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003343
David Blaikie581deb32012-06-06 20:45:41 +00003344 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003345 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003346 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003347 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003348 diag::warn_transparent_union_attribute_floating)
3349 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003350 return;
3351 }
3352
3353 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3354 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3355 for (; Field != FieldEnd; ++Field) {
3356 QualType FieldType = Field->getType();
3357 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3358 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3359 // Warn if we drop the attribute.
3360 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003361 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003362 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003363 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003364 diag::warn_transparent_union_attribute_field_size_align)
3365 << isSize << Field->getDeclName() << FieldBits;
3366 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003367 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003368 diag::note_transparent_union_first_field_size_align)
3369 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003370 return;
3371 }
3372 }
3373
Michael Han51d8c522013-01-24 16:46:58 +00003374 RD->addAttr(::new (S.Context)
3375 TransparentUnionAttr(Attr.getRange(), S.Context,
3376 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003377}
3378
Chandler Carruth1b03c872011-07-02 00:01:44 +00003379static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003380 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003381 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003382 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003383
Peter Collingbourne7a730022010-11-23 20:45:58 +00003384 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003385 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003386
Chris Lattner6b6b5372008-06-26 18:38:35 +00003387 // Make sure that there is a string literal as the annotation's single
3388 // argument.
3389 if (!SE) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003390 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
3391 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003392 return;
3393 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003394
3395 // Don't duplicate annotations that are already set.
3396 for (specific_attr_iterator<AnnotateAttr>
3397 i = D->specific_attr_begin<AnnotateAttr>(),
3398 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3399 if ((*i)->getAnnotation() == SE->getString())
3400 return;
3401 }
Michael Han51d8c522013-01-24 16:46:58 +00003402
3403 D->addAttr(::new (S.Context)
3404 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3405 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003406}
3407
Chandler Carruth1b03c872011-07-02 00:01:44 +00003408static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003409 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003410 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003411 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3412 << Attr.getName() << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003413 return;
3414 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003415
Richard Smithbe507b62013-02-01 08:12:08 +00003416 if (Attr.getNumArgs() == 0) {
3417 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3418 true, 0, Attr.getAttributeSpellingListIndex()));
3419 return;
3420 }
3421
Richard Smithf6565a92013-02-22 08:32:16 +00003422 Expr *E = Attr.getArg(0);
3423 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3424 S.Diag(Attr.getEllipsisLoc(),
3425 diag::err_pack_expansion_without_parameter_packs);
3426 return;
3427 }
3428
3429 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3430 return;
3431
3432 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3433 Attr.isPackExpansion());
Richard Smithbe507b62013-02-01 08:12:08 +00003434}
3435
3436void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smithf6565a92013-02-22 08:32:16 +00003437 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smithbe507b62013-02-01 08:12:08 +00003438 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3439 SourceLocation AttrLoc = AttrRange.getBegin();
3440
Richard Smith4cd81c52013-01-29 09:02:09 +00003441 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smithbe507b62013-02-01 08:12:08 +00003442 if (TmpAttr.isAlignas()) {
Richard Smith4cd81c52013-01-29 09:02:09 +00003443 // C++11 [dcl.align]p1:
3444 // An alignment-specifier may be applied to a variable or to a class
3445 // data member, but it shall not be applied to a bit-field, a function
3446 // parameter, the formal parameter of a catch clause, or a variable
3447 // declared with the register storage class specifier. An
3448 // alignment-specifier may also be applied to the declaration of a class
3449 // or enumeration type.
3450 // C11 6.7.5/2:
3451 // An alignment attribute shall not be specified in a declaration of
3452 // a typedef, or a bit-field, or a function, or a parameter, or an
3453 // object declared with the register storage-class specifier.
3454 int DiagKind = -1;
3455 if (isa<ParmVarDecl>(D)) {
3456 DiagKind = 0;
3457 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3458 if (VD->getStorageClass() == SC_Register)
3459 DiagKind = 1;
3460 if (VD->isExceptionVariable())
3461 DiagKind = 2;
3462 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3463 if (FD->isBitField())
3464 DiagKind = 3;
3465 } else if (!isa<TagDecl>(D)) {
Richard Smithbe507b62013-02-01 08:12:08 +00003466 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3467 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith5f838aa2013-02-01 08:25:07 +00003468 << (TmpAttr.isC11() ? ExpectedVariableOrField
3469 : ExpectedVariableFieldOrTag);
Richard Smith4cd81c52013-01-29 09:02:09 +00003470 return;
3471 }
3472 if (DiagKind != -1) {
Richard Smithbe507b62013-02-01 08:12:08 +00003473 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smith671b3212013-02-22 04:55:39 +00003474 << TmpAttr.isC11() << DiagKind;
Richard Smith4cd81c52013-01-29 09:02:09 +00003475 return;
3476 }
3477 }
3478
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003479 if (E->isTypeDependent() || E->isValueDependent()) {
3480 // Save dependent expressions in the AST to be instantiated.
Richard Smithf6565a92013-02-22 08:32:16 +00003481 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3482 AA->setPackExpansion(IsPackExpansion);
3483 D->addAttr(AA);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003484 return;
3485 }
Michael Hana31f65b2013-02-01 01:19:17 +00003486
Sean Huntcf807c42010-08-18 23:23:40 +00003487 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003488 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003489 ExprResult ICE
3490 = VerifyIntegerConstantExpression(E, &Alignment,
3491 diag::err_aligned_attribute_argument_not_int,
3492 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003493 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003494 return;
Richard Smithbe507b62013-02-01 08:12:08 +00003495
3496 // C++11 [dcl.align]p2:
3497 // -- if the constant expression evaluates to zero, the alignment
3498 // specifier shall have no effect
3499 // C11 6.7.5p6:
3500 // An alignment specification of zero has no effect.
3501 if (!(TmpAttr.isAlignas() && !Alignment) &&
3502 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003503 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3504 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003505 return;
3506 }
Michael Hana31f65b2013-02-01 01:19:17 +00003507
Richard Smithbe507b62013-02-01 08:12:08 +00003508 if (TmpAttr.isDeclspec()) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003509 // We've already verified it's a power of 2, now let's make sure it's
3510 // 8192 or less.
3511 if (Alignment.getZExtValue() > 8192) {
Michael Hana31f65b2013-02-01 01:19:17 +00003512 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003513 << E->getSourceRange();
3514 return;
3515 }
3516 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003517
Richard Smithf6565a92013-02-22 08:32:16 +00003518 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3519 ICE.take(), SpellingListIndex);
3520 AA->setPackExpansion(IsPackExpansion);
3521 D->addAttr(AA);
Sean Huntcf807c42010-08-18 23:23:40 +00003522}
3523
Michael Hana31f65b2013-02-01 01:19:17 +00003524void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smithf6565a92013-02-22 08:32:16 +00003525 unsigned SpellingListIndex, bool IsPackExpansion) {
Sean Huntcf807c42010-08-18 23:23:40 +00003526 // FIXME: Cache the number on the Attr object if non-dependent?
3527 // FIXME: Perform checking of type validity
Richard Smithf6565a92013-02-22 08:32:16 +00003528 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3529 SpellingListIndex);
3530 AA->setPackExpansion(IsPackExpansion);
3531 D->addAttr(AA);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003532}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003533
Richard Smithbe507b62013-02-01 08:12:08 +00003534void Sema::CheckAlignasUnderalignment(Decl *D) {
3535 assert(D->hasAttrs() && "no attributes on decl");
3536
3537 QualType Ty;
3538 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3539 Ty = VD->getType();
3540 else
3541 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith4da09032013-02-22 09:21:42 +00003542 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smithbe507b62013-02-01 08:12:08 +00003543 return;
3544
3545 // C++11 [dcl.align]p5, C11 6.7.5/4:
3546 // The combined effect of all alignment attributes in a declaration shall
3547 // not specify an alignment that is less strict than the alignment that
3548 // would otherwise be required for the entity being declared.
3549 AlignedAttr *AlignasAttr = 0;
3550 unsigned Align = 0;
3551 for (specific_attr_iterator<AlignedAttr>
3552 I = D->specific_attr_begin<AlignedAttr>(),
3553 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3554 if (I->isAlignmentDependent())
3555 return;
3556 if (I->isAlignas())
3557 AlignasAttr = *I;
3558 Align = std::max(Align, I->getAlignment(Context));
3559 }
3560
3561 if (AlignasAttr && Align) {
3562 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3563 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3564 if (NaturalAlign > RequestedAlign)
3565 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3566 << Ty << (unsigned)NaturalAlign.getQuantity();
3567 }
3568}
3569
Chandler Carruthd309c812011-07-01 23:49:16 +00003570/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003571/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003572///
Mike Stumpbf916502009-07-24 19:02:52 +00003573/// Despite what would be logical, the mode attribute is a decl attribute, not a
3574/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3575/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003576static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003577 // This attribute isn't documented, but glibc uses it. It changes
3578 // the width of an int or unsigned int to the specified size.
3579
3580 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00003581 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003582 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003583
Chris Lattnerfbf13472008-06-27 22:18:37 +00003584
3585 IdentifierInfo *Name = Attr.getParameterName();
3586 if (!Name) {
Aaron Ballman750f73a2013-07-30 14:29:12 +00003587 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3588 << AANT_ArgumentIdentifier;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003589 return;
3590 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00003591
Chris Lattner5f9e2722011-07-23 10:55:15 +00003592 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003593
3594 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003595 if (Str.startswith("__") && Str.endswith("__"))
3596 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003597
3598 unsigned DestWidth = 0;
3599 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003600 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003601 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003602 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003603 switch (Str[0]) {
3604 case 'Q': DestWidth = 8; break;
3605 case 'H': DestWidth = 16; break;
3606 case 'S': DestWidth = 32; break;
3607 case 'D': DestWidth = 64; break;
3608 case 'X': DestWidth = 96; break;
3609 case 'T': DestWidth = 128; break;
3610 }
3611 if (Str[1] == 'F') {
3612 IntegerMode = false;
3613 } else if (Str[1] == 'C') {
3614 IntegerMode = false;
3615 ComplexMode = true;
3616 } else if (Str[1] != 'I') {
3617 DestWidth = 0;
3618 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003619 break;
3620 case 4:
3621 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3622 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003623 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003624 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003625 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003626 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003627 break;
3628 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003629 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003630 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003631 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003632 case 11:
3633 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003634 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003635 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003636 }
3637
3638 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003639 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003640 OldTy = TD->getUnderlyingType();
3641 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3642 OldTy = VD->getType();
3643 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003644 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003645 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003646 return;
3647 }
Eli Friedman73397492009-03-03 06:41:03 +00003648
John McCall183700f2009-09-21 23:43:11 +00003649 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003650 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3651 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003652 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003653 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3654 } else if (ComplexMode) {
3655 if (!OldTy->isComplexType())
3656 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3657 } else {
3658 if (!OldTy->isFloatingType())
3659 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3660 }
3661
Mike Stump390b4cc2009-05-16 07:39:55 +00003662 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3663 // and friends, at least with glibc.
3664 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3665 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003666 // FIXME: Make sure floating-point mappings are accurate
3667 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003668 QualType NewTy;
3669 switch (DestWidth) {
3670 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003671 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003672 return;
3673 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003674 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003675 return;
3676 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003677 if (!IntegerMode) {
3678 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3679 return;
3680 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003681 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003682 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003683 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003684 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003685 break;
3686 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003687 if (!IntegerMode) {
3688 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3689 return;
3690 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003691 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003692 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003693 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003694 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003695 break;
3696 case 32:
3697 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003698 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003699 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003700 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003701 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003702 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003703 break;
3704 case 64:
3705 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003706 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003707 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003708 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003709 NewTy = S.Context.LongTy;
3710 else
3711 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003712 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003713 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003714 NewTy = S.Context.UnsignedLongTy;
3715 else
3716 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003717 break;
Eli Friedman73397492009-03-03 06:41:03 +00003718 case 96:
3719 NewTy = S.Context.LongDoubleTy;
3720 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003721 case 128:
Roman Divacky19645542013-07-03 21:08:41 +00003722 if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() !=
3723 &llvm::APFloat::PPCDoubleDouble) {
Eli Friedmanf98aba32009-02-13 02:31:07 +00003724 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3725 return;
3726 }
Roman Divackyf0d14cb2013-07-03 20:48:06 +00003727 if (IntegerMode) {
3728 if (OldTy->isSignedIntegerType())
3729 NewTy = S.Context.Int128Ty;
3730 else
3731 NewTy = S.Context.UnsignedInt128Ty;
3732 } else
3733 NewTy = S.Context.LongDoubleTy;
Eli Friedman73397492009-03-03 06:41:03 +00003734 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003735 }
3736
Eli Friedman73397492009-03-03 06:41:03 +00003737 if (ComplexMode) {
3738 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003739 }
3740
3741 // Install the new type.
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003742 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3743 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3744 else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003745 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003746
3747 D->addAttr(::new (S.Context)
3748 ModeAttr(Attr.getRange(), S.Context, Name,
3749 Attr.getAttributeSpellingListIndex()));
Chris Lattnerfbf13472008-06-27 22:18:37 +00003750}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003751
Chandler Carruth1b03c872011-07-02 00:01:44 +00003752static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003753 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003754 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003755 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003756
Nick Lewycky78d1a102012-07-24 01:40:49 +00003757 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3758 if (!VD->hasGlobalStorage())
3759 S.Diag(Attr.getLoc(),
3760 diag::warn_attribute_requires_functions_or_static_globals)
3761 << Attr.getName();
3762 } else if (!isFunctionOrMethod(D)) {
3763 S.Diag(Attr.getLoc(),
3764 diag::warn_attribute_requires_functions_or_static_globals)
3765 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003766 return;
3767 }
Mike Stumpbf916502009-07-24 19:02:52 +00003768
Michael Han51d8c522013-01-24 16:46:58 +00003769 D->addAttr(::new (S.Context)
3770 NoDebugAttr(Attr.getRange(), S.Context,
3771 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003772}
3773
Chandler Carruth1b03c872011-07-02 00:01:44 +00003774static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003775 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003776 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003777 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003778
Mike Stumpbf916502009-07-24 19:02:52 +00003779
Chandler Carruth87c44602011-07-01 23:49:12 +00003780 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003781 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003782 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003783 return;
3784 }
Mike Stumpbf916502009-07-24 19:02:52 +00003785
Michael Han51d8c522013-01-24 16:46:58 +00003786 D->addAttr(::new (S.Context)
3787 NoInlineAttr(Attr.getRange(), S.Context,
3788 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003789}
3790
Chandler Carruth1b03c872011-07-02 00:01:44 +00003791static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3792 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003793 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003794 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003795 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003796
Chris Lattner7255a2d2010-06-22 00:03:40 +00003797
Chandler Carruth87c44602011-07-01 23:49:12 +00003798 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003799 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003800 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003801 return;
3802 }
3803
Michael Han51d8c522013-01-24 16:46:58 +00003804 D->addAttr(::new (S.Context)
3805 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3806 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003807}
3808
Chandler Carruth1b03c872011-07-02 00:01:44 +00003809static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003810 if (S.LangOpts.CUDA) {
3811 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00003812 if (Attr.hasParameterOrArguments()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003813 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3814 << Attr.getName() << 0;
Peter Collingbourneced76712010-12-01 03:15:31 +00003815 return;
3816 }
3817
Chandler Carruth87c44602011-07-01 23:49:12 +00003818 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003819 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003820 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003821 return;
3822 }
3823
Michael Han51d8c522013-01-24 16:46:58 +00003824 D->addAttr(::new (S.Context)
3825 CUDAConstantAttr(Attr.getRange(), S.Context,
3826 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003827 } else {
3828 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3829 }
3830}
3831
Chandler Carruth1b03c872011-07-02 00:01:44 +00003832static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003833 if (S.LangOpts.CUDA) {
3834 // check the attribute arguments.
3835 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003836 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3837 << Attr.getName() << 0;
Peter Collingbourneced76712010-12-01 03:15:31 +00003838 return;
3839 }
3840
Chandler Carruth87c44602011-07-01 23:49:12 +00003841 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003842 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003843 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003844 return;
3845 }
3846
Michael Han51d8c522013-01-24 16:46:58 +00003847 D->addAttr(::new (S.Context)
3848 CUDADeviceAttr(Attr.getRange(), S.Context,
3849 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003850 } else {
3851 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3852 }
3853}
3854
Chandler Carruth1b03c872011-07-02 00:01:44 +00003855static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003856 if (S.LangOpts.CUDA) {
3857 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003858 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003859 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003860
Chandler Carruth87c44602011-07-01 23:49:12 +00003861 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003862 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003863 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003864 return;
3865 }
3866
Chandler Carruth87c44602011-07-01 23:49:12 +00003867 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003868 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003869 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00003870 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003871 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3872 << FD->getType()
David Blaikie39e6ab42013-02-18 22:06:02 +00003873 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003874 "void");
3875 } else {
3876 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3877 << FD->getType();
3878 }
3879 return;
3880 }
3881
Michael Han51d8c522013-01-24 16:46:58 +00003882 D->addAttr(::new (S.Context)
3883 CUDAGlobalAttr(Attr.getRange(), S.Context,
3884 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003885 } else {
3886 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3887 }
3888}
3889
Chandler Carruth1b03c872011-07-02 00:01:44 +00003890static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003891 if (S.LangOpts.CUDA) {
3892 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003893 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003894 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003895
Peter Collingbourneced76712010-12-01 03:15:31 +00003896
Chandler Carruth87c44602011-07-01 23:49:12 +00003897 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003898 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003899 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003900 return;
3901 }
3902
Michael Han51d8c522013-01-24 16:46:58 +00003903 D->addAttr(::new (S.Context)
3904 CUDAHostAttr(Attr.getRange(), S.Context,
3905 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003906 } else {
3907 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3908 }
3909}
3910
Chandler Carruth1b03c872011-07-02 00:01:44 +00003911static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003912 if (S.LangOpts.CUDA) {
3913 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003914 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003915 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003916
Chandler Carruth87c44602011-07-01 23:49:12 +00003917 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003918 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003919 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003920 return;
3921 }
3922
Michael Han51d8c522013-01-24 16:46:58 +00003923 D->addAttr(::new (S.Context)
3924 CUDASharedAttr(Attr.getRange(), S.Context,
3925 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003926 } else {
3927 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3928 }
3929}
3930
Chandler Carruth1b03c872011-07-02 00:01:44 +00003931static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003932 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003933 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003934 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003935
Chandler Carruth87c44602011-07-01 23:49:12 +00003936 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003937 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003938 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003939 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003940 return;
3941 }
Mike Stumpbf916502009-07-24 19:02:52 +00003942
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003943 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003944 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003945 return;
3946 }
Mike Stumpbf916502009-07-24 19:02:52 +00003947
Michael Han51d8c522013-01-24 16:46:58 +00003948 D->addAttr(::new (S.Context)
3949 GNUInlineAttr(Attr.getRange(), S.Context,
3950 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00003951}
3952
Chandler Carruth1b03c872011-07-02 00:01:44 +00003953static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003954 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003955
Aaron Ballmanfff32482012-12-09 17:45:41 +00003956 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003957 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003958 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3959 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003960 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003961 return;
3962
Chandler Carruth87c44602011-07-01 23:49:12 +00003963 if (!isa<ObjCMethodDecl>(D)) {
3964 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3965 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003966 return;
3967 }
3968
Chandler Carruth87c44602011-07-01 23:49:12 +00003969 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003970 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00003971 D->addAttr(::new (S.Context)
3972 FastCallAttr(Attr.getRange(), S.Context,
3973 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003974 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003975 case AttributeList::AT_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00003976 D->addAttr(::new (S.Context)
3977 StdCallAttr(Attr.getRange(), S.Context,
3978 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003979 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003980 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00003981 D->addAttr(::new (S.Context)
3982 ThisCallAttr(Attr.getRange(), S.Context,
3983 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003984 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003985 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00003986 D->addAttr(::new (S.Context)
3987 CDeclAttr(Attr.getRange(), S.Context,
3988 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003989 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003990 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00003991 D->addAttr(::new (S.Context)
3992 PascalAttr(Attr.getRange(), S.Context,
3993 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003994 return;
Charles Davise8519c32013-08-30 04:39:01 +00003995 case AttributeList::AT_MSABI:
3996 D->addAttr(::new (S.Context)
3997 MSABIAttr(Attr.getRange(), S.Context,
3998 Attr.getAttributeSpellingListIndex()));
3999 return;
4000 case AttributeList::AT_SysVABI:
4001 D->addAttr(::new (S.Context)
4002 SysVABIAttr(Attr.getRange(), S.Context,
4003 Attr.getAttributeSpellingListIndex()));
4004 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004005 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004006 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00004007 switch (CC) {
4008 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004009 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00004010 break;
4011 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004012 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00004013 break;
4014 default:
4015 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004016 }
4017
Michael Han51d8c522013-01-24 16:46:58 +00004018 D->addAttr(::new (S.Context)
4019 PcsAttr(Attr.getRange(), S.Context, PCS,
4020 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00004021 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004022 }
Derek Schuff263366f2012-10-16 22:30:41 +00004023 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00004024 D->addAttr(::new (S.Context)
4025 PnaclCallAttr(Attr.getRange(), S.Context,
4026 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00004027 return;
Guy Benyei38980082012-12-25 08:53:55 +00004028 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00004029 D->addAttr(::new (S.Context)
4030 IntelOclBiccAttr(Attr.getRange(), S.Context,
4031 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00004032 return;
Derek Schuff263366f2012-10-16 22:30:41 +00004033
Abramo Bagnarae215f722010-04-30 13:10:51 +00004034 default:
4035 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00004036 }
4037}
4038
Chandler Carruth1b03c872011-07-02 00:01:44 +00004039static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004040 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00004041}
4042
Guy Benyei1db70402013-03-24 13:58:12 +00004043static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
Guy Benyei1db70402013-03-24 13:58:12 +00004044 Expr *E = Attr.getArg(0);
4045 llvm::APSInt ArgNum(32);
4046 if (E->isTypeDependent() || E->isValueDependent() ||
4047 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00004048 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4049 << Attr.getName() << AANT_ArgumentIntegerConstant
4050 << E->getSourceRange();
Guy Benyei1db70402013-03-24 13:58:12 +00004051 return;
4052 }
4053
4054 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
4055 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
4056}
4057
Aaron Ballmanfff32482012-12-09 17:45:41 +00004058bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
4059 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00004060 if (attr.isInvalid())
4061 return true;
4062
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004063 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
4064 if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00004065 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments)
4066 << attr.getName() << ReqArgs;
John McCall711c52b2011-01-05 12:14:39 +00004067 attr.setInvalid();
4068 return true;
4069 }
4070
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004071 // TODO: diagnose uses of these conventions on the wrong target. Or, better
4072 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00004073 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004074 case AttributeList::AT_CDecl: CC = CC_C; break;
4075 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
4076 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
4077 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
4078 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davise8519c32013-08-30 04:39:01 +00004079 case AttributeList::AT_MSABI:
4080 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
4081 CC_X86_64Win64;
4082 break;
4083 case AttributeList::AT_SysVABI:
4084 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
4085 CC_C;
4086 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004087 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004088 Expr *Arg = attr.getArg(0);
4089 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00004090 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004091 Diag(attr.getLoc(), diag::err_attribute_argument_type) << attr.getName()
4092 << AANT_ArgumentString;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004093 attr.setInvalid();
4094 return true;
4095 }
4096
Chris Lattner5f9e2722011-07-23 10:55:15 +00004097 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004098 if (StrRef == "aapcs") {
4099 CC = CC_AAPCS;
4100 break;
4101 } else if (StrRef == "aapcs-vfp") {
4102 CC = CC_AAPCS_VFP;
4103 break;
4104 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004105
4106 attr.setInvalid();
4107 Diag(attr.getLoc(), diag::err_invalid_pcs);
4108 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004109 }
Derek Schuff263366f2012-10-16 22:30:41 +00004110 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00004111 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00004112 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00004113 }
4114
Aaron Ballman82bfa192012-10-02 14:26:08 +00004115 const TargetInfo &TI = Context.getTargetInfo();
4116 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
4117 if (A == TargetInfo::CCCR_Warning) {
4118 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00004119
4120 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
4121 if (FD)
4122 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
4123 TargetInfo::CCMT_NonMember;
4124 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00004125 }
4126
John McCall711c52b2011-01-05 12:14:39 +00004127 return false;
4128}
4129
Chandler Carruth1b03c872011-07-02 00:01:44 +00004130static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004131 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00004132
4133 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00004134 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00004135 return;
4136
Chandler Carruth87c44602011-07-01 23:49:12 +00004137 if (!isa<ObjCMethodDecl>(D)) {
4138 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4139 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004140 return;
4141 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004142
Michael Han51d8c522013-01-24 16:46:58 +00004143 D->addAttr(::new (S.Context)
4144 RegparmAttr(Attr.getRange(), S.Context, numParams,
4145 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00004146}
4147
4148/// Checks a regparm attribute, returning true if it is ill-formed and
4149/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00004150bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
4151 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00004152 return true;
4153
Aaron Ballmanffa9d572013-07-18 18:01:48 +00004154 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004155 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004156 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004157 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004158
Chandler Carruth87c44602011-07-01 23:49:12 +00004159 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004160 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00004161 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00004162 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00004163 Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4164 << Attr.getName() << AANT_ArgumentIntegerConstant
4165 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004166 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004167 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004168 }
4169
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004170 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004171 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004172 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004173 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004174 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004175 }
4176
John McCall711c52b2011-01-05 12:14:39 +00004177 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004178 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004179 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004180 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004181 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004182 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004183 }
4184
John McCall711c52b2011-01-05 12:14:39 +00004185 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004186}
4187
Chandler Carruth1b03c872011-07-02 00:01:44 +00004188static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00004189 if (S.LangOpts.CUDA) {
4190 // check the attribute arguments.
4191 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00004192 // FIXME: 0 is not okay.
4193 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004194 return;
4195 }
4196
Chandler Carruth87c44602011-07-01 23:49:12 +00004197 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00004198 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00004199 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004200 return;
4201 }
4202
4203 Expr *MaxThreadsExpr = Attr.getArg(0);
4204 llvm::APSInt MaxThreads(32);
4205 if (MaxThreadsExpr->isTypeDependent() ||
4206 MaxThreadsExpr->isValueDependent() ||
4207 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004208 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004209 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004210 << MaxThreadsExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004211 return;
4212 }
4213
4214 llvm::APSInt MinBlocks(32);
4215 if (Attr.getNumArgs() > 1) {
4216 Expr *MinBlocksExpr = Attr.getArg(1);
4217 if (MinBlocksExpr->isTypeDependent() ||
4218 MinBlocksExpr->isValueDependent() ||
4219 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004220 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004221 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004222 << MinBlocksExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004223 return;
4224 }
4225 }
4226
Michael Han51d8c522013-01-24 16:46:58 +00004227 D->addAttr(::new (S.Context)
4228 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4229 MaxThreads.getZExtValue(),
4230 MinBlocks.getZExtValue(),
4231 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne7b381982010-12-12 23:03:07 +00004232 } else {
4233 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4234 }
4235}
4236
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004237static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4238 const AttributeList &Attr) {
4239 StringRef AttrName = Attr.getName()->getName();
4240 if (!Attr.getParameterName()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004241 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004242 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004243 return;
4244 }
4245
4246 if (Attr.getNumArgs() != 2) {
4247 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
Aaron Ballmanbaec7782013-07-23 19:30:11 +00004248 << Attr.getName() << /* required args = */ 3;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004249 return;
4250 }
4251
4252 IdentifierInfo *ArgumentKind = Attr.getParameterName();
4253
4254 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4255 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4256 << Attr.getName() << ExpectedFunctionOrMethod;
4257 return;
4258 }
4259
4260 uint64_t ArgumentIdx;
4261 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4262 Attr.getLoc(), 2,
4263 Attr.getArg(0), ArgumentIdx))
4264 return;
4265
4266 uint64_t TypeTagIdx;
4267 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4268 Attr.getLoc(), 3,
4269 Attr.getArg(1), TypeTagIdx))
4270 return;
4271
4272 bool IsPointer = (AttrName == "pointer_with_type_tag");
4273 if (IsPointer) {
4274 // Ensure that buffer has a pointer type.
4275 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4276 if (!BufferTy->isPointerType()) {
4277 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004278 << Attr.getName();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004279 }
4280 }
4281
Michael Han51d8c522013-01-24 16:46:58 +00004282 D->addAttr(::new (S.Context)
4283 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4284 ArgumentIdx, TypeTagIdx, IsPointer,
4285 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004286}
4287
4288static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4289 const AttributeList &Attr) {
4290 IdentifierInfo *PointerKind = Attr.getParameterName();
4291 if (!PointerKind) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004292 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004293 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004294 return;
4295 }
4296
4297 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4298
Michael Han51d8c522013-01-24 16:46:58 +00004299 D->addAttr(::new (S.Context)
4300 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4301 MatchingCType,
4302 Attr.getLayoutCompatible(),
4303 Attr.getMustBeNull(),
4304 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004305}
4306
Chris Lattner0744e5f2008-06-29 00:23:49 +00004307//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00004308// Checker-specific attribute handlers.
4309//===----------------------------------------------------------------------===//
4310
John McCallc7ad3812011-01-25 03:31:58 +00004311static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004312 return type->isDependentType() ||
4313 type->isObjCObjectPointerType() ||
4314 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00004315}
4316static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004317 return type->isDependentType() ||
4318 type->isPointerType() ||
4319 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00004320}
4321
Chandler Carruth1b03c872011-07-02 00:01:44 +00004322static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004323 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00004324 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004325 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004326 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00004327 return;
4328 }
4329
4330 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00004331 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00004332 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4333 cf = false;
4334 } else {
4335 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4336 cf = true;
4337 }
4338
4339 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004340 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004341 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00004342 return;
4343 }
4344
4345 if (cf)
Michael Han51d8c522013-01-24 16:46:58 +00004346 param->addAttr(::new (S.Context)
4347 CFConsumedAttr(Attr.getRange(), S.Context,
4348 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004349 else
Michael Han51d8c522013-01-24 16:46:58 +00004350 param->addAttr(::new (S.Context)
4351 NSConsumedAttr(Attr.getRange(), S.Context,
4352 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004353}
4354
Chandler Carruth1b03c872011-07-02 00:01:44 +00004355static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4356 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004357 if (!isa<ObjCMethodDecl>(D)) {
4358 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004359 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00004360 return;
4361 }
4362
Michael Han51d8c522013-01-24 16:46:58 +00004363 D->addAttr(::new (S.Context)
4364 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4365 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004366}
4367
Chandler Carruth1b03c872011-07-02 00:01:44 +00004368static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4369 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004370
John McCallc7ad3812011-01-25 03:31:58 +00004371 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00004372
Chandler Carruth87c44602011-07-01 23:49:12 +00004373 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004374 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004375 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00004376 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00004377 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00004378 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4379 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00004380 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004381 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004382 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00004383 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004384 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00004385 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004386 return;
4387 }
Mike Stumpbf916502009-07-24 19:02:52 +00004388
John McCallc7ad3812011-01-25 03:31:58 +00004389 bool typeOK;
4390 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00004391 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00004392 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004393 case AttributeList::AT_NSReturnsAutoreleased:
4394 case AttributeList::AT_NSReturnsRetained:
4395 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004396 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4397 cf = false;
4398 break;
4399
Sean Hunt8e083e72012-06-19 23:57:03 +00004400 case AttributeList::AT_CFReturnsRetained:
4401 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004402 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4403 cf = true;
4404 break;
4405 }
4406
4407 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004408 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004409 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00004410 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004411 }
Mike Stumpbf916502009-07-24 19:02:52 +00004412
Chandler Carruth87c44602011-07-01 23:49:12 +00004413 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004414 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00004415 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004416 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han51d8c522013-01-24 16:46:58 +00004417 D->addAttr(::new (S.Context)
4418 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4419 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004420 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004421 case AttributeList::AT_CFReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004422 D->addAttr(::new (S.Context)
4423 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4424 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004425 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004426 case AttributeList::AT_NSReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004427 D->addAttr(::new (S.Context)
4428 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4429 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004430 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004431 case AttributeList::AT_CFReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004432 D->addAttr(::new (S.Context)
4433 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4434 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004435 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004436 case AttributeList::AT_NSReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004437 D->addAttr(::new (S.Context)
4438 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4439 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004440 return;
4441 };
4442}
4443
John McCalldc7c5ad2011-07-22 08:53:00 +00004444static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4445 const AttributeList &attr) {
4446 SourceLocation loc = attr.getLoc();
4447
4448 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4449
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004450 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00004451 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004452 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00004453 return;
4454 }
4455
4456 // Check that the method returns a normal pointer.
4457 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004458
4459 if (!resultType->isReferenceType() &&
4460 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00004461 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4462 << SourceRange(loc)
4463 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4464
4465 // Drop the attribute.
4466 return;
4467 }
4468
Michael Han51d8c522013-01-24 16:46:58 +00004469 method->addAttr(::new (S.Context)
4470 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4471 attr.getAttributeSpellingListIndex()));
John McCalldc7c5ad2011-07-22 08:53:00 +00004472}
4473
Fariborz Jahanian84101132012-09-07 23:46:23 +00004474static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4475 const AttributeList &attr) {
4476 SourceLocation loc = attr.getLoc();
4477 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4478
4479 if (!method) {
4480 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4481 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4482 return;
4483 }
4484 DeclContext *DC = method->getDeclContext();
4485 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4486 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4487 << attr.getName() << 0;
4488 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4489 return;
4490 }
4491 if (method->getMethodFamily() == OMF_dealloc) {
4492 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4493 << attr.getName() << 1;
4494 return;
4495 }
4496
Michael Han51d8c522013-01-24 16:46:58 +00004497 method->addAttr(::new (S.Context)
4498 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4499 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian84101132012-09-07 23:46:23 +00004500}
4501
John McCall8dfac0b2011-09-30 05:12:12 +00004502/// Handle cf_audited_transfer and cf_unknown_transfer.
4503static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4504 if (!isa<FunctionDecl>(D)) {
4505 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004506 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004507 return;
4508 }
4509
Sean Hunt8e083e72012-06-19 23:57:03 +00004510 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004511
4512 // Check whether there's a conflicting attribute already present.
4513 Attr *Existing;
4514 if (IsAudited) {
4515 Existing = D->getAttr<CFUnknownTransferAttr>();
4516 } else {
4517 Existing = D->getAttr<CFAuditedTransferAttr>();
4518 }
4519 if (Existing) {
4520 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4521 << A.getName()
4522 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4523 << A.getRange() << Existing->getRange();
4524 return;
4525 }
4526
4527 // All clear; add the attribute.
4528 if (IsAudited) {
Michael Han51d8c522013-01-24 16:46:58 +00004529 D->addAttr(::new (S.Context)
4530 CFAuditedTransferAttr(A.getRange(), S.Context,
4531 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004532 } else {
Michael Han51d8c522013-01-24 16:46:58 +00004533 D->addAttr(::new (S.Context)
4534 CFUnknownTransferAttr(A.getRange(), S.Context,
4535 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004536 }
4537}
4538
John McCallfe98da02011-09-29 07:17:38 +00004539static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4540 const AttributeList &Attr) {
4541 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4542 if (!RD || RD->isUnion()) {
4543 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004544 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004545 }
4546
4547 IdentifierInfo *ParmName = Attr.getParameterName();
4548
4549 // In Objective-C, verify that the type names an Objective-C type.
4550 // We don't want to check this outside of ObjC because people sometimes
4551 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00004552 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004553 // Check for an existing type with this name.
4554 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
4555 Sema::LookupOrdinaryName);
4556 if (S.LookupName(R, Sc)) {
4557 NamedDecl *Target = R.getFoundDecl();
4558 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4559 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4560 S.Diag(Target->getLocStart(), diag::note_declared_at);
4561 }
4562 }
4563 }
4564
Michael Han51d8c522013-01-24 16:46:58 +00004565 D->addAttr(::new (S.Context)
4566 NSBridgedAttr(Attr.getRange(), S.Context, ParmName,
4567 Attr.getAttributeSpellingListIndex()));
John McCallfe98da02011-09-29 07:17:38 +00004568}
4569
Chandler Carruth1b03c872011-07-02 00:01:44 +00004570static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4571 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004572 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004573
Chandler Carruth87c44602011-07-01 23:49:12 +00004574 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004575 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004576}
4577
Chandler Carruth1b03c872011-07-02 00:01:44 +00004578static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4579 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004580 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004581 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004582 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004583 return;
4584 }
4585
Chandler Carruth87c44602011-07-01 23:49:12 +00004586 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004587 QualType type = vd->getType();
4588
4589 if (!type->isDependentType() &&
4590 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004591 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004592 << type;
4593 return;
4594 }
4595
4596 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4597
4598 // If we have no lifetime yet, check the lifetime we're presumably
4599 // going to infer.
4600 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4601 lifetime = type->getObjCARCImplicitLifetime();
4602
4603 switch (lifetime) {
4604 case Qualifiers::OCL_None:
4605 assert(type->isDependentType() &&
4606 "didn't infer lifetime for non-dependent type?");
4607 break;
4608
4609 case Qualifiers::OCL_Weak: // meaningful
4610 case Qualifiers::OCL_Strong: // meaningful
4611 break;
4612
4613 case Qualifiers::OCL_ExplicitNone:
4614 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004615 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004616 << (lifetime == Qualifiers::OCL_Autoreleasing);
4617 break;
4618 }
4619
Chandler Carruth87c44602011-07-01 23:49:12 +00004620 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004621 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4622 Attr.getAttributeSpellingListIndex()));
John McCallf85e1932011-06-15 23:02:42 +00004623}
4624
Francois Pichet11542142010-12-19 06:50:37 +00004625//===----------------------------------------------------------------------===//
4626// Microsoft specific attribute handlers.
4627//===----------------------------------------------------------------------===//
4628
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004629// Check if MS extensions or some other language extensions are enabled. If
4630// not, issue a diagnostic that the given attribute is unused.
4631static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4632 bool OtherExtension = false) {
4633 if (S.LangOpts.MicrosoftExt || OtherExtension)
4634 return true;
4635 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4636 return false;
4637}
4638
Chandler Carruth1b03c872011-07-02 00:01:44 +00004639static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004640 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4641 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004642
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004643 // check the attribute arguments.
4644 if (!checkAttributeNumArgs(S, Attr, 1))
4645 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004646
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004647 Expr *Arg = Attr.getArg(0);
4648 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
4649 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004650 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4651 << Attr.getName() << AANT_ArgumentString;
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004652 return;
4653 }
Francois Pichetd3d3be92010-12-20 01:41:49 +00004654
David Majnemerbb0ed292013-08-09 08:56:20 +00004655 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4656 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004657 StringRef StrRef = Str->getString();
David Majnemerbb0ed292013-08-09 08:56:20 +00004658 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4659 StrRef = StrRef.drop_front().drop_back();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004660
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004661 // Validate GUID length.
David Majnemerbb0ed292013-08-09 08:56:20 +00004662 if (StrRef.size() != 36) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004663 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4664 return;
4665 }
Anders Carlssonf89e0422011-01-23 21:07:30 +00004666
David Majnemerbb0ed292013-08-09 08:56:20 +00004667 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004668 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemerbb0ed292013-08-09 08:56:20 +00004669 if (StrRef[i] != '-') {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004670 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4671 return;
4672 }
David Majnemerbb0ed292013-08-09 08:56:20 +00004673 } else if (!isHexDigit(StrRef[i])) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004674 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4675 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004676 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004677 }
Francois Pichet11542142010-12-19 06:50:37 +00004678
David Majnemerbb0ed292013-08-09 08:56:20 +00004679 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4680 Attr.getAttributeSpellingListIndex()));
Charles Davisf0122fe2010-02-16 18:27:26 +00004681}
4682
John McCallc052dbb2012-05-22 21:28:12 +00004683static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004684 if (!checkMicrosoftExt(S, Attr))
Nico Weber7b89ab72012-11-07 21:31:36 +00004685 return;
Nico Weber7b89ab72012-11-07 21:31:36 +00004686
4687 AttributeList::Kind Kind = Attr.getKind();
4688 if (Kind == AttributeList::AT_SingleInheritance)
4689 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004690 ::new (S.Context)
4691 SingleInheritanceAttr(Attr.getRange(), S.Context,
4692 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004693 else if (Kind == AttributeList::AT_MultipleInheritance)
4694 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004695 ::new (S.Context)
4696 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4697 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004698 else if (Kind == AttributeList::AT_VirtualInheritance)
4699 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004700 ::new (S.Context)
4701 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4702 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004703}
4704
4705static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004706 if (!checkMicrosoftExt(S, Attr))
4707 return;
4708
4709 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004710 if (Kind == AttributeList::AT_Win64)
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004711 D->addAttr(
4712 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4713 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004714}
4715
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004716static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004717 if (!checkMicrosoftExt(S, Attr))
4718 return;
4719 D->addAttr(::new (S.Context)
4720 ForceInlineAttr(Attr.getRange(), S.Context,
4721 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004722}
4723
Reid Klecknera7225342013-05-20 14:02:37 +00004724static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4725 if (!checkMicrosoftExt(S, Attr))
4726 return;
4727 // Check linkage after possibly merging declaratinos. See
4728 // checkAttributesAfterMerging().
4729 D->addAttr(::new (S.Context)
4730 SelectAnyAttr(Attr.getRange(), S.Context,
4731 Attr.getAttributeSpellingListIndex()));
4732}
4733
Ted Kremenekb71368d2009-05-09 02:44:38 +00004734//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004735// Top Level Sema Entry Points
4736//===----------------------------------------------------------------------===//
4737
Richard Smith4a97b8e2013-08-29 00:47:48 +00004738/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4739/// the attribute applies to decls. If the attribute is a type attribute, just
4740/// silently ignore it if a GNU attribute.
4741static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4742 const AttributeList &Attr,
4743 bool IncludeCXX11Attributes) {
4744 if (Attr.isInvalid())
4745 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00004746
Richard Smith4a97b8e2013-08-29 00:47:48 +00004747 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4748 // instead.
4749 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4750 return;
4751
Chris Lattner803d0802008-06-29 00:43:07 +00004752 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004753 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4754 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4755 case AttributeList::AT_IBOutletCollection:
4756 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004757 case AttributeList::AT_AddressSpace:
Sean Hunt8e083e72012-06-19 23:57:03 +00004758 case AttributeList::AT_ObjCGC:
4759 case AttributeList::AT_VectorSize:
4760 case AttributeList::AT_NeonVectorType:
4761 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004762 case AttributeList::AT_Ptr32:
4763 case AttributeList::AT_Ptr64:
4764 case AttributeList::AT_SPtr:
4765 case AttributeList::AT_UPtr:
Mike Stumpbf916502009-07-24 19:02:52 +00004766 // Ignore these, these are type attributes, handled by
4767 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004768 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004769 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4770 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4771 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4772 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004773 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004774 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004775 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004776 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004777 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4778 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4779 case AttributeList::AT_CarriesDependency:
Richard Smith3a2b7a12013-01-28 22:42:45 +00004780 handleDependencyAttr(S, scope, D, Attr);
4781 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004782 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4783 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4784 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004785 case AttributeList::AT_CXX11NoReturn:
4786 handleCXX11NoReturnAttr(S, D, Attr);
4787 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004788 case AttributeList::AT_Deprecated:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004789 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004790 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004791 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4792 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004793 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004794 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004795 case AttributeList::AT_MinSize:
4796 handleMinSizeAttr(S, D, Attr);
4797 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004798 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4799 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4800 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman19513232013-08-28 23:13:26 +00004801 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4802 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004803 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4804 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004805 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004806 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004807 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4808 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004809 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004810 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4811 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004812 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004813 case AttributeList::AT_ownership_returns:
4814 case AttributeList::AT_ownership_takes:
4815 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004816 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004817 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4818 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4819 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4820 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4821 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4822 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4823 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004824
Sean Hunt8e083e72012-06-19 23:57:03 +00004825 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004826 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004827 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004828 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004829
Sean Hunt8e083e72012-06-19 23:57:03 +00004830 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004831 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4832
Fariborz Jahanian84101132012-09-07 23:46:23 +00004833 case AttributeList::AT_ObjCRequiresSuper:
4834 handleObjCRequiresSuperAttr(S, D, Attr); break;
4835
Sean Hunt8e083e72012-06-19 23:57:03 +00004836 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004837 handleNSBridgedAttr(S, scope, D, Attr); break;
4838
Sean Hunt8e083e72012-06-19 23:57:03 +00004839 case AttributeList::AT_CFAuditedTransfer:
4840 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004841 handleCFTransferAttr(S, D, Attr); break;
4842
Ted Kremenekb71368d2009-05-09 02:44:38 +00004843 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004844 case AttributeList::AT_CFConsumed:
4845 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4846 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004847 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004848
Sean Hunt8e083e72012-06-19 23:57:03 +00004849 case AttributeList::AT_NSReturnsAutoreleased:
4850 case AttributeList::AT_NSReturnsNotRetained:
4851 case AttributeList::AT_CFReturnsNotRetained:
4852 case AttributeList::AT_NSReturnsRetained:
4853 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004854 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004855
Tanya Lattner0df579e2012-07-09 22:06:01 +00004856 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004857 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004858 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004859
Joey Gouly37453b92013-03-08 09:42:32 +00004860 case AttributeList::AT_VecTypeHint:
4861 handleVecTypeHint(S, D, Attr); break;
4862
Joey Gouly96cead52013-03-14 09:54:43 +00004863 case AttributeList::AT_Endian:
4864 handleEndianAttr(S, D, Attr);
4865 break;
4866
Sean Hunt8e083e72012-06-19 23:57:03 +00004867 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004868 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004869
Sean Hunt8e083e72012-06-19 23:57:03 +00004870 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4871 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4872 case AttributeList::AT_Unavailable:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004873 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004874 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004875 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004876 handleArcWeakrefUnavailableAttr (S, D, Attr);
4877 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004878 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004879 handleObjCRootClassAttr(S, D, Attr);
4880 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004881 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004882 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004883 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004884 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4885 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004886 handleReturnsTwiceAttr(S, D, Attr);
4887 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004888 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld4c3d662013-02-20 01:54:26 +00004889 case AttributeList::AT_Visibility:
4890 handleVisibilityAttr(S, D, Attr, false);
4891 break;
4892 case AttributeList::AT_TypeVisibility:
4893 handleVisibilityAttr(S, D, Attr, true);
4894 break;
Lubos Lunak1d3ce652013-07-20 15:05:36 +00004895 case AttributeList::AT_WarnUnused:
4896 handleWarnUnusedAttr(S, D, Attr);
4897 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004898 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004899 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004900 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4901 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4902 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4903 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004904 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004905 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004906 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004907 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004908 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004909 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004910 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004911 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004912 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4913 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4914 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4915 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4916 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4917 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4918 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4919 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4920 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004921 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004922 // Just ignore
4923 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004924 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004925 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004926 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004927 case AttributeList::AT_StdCall:
4928 case AttributeList::AT_CDecl:
4929 case AttributeList::AT_FastCall:
4930 case AttributeList::AT_ThisCall:
4931 case AttributeList::AT_Pascal:
Charles Davise8519c32013-08-30 04:39:01 +00004932 case AttributeList::AT_MSABI:
4933 case AttributeList::AT_SysVABI:
Sean Hunt8e083e72012-06-19 23:57:03 +00004934 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004935 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004936 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004937 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004938 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004939 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004940 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004941 break;
Guy Benyei1db70402013-03-24 13:58:12 +00004942 case AttributeList::AT_OpenCLImageAccess:
4943 handleOpenCLImageAccessAttr(S, D, Attr);
4944 break;
John McCallc052dbb2012-05-22 21:28:12 +00004945
4946 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004947 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004948 handleMsStructAttr(S, D, Attr);
4949 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004950 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004951 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004952 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004953 case AttributeList::AT_SingleInheritance:
4954 case AttributeList::AT_MultipleInheritance:
4955 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004956 handleInheritanceAttr(S, D, Attr);
4957 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004958 case AttributeList::AT_Win64:
John McCallc052dbb2012-05-22 21:28:12 +00004959 handlePortabilityAttr(S, D, Attr);
4960 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004961 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004962 handleForceInlineAttr(S, D, Attr);
4963 break;
Reid Klecknera7225342013-05-20 14:02:37 +00004964 case AttributeList::AT_SelectAny:
4965 handleSelectAnyAttr(S, D, Attr);
4966 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004967
4968 // Thread safety attributes:
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +00004969 case AttributeList::AT_AssertExclusiveLock:
4970 handleAssertExclusiveLockAttr(S, D, Attr);
4971 break;
4972 case AttributeList::AT_AssertSharedLock:
4973 handleAssertSharedLockAttr(S, D, Attr);
4974 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004975 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004976 handleGuardedVarAttr(S, D, Attr);
4977 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004978 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004979 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004980 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004981 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004982 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004983 break;
Kostya Serebryany85aee962013-02-26 06:58:27 +00004984 case AttributeList::AT_NoSanitizeAddress:
4985 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany71efba02012-01-24 19:25:38 +00004986 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004987 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany85aee962013-02-26 06:58:27 +00004988 handleNoThreadSafetyAnalysis(S, D, Attr);
4989 break;
4990 case AttributeList::AT_NoSanitizeThread:
4991 handleNoSanitizeThread(S, D, Attr);
4992 break;
4993 case AttributeList::AT_NoSanitizeMemory:
4994 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004995 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004996 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004997 handleLockableAttr(S, D, Attr);
4998 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004999 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005000 handleGuardedByAttr(S, D, Attr);
5001 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005002 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00005003 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005004 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005005 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00005006 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005007 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005008 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00005009 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005010 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005011 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00005012 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005013 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005014 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005015 handleLockReturnedAttr(S, D, Attr);
5016 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005017 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005018 handleLocksExcludedAttr(S, D, Attr);
5019 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005020 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00005021 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005022 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005023 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00005024 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005025 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005026 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00005027 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005028 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005029 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005030 handleUnlockFunAttr(S, D, Attr);
5031 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005032 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00005033 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005034 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005035 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00005036 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005037 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005038
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00005039 // Uniqueness analysis attributes.
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00005040 case AttributeList::AT_Consumable:
5041 handleConsumableAttr(S, D, Attr);
5042 break;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00005043 case AttributeList::AT_Consumes:
5044 handleConsumesAttr(S, D, Attr);
5045 break;
5046 case AttributeList::AT_CallableWhenUnconsumed:
5047 handleCallableWhenUnconsumedAttr(S, D, Attr);
5048 break;
5049 case AttributeList::AT_TestsConsumed:
5050 handleTestsConsumedAttr(S, D, Attr);
5051 break;
5052 case AttributeList::AT_TestsUnconsumed:
5053 handleTestsUnconsumedAttr(S, D, Attr);
5054 break;
5055
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00005056 // Type safety attributes.
5057 case AttributeList::AT_ArgumentWithTypeTag:
5058 handleArgumentWithTypeTagAttr(S, D, Attr);
5059 break;
5060 case AttributeList::AT_TypeTagForDatatype:
5061 handleTypeTagForDatatypeAttr(S, D, Attr);
5062 break;
5063
Chris Lattner803d0802008-06-29 00:43:07 +00005064 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005065 // Ask target about the attribute.
5066 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
5067 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00005068 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
5069 diag::warn_unhandled_ms_attribute_ignored :
5070 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00005071 break;
5072 }
5073}
5074
5075/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5076/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00005077void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00005078 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00005079 bool IncludeCXX11Attributes) {
5080 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005081 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005082
5083 // GCC accepts
5084 // static int a9 __attribute__((weakref));
5085 // but that looks really pointless. We reject it.
Richard Smith4a97b8e2013-08-29 00:47:48 +00005086 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005087 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00005088 cast<NamedDecl>(D)->getNameAsString();
5089 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005090 return;
Chris Lattner803d0802008-06-29 00:43:07 +00005091 }
5092}
5093
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005094// Annotation attributes are the only attributes allowed after an access
5095// specifier.
5096bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5097 const AttributeList *AttrList) {
5098 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00005099 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005100 handleAnnotateAttr(*this, ASDecl, *l);
5101 } else {
5102 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5103 return true;
5104 }
5105 }
5106
5107 return false;
5108}
5109
John McCalle82247a2011-10-01 05:17:03 +00005110/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5111/// contains any decl attributes that we should warn about.
5112static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5113 for ( ; A; A = A->getNext()) {
5114 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smithd03de6a2013-01-29 10:02:16 +00005115 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCalle82247a2011-10-01 05:17:03 +00005116 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5117
5118 if (A->getKind() == AttributeList::UnknownAttribute) {
5119 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5120 << A->getName() << A->getRange();
5121 } else {
5122 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5123 << A->getName() << A->getRange();
5124 }
5125 }
5126}
5127
5128/// checkUnusedDeclAttributes - Given a declarator which is not being
5129/// used to build a declaration, complain about any decl attributes
5130/// which might be lying around on it.
5131void Sema::checkUnusedDeclAttributes(Declarator &D) {
5132 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5133 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5134 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5135 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5136}
5137
Ryan Flynne25ff832009-07-30 03:15:39 +00005138/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00005139/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00005140NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5141 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005142 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00005143 NamedDecl *NewD = 0;
5144 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00005145 FunctionDecl *NewFD;
5146 // FIXME: Missing call to CheckFunctionDeclaration().
5147 // FIXME: Mangling?
5148 // FIXME: Is the qualifier info correct?
5149 // FIXME: Is the DeclContext correct?
5150 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5151 Loc, Loc, DeclarationName(II),
5152 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005153 SC_None, false/*isInlineSpecified*/,
Eli Friedman900693b2011-09-07 04:05:06 +00005154 FD->hasPrototype(),
5155 false/*isConstexprSpecified*/);
5156 NewD = NewFD;
5157
5158 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005159 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00005160
5161 // Fake up parameter variables; they are declared as if this were
5162 // a typedef.
5163 QualType FDTy = FD->getType();
5164 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5165 SmallVector<ParmVarDecl*, 16> Params;
5166 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5167 AE = FT->arg_type_end(); AI != AE; ++AI) {
5168 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5169 Param->setScopeInfo(0, Params.size());
5170 Params.push_back(Param);
5171 }
David Blaikie4278c652011-09-21 18:16:56 +00005172 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00005173 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005174 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5175 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005176 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00005177 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005178 VD->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00005179 if (VD->getQualifier()) {
5180 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005181 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00005182 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005183 }
5184 return NewD;
5185}
5186
James Dennett1dfbd922012-06-14 21:40:34 +00005187/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00005188/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005189void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005190 if (W.getUsed()) return; // only do this once
5191 W.setUsed(true);
5192 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5193 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00005194 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00005195 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5196 NDId->getName()));
5197 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005198 WeakTopLevelDecl.push_back(NewD);
5199 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5200 // to insert Decl at TU scope, sorry.
5201 DeclContext *SavedContext = CurContext;
5202 CurContext = Context.getTranslationUnitDecl();
5203 PushOnScopeChains(NewD, S);
5204 CurContext = SavedContext;
5205 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00005206 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00005207 }
5208}
5209
Rafael Espindola65611bf2013-03-02 21:41:48 +00005210void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5211 // It's valid to "forward-declare" #pragma weak, in which case we
5212 // have to do this.
5213 LoadExternalWeakUndeclaredIdentifiers();
5214 if (!WeakUndeclaredIdentifiers.empty()) {
5215 NamedDecl *ND = NULL;
5216 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5217 if (VD->isExternC())
5218 ND = VD;
5219 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5220 if (FD->isExternC())
5221 ND = FD;
5222 if (ND) {
5223 if (IdentifierInfo *Id = ND->getIdentifier()) {
5224 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5225 = WeakUndeclaredIdentifiers.find(Id);
5226 if (I != WeakUndeclaredIdentifiers.end()) {
5227 WeakInfo W = I->second;
5228 DeclApplyPragmaWeak(S, ND, W);
5229 WeakUndeclaredIdentifiers[Id] = W;
5230 }
5231 }
5232 }
5233 }
5234}
5235
Chris Lattner0744e5f2008-06-29 00:23:49 +00005236/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5237/// it, apply them to D. This is a bit tricky because PD can have attributes
5238/// specified in many different places, and we need to find and apply them all.
Richard Smith4a97b8e2013-08-29 00:47:48 +00005239void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner0744e5f2008-06-29 00:23:49 +00005240 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00005241 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005242 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00005243
Chris Lattner0744e5f2008-06-29 00:23:49 +00005244 // Walk the declarator structure, applying decl attributes that were in a type
5245 // position to the decl itself. This handles cases like:
5246 // int *__attr__(x)** D;
5247 // when X is a decl attribute.
5248 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5249 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005250 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00005251
Chris Lattner0744e5f2008-06-29 00:23:49 +00005252 // Finally, apply any attributes on the decl itself.
5253 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005254 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00005255}
John McCall54abf7d2009-11-04 02:18:39 +00005256
John McCallf85e1932011-06-15 23:02:42 +00005257/// Is the given declaration allowed to use a forbidden type?
5258static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5259 // Private ivars are always okay. Unfortunately, people don't
5260 // always properly make their ivars private, even in system headers.
5261 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00005262 // Function declarations in sys headers will be marked unavailable.
5263 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5264 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00005265 return false;
5266
5267 // Require it to be declared in a system header.
5268 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5269}
5270
5271/// Handle a delayed forbidden-type diagnostic.
5272static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5273 Decl *decl) {
5274 if (decl && isForbiddenTypeAllowed(S, decl)) {
5275 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5276 "this system declaration uses an unsupported type"));
5277 return;
5278 }
David Blaikie4e4d0842012-03-11 07:00:24 +00005279 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005280 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00005281 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005282 // kind of forbidden type messages on unavailable functions.
5283 if (FD->hasAttr<UnavailableAttr>() &&
5284 diag.getForbiddenTypeDiagnostic() ==
5285 diag::err_arc_array_param_no_ownership) {
5286 diag.Triggered = true;
5287 return;
5288 }
5289 }
John McCallf85e1932011-06-15 23:02:42 +00005290
5291 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5292 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5293 diag.Triggered = true;
5294}
5295
John McCall92576642012-05-07 06:16:41 +00005296void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5297 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00005298 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00005299 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00005300
John McCall92576642012-05-07 06:16:41 +00005301 // When delaying diagnostics to run in the context of a parsed
5302 // declaration, we only want to actually emit anything if parsing
5303 // succeeds.
5304 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00005305
John McCall92576642012-05-07 06:16:41 +00005306 // We emit all the active diagnostics in this pool or any of its
5307 // parents. In general, we'll get one pool for the decl spec
5308 // and a child pool for each declarator; in a decl group like:
5309 // deprecated_typedef foo, *bar, baz();
5310 // only the declarator pops will be passed decls. This is correct;
5311 // we really do need to consider delayed diagnostics from the decl spec
5312 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005313 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005314 do {
John McCall13489672012-05-07 06:16:58 +00005315 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005316 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5317 // This const_cast is a bit lame. Really, Triggered should be mutable.
5318 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005319 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005320 continue;
5321
John McCalleee1d542011-02-14 07:13:47 +00005322 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005323 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005324 // Don't bother giving deprecation diagnostics if the decl is invalid.
5325 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005326 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005327 break;
5328
5329 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005330 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005331 break;
John McCallf85e1932011-06-15 23:02:42 +00005332
5333 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005334 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005335 break;
John McCall2f514482010-01-27 03:50:35 +00005336 }
5337 }
John McCall92576642012-05-07 06:16:41 +00005338 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005339}
5340
John McCall13489672012-05-07 06:16:58 +00005341/// Given a set of delayed diagnostics, re-emit them as if they had
5342/// been delayed in the current context instead of in the given pool.
5343/// Essentially, this just moves them to the current pool.
5344void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5345 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5346 assert(curPool && "re-emitting in undelayed context not supported");
5347 curPool->steal(pool);
5348}
5349
John McCall54abf7d2009-11-04 02:18:39 +00005350static bool isDeclDeprecated(Decl *D) {
5351 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005352 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005353 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005354 // A category implicitly has the availability of the interface.
5355 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5356 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005357 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5358 return false;
5359}
5360
Eli Friedmanc3b23082012-08-08 21:52:41 +00005361static void
5362DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5363 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005364 const ObjCInterfaceDecl *UnknownObjCClass,
5365 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005366 DeclarationName Name = D->getDeclName();
5367 if (!Message.empty()) {
5368 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5369 S.Diag(D->getLocation(),
5370 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5371 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005372 if (ObjCPropery)
5373 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5374 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005375 } else if (!UnknownObjCClass) {
5376 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5377 S.Diag(D->getLocation(),
5378 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5379 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005380 if (ObjCPropery)
5381 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5382 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005383 } else {
5384 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5385 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5386 }
5387}
5388
John McCall9c3087b2010-08-26 02:13:20 +00005389void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005390 Decl *Ctx) {
5391 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005392 return;
5393
John McCall2f514482010-01-27 03:50:35 +00005394 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005395 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5396 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005397 DD.getUnknownObjCClass(),
5398 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005399}
5400
Chris Lattner5f9e2722011-07-23 10:55:15 +00005401void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005402 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005403 const ObjCInterfaceDecl *UnknownObjCClass,
5404 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005405 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005406 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005407 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5408 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005409 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005410 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005411 return;
5412 }
5413
5414 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005415 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005416 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005417 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005418}