blob: 00f8af9382bc0daff6a645ee8400f100c37d4aa0 [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) {
Aaron Ballman624421f2013-08-31 01:11:41 +0000422 Expr *ArgExp = Attr.getArgAsExpr(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
Aaron Ballman624421f2013-08-31 01:11:41 +0000818 if (!isIntOrBool(Attr.getArgAsExpr(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
Michael Han51d8c522013-01-24 16:46:58 +0000836 D->addAttr(::new (S.Context)
837 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman624421f2013-08-31 01:11:41 +0000838 Attr.getArgAsExpr(0),
839 Args.data(), Args.size(),
Michael Han51d8c522013-01-24 16:46:58 +0000840 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000841}
842
Michael Hanf1aae3b2012-08-03 17:40:43 +0000843static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000844 const AttributeList &Attr) {
845 SmallVector<Expr*, 2> Args;
846 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
847 return;
848
Michael Han51d8c522013-01-24 16:46:58 +0000849 D->addAttr(::new (S.Context)
850 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman624421f2013-08-31 01:11:41 +0000851 Attr.getArgAsExpr(0),
852 Args.data(), Args.size(),
Michael Han51d8c522013-01-24 16:46:58 +0000853 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000854}
855
Michael Hanf1aae3b2012-08-03 17:40:43 +0000856static bool checkLocksRequiredCommon(Sema &S, Decl *D,
857 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000858 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000859 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000860 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000861
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000862 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000863 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
864 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000865 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000866 }
867
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000868 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000869 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000870 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000871 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000872
Michael Handc691572012-07-23 18:48:41 +0000873 return true;
874}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000875
Michael Hanf1aae3b2012-08-03 17:40:43 +0000876static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000877 const AttributeList &Attr) {
878 SmallVector<Expr*, 1> Args;
879 if (!checkLocksRequiredCommon(S, D, Attr, Args))
880 return;
881
882 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000883 D->addAttr(::new (S.Context)
884 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
885 StartArg, Args.size(),
886 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000887}
888
Michael Hanf1aae3b2012-08-03 17:40:43 +0000889static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000890 const AttributeList &Attr) {
891 SmallVector<Expr*, 1> Args;
892 if (!checkLocksRequiredCommon(S, D, Attr, Args))
893 return;
894
895 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000896 D->addAttr(::new (S.Context)
897 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
898 StartArg, Args.size(),
899 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000900}
901
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000902static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000903 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000904 // zero or more arguments ok
905
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000906 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000907 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
908 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000909 return;
910 }
911
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000912 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000913 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000914 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000915 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000916 Expr **StartArg = Size == 0 ? 0 : &Args[0];
917
Michael Han51d8c522013-01-24 16:46:58 +0000918 D->addAttr(::new (S.Context)
919 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
920 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000921}
922
923static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000924 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000925 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000926 return;
927
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000928 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000929 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
930 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000931 return;
932 }
933
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000934 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000935 SmallVector<Expr*, 1> Args;
936 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
937 unsigned Size = Args.size();
938 if (Size == 0)
939 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000940
Michael Han51d8c522013-01-24 16:46:58 +0000941 D->addAttr(::new (S.Context)
942 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
943 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000944}
945
946static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000947 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000948 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000949 return;
950
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000951 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000952 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
953 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000954 return;
955 }
956
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000957 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000958 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000959 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000960 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000961 if (Size == 0)
962 return;
963 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000964
Michael Han51d8c522013-01-24 16:46:58 +0000965 D->addAttr(::new (S.Context)
966 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
967 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000968}
969
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +0000970static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +0000971 if (!checkAttributeNumArgs(S, Attr, 0)) return;
972
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +0000973 if (!isa<CXXRecordDecl>(D)) {
974 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
975 Attr.getName() << ExpectedClass;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +0000976 return;
977 }
978
979 D->addAttr(::new (S.Context)
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +0000980 ConsumableAttr(Attr.getRange(), S.Context,
981 Attr.getAttributeSpellingListIndex()));
982}
983
984static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
985 const AttributeList &Attr) {
986 ASTContext &CurrContext = S.getASTContext();
987 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
988
989 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
990 if (!RD->hasAttr<ConsumableAttr>()) {
991 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
992 RD->getNameAsString();
993
994 return false;
995 }
996 }
997
998 return true;
999}
1000
1001static void handleConsumesAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1002 if (!checkAttributeNumArgs(S, Attr, 0)) return;
1003
1004 if (!isa<CXXMethodDecl>(D)) {
1005 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1006 Attr.getName() << ExpectedMethod;
1007 return;
1008 }
1009
1010 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1011 return;
1012
1013 D->addAttr(::new (S.Context)
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001014 ConsumesAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001015 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001016}
1017
1018static void handleCallableWhenUnconsumedAttr(Sema &S, Decl *D,
1019 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001020 if (!checkAttributeNumArgs(S, Attr, 0)) return;
1021
1022 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001023 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1024 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001025 return;
1026 }
1027
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001028 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1029 return;
1030
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001031 D->addAttr(::new (S.Context)
1032 CallableWhenUnconsumedAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001033 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001034}
1035
1036static void handleTestsConsumedAttr(Sema &S, Decl *D,
1037 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001038 if (!checkAttributeNumArgs(S, Attr, 0)) return;
1039
1040 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001041 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1042 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001043 return;
1044 }
1045
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001046 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1047 return;
1048
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001049 D->addAttr(::new (S.Context)
1050 TestsConsumedAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001051 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001052}
1053
1054static void handleTestsUnconsumedAttr(Sema &S, Decl *D,
1055 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001056 if (!checkAttributeNumArgs(S, Attr, 0)) return;
1057
1058 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001059 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1060 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001061 return;
1062 }
1063
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001064 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1065 return;
1066
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001067 D->addAttr(::new (S.Context)
1068 TestsUnconsumedAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001069 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001070}
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00001071
Chandler Carruth1b03c872011-07-02 00:01:44 +00001072static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1073 const AttributeList &Attr) {
Richard Smitha4fa9002013-01-13 02:11:23 +00001074 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1075 if (TD == 0) {
1076 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1077 // and type-ids.
Chris Lattner803d0802008-06-29 00:43:07 +00001078 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +00001079 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001080 }
Mike Stumpbf916502009-07-24 19:02:52 +00001081
Richard Smitha4fa9002013-01-13 02:11:23 +00001082 // Remember this typedef decl, we will need it later for diagnostics.
1083 S.ExtVectorDecls.push_back(TD);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001084}
1085
Chandler Carruth1b03c872011-07-02 00:01:44 +00001086static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001087 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001088 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001089 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001090
Chandler Carruth87c44602011-07-01 23:49:12 +00001091 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001092 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +00001093 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001094 // If the alignment is less than or equal to 8 bits, the packed attribute
1095 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +00001096 if (!FD->getType()->isDependentType() &&
1097 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +00001098 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001099 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001100 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001101 else
Michael Han51d8c522013-01-24 16:46:58 +00001102 FD->addAttr(::new (S.Context)
1103 PackedAttr(Attr.getRange(), S.Context,
1104 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001105 } else
Chris Lattner3c73c412008-11-19 08:23:25 +00001106 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001107}
1108
Chandler Carruth1b03c872011-07-02 00:01:44 +00001109static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +00001110 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001111 RD->addAttr(::new (S.Context)
1112 MsStructAttr(Attr.getRange(), S.Context,
1113 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +00001114 else
1115 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1116}
1117
Chandler Carruth1b03c872011-07-02 00:01:44 +00001118static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +00001119 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001120 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +00001121 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001122
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001123 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +00001124 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001125 if (MD->isInstanceMethod()) {
Michael Han51d8c522013-01-24 16:46:58 +00001126 D->addAttr(::new (S.Context)
1127 IBActionAttr(Attr.getRange(), S.Context,
1128 Attr.getAttributeSpellingListIndex()));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001129 return;
1130 }
1131
Ted Kremenek4ee2bb12011-02-04 06:54:16 +00001132 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001133}
1134
Ted Kremenek2f041d02011-09-29 07:02:25 +00001135static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1136 // The IBOutlet/IBOutletCollection attributes only apply to instance
1137 // variables or properties of Objective-C classes. The outlet must also
1138 // have an object reference type.
1139 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1140 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001141 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001142 << Attr.getName() << VD->getType() << 0;
1143 return false;
1144 }
1145 }
1146 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1147 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001148 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001149 << Attr.getName() << PD->getType() << 1;
1150 return false;
1151 }
1152 }
1153 else {
1154 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1155 return false;
1156 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001157
Ted Kremenek2f041d02011-09-29 07:02:25 +00001158 return true;
1159}
1160
Chandler Carruth1b03c872011-07-02 00:01:44 +00001161static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001162 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001163 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001164 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001165
1166 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001167 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001168
Michael Han51d8c522013-01-24 16:46:58 +00001169 D->addAttr(::new (S.Context)
1170 IBOutletAttr(Attr.getRange(), S.Context,
1171 Attr.getAttributeSpellingListIndex()));
Ted Kremenek96329d42008-07-15 22:26:48 +00001172}
1173
Chandler Carruth1b03c872011-07-02 00:01:44 +00001174static void handleIBOutletCollection(Sema &S, Decl *D,
1175 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001176
1177 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00001178 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001179 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1180 << Attr.getName() << 1;
Ted Kremenek857e9182010-05-19 17:38:06 +00001181 return;
1182 }
1183
Ted Kremenek2f041d02011-09-29 07:02:25 +00001184 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001185 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001186
Aaron Ballman624421f2013-08-31 01:11:41 +00001187 IdentifierLoc *IL = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
1188 IdentifierInfo *II;
1189 SourceLocation ILS;
1190 if (IL) {
1191 II = IL->Ident;
1192 ILS = IL->Loc;
1193 } else {
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001194 II = &S.Context.Idents.get("NSObject");
Aaron Ballman624421f2013-08-31 01:11:41 +00001195 }
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001196
John McCallb3d87482010-08-24 05:47:05 +00001197 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001198 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001199 if (!TypeRep) {
1200 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1201 return;
1202 }
John McCallb3d87482010-08-24 05:47:05 +00001203 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001204 // Diagnose use of non-object type in iboutletcollection attribute.
1205 // FIXME. Gnu attribute extension ignores use of builtin types in
1206 // attributes. So, __attribute__((iboutletcollection(char))) will be
1207 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001208 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001209 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1210 return;
1211 }
Michael Han51d8c522013-01-24 16:46:58 +00001212 D->addAttr(::new (S.Context)
Aaron Ballman624421f2013-08-31 01:11:41 +00001213 IBOutletCollectionAttr(Attr.getRange(), S.Context, QT, ILS,
Michael Han51d8c522013-01-24 16:46:58 +00001214 Attr.getAttributeSpellingListIndex()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001215}
1216
Chandler Carruthd309c812011-07-01 23:49:16 +00001217static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001218 if (const RecordType *UT = T->getAsUnionType())
1219 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1220 RecordDecl *UD = UT->getDecl();
1221 for (RecordDecl::field_iterator it = UD->field_begin(),
1222 itend = UD->field_end(); it != itend; ++it) {
1223 QualType QT = it->getType();
1224 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1225 T = QT;
1226 return;
1227 }
1228 }
1229 }
1230}
1231
Nuno Lopes587de5b2012-05-24 00:22:00 +00001232static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001233 if (!isFunctionOrMethod(D)) {
1234 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001235 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopes174930d2012-06-18 16:39:04 +00001236 return;
1237 }
1238
Nuno Lopes587de5b2012-05-24 00:22:00 +00001239 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1240 return;
1241
Nuno Lopes587de5b2012-05-24 00:22:00 +00001242 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001243 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001244 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001245 uint64_t Idx;
1246 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1247 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes587de5b2012-05-24 00:22:00 +00001248 return;
Nuno Lopes587de5b2012-05-24 00:22:00 +00001249
1250 // check if the function argument is of an integer type
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001251 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001252 if (!T->isIntegerType()) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00001253 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1254 << Attr.getName() << AANT_ArgumentIntegerConstant
1255 << Ex->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001256 return;
1257 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001258 SizeArgs.push_back(Idx);
Nuno Lopes587de5b2012-05-24 00:22:00 +00001259 }
1260
1261 // check if the function returns a pointer
1262 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1263 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001264 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001265 }
1266
Michael Han51d8c522013-01-24 16:46:58 +00001267 D->addAttr(::new (S.Context)
1268 AllocSizeAttr(Attr.getRange(), S.Context,
1269 SizeArgs.data(), SizeArgs.size(),
1270 Attr.getAttributeSpellingListIndex()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001271}
1272
Chandler Carruth1b03c872011-07-02 00:01:44 +00001273static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001274 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1275 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001276 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001277 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001278 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001279 return;
1280 }
Mike Stumpbf916502009-07-24 19:02:52 +00001281
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001282 SmallVector<unsigned, 8> NonNullArgs;
1283 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001284 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001285 uint64_t Idx;
1286 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1287 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001288 return;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001289
1290 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001291 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001292 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001293
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001294 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001295 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001296 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001297 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001298 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001299 }
Mike Stumpbf916502009-07-24 19:02:52 +00001300
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001301 NonNullArgs.push_back(Idx);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001302 }
Mike Stumpbf916502009-07-24 19:02:52 +00001303
1304 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1305 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001306 if (NonNullArgs.empty()) {
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001307 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1308 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001309 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001310 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001311 NonNullArgs.push_back(i);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001312 }
Mike Stumpbf916502009-07-24 19:02:52 +00001313
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001314 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001315 if (NonNullArgs.empty()) {
1316 // Warn the trivial case only if attribute is not coming from a
1317 // macro instantiation.
1318 if (Attr.getLoc().isFileID())
1319 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001320 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001321 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001322 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001323
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001324 unsigned *start = &NonNullArgs[0];
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001325 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001326 llvm::array_pod_sort(start, start + size);
Michael Han51d8c522013-01-24 16:46:58 +00001327 D->addAttr(::new (S.Context)
1328 NonNullAttr(Attr.getRange(), S.Context, start, size,
1329 Attr.getAttributeSpellingListIndex()));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001330}
1331
Chandler Carruth1b03c872011-07-02 00:01:44 +00001332static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001333 // This attribute must be applied to a function declaration.
1334 // The first argument to the attribute must be a string,
1335 // the name of the resource, for example "malloc".
1336 // The following arguments must be argument indexes, the arguments must be
1337 // of integer type for Returns, otherwise of pointer type.
1338 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001339 // after being held. free() should be __attribute((ownership_takes)), whereas
1340 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001341
Aaron Ballman624421f2013-08-31 01:11:41 +00001342 if (!AL.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001343 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001344 << AL.getName()->getName() << 1 << AANT_ArgumentString;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001345 return;
1346 }
1347 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001348 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001349 switch (AL.getKind()) {
1350 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001351 K = OwnershipAttr::Takes;
Aaron Ballman624421f2013-08-31 01:11:41 +00001352 if (AL.getNumArgs() < 2) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001353 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1354 << AL.getName() << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001355 return;
1356 }
Jordy Rose2a479922010-08-12 08:54:03 +00001357 break;
1358 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001359 K = OwnershipAttr::Holds;
Aaron Ballman624421f2013-08-31 01:11:41 +00001360 if (AL.getNumArgs() < 2) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001361 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1362 << AL.getName() << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001363 return;
1364 }
Jordy Rose2a479922010-08-12 08:54:03 +00001365 break;
1366 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001367 K = OwnershipAttr::Returns;
Aaron Ballman624421f2013-08-31 01:11:41 +00001368 if (AL.getNumArgs() > 2) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001369 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
Aaron Ballman624421f2013-08-31 01:11:41 +00001370 << AL.getName() << AL.getNumArgs() + 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001371 return;
1372 }
Jordy Rose2a479922010-08-12 08:54:03 +00001373 break;
1374 default:
1375 // This should never happen given how we are called.
1376 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001377 }
1378
Chandler Carruth87c44602011-07-01 23:49:12 +00001379 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001380 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1381 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001382 return;
1383 }
1384
Aaron Ballman624421f2013-08-31 01:11:41 +00001385 StringRef Module = AL.getArgAsIdent(0)->Ident->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001386
1387 // Normalize the argument, __foo__ becomes foo.
1388 if (Module.startswith("__") && Module.endswith("__"))
1389 Module = Module.substr(2, Module.size() - 4);
1390
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001391
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001392 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman624421f2013-08-31 01:11:41 +00001393 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1394 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001395 uint64_t Idx;
1396 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
Aaron Ballman624421f2013-08-31 01:11:41 +00001397 AL.getLoc(), i, Ex, Idx))
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001398 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001399
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001400 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001401 case OwnershipAttr::Takes:
1402 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001403 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001404 QualType T = getFunctionOrMethodArgType(D, Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001405 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1406 // FIXME: Should also highlight argument in decl.
1407 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001408 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001409 << "pointer"
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001410 << Ex->getSourceRange();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001411 continue;
1412 }
1413 break;
1414 }
Sean Huntcf807c42010-08-18 23:23:40 +00001415 case OwnershipAttr::Returns: {
Aaron Ballman624421f2013-08-31 01:11:41 +00001416 if (AL.getNumArgs() > 2) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001417 // Is the function argument an integer type?
Aaron Ballman624421f2013-08-31 01:11:41 +00001418 Expr *IdxExpr = AL.getArgAsExpr(1);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001419 llvm::APSInt ArgNum(32);
1420 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1421 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1422 S.Diag(AL.getLoc(), diag::err_ownership_type)
1423 << "ownership_returns" << "integer"
1424 << IdxExpr->getSourceRange();
1425 return;
1426 }
1427 }
1428 break;
1429 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001430 } // switch
1431
1432 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001433 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001434 i = D->specific_attr_begin<OwnershipAttr>(),
1435 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001436 i != e; ++i) {
1437 if ((*i)->getOwnKind() != K) {
1438 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1439 I!=E; ++I) {
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001440 if (Idx == *I) {
Sean Huntcf807c42010-08-18 23:23:40 +00001441 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1442 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001443 }
1444 }
1445 }
1446 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001447 OwnershipArgs.push_back(Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001448 }
1449
1450 unsigned* start = OwnershipArgs.data();
1451 unsigned size = OwnershipArgs.size();
1452 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001453
1454 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001455 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1456 << AL.getName() << 2;
Sean Huntcf807c42010-08-18 23:23:40 +00001457 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001458 }
Sean Huntcf807c42010-08-18 23:23:40 +00001459
Michael Han51d8c522013-01-24 16:46:58 +00001460 D->addAttr(::new (S.Context)
1461 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1462 AL.getAttributeSpellingListIndex()));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001463}
1464
Chandler Carruth1b03c872011-07-02 00:01:44 +00001465static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001466 // Check the attribute arguments.
1467 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001468 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1469 << Attr.getName() << 1;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001470 return;
1471 }
1472
Chandler Carruth87c44602011-07-01 23:49:12 +00001473 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001474 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001475 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001476 return;
1477 }
1478
Chandler Carruth87c44602011-07-01 23:49:12 +00001479 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001480
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001481 // gcc rejects
1482 // class c {
1483 // static int a __attribute__((weakref ("v2")));
1484 // static int b() __attribute__((weakref ("f3")));
1485 // };
1486 // and ignores the attributes of
1487 // void f(void) {
1488 // static int a __attribute__((weakref ("v2")));
1489 // }
1490 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001491 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001492 if (!Ctx->isFileContext()) {
1493 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001494 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001495 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001496 }
1497
1498 // The GCC manual says
1499 //
1500 // At present, a declaration to which `weakref' is attached can only
1501 // be `static'.
1502 //
1503 // It also says
1504 //
1505 // Without a TARGET,
1506 // given as an argument to `weakref' or to `alias', `weakref' is
1507 // equivalent to `weak'.
1508 //
1509 // gcc 4.4.1 will accept
1510 // int a7 __attribute__((weakref));
1511 // as
1512 // int a7 __attribute__((weak));
1513 // This looks like a bug in gcc. We reject that for now. We should revisit
1514 // it if this behaviour is actually used.
1515
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001516 // GCC rejects
1517 // static ((alias ("y"), weakref)).
1518 // Should we? How to check that weakref is before or after alias?
1519
Aaron Ballman624421f2013-08-31 01:11:41 +00001520 if (Attr.isArgExpr(0)) {
1521 Expr *Arg = Attr.getArgAsExpr(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001522 Arg = Arg->IgnoreParenCasts();
1523 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1524
Douglas Gregor5cee1192011-07-27 05:40:30 +00001525 if (!Str || !Str->isAscii()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001526 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001527 << Attr.getName() << 1 << AANT_ArgumentString;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001528 return;
1529 }
1530 // GCC will accept anything as the argument of weakref. Should we
1531 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001532 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001533 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001534 }
1535
Michael Han51d8c522013-01-24 16:46:58 +00001536 D->addAttr(::new (S.Context)
1537 WeakRefAttr(Attr.getRange(), S.Context,
1538 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001539}
1540
Chandler Carruth1b03c872011-07-02 00:01:44 +00001541static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001542 // check the attribute arguments.
Aaron Ballmanffa9d572013-07-18 18:01:48 +00001543 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001544 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001545
Aaron Ballman624421f2013-08-31 01:11:41 +00001546 StringLiteral *Str = 0;
1547 if (Attr.isArgExpr(0))
1548 Str = dyn_cast<StringLiteral>(Attr.getArgAsExpr(0)->IgnoreParenCasts());
Mike Stumpbf916502009-07-24 19:02:52 +00001549
Douglas Gregor5cee1192011-07-27 05:40:30 +00001550 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001551 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1552 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001553 return;
1554 }
Mike Stumpbf916502009-07-24 19:02:52 +00001555
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001556 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001557 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1558 return;
1559 }
1560
Chris Lattner6b6b5372008-06-26 18:38:35 +00001561 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001562
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001563 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00001564 Str->getString(),
1565 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001566}
1567
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001568static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1569 // Check the attribute arguments.
1570 if (!checkAttributeNumArgs(S, Attr, 0))
1571 return;
1572
1573 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1574 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1575 << Attr.getName() << ExpectedFunctionOrMethod;
1576 return;
1577 }
1578
Michael Han51d8c522013-01-24 16:46:58 +00001579 D->addAttr(::new (S.Context)
1580 MinSizeAttr(Attr.getRange(), S.Context,
1581 Attr.getAttributeSpellingListIndex()));
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001582}
1583
Benjamin Krameree409a92012-05-12 21:10:52 +00001584static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1585 // Check the attribute arguments.
1586 if (!checkAttributeNumArgs(S, Attr, 0))
1587 return;
1588
1589 if (!isa<FunctionDecl>(D)) {
1590 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1591 << Attr.getName() << ExpectedFunction;
1592 return;
1593 }
1594
1595 if (D->hasAttr<HotAttr>()) {
1596 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1597 << Attr.getName() << "hot";
1598 return;
1599 }
1600
Michael Han51d8c522013-01-24 16:46:58 +00001601 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1602 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001603}
1604
1605static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1606 // Check the attribute arguments.
1607 if (!checkAttributeNumArgs(S, Attr, 0))
1608 return;
1609
1610 if (!isa<FunctionDecl>(D)) {
1611 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1612 << Attr.getName() << ExpectedFunction;
1613 return;
1614 }
1615
1616 if (D->hasAttr<ColdAttr>()) {
1617 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1618 << Attr.getName() << "cold";
1619 return;
1620 }
1621
Michael Han51d8c522013-01-24 16:46:58 +00001622 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1623 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001624}
1625
Chandler Carruth1b03c872011-07-02 00:01:44 +00001626static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001627 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001628 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001629 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001630
Chandler Carruth87c44602011-07-01 23:49:12 +00001631 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001632 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001633 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001634 return;
1635 }
1636
Michael Han51d8c522013-01-24 16:46:58 +00001637 D->addAttr(::new (S.Context)
1638 NakedAttr(Attr.getRange(), S.Context,
1639 Attr.getAttributeSpellingListIndex()));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001640}
1641
Chandler Carruth1b03c872011-07-02 00:01:44 +00001642static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1643 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001644 // Check the attribute arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00001645 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001646 return;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001647
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
Aaron Ballman624421f2013-08-31 01:11:41 +00001665 Expr *Arg = Attr.getArgAsExpr(0);
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001666 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.
Aaron Ballman624421f2013-08-31 01:11:41 +00001697 if (!checkAttributeNumArgs(S, Attr, 0))
Ryan Flynn76168e22009-08-09 20:07:29 +00001698 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001699
Chandler Carruth87c44602011-07-01 23:49:12 +00001700 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001701 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001702 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han51d8c522013-01-24 16:46:58 +00001703 D->addAttr(::new (S.Context)
1704 MallocAttr(Attr.getRange(), S.Context,
1705 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001706 return;
1707 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001708 }
1709
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001710 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001711}
1712
Chandler Carruth1b03c872011-07-02 00:01:44 +00001713static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001714 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001715 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001716 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001717
Michael Han51d8c522013-01-24 16:46:58 +00001718 D->addAttr(::new (S.Context)
1719 MayAliasAttr(Attr.getRange(), S.Context,
1720 Attr.getAttributeSpellingListIndex()));
Dan Gohman34c26302010-11-17 00:03:07 +00001721}
1722
Chandler Carruth1b03c872011-07-02 00:01:44 +00001723static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001724 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001725 D->addAttr(::new (S.Context)
1726 NoCommonAttr(Attr.getRange(), S.Context,
1727 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001728 else
1729 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001730 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001731}
1732
Chandler Carruth1b03c872011-07-02 00:01:44 +00001733static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman3e1aca22013-06-20 22:55:04 +00001734 if (S.LangOpts.CPlusPlus) {
1735 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1736 return;
1737 }
1738
Chandler Carruth87c44602011-07-01 23:49:12 +00001739 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001740 D->addAttr(::new (S.Context)
1741 CommonAttr(Attr.getRange(), S.Context,
1742 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001743 else
1744 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001745 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001746}
1747
Chandler Carruth1b03c872011-07-02 00:01:44 +00001748static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001749 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001750
1751 if (S.CheckNoReturnAttr(attr)) return;
1752
Chandler Carruth87c44602011-07-01 23:49:12 +00001753 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001754 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001755 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001756 return;
1757 }
1758
Michael Han51d8c522013-01-24 16:46:58 +00001759 D->addAttr(::new (S.Context)
1760 NoReturnAttr(attr.getRange(), S.Context,
1761 attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00001762}
1763
1764bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001765 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall711c52b2011-01-05 12:14:39 +00001766 attr.setInvalid();
1767 return true;
1768 }
1769
1770 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001771}
1772
Chandler Carruth1b03c872011-07-02 00:01:44 +00001773static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1774 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001775
1776 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1777 // because 'analyzer_noreturn' does not impact the type.
1778
Chandler Carruth1731e202011-07-11 23:30:35 +00001779 if(!checkAttributeNumArgs(S, Attr, 0))
1780 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001781
Chandler Carruth87c44602011-07-01 23:49:12 +00001782 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1783 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001784 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1785 && !VD->getType()->isFunctionPointerType())) {
1786 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001787 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001788 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001789 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001790 return;
1791 }
1792 }
1793
Michael Han51d8c522013-01-24 16:46:58 +00001794 D->addAttr(::new (S.Context)
1795 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1796 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001797}
1798
Richard Smithcd8ab512013-01-17 01:30:42 +00001799static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1800 const AttributeList &Attr) {
1801 // C++11 [dcl.attr.noreturn]p1:
1802 // The attribute may be applied to the declarator-id in a function
1803 // declaration.
1804 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1805 if (!FD) {
1806 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1807 << Attr.getName() << ExpectedFunctionOrMethod;
1808 return;
1809 }
1810
Michael Han51d8c522013-01-24 16:46:58 +00001811 D->addAttr(::new (S.Context)
1812 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1813 Attr.getAttributeSpellingListIndex()));
Richard Smithcd8ab512013-01-17 01:30:42 +00001814}
1815
John Thompson35cc9622010-08-09 21:53:52 +00001816// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001817static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001818/*
1819 Returning a Vector Class in Registers
1820
Eric Christopherf48f3672010-12-01 22:13:54 +00001821 According to the PPU ABI specifications, a class with a single member of
1822 vector type is returned in memory when used as the return value of a function.
1823 This results in inefficient code when implementing vector classes. To return
1824 the value in a single vector register, add the vecreturn attribute to the
1825 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001826
1827 Example:
1828
1829 struct Vector
1830 {
1831 __vector float xyzw;
1832 } __attribute__((vecreturn));
1833
1834 Vector Add(Vector lhs, Vector rhs)
1835 {
1836 Vector result;
1837 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1838 return result; // This will be returned in a register
1839 }
1840*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001841 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001842 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001843 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001844 return;
1845 }
1846
Chandler Carruth87c44602011-07-01 23:49:12 +00001847 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001848 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1849 return;
1850 }
1851
Chandler Carruth87c44602011-07-01 23:49:12 +00001852 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001853 int count = 0;
1854
1855 if (!isa<CXXRecordDecl>(record)) {
1856 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1857 return;
1858 }
1859
1860 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1861 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1862 return;
1863 }
1864
Eric Christopherf48f3672010-12-01 22:13:54 +00001865 for (RecordDecl::field_iterator iter = record->field_begin();
1866 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001867 if ((count == 1) || !iter->getType()->isVectorType()) {
1868 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1869 return;
1870 }
1871 count++;
1872 }
1873
Michael Han51d8c522013-01-24 16:46:58 +00001874 D->addAttr(::new (S.Context)
1875 VecReturnAttr(Attr.getRange(), S.Context,
1876 Attr.getAttributeSpellingListIndex()));
John Thompson35cc9622010-08-09 21:53:52 +00001877}
1878
Richard Smith3a2b7a12013-01-28 22:42:45 +00001879static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1880 const AttributeList &Attr) {
1881 if (isa<ParmVarDecl>(D)) {
1882 // [[carries_dependency]] can only be applied to a parameter if it is a
1883 // parameter of a function declaration or lambda.
1884 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1885 S.Diag(Attr.getLoc(),
1886 diag::err_carries_dependency_param_not_function_decl);
1887 return;
1888 }
1889 } else if (!isa<FunctionDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001890 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001891 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001892 return;
1893 }
Richard Smith3a2b7a12013-01-28 22:42:45 +00001894
1895 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1896 Attr.getRange(), S.Context,
1897 Attr.getAttributeSpellingListIndex()));
Sean Huntbbd37c62009-11-21 08:43:09 +00001898}
1899
Chandler Carruth1b03c872011-07-02 00:01:44 +00001900static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001901 // check the attribute arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00001902 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek73798892008-07-25 04:39:19 +00001903 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001904
Chandler Carruth87c44602011-07-01 23:49:12 +00001905 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001906 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001907 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001908 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001909 return;
1910 }
Mike Stumpbf916502009-07-24 19:02:52 +00001911
Michael Han51d8c522013-01-24 16:46:58 +00001912 D->addAttr(::new (S.Context)
1913 UnusedAttr(Attr.getRange(), S.Context,
1914 Attr.getAttributeSpellingListIndex()));
Ted Kremenek73798892008-07-25 04:39:19 +00001915}
1916
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001917static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1918 const AttributeList &Attr) {
1919 // check the attribute arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00001920 if (!checkAttributeNumArgs(S, Attr, 0))
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001921 return;
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001922
1923 if (!isa<FunctionDecl>(D)) {
1924 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1925 << Attr.getName() << ExpectedFunction;
1926 return;
1927 }
1928
Michael Han51d8c522013-01-24 16:46:58 +00001929 D->addAttr(::new (S.Context)
1930 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1931 Attr.getAttributeSpellingListIndex()));
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001932}
1933
Chandler Carruth1b03c872011-07-02 00:01:44 +00001934static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001935 // check the attribute arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00001936 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001937 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001938
Chandler Carruth87c44602011-07-01 23:49:12 +00001939 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola29535ba2013-08-16 23:18:50 +00001940 if (VD->hasLocalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001941 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1942 return;
1943 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001944 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001945 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001946 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001947 return;
1948 }
Mike Stumpbf916502009-07-24 19:02:52 +00001949
Michael Han51d8c522013-01-24 16:46:58 +00001950 D->addAttr(::new (S.Context)
1951 UsedAttr(Attr.getRange(), S.Context,
1952 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001953}
1954
Chandler Carruth1b03c872011-07-02 00:01:44 +00001955static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001956 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001957 if (Attr.getNumArgs() > 1) {
1958 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001959 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001960 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001961
1962 int priority = 65535; // FIXME: Do not hardcode such constants.
1963 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001964 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001965 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001966 if (E->isTypeDependent() || E->isValueDependent() ||
1967 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001968 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001969 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00001970 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001971 return;
1972 }
1973 priority = Idx.getZExtValue();
1974 }
Mike Stumpbf916502009-07-24 19:02:52 +00001975
Chandler Carruth87c44602011-07-01 23:49:12 +00001976 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001977 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001978 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001979 return;
1980 }
1981
Michael Han51d8c522013-01-24 16:46:58 +00001982 D->addAttr(::new (S.Context)
1983 ConstructorAttr(Attr.getRange(), S.Context, priority,
1984 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001985}
1986
Chandler Carruth1b03c872011-07-02 00:01:44 +00001987static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001988 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001989 if (Attr.getNumArgs() > 1) {
1990 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001991 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001992 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001993
1994 int priority = 65535; // FIXME: Do not hardcode such constants.
1995 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001996 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001997 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001998 if (E->isTypeDependent() || E->isValueDependent() ||
1999 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002000 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002001 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002002 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002003 return;
2004 }
2005 priority = Idx.getZExtValue();
2006 }
Mike Stumpbf916502009-07-24 19:02:52 +00002007
Chandler Carruth87c44602011-07-01 23:49:12 +00002008 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002009 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002010 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002011 return;
2012 }
2013
Michael Han51d8c522013-01-24 16:46:58 +00002014 D->addAttr(::new (S.Context)
2015 DestructorAttr(Attr.getRange(), S.Context, priority,
2016 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002017}
2018
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002019template <typename AttrTy>
Aaron Ballman2dbdef22013-07-18 13:13:52 +00002020static void handleAttrWithMessage(Sema &S, Decl *D,
2021 const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00002022 unsigned NumArgs = Attr.getNumArgs();
2023 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00002024 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002025 return;
2026 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002027
2028 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002029 StringRef Str;
Aaron Ballman624421f2013-08-31 01:11:41 +00002030 if (Attr.isArgExpr(0)) {
2031 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArgAsExpr(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002032 if (!SE) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002033 S.Diag(Attr.getArgAsExpr(0)->getLocStart(),
2034 diag::err_attribute_argument_type) << Attr.getName()
2035 << AANT_ArgumentString;
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002036 return;
2037 }
Chris Lattner951bbb22011-02-24 05:42:24 +00002038 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002039 }
Mike Stumpbf916502009-07-24 19:02:52 +00002040
Michael Han51d8c522013-01-24 16:46:58 +00002041 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2042 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00002043}
2044
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002045static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2046 const AttributeList &Attr) {
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002047 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002048 return;
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002049
Michael Han51d8c522013-01-24 16:46:58 +00002050 D->addAttr(::new (S.Context)
2051 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2052 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002053}
2054
Patrick Beardb2f68202012-04-06 18:12:22 +00002055static void handleObjCRootClassAttr(Sema &S, Decl *D,
2056 const AttributeList &Attr) {
2057 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002058 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2059 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardb2f68202012-04-06 18:12:22 +00002060 return;
2061 }
2062
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002063 if (!checkAttributeNumArgs(S, Attr, 0))
Patrick Beardb2f68202012-04-06 18:12:22 +00002064 return;
Patrick Beardb2f68202012-04-06 18:12:22 +00002065
Michael Han51d8c522013-01-24 16:46:58 +00002066 D->addAttr(::new (S.Context)
2067 ObjCRootClassAttr(Attr.getRange(), S.Context,
2068 Attr.getAttributeSpellingListIndex()));
Patrick Beardb2f68202012-04-06 18:12:22 +00002069}
2070
Michael Han51d8c522013-01-24 16:46:58 +00002071static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2072 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00002073 if (!isa<ObjCInterfaceDecl>(D)) {
2074 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2075 return;
2076 }
2077
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002078 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002079 return;
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002080
Michael Han51d8c522013-01-24 16:46:58 +00002081 D->addAttr(::new (S.Context)
2082 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2083 Attr.getAttributeSpellingListIndex()));
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002084}
2085
Jordy Rosefad5de92012-05-08 03:27:22 +00002086static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2087 IdentifierInfo *Platform,
2088 VersionTuple Introduced,
2089 VersionTuple Deprecated,
2090 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002091 StringRef PlatformName
2092 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2093 if (PlatformName.empty())
2094 PlatformName = Platform->getName();
2095
2096 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2097 // of these steps are needed).
2098 if (!Introduced.empty() && !Deprecated.empty() &&
2099 !(Introduced <= Deprecated)) {
2100 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2101 << 1 << PlatformName << Deprecated.getAsString()
2102 << 0 << Introduced.getAsString();
2103 return true;
2104 }
2105
2106 if (!Introduced.empty() && !Obsoleted.empty() &&
2107 !(Introduced <= Obsoleted)) {
2108 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2109 << 2 << PlatformName << Obsoleted.getAsString()
2110 << 0 << Introduced.getAsString();
2111 return true;
2112 }
2113
2114 if (!Deprecated.empty() && !Obsoleted.empty() &&
2115 !(Deprecated <= Obsoleted)) {
2116 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2117 << 2 << PlatformName << Obsoleted.getAsString()
2118 << 1 << Deprecated.getAsString();
2119 return true;
2120 }
2121
2122 return false;
2123}
2124
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002125/// \brief Check whether the two versions match.
2126///
2127/// If either version tuple is empty, then they are assumed to match. If
2128/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2129static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2130 bool BeforeIsOkay) {
2131 if (X.empty() || Y.empty())
2132 return true;
2133
2134 if (X == Y)
2135 return true;
2136
2137 if (BeforeIsOkay && X < Y)
2138 return true;
2139
2140 return false;
2141}
2142
Rafael Espindola51be6e32013-01-08 22:04:34 +00002143AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002144 IdentifierInfo *Platform,
2145 VersionTuple Introduced,
2146 VersionTuple Deprecated,
2147 VersionTuple Obsoleted,
2148 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002149 StringRef Message,
Michael Han51d8c522013-01-24 16:46:58 +00002150 bool Override,
2151 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002152 VersionTuple MergedIntroduced = Introduced;
2153 VersionTuple MergedDeprecated = Deprecated;
2154 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002155 bool FoundAny = false;
2156
Rafael Espindola98ae8342012-05-10 02:50:16 +00002157 if (D->hasAttrs()) {
2158 AttrVec &Attrs = D->getAttrs();
2159 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2160 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2161 if (!OldAA) {
2162 ++i;
2163 continue;
2164 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002165
Rafael Espindola98ae8342012-05-10 02:50:16 +00002166 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2167 if (OldPlatform != Platform) {
2168 ++i;
2169 continue;
2170 }
2171
2172 FoundAny = true;
2173 VersionTuple OldIntroduced = OldAA->getIntroduced();
2174 VersionTuple OldDeprecated = OldAA->getDeprecated();
2175 VersionTuple OldObsoleted = OldAA->getObsoleted();
2176 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002177
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002178 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2179 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2180 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2181 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002182 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002183 if (Override) {
2184 int Which = -1;
2185 VersionTuple FirstVersion;
2186 VersionTuple SecondVersion;
2187 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2188 Which = 0;
2189 FirstVersion = OldIntroduced;
2190 SecondVersion = Introduced;
2191 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2192 Which = 1;
2193 FirstVersion = Deprecated;
2194 SecondVersion = OldDeprecated;
2195 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2196 Which = 2;
2197 FirstVersion = Obsoleted;
2198 SecondVersion = OldObsoleted;
2199 }
2200
2201 if (Which == -1) {
2202 Diag(OldAA->getLocation(),
2203 diag::warn_mismatched_availability_override_unavail)
2204 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2205 } else {
2206 Diag(OldAA->getLocation(),
2207 diag::warn_mismatched_availability_override)
2208 << Which
2209 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2210 << FirstVersion.getAsString() << SecondVersion.getAsString();
2211 }
2212 Diag(Range.getBegin(), diag::note_overridden_method);
2213 } else {
2214 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2215 Diag(Range.getBegin(), diag::note_previous_attribute);
2216 }
2217
Rafael Espindola98ae8342012-05-10 02:50:16 +00002218 Attrs.erase(Attrs.begin() + i);
2219 --e;
2220 continue;
2221 }
2222
2223 VersionTuple MergedIntroduced2 = MergedIntroduced;
2224 VersionTuple MergedDeprecated2 = MergedDeprecated;
2225 VersionTuple MergedObsoleted2 = MergedObsoleted;
2226
2227 if (MergedIntroduced2.empty())
2228 MergedIntroduced2 = OldIntroduced;
2229 if (MergedDeprecated2.empty())
2230 MergedDeprecated2 = OldDeprecated;
2231 if (MergedObsoleted2.empty())
2232 MergedObsoleted2 = OldObsoleted;
2233
2234 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2235 MergedIntroduced2, MergedDeprecated2,
2236 MergedObsoleted2)) {
2237 Attrs.erase(Attrs.begin() + i);
2238 --e;
2239 continue;
2240 }
2241
2242 MergedIntroduced = MergedIntroduced2;
2243 MergedDeprecated = MergedDeprecated2;
2244 MergedObsoleted = MergedObsoleted2;
2245 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002246 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002247 }
2248
2249 if (FoundAny &&
2250 MergedIntroduced == Introduced &&
2251 MergedDeprecated == Deprecated &&
2252 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002253 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002254
Ted Kremenekcb344392013-04-06 00:34:27 +00002255 // Only create a new attribute if !Override, but we want to do
2256 // the checking.
Rafael Espindola98ae8342012-05-10 02:50:16 +00002257 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekcb344392013-04-06 00:34:27 +00002258 MergedDeprecated, MergedObsoleted) &&
2259 !Override) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002260 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2261 Introduced, Deprecated,
Michael Han51d8c522013-01-24 16:46:58 +00002262 Obsoleted, IsUnavailable, Message,
2263 AttrSpellingListIndex);
Rafael Espindola3b294362012-05-06 19:56:25 +00002264 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002265 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002266}
2267
Chandler Carruth1b03c872011-07-02 00:01:44 +00002268static void handleAvailabilityAttr(Sema &S, Decl *D,
2269 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002270 if (!checkAttributeNumArgs(S, Attr, 1))
2271 return;
2272 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han51d8c522013-01-24 16:46:58 +00002273 unsigned Index = Attr.getAttributeSpellingListIndex();
2274
Aaron Ballman624421f2013-08-31 01:11:41 +00002275 IdentifierInfo *II = Platform->Ident;
2276 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2277 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2278 << Platform->Ident;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002279
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002280 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2281 if (!ND) {
2282 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2283 return;
2284 }
2285
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002286 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2287 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2288 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002289 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002290 StringRef Str;
2291 const StringLiteral *SE =
2292 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2293 if (SE)
2294 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002295
Aaron Ballman624421f2013-08-31 01:11:41 +00002296 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002297 Introduced.Version,
2298 Deprecated.Version,
2299 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002300 IsUnavailable, Str,
Michael Han51d8c522013-01-24 16:46:58 +00002301 /*Override=*/false,
2302 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002303 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002304 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002305}
2306
John McCalld4c3d662013-02-20 01:54:26 +00002307template <class T>
2308static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2309 typename T::VisibilityType value,
2310 unsigned attrSpellingListIndex) {
2311 T *existingAttr = D->getAttr<T>();
2312 if (existingAttr) {
2313 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2314 if (existingValue == value)
2315 return NULL;
2316 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2317 S.Diag(range.getBegin(), diag::note_previous_attribute);
2318 D->dropAttr<T>();
2319 }
2320 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2321}
2322
Rafael Espindola599f1b72012-05-13 03:25:18 +00002323VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002324 VisibilityAttr::VisibilityType Vis,
2325 unsigned AttrSpellingListIndex) {
John McCalld4c3d662013-02-20 01:54:26 +00002326 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2327 AttrSpellingListIndex);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002328}
2329
John McCalld4c3d662013-02-20 01:54:26 +00002330TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2331 TypeVisibilityAttr::VisibilityType Vis,
2332 unsigned AttrSpellingListIndex) {
2333 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2334 AttrSpellingListIndex);
2335}
2336
2337static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2338 bool isTypeVisibility) {
2339 // Visibility attributes don't mean anything on a typedef.
2340 if (isa<TypedefNameDecl>(D)) {
2341 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2342 << Attr.getName();
2343 return;
2344 }
2345
2346 // 'type_visibility' can only go on a type or namespace.
2347 if (isTypeVisibility &&
2348 !(isa<TagDecl>(D) ||
2349 isa<ObjCInterfaceDecl>(D) ||
2350 isa<NamespaceDecl>(D))) {
2351 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2352 << Attr.getName() << ExpectedTypeOrNamespace;
2353 return;
2354 }
2355
Chris Lattner6b6b5372008-06-26 18:38:35 +00002356 // check the attribute arguments.
John McCalld4c3d662013-02-20 01:54:26 +00002357 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002358 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002359
Aaron Ballman624421f2013-08-31 01:11:41 +00002360 Expr *Arg = Attr.getArgAsExpr(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002361 Arg = Arg->IgnoreParenCasts();
2362 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002363
Douglas Gregor5cee1192011-07-27 05:40:30 +00002364 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002365 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2366 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002367 return;
2368 }
Mike Stumpbf916502009-07-24 19:02:52 +00002369
Chris Lattner5f9e2722011-07-23 10:55:15 +00002370 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002371 VisibilityAttr::VisibilityType type;
Michael Han51d8c522013-01-24 16:46:58 +00002372
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002373 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002374 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002375 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002376 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002377 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002378 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002379 else if (TypeStr == "protected") {
2380 // Complain about attempts to use protected visibility on targets
2381 // (like Darwin) that don't support it.
2382 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2383 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2384 type = VisibilityAttr::Default;
2385 } else {
2386 type = VisibilityAttr::Protected;
2387 }
2388 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002389 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002390 return;
2391 }
Mike Stumpbf916502009-07-24 19:02:52 +00002392
Michael Han51d8c522013-01-24 16:46:58 +00002393 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld4c3d662013-02-20 01:54:26 +00002394 clang::Attr *newAttr;
2395 if (isTypeVisibility) {
2396 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2397 (TypeVisibilityAttr::VisibilityType) type,
2398 Index);
2399 } else {
2400 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2401 }
2402 if (newAttr)
2403 D->addAttr(newAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002404}
2405
Chandler Carruth1b03c872011-07-02 00:01:44 +00002406static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2407 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002408 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2409 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002410 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002411 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002412 return;
2413 }
2414
Aaron Ballman624421f2013-08-31 01:11:41 +00002415 if (!Attr.isArgIdent(0)) {
2416 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2417 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCalld5313b02011-03-02 11:33:24 +00002418 return;
2419 }
Aaron Ballman624421f2013-08-31 01:11:41 +00002420
2421 if (!checkAttributeNumArgs(S, Attr, 1))
2422 return;
John McCalld5313b02011-03-02 11:33:24 +00002423
Aaron Ballman624421f2013-08-31 01:11:41 +00002424 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2425
2426 StringRef param = IL->Ident->getName();
John McCalld5313b02011-03-02 11:33:24 +00002427 ObjCMethodFamilyAttr::FamilyKind family;
2428 if (param == "none")
2429 family = ObjCMethodFamilyAttr::OMF_None;
2430 else if (param == "alloc")
2431 family = ObjCMethodFamilyAttr::OMF_alloc;
2432 else if (param == "copy")
2433 family = ObjCMethodFamilyAttr::OMF_copy;
2434 else if (param == "init")
2435 family = ObjCMethodFamilyAttr::OMF_init;
2436 else if (param == "mutableCopy")
2437 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2438 else if (param == "new")
2439 family = ObjCMethodFamilyAttr::OMF_new;
2440 else {
2441 // Just warn and ignore it. This is future-proof against new
2442 // families being used in system headers.
Aaron Ballman624421f2013-08-31 01:11:41 +00002443 S.Diag(IL->Loc, diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002444 return;
2445 }
2446
John McCallf85e1932011-06-15 23:02:42 +00002447 if (family == ObjCMethodFamilyAttr::OMF_init &&
2448 !method->getResultType()->isObjCObjectPointerType()) {
2449 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2450 << method->getResultType();
2451 // Ignore the attribute.
2452 return;
2453 }
2454
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002455 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002456 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002457}
2458
Chandler Carruth1b03c872011-07-02 00:01:44 +00002459static void handleObjCExceptionAttr(Sema &S, Decl *D,
2460 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002461 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002462 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002463
Chris Lattner0db29ec2009-02-14 08:09:34 +00002464 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2465 if (OCI == 0) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002466 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2467 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002468 return;
2469 }
Mike Stumpbf916502009-07-24 19:02:52 +00002470
Michael Han51d8c522013-01-24 16:46:58 +00002471 D->addAttr(::new (S.Context)
2472 ObjCExceptionAttr(Attr.getRange(), S.Context,
2473 Attr.getAttributeSpellingListIndex()));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002474}
2475
Chandler Carruth1b03c872011-07-02 00:01:44 +00002476static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman081c8832013-07-23 12:13:14 +00002477 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002478 return;
Richard Smith162e1c12011-04-15 14:24:37 +00002479 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002480 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002481 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002482 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2483 return;
2484 }
2485 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002486 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2487 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002488 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002489 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2490 return;
2491 }
2492 }
2493 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002494 // It is okay to include this attribute on properties, e.g.:
2495 //
2496 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2497 //
2498 // In this case it follows tradition and suppresses an error in the above
2499 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002500 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002501 }
Michael Han51d8c522013-01-24 16:46:58 +00002502 D->addAttr(::new (S.Context)
2503 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2504 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002505}
2506
Mike Stumpbf916502009-07-24 19:02:52 +00002507static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002508handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman081c8832013-07-23 12:13:14 +00002509 if (!checkAttributeNumArgs(S, Attr, 0))
Douglas Gregorf9201e02009-02-11 23:02:49 +00002510 return;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002511
2512 if (!isa<FunctionDecl>(D)) {
2513 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2514 return;
2515 }
2516
Michael Han51d8c522013-01-24 16:46:58 +00002517 D->addAttr(::new (S.Context)
2518 OverloadableAttr(Attr.getRange(), S.Context,
2519 Attr.getAttributeSpellingListIndex()));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002520}
2521
Chandler Carruth1b03c872011-07-02 00:01:44 +00002522static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002523 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002524 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00002525 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff9eae5762008-09-18 16:44:58 +00002526 return;
2527 }
Aaron Ballman624421f2013-08-31 01:11:41 +00002528
2529 if (!checkAttributeNumArgs(S, Attr, 1))
Steve Naroff9eae5762008-09-18 16:44:58 +00002530 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002531
Aaron Ballman624421f2013-08-31 01:11:41 +00002532 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Sean Huntcf807c42010-08-18 23:23:40 +00002533 BlocksAttr::BlockType type;
Aaron Ballman624421f2013-08-31 01:11:41 +00002534 if (II->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002535 type = BlocksAttr::ByRef;
2536 else {
Aaron Ballman624421f2013-08-31 01:11:41 +00002537 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) << "blocks"
2538 << II;
Steve Naroff9eae5762008-09-18 16:44:58 +00002539 return;
2540 }
Mike Stumpbf916502009-07-24 19:02:52 +00002541
Michael Han51d8c522013-01-24 16:46:58 +00002542 D->addAttr(::new (S.Context)
2543 BlocksAttr(Attr.getRange(), S.Context, type,
2544 Attr.getAttributeSpellingListIndex()));
Steve Naroff9eae5762008-09-18 16:44:58 +00002545}
2546
Chandler Carruth1b03c872011-07-02 00:01:44 +00002547static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002548 // check the attribute arguments.
2549 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002550 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002551 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002552 }
2553
John McCall3323fad2011-09-09 07:56:05 +00002554 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002555 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002556 Expr *E = Attr.getArgAsExpr(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002557 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002558 if (E->isTypeDependent() || E->isValueDependent() ||
2559 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002560 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002561 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002562 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002563 return;
2564 }
Mike Stumpbf916502009-07-24 19:02:52 +00002565
John McCall3323fad2011-09-09 07:56:05 +00002566 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002567 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2568 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002569 return;
2570 }
John McCall3323fad2011-09-09 07:56:05 +00002571
2572 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002573 }
2574
John McCall3323fad2011-09-09 07:56:05 +00002575 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002576 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002577 Expr *E = Attr.getArgAsExpr(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002578 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002579 if (E->isTypeDependent() || E->isValueDependent() ||
2580 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002581 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002582 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002583 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002584 return;
2585 }
2586 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002587
John McCall3323fad2011-09-09 07:56:05 +00002588 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002589 // FIXME: This error message could be improved, it would be nice
2590 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002591 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2592 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002593 return;
2594 }
2595 }
2596
Chandler Carruth87c44602011-07-01 23:49:12 +00002597 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002598 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002599 if (isa<FunctionNoProtoType>(FT)) {
2600 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2601 return;
2602 }
Mike Stumpbf916502009-07-24 19:02:52 +00002603
Chris Lattner897cd902009-03-17 23:03:47 +00002604 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002605 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002606 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002607 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002608 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002609 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002610 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002611 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002612 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002613 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2614 if (!BD->isVariadic()) {
2615 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2616 return;
2617 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002618 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002619 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002620 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002621 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002622 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002623 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002624 int m = Ty->isFunctionPointerType() ? 0 : 1;
2625 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002626 return;
2627 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002628 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002629 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002630 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002631 return;
2632 }
Anders Carlsson77091822008-10-05 18:05:59 +00002633 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002634 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002635 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002636 return;
2637 }
Michael Han51d8c522013-01-24 16:46:58 +00002638 D->addAttr(::new (S.Context)
2639 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2640 Attr.getAttributeSpellingListIndex()));
Anders Carlsson77091822008-10-05 18:05:59 +00002641}
2642
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002643static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2644 // Check the attribute arguments.
2645 if (!checkAttributeNumArgs(S, Attr, 0))
2646 return;
2647
2648 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2649 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2650 else
2651 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2652}
2653
Chandler Carruth1b03c872011-07-02 00:01:44 +00002654static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002655 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002656 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002657 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002658
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002659 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002660 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002661 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002662 return;
2663 }
Mike Stumpbf916502009-07-24 19:02:52 +00002664
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002665 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2666 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2667 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002668 return;
2669 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002670 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2671 if (MD->getResultType()->isVoidType()) {
2672 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2673 << Attr.getName() << 1;
2674 return;
2675 }
2676
Michael Han51d8c522013-01-24 16:46:58 +00002677 D->addAttr(::new (S.Context)
2678 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2679 Attr.getAttributeSpellingListIndex()));
Chris Lattner026dc962009-02-14 07:37:35 +00002680}
2681
Chandler Carruth1b03c872011-07-02 00:01:44 +00002682static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002683 // check the attribute arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00002684 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002685 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002686
Chandler Carruth87c44602011-07-01 23:49:12 +00002687 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002688 if (isa<CXXRecordDecl>(D)) {
2689 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2690 return;
2691 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002692 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2693 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002694 return;
2695 }
2696
Chandler Carruth87c44602011-07-01 23:49:12 +00002697 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002698
Michael Han51d8c522013-01-24 16:46:58 +00002699 nd->addAttr(::new (S.Context)
2700 WeakAttr(Attr.getRange(), S.Context,
2701 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002702}
2703
Chandler Carruth1b03c872011-07-02 00:01:44 +00002704static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002705 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002706 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002707 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002708
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002709
2710 // weak_import only applies to variable & function declarations.
2711 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002712 if (!D->canBeWeakImported(isDef)) {
2713 if (isDef)
Reid Klecknerbeba3e82013-05-20 21:53:29 +00002714 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2715 << "weak_import";
Douglas Gregordef86312011-03-23 13:27:51 +00002716 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002717 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002718 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002719 // Nothing to warn about here.
2720 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002721 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002722 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002723
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002724 return;
2725 }
2726
Michael Han51d8c522013-01-24 16:46:58 +00002727 D->addAttr(::new (S.Context)
2728 WeakImportAttr(Attr.getRange(), S.Context,
2729 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002730}
2731
Tanya Lattner0df579e2012-07-09 22:06:01 +00002732// Handles reqd_work_group_size and work_group_size_hint.
2733static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002734 const AttributeList &Attr) {
Tanya Lattner0df579e2012-07-09 22:06:01 +00002735 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2736 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2737
Nate Begeman6f3d8382009-06-26 06:32:41 +00002738 // Attribute has 3 arguments.
Tanya Lattner0df579e2012-07-09 22:06:01 +00002739 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002740
2741 unsigned WGSize[3];
2742 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002743 Expr *E = Attr.getArgAsExpr(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002744 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002745 if (E->isTypeDependent() || E->isValueDependent() ||
2746 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00002747 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2748 << Attr.getName() << AANT_ArgumentIntegerConstant
2749 << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002750 return;
2751 }
2752 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2753 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002754
2755 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2756 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2757 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2758 if (!(A->getXDim() == WGSize[0] &&
2759 A->getYDim() == WGSize[1] &&
2760 A->getZDim() == WGSize[2])) {
2761 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2762 Attr.getName();
2763 }
2764 }
2765
2766 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2767 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2768 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2769 if (!(A->getXDim() == WGSize[0] &&
2770 A->getYDim() == WGSize[1] &&
2771 A->getZDim() == WGSize[2])) {
2772 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2773 Attr.getName();
2774 }
2775 }
2776
2777 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2778 D->addAttr(::new (S.Context)
2779 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002780 WGSize[0], WGSize[1], WGSize[2],
2781 Attr.getAttributeSpellingListIndex()));
Tanya Lattner0df579e2012-07-09 22:06:01 +00002782 else
2783 D->addAttr(::new (S.Context)
2784 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002785 WGSize[0], WGSize[1], WGSize[2],
2786 Attr.getAttributeSpellingListIndex()));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002787}
2788
Joey Gouly37453b92013-03-08 09:42:32 +00002789static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2790 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2791
Aaron Ballman624421f2013-08-31 01:11:41 +00002792 if (!checkAttributeNumArgs(S, Attr, 0))
Joey Gouly37453b92013-03-08 09:42:32 +00002793 return;
2794
Aaron Ballman624421f2013-08-31 01:11:41 +00002795 if (!Attr.hasParsedType()) {
2796 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2797 << Attr.getName() << 1;
2798 return;
2799 }
2800
Joey Gouly37453b92013-03-08 09:42:32 +00002801 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg());
2802
2803 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2804 (ParmType->isBooleanType() ||
2805 !ParmType->isIntegralType(S.getASTContext()))) {
2806 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2807 << ParmType;
2808 return;
2809 }
2810
2811 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2812 D->hasAttr<VecTypeHintAttr>()) {
2813 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
2814 if (A->getTypeHint() != ParmType) {
2815 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2816 return;
2817 }
2818 }
2819
2820 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
2821 ParmType, Attr.getLoc()));
2822}
2823
Joey Gouly96cead52013-03-14 09:54:43 +00002824static void handleEndianAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002825 if (!Attr.isArgIdent(0)) {
2826 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2827 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
2828 return;
2829 }
2830
2831 if (!checkAttributeNumArgs(S, Attr, 1))
2832 return;
2833
Joey Gouly96cead52013-03-14 09:54:43 +00002834 if (!dyn_cast<VarDecl>(D))
Aaron Ballman624421f2013-08-31 01:11:41 +00002835 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2836 << Attr.getName() << ExpectedVariable;
2837 StringRef EndianType = Attr.getArgAsIdent(0)->Ident->getName();
Joey Gouly96cead52013-03-14 09:54:43 +00002838 if (EndianType != "host" && EndianType != "device")
2839 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_endian) << EndianType;
2840}
2841
Rafael Espindola599f1b72012-05-13 03:25:18 +00002842SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002843 StringRef Name,
2844 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002845 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2846 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002847 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002848 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2849 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002850 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002851 }
Michael Han51d8c522013-01-24 16:46:58 +00002852 return ::new (Context) SectionAttr(Range, Context, Name,
2853 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002854}
2855
Chandler Carruth1b03c872011-07-02 00:01:44 +00002856static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002857 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002858 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002859 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002860
2861 // Make sure that there is a string literal as the sections's single
2862 // argument.
Aaron Ballman624421f2013-08-31 01:11:41 +00002863 Expr *ArgExpr = Attr.getArgAsExpr(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002864 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002865 if (!SE) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002866 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
2867 << Attr.getName() << AANT_ArgumentString;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002868 return;
2869 }
Mike Stump1eb44332009-09-09 15:08:12 +00002870
Chris Lattner797c3c42009-08-10 19:03:04 +00002871 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002872 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002873 if (!Error.empty()) {
2874 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2875 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002876 return;
2877 }
Mike Stump1eb44332009-09-09 15:08:12 +00002878
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002879 // This attribute cannot be applied to local variables.
2880 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2881 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2882 return;
2883 }
Michael Han51d8c522013-01-24 16:46:58 +00002884
2885 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindola599f1b72012-05-13 03:25:18 +00002886 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han51d8c522013-01-24 16:46:58 +00002887 SE->getString(), Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002888 if (NewAttr)
2889 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002890}
2891
Chris Lattner6b6b5372008-06-26 18:38:35 +00002892
Chandler Carruth1b03c872011-07-02 00:01:44 +00002893static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002894 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002895 return;
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002896
Chandler Carruth87c44602011-07-01 23:49:12 +00002897 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002898 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002899 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002900 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002901 D->addAttr(::new (S.Context)
2902 NoThrowAttr(Attr.getRange(), S.Context,
2903 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002904 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002905}
2906
Chandler Carruth1b03c872011-07-02 00:01:44 +00002907static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002908 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002909 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002910
Chandler Carruth87c44602011-07-01 23:49:12 +00002911 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002912 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002913 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002914 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002915 D->addAttr(::new (S.Context)
2916 ConstAttr(Attr.getRange(), S.Context,
2917 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002918 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002919}
2920
Chandler Carruth1b03c872011-07-02 00:01:44 +00002921static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002922 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002923 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002924 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002925
Michael Han51d8c522013-01-24 16:46:58 +00002926 D->addAttr(::new (S.Context)
2927 PureAttr(Attr.getRange(), S.Context,
2928 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002929}
2930
Chandler Carruth1b03c872011-07-02 00:01:44 +00002931static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002932 if (!Attr.isArgIdent(0)) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002933 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2934 << Attr.getName() << 1;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002935 return;
2936 }
Aaron Ballman624421f2013-08-31 01:11:41 +00002937
2938 if (!checkAttributeNumArgs(S, Attr, 1))
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002939 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002940
Chandler Carruth87c44602011-07-01 23:49:12 +00002941 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002942
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002943 if (!VD || !VD->hasLocalStorage()) {
2944 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2945 return;
2946 }
Mike Stumpbf916502009-07-24 19:02:52 +00002947
Aaron Ballman624421f2013-08-31 01:11:41 +00002948 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2949
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002950 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002951 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002952 NamedDecl *CleanupDecl
Aaron Ballman624421f2013-08-31 01:11:41 +00002953 = S.LookupSingleName(S.TUScope, IL->Ident, IL->Loc,
2954 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002955 if (!CleanupDecl) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002956 S.Diag(IL->Loc, diag::err_attribute_cleanup_arg_not_found) << IL->Ident;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002957 return;
2958 }
Mike Stumpbf916502009-07-24 19:02:52 +00002959
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002960 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2961 if (!FD) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002962 S.Diag(IL->Loc, diag::err_attribute_cleanup_arg_not_function) << IL->Ident;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002963 return;
2964 }
2965
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002966 if (FD->getNumParams() != 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002967 S.Diag(IL->Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2968 << IL->Ident;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002969 return;
2970 }
Mike Stumpbf916502009-07-24 19:02:52 +00002971
Anders Carlsson89941c12009-02-07 23:16:50 +00002972 // We're currently more strict than GCC about what function types we accept.
2973 // If this ever proves to be a problem it should be easy to fix.
2974 QualType Ty = S.Context.getPointerType(VD->getType());
2975 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002976 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2977 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002978 S.Diag(IL->Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2979 IL->Ident << ParamTy << Ty;
Anders Carlsson89941c12009-02-07 23:16:50 +00002980 return;
2981 }
Mike Stumpbf916502009-07-24 19:02:52 +00002982
Michael Han51d8c522013-01-24 16:46:58 +00002983 D->addAttr(::new (S.Context)
2984 CleanupAttr(Attr.getRange(), S.Context, FD,
2985 Attr.getAttributeSpellingListIndex()));
Aaron Ballman624421f2013-08-31 01:11:41 +00002986 S.MarkFunctionReferenced(IL->Loc, FD);
2987 S.DiagnoseUseOfDecl(FD, IL->Loc);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002988}
2989
Mike Stumpbf916502009-07-24 19:02:52 +00002990/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002991/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002992static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002993 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002994 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002995
Chandler Carruth87c44602011-07-01 23:49:12 +00002996 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002997 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002998 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002999 return;
3000 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003001
Aaron Ballman624421f2013-08-31 01:11:41 +00003002 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00003003 uint64_t ArgIdx;
3004 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
3005 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003006 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003007
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003008 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003009 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00003010
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003011 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
3012 if (not_nsstring_type &&
3013 !isCFStringType(Ty, S.Context) &&
3014 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003015 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003016 // FIXME: Should highlight the actual expression that has the wrong type.
3017 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00003018 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003019 << IdxExpr->getSourceRange();
3020 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003021 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003022 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003023 if (!isNSStringType(Ty, S.Context) &&
3024 !isCFStringType(Ty, S.Context) &&
3025 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003026 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003027 // FIXME: Should highlight the actual expression that has the wrong type.
3028 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00003029 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003030 << IdxExpr->getSourceRange();
3031 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003032 }
3033
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00003034 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
3035 // because that has corrected for the implicit this parameter, and is zero-
3036 // based. The attribute expects what the user wrote explicitly.
3037 llvm::APSInt Val;
3038 IdxExpr->EvaluateAsInt(Val, S.Context);
3039
Michael Han51d8c522013-01-24 16:46:58 +00003040 D->addAttr(::new (S.Context)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00003041 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003042 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003043}
3044
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003045enum FormatAttrKind {
3046 CFStringFormat,
3047 NSStringFormat,
3048 StrftimeFormat,
3049 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00003050 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003051 InvalidFormat
3052};
3053
3054/// getFormatAttrKind - Map from format attribute names to supported format
3055/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003056static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003057 return llvm::StringSwitch<FormatAttrKind>(Format)
3058 // Check for formats that get handled specially.
3059 .Case("NSString", NSStringFormat)
3060 .Case("CFString", CFStringFormat)
3061 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003062
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003063 // Otherwise, check for supported formats.
3064 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3065 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3066 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003067
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003068 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3069 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003070}
3071
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003072/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003073/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003074static void handleInitPriorityAttr(Sema &S, Decl *D,
3075 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003076 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003077 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3078 return;
3079 }
3080
Chandler Carruth87c44602011-07-01 23:49:12 +00003081 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003082 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3083 Attr.setInvalid();
3084 return;
3085 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003086 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003087 if (S.Context.getAsArrayType(T))
3088 T = S.Context.getBaseElementType(T);
3089 if (!T->getAs<RecordType>()) {
3090 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3091 Attr.setInvalid();
3092 return;
3093 }
3094
Aaron Ballmanffa9d572013-07-18 18:01:48 +00003095 if (!checkAttributeNumArgs(S, Attr, 1)) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003096 Attr.setInvalid();
3097 return;
3098 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003099 Expr *priorityExpr = Attr.getArgAsExpr(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003100
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003101 llvm::APSInt priority(32);
3102 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3103 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003104 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3105 << Attr.getName() << AANT_ArgumentIntegerConstant
3106 << priorityExpr->getSourceRange();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003107 Attr.setInvalid();
3108 return;
3109 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00003110 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003111 if (prioritynum < 101 || prioritynum > 65535) {
3112 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3113 << priorityExpr->getSourceRange();
3114 Attr.setInvalid();
3115 return;
3116 }
Michael Han51d8c522013-01-24 16:46:58 +00003117 D->addAttr(::new (S.Context)
3118 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3119 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003120}
3121
Rafael Espindola599f1b72012-05-13 03:25:18 +00003122FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format,
Michael Han51d8c522013-01-24 16:46:58 +00003123 int FormatIdx, int FirstArg,
3124 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003125 // Check whether we already have an equivalent format attribute.
3126 for (specific_attr_iterator<FormatAttr>
3127 i = D->specific_attr_begin<FormatAttr>(),
3128 e = D->specific_attr_end<FormatAttr>();
3129 i != e ; ++i) {
3130 FormatAttr *f = *i;
3131 if (f->getType() == Format &&
3132 f->getFormatIdx() == FormatIdx &&
3133 f->getFirstArg() == FirstArg) {
3134 // If we don't have a valid location for this attribute, adopt the
3135 // location.
3136 if (f->getLocation().isInvalid())
3137 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00003138 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003139 }
3140 }
3141
Michael Han51d8c522013-01-24 16:46:58 +00003142 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg,
3143 AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003144}
3145
Mike Stumpbf916502009-07-24 19:02:52 +00003146/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003147/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003148static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003149 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003150 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00003151 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003152 return;
3153 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003154
3155 if (!checkAttributeNumArgs(S, Attr, 3))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003156 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003157
Chandler Carruth87c44602011-07-01 23:49:12 +00003158 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003159 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003160 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003161 return;
3162 }
3163
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003164 // In C++ the implicit 'this' function parameter also counts, and they are
3165 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003166 bool HasImplicitThisParam = isInstanceMethod(D);
3167 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003168 unsigned FirstIdx = 1;
3169
Aaron Ballman624421f2013-08-31 01:11:41 +00003170 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
3171 StringRef Format = II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003172
3173 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003174 if (Format.startswith("__") && Format.endswith("__"))
3175 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003176
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003177 // Check for supported formats.
3178 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003179
3180 if (Kind == IgnoredFormat)
3181 return;
3182
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003183 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003184 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman624421f2013-08-31 01:11:41 +00003185 << "format" << II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003186 return;
3187 }
3188
3189 // checks for the 2nd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003190 Expr *IdxExpr = Attr.getArgAsExpr(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003191 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003192 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3193 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003194 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003195 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003196 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003197 return;
3198 }
3199
3200 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003201 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003202 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003203 return;
3204 }
3205
3206 // FIXME: Do we need to bounds check?
3207 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003208
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003209 if (HasImplicitThisParam) {
3210 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003211 S.Diag(Attr.getLoc(),
3212 diag::err_format_attribute_implicit_this_format_string)
3213 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003214 return;
3215 }
3216 ArgIdx--;
3217 }
Mike Stump1eb44332009-09-09 15:08:12 +00003218
Chris Lattner6b6b5372008-06-26 18:38:35 +00003219 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003220 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003221
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003222 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003223 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003224 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3225 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003226 return;
3227 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003228 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003229 // FIXME: do we need to check if the type is NSString*? What are the
3230 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003231 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003232 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003233 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3234 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003235 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003236 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003237 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003238 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003239 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003240 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3241 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003242 return;
3243 }
3244
3245 // check the 3rd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003246 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Chris Lattner803d0802008-06-29 00:43:07 +00003247 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003248 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3249 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003250 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003251 << Attr.getName() << 3 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003252 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003253 return;
3254 }
3255
3256 // check if the function is variadic if the 3rd argument non-zero
3257 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003258 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003259 ++NumArgs; // +1 for ...
3260 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003261 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003262 return;
3263 }
3264 }
3265
Chris Lattner3c73c412008-11-19 08:23:25 +00003266 // strftime requires FirstArg to be 0 because it doesn't read from any
3267 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003268 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003269 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003270 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3271 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003272 return;
3273 }
3274 // if 0 it disables parameter checking (to use with e.g. va_list)
3275 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003276 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003277 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003278 return;
3279 }
3280
Rafael Espindola599f1b72012-05-13 03:25:18 +00003281 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format,
3282 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003283 FirstArg.getZExtValue(),
3284 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003285 if (NewAttr)
3286 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003287}
3288
Chandler Carruth1b03c872011-07-02 00:01:44 +00003289static void handleTransparentUnionAttr(Sema &S, Decl *D,
3290 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003291 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003292 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003293 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003294
Chris Lattner6b6b5372008-06-26 18:38:35 +00003295
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003296 // Try to find the underlying union declaration.
3297 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003298 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003299 if (TD && TD->getUnderlyingType()->isUnionType())
3300 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3301 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003302 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003303
3304 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003305 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003306 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003307 return;
3308 }
3309
John McCall5e1cdac2011-10-07 06:10:15 +00003310 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003311 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003312 diag::warn_transparent_union_attribute_not_definition);
3313 return;
3314 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003315
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003316 RecordDecl::field_iterator Field = RD->field_begin(),
3317 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003318 if (Field == FieldEnd) {
3319 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3320 return;
3321 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003322
David Blaikie581deb32012-06-06 20:45:41 +00003323 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003324 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003325 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003326 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003327 diag::warn_transparent_union_attribute_floating)
3328 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003329 return;
3330 }
3331
3332 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3333 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3334 for (; Field != FieldEnd; ++Field) {
3335 QualType FieldType = Field->getType();
3336 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3337 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3338 // Warn if we drop the attribute.
3339 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003340 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003341 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003342 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003343 diag::warn_transparent_union_attribute_field_size_align)
3344 << isSize << Field->getDeclName() << FieldBits;
3345 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003346 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003347 diag::note_transparent_union_first_field_size_align)
3348 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003349 return;
3350 }
3351 }
3352
Michael Han51d8c522013-01-24 16:46:58 +00003353 RD->addAttr(::new (S.Context)
3354 TransparentUnionAttr(Attr.getRange(), S.Context,
3355 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003356}
3357
Chandler Carruth1b03c872011-07-02 00:01:44 +00003358static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003359 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003360 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003361 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003362
Aaron Ballman624421f2013-08-31 01:11:41 +00003363 Expr *ArgExpr = Attr.getArgAsExpr(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003364 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003365
Chris Lattner6b6b5372008-06-26 18:38:35 +00003366 // Make sure that there is a string literal as the annotation's single
3367 // argument.
3368 if (!SE) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003369 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
3370 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003371 return;
3372 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003373
3374 // Don't duplicate annotations that are already set.
3375 for (specific_attr_iterator<AnnotateAttr>
3376 i = D->specific_attr_begin<AnnotateAttr>(),
3377 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3378 if ((*i)->getAnnotation() == SE->getString())
3379 return;
3380 }
Michael Han51d8c522013-01-24 16:46:58 +00003381
3382 D->addAttr(::new (S.Context)
3383 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3384 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003385}
3386
Chandler Carruth1b03c872011-07-02 00:01:44 +00003387static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003388 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003389 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003390 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3391 << Attr.getName() << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003392 return;
3393 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003394
Richard Smithbe507b62013-02-01 08:12:08 +00003395 if (Attr.getNumArgs() == 0) {
3396 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3397 true, 0, Attr.getAttributeSpellingListIndex()));
3398 return;
3399 }
3400
Aaron Ballman624421f2013-08-31 01:11:41 +00003401 Expr *E = Attr.getArgAsExpr(0);
Richard Smithf6565a92013-02-22 08:32:16 +00003402 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3403 S.Diag(Attr.getEllipsisLoc(),
3404 diag::err_pack_expansion_without_parameter_packs);
3405 return;
3406 }
3407
3408 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3409 return;
3410
3411 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3412 Attr.isPackExpansion());
Richard Smithbe507b62013-02-01 08:12:08 +00003413}
3414
3415void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smithf6565a92013-02-22 08:32:16 +00003416 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smithbe507b62013-02-01 08:12:08 +00003417 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3418 SourceLocation AttrLoc = AttrRange.getBegin();
3419
Richard Smith4cd81c52013-01-29 09:02:09 +00003420 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smithbe507b62013-02-01 08:12:08 +00003421 if (TmpAttr.isAlignas()) {
Richard Smith4cd81c52013-01-29 09:02:09 +00003422 // C++11 [dcl.align]p1:
3423 // An alignment-specifier may be applied to a variable or to a class
3424 // data member, but it shall not be applied to a bit-field, a function
3425 // parameter, the formal parameter of a catch clause, or a variable
3426 // declared with the register storage class specifier. An
3427 // alignment-specifier may also be applied to the declaration of a class
3428 // or enumeration type.
3429 // C11 6.7.5/2:
3430 // An alignment attribute shall not be specified in a declaration of
3431 // a typedef, or a bit-field, or a function, or a parameter, or an
3432 // object declared with the register storage-class specifier.
3433 int DiagKind = -1;
3434 if (isa<ParmVarDecl>(D)) {
3435 DiagKind = 0;
3436 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3437 if (VD->getStorageClass() == SC_Register)
3438 DiagKind = 1;
3439 if (VD->isExceptionVariable())
3440 DiagKind = 2;
3441 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3442 if (FD->isBitField())
3443 DiagKind = 3;
3444 } else if (!isa<TagDecl>(D)) {
Richard Smithbe507b62013-02-01 08:12:08 +00003445 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3446 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith5f838aa2013-02-01 08:25:07 +00003447 << (TmpAttr.isC11() ? ExpectedVariableOrField
3448 : ExpectedVariableFieldOrTag);
Richard Smith4cd81c52013-01-29 09:02:09 +00003449 return;
3450 }
3451 if (DiagKind != -1) {
Richard Smithbe507b62013-02-01 08:12:08 +00003452 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smith671b3212013-02-22 04:55:39 +00003453 << TmpAttr.isC11() << DiagKind;
Richard Smith4cd81c52013-01-29 09:02:09 +00003454 return;
3455 }
3456 }
3457
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003458 if (E->isTypeDependent() || E->isValueDependent()) {
3459 // Save dependent expressions in the AST to be instantiated.
Richard Smithf6565a92013-02-22 08:32:16 +00003460 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3461 AA->setPackExpansion(IsPackExpansion);
3462 D->addAttr(AA);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003463 return;
3464 }
Michael Hana31f65b2013-02-01 01:19:17 +00003465
Sean Huntcf807c42010-08-18 23:23:40 +00003466 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003467 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003468 ExprResult ICE
3469 = VerifyIntegerConstantExpression(E, &Alignment,
3470 diag::err_aligned_attribute_argument_not_int,
3471 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003472 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003473 return;
Richard Smithbe507b62013-02-01 08:12:08 +00003474
3475 // C++11 [dcl.align]p2:
3476 // -- if the constant expression evaluates to zero, the alignment
3477 // specifier shall have no effect
3478 // C11 6.7.5p6:
3479 // An alignment specification of zero has no effect.
3480 if (!(TmpAttr.isAlignas() && !Alignment) &&
3481 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003482 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3483 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003484 return;
3485 }
Michael Hana31f65b2013-02-01 01:19:17 +00003486
Richard Smithbe507b62013-02-01 08:12:08 +00003487 if (TmpAttr.isDeclspec()) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003488 // We've already verified it's a power of 2, now let's make sure it's
3489 // 8192 or less.
3490 if (Alignment.getZExtValue() > 8192) {
Michael Hana31f65b2013-02-01 01:19:17 +00003491 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003492 << E->getSourceRange();
3493 return;
3494 }
3495 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003496
Richard Smithf6565a92013-02-22 08:32:16 +00003497 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3498 ICE.take(), SpellingListIndex);
3499 AA->setPackExpansion(IsPackExpansion);
3500 D->addAttr(AA);
Sean Huntcf807c42010-08-18 23:23:40 +00003501}
3502
Michael Hana31f65b2013-02-01 01:19:17 +00003503void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smithf6565a92013-02-22 08:32:16 +00003504 unsigned SpellingListIndex, bool IsPackExpansion) {
Sean Huntcf807c42010-08-18 23:23:40 +00003505 // FIXME: Cache the number on the Attr object if non-dependent?
3506 // FIXME: Perform checking of type validity
Richard Smithf6565a92013-02-22 08:32:16 +00003507 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3508 SpellingListIndex);
3509 AA->setPackExpansion(IsPackExpansion);
3510 D->addAttr(AA);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003511}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003512
Richard Smithbe507b62013-02-01 08:12:08 +00003513void Sema::CheckAlignasUnderalignment(Decl *D) {
3514 assert(D->hasAttrs() && "no attributes on decl");
3515
3516 QualType Ty;
3517 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3518 Ty = VD->getType();
3519 else
3520 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith4da09032013-02-22 09:21:42 +00003521 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smithbe507b62013-02-01 08:12:08 +00003522 return;
3523
3524 // C++11 [dcl.align]p5, C11 6.7.5/4:
3525 // The combined effect of all alignment attributes in a declaration shall
3526 // not specify an alignment that is less strict than the alignment that
3527 // would otherwise be required for the entity being declared.
3528 AlignedAttr *AlignasAttr = 0;
3529 unsigned Align = 0;
3530 for (specific_attr_iterator<AlignedAttr>
3531 I = D->specific_attr_begin<AlignedAttr>(),
3532 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3533 if (I->isAlignmentDependent())
3534 return;
3535 if (I->isAlignas())
3536 AlignasAttr = *I;
3537 Align = std::max(Align, I->getAlignment(Context));
3538 }
3539
3540 if (AlignasAttr && Align) {
3541 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3542 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3543 if (NaturalAlign > RequestedAlign)
3544 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3545 << Ty << (unsigned)NaturalAlign.getQuantity();
3546 }
3547}
3548
Chandler Carruthd309c812011-07-01 23:49:16 +00003549/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003550/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003551///
Mike Stumpbf916502009-07-24 19:02:52 +00003552/// Despite what would be logical, the mode attribute is a decl attribute, not a
3553/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3554/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003555static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003556 // This attribute isn't documented, but glibc uses it. It changes
3557 // the width of an int or unsigned int to the specified size.
Aaron Ballman624421f2013-08-31 01:11:41 +00003558 if (!Attr.isArgIdent(0)) {
Aaron Ballman750f73a2013-07-30 14:29:12 +00003559 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3560 << AANT_ArgumentIdentifier;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003561 return;
3562 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003563
3564 if (!checkAttributeNumArgs(S, Attr, 1))
3565 return;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003566
Aaron Ballman624421f2013-08-31 01:11:41 +00003567 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3568 StringRef Str = Name->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003569
3570 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003571 if (Str.startswith("__") && Str.endswith("__"))
3572 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003573
3574 unsigned DestWidth = 0;
3575 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003576 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003577 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003578 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003579 switch (Str[0]) {
3580 case 'Q': DestWidth = 8; break;
3581 case 'H': DestWidth = 16; break;
3582 case 'S': DestWidth = 32; break;
3583 case 'D': DestWidth = 64; break;
3584 case 'X': DestWidth = 96; break;
3585 case 'T': DestWidth = 128; break;
3586 }
3587 if (Str[1] == 'F') {
3588 IntegerMode = false;
3589 } else if (Str[1] == 'C') {
3590 IntegerMode = false;
3591 ComplexMode = true;
3592 } else if (Str[1] != 'I') {
3593 DestWidth = 0;
3594 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003595 break;
3596 case 4:
3597 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3598 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003599 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003600 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003601 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003602 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003603 break;
3604 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003605 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003606 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003607 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003608 case 11:
3609 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003610 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003611 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003612 }
3613
3614 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003615 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003616 OldTy = TD->getUnderlyingType();
3617 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3618 OldTy = VD->getType();
3619 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003620 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003621 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003622 return;
3623 }
Eli Friedman73397492009-03-03 06:41:03 +00003624
John McCall183700f2009-09-21 23:43:11 +00003625 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003626 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3627 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003628 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003629 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3630 } else if (ComplexMode) {
3631 if (!OldTy->isComplexType())
3632 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3633 } else {
3634 if (!OldTy->isFloatingType())
3635 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3636 }
3637
Mike Stump390b4cc2009-05-16 07:39:55 +00003638 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3639 // and friends, at least with glibc.
3640 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3641 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003642 // FIXME: Make sure floating-point mappings are accurate
3643 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003644 QualType NewTy;
3645 switch (DestWidth) {
3646 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003647 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003648 return;
3649 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003650 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003651 return;
3652 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003653 if (!IntegerMode) {
3654 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3655 return;
3656 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003657 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003658 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003659 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003660 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003661 break;
3662 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003663 if (!IntegerMode) {
3664 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3665 return;
3666 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003667 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003668 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003669 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003670 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003671 break;
3672 case 32:
3673 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003674 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003675 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003676 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003677 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003678 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003679 break;
3680 case 64:
3681 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003682 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003683 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003684 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003685 NewTy = S.Context.LongTy;
3686 else
3687 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003688 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003689 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003690 NewTy = S.Context.UnsignedLongTy;
3691 else
3692 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003693 break;
Eli Friedman73397492009-03-03 06:41:03 +00003694 case 96:
3695 NewTy = S.Context.LongDoubleTy;
3696 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003697 case 128:
Roman Divacky19645542013-07-03 21:08:41 +00003698 if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() !=
3699 &llvm::APFloat::PPCDoubleDouble) {
Eli Friedmanf98aba32009-02-13 02:31:07 +00003700 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3701 return;
3702 }
Roman Divackyf0d14cb2013-07-03 20:48:06 +00003703 if (IntegerMode) {
3704 if (OldTy->isSignedIntegerType())
3705 NewTy = S.Context.Int128Ty;
3706 else
3707 NewTy = S.Context.UnsignedInt128Ty;
3708 } else
3709 NewTy = S.Context.LongDoubleTy;
Eli Friedman73397492009-03-03 06:41:03 +00003710 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003711 }
3712
Eli Friedman73397492009-03-03 06:41:03 +00003713 if (ComplexMode) {
3714 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003715 }
3716
3717 // Install the new type.
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003718 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3719 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3720 else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003721 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003722
3723 D->addAttr(::new (S.Context)
3724 ModeAttr(Attr.getRange(), S.Context, Name,
3725 Attr.getAttributeSpellingListIndex()));
Chris Lattnerfbf13472008-06-27 22:18:37 +00003726}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003727
Chandler Carruth1b03c872011-07-02 00:01:44 +00003728static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003729 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003730 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003731 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003732
Nick Lewycky78d1a102012-07-24 01:40:49 +00003733 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3734 if (!VD->hasGlobalStorage())
3735 S.Diag(Attr.getLoc(),
3736 diag::warn_attribute_requires_functions_or_static_globals)
3737 << Attr.getName();
3738 } else if (!isFunctionOrMethod(D)) {
3739 S.Diag(Attr.getLoc(),
3740 diag::warn_attribute_requires_functions_or_static_globals)
3741 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003742 return;
3743 }
Mike Stumpbf916502009-07-24 19:02:52 +00003744
Michael Han51d8c522013-01-24 16:46:58 +00003745 D->addAttr(::new (S.Context)
3746 NoDebugAttr(Attr.getRange(), S.Context,
3747 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003748}
3749
Chandler Carruth1b03c872011-07-02 00:01:44 +00003750static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003751 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003752 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003753 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003754
Mike Stumpbf916502009-07-24 19:02:52 +00003755
Chandler Carruth87c44602011-07-01 23:49:12 +00003756 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003757 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003758 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003759 return;
3760 }
Mike Stumpbf916502009-07-24 19:02:52 +00003761
Michael Han51d8c522013-01-24 16:46:58 +00003762 D->addAttr(::new (S.Context)
3763 NoInlineAttr(Attr.getRange(), S.Context,
3764 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003765}
3766
Chandler Carruth1b03c872011-07-02 00:01:44 +00003767static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3768 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003769 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003770 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003771 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003772
Chris Lattner7255a2d2010-06-22 00:03:40 +00003773
Chandler Carruth87c44602011-07-01 23:49:12 +00003774 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003775 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003776 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003777 return;
3778 }
3779
Michael Han51d8c522013-01-24 16:46:58 +00003780 D->addAttr(::new (S.Context)
3781 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3782 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003783}
3784
Chandler Carruth1b03c872011-07-02 00:01:44 +00003785static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003786 if (S.LangOpts.CUDA) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003787 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003788 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003789
Chandler Carruth87c44602011-07-01 23:49:12 +00003790 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003791 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003792 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003793 return;
3794 }
3795
Michael Han51d8c522013-01-24 16:46:58 +00003796 D->addAttr(::new (S.Context)
3797 CUDAConstantAttr(Attr.getRange(), S.Context,
3798 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003799 } else {
3800 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3801 }
3802}
3803
Chandler Carruth1b03c872011-07-02 00:01:44 +00003804static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003805 if (S.LangOpts.CUDA) {
3806 // check the attribute arguments.
3807 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003808 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3809 << Attr.getName() << 0;
Peter Collingbourneced76712010-12-01 03:15:31 +00003810 return;
3811 }
3812
Chandler Carruth87c44602011-07-01 23:49:12 +00003813 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003814 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003815 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003816 return;
3817 }
3818
Michael Han51d8c522013-01-24 16:46:58 +00003819 D->addAttr(::new (S.Context)
3820 CUDADeviceAttr(Attr.getRange(), S.Context,
3821 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003822 } else {
3823 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3824 }
3825}
3826
Chandler Carruth1b03c872011-07-02 00:01:44 +00003827static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003828 if (S.LangOpts.CUDA) {
3829 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003830 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003831 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003832
Chandler Carruth87c44602011-07-01 23:49:12 +00003833 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003834 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003835 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003836 return;
3837 }
3838
Chandler Carruth87c44602011-07-01 23:49:12 +00003839 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003840 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003841 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00003842 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003843 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3844 << FD->getType()
David Blaikie39e6ab42013-02-18 22:06:02 +00003845 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003846 "void");
3847 } else {
3848 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3849 << FD->getType();
3850 }
3851 return;
3852 }
3853
Michael Han51d8c522013-01-24 16:46:58 +00003854 D->addAttr(::new (S.Context)
3855 CUDAGlobalAttr(Attr.getRange(), S.Context,
3856 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003857 } else {
3858 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3859 }
3860}
3861
Chandler Carruth1b03c872011-07-02 00:01:44 +00003862static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003863 if (S.LangOpts.CUDA) {
3864 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003865 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003866 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003867
Peter Collingbourneced76712010-12-01 03:15:31 +00003868
Chandler Carruth87c44602011-07-01 23:49:12 +00003869 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003870 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003871 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003872 return;
3873 }
3874
Michael Han51d8c522013-01-24 16:46:58 +00003875 D->addAttr(::new (S.Context)
3876 CUDAHostAttr(Attr.getRange(), S.Context,
3877 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003878 } else {
3879 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3880 }
3881}
3882
Chandler Carruth1b03c872011-07-02 00:01:44 +00003883static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003884 if (S.LangOpts.CUDA) {
3885 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003886 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003887 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003888
Chandler Carruth87c44602011-07-01 23:49:12 +00003889 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003890 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003891 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003892 return;
3893 }
3894
Michael Han51d8c522013-01-24 16:46:58 +00003895 D->addAttr(::new (S.Context)
3896 CUDASharedAttr(Attr.getRange(), S.Context,
3897 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003898 } else {
3899 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3900 }
3901}
3902
Chandler Carruth1b03c872011-07-02 00:01:44 +00003903static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003904 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003905 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003906 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003907
Chandler Carruth87c44602011-07-01 23:49:12 +00003908 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003909 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003910 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003911 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003912 return;
3913 }
Mike Stumpbf916502009-07-24 19:02:52 +00003914
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003915 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003916 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003917 return;
3918 }
Mike Stumpbf916502009-07-24 19:02:52 +00003919
Michael Han51d8c522013-01-24 16:46:58 +00003920 D->addAttr(::new (S.Context)
3921 GNUInlineAttr(Attr.getRange(), S.Context,
3922 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00003923}
3924
Chandler Carruth1b03c872011-07-02 00:01:44 +00003925static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003926 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003927
Aaron Ballmanfff32482012-12-09 17:45:41 +00003928 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003929 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003930 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3931 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003932 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003933 return;
3934
Chandler Carruth87c44602011-07-01 23:49:12 +00003935 if (!isa<ObjCMethodDecl>(D)) {
3936 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3937 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003938 return;
3939 }
3940
Chandler Carruth87c44602011-07-01 23:49:12 +00003941 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003942 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00003943 D->addAttr(::new (S.Context)
3944 FastCallAttr(Attr.getRange(), S.Context,
3945 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003946 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003947 case AttributeList::AT_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00003948 D->addAttr(::new (S.Context)
3949 StdCallAttr(Attr.getRange(), S.Context,
3950 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003951 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003952 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00003953 D->addAttr(::new (S.Context)
3954 ThisCallAttr(Attr.getRange(), S.Context,
3955 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003956 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003957 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00003958 D->addAttr(::new (S.Context)
3959 CDeclAttr(Attr.getRange(), S.Context,
3960 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003961 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003962 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00003963 D->addAttr(::new (S.Context)
3964 PascalAttr(Attr.getRange(), S.Context,
3965 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003966 return;
Charles Davise8519c32013-08-30 04:39:01 +00003967 case AttributeList::AT_MSABI:
3968 D->addAttr(::new (S.Context)
3969 MSABIAttr(Attr.getRange(), S.Context,
3970 Attr.getAttributeSpellingListIndex()));
3971 return;
3972 case AttributeList::AT_SysVABI:
3973 D->addAttr(::new (S.Context)
3974 SysVABIAttr(Attr.getRange(), S.Context,
3975 Attr.getAttributeSpellingListIndex()));
3976 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003977 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003978 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003979 switch (CC) {
3980 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003981 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003982 break;
3983 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003984 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003985 break;
3986 default:
3987 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003988 }
3989
Michael Han51d8c522013-01-24 16:46:58 +00003990 D->addAttr(::new (S.Context)
3991 PcsAttr(Attr.getRange(), S.Context, PCS,
3992 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003993 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003994 }
Derek Schuff263366f2012-10-16 22:30:41 +00003995 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00003996 D->addAttr(::new (S.Context)
3997 PnaclCallAttr(Attr.getRange(), S.Context,
3998 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003999 return;
Guy Benyei38980082012-12-25 08:53:55 +00004000 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00004001 D->addAttr(::new (S.Context)
4002 IntelOclBiccAttr(Attr.getRange(), S.Context,
4003 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00004004 return;
Derek Schuff263366f2012-10-16 22:30:41 +00004005
Abramo Bagnarae215f722010-04-30 13:10:51 +00004006 default:
4007 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00004008 }
4009}
4010
Chandler Carruth1b03c872011-07-02 00:01:44 +00004011static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004012 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00004013}
4014
Guy Benyei1db70402013-03-24 13:58:12 +00004015static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
Aaron Ballman624421f2013-08-31 01:11:41 +00004016 Expr *E = Attr.getArgAsExpr(0);
Guy Benyei1db70402013-03-24 13:58:12 +00004017 llvm::APSInt ArgNum(32);
4018 if (E->isTypeDependent() || E->isValueDependent() ||
4019 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00004020 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4021 << Attr.getName() << AANT_ArgumentIntegerConstant
4022 << E->getSourceRange();
Guy Benyei1db70402013-03-24 13:58:12 +00004023 return;
4024 }
4025
4026 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
4027 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
4028}
4029
Aaron Ballmanfff32482012-12-09 17:45:41 +00004030bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
4031 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00004032 if (attr.isInvalid())
4033 return true;
4034
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004035 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman624421f2013-08-31 01:11:41 +00004036 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall711c52b2011-01-05 12:14:39 +00004037 attr.setInvalid();
4038 return true;
4039 }
4040
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004041 // TODO: diagnose uses of these conventions on the wrong target. Or, better
4042 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00004043 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004044 case AttributeList::AT_CDecl: CC = CC_C; break;
4045 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
4046 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
4047 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
4048 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davise8519c32013-08-30 04:39:01 +00004049 case AttributeList::AT_MSABI:
4050 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
4051 CC_X86_64Win64;
4052 break;
4053 case AttributeList::AT_SysVABI:
4054 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
4055 CC_C;
4056 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004057 case AttributeList::AT_Pcs: {
Aaron Ballman624421f2013-08-31 01:11:41 +00004058 StringLiteral *Str = 0;
4059 if (attr.isArgExpr(0))
4060 Str = dyn_cast<StringLiteral>(attr.getArgAsExpr(0));
4061
Douglas Gregor5cee1192011-07-27 05:40:30 +00004062 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004063 Diag(attr.getLoc(), diag::err_attribute_argument_type) << attr.getName()
4064 << AANT_ArgumentString;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004065 attr.setInvalid();
4066 return true;
4067 }
4068
Chris Lattner5f9e2722011-07-23 10:55:15 +00004069 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004070 if (StrRef == "aapcs") {
4071 CC = CC_AAPCS;
4072 break;
4073 } else if (StrRef == "aapcs-vfp") {
4074 CC = CC_AAPCS_VFP;
4075 break;
4076 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004077
4078 attr.setInvalid();
4079 Diag(attr.getLoc(), diag::err_invalid_pcs);
4080 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004081 }
Derek Schuff263366f2012-10-16 22:30:41 +00004082 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00004083 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00004084 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00004085 }
4086
Aaron Ballman82bfa192012-10-02 14:26:08 +00004087 const TargetInfo &TI = Context.getTargetInfo();
4088 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
4089 if (A == TargetInfo::CCCR_Warning) {
4090 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00004091
4092 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
4093 if (FD)
4094 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
4095 TargetInfo::CCMT_NonMember;
4096 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00004097 }
4098
John McCall711c52b2011-01-05 12:14:39 +00004099 return false;
4100}
4101
Chandler Carruth1b03c872011-07-02 00:01:44 +00004102static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004103 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00004104
4105 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00004106 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00004107 return;
4108
Chandler Carruth87c44602011-07-01 23:49:12 +00004109 if (!isa<ObjCMethodDecl>(D)) {
4110 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4111 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004112 return;
4113 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004114
Michael Han51d8c522013-01-24 16:46:58 +00004115 D->addAttr(::new (S.Context)
4116 RegparmAttr(Attr.getRange(), S.Context, numParams,
4117 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00004118}
4119
4120/// Checks a regparm attribute, returning true if it is ill-formed and
4121/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00004122bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
4123 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00004124 return true;
4125
Aaron Ballmanffa9d572013-07-18 18:01:48 +00004126 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004127 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004128 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004129 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004130
Aaron Ballman624421f2013-08-31 01:11:41 +00004131 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004132 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00004133 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00004134 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00004135 Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4136 << Attr.getName() << AANT_ArgumentIntegerConstant
4137 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004138 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004139 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004140 }
4141
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004142 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004143 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004144 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004145 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004146 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004147 }
4148
John McCall711c52b2011-01-05 12:14:39 +00004149 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004150 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004151 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004152 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004153 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004154 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004155 }
4156
John McCall711c52b2011-01-05 12:14:39 +00004157 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004158}
4159
Chandler Carruth1b03c872011-07-02 00:01:44 +00004160static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00004161 if (S.LangOpts.CUDA) {
4162 // check the attribute arguments.
4163 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00004164 // FIXME: 0 is not okay.
4165 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004166 return;
4167 }
4168
Chandler Carruth87c44602011-07-01 23:49:12 +00004169 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00004170 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00004171 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004172 return;
4173 }
4174
Aaron Ballman624421f2013-08-31 01:11:41 +00004175 Expr *MaxThreadsExpr = Attr.getArgAsExpr(0);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004176 llvm::APSInt MaxThreads(32);
4177 if (MaxThreadsExpr->isTypeDependent() ||
4178 MaxThreadsExpr->isValueDependent() ||
4179 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004180 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004181 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004182 << MaxThreadsExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004183 return;
4184 }
4185
4186 llvm::APSInt MinBlocks(32);
4187 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004188 Expr *MinBlocksExpr = Attr.getArgAsExpr(1);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004189 if (MinBlocksExpr->isTypeDependent() ||
4190 MinBlocksExpr->isValueDependent() ||
4191 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004192 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004193 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004194 << MinBlocksExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004195 return;
4196 }
4197 }
4198
Michael Han51d8c522013-01-24 16:46:58 +00004199 D->addAttr(::new (S.Context)
4200 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4201 MaxThreads.getZExtValue(),
4202 MinBlocks.getZExtValue(),
4203 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne7b381982010-12-12 23:03:07 +00004204 } else {
4205 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4206 }
4207}
4208
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004209static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4210 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004211 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004212 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004213 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004214 return;
4215 }
Aaron Ballman624421f2013-08-31 01:11:41 +00004216
4217 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004218 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004219
Aaron Ballman624421f2013-08-31 01:11:41 +00004220 StringRef AttrName = Attr.getName()->getName();
4221 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004222
4223 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4224 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4225 << Attr.getName() << ExpectedFunctionOrMethod;
4226 return;
4227 }
4228
4229 uint64_t ArgumentIdx;
4230 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4231 Attr.getLoc(), 2,
Aaron Ballman624421f2013-08-31 01:11:41 +00004232 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004233 return;
4234
4235 uint64_t TypeTagIdx;
4236 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4237 Attr.getLoc(), 3,
Aaron Ballman624421f2013-08-31 01:11:41 +00004238 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004239 return;
4240
4241 bool IsPointer = (AttrName == "pointer_with_type_tag");
4242 if (IsPointer) {
4243 // Ensure that buffer has a pointer type.
4244 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4245 if (!BufferTy->isPointerType()) {
4246 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004247 << Attr.getName();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004248 }
4249 }
4250
Michael Han51d8c522013-01-24 16:46:58 +00004251 D->addAttr(::new (S.Context)
4252 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4253 ArgumentIdx, TypeTagIdx, IsPointer,
4254 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004255}
4256
4257static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4258 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004259 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004260 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004261 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004262 return;
4263 }
Aaron Ballman624421f2013-08-31 01:11:41 +00004264
4265 if (!checkAttributeNumArgs(S, Attr, 1))
4266 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004267
Aaron Ballman624421f2013-08-31 01:11:41 +00004268 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004269 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4270
Michael Han51d8c522013-01-24 16:46:58 +00004271 D->addAttr(::new (S.Context)
4272 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4273 MatchingCType,
4274 Attr.getLayoutCompatible(),
4275 Attr.getMustBeNull(),
4276 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004277}
4278
Chris Lattner0744e5f2008-06-29 00:23:49 +00004279//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00004280// Checker-specific attribute handlers.
4281//===----------------------------------------------------------------------===//
4282
John McCallc7ad3812011-01-25 03:31:58 +00004283static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004284 return type->isDependentType() ||
4285 type->isObjCObjectPointerType() ||
4286 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00004287}
4288static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004289 return type->isDependentType() ||
4290 type->isPointerType() ||
4291 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00004292}
4293
Chandler Carruth1b03c872011-07-02 00:01:44 +00004294static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004295 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00004296 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004297 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004298 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00004299 return;
4300 }
4301
4302 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00004303 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00004304 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4305 cf = false;
4306 } else {
4307 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4308 cf = true;
4309 }
4310
4311 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004312 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004313 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00004314 return;
4315 }
4316
4317 if (cf)
Michael Han51d8c522013-01-24 16:46:58 +00004318 param->addAttr(::new (S.Context)
4319 CFConsumedAttr(Attr.getRange(), S.Context,
4320 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004321 else
Michael Han51d8c522013-01-24 16:46:58 +00004322 param->addAttr(::new (S.Context)
4323 NSConsumedAttr(Attr.getRange(), S.Context,
4324 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004325}
4326
Chandler Carruth1b03c872011-07-02 00:01:44 +00004327static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4328 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004329 if (!isa<ObjCMethodDecl>(D)) {
4330 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004331 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00004332 return;
4333 }
4334
Michael Han51d8c522013-01-24 16:46:58 +00004335 D->addAttr(::new (S.Context)
4336 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4337 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004338}
4339
Chandler Carruth1b03c872011-07-02 00:01:44 +00004340static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4341 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004342
John McCallc7ad3812011-01-25 03:31:58 +00004343 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00004344
Chandler Carruth87c44602011-07-01 23:49:12 +00004345 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004346 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004347 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00004348 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00004349 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00004350 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4351 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00004352 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004353 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004354 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00004355 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004356 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00004357 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004358 return;
4359 }
Mike Stumpbf916502009-07-24 19:02:52 +00004360
John McCallc7ad3812011-01-25 03:31:58 +00004361 bool typeOK;
4362 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00004363 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00004364 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004365 case AttributeList::AT_NSReturnsAutoreleased:
4366 case AttributeList::AT_NSReturnsRetained:
4367 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004368 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4369 cf = false;
4370 break;
4371
Sean Hunt8e083e72012-06-19 23:57:03 +00004372 case AttributeList::AT_CFReturnsRetained:
4373 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004374 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4375 cf = true;
4376 break;
4377 }
4378
4379 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004380 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004381 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00004382 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004383 }
Mike Stumpbf916502009-07-24 19:02:52 +00004384
Chandler Carruth87c44602011-07-01 23:49:12 +00004385 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004386 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00004387 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004388 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han51d8c522013-01-24 16:46:58 +00004389 D->addAttr(::new (S.Context)
4390 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4391 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004392 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004393 case AttributeList::AT_CFReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004394 D->addAttr(::new (S.Context)
4395 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4396 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004397 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004398 case AttributeList::AT_NSReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004399 D->addAttr(::new (S.Context)
4400 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4401 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004402 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004403 case AttributeList::AT_CFReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004404 D->addAttr(::new (S.Context)
4405 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4406 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004407 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004408 case AttributeList::AT_NSReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004409 D->addAttr(::new (S.Context)
4410 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4411 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004412 return;
4413 };
4414}
4415
John McCalldc7c5ad2011-07-22 08:53:00 +00004416static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4417 const AttributeList &attr) {
4418 SourceLocation loc = attr.getLoc();
4419
4420 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4421
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004422 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00004423 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004424 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00004425 return;
4426 }
4427
4428 // Check that the method returns a normal pointer.
4429 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004430
4431 if (!resultType->isReferenceType() &&
4432 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00004433 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4434 << SourceRange(loc)
4435 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4436
4437 // Drop the attribute.
4438 return;
4439 }
4440
Michael Han51d8c522013-01-24 16:46:58 +00004441 method->addAttr(::new (S.Context)
4442 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4443 attr.getAttributeSpellingListIndex()));
John McCalldc7c5ad2011-07-22 08:53:00 +00004444}
4445
Fariborz Jahanian84101132012-09-07 23:46:23 +00004446static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4447 const AttributeList &attr) {
4448 SourceLocation loc = attr.getLoc();
4449 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4450
4451 if (!method) {
4452 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4453 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4454 return;
4455 }
4456 DeclContext *DC = method->getDeclContext();
4457 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4458 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4459 << attr.getName() << 0;
4460 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4461 return;
4462 }
4463 if (method->getMethodFamily() == OMF_dealloc) {
4464 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4465 << attr.getName() << 1;
4466 return;
4467 }
4468
Michael Han51d8c522013-01-24 16:46:58 +00004469 method->addAttr(::new (S.Context)
4470 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4471 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian84101132012-09-07 23:46:23 +00004472}
4473
John McCall8dfac0b2011-09-30 05:12:12 +00004474/// Handle cf_audited_transfer and cf_unknown_transfer.
4475static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4476 if (!isa<FunctionDecl>(D)) {
4477 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004478 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004479 return;
4480 }
4481
Sean Hunt8e083e72012-06-19 23:57:03 +00004482 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004483
4484 // Check whether there's a conflicting attribute already present.
4485 Attr *Existing;
4486 if (IsAudited) {
4487 Existing = D->getAttr<CFUnknownTransferAttr>();
4488 } else {
4489 Existing = D->getAttr<CFAuditedTransferAttr>();
4490 }
4491 if (Existing) {
4492 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4493 << A.getName()
4494 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4495 << A.getRange() << Existing->getRange();
4496 return;
4497 }
4498
4499 // All clear; add the attribute.
4500 if (IsAudited) {
Michael Han51d8c522013-01-24 16:46:58 +00004501 D->addAttr(::new (S.Context)
4502 CFAuditedTransferAttr(A.getRange(), S.Context,
4503 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004504 } else {
Michael Han51d8c522013-01-24 16:46:58 +00004505 D->addAttr(::new (S.Context)
4506 CFUnknownTransferAttr(A.getRange(), S.Context,
4507 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004508 }
4509}
4510
John McCallfe98da02011-09-29 07:17:38 +00004511static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4512 const AttributeList &Attr) {
4513 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4514 if (!RD || RD->isUnion()) {
4515 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004516 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004517 }
4518
Aaron Ballman624421f2013-08-31 01:11:41 +00004519 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallfe98da02011-09-29 07:17:38 +00004520
4521 // In Objective-C, verify that the type names an Objective-C type.
4522 // We don't want to check this outside of ObjC because people sometimes
4523 // do crazy C declarations of Objective-C types.
Aaron Ballman624421f2013-08-31 01:11:41 +00004524 if (Parm && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004525 // Check for an existing type with this name.
Aaron Ballman624421f2013-08-31 01:11:41 +00004526 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallfe98da02011-09-29 07:17:38 +00004527 Sema::LookupOrdinaryName);
4528 if (S.LookupName(R, Sc)) {
4529 NamedDecl *Target = R.getFoundDecl();
4530 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4531 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4532 S.Diag(Target->getLocStart(), diag::note_declared_at);
4533 }
4534 }
4535 }
4536
Michael Han51d8c522013-01-24 16:46:58 +00004537 D->addAttr(::new (S.Context)
Aaron Ballman624421f2013-08-31 01:11:41 +00004538 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han51d8c522013-01-24 16:46:58 +00004539 Attr.getAttributeSpellingListIndex()));
John McCallfe98da02011-09-29 07:17:38 +00004540}
4541
Chandler Carruth1b03c872011-07-02 00:01:44 +00004542static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4543 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004544 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004545
Chandler Carruth87c44602011-07-01 23:49:12 +00004546 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004547 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004548}
4549
Chandler Carruth1b03c872011-07-02 00:01:44 +00004550static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4551 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004552 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004553 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004554 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004555 return;
4556 }
4557
Chandler Carruth87c44602011-07-01 23:49:12 +00004558 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004559 QualType type = vd->getType();
4560
4561 if (!type->isDependentType() &&
4562 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004563 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004564 << type;
4565 return;
4566 }
4567
4568 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4569
4570 // If we have no lifetime yet, check the lifetime we're presumably
4571 // going to infer.
4572 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4573 lifetime = type->getObjCARCImplicitLifetime();
4574
4575 switch (lifetime) {
4576 case Qualifiers::OCL_None:
4577 assert(type->isDependentType() &&
4578 "didn't infer lifetime for non-dependent type?");
4579 break;
4580
4581 case Qualifiers::OCL_Weak: // meaningful
4582 case Qualifiers::OCL_Strong: // meaningful
4583 break;
4584
4585 case Qualifiers::OCL_ExplicitNone:
4586 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004587 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004588 << (lifetime == Qualifiers::OCL_Autoreleasing);
4589 break;
4590 }
4591
Chandler Carruth87c44602011-07-01 23:49:12 +00004592 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004593 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4594 Attr.getAttributeSpellingListIndex()));
John McCallf85e1932011-06-15 23:02:42 +00004595}
4596
Francois Pichet11542142010-12-19 06:50:37 +00004597//===----------------------------------------------------------------------===//
4598// Microsoft specific attribute handlers.
4599//===----------------------------------------------------------------------===//
4600
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004601// Check if MS extensions or some other language extensions are enabled. If
4602// not, issue a diagnostic that the given attribute is unused.
4603static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4604 bool OtherExtension = false) {
4605 if (S.LangOpts.MicrosoftExt || OtherExtension)
4606 return true;
4607 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4608 return false;
4609}
4610
Chandler Carruth1b03c872011-07-02 00:01:44 +00004611static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004612 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4613 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004614
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004615 // check the attribute arguments.
4616 if (!checkAttributeNumArgs(S, Attr, 1))
4617 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004618
Aaron Ballman624421f2013-08-31 01:11:41 +00004619 Expr *Arg = Attr.getArgAsExpr(0);
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004620 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
4621 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004622 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4623 << Attr.getName() << AANT_ArgumentString;
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004624 return;
4625 }
Francois Pichetd3d3be92010-12-20 01:41:49 +00004626
David Majnemerbb0ed292013-08-09 08:56:20 +00004627 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4628 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004629 StringRef StrRef = Str->getString();
David Majnemerbb0ed292013-08-09 08:56:20 +00004630 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4631 StrRef = StrRef.drop_front().drop_back();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004632
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004633 // Validate GUID length.
David Majnemerbb0ed292013-08-09 08:56:20 +00004634 if (StrRef.size() != 36) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004635 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4636 return;
4637 }
Anders Carlssonf89e0422011-01-23 21:07:30 +00004638
David Majnemerbb0ed292013-08-09 08:56:20 +00004639 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004640 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemerbb0ed292013-08-09 08:56:20 +00004641 if (StrRef[i] != '-') {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004642 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4643 return;
4644 }
David Majnemerbb0ed292013-08-09 08:56:20 +00004645 } else if (!isHexDigit(StrRef[i])) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004646 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4647 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004648 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004649 }
Francois Pichet11542142010-12-19 06:50:37 +00004650
David Majnemerbb0ed292013-08-09 08:56:20 +00004651 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4652 Attr.getAttributeSpellingListIndex()));
Charles Davisf0122fe2010-02-16 18:27:26 +00004653}
4654
John McCallc052dbb2012-05-22 21:28:12 +00004655static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004656 if (!checkMicrosoftExt(S, Attr))
Nico Weber7b89ab72012-11-07 21:31:36 +00004657 return;
Nico Weber7b89ab72012-11-07 21:31:36 +00004658
4659 AttributeList::Kind Kind = Attr.getKind();
4660 if (Kind == AttributeList::AT_SingleInheritance)
4661 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004662 ::new (S.Context)
4663 SingleInheritanceAttr(Attr.getRange(), S.Context,
4664 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004665 else if (Kind == AttributeList::AT_MultipleInheritance)
4666 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004667 ::new (S.Context)
4668 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4669 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004670 else if (Kind == AttributeList::AT_VirtualInheritance)
4671 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004672 ::new (S.Context)
4673 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4674 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004675}
4676
4677static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004678 if (!checkMicrosoftExt(S, Attr))
4679 return;
4680
4681 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004682 if (Kind == AttributeList::AT_Win64)
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004683 D->addAttr(
4684 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4685 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004686}
4687
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004688static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004689 if (!checkMicrosoftExt(S, Attr))
4690 return;
4691 D->addAttr(::new (S.Context)
4692 ForceInlineAttr(Attr.getRange(), S.Context,
4693 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004694}
4695
Reid Klecknera7225342013-05-20 14:02:37 +00004696static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4697 if (!checkMicrosoftExt(S, Attr))
4698 return;
4699 // Check linkage after possibly merging declaratinos. See
4700 // checkAttributesAfterMerging().
4701 D->addAttr(::new (S.Context)
4702 SelectAnyAttr(Attr.getRange(), S.Context,
4703 Attr.getAttributeSpellingListIndex()));
4704}
4705
Ted Kremenekb71368d2009-05-09 02:44:38 +00004706//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004707// Top Level Sema Entry Points
4708//===----------------------------------------------------------------------===//
4709
Richard Smith4a97b8e2013-08-29 00:47:48 +00004710/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4711/// the attribute applies to decls. If the attribute is a type attribute, just
4712/// silently ignore it if a GNU attribute.
4713static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4714 const AttributeList &Attr,
4715 bool IncludeCXX11Attributes) {
4716 if (Attr.isInvalid())
4717 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00004718
Richard Smith4a97b8e2013-08-29 00:47:48 +00004719 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4720 // instead.
4721 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4722 return;
4723
Chris Lattner803d0802008-06-29 00:43:07 +00004724 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004725 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4726 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4727 case AttributeList::AT_IBOutletCollection:
4728 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004729 case AttributeList::AT_AddressSpace:
Sean Hunt8e083e72012-06-19 23:57:03 +00004730 case AttributeList::AT_ObjCGC:
4731 case AttributeList::AT_VectorSize:
4732 case AttributeList::AT_NeonVectorType:
4733 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004734 case AttributeList::AT_Ptr32:
4735 case AttributeList::AT_Ptr64:
4736 case AttributeList::AT_SPtr:
4737 case AttributeList::AT_UPtr:
Mike Stumpbf916502009-07-24 19:02:52 +00004738 // Ignore these, these are type attributes, handled by
4739 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004740 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004741 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4742 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4743 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4744 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004745 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004746 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004747 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004748 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004749 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4750 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4751 case AttributeList::AT_CarriesDependency:
Richard Smith3a2b7a12013-01-28 22:42:45 +00004752 handleDependencyAttr(S, scope, D, Attr);
4753 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004754 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4755 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4756 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004757 case AttributeList::AT_CXX11NoReturn:
4758 handleCXX11NoReturnAttr(S, D, Attr);
4759 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004760 case AttributeList::AT_Deprecated:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004761 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004762 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004763 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4764 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004765 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004766 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004767 case AttributeList::AT_MinSize:
4768 handleMinSizeAttr(S, D, Attr);
4769 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004770 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4771 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4772 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman19513232013-08-28 23:13:26 +00004773 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4774 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004775 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4776 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004777 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004778 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004779 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4780 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004781 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004782 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4783 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004784 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004785 case AttributeList::AT_ownership_returns:
4786 case AttributeList::AT_ownership_takes:
4787 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004788 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004789 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4790 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4791 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4792 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4793 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4794 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4795 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004796
Sean Hunt8e083e72012-06-19 23:57:03 +00004797 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004798 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004799 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004800 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004801
Sean Hunt8e083e72012-06-19 23:57:03 +00004802 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004803 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4804
Fariborz Jahanian84101132012-09-07 23:46:23 +00004805 case AttributeList::AT_ObjCRequiresSuper:
4806 handleObjCRequiresSuperAttr(S, D, Attr); break;
4807
Sean Hunt8e083e72012-06-19 23:57:03 +00004808 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004809 handleNSBridgedAttr(S, scope, D, Attr); break;
4810
Sean Hunt8e083e72012-06-19 23:57:03 +00004811 case AttributeList::AT_CFAuditedTransfer:
4812 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004813 handleCFTransferAttr(S, D, Attr); break;
4814
Ted Kremenekb71368d2009-05-09 02:44:38 +00004815 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004816 case AttributeList::AT_CFConsumed:
4817 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4818 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004819 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004820
Sean Hunt8e083e72012-06-19 23:57:03 +00004821 case AttributeList::AT_NSReturnsAutoreleased:
4822 case AttributeList::AT_NSReturnsNotRetained:
4823 case AttributeList::AT_CFReturnsNotRetained:
4824 case AttributeList::AT_NSReturnsRetained:
4825 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004826 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004827
Tanya Lattner0df579e2012-07-09 22:06:01 +00004828 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004829 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004830 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004831
Joey Gouly37453b92013-03-08 09:42:32 +00004832 case AttributeList::AT_VecTypeHint:
4833 handleVecTypeHint(S, D, Attr); break;
4834
Joey Gouly96cead52013-03-14 09:54:43 +00004835 case AttributeList::AT_Endian:
4836 handleEndianAttr(S, D, Attr);
4837 break;
4838
Sean Hunt8e083e72012-06-19 23:57:03 +00004839 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004840 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004841
Sean Hunt8e083e72012-06-19 23:57:03 +00004842 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4843 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4844 case AttributeList::AT_Unavailable:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004845 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004846 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004847 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004848 handleArcWeakrefUnavailableAttr (S, D, Attr);
4849 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004850 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004851 handleObjCRootClassAttr(S, D, Attr);
4852 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004853 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004854 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004855 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004856 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4857 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004858 handleReturnsTwiceAttr(S, D, Attr);
4859 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004860 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld4c3d662013-02-20 01:54:26 +00004861 case AttributeList::AT_Visibility:
4862 handleVisibilityAttr(S, D, Attr, false);
4863 break;
4864 case AttributeList::AT_TypeVisibility:
4865 handleVisibilityAttr(S, D, Attr, true);
4866 break;
Lubos Lunak1d3ce652013-07-20 15:05:36 +00004867 case AttributeList::AT_WarnUnused:
4868 handleWarnUnusedAttr(S, D, Attr);
4869 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004870 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004871 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004872 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4873 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4874 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4875 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004876 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004877 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004878 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004879 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004880 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004881 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004882 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004883 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004884 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4885 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4886 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4887 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4888 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4889 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4890 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4891 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4892 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004893 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004894 // Just ignore
4895 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004896 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004897 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004898 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004899 case AttributeList::AT_StdCall:
4900 case AttributeList::AT_CDecl:
4901 case AttributeList::AT_FastCall:
4902 case AttributeList::AT_ThisCall:
4903 case AttributeList::AT_Pascal:
Charles Davise8519c32013-08-30 04:39:01 +00004904 case AttributeList::AT_MSABI:
4905 case AttributeList::AT_SysVABI:
Sean Hunt8e083e72012-06-19 23:57:03 +00004906 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004907 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004908 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004909 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004910 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004911 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004912 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004913 break;
Guy Benyei1db70402013-03-24 13:58:12 +00004914 case AttributeList::AT_OpenCLImageAccess:
4915 handleOpenCLImageAccessAttr(S, D, Attr);
4916 break;
John McCallc052dbb2012-05-22 21:28:12 +00004917
4918 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004919 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004920 handleMsStructAttr(S, D, Attr);
4921 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004922 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004923 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004924 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004925 case AttributeList::AT_SingleInheritance:
4926 case AttributeList::AT_MultipleInheritance:
4927 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004928 handleInheritanceAttr(S, D, Attr);
4929 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004930 case AttributeList::AT_Win64:
John McCallc052dbb2012-05-22 21:28:12 +00004931 handlePortabilityAttr(S, D, Attr);
4932 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004933 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004934 handleForceInlineAttr(S, D, Attr);
4935 break;
Reid Klecknera7225342013-05-20 14:02:37 +00004936 case AttributeList::AT_SelectAny:
4937 handleSelectAnyAttr(S, D, Attr);
4938 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004939
4940 // Thread safety attributes:
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +00004941 case AttributeList::AT_AssertExclusiveLock:
4942 handleAssertExclusiveLockAttr(S, D, Attr);
4943 break;
4944 case AttributeList::AT_AssertSharedLock:
4945 handleAssertSharedLockAttr(S, D, Attr);
4946 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004947 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004948 handleGuardedVarAttr(S, D, Attr);
4949 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004950 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004951 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004952 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004953 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004954 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004955 break;
Kostya Serebryany85aee962013-02-26 06:58:27 +00004956 case AttributeList::AT_NoSanitizeAddress:
4957 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany71efba02012-01-24 19:25:38 +00004958 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004959 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany85aee962013-02-26 06:58:27 +00004960 handleNoThreadSafetyAnalysis(S, D, Attr);
4961 break;
4962 case AttributeList::AT_NoSanitizeThread:
4963 handleNoSanitizeThread(S, D, Attr);
4964 break;
4965 case AttributeList::AT_NoSanitizeMemory:
4966 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004967 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004968 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004969 handleLockableAttr(S, D, Attr);
4970 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004971 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004972 handleGuardedByAttr(S, D, Attr);
4973 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004974 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004975 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004976 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004977 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004978 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004979 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004980 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004981 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004982 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004983 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004984 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004985 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004986 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004987 handleLockReturnedAttr(S, D, Attr);
4988 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004989 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004990 handleLocksExcludedAttr(S, D, Attr);
4991 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004992 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004993 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004994 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004995 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004996 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004997 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004998 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004999 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005000 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005001 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005002 handleUnlockFunAttr(S, D, Attr);
5003 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005004 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00005005 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005006 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005007 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00005008 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005009 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005010
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00005011 // Uniqueness analysis attributes.
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00005012 case AttributeList::AT_Consumable:
5013 handleConsumableAttr(S, D, Attr);
5014 break;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00005015 case AttributeList::AT_Consumes:
5016 handleConsumesAttr(S, D, Attr);
5017 break;
5018 case AttributeList::AT_CallableWhenUnconsumed:
5019 handleCallableWhenUnconsumedAttr(S, D, Attr);
5020 break;
5021 case AttributeList::AT_TestsConsumed:
5022 handleTestsConsumedAttr(S, D, Attr);
5023 break;
5024 case AttributeList::AT_TestsUnconsumed:
5025 handleTestsUnconsumedAttr(S, D, Attr);
5026 break;
5027
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00005028 // Type safety attributes.
5029 case AttributeList::AT_ArgumentWithTypeTag:
5030 handleArgumentWithTypeTagAttr(S, D, Attr);
5031 break;
5032 case AttributeList::AT_TypeTagForDatatype:
5033 handleTypeTagForDatatypeAttr(S, D, Attr);
5034 break;
5035
Chris Lattner803d0802008-06-29 00:43:07 +00005036 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005037 // Ask target about the attribute.
5038 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
5039 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00005040 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
5041 diag::warn_unhandled_ms_attribute_ignored :
5042 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00005043 break;
5044 }
5045}
5046
5047/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5048/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00005049void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00005050 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00005051 bool IncludeCXX11Attributes) {
5052 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005053 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005054
5055 // GCC accepts
5056 // static int a9 __attribute__((weakref));
5057 // but that looks really pointless. We reject it.
Richard Smith4a97b8e2013-08-29 00:47:48 +00005058 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005059 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00005060 cast<NamedDecl>(D)->getNameAsString();
5061 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005062 return;
Chris Lattner803d0802008-06-29 00:43:07 +00005063 }
5064}
5065
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005066// Annotation attributes are the only attributes allowed after an access
5067// specifier.
5068bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5069 const AttributeList *AttrList) {
5070 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00005071 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005072 handleAnnotateAttr(*this, ASDecl, *l);
5073 } else {
5074 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5075 return true;
5076 }
5077 }
5078
5079 return false;
5080}
5081
John McCalle82247a2011-10-01 05:17:03 +00005082/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5083/// contains any decl attributes that we should warn about.
5084static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5085 for ( ; A; A = A->getNext()) {
5086 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smithd03de6a2013-01-29 10:02:16 +00005087 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCalle82247a2011-10-01 05:17:03 +00005088 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5089
5090 if (A->getKind() == AttributeList::UnknownAttribute) {
5091 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5092 << A->getName() << A->getRange();
5093 } else {
5094 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5095 << A->getName() << A->getRange();
5096 }
5097 }
5098}
5099
5100/// checkUnusedDeclAttributes - Given a declarator which is not being
5101/// used to build a declaration, complain about any decl attributes
5102/// which might be lying around on it.
5103void Sema::checkUnusedDeclAttributes(Declarator &D) {
5104 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5105 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5106 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5107 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5108}
5109
Ryan Flynne25ff832009-07-30 03:15:39 +00005110/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00005111/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00005112NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5113 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005114 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00005115 NamedDecl *NewD = 0;
5116 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00005117 FunctionDecl *NewFD;
5118 // FIXME: Missing call to CheckFunctionDeclaration().
5119 // FIXME: Mangling?
5120 // FIXME: Is the qualifier info correct?
5121 // FIXME: Is the DeclContext correct?
5122 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5123 Loc, Loc, DeclarationName(II),
5124 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005125 SC_None, false/*isInlineSpecified*/,
Eli Friedman900693b2011-09-07 04:05:06 +00005126 FD->hasPrototype(),
5127 false/*isConstexprSpecified*/);
5128 NewD = NewFD;
5129
5130 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005131 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00005132
5133 // Fake up parameter variables; they are declared as if this were
5134 // a typedef.
5135 QualType FDTy = FD->getType();
5136 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5137 SmallVector<ParmVarDecl*, 16> Params;
5138 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5139 AE = FT->arg_type_end(); AI != AE; ++AI) {
5140 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5141 Param->setScopeInfo(0, Params.size());
5142 Params.push_back(Param);
5143 }
David Blaikie4278c652011-09-21 18:16:56 +00005144 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00005145 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005146 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5147 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005148 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00005149 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005150 VD->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00005151 if (VD->getQualifier()) {
5152 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005153 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00005154 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005155 }
5156 return NewD;
5157}
5158
James Dennett1dfbd922012-06-14 21:40:34 +00005159/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00005160/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005161void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005162 if (W.getUsed()) return; // only do this once
5163 W.setUsed(true);
5164 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5165 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00005166 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00005167 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5168 NDId->getName()));
5169 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005170 WeakTopLevelDecl.push_back(NewD);
5171 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5172 // to insert Decl at TU scope, sorry.
5173 DeclContext *SavedContext = CurContext;
5174 CurContext = Context.getTranslationUnitDecl();
5175 PushOnScopeChains(NewD, S);
5176 CurContext = SavedContext;
5177 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00005178 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00005179 }
5180}
5181
Rafael Espindola65611bf2013-03-02 21:41:48 +00005182void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5183 // It's valid to "forward-declare" #pragma weak, in which case we
5184 // have to do this.
5185 LoadExternalWeakUndeclaredIdentifiers();
5186 if (!WeakUndeclaredIdentifiers.empty()) {
5187 NamedDecl *ND = NULL;
5188 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5189 if (VD->isExternC())
5190 ND = VD;
5191 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5192 if (FD->isExternC())
5193 ND = FD;
5194 if (ND) {
5195 if (IdentifierInfo *Id = ND->getIdentifier()) {
5196 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5197 = WeakUndeclaredIdentifiers.find(Id);
5198 if (I != WeakUndeclaredIdentifiers.end()) {
5199 WeakInfo W = I->second;
5200 DeclApplyPragmaWeak(S, ND, W);
5201 WeakUndeclaredIdentifiers[Id] = W;
5202 }
5203 }
5204 }
5205 }
5206}
5207
Chris Lattner0744e5f2008-06-29 00:23:49 +00005208/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5209/// it, apply them to D. This is a bit tricky because PD can have attributes
5210/// specified in many different places, and we need to find and apply them all.
Richard Smith4a97b8e2013-08-29 00:47:48 +00005211void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner0744e5f2008-06-29 00:23:49 +00005212 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00005213 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005214 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00005215
Chris Lattner0744e5f2008-06-29 00:23:49 +00005216 // Walk the declarator structure, applying decl attributes that were in a type
5217 // position to the decl itself. This handles cases like:
5218 // int *__attr__(x)** D;
5219 // when X is a decl attribute.
5220 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5221 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005222 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00005223
Chris Lattner0744e5f2008-06-29 00:23:49 +00005224 // Finally, apply any attributes on the decl itself.
5225 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005226 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00005227}
John McCall54abf7d2009-11-04 02:18:39 +00005228
John McCallf85e1932011-06-15 23:02:42 +00005229/// Is the given declaration allowed to use a forbidden type?
5230static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5231 // Private ivars are always okay. Unfortunately, people don't
5232 // always properly make their ivars private, even in system headers.
5233 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00005234 // Function declarations in sys headers will be marked unavailable.
5235 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5236 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00005237 return false;
5238
5239 // Require it to be declared in a system header.
5240 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5241}
5242
5243/// Handle a delayed forbidden-type diagnostic.
5244static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5245 Decl *decl) {
5246 if (decl && isForbiddenTypeAllowed(S, decl)) {
5247 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5248 "this system declaration uses an unsupported type"));
5249 return;
5250 }
David Blaikie4e4d0842012-03-11 07:00:24 +00005251 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005252 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00005253 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005254 // kind of forbidden type messages on unavailable functions.
5255 if (FD->hasAttr<UnavailableAttr>() &&
5256 diag.getForbiddenTypeDiagnostic() ==
5257 diag::err_arc_array_param_no_ownership) {
5258 diag.Triggered = true;
5259 return;
5260 }
5261 }
John McCallf85e1932011-06-15 23:02:42 +00005262
5263 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5264 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5265 diag.Triggered = true;
5266}
5267
John McCall92576642012-05-07 06:16:41 +00005268void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5269 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00005270 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00005271 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00005272
John McCall92576642012-05-07 06:16:41 +00005273 // When delaying diagnostics to run in the context of a parsed
5274 // declaration, we only want to actually emit anything if parsing
5275 // succeeds.
5276 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00005277
John McCall92576642012-05-07 06:16:41 +00005278 // We emit all the active diagnostics in this pool or any of its
5279 // parents. In general, we'll get one pool for the decl spec
5280 // and a child pool for each declarator; in a decl group like:
5281 // deprecated_typedef foo, *bar, baz();
5282 // only the declarator pops will be passed decls. This is correct;
5283 // we really do need to consider delayed diagnostics from the decl spec
5284 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005285 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005286 do {
John McCall13489672012-05-07 06:16:58 +00005287 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005288 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5289 // This const_cast is a bit lame. Really, Triggered should be mutable.
5290 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005291 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005292 continue;
5293
John McCalleee1d542011-02-14 07:13:47 +00005294 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005295 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005296 // Don't bother giving deprecation diagnostics if the decl is invalid.
5297 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005298 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005299 break;
5300
5301 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005302 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005303 break;
John McCallf85e1932011-06-15 23:02:42 +00005304
5305 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005306 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005307 break;
John McCall2f514482010-01-27 03:50:35 +00005308 }
5309 }
John McCall92576642012-05-07 06:16:41 +00005310 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005311}
5312
John McCall13489672012-05-07 06:16:58 +00005313/// Given a set of delayed diagnostics, re-emit them as if they had
5314/// been delayed in the current context instead of in the given pool.
5315/// Essentially, this just moves them to the current pool.
5316void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5317 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5318 assert(curPool && "re-emitting in undelayed context not supported");
5319 curPool->steal(pool);
5320}
5321
John McCall54abf7d2009-11-04 02:18:39 +00005322static bool isDeclDeprecated(Decl *D) {
5323 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005324 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005325 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005326 // A category implicitly has the availability of the interface.
5327 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5328 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005329 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5330 return false;
5331}
5332
Eli Friedmanc3b23082012-08-08 21:52:41 +00005333static void
5334DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5335 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005336 const ObjCInterfaceDecl *UnknownObjCClass,
5337 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005338 DeclarationName Name = D->getDeclName();
5339 if (!Message.empty()) {
5340 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5341 S.Diag(D->getLocation(),
5342 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5343 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005344 if (ObjCPropery)
5345 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5346 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005347 } else if (!UnknownObjCClass) {
5348 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5349 S.Diag(D->getLocation(),
5350 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5351 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005352 if (ObjCPropery)
5353 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5354 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005355 } else {
5356 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5357 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5358 }
5359}
5360
John McCall9c3087b2010-08-26 02:13:20 +00005361void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005362 Decl *Ctx) {
5363 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005364 return;
5365
John McCall2f514482010-01-27 03:50:35 +00005366 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005367 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5368 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005369 DD.getUnknownObjCClass(),
5370 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005371}
5372
Chris Lattner5f9e2722011-07-23 10:55:15 +00005373void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005374 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005375 const ObjCInterfaceDecl *UnknownObjCClass,
5376 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005377 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005378 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005379 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5380 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005381 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005382 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005383 return;
5384 }
5385
5386 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005387 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005388 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005389 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005390}