blob: 97d12d57dcb3b133eb9add45ebf44556aa945fc7 [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
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001072static void handleReturnTypestateAttr(Sema &S, Decl *D,
1073 const AttributeList &Attr) {
1074 if (!checkAttributeNumArgs(S, Attr, 1)) return;
1075
1076 ReturnTypestateAttr::ConsumedState ReturnState;
1077
1078 if (Attr.isArgIdent(0)) {
1079 StringRef Param = Attr.getArgAsIdent(0)->Ident->getName();
1080
1081 if (Param == "unknown") {
1082 ReturnState = ReturnTypestateAttr::Unknown;
1083 } else if (Param == "consumed") {
1084 ReturnState = ReturnTypestateAttr::Consumed;
1085 } else if (Param == "unconsumed") {
1086 ReturnState = ReturnTypestateAttr::Unconsumed;
1087 } else {
1088 S.Diag(Attr.getLoc(), diag::warn_unknown_consumed_state) << Param;
1089 return;
1090 }
1091
1092 } else {
1093 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1094 Attr.getName() << AANT_ArgumentIdentifier;
1095 return;
1096 }
1097
1098 if (!isa<FunctionDecl>(D)) {
1099 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1100 Attr.getName() << ExpectedFunction;
1101 return;
1102 }
1103
1104 // FIXME: This check is currently being done in the analysis. It can be
1105 // enabled here only after the parser propagates attributes at
1106 // template specialization definition, not declaration.
1107 //QualType ReturnType;
1108 //
1109 //if (const CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1110 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1111 //
1112 //} else {
1113 //
1114 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1115 //}
1116 //
1117 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1118 //
1119 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1120 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1121 // ReturnType.getAsString();
1122 // return;
1123 //}
1124
1125 D->addAttr(::new (S.Context)
1126 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1127 Attr.getAttributeSpellingListIndex()));
1128}
1129
Chandler Carruth1b03c872011-07-02 00:01:44 +00001130static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1131 const AttributeList &Attr) {
Richard Smitha4fa9002013-01-13 02:11:23 +00001132 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1133 if (TD == 0) {
1134 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1135 // and type-ids.
Chris Lattner803d0802008-06-29 00:43:07 +00001136 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +00001137 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001138 }
Mike Stumpbf916502009-07-24 19:02:52 +00001139
Richard Smitha4fa9002013-01-13 02:11:23 +00001140 // Remember this typedef decl, we will need it later for diagnostics.
1141 S.ExtVectorDecls.push_back(TD);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001142}
1143
Chandler Carruth1b03c872011-07-02 00:01:44 +00001144static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001145 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001146 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001147 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001148
Chandler Carruth87c44602011-07-01 23:49:12 +00001149 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001150 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +00001151 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001152 // If the alignment is less than or equal to 8 bits, the packed attribute
1153 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +00001154 if (!FD->getType()->isDependentType() &&
1155 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +00001156 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001157 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001158 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001159 else
Michael Han51d8c522013-01-24 16:46:58 +00001160 FD->addAttr(::new (S.Context)
1161 PackedAttr(Attr.getRange(), S.Context,
1162 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001163 } else
Chris Lattner3c73c412008-11-19 08:23:25 +00001164 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001165}
1166
Chandler Carruth1b03c872011-07-02 00:01:44 +00001167static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +00001168 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001169 RD->addAttr(::new (S.Context)
1170 MsStructAttr(Attr.getRange(), S.Context,
1171 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +00001172 else
1173 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1174}
1175
Chandler Carruth1b03c872011-07-02 00:01:44 +00001176static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +00001177 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001178 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +00001179 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001180
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001181 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +00001182 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001183 if (MD->isInstanceMethod()) {
Michael Han51d8c522013-01-24 16:46:58 +00001184 D->addAttr(::new (S.Context)
1185 IBActionAttr(Attr.getRange(), S.Context,
1186 Attr.getAttributeSpellingListIndex()));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001187 return;
1188 }
1189
Ted Kremenek4ee2bb12011-02-04 06:54:16 +00001190 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001191}
1192
Ted Kremenek2f041d02011-09-29 07:02:25 +00001193static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1194 // The IBOutlet/IBOutletCollection attributes only apply to instance
1195 // variables or properties of Objective-C classes. The outlet must also
1196 // have an object reference type.
1197 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1198 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001199 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001200 << Attr.getName() << VD->getType() << 0;
1201 return false;
1202 }
1203 }
1204 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1205 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001206 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001207 << Attr.getName() << PD->getType() << 1;
1208 return false;
1209 }
1210 }
1211 else {
1212 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1213 return false;
1214 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001215
Ted Kremenek2f041d02011-09-29 07:02:25 +00001216 return true;
1217}
1218
Chandler Carruth1b03c872011-07-02 00:01:44 +00001219static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001220 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001221 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001222 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001223
1224 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001225 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001226
Michael Han51d8c522013-01-24 16:46:58 +00001227 D->addAttr(::new (S.Context)
1228 IBOutletAttr(Attr.getRange(), S.Context,
1229 Attr.getAttributeSpellingListIndex()));
Ted Kremenek96329d42008-07-15 22:26:48 +00001230}
1231
Chandler Carruth1b03c872011-07-02 00:01:44 +00001232static void handleIBOutletCollection(Sema &S, Decl *D,
1233 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001234
1235 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00001236 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001237 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1238 << Attr.getName() << 1;
Ted Kremenek857e9182010-05-19 17:38:06 +00001239 return;
1240 }
1241
Ted Kremenek2f041d02011-09-29 07:02:25 +00001242 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001243 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001244
Aaron Ballman624421f2013-08-31 01:11:41 +00001245 IdentifierLoc *IL = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
1246 IdentifierInfo *II;
1247 SourceLocation ILS;
1248 if (IL) {
1249 II = IL->Ident;
1250 ILS = IL->Loc;
1251 } else {
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001252 II = &S.Context.Idents.get("NSObject");
Aaron Ballman624421f2013-08-31 01:11:41 +00001253 }
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001254
John McCallb3d87482010-08-24 05:47:05 +00001255 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001256 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001257 if (!TypeRep) {
1258 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1259 return;
1260 }
John McCallb3d87482010-08-24 05:47:05 +00001261 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001262 // Diagnose use of non-object type in iboutletcollection attribute.
1263 // FIXME. Gnu attribute extension ignores use of builtin types in
1264 // attributes. So, __attribute__((iboutletcollection(char))) will be
1265 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001266 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001267 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1268 return;
1269 }
Michael Han51d8c522013-01-24 16:46:58 +00001270 D->addAttr(::new (S.Context)
Aaron Ballman624421f2013-08-31 01:11:41 +00001271 IBOutletCollectionAttr(Attr.getRange(), S.Context, QT, ILS,
Michael Han51d8c522013-01-24 16:46:58 +00001272 Attr.getAttributeSpellingListIndex()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001273}
1274
Chandler Carruthd309c812011-07-01 23:49:16 +00001275static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001276 if (const RecordType *UT = T->getAsUnionType())
1277 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1278 RecordDecl *UD = UT->getDecl();
1279 for (RecordDecl::field_iterator it = UD->field_begin(),
1280 itend = UD->field_end(); it != itend; ++it) {
1281 QualType QT = it->getType();
1282 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1283 T = QT;
1284 return;
1285 }
1286 }
1287 }
1288}
1289
Nuno Lopes587de5b2012-05-24 00:22:00 +00001290static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001291 if (!isFunctionOrMethod(D)) {
1292 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001293 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopes174930d2012-06-18 16:39:04 +00001294 return;
1295 }
1296
Nuno Lopes587de5b2012-05-24 00:22:00 +00001297 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1298 return;
1299
Nuno Lopes587de5b2012-05-24 00:22:00 +00001300 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001301 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001302 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001303 uint64_t Idx;
1304 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1305 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes587de5b2012-05-24 00:22:00 +00001306 return;
Nuno Lopes587de5b2012-05-24 00:22:00 +00001307
1308 // check if the function argument is of an integer type
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001309 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001310 if (!T->isIntegerType()) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00001311 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1312 << Attr.getName() << AANT_ArgumentIntegerConstant
1313 << Ex->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001314 return;
1315 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001316 SizeArgs.push_back(Idx);
Nuno Lopes587de5b2012-05-24 00:22:00 +00001317 }
1318
1319 // check if the function returns a pointer
1320 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1321 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001322 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001323 }
1324
Michael Han51d8c522013-01-24 16:46:58 +00001325 D->addAttr(::new (S.Context)
1326 AllocSizeAttr(Attr.getRange(), S.Context,
1327 SizeArgs.data(), SizeArgs.size(),
1328 Attr.getAttributeSpellingListIndex()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001329}
1330
Chandler Carruth1b03c872011-07-02 00:01:44 +00001331static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001332 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1333 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001334 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001335 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001336 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001337 return;
1338 }
Mike Stumpbf916502009-07-24 19:02:52 +00001339
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001340 SmallVector<unsigned, 8> NonNullArgs;
1341 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001342 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001343 uint64_t Idx;
1344 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1345 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001346 return;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001347
1348 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001349 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001350 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001351
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001352 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001353 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001354 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001355 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001356 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001357 }
Mike Stumpbf916502009-07-24 19:02:52 +00001358
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001359 NonNullArgs.push_back(Idx);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001360 }
Mike Stumpbf916502009-07-24 19:02:52 +00001361
1362 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1363 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001364 if (NonNullArgs.empty()) {
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001365 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1366 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001367 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001368 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001369 NonNullArgs.push_back(i);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001370 }
Mike Stumpbf916502009-07-24 19:02:52 +00001371
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001372 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001373 if (NonNullArgs.empty()) {
1374 // Warn the trivial case only if attribute is not coming from a
1375 // macro instantiation.
1376 if (Attr.getLoc().isFileID())
1377 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001378 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001379 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001380 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001381
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001382 unsigned *start = &NonNullArgs[0];
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001383 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001384 llvm::array_pod_sort(start, start + size);
Michael Han51d8c522013-01-24 16:46:58 +00001385 D->addAttr(::new (S.Context)
1386 NonNullAttr(Attr.getRange(), S.Context, start, size,
1387 Attr.getAttributeSpellingListIndex()));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001388}
1389
Chandler Carruth1b03c872011-07-02 00:01:44 +00001390static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001391 // This attribute must be applied to a function declaration.
1392 // The first argument to the attribute must be a string,
1393 // the name of the resource, for example "malloc".
1394 // The following arguments must be argument indexes, the arguments must be
1395 // of integer type for Returns, otherwise of pointer type.
1396 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001397 // after being held. free() should be __attribute((ownership_takes)), whereas
1398 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001399
Aaron Ballman624421f2013-08-31 01:11:41 +00001400 if (!AL.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001401 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001402 << AL.getName()->getName() << 1 << AANT_ArgumentString;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001403 return;
1404 }
1405 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001406 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001407 switch (AL.getKind()) {
1408 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001409 K = OwnershipAttr::Takes;
Aaron Ballman624421f2013-08-31 01:11:41 +00001410 if (AL.getNumArgs() < 2) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001411 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1412 << AL.getName() << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001413 return;
1414 }
Jordy Rose2a479922010-08-12 08:54:03 +00001415 break;
1416 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001417 K = OwnershipAttr::Holds;
Aaron Ballman624421f2013-08-31 01:11:41 +00001418 if (AL.getNumArgs() < 2) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001419 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1420 << AL.getName() << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001421 return;
1422 }
Jordy Rose2a479922010-08-12 08:54:03 +00001423 break;
1424 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001425 K = OwnershipAttr::Returns;
Aaron Ballman624421f2013-08-31 01:11:41 +00001426 if (AL.getNumArgs() > 2) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001427 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
Aaron Ballman624421f2013-08-31 01:11:41 +00001428 << AL.getName() << AL.getNumArgs() + 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001429 return;
1430 }
Jordy Rose2a479922010-08-12 08:54:03 +00001431 break;
1432 default:
1433 // This should never happen given how we are called.
1434 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001435 }
1436
Chandler Carruth87c44602011-07-01 23:49:12 +00001437 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001438 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1439 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001440 return;
1441 }
1442
Aaron Ballman624421f2013-08-31 01:11:41 +00001443 StringRef Module = AL.getArgAsIdent(0)->Ident->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001444
1445 // Normalize the argument, __foo__ becomes foo.
1446 if (Module.startswith("__") && Module.endswith("__"))
1447 Module = Module.substr(2, Module.size() - 4);
1448
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001449
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001450 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman624421f2013-08-31 01:11:41 +00001451 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1452 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001453 uint64_t Idx;
1454 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
Aaron Ballman624421f2013-08-31 01:11:41 +00001455 AL.getLoc(), i, Ex, Idx))
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001456 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001457
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001458 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001459 case OwnershipAttr::Takes:
1460 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001461 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001462 QualType T = getFunctionOrMethodArgType(D, Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001463 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1464 // FIXME: Should also highlight argument in decl.
1465 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001466 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001467 << "pointer"
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001468 << Ex->getSourceRange();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001469 continue;
1470 }
1471 break;
1472 }
Sean Huntcf807c42010-08-18 23:23:40 +00001473 case OwnershipAttr::Returns: {
Aaron Ballman624421f2013-08-31 01:11:41 +00001474 if (AL.getNumArgs() > 2) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001475 // Is the function argument an integer type?
Aaron Ballman624421f2013-08-31 01:11:41 +00001476 Expr *IdxExpr = AL.getArgAsExpr(1);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001477 llvm::APSInt ArgNum(32);
1478 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1479 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1480 S.Diag(AL.getLoc(), diag::err_ownership_type)
1481 << "ownership_returns" << "integer"
1482 << IdxExpr->getSourceRange();
1483 return;
1484 }
1485 }
1486 break;
1487 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001488 } // switch
1489
1490 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001491 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001492 i = D->specific_attr_begin<OwnershipAttr>(),
1493 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001494 i != e; ++i) {
1495 if ((*i)->getOwnKind() != K) {
1496 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1497 I!=E; ++I) {
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001498 if (Idx == *I) {
Sean Huntcf807c42010-08-18 23:23:40 +00001499 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1500 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001501 }
1502 }
1503 }
1504 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001505 OwnershipArgs.push_back(Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001506 }
1507
1508 unsigned* start = OwnershipArgs.data();
1509 unsigned size = OwnershipArgs.size();
1510 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001511
1512 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001513 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1514 << AL.getName() << 2;
Sean Huntcf807c42010-08-18 23:23:40 +00001515 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001516 }
Sean Huntcf807c42010-08-18 23:23:40 +00001517
Michael Han51d8c522013-01-24 16:46:58 +00001518 D->addAttr(::new (S.Context)
1519 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1520 AL.getAttributeSpellingListIndex()));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001521}
1522
Chandler Carruth1b03c872011-07-02 00:01:44 +00001523static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001524 // Check the attribute arguments.
1525 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001526 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1527 << Attr.getName() << 1;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001528 return;
1529 }
1530
Chandler Carruth87c44602011-07-01 23:49:12 +00001531 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001532 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001533 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001534 return;
1535 }
1536
Chandler Carruth87c44602011-07-01 23:49:12 +00001537 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001538
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001539 // gcc rejects
1540 // class c {
1541 // static int a __attribute__((weakref ("v2")));
1542 // static int b() __attribute__((weakref ("f3")));
1543 // };
1544 // and ignores the attributes of
1545 // void f(void) {
1546 // static int a __attribute__((weakref ("v2")));
1547 // }
1548 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001549 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001550 if (!Ctx->isFileContext()) {
1551 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001552 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001553 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001554 }
1555
1556 // The GCC manual says
1557 //
1558 // At present, a declaration to which `weakref' is attached can only
1559 // be `static'.
1560 //
1561 // It also says
1562 //
1563 // Without a TARGET,
1564 // given as an argument to `weakref' or to `alias', `weakref' is
1565 // equivalent to `weak'.
1566 //
1567 // gcc 4.4.1 will accept
1568 // int a7 __attribute__((weakref));
1569 // as
1570 // int a7 __attribute__((weak));
1571 // This looks like a bug in gcc. We reject that for now. We should revisit
1572 // it if this behaviour is actually used.
1573
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001574 // GCC rejects
1575 // static ((alias ("y"), weakref)).
1576 // Should we? How to check that weakref is before or after alias?
1577
Aaron Ballman624421f2013-08-31 01:11:41 +00001578 if (Attr.isArgExpr(0)) {
1579 Expr *Arg = Attr.getArgAsExpr(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001580 Arg = Arg->IgnoreParenCasts();
1581 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1582
Douglas Gregor5cee1192011-07-27 05:40:30 +00001583 if (!Str || !Str->isAscii()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001584 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001585 << Attr.getName() << 1 << AANT_ArgumentString;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001586 return;
1587 }
1588 // GCC will accept anything as the argument of weakref. Should we
1589 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001590 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001591 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001592 }
1593
Michael Han51d8c522013-01-24 16:46:58 +00001594 D->addAttr(::new (S.Context)
1595 WeakRefAttr(Attr.getRange(), S.Context,
1596 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001597}
1598
Chandler Carruth1b03c872011-07-02 00:01:44 +00001599static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001600 // check the attribute arguments.
Aaron Ballmanffa9d572013-07-18 18:01:48 +00001601 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001602 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001603
Aaron Ballman624421f2013-08-31 01:11:41 +00001604 StringLiteral *Str = 0;
1605 if (Attr.isArgExpr(0))
1606 Str = dyn_cast<StringLiteral>(Attr.getArgAsExpr(0)->IgnoreParenCasts());
Mike Stumpbf916502009-07-24 19:02:52 +00001607
Douglas Gregor5cee1192011-07-27 05:40:30 +00001608 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001609 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1610 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001611 return;
1612 }
Mike Stumpbf916502009-07-24 19:02:52 +00001613
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001614 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001615 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1616 return;
1617 }
1618
Chris Lattner6b6b5372008-06-26 18:38:35 +00001619 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001620
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001621 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00001622 Str->getString(),
1623 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001624}
1625
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001626static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1627 // Check the attribute arguments.
1628 if (!checkAttributeNumArgs(S, Attr, 0))
1629 return;
1630
1631 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1632 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1633 << Attr.getName() << ExpectedFunctionOrMethod;
1634 return;
1635 }
1636
Michael Han51d8c522013-01-24 16:46:58 +00001637 D->addAttr(::new (S.Context)
1638 MinSizeAttr(Attr.getRange(), S.Context,
1639 Attr.getAttributeSpellingListIndex()));
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001640}
1641
Benjamin Krameree409a92012-05-12 21:10:52 +00001642static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1643 // Check the attribute arguments.
1644 if (!checkAttributeNumArgs(S, Attr, 0))
1645 return;
1646
1647 if (!isa<FunctionDecl>(D)) {
1648 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1649 << Attr.getName() << ExpectedFunction;
1650 return;
1651 }
1652
1653 if (D->hasAttr<HotAttr>()) {
1654 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1655 << Attr.getName() << "hot";
1656 return;
1657 }
1658
Michael Han51d8c522013-01-24 16:46:58 +00001659 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1660 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001661}
1662
1663static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1664 // Check the attribute arguments.
1665 if (!checkAttributeNumArgs(S, Attr, 0))
1666 return;
1667
1668 if (!isa<FunctionDecl>(D)) {
1669 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1670 << Attr.getName() << ExpectedFunction;
1671 return;
1672 }
1673
1674 if (D->hasAttr<ColdAttr>()) {
1675 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1676 << Attr.getName() << "cold";
1677 return;
1678 }
1679
Michael Han51d8c522013-01-24 16:46:58 +00001680 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1681 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001682}
1683
Chandler Carruth1b03c872011-07-02 00:01:44 +00001684static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001685 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001686 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001687 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001688
Chandler Carruth87c44602011-07-01 23:49:12 +00001689 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001690 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001691 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001692 return;
1693 }
1694
Michael Han51d8c522013-01-24 16:46:58 +00001695 D->addAttr(::new (S.Context)
1696 NakedAttr(Attr.getRange(), S.Context,
1697 Attr.getAttributeSpellingListIndex()));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001698}
1699
Chandler Carruth1b03c872011-07-02 00:01:44 +00001700static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1701 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001702 // Check the attribute arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00001703 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001704 return;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001705
Chandler Carruth87c44602011-07-01 23:49:12 +00001706 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001707 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001708 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001709 return;
1710 }
Mike Stumpbf916502009-07-24 19:02:52 +00001711
Michael Han51d8c522013-01-24 16:46:58 +00001712 D->addAttr(::new (S.Context)
1713 AlwaysInlineAttr(Attr.getRange(), S.Context,
1714 Attr.getAttributeSpellingListIndex()));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001715}
1716
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001717static void handleTLSModelAttr(Sema &S, Decl *D,
1718 const AttributeList &Attr) {
1719 // Check the attribute arguments.
Aaron Ballmanffa9d572013-07-18 18:01:48 +00001720 if (!checkAttributeNumArgs(S, Attr, 1))
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001721 return;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001722
Aaron Ballman624421f2013-08-31 01:11:41 +00001723 Expr *Arg = Attr.getArgAsExpr(0);
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001724 Arg = Arg->IgnoreParenCasts();
1725 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1726
1727 // Check that it is a string.
1728 if (!Str) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001729 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1730 << Attr.getName() << AANT_ArgumentString;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001731 return;
1732 }
1733
Richard Smith38afbc72013-04-13 02:43:54 +00001734 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001735 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1736 << Attr.getName() << ExpectedTLSVar;
1737 return;
1738 }
1739
1740 // Check that the value.
1741 StringRef Model = Str->getString();
1742 if (Model != "global-dynamic" && Model != "local-dynamic"
1743 && Model != "initial-exec" && Model != "local-exec") {
1744 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1745 return;
1746 }
1747
Michael Han51d8c522013-01-24 16:46:58 +00001748 D->addAttr(::new (S.Context)
1749 TLSModelAttr(Attr.getRange(), S.Context, Model,
1750 Attr.getAttributeSpellingListIndex()));
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001751}
1752
Chandler Carruth1b03c872011-07-02 00:01:44 +00001753static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001754 // Check the attribute arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00001755 if (!checkAttributeNumArgs(S, Attr, 0))
Ryan Flynn76168e22009-08-09 20:07:29 +00001756 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001757
Chandler Carruth87c44602011-07-01 23:49:12 +00001758 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001759 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001760 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han51d8c522013-01-24 16:46:58 +00001761 D->addAttr(::new (S.Context)
1762 MallocAttr(Attr.getRange(), S.Context,
1763 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001764 return;
1765 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001766 }
1767
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001768 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001769}
1770
Chandler Carruth1b03c872011-07-02 00:01:44 +00001771static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001772 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001773 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001774 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001775
Michael Han51d8c522013-01-24 16:46:58 +00001776 D->addAttr(::new (S.Context)
1777 MayAliasAttr(Attr.getRange(), S.Context,
1778 Attr.getAttributeSpellingListIndex()));
Dan Gohman34c26302010-11-17 00:03:07 +00001779}
1780
Chandler Carruth1b03c872011-07-02 00:01:44 +00001781static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001782 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001783 D->addAttr(::new (S.Context)
1784 NoCommonAttr(Attr.getRange(), S.Context,
1785 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001786 else
1787 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001788 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001789}
1790
Chandler Carruth1b03c872011-07-02 00:01:44 +00001791static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman3e1aca22013-06-20 22:55:04 +00001792 if (S.LangOpts.CPlusPlus) {
1793 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1794 return;
1795 }
1796
Chandler Carruth87c44602011-07-01 23:49:12 +00001797 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001798 D->addAttr(::new (S.Context)
1799 CommonAttr(Attr.getRange(), S.Context,
1800 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001801 else
1802 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001803 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001804}
1805
Chandler Carruth1b03c872011-07-02 00:01:44 +00001806static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001807 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001808
1809 if (S.CheckNoReturnAttr(attr)) return;
1810
Chandler Carruth87c44602011-07-01 23:49:12 +00001811 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001812 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001813 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001814 return;
1815 }
1816
Michael Han51d8c522013-01-24 16:46:58 +00001817 D->addAttr(::new (S.Context)
1818 NoReturnAttr(attr.getRange(), S.Context,
1819 attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00001820}
1821
1822bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001823 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall711c52b2011-01-05 12:14:39 +00001824 attr.setInvalid();
1825 return true;
1826 }
1827
1828 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001829}
1830
Chandler Carruth1b03c872011-07-02 00:01:44 +00001831static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1832 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001833
1834 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1835 // because 'analyzer_noreturn' does not impact the type.
1836
Chandler Carruth1731e202011-07-11 23:30:35 +00001837 if(!checkAttributeNumArgs(S, Attr, 0))
1838 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001839
Chandler Carruth87c44602011-07-01 23:49:12 +00001840 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1841 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001842 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1843 && !VD->getType()->isFunctionPointerType())) {
1844 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001845 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001846 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001847 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001848 return;
1849 }
1850 }
1851
Michael Han51d8c522013-01-24 16:46:58 +00001852 D->addAttr(::new (S.Context)
1853 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1854 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001855}
1856
Richard Smithcd8ab512013-01-17 01:30:42 +00001857static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1858 const AttributeList &Attr) {
1859 // C++11 [dcl.attr.noreturn]p1:
1860 // The attribute may be applied to the declarator-id in a function
1861 // declaration.
1862 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1863 if (!FD) {
1864 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1865 << Attr.getName() << ExpectedFunctionOrMethod;
1866 return;
1867 }
1868
Michael Han51d8c522013-01-24 16:46:58 +00001869 D->addAttr(::new (S.Context)
1870 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1871 Attr.getAttributeSpellingListIndex()));
Richard Smithcd8ab512013-01-17 01:30:42 +00001872}
1873
John Thompson35cc9622010-08-09 21:53:52 +00001874// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001875static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001876/*
1877 Returning a Vector Class in Registers
1878
Eric Christopherf48f3672010-12-01 22:13:54 +00001879 According to the PPU ABI specifications, a class with a single member of
1880 vector type is returned in memory when used as the return value of a function.
1881 This results in inefficient code when implementing vector classes. To return
1882 the value in a single vector register, add the vecreturn attribute to the
1883 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001884
1885 Example:
1886
1887 struct Vector
1888 {
1889 __vector float xyzw;
1890 } __attribute__((vecreturn));
1891
1892 Vector Add(Vector lhs, Vector rhs)
1893 {
1894 Vector result;
1895 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1896 return result; // This will be returned in a register
1897 }
1898*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001899 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001900 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001901 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001902 return;
1903 }
1904
Chandler Carruth87c44602011-07-01 23:49:12 +00001905 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001906 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1907 return;
1908 }
1909
Chandler Carruth87c44602011-07-01 23:49:12 +00001910 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001911 int count = 0;
1912
1913 if (!isa<CXXRecordDecl>(record)) {
1914 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1915 return;
1916 }
1917
1918 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1919 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1920 return;
1921 }
1922
Eric Christopherf48f3672010-12-01 22:13:54 +00001923 for (RecordDecl::field_iterator iter = record->field_begin();
1924 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001925 if ((count == 1) || !iter->getType()->isVectorType()) {
1926 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1927 return;
1928 }
1929 count++;
1930 }
1931
Michael Han51d8c522013-01-24 16:46:58 +00001932 D->addAttr(::new (S.Context)
1933 VecReturnAttr(Attr.getRange(), S.Context,
1934 Attr.getAttributeSpellingListIndex()));
John Thompson35cc9622010-08-09 21:53:52 +00001935}
1936
Richard Smith3a2b7a12013-01-28 22:42:45 +00001937static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1938 const AttributeList &Attr) {
1939 if (isa<ParmVarDecl>(D)) {
1940 // [[carries_dependency]] can only be applied to a parameter if it is a
1941 // parameter of a function declaration or lambda.
1942 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1943 S.Diag(Attr.getLoc(),
1944 diag::err_carries_dependency_param_not_function_decl);
1945 return;
1946 }
1947 } else if (!isa<FunctionDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001948 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001949 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001950 return;
1951 }
Richard Smith3a2b7a12013-01-28 22:42:45 +00001952
1953 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1954 Attr.getRange(), S.Context,
1955 Attr.getAttributeSpellingListIndex()));
Sean Huntbbd37c62009-11-21 08:43:09 +00001956}
1957
Chandler Carruth1b03c872011-07-02 00:01:44 +00001958static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001959 // check the attribute arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00001960 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek73798892008-07-25 04:39:19 +00001961 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001962
Chandler Carruth87c44602011-07-01 23:49:12 +00001963 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001964 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001965 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001966 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001967 return;
1968 }
Mike Stumpbf916502009-07-24 19:02:52 +00001969
Michael Han51d8c522013-01-24 16:46:58 +00001970 D->addAttr(::new (S.Context)
1971 UnusedAttr(Attr.getRange(), S.Context,
1972 Attr.getAttributeSpellingListIndex()));
Ted Kremenek73798892008-07-25 04:39:19 +00001973}
1974
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001975static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1976 const AttributeList &Attr) {
1977 // check the attribute arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00001978 if (!checkAttributeNumArgs(S, Attr, 0))
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001979 return;
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001980
1981 if (!isa<FunctionDecl>(D)) {
1982 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1983 << Attr.getName() << ExpectedFunction;
1984 return;
1985 }
1986
Michael Han51d8c522013-01-24 16:46:58 +00001987 D->addAttr(::new (S.Context)
1988 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1989 Attr.getAttributeSpellingListIndex()));
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001990}
1991
Chandler Carruth1b03c872011-07-02 00:01:44 +00001992static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001993 // check the attribute arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00001994 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001995 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001996
Chandler Carruth87c44602011-07-01 23:49:12 +00001997 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola29535ba2013-08-16 23:18:50 +00001998 if (VD->hasLocalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001999 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
2000 return;
2001 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002002 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00002003 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002004 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00002005 return;
2006 }
Mike Stumpbf916502009-07-24 19:02:52 +00002007
Michael Han51d8c522013-01-24 16:46:58 +00002008 D->addAttr(::new (S.Context)
2009 UsedAttr(Attr.getRange(), S.Context,
2010 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00002011}
2012
Chandler Carruth1b03c872011-07-02 00:01:44 +00002013static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002014 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00002015 if (Attr.getNumArgs() > 1) {
2016 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002017 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002018 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002019
2020 int priority = 65535; // FIXME: Do not hardcode such constants.
2021 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002022 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002023 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002024 if (E->isTypeDependent() || E->isValueDependent() ||
2025 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002026 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002027 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002028 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002029 return;
2030 }
2031 priority = Idx.getZExtValue();
2032 }
Mike Stumpbf916502009-07-24 19:02:52 +00002033
Chandler Carruth87c44602011-07-01 23:49:12 +00002034 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002035 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002036 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002037 return;
2038 }
2039
Michael Han51d8c522013-01-24 16:46:58 +00002040 D->addAttr(::new (S.Context)
2041 ConstructorAttr(Attr.getRange(), S.Context, priority,
2042 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002043}
2044
Chandler Carruth1b03c872011-07-02 00:01:44 +00002045static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002046 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00002047 if (Attr.getNumArgs() > 1) {
2048 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002049 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002050 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002051
2052 int priority = 65535; // FIXME: Do not hardcode such constants.
2053 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002054 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002055 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002056 if (E->isTypeDependent() || E->isValueDependent() ||
2057 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002058 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002059 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002060 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002061 return;
2062 }
2063 priority = Idx.getZExtValue();
2064 }
Mike Stumpbf916502009-07-24 19:02:52 +00002065
Chandler Carruth87c44602011-07-01 23:49:12 +00002066 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002067 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002068 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002069 return;
2070 }
2071
Michael Han51d8c522013-01-24 16:46:58 +00002072 D->addAttr(::new (S.Context)
2073 DestructorAttr(Attr.getRange(), S.Context, priority,
2074 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002075}
2076
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002077template <typename AttrTy>
Aaron Ballman2dbdef22013-07-18 13:13:52 +00002078static void handleAttrWithMessage(Sema &S, Decl *D,
2079 const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00002080 unsigned NumArgs = Attr.getNumArgs();
2081 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00002082 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002083 return;
2084 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002085
2086 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002087 StringRef Str;
Aaron Ballman624421f2013-08-31 01:11:41 +00002088 if (Attr.isArgExpr(0)) {
2089 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArgAsExpr(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002090 if (!SE) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002091 S.Diag(Attr.getArgAsExpr(0)->getLocStart(),
2092 diag::err_attribute_argument_type) << Attr.getName()
2093 << AANT_ArgumentString;
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002094 return;
2095 }
Chris Lattner951bbb22011-02-24 05:42:24 +00002096 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002097 }
Mike Stumpbf916502009-07-24 19:02:52 +00002098
Michael Han51d8c522013-01-24 16:46:58 +00002099 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2100 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00002101}
2102
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002103static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2104 const AttributeList &Attr) {
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002105 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002106 return;
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002107
Michael Han51d8c522013-01-24 16:46:58 +00002108 D->addAttr(::new (S.Context)
2109 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2110 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002111}
2112
Patrick Beardb2f68202012-04-06 18:12:22 +00002113static void handleObjCRootClassAttr(Sema &S, Decl *D,
2114 const AttributeList &Attr) {
2115 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002116 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2117 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardb2f68202012-04-06 18:12:22 +00002118 return;
2119 }
2120
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002121 if (!checkAttributeNumArgs(S, Attr, 0))
Patrick Beardb2f68202012-04-06 18:12:22 +00002122 return;
Patrick Beardb2f68202012-04-06 18:12:22 +00002123
Michael Han51d8c522013-01-24 16:46:58 +00002124 D->addAttr(::new (S.Context)
2125 ObjCRootClassAttr(Attr.getRange(), S.Context,
2126 Attr.getAttributeSpellingListIndex()));
Patrick Beardb2f68202012-04-06 18:12:22 +00002127}
2128
Michael Han51d8c522013-01-24 16:46:58 +00002129static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2130 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00002131 if (!isa<ObjCInterfaceDecl>(D)) {
2132 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2133 return;
2134 }
2135
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002136 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002137 return;
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002138
Michael Han51d8c522013-01-24 16:46:58 +00002139 D->addAttr(::new (S.Context)
2140 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2141 Attr.getAttributeSpellingListIndex()));
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002142}
2143
Jordy Rosefad5de92012-05-08 03:27:22 +00002144static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2145 IdentifierInfo *Platform,
2146 VersionTuple Introduced,
2147 VersionTuple Deprecated,
2148 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002149 StringRef PlatformName
2150 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2151 if (PlatformName.empty())
2152 PlatformName = Platform->getName();
2153
2154 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2155 // of these steps are needed).
2156 if (!Introduced.empty() && !Deprecated.empty() &&
2157 !(Introduced <= Deprecated)) {
2158 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2159 << 1 << PlatformName << Deprecated.getAsString()
2160 << 0 << Introduced.getAsString();
2161 return true;
2162 }
2163
2164 if (!Introduced.empty() && !Obsoleted.empty() &&
2165 !(Introduced <= Obsoleted)) {
2166 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2167 << 2 << PlatformName << Obsoleted.getAsString()
2168 << 0 << Introduced.getAsString();
2169 return true;
2170 }
2171
2172 if (!Deprecated.empty() && !Obsoleted.empty() &&
2173 !(Deprecated <= Obsoleted)) {
2174 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2175 << 2 << PlatformName << Obsoleted.getAsString()
2176 << 1 << Deprecated.getAsString();
2177 return true;
2178 }
2179
2180 return false;
2181}
2182
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002183/// \brief Check whether the two versions match.
2184///
2185/// If either version tuple is empty, then they are assumed to match. If
2186/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2187static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2188 bool BeforeIsOkay) {
2189 if (X.empty() || Y.empty())
2190 return true;
2191
2192 if (X == Y)
2193 return true;
2194
2195 if (BeforeIsOkay && X < Y)
2196 return true;
2197
2198 return false;
2199}
2200
Rafael Espindola51be6e32013-01-08 22:04:34 +00002201AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002202 IdentifierInfo *Platform,
2203 VersionTuple Introduced,
2204 VersionTuple Deprecated,
2205 VersionTuple Obsoleted,
2206 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002207 StringRef Message,
Michael Han51d8c522013-01-24 16:46:58 +00002208 bool Override,
2209 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002210 VersionTuple MergedIntroduced = Introduced;
2211 VersionTuple MergedDeprecated = Deprecated;
2212 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002213 bool FoundAny = false;
2214
Rafael Espindola98ae8342012-05-10 02:50:16 +00002215 if (D->hasAttrs()) {
2216 AttrVec &Attrs = D->getAttrs();
2217 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2218 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2219 if (!OldAA) {
2220 ++i;
2221 continue;
2222 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002223
Rafael Espindola98ae8342012-05-10 02:50:16 +00002224 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2225 if (OldPlatform != Platform) {
2226 ++i;
2227 continue;
2228 }
2229
2230 FoundAny = true;
2231 VersionTuple OldIntroduced = OldAA->getIntroduced();
2232 VersionTuple OldDeprecated = OldAA->getDeprecated();
2233 VersionTuple OldObsoleted = OldAA->getObsoleted();
2234 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002235
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002236 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2237 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2238 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2239 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002240 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002241 if (Override) {
2242 int Which = -1;
2243 VersionTuple FirstVersion;
2244 VersionTuple SecondVersion;
2245 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2246 Which = 0;
2247 FirstVersion = OldIntroduced;
2248 SecondVersion = Introduced;
2249 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2250 Which = 1;
2251 FirstVersion = Deprecated;
2252 SecondVersion = OldDeprecated;
2253 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2254 Which = 2;
2255 FirstVersion = Obsoleted;
2256 SecondVersion = OldObsoleted;
2257 }
2258
2259 if (Which == -1) {
2260 Diag(OldAA->getLocation(),
2261 diag::warn_mismatched_availability_override_unavail)
2262 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2263 } else {
2264 Diag(OldAA->getLocation(),
2265 diag::warn_mismatched_availability_override)
2266 << Which
2267 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2268 << FirstVersion.getAsString() << SecondVersion.getAsString();
2269 }
2270 Diag(Range.getBegin(), diag::note_overridden_method);
2271 } else {
2272 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2273 Diag(Range.getBegin(), diag::note_previous_attribute);
2274 }
2275
Rafael Espindola98ae8342012-05-10 02:50:16 +00002276 Attrs.erase(Attrs.begin() + i);
2277 --e;
2278 continue;
2279 }
2280
2281 VersionTuple MergedIntroduced2 = MergedIntroduced;
2282 VersionTuple MergedDeprecated2 = MergedDeprecated;
2283 VersionTuple MergedObsoleted2 = MergedObsoleted;
2284
2285 if (MergedIntroduced2.empty())
2286 MergedIntroduced2 = OldIntroduced;
2287 if (MergedDeprecated2.empty())
2288 MergedDeprecated2 = OldDeprecated;
2289 if (MergedObsoleted2.empty())
2290 MergedObsoleted2 = OldObsoleted;
2291
2292 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2293 MergedIntroduced2, MergedDeprecated2,
2294 MergedObsoleted2)) {
2295 Attrs.erase(Attrs.begin() + i);
2296 --e;
2297 continue;
2298 }
2299
2300 MergedIntroduced = MergedIntroduced2;
2301 MergedDeprecated = MergedDeprecated2;
2302 MergedObsoleted = MergedObsoleted2;
2303 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002304 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002305 }
2306
2307 if (FoundAny &&
2308 MergedIntroduced == Introduced &&
2309 MergedDeprecated == Deprecated &&
2310 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002311 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002312
Ted Kremenekcb344392013-04-06 00:34:27 +00002313 // Only create a new attribute if !Override, but we want to do
2314 // the checking.
Rafael Espindola98ae8342012-05-10 02:50:16 +00002315 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekcb344392013-04-06 00:34:27 +00002316 MergedDeprecated, MergedObsoleted) &&
2317 !Override) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002318 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2319 Introduced, Deprecated,
Michael Han51d8c522013-01-24 16:46:58 +00002320 Obsoleted, IsUnavailable, Message,
2321 AttrSpellingListIndex);
Rafael Espindola3b294362012-05-06 19:56:25 +00002322 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002323 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002324}
2325
Chandler Carruth1b03c872011-07-02 00:01:44 +00002326static void handleAvailabilityAttr(Sema &S, Decl *D,
2327 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002328 if (!checkAttributeNumArgs(S, Attr, 1))
2329 return;
2330 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han51d8c522013-01-24 16:46:58 +00002331 unsigned Index = Attr.getAttributeSpellingListIndex();
2332
Aaron Ballman624421f2013-08-31 01:11:41 +00002333 IdentifierInfo *II = Platform->Ident;
2334 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2335 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2336 << Platform->Ident;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002337
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002338 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2339 if (!ND) {
2340 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2341 return;
2342 }
2343
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002344 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2345 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2346 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002347 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002348 StringRef Str;
2349 const StringLiteral *SE =
2350 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2351 if (SE)
2352 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002353
Aaron Ballman624421f2013-08-31 01:11:41 +00002354 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002355 Introduced.Version,
2356 Deprecated.Version,
2357 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002358 IsUnavailable, Str,
Michael Han51d8c522013-01-24 16:46:58 +00002359 /*Override=*/false,
2360 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002361 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002362 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002363}
2364
John McCalld4c3d662013-02-20 01:54:26 +00002365template <class T>
2366static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2367 typename T::VisibilityType value,
2368 unsigned attrSpellingListIndex) {
2369 T *existingAttr = D->getAttr<T>();
2370 if (existingAttr) {
2371 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2372 if (existingValue == value)
2373 return NULL;
2374 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2375 S.Diag(range.getBegin(), diag::note_previous_attribute);
2376 D->dropAttr<T>();
2377 }
2378 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2379}
2380
Rafael Espindola599f1b72012-05-13 03:25:18 +00002381VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002382 VisibilityAttr::VisibilityType Vis,
2383 unsigned AttrSpellingListIndex) {
John McCalld4c3d662013-02-20 01:54:26 +00002384 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2385 AttrSpellingListIndex);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002386}
2387
John McCalld4c3d662013-02-20 01:54:26 +00002388TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2389 TypeVisibilityAttr::VisibilityType Vis,
2390 unsigned AttrSpellingListIndex) {
2391 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2392 AttrSpellingListIndex);
2393}
2394
2395static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2396 bool isTypeVisibility) {
2397 // Visibility attributes don't mean anything on a typedef.
2398 if (isa<TypedefNameDecl>(D)) {
2399 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2400 << Attr.getName();
2401 return;
2402 }
2403
2404 // 'type_visibility' can only go on a type or namespace.
2405 if (isTypeVisibility &&
2406 !(isa<TagDecl>(D) ||
2407 isa<ObjCInterfaceDecl>(D) ||
2408 isa<NamespaceDecl>(D))) {
2409 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2410 << Attr.getName() << ExpectedTypeOrNamespace;
2411 return;
2412 }
2413
Chris Lattner6b6b5372008-06-26 18:38:35 +00002414 // check the attribute arguments.
John McCalld4c3d662013-02-20 01:54:26 +00002415 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002416 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002417
Aaron Ballman624421f2013-08-31 01:11:41 +00002418 Expr *Arg = Attr.getArgAsExpr(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002419 Arg = Arg->IgnoreParenCasts();
2420 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002421
Douglas Gregor5cee1192011-07-27 05:40:30 +00002422 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002423 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2424 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002425 return;
2426 }
Mike Stumpbf916502009-07-24 19:02:52 +00002427
Chris Lattner5f9e2722011-07-23 10:55:15 +00002428 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002429 VisibilityAttr::VisibilityType type;
Michael Han51d8c522013-01-24 16:46:58 +00002430
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002431 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002432 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002433 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002434 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002435 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002436 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002437 else if (TypeStr == "protected") {
2438 // Complain about attempts to use protected visibility on targets
2439 // (like Darwin) that don't support it.
2440 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2441 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2442 type = VisibilityAttr::Default;
2443 } else {
2444 type = VisibilityAttr::Protected;
2445 }
2446 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002447 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002448 return;
2449 }
Mike Stumpbf916502009-07-24 19:02:52 +00002450
Michael Han51d8c522013-01-24 16:46:58 +00002451 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld4c3d662013-02-20 01:54:26 +00002452 clang::Attr *newAttr;
2453 if (isTypeVisibility) {
2454 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2455 (TypeVisibilityAttr::VisibilityType) type,
2456 Index);
2457 } else {
2458 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2459 }
2460 if (newAttr)
2461 D->addAttr(newAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002462}
2463
Chandler Carruth1b03c872011-07-02 00:01:44 +00002464static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2465 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002466 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2467 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002468 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002469 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002470 return;
2471 }
2472
Aaron Ballman624421f2013-08-31 01:11:41 +00002473 if (!Attr.isArgIdent(0)) {
2474 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2475 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCalld5313b02011-03-02 11:33:24 +00002476 return;
2477 }
Aaron Ballman624421f2013-08-31 01:11:41 +00002478
2479 if (!checkAttributeNumArgs(S, Attr, 1))
2480 return;
John McCalld5313b02011-03-02 11:33:24 +00002481
Aaron Ballman624421f2013-08-31 01:11:41 +00002482 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2483
2484 StringRef param = IL->Ident->getName();
John McCalld5313b02011-03-02 11:33:24 +00002485 ObjCMethodFamilyAttr::FamilyKind family;
2486 if (param == "none")
2487 family = ObjCMethodFamilyAttr::OMF_None;
2488 else if (param == "alloc")
2489 family = ObjCMethodFamilyAttr::OMF_alloc;
2490 else if (param == "copy")
2491 family = ObjCMethodFamilyAttr::OMF_copy;
2492 else if (param == "init")
2493 family = ObjCMethodFamilyAttr::OMF_init;
2494 else if (param == "mutableCopy")
2495 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2496 else if (param == "new")
2497 family = ObjCMethodFamilyAttr::OMF_new;
2498 else {
2499 // Just warn and ignore it. This is future-proof against new
2500 // families being used in system headers.
Aaron Ballman624421f2013-08-31 01:11:41 +00002501 S.Diag(IL->Loc, diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002502 return;
2503 }
2504
John McCallf85e1932011-06-15 23:02:42 +00002505 if (family == ObjCMethodFamilyAttr::OMF_init &&
2506 !method->getResultType()->isObjCObjectPointerType()) {
2507 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2508 << method->getResultType();
2509 // Ignore the attribute.
2510 return;
2511 }
2512
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002513 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002514 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002515}
2516
Chandler Carruth1b03c872011-07-02 00:01:44 +00002517static void handleObjCExceptionAttr(Sema &S, Decl *D,
2518 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002519 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002520 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002521
Chris Lattner0db29ec2009-02-14 08:09:34 +00002522 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2523 if (OCI == 0) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002524 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2525 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002526 return;
2527 }
Mike Stumpbf916502009-07-24 19:02:52 +00002528
Michael Han51d8c522013-01-24 16:46:58 +00002529 D->addAttr(::new (S.Context)
2530 ObjCExceptionAttr(Attr.getRange(), S.Context,
2531 Attr.getAttributeSpellingListIndex()));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002532}
2533
Chandler Carruth1b03c872011-07-02 00:01:44 +00002534static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman081c8832013-07-23 12:13:14 +00002535 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002536 return;
Richard Smith162e1c12011-04-15 14:24:37 +00002537 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002538 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002539 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002540 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2541 return;
2542 }
2543 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002544 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2545 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002546 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002547 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2548 return;
2549 }
2550 }
2551 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002552 // It is okay to include this attribute on properties, e.g.:
2553 //
2554 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2555 //
2556 // In this case it follows tradition and suppresses an error in the above
2557 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002558 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002559 }
Michael Han51d8c522013-01-24 16:46:58 +00002560 D->addAttr(::new (S.Context)
2561 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2562 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002563}
2564
Mike Stumpbf916502009-07-24 19:02:52 +00002565static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002566handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman081c8832013-07-23 12:13:14 +00002567 if (!checkAttributeNumArgs(S, Attr, 0))
Douglas Gregorf9201e02009-02-11 23:02:49 +00002568 return;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002569
2570 if (!isa<FunctionDecl>(D)) {
2571 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2572 return;
2573 }
2574
Michael Han51d8c522013-01-24 16:46:58 +00002575 D->addAttr(::new (S.Context)
2576 OverloadableAttr(Attr.getRange(), S.Context,
2577 Attr.getAttributeSpellingListIndex()));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002578}
2579
Chandler Carruth1b03c872011-07-02 00:01:44 +00002580static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002581 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002582 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00002583 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff9eae5762008-09-18 16:44:58 +00002584 return;
2585 }
Aaron Ballman624421f2013-08-31 01:11:41 +00002586
2587 if (!checkAttributeNumArgs(S, Attr, 1))
Steve Naroff9eae5762008-09-18 16:44:58 +00002588 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002589
Aaron Ballman624421f2013-08-31 01:11:41 +00002590 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Sean Huntcf807c42010-08-18 23:23:40 +00002591 BlocksAttr::BlockType type;
Aaron Ballman624421f2013-08-31 01:11:41 +00002592 if (II->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002593 type = BlocksAttr::ByRef;
2594 else {
Aaron Ballman624421f2013-08-31 01:11:41 +00002595 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) << "blocks"
2596 << II;
Steve Naroff9eae5762008-09-18 16:44:58 +00002597 return;
2598 }
Mike Stumpbf916502009-07-24 19:02:52 +00002599
Michael Han51d8c522013-01-24 16:46:58 +00002600 D->addAttr(::new (S.Context)
2601 BlocksAttr(Attr.getRange(), S.Context, type,
2602 Attr.getAttributeSpellingListIndex()));
Steve Naroff9eae5762008-09-18 16:44:58 +00002603}
2604
Chandler Carruth1b03c872011-07-02 00:01:44 +00002605static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002606 // check the attribute arguments.
2607 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002608 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002609 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002610 }
2611
John McCall3323fad2011-09-09 07:56:05 +00002612 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002613 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002614 Expr *E = Attr.getArgAsExpr(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002615 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002616 if (E->isTypeDependent() || E->isValueDependent() ||
2617 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002618 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002619 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002620 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002621 return;
2622 }
Mike Stumpbf916502009-07-24 19:02:52 +00002623
John McCall3323fad2011-09-09 07:56:05 +00002624 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002625 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2626 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002627 return;
2628 }
John McCall3323fad2011-09-09 07:56:05 +00002629
2630 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002631 }
2632
John McCall3323fad2011-09-09 07:56:05 +00002633 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002634 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002635 Expr *E = Attr.getArgAsExpr(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002636 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002637 if (E->isTypeDependent() || E->isValueDependent() ||
2638 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002639 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002640 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002641 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002642 return;
2643 }
2644 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002645
John McCall3323fad2011-09-09 07:56:05 +00002646 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002647 // FIXME: This error message could be improved, it would be nice
2648 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002649 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2650 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002651 return;
2652 }
2653 }
2654
Chandler Carruth87c44602011-07-01 23:49:12 +00002655 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002656 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002657 if (isa<FunctionNoProtoType>(FT)) {
2658 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2659 return;
2660 }
Mike Stumpbf916502009-07-24 19:02:52 +00002661
Chris Lattner897cd902009-03-17 23:03:47 +00002662 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002663 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002664 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002665 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002666 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002667 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002668 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002669 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002670 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002671 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2672 if (!BD->isVariadic()) {
2673 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2674 return;
2675 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002676 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002677 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002678 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002679 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002680 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002681 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002682 int m = Ty->isFunctionPointerType() ? 0 : 1;
2683 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002684 return;
2685 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002686 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002687 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002688 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002689 return;
2690 }
Anders Carlsson77091822008-10-05 18:05:59 +00002691 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002692 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002693 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002694 return;
2695 }
Michael Han51d8c522013-01-24 16:46:58 +00002696 D->addAttr(::new (S.Context)
2697 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2698 Attr.getAttributeSpellingListIndex()));
Anders Carlsson77091822008-10-05 18:05:59 +00002699}
2700
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002701static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2702 // Check the attribute arguments.
2703 if (!checkAttributeNumArgs(S, Attr, 0))
2704 return;
2705
2706 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2707 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2708 else
2709 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2710}
2711
Chandler Carruth1b03c872011-07-02 00:01:44 +00002712static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002713 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002714 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002715 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002716
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002717 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002718 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002719 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002720 return;
2721 }
Mike Stumpbf916502009-07-24 19:02:52 +00002722
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002723 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2724 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2725 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002726 return;
2727 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002728 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2729 if (MD->getResultType()->isVoidType()) {
2730 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2731 << Attr.getName() << 1;
2732 return;
2733 }
2734
Michael Han51d8c522013-01-24 16:46:58 +00002735 D->addAttr(::new (S.Context)
2736 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2737 Attr.getAttributeSpellingListIndex()));
Chris Lattner026dc962009-02-14 07:37:35 +00002738}
2739
Chandler Carruth1b03c872011-07-02 00:01:44 +00002740static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002741 // check the attribute arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00002742 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002743 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002744
Chandler Carruth87c44602011-07-01 23:49:12 +00002745 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002746 if (isa<CXXRecordDecl>(D)) {
2747 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2748 return;
2749 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002750 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2751 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002752 return;
2753 }
2754
Chandler Carruth87c44602011-07-01 23:49:12 +00002755 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002756
Michael Han51d8c522013-01-24 16:46:58 +00002757 nd->addAttr(::new (S.Context)
2758 WeakAttr(Attr.getRange(), S.Context,
2759 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002760}
2761
Chandler Carruth1b03c872011-07-02 00:01:44 +00002762static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002763 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002764 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002765 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002766
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002767
2768 // weak_import only applies to variable & function declarations.
2769 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002770 if (!D->canBeWeakImported(isDef)) {
2771 if (isDef)
Reid Klecknerbeba3e82013-05-20 21:53:29 +00002772 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2773 << "weak_import";
Douglas Gregordef86312011-03-23 13:27:51 +00002774 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002775 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002776 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002777 // Nothing to warn about here.
2778 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002779 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002780 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002781
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002782 return;
2783 }
2784
Michael Han51d8c522013-01-24 16:46:58 +00002785 D->addAttr(::new (S.Context)
2786 WeakImportAttr(Attr.getRange(), S.Context,
2787 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002788}
2789
Tanya Lattner0df579e2012-07-09 22:06:01 +00002790// Handles reqd_work_group_size and work_group_size_hint.
2791static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002792 const AttributeList &Attr) {
Tanya Lattner0df579e2012-07-09 22:06:01 +00002793 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2794 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2795
Nate Begeman6f3d8382009-06-26 06:32:41 +00002796 // Attribute has 3 arguments.
Tanya Lattner0df579e2012-07-09 22:06:01 +00002797 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002798
2799 unsigned WGSize[3];
2800 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002801 Expr *E = Attr.getArgAsExpr(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002802 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002803 if (E->isTypeDependent() || E->isValueDependent() ||
2804 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00002805 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2806 << Attr.getName() << AANT_ArgumentIntegerConstant
2807 << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002808 return;
2809 }
2810 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2811 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002812
2813 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2814 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2815 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2816 if (!(A->getXDim() == WGSize[0] &&
2817 A->getYDim() == WGSize[1] &&
2818 A->getZDim() == WGSize[2])) {
2819 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2820 Attr.getName();
2821 }
2822 }
2823
2824 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2825 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2826 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2827 if (!(A->getXDim() == WGSize[0] &&
2828 A->getYDim() == WGSize[1] &&
2829 A->getZDim() == WGSize[2])) {
2830 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2831 Attr.getName();
2832 }
2833 }
2834
2835 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2836 D->addAttr(::new (S.Context)
2837 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002838 WGSize[0], WGSize[1], WGSize[2],
2839 Attr.getAttributeSpellingListIndex()));
Tanya Lattner0df579e2012-07-09 22:06:01 +00002840 else
2841 D->addAttr(::new (S.Context)
2842 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002843 WGSize[0], WGSize[1], WGSize[2],
2844 Attr.getAttributeSpellingListIndex()));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002845}
2846
Joey Gouly37453b92013-03-08 09:42:32 +00002847static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2848 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2849
Aaron Ballman624421f2013-08-31 01:11:41 +00002850 if (!checkAttributeNumArgs(S, Attr, 0))
Joey Gouly37453b92013-03-08 09:42:32 +00002851 return;
2852
Aaron Ballman624421f2013-08-31 01:11:41 +00002853 if (!Attr.hasParsedType()) {
2854 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2855 << Attr.getName() << 1;
2856 return;
2857 }
2858
Joey Gouly37453b92013-03-08 09:42:32 +00002859 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg());
2860
2861 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2862 (ParmType->isBooleanType() ||
2863 !ParmType->isIntegralType(S.getASTContext()))) {
2864 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2865 << ParmType;
2866 return;
2867 }
2868
2869 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2870 D->hasAttr<VecTypeHintAttr>()) {
2871 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
2872 if (A->getTypeHint() != ParmType) {
2873 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2874 return;
2875 }
2876 }
2877
2878 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
2879 ParmType, Attr.getLoc()));
2880}
2881
Joey Gouly96cead52013-03-14 09:54:43 +00002882static void handleEndianAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002883 if (!Attr.isArgIdent(0)) {
2884 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2885 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
2886 return;
2887 }
2888
2889 if (!checkAttributeNumArgs(S, Attr, 1))
2890 return;
2891
Joey Gouly96cead52013-03-14 09:54:43 +00002892 if (!dyn_cast<VarDecl>(D))
Aaron Ballman624421f2013-08-31 01:11:41 +00002893 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2894 << Attr.getName() << ExpectedVariable;
2895 StringRef EndianType = Attr.getArgAsIdent(0)->Ident->getName();
Joey Gouly96cead52013-03-14 09:54:43 +00002896 if (EndianType != "host" && EndianType != "device")
2897 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_endian) << EndianType;
2898}
2899
Rafael Espindola599f1b72012-05-13 03:25:18 +00002900SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002901 StringRef Name,
2902 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002903 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2904 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002905 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002906 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2907 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002908 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002909 }
Michael Han51d8c522013-01-24 16:46:58 +00002910 return ::new (Context) SectionAttr(Range, Context, Name,
2911 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002912}
2913
Chandler Carruth1b03c872011-07-02 00:01:44 +00002914static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002915 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002916 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002917 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002918
2919 // Make sure that there is a string literal as the sections's single
2920 // argument.
Aaron Ballman624421f2013-08-31 01:11:41 +00002921 Expr *ArgExpr = Attr.getArgAsExpr(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002922 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002923 if (!SE) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002924 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
2925 << Attr.getName() << AANT_ArgumentString;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002926 return;
2927 }
Mike Stump1eb44332009-09-09 15:08:12 +00002928
Chris Lattner797c3c42009-08-10 19:03:04 +00002929 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002930 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002931 if (!Error.empty()) {
2932 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2933 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002934 return;
2935 }
Mike Stump1eb44332009-09-09 15:08:12 +00002936
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002937 // This attribute cannot be applied to local variables.
2938 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2939 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2940 return;
2941 }
Michael Han51d8c522013-01-24 16:46:58 +00002942
2943 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindola599f1b72012-05-13 03:25:18 +00002944 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han51d8c522013-01-24 16:46:58 +00002945 SE->getString(), Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002946 if (NewAttr)
2947 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002948}
2949
Chris Lattner6b6b5372008-06-26 18:38:35 +00002950
Chandler Carruth1b03c872011-07-02 00:01:44 +00002951static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002952 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002953 return;
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002954
Chandler Carruth87c44602011-07-01 23:49:12 +00002955 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002956 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002957 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002958 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002959 D->addAttr(::new (S.Context)
2960 NoThrowAttr(Attr.getRange(), S.Context,
2961 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002962 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002963}
2964
Chandler Carruth1b03c872011-07-02 00:01:44 +00002965static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002966 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002967 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002968
Chandler Carruth87c44602011-07-01 23:49:12 +00002969 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002970 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002971 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002972 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002973 D->addAttr(::new (S.Context)
2974 ConstAttr(Attr.getRange(), S.Context,
2975 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002976 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002977}
2978
Chandler Carruth1b03c872011-07-02 00:01:44 +00002979static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002980 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002981 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002982 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002983
Michael Han51d8c522013-01-24 16:46:58 +00002984 D->addAttr(::new (S.Context)
2985 PureAttr(Attr.getRange(), S.Context,
2986 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002987}
2988
Chandler Carruth1b03c872011-07-02 00:01:44 +00002989static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002990 if (!Attr.isArgIdent(0)) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002991 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2992 << Attr.getName() << 1;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002993 return;
2994 }
Aaron Ballman624421f2013-08-31 01:11:41 +00002995
2996 if (!checkAttributeNumArgs(S, Attr, 1))
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002997 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002998
Chandler Carruth87c44602011-07-01 23:49:12 +00002999 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00003000
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003001 if (!VD || !VD->hasLocalStorage()) {
3002 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
3003 return;
3004 }
Mike Stumpbf916502009-07-24 19:02:52 +00003005
Aaron Ballman624421f2013-08-31 01:11:41 +00003006 IdentifierLoc *IL = Attr.getArgAsIdent(0);
3007
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003008 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00003009 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00003010 NamedDecl *CleanupDecl
Aaron Ballman624421f2013-08-31 01:11:41 +00003011 = S.LookupSingleName(S.TUScope, IL->Ident, IL->Loc,
3012 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003013 if (!CleanupDecl) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003014 S.Diag(IL->Loc, diag::err_attribute_cleanup_arg_not_found) << IL->Ident;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003015 return;
3016 }
Mike Stumpbf916502009-07-24 19:02:52 +00003017
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003018 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
3019 if (!FD) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003020 S.Diag(IL->Loc, diag::err_attribute_cleanup_arg_not_function) << IL->Ident;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003021 return;
3022 }
3023
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003024 if (FD->getNumParams() != 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003025 S.Diag(IL->Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
3026 << IL->Ident;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003027 return;
3028 }
Mike Stumpbf916502009-07-24 19:02:52 +00003029
Anders Carlsson89941c12009-02-07 23:16:50 +00003030 // We're currently more strict than GCC about what function types we accept.
3031 // If this ever proves to be a problem it should be easy to fix.
3032 QualType Ty = S.Context.getPointerType(VD->getType());
3033 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00003034 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
3035 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003036 S.Diag(IL->Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) <<
3037 IL->Ident << ParamTy << Ty;
Anders Carlsson89941c12009-02-07 23:16:50 +00003038 return;
3039 }
Mike Stumpbf916502009-07-24 19:02:52 +00003040
Michael Han51d8c522013-01-24 16:46:58 +00003041 D->addAttr(::new (S.Context)
3042 CleanupAttr(Attr.getRange(), S.Context, FD,
3043 Attr.getAttributeSpellingListIndex()));
Aaron Ballman624421f2013-08-31 01:11:41 +00003044 S.MarkFunctionReferenced(IL->Loc, FD);
3045 S.DiagnoseUseOfDecl(FD, IL->Loc);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003046}
3047
Mike Stumpbf916502009-07-24 19:02:52 +00003048/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003049/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003050static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00003051 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003052 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003053
Chandler Carruth87c44602011-07-01 23:49:12 +00003054 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003055 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003056 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003057 return;
3058 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003059
Aaron Ballman624421f2013-08-31 01:11:41 +00003060 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00003061 uint64_t ArgIdx;
3062 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
3063 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003064 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003065
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003066 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003067 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00003068
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003069 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
3070 if (not_nsstring_type &&
3071 !isCFStringType(Ty, S.Context) &&
3072 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003073 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003074 // FIXME: Should highlight the actual expression that has the wrong type.
3075 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00003076 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003077 << IdxExpr->getSourceRange();
3078 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003079 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003080 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003081 if (!isNSStringType(Ty, S.Context) &&
3082 !isCFStringType(Ty, S.Context) &&
3083 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003084 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003085 // FIXME: Should highlight the actual expression that has the wrong type.
3086 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00003087 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003088 << IdxExpr->getSourceRange();
3089 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003090 }
3091
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00003092 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
3093 // because that has corrected for the implicit this parameter, and is zero-
3094 // based. The attribute expects what the user wrote explicitly.
3095 llvm::APSInt Val;
3096 IdxExpr->EvaluateAsInt(Val, S.Context);
3097
Michael Han51d8c522013-01-24 16:46:58 +00003098 D->addAttr(::new (S.Context)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00003099 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003100 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003101}
3102
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003103enum FormatAttrKind {
3104 CFStringFormat,
3105 NSStringFormat,
3106 StrftimeFormat,
3107 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00003108 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003109 InvalidFormat
3110};
3111
3112/// getFormatAttrKind - Map from format attribute names to supported format
3113/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003114static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003115 return llvm::StringSwitch<FormatAttrKind>(Format)
3116 // Check for formats that get handled specially.
3117 .Case("NSString", NSStringFormat)
3118 .Case("CFString", CFStringFormat)
3119 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003120
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003121 // Otherwise, check for supported formats.
3122 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3123 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3124 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003125
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003126 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3127 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003128}
3129
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003130/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003131/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003132static void handleInitPriorityAttr(Sema &S, Decl *D,
3133 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003134 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003135 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3136 return;
3137 }
3138
Chandler Carruth87c44602011-07-01 23:49:12 +00003139 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003140 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3141 Attr.setInvalid();
3142 return;
3143 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003144 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003145 if (S.Context.getAsArrayType(T))
3146 T = S.Context.getBaseElementType(T);
3147 if (!T->getAs<RecordType>()) {
3148 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3149 Attr.setInvalid();
3150 return;
3151 }
3152
Aaron Ballmanffa9d572013-07-18 18:01:48 +00003153 if (!checkAttributeNumArgs(S, Attr, 1)) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003154 Attr.setInvalid();
3155 return;
3156 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003157 Expr *priorityExpr = Attr.getArgAsExpr(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003158
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003159 llvm::APSInt priority(32);
3160 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3161 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003162 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3163 << Attr.getName() << AANT_ArgumentIntegerConstant
3164 << priorityExpr->getSourceRange();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003165 Attr.setInvalid();
3166 return;
3167 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00003168 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003169 if (prioritynum < 101 || prioritynum > 65535) {
3170 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3171 << priorityExpr->getSourceRange();
3172 Attr.setInvalid();
3173 return;
3174 }
Michael Han51d8c522013-01-24 16:46:58 +00003175 D->addAttr(::new (S.Context)
3176 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3177 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003178}
3179
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003180FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3181 IdentifierInfo *Format, int FormatIdx,
3182 int FirstArg,
Michael Han51d8c522013-01-24 16:46:58 +00003183 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003184 // Check whether we already have an equivalent format attribute.
3185 for (specific_attr_iterator<FormatAttr>
3186 i = D->specific_attr_begin<FormatAttr>(),
3187 e = D->specific_attr_end<FormatAttr>();
3188 i != e ; ++i) {
3189 FormatAttr *f = *i;
3190 if (f->getType() == Format &&
3191 f->getFormatIdx() == FormatIdx &&
3192 f->getFirstArg() == FirstArg) {
3193 // If we don't have a valid location for this attribute, adopt the
3194 // location.
3195 if (f->getLocation().isInvalid())
3196 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00003197 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003198 }
3199 }
3200
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003201 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3202 FirstArg, AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003203}
3204
Mike Stumpbf916502009-07-24 19:02:52 +00003205/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003206/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003207static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003208 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003209 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00003210 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003211 return;
3212 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003213
3214 if (!checkAttributeNumArgs(S, Attr, 3))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003215 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003216
Chandler Carruth87c44602011-07-01 23:49:12 +00003217 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003218 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003219 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003220 return;
3221 }
3222
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003223 // In C++ the implicit 'this' function parameter also counts, and they are
3224 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003225 bool HasImplicitThisParam = isInstanceMethod(D);
3226 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003227 unsigned FirstIdx = 1;
3228
Aaron Ballman624421f2013-08-31 01:11:41 +00003229 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
3230 StringRef Format = II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003231
3232 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003233 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003234 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003235 // If we've modified the string name, we need a new identifier for it.
3236 II = &S.Context.Idents.get(Format);
3237 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003238
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003239 // Check for supported formats.
3240 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003241
3242 if (Kind == IgnoredFormat)
3243 return;
3244
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003245 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003246 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman624421f2013-08-31 01:11:41 +00003247 << "format" << II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003248 return;
3249 }
3250
3251 // checks for the 2nd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003252 Expr *IdxExpr = Attr.getArgAsExpr(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003253 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003254 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3255 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003256 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003257 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003258 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003259 return;
3260 }
3261
3262 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003263 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003264 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003265 return;
3266 }
3267
3268 // FIXME: Do we need to bounds check?
3269 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003270
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003271 if (HasImplicitThisParam) {
3272 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003273 S.Diag(Attr.getLoc(),
3274 diag::err_format_attribute_implicit_this_format_string)
3275 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003276 return;
3277 }
3278 ArgIdx--;
3279 }
Mike Stump1eb44332009-09-09 15:08:12 +00003280
Chris Lattner6b6b5372008-06-26 18:38:35 +00003281 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003282 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003283
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003284 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003285 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003286 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3287 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003288 return;
3289 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003290 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003291 // FIXME: do we need to check if the type is NSString*? What are the
3292 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003293 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003294 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003295 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3296 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003297 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003298 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003299 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003300 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003301 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003302 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3303 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003304 return;
3305 }
3306
3307 // check the 3rd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003308 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Chris Lattner803d0802008-06-29 00:43:07 +00003309 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003310 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3311 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003312 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003313 << Attr.getName() << 3 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003314 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003315 return;
3316 }
3317
3318 // check if the function is variadic if the 3rd argument non-zero
3319 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003320 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003321 ++NumArgs; // +1 for ...
3322 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003323 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003324 return;
3325 }
3326 }
3327
Chris Lattner3c73c412008-11-19 08:23:25 +00003328 // strftime requires FirstArg to be 0 because it doesn't read from any
3329 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003330 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003331 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003332 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3333 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003334 return;
3335 }
3336 // if 0 it disables parameter checking (to use with e.g. va_list)
3337 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003338 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003339 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003340 return;
3341 }
3342
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003343 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Rafael Espindola599f1b72012-05-13 03:25:18 +00003344 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003345 FirstArg.getZExtValue(),
3346 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003347 if (NewAttr)
3348 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003349}
3350
Chandler Carruth1b03c872011-07-02 00:01:44 +00003351static void handleTransparentUnionAttr(Sema &S, Decl *D,
3352 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003353 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003354 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003355 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003356
Chris Lattner6b6b5372008-06-26 18:38:35 +00003357
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003358 // Try to find the underlying union declaration.
3359 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003360 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003361 if (TD && TD->getUnderlyingType()->isUnionType())
3362 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3363 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003364 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003365
3366 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003367 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003368 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003369 return;
3370 }
3371
John McCall5e1cdac2011-10-07 06:10:15 +00003372 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003373 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003374 diag::warn_transparent_union_attribute_not_definition);
3375 return;
3376 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003377
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003378 RecordDecl::field_iterator Field = RD->field_begin(),
3379 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003380 if (Field == FieldEnd) {
3381 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3382 return;
3383 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003384
David Blaikie581deb32012-06-06 20:45:41 +00003385 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003386 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003387 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003388 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003389 diag::warn_transparent_union_attribute_floating)
3390 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003391 return;
3392 }
3393
3394 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3395 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3396 for (; Field != FieldEnd; ++Field) {
3397 QualType FieldType = Field->getType();
3398 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3399 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3400 // Warn if we drop the attribute.
3401 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003402 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003403 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003404 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003405 diag::warn_transparent_union_attribute_field_size_align)
3406 << isSize << Field->getDeclName() << FieldBits;
3407 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003408 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003409 diag::note_transparent_union_first_field_size_align)
3410 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003411 return;
3412 }
3413 }
3414
Michael Han51d8c522013-01-24 16:46:58 +00003415 RD->addAttr(::new (S.Context)
3416 TransparentUnionAttr(Attr.getRange(), S.Context,
3417 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003418}
3419
Chandler Carruth1b03c872011-07-02 00:01:44 +00003420static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003421 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003422 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003423 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003424
Aaron Ballman624421f2013-08-31 01:11:41 +00003425 Expr *ArgExpr = Attr.getArgAsExpr(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003426 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003427
Chris Lattner6b6b5372008-06-26 18:38:35 +00003428 // Make sure that there is a string literal as the annotation's single
3429 // argument.
3430 if (!SE) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003431 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
3432 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003433 return;
3434 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003435
3436 // Don't duplicate annotations that are already set.
3437 for (specific_attr_iterator<AnnotateAttr>
3438 i = D->specific_attr_begin<AnnotateAttr>(),
3439 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3440 if ((*i)->getAnnotation() == SE->getString())
3441 return;
3442 }
Michael Han51d8c522013-01-24 16:46:58 +00003443
3444 D->addAttr(::new (S.Context)
3445 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3446 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003447}
3448
Chandler Carruth1b03c872011-07-02 00:01:44 +00003449static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003450 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003451 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003452 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3453 << Attr.getName() << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003454 return;
3455 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003456
Richard Smithbe507b62013-02-01 08:12:08 +00003457 if (Attr.getNumArgs() == 0) {
3458 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3459 true, 0, Attr.getAttributeSpellingListIndex()));
3460 return;
3461 }
3462
Aaron Ballman624421f2013-08-31 01:11:41 +00003463 Expr *E = Attr.getArgAsExpr(0);
Richard Smithf6565a92013-02-22 08:32:16 +00003464 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3465 S.Diag(Attr.getEllipsisLoc(),
3466 diag::err_pack_expansion_without_parameter_packs);
3467 return;
3468 }
3469
3470 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3471 return;
3472
3473 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3474 Attr.isPackExpansion());
Richard Smithbe507b62013-02-01 08:12:08 +00003475}
3476
3477void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smithf6565a92013-02-22 08:32:16 +00003478 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smithbe507b62013-02-01 08:12:08 +00003479 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3480 SourceLocation AttrLoc = AttrRange.getBegin();
3481
Richard Smith4cd81c52013-01-29 09:02:09 +00003482 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smithbe507b62013-02-01 08:12:08 +00003483 if (TmpAttr.isAlignas()) {
Richard Smith4cd81c52013-01-29 09:02:09 +00003484 // C++11 [dcl.align]p1:
3485 // An alignment-specifier may be applied to a variable or to a class
3486 // data member, but it shall not be applied to a bit-field, a function
3487 // parameter, the formal parameter of a catch clause, or a variable
3488 // declared with the register storage class specifier. An
3489 // alignment-specifier may also be applied to the declaration of a class
3490 // or enumeration type.
3491 // C11 6.7.5/2:
3492 // An alignment attribute shall not be specified in a declaration of
3493 // a typedef, or a bit-field, or a function, or a parameter, or an
3494 // object declared with the register storage-class specifier.
3495 int DiagKind = -1;
3496 if (isa<ParmVarDecl>(D)) {
3497 DiagKind = 0;
3498 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3499 if (VD->getStorageClass() == SC_Register)
3500 DiagKind = 1;
3501 if (VD->isExceptionVariable())
3502 DiagKind = 2;
3503 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3504 if (FD->isBitField())
3505 DiagKind = 3;
3506 } else if (!isa<TagDecl>(D)) {
Richard Smithbe507b62013-02-01 08:12:08 +00003507 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3508 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith5f838aa2013-02-01 08:25:07 +00003509 << (TmpAttr.isC11() ? ExpectedVariableOrField
3510 : ExpectedVariableFieldOrTag);
Richard Smith4cd81c52013-01-29 09:02:09 +00003511 return;
3512 }
3513 if (DiagKind != -1) {
Richard Smithbe507b62013-02-01 08:12:08 +00003514 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smith671b3212013-02-22 04:55:39 +00003515 << TmpAttr.isC11() << DiagKind;
Richard Smith4cd81c52013-01-29 09:02:09 +00003516 return;
3517 }
3518 }
3519
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003520 if (E->isTypeDependent() || E->isValueDependent()) {
3521 // Save dependent expressions in the AST to be instantiated.
Richard Smithf6565a92013-02-22 08:32:16 +00003522 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3523 AA->setPackExpansion(IsPackExpansion);
3524 D->addAttr(AA);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003525 return;
3526 }
Michael Hana31f65b2013-02-01 01:19:17 +00003527
Sean Huntcf807c42010-08-18 23:23:40 +00003528 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003529 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003530 ExprResult ICE
3531 = VerifyIntegerConstantExpression(E, &Alignment,
3532 diag::err_aligned_attribute_argument_not_int,
3533 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003534 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003535 return;
Richard Smithbe507b62013-02-01 08:12:08 +00003536
3537 // C++11 [dcl.align]p2:
3538 // -- if the constant expression evaluates to zero, the alignment
3539 // specifier shall have no effect
3540 // C11 6.7.5p6:
3541 // An alignment specification of zero has no effect.
3542 if (!(TmpAttr.isAlignas() && !Alignment) &&
3543 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003544 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3545 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003546 return;
3547 }
Michael Hana31f65b2013-02-01 01:19:17 +00003548
Richard Smithbe507b62013-02-01 08:12:08 +00003549 if (TmpAttr.isDeclspec()) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003550 // We've already verified it's a power of 2, now let's make sure it's
3551 // 8192 or less.
3552 if (Alignment.getZExtValue() > 8192) {
Michael Hana31f65b2013-02-01 01:19:17 +00003553 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003554 << E->getSourceRange();
3555 return;
3556 }
3557 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003558
Richard Smithf6565a92013-02-22 08:32:16 +00003559 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3560 ICE.take(), SpellingListIndex);
3561 AA->setPackExpansion(IsPackExpansion);
3562 D->addAttr(AA);
Sean Huntcf807c42010-08-18 23:23:40 +00003563}
3564
Michael Hana31f65b2013-02-01 01:19:17 +00003565void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smithf6565a92013-02-22 08:32:16 +00003566 unsigned SpellingListIndex, bool IsPackExpansion) {
Sean Huntcf807c42010-08-18 23:23:40 +00003567 // FIXME: Cache the number on the Attr object if non-dependent?
3568 // FIXME: Perform checking of type validity
Richard Smithf6565a92013-02-22 08:32:16 +00003569 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3570 SpellingListIndex);
3571 AA->setPackExpansion(IsPackExpansion);
3572 D->addAttr(AA);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003573}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003574
Richard Smithbe507b62013-02-01 08:12:08 +00003575void Sema::CheckAlignasUnderalignment(Decl *D) {
3576 assert(D->hasAttrs() && "no attributes on decl");
3577
3578 QualType Ty;
3579 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3580 Ty = VD->getType();
3581 else
3582 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith4da09032013-02-22 09:21:42 +00003583 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smithbe507b62013-02-01 08:12:08 +00003584 return;
3585
3586 // C++11 [dcl.align]p5, C11 6.7.5/4:
3587 // The combined effect of all alignment attributes in a declaration shall
3588 // not specify an alignment that is less strict than the alignment that
3589 // would otherwise be required for the entity being declared.
3590 AlignedAttr *AlignasAttr = 0;
3591 unsigned Align = 0;
3592 for (specific_attr_iterator<AlignedAttr>
3593 I = D->specific_attr_begin<AlignedAttr>(),
3594 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3595 if (I->isAlignmentDependent())
3596 return;
3597 if (I->isAlignas())
3598 AlignasAttr = *I;
3599 Align = std::max(Align, I->getAlignment(Context));
3600 }
3601
3602 if (AlignasAttr && Align) {
3603 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3604 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3605 if (NaturalAlign > RequestedAlign)
3606 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3607 << Ty << (unsigned)NaturalAlign.getQuantity();
3608 }
3609}
3610
Chandler Carruthd309c812011-07-01 23:49:16 +00003611/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003612/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003613///
Mike Stumpbf916502009-07-24 19:02:52 +00003614/// Despite what would be logical, the mode attribute is a decl attribute, not a
3615/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3616/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003617static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003618 // This attribute isn't documented, but glibc uses it. It changes
3619 // the width of an int or unsigned int to the specified size.
Aaron Ballman624421f2013-08-31 01:11:41 +00003620 if (!Attr.isArgIdent(0)) {
Aaron Ballman750f73a2013-07-30 14:29:12 +00003621 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3622 << AANT_ArgumentIdentifier;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003623 return;
3624 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003625
3626 if (!checkAttributeNumArgs(S, Attr, 1))
3627 return;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003628
Aaron Ballman624421f2013-08-31 01:11:41 +00003629 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3630 StringRef Str = Name->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003631
3632 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003633 if (Str.startswith("__") && Str.endswith("__"))
3634 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003635
3636 unsigned DestWidth = 0;
3637 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003638 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003639 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003640 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003641 switch (Str[0]) {
3642 case 'Q': DestWidth = 8; break;
3643 case 'H': DestWidth = 16; break;
3644 case 'S': DestWidth = 32; break;
3645 case 'D': DestWidth = 64; break;
3646 case 'X': DestWidth = 96; break;
3647 case 'T': DestWidth = 128; break;
3648 }
3649 if (Str[1] == 'F') {
3650 IntegerMode = false;
3651 } else if (Str[1] == 'C') {
3652 IntegerMode = false;
3653 ComplexMode = true;
3654 } else if (Str[1] != 'I') {
3655 DestWidth = 0;
3656 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003657 break;
3658 case 4:
3659 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3660 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003661 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003662 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003663 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003664 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003665 break;
3666 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003667 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003668 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003669 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003670 case 11:
3671 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003672 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003673 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003674 }
3675
3676 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003677 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003678 OldTy = TD->getUnderlyingType();
3679 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3680 OldTy = VD->getType();
3681 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003682 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003683 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003684 return;
3685 }
Eli Friedman73397492009-03-03 06:41:03 +00003686
John McCall183700f2009-09-21 23:43:11 +00003687 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003688 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3689 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003690 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003691 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3692 } else if (ComplexMode) {
3693 if (!OldTy->isComplexType())
3694 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3695 } else {
3696 if (!OldTy->isFloatingType())
3697 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3698 }
3699
Mike Stump390b4cc2009-05-16 07:39:55 +00003700 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3701 // and friends, at least with glibc.
3702 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3703 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003704 // FIXME: Make sure floating-point mappings are accurate
3705 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003706 QualType NewTy;
3707 switch (DestWidth) {
3708 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003709 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003710 return;
3711 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003712 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003713 return;
3714 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003715 if (!IntegerMode) {
3716 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3717 return;
3718 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003719 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003720 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003721 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003722 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003723 break;
3724 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003725 if (!IntegerMode) {
3726 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3727 return;
3728 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003729 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003730 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003731 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003732 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003733 break;
3734 case 32:
3735 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003736 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003737 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003738 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003739 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003740 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003741 break;
3742 case 64:
3743 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003744 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003745 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003746 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003747 NewTy = S.Context.LongTy;
3748 else
3749 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003750 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003751 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003752 NewTy = S.Context.UnsignedLongTy;
3753 else
3754 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003755 break;
Eli Friedman73397492009-03-03 06:41:03 +00003756 case 96:
3757 NewTy = S.Context.LongDoubleTy;
3758 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003759 case 128:
Roman Divacky19645542013-07-03 21:08:41 +00003760 if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() !=
3761 &llvm::APFloat::PPCDoubleDouble) {
Eli Friedmanf98aba32009-02-13 02:31:07 +00003762 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3763 return;
3764 }
Roman Divackyf0d14cb2013-07-03 20:48:06 +00003765 if (IntegerMode) {
3766 if (OldTy->isSignedIntegerType())
3767 NewTy = S.Context.Int128Ty;
3768 else
3769 NewTy = S.Context.UnsignedInt128Ty;
3770 } else
3771 NewTy = S.Context.LongDoubleTy;
Eli Friedman73397492009-03-03 06:41:03 +00003772 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003773 }
3774
Eli Friedman73397492009-03-03 06:41:03 +00003775 if (ComplexMode) {
3776 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003777 }
3778
3779 // Install the new type.
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003780 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3781 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3782 else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003783 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003784
3785 D->addAttr(::new (S.Context)
3786 ModeAttr(Attr.getRange(), S.Context, Name,
3787 Attr.getAttributeSpellingListIndex()));
Chris Lattnerfbf13472008-06-27 22:18:37 +00003788}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003789
Chandler Carruth1b03c872011-07-02 00:01:44 +00003790static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003791 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003792 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003793 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003794
Nick Lewycky78d1a102012-07-24 01:40:49 +00003795 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3796 if (!VD->hasGlobalStorage())
3797 S.Diag(Attr.getLoc(),
3798 diag::warn_attribute_requires_functions_or_static_globals)
3799 << Attr.getName();
3800 } else if (!isFunctionOrMethod(D)) {
3801 S.Diag(Attr.getLoc(),
3802 diag::warn_attribute_requires_functions_or_static_globals)
3803 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003804 return;
3805 }
Mike Stumpbf916502009-07-24 19:02:52 +00003806
Michael Han51d8c522013-01-24 16:46:58 +00003807 D->addAttr(::new (S.Context)
3808 NoDebugAttr(Attr.getRange(), S.Context,
3809 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003810}
3811
Chandler Carruth1b03c872011-07-02 00:01:44 +00003812static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003813 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003814 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003815 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003816
Mike Stumpbf916502009-07-24 19:02:52 +00003817
Chandler Carruth87c44602011-07-01 23:49:12 +00003818 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003819 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003820 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003821 return;
3822 }
Mike Stumpbf916502009-07-24 19:02:52 +00003823
Michael Han51d8c522013-01-24 16:46:58 +00003824 D->addAttr(::new (S.Context)
3825 NoInlineAttr(Attr.getRange(), S.Context,
3826 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003827}
3828
Chandler Carruth1b03c872011-07-02 00:01:44 +00003829static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3830 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003831 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003832 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003833 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003834
Chris Lattner7255a2d2010-06-22 00:03:40 +00003835
Chandler Carruth87c44602011-07-01 23:49:12 +00003836 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003837 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003838 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003839 return;
3840 }
3841
Michael Han51d8c522013-01-24 16:46:58 +00003842 D->addAttr(::new (S.Context)
3843 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3844 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003845}
3846
Chandler Carruth1b03c872011-07-02 00:01:44 +00003847static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003848 if (S.LangOpts.CUDA) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003849 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003850 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003851
Chandler Carruth87c44602011-07-01 23:49:12 +00003852 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003853 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003854 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003855 return;
3856 }
3857
Michael Han51d8c522013-01-24 16:46:58 +00003858 D->addAttr(::new (S.Context)
3859 CUDAConstantAttr(Attr.getRange(), S.Context,
3860 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003861 } else {
3862 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3863 }
3864}
3865
Chandler Carruth1b03c872011-07-02 00:01:44 +00003866static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003867 if (S.LangOpts.CUDA) {
3868 // check the attribute arguments.
3869 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003870 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3871 << Attr.getName() << 0;
Peter Collingbourneced76712010-12-01 03:15:31 +00003872 return;
3873 }
3874
Chandler Carruth87c44602011-07-01 23:49:12 +00003875 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003876 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003877 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003878 return;
3879 }
3880
Michael Han51d8c522013-01-24 16:46:58 +00003881 D->addAttr(::new (S.Context)
3882 CUDADeviceAttr(Attr.getRange(), S.Context,
3883 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003884 } else {
3885 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3886 }
3887}
3888
Chandler Carruth1b03c872011-07-02 00:01:44 +00003889static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003890 if (S.LangOpts.CUDA) {
3891 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003892 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003893 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003894
Chandler Carruth87c44602011-07-01 23:49:12 +00003895 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003896 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003897 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003898 return;
3899 }
3900
Chandler Carruth87c44602011-07-01 23:49:12 +00003901 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003902 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003903 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00003904 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003905 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3906 << FD->getType()
David Blaikie39e6ab42013-02-18 22:06:02 +00003907 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003908 "void");
3909 } else {
3910 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3911 << FD->getType();
3912 }
3913 return;
3914 }
3915
Michael Han51d8c522013-01-24 16:46:58 +00003916 D->addAttr(::new (S.Context)
3917 CUDAGlobalAttr(Attr.getRange(), S.Context,
3918 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003919 } else {
3920 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3921 }
3922}
3923
Chandler Carruth1b03c872011-07-02 00:01:44 +00003924static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003925 if (S.LangOpts.CUDA) {
3926 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003927 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003928 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003929
Peter Collingbourneced76712010-12-01 03:15:31 +00003930
Chandler Carruth87c44602011-07-01 23:49:12 +00003931 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003932 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003933 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003934 return;
3935 }
3936
Michael Han51d8c522013-01-24 16:46:58 +00003937 D->addAttr(::new (S.Context)
3938 CUDAHostAttr(Attr.getRange(), S.Context,
3939 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003940 } else {
3941 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3942 }
3943}
3944
Chandler Carruth1b03c872011-07-02 00:01:44 +00003945static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003946 if (S.LangOpts.CUDA) {
3947 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003948 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003949 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003950
Chandler Carruth87c44602011-07-01 23:49:12 +00003951 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003952 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003953 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003954 return;
3955 }
3956
Michael Han51d8c522013-01-24 16:46:58 +00003957 D->addAttr(::new (S.Context)
3958 CUDASharedAttr(Attr.getRange(), S.Context,
3959 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003960 } else {
3961 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3962 }
3963}
3964
Chandler Carruth1b03c872011-07-02 00:01:44 +00003965static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003966 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003967 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003968 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003969
Chandler Carruth87c44602011-07-01 23:49:12 +00003970 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003971 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003972 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003973 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003974 return;
3975 }
Mike Stumpbf916502009-07-24 19:02:52 +00003976
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003977 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003978 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003979 return;
3980 }
Mike Stumpbf916502009-07-24 19:02:52 +00003981
Michael Han51d8c522013-01-24 16:46:58 +00003982 D->addAttr(::new (S.Context)
3983 GNUInlineAttr(Attr.getRange(), S.Context,
3984 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00003985}
3986
Chandler Carruth1b03c872011-07-02 00:01:44 +00003987static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003988 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003989
Aaron Ballmanfff32482012-12-09 17:45:41 +00003990 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003991 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003992 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3993 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003994 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003995 return;
3996
Chandler Carruth87c44602011-07-01 23:49:12 +00003997 if (!isa<ObjCMethodDecl>(D)) {
3998 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3999 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00004000 return;
4001 }
4002
Chandler Carruth87c44602011-07-01 23:49:12 +00004003 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004004 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00004005 D->addAttr(::new (S.Context)
4006 FastCallAttr(Attr.getRange(), S.Context,
4007 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00004008 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004009 case AttributeList::AT_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00004010 D->addAttr(::new (S.Context)
4011 StdCallAttr(Attr.getRange(), S.Context,
4012 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00004013 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004014 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00004015 D->addAttr(::new (S.Context)
4016 ThisCallAttr(Attr.getRange(), S.Context,
4017 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00004018 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004019 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00004020 D->addAttr(::new (S.Context)
4021 CDeclAttr(Attr.getRange(), S.Context,
4022 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00004023 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004024 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00004025 D->addAttr(::new (S.Context)
4026 PascalAttr(Attr.getRange(), S.Context,
4027 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00004028 return;
Charles Davise8519c32013-08-30 04:39:01 +00004029 case AttributeList::AT_MSABI:
4030 D->addAttr(::new (S.Context)
4031 MSABIAttr(Attr.getRange(), S.Context,
4032 Attr.getAttributeSpellingListIndex()));
4033 return;
4034 case AttributeList::AT_SysVABI:
4035 D->addAttr(::new (S.Context)
4036 SysVABIAttr(Attr.getRange(), S.Context,
4037 Attr.getAttributeSpellingListIndex()));
4038 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004039 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004040 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00004041 switch (CC) {
4042 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004043 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00004044 break;
4045 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004046 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00004047 break;
4048 default:
4049 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004050 }
4051
Michael Han51d8c522013-01-24 16:46:58 +00004052 D->addAttr(::new (S.Context)
4053 PcsAttr(Attr.getRange(), S.Context, PCS,
4054 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00004055 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004056 }
Derek Schuff263366f2012-10-16 22:30:41 +00004057 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00004058 D->addAttr(::new (S.Context)
4059 PnaclCallAttr(Attr.getRange(), S.Context,
4060 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00004061 return;
Guy Benyei38980082012-12-25 08:53:55 +00004062 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00004063 D->addAttr(::new (S.Context)
4064 IntelOclBiccAttr(Attr.getRange(), S.Context,
4065 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00004066 return;
Derek Schuff263366f2012-10-16 22:30:41 +00004067
Abramo Bagnarae215f722010-04-30 13:10:51 +00004068 default:
4069 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00004070 }
4071}
4072
Chandler Carruth1b03c872011-07-02 00:01:44 +00004073static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004074 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00004075}
4076
Guy Benyei1db70402013-03-24 13:58:12 +00004077static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
Aaron Ballman624421f2013-08-31 01:11:41 +00004078 Expr *E = Attr.getArgAsExpr(0);
Guy Benyei1db70402013-03-24 13:58:12 +00004079 llvm::APSInt ArgNum(32);
4080 if (E->isTypeDependent() || E->isValueDependent() ||
4081 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00004082 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4083 << Attr.getName() << AANT_ArgumentIntegerConstant
4084 << E->getSourceRange();
Guy Benyei1db70402013-03-24 13:58:12 +00004085 return;
4086 }
4087
4088 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
4089 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
4090}
4091
Aaron Ballmanfff32482012-12-09 17:45:41 +00004092bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
4093 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00004094 if (attr.isInvalid())
4095 return true;
4096
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004097 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman624421f2013-08-31 01:11:41 +00004098 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall711c52b2011-01-05 12:14:39 +00004099 attr.setInvalid();
4100 return true;
4101 }
4102
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004103 // TODO: diagnose uses of these conventions on the wrong target. Or, better
4104 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00004105 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004106 case AttributeList::AT_CDecl: CC = CC_C; break;
4107 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
4108 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
4109 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
4110 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davise8519c32013-08-30 04:39:01 +00004111 case AttributeList::AT_MSABI:
4112 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
4113 CC_X86_64Win64;
4114 break;
4115 case AttributeList::AT_SysVABI:
4116 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
4117 CC_C;
4118 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004119 case AttributeList::AT_Pcs: {
Aaron Ballman624421f2013-08-31 01:11:41 +00004120 StringLiteral *Str = 0;
4121 if (attr.isArgExpr(0))
4122 Str = dyn_cast<StringLiteral>(attr.getArgAsExpr(0));
4123
Douglas Gregor5cee1192011-07-27 05:40:30 +00004124 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004125 Diag(attr.getLoc(), diag::err_attribute_argument_type) << attr.getName()
4126 << AANT_ArgumentString;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004127 attr.setInvalid();
4128 return true;
4129 }
4130
Chris Lattner5f9e2722011-07-23 10:55:15 +00004131 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004132 if (StrRef == "aapcs") {
4133 CC = CC_AAPCS;
4134 break;
4135 } else if (StrRef == "aapcs-vfp") {
4136 CC = CC_AAPCS_VFP;
4137 break;
4138 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004139
4140 attr.setInvalid();
4141 Diag(attr.getLoc(), diag::err_invalid_pcs);
4142 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004143 }
Derek Schuff263366f2012-10-16 22:30:41 +00004144 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00004145 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00004146 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00004147 }
4148
Aaron Ballman82bfa192012-10-02 14:26:08 +00004149 const TargetInfo &TI = Context.getTargetInfo();
4150 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
4151 if (A == TargetInfo::CCCR_Warning) {
4152 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00004153
4154 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
4155 if (FD)
4156 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
4157 TargetInfo::CCMT_NonMember;
4158 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00004159 }
4160
John McCall711c52b2011-01-05 12:14:39 +00004161 return false;
4162}
4163
Chandler Carruth1b03c872011-07-02 00:01:44 +00004164static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004165 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00004166
4167 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00004168 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00004169 return;
4170
Chandler Carruth87c44602011-07-01 23:49:12 +00004171 if (!isa<ObjCMethodDecl>(D)) {
4172 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4173 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004174 return;
4175 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004176
Michael Han51d8c522013-01-24 16:46:58 +00004177 D->addAttr(::new (S.Context)
4178 RegparmAttr(Attr.getRange(), S.Context, numParams,
4179 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00004180}
4181
4182/// Checks a regparm attribute, returning true if it is ill-formed and
4183/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00004184bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
4185 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00004186 return true;
4187
Aaron Ballmanffa9d572013-07-18 18:01:48 +00004188 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004189 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004190 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004191 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004192
Aaron Ballman624421f2013-08-31 01:11:41 +00004193 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004194 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00004195 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00004196 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00004197 Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4198 << Attr.getName() << AANT_ArgumentIntegerConstant
4199 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004200 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004201 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004202 }
4203
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004204 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004205 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004206 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004207 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004208 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004209 }
4210
John McCall711c52b2011-01-05 12:14:39 +00004211 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004212 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004213 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004214 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004215 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004216 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004217 }
4218
John McCall711c52b2011-01-05 12:14:39 +00004219 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004220}
4221
Chandler Carruth1b03c872011-07-02 00:01:44 +00004222static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00004223 if (S.LangOpts.CUDA) {
4224 // check the attribute arguments.
4225 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00004226 // FIXME: 0 is not okay.
4227 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004228 return;
4229 }
4230
Chandler Carruth87c44602011-07-01 23:49:12 +00004231 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00004232 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00004233 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004234 return;
4235 }
4236
Aaron Ballman624421f2013-08-31 01:11:41 +00004237 Expr *MaxThreadsExpr = Attr.getArgAsExpr(0);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004238 llvm::APSInt MaxThreads(32);
4239 if (MaxThreadsExpr->isTypeDependent() ||
4240 MaxThreadsExpr->isValueDependent() ||
4241 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004242 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004243 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004244 << MaxThreadsExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004245 return;
4246 }
4247
4248 llvm::APSInt MinBlocks(32);
4249 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004250 Expr *MinBlocksExpr = Attr.getArgAsExpr(1);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004251 if (MinBlocksExpr->isTypeDependent() ||
4252 MinBlocksExpr->isValueDependent() ||
4253 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004254 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004255 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004256 << MinBlocksExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004257 return;
4258 }
4259 }
4260
Michael Han51d8c522013-01-24 16:46:58 +00004261 D->addAttr(::new (S.Context)
4262 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4263 MaxThreads.getZExtValue(),
4264 MinBlocks.getZExtValue(),
4265 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne7b381982010-12-12 23:03:07 +00004266 } else {
4267 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4268 }
4269}
4270
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004271static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4272 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004273 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004274 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004275 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004276 return;
4277 }
Aaron Ballman624421f2013-08-31 01:11:41 +00004278
4279 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004280 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004281
Aaron Ballman624421f2013-08-31 01:11:41 +00004282 StringRef AttrName = Attr.getName()->getName();
4283 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004284
4285 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4286 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4287 << Attr.getName() << ExpectedFunctionOrMethod;
4288 return;
4289 }
4290
4291 uint64_t ArgumentIdx;
4292 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4293 Attr.getLoc(), 2,
Aaron Ballman624421f2013-08-31 01:11:41 +00004294 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004295 return;
4296
4297 uint64_t TypeTagIdx;
4298 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4299 Attr.getLoc(), 3,
Aaron Ballman624421f2013-08-31 01:11:41 +00004300 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004301 return;
4302
4303 bool IsPointer = (AttrName == "pointer_with_type_tag");
4304 if (IsPointer) {
4305 // Ensure that buffer has a pointer type.
4306 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4307 if (!BufferTy->isPointerType()) {
4308 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004309 << Attr.getName();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004310 }
4311 }
4312
Michael Han51d8c522013-01-24 16:46:58 +00004313 D->addAttr(::new (S.Context)
4314 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4315 ArgumentIdx, TypeTagIdx, IsPointer,
4316 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004317}
4318
4319static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4320 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004321 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004322 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004323 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004324 return;
4325 }
Aaron Ballman624421f2013-08-31 01:11:41 +00004326
4327 if (!checkAttributeNumArgs(S, Attr, 1))
4328 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004329
Aaron Ballman624421f2013-08-31 01:11:41 +00004330 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004331 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4332
Michael Han51d8c522013-01-24 16:46:58 +00004333 D->addAttr(::new (S.Context)
4334 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4335 MatchingCType,
4336 Attr.getLayoutCompatible(),
4337 Attr.getMustBeNull(),
4338 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004339}
4340
Chris Lattner0744e5f2008-06-29 00:23:49 +00004341//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00004342// Checker-specific attribute handlers.
4343//===----------------------------------------------------------------------===//
4344
John McCallc7ad3812011-01-25 03:31:58 +00004345static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004346 return type->isDependentType() ||
4347 type->isObjCObjectPointerType() ||
4348 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00004349}
4350static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004351 return type->isDependentType() ||
4352 type->isPointerType() ||
4353 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00004354}
4355
Chandler Carruth1b03c872011-07-02 00:01:44 +00004356static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004357 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00004358 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004359 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004360 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00004361 return;
4362 }
4363
4364 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00004365 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00004366 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4367 cf = false;
4368 } else {
4369 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4370 cf = true;
4371 }
4372
4373 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004374 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004375 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00004376 return;
4377 }
4378
4379 if (cf)
Michael Han51d8c522013-01-24 16:46:58 +00004380 param->addAttr(::new (S.Context)
4381 CFConsumedAttr(Attr.getRange(), S.Context,
4382 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004383 else
Michael Han51d8c522013-01-24 16:46:58 +00004384 param->addAttr(::new (S.Context)
4385 NSConsumedAttr(Attr.getRange(), S.Context,
4386 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004387}
4388
Chandler Carruth1b03c872011-07-02 00:01:44 +00004389static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4390 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004391 if (!isa<ObjCMethodDecl>(D)) {
4392 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004393 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00004394 return;
4395 }
4396
Michael Han51d8c522013-01-24 16:46:58 +00004397 D->addAttr(::new (S.Context)
4398 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4399 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004400}
4401
Chandler Carruth1b03c872011-07-02 00:01:44 +00004402static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4403 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004404
John McCallc7ad3812011-01-25 03:31:58 +00004405 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00004406
Chandler Carruth87c44602011-07-01 23:49:12 +00004407 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004408 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004409 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00004410 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00004411 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00004412 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4413 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00004414 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004415 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004416 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00004417 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004418 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00004419 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004420 return;
4421 }
Mike Stumpbf916502009-07-24 19:02:52 +00004422
John McCallc7ad3812011-01-25 03:31:58 +00004423 bool typeOK;
4424 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00004425 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00004426 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004427 case AttributeList::AT_NSReturnsAutoreleased:
4428 case AttributeList::AT_NSReturnsRetained:
4429 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004430 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4431 cf = false;
4432 break;
4433
Sean Hunt8e083e72012-06-19 23:57:03 +00004434 case AttributeList::AT_CFReturnsRetained:
4435 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004436 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4437 cf = true;
4438 break;
4439 }
4440
4441 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004442 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004443 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00004444 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004445 }
Mike Stumpbf916502009-07-24 19:02:52 +00004446
Chandler Carruth87c44602011-07-01 23:49:12 +00004447 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004448 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00004449 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004450 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han51d8c522013-01-24 16:46:58 +00004451 D->addAttr(::new (S.Context)
4452 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4453 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004454 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004455 case AttributeList::AT_CFReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004456 D->addAttr(::new (S.Context)
4457 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4458 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004459 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004460 case AttributeList::AT_NSReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004461 D->addAttr(::new (S.Context)
4462 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4463 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004464 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004465 case AttributeList::AT_CFReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004466 D->addAttr(::new (S.Context)
4467 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4468 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004469 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004470 case AttributeList::AT_NSReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004471 D->addAttr(::new (S.Context)
4472 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4473 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004474 return;
4475 };
4476}
4477
John McCalldc7c5ad2011-07-22 08:53:00 +00004478static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4479 const AttributeList &attr) {
4480 SourceLocation loc = attr.getLoc();
4481
4482 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4483
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004484 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00004485 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004486 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00004487 return;
4488 }
4489
4490 // Check that the method returns a normal pointer.
4491 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004492
4493 if (!resultType->isReferenceType() &&
4494 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00004495 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4496 << SourceRange(loc)
4497 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4498
4499 // Drop the attribute.
4500 return;
4501 }
4502
Michael Han51d8c522013-01-24 16:46:58 +00004503 method->addAttr(::new (S.Context)
4504 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4505 attr.getAttributeSpellingListIndex()));
John McCalldc7c5ad2011-07-22 08:53:00 +00004506}
4507
Fariborz Jahanian84101132012-09-07 23:46:23 +00004508static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4509 const AttributeList &attr) {
4510 SourceLocation loc = attr.getLoc();
4511 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4512
4513 if (!method) {
4514 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4515 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4516 return;
4517 }
4518 DeclContext *DC = method->getDeclContext();
4519 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4520 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4521 << attr.getName() << 0;
4522 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4523 return;
4524 }
4525 if (method->getMethodFamily() == OMF_dealloc) {
4526 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4527 << attr.getName() << 1;
4528 return;
4529 }
4530
Michael Han51d8c522013-01-24 16:46:58 +00004531 method->addAttr(::new (S.Context)
4532 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4533 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian84101132012-09-07 23:46:23 +00004534}
4535
John McCall8dfac0b2011-09-30 05:12:12 +00004536/// Handle cf_audited_transfer and cf_unknown_transfer.
4537static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4538 if (!isa<FunctionDecl>(D)) {
4539 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004540 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004541 return;
4542 }
4543
Sean Hunt8e083e72012-06-19 23:57:03 +00004544 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004545
4546 // Check whether there's a conflicting attribute already present.
4547 Attr *Existing;
4548 if (IsAudited) {
4549 Existing = D->getAttr<CFUnknownTransferAttr>();
4550 } else {
4551 Existing = D->getAttr<CFAuditedTransferAttr>();
4552 }
4553 if (Existing) {
4554 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4555 << A.getName()
4556 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4557 << A.getRange() << Existing->getRange();
4558 return;
4559 }
4560
4561 // All clear; add the attribute.
4562 if (IsAudited) {
Michael Han51d8c522013-01-24 16:46:58 +00004563 D->addAttr(::new (S.Context)
4564 CFAuditedTransferAttr(A.getRange(), S.Context,
4565 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004566 } else {
Michael Han51d8c522013-01-24 16:46:58 +00004567 D->addAttr(::new (S.Context)
4568 CFUnknownTransferAttr(A.getRange(), S.Context,
4569 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004570 }
4571}
4572
John McCallfe98da02011-09-29 07:17:38 +00004573static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4574 const AttributeList &Attr) {
4575 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4576 if (!RD || RD->isUnion()) {
4577 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004578 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004579 }
4580
Aaron Ballman624421f2013-08-31 01:11:41 +00004581 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallfe98da02011-09-29 07:17:38 +00004582
4583 // In Objective-C, verify that the type names an Objective-C type.
4584 // We don't want to check this outside of ObjC because people sometimes
4585 // do crazy C declarations of Objective-C types.
Aaron Ballman624421f2013-08-31 01:11:41 +00004586 if (Parm && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004587 // Check for an existing type with this name.
Aaron Ballman624421f2013-08-31 01:11:41 +00004588 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallfe98da02011-09-29 07:17:38 +00004589 Sema::LookupOrdinaryName);
4590 if (S.LookupName(R, Sc)) {
4591 NamedDecl *Target = R.getFoundDecl();
4592 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4593 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4594 S.Diag(Target->getLocStart(), diag::note_declared_at);
4595 }
4596 }
4597 }
4598
Michael Han51d8c522013-01-24 16:46:58 +00004599 D->addAttr(::new (S.Context)
Aaron Ballman624421f2013-08-31 01:11:41 +00004600 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han51d8c522013-01-24 16:46:58 +00004601 Attr.getAttributeSpellingListIndex()));
John McCallfe98da02011-09-29 07:17:38 +00004602}
4603
Chandler Carruth1b03c872011-07-02 00:01:44 +00004604static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4605 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004606 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004607
Chandler Carruth87c44602011-07-01 23:49:12 +00004608 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004609 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004610}
4611
Chandler Carruth1b03c872011-07-02 00:01:44 +00004612static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4613 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004614 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004615 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004616 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004617 return;
4618 }
4619
Chandler Carruth87c44602011-07-01 23:49:12 +00004620 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004621 QualType type = vd->getType();
4622
4623 if (!type->isDependentType() &&
4624 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004625 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004626 << type;
4627 return;
4628 }
4629
4630 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4631
4632 // If we have no lifetime yet, check the lifetime we're presumably
4633 // going to infer.
4634 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4635 lifetime = type->getObjCARCImplicitLifetime();
4636
4637 switch (lifetime) {
4638 case Qualifiers::OCL_None:
4639 assert(type->isDependentType() &&
4640 "didn't infer lifetime for non-dependent type?");
4641 break;
4642
4643 case Qualifiers::OCL_Weak: // meaningful
4644 case Qualifiers::OCL_Strong: // meaningful
4645 break;
4646
4647 case Qualifiers::OCL_ExplicitNone:
4648 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004649 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004650 << (lifetime == Qualifiers::OCL_Autoreleasing);
4651 break;
4652 }
4653
Chandler Carruth87c44602011-07-01 23:49:12 +00004654 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004655 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4656 Attr.getAttributeSpellingListIndex()));
John McCallf85e1932011-06-15 23:02:42 +00004657}
4658
Francois Pichet11542142010-12-19 06:50:37 +00004659//===----------------------------------------------------------------------===//
4660// Microsoft specific attribute handlers.
4661//===----------------------------------------------------------------------===//
4662
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004663// Check if MS extensions or some other language extensions are enabled. If
4664// not, issue a diagnostic that the given attribute is unused.
4665static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4666 bool OtherExtension = false) {
4667 if (S.LangOpts.MicrosoftExt || OtherExtension)
4668 return true;
4669 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4670 return false;
4671}
4672
Chandler Carruth1b03c872011-07-02 00:01:44 +00004673static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004674 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4675 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004676
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004677 // check the attribute arguments.
4678 if (!checkAttributeNumArgs(S, Attr, 1))
4679 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004680
Aaron Ballman624421f2013-08-31 01:11:41 +00004681 Expr *Arg = Attr.getArgAsExpr(0);
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004682 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
4683 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004684 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4685 << Attr.getName() << AANT_ArgumentString;
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004686 return;
4687 }
Francois Pichetd3d3be92010-12-20 01:41:49 +00004688
David Majnemerbb0ed292013-08-09 08:56:20 +00004689 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4690 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004691 StringRef StrRef = Str->getString();
David Majnemerbb0ed292013-08-09 08:56:20 +00004692 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4693 StrRef = StrRef.drop_front().drop_back();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004694
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004695 // Validate GUID length.
David Majnemerbb0ed292013-08-09 08:56:20 +00004696 if (StrRef.size() != 36) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004697 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4698 return;
4699 }
Anders Carlssonf89e0422011-01-23 21:07:30 +00004700
David Majnemerbb0ed292013-08-09 08:56:20 +00004701 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004702 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemerbb0ed292013-08-09 08:56:20 +00004703 if (StrRef[i] != '-') {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004704 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4705 return;
4706 }
David Majnemerbb0ed292013-08-09 08:56:20 +00004707 } else if (!isHexDigit(StrRef[i])) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004708 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4709 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004710 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004711 }
Francois Pichet11542142010-12-19 06:50:37 +00004712
David Majnemerbb0ed292013-08-09 08:56:20 +00004713 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4714 Attr.getAttributeSpellingListIndex()));
Charles Davisf0122fe2010-02-16 18:27:26 +00004715}
4716
John McCallc052dbb2012-05-22 21:28:12 +00004717static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004718 if (!checkMicrosoftExt(S, Attr))
Nico Weber7b89ab72012-11-07 21:31:36 +00004719 return;
Nico Weber7b89ab72012-11-07 21:31:36 +00004720
4721 AttributeList::Kind Kind = Attr.getKind();
4722 if (Kind == AttributeList::AT_SingleInheritance)
4723 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004724 ::new (S.Context)
4725 SingleInheritanceAttr(Attr.getRange(), S.Context,
4726 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004727 else if (Kind == AttributeList::AT_MultipleInheritance)
4728 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004729 ::new (S.Context)
4730 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4731 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004732 else if (Kind == AttributeList::AT_VirtualInheritance)
4733 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004734 ::new (S.Context)
4735 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4736 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004737}
4738
4739static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004740 if (!checkMicrosoftExt(S, Attr))
4741 return;
4742
4743 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004744 if (Kind == AttributeList::AT_Win64)
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004745 D->addAttr(
4746 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4747 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004748}
4749
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004750static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004751 if (!checkMicrosoftExt(S, Attr))
4752 return;
4753 D->addAttr(::new (S.Context)
4754 ForceInlineAttr(Attr.getRange(), S.Context,
4755 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004756}
4757
Reid Klecknera7225342013-05-20 14:02:37 +00004758static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4759 if (!checkMicrosoftExt(S, Attr))
4760 return;
4761 // Check linkage after possibly merging declaratinos. See
4762 // checkAttributesAfterMerging().
4763 D->addAttr(::new (S.Context)
4764 SelectAnyAttr(Attr.getRange(), S.Context,
4765 Attr.getAttributeSpellingListIndex()));
4766}
4767
Ted Kremenekb71368d2009-05-09 02:44:38 +00004768//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004769// Top Level Sema Entry Points
4770//===----------------------------------------------------------------------===//
4771
Richard Smith4a97b8e2013-08-29 00:47:48 +00004772/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4773/// the attribute applies to decls. If the attribute is a type attribute, just
4774/// silently ignore it if a GNU attribute.
4775static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4776 const AttributeList &Attr,
4777 bool IncludeCXX11Attributes) {
4778 if (Attr.isInvalid())
4779 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00004780
Richard Smith4a97b8e2013-08-29 00:47:48 +00004781 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4782 // instead.
4783 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4784 return;
4785
Chris Lattner803d0802008-06-29 00:43:07 +00004786 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004787 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4788 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4789 case AttributeList::AT_IBOutletCollection:
4790 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004791 case AttributeList::AT_AddressSpace:
Sean Hunt8e083e72012-06-19 23:57:03 +00004792 case AttributeList::AT_ObjCGC:
4793 case AttributeList::AT_VectorSize:
4794 case AttributeList::AT_NeonVectorType:
4795 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004796 case AttributeList::AT_Ptr32:
4797 case AttributeList::AT_Ptr64:
4798 case AttributeList::AT_SPtr:
4799 case AttributeList::AT_UPtr:
Mike Stumpbf916502009-07-24 19:02:52 +00004800 // Ignore these, these are type attributes, handled by
4801 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004802 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004803 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4804 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4805 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4806 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004807 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004808 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004809 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004810 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004811 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4812 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4813 case AttributeList::AT_CarriesDependency:
Richard Smith3a2b7a12013-01-28 22:42:45 +00004814 handleDependencyAttr(S, scope, D, Attr);
4815 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004816 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4817 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4818 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004819 case AttributeList::AT_CXX11NoReturn:
4820 handleCXX11NoReturnAttr(S, D, Attr);
4821 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004822 case AttributeList::AT_Deprecated:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004823 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004824 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004825 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4826 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004827 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004828 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004829 case AttributeList::AT_MinSize:
4830 handleMinSizeAttr(S, D, Attr);
4831 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004832 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4833 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4834 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman19513232013-08-28 23:13:26 +00004835 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4836 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004837 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4838 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004839 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004840 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004841 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4842 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004843 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004844 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4845 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004846 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004847 case AttributeList::AT_ownership_returns:
4848 case AttributeList::AT_ownership_takes:
4849 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004850 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004851 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4852 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4853 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4854 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4855 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4856 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4857 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004858
Sean Hunt8e083e72012-06-19 23:57:03 +00004859 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004860 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004861 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004862 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004863
Sean Hunt8e083e72012-06-19 23:57:03 +00004864 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004865 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4866
Fariborz Jahanian84101132012-09-07 23:46:23 +00004867 case AttributeList::AT_ObjCRequiresSuper:
4868 handleObjCRequiresSuperAttr(S, D, Attr); break;
4869
Sean Hunt8e083e72012-06-19 23:57:03 +00004870 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004871 handleNSBridgedAttr(S, scope, D, Attr); break;
4872
Sean Hunt8e083e72012-06-19 23:57:03 +00004873 case AttributeList::AT_CFAuditedTransfer:
4874 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004875 handleCFTransferAttr(S, D, Attr); break;
4876
Ted Kremenekb71368d2009-05-09 02:44:38 +00004877 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004878 case AttributeList::AT_CFConsumed:
4879 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4880 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004881 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004882
Sean Hunt8e083e72012-06-19 23:57:03 +00004883 case AttributeList::AT_NSReturnsAutoreleased:
4884 case AttributeList::AT_NSReturnsNotRetained:
4885 case AttributeList::AT_CFReturnsNotRetained:
4886 case AttributeList::AT_NSReturnsRetained:
4887 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004888 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004889
Tanya Lattner0df579e2012-07-09 22:06:01 +00004890 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004891 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004892 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004893
Joey Gouly37453b92013-03-08 09:42:32 +00004894 case AttributeList::AT_VecTypeHint:
4895 handleVecTypeHint(S, D, Attr); break;
4896
Joey Gouly96cead52013-03-14 09:54:43 +00004897 case AttributeList::AT_Endian:
4898 handleEndianAttr(S, D, Attr);
4899 break;
4900
Sean Hunt8e083e72012-06-19 23:57:03 +00004901 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004902 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004903
Sean Hunt8e083e72012-06-19 23:57:03 +00004904 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4905 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4906 case AttributeList::AT_Unavailable:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004907 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004908 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004909 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004910 handleArcWeakrefUnavailableAttr (S, D, Attr);
4911 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004912 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004913 handleObjCRootClassAttr(S, D, Attr);
4914 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004915 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004916 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004917 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004918 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4919 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004920 handleReturnsTwiceAttr(S, D, Attr);
4921 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004922 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld4c3d662013-02-20 01:54:26 +00004923 case AttributeList::AT_Visibility:
4924 handleVisibilityAttr(S, D, Attr, false);
4925 break;
4926 case AttributeList::AT_TypeVisibility:
4927 handleVisibilityAttr(S, D, Attr, true);
4928 break;
Lubos Lunak1d3ce652013-07-20 15:05:36 +00004929 case AttributeList::AT_WarnUnused:
4930 handleWarnUnusedAttr(S, D, Attr);
4931 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004932 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004933 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004934 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4935 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4936 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4937 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004938 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004939 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004940 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004941 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004942 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004943 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004944 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004945 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004946 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4947 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4948 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4949 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4950 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4951 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4952 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4953 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4954 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004955 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004956 // Just ignore
4957 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004958 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004959 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004960 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004961 case AttributeList::AT_StdCall:
4962 case AttributeList::AT_CDecl:
4963 case AttributeList::AT_FastCall:
4964 case AttributeList::AT_ThisCall:
4965 case AttributeList::AT_Pascal:
Charles Davise8519c32013-08-30 04:39:01 +00004966 case AttributeList::AT_MSABI:
4967 case AttributeList::AT_SysVABI:
Sean Hunt8e083e72012-06-19 23:57:03 +00004968 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004969 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004970 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004971 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004972 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004973 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004974 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004975 break;
Guy Benyei1db70402013-03-24 13:58:12 +00004976 case AttributeList::AT_OpenCLImageAccess:
4977 handleOpenCLImageAccessAttr(S, D, Attr);
4978 break;
John McCallc052dbb2012-05-22 21:28:12 +00004979
4980 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004981 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004982 handleMsStructAttr(S, D, Attr);
4983 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004984 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004985 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004986 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004987 case AttributeList::AT_SingleInheritance:
4988 case AttributeList::AT_MultipleInheritance:
4989 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004990 handleInheritanceAttr(S, D, Attr);
4991 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004992 case AttributeList::AT_Win64:
John McCallc052dbb2012-05-22 21:28:12 +00004993 handlePortabilityAttr(S, D, Attr);
4994 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004995 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004996 handleForceInlineAttr(S, D, Attr);
4997 break;
Reid Klecknera7225342013-05-20 14:02:37 +00004998 case AttributeList::AT_SelectAny:
4999 handleSelectAnyAttr(S, D, Attr);
5000 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005001
5002 // Thread safety attributes:
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +00005003 case AttributeList::AT_AssertExclusiveLock:
5004 handleAssertExclusiveLockAttr(S, D, Attr);
5005 break;
5006 case AttributeList::AT_AssertSharedLock:
5007 handleAssertSharedLockAttr(S, D, Attr);
5008 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005009 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005010 handleGuardedVarAttr(S, D, Attr);
5011 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005012 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00005013 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005014 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005015 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00005016 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005017 break;
Kostya Serebryany85aee962013-02-26 06:58:27 +00005018 case AttributeList::AT_NoSanitizeAddress:
5019 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany71efba02012-01-24 19:25:38 +00005020 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005021 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany85aee962013-02-26 06:58:27 +00005022 handleNoThreadSafetyAnalysis(S, D, Attr);
5023 break;
5024 case AttributeList::AT_NoSanitizeThread:
5025 handleNoSanitizeThread(S, D, Attr);
5026 break;
5027 case AttributeList::AT_NoSanitizeMemory:
5028 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005029 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005030 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005031 handleLockableAttr(S, D, Attr);
5032 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005033 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005034 handleGuardedByAttr(S, D, Attr);
5035 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005036 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00005037 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005038 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005039 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00005040 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005041 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005042 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00005043 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005044 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005045 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00005046 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005047 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005048 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005049 handleLockReturnedAttr(S, D, Attr);
5050 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005051 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005052 handleLocksExcludedAttr(S, D, Attr);
5053 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005054 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00005055 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005056 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005057 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00005058 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005059 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005060 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00005061 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005062 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005063 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005064 handleUnlockFunAttr(S, D, Attr);
5065 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005066 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00005067 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005068 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005069 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00005070 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005071 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005072
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00005073 // Uniqueness analysis attributes.
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00005074 case AttributeList::AT_Consumable:
5075 handleConsumableAttr(S, D, Attr);
5076 break;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00005077 case AttributeList::AT_Consumes:
5078 handleConsumesAttr(S, D, Attr);
5079 break;
5080 case AttributeList::AT_CallableWhenUnconsumed:
5081 handleCallableWhenUnconsumedAttr(S, D, Attr);
5082 break;
5083 case AttributeList::AT_TestsConsumed:
5084 handleTestsConsumedAttr(S, D, Attr);
5085 break;
5086 case AttributeList::AT_TestsUnconsumed:
5087 handleTestsUnconsumedAttr(S, D, Attr);
5088 break;
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00005089 case AttributeList::AT_ReturnTypestate:
5090 handleReturnTypestateAttr(S, D, Attr);
5091 break;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00005092
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00005093 // Type safety attributes.
5094 case AttributeList::AT_ArgumentWithTypeTag:
5095 handleArgumentWithTypeTagAttr(S, D, Attr);
5096 break;
5097 case AttributeList::AT_TypeTagForDatatype:
5098 handleTypeTagForDatatypeAttr(S, D, Attr);
5099 break;
5100
Chris Lattner803d0802008-06-29 00:43:07 +00005101 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005102 // Ask target about the attribute.
5103 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
5104 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00005105 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
5106 diag::warn_unhandled_ms_attribute_ignored :
5107 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00005108 break;
5109 }
5110}
5111
5112/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5113/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00005114void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00005115 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00005116 bool IncludeCXX11Attributes) {
5117 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005118 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005119
5120 // GCC accepts
5121 // static int a9 __attribute__((weakref));
5122 // but that looks really pointless. We reject it.
Richard Smith4a97b8e2013-08-29 00:47:48 +00005123 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005124 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00005125 cast<NamedDecl>(D)->getNameAsString();
5126 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005127 return;
Chris Lattner803d0802008-06-29 00:43:07 +00005128 }
5129}
5130
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005131// Annotation attributes are the only attributes allowed after an access
5132// specifier.
5133bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5134 const AttributeList *AttrList) {
5135 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00005136 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005137 handleAnnotateAttr(*this, ASDecl, *l);
5138 } else {
5139 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5140 return true;
5141 }
5142 }
5143
5144 return false;
5145}
5146
John McCalle82247a2011-10-01 05:17:03 +00005147/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5148/// contains any decl attributes that we should warn about.
5149static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5150 for ( ; A; A = A->getNext()) {
5151 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smithd03de6a2013-01-29 10:02:16 +00005152 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCalle82247a2011-10-01 05:17:03 +00005153 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5154
5155 if (A->getKind() == AttributeList::UnknownAttribute) {
5156 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5157 << A->getName() << A->getRange();
5158 } else {
5159 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5160 << A->getName() << A->getRange();
5161 }
5162 }
5163}
5164
5165/// checkUnusedDeclAttributes - Given a declarator which is not being
5166/// used to build a declaration, complain about any decl attributes
5167/// which might be lying around on it.
5168void Sema::checkUnusedDeclAttributes(Declarator &D) {
5169 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5170 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5171 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5172 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5173}
5174
Ryan Flynne25ff832009-07-30 03:15:39 +00005175/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00005176/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00005177NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5178 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005179 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00005180 NamedDecl *NewD = 0;
5181 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00005182 FunctionDecl *NewFD;
5183 // FIXME: Missing call to CheckFunctionDeclaration().
5184 // FIXME: Mangling?
5185 // FIXME: Is the qualifier info correct?
5186 // FIXME: Is the DeclContext correct?
5187 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5188 Loc, Loc, DeclarationName(II),
5189 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005190 SC_None, false/*isInlineSpecified*/,
Eli Friedman900693b2011-09-07 04:05:06 +00005191 FD->hasPrototype(),
5192 false/*isConstexprSpecified*/);
5193 NewD = NewFD;
5194
5195 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005196 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00005197
5198 // Fake up parameter variables; they are declared as if this were
5199 // a typedef.
5200 QualType FDTy = FD->getType();
5201 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5202 SmallVector<ParmVarDecl*, 16> Params;
5203 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5204 AE = FT->arg_type_end(); AI != AE; ++AI) {
5205 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5206 Param->setScopeInfo(0, Params.size());
5207 Params.push_back(Param);
5208 }
David Blaikie4278c652011-09-21 18:16:56 +00005209 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00005210 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005211 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5212 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005213 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00005214 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005215 VD->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00005216 if (VD->getQualifier()) {
5217 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005218 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00005219 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005220 }
5221 return NewD;
5222}
5223
James Dennett1dfbd922012-06-14 21:40:34 +00005224/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00005225/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005226void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005227 if (W.getUsed()) return; // only do this once
5228 W.setUsed(true);
5229 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5230 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00005231 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00005232 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5233 NDId->getName()));
5234 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005235 WeakTopLevelDecl.push_back(NewD);
5236 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5237 // to insert Decl at TU scope, sorry.
5238 DeclContext *SavedContext = CurContext;
5239 CurContext = Context.getTranslationUnitDecl();
5240 PushOnScopeChains(NewD, S);
5241 CurContext = SavedContext;
5242 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00005243 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00005244 }
5245}
5246
Rafael Espindola65611bf2013-03-02 21:41:48 +00005247void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5248 // It's valid to "forward-declare" #pragma weak, in which case we
5249 // have to do this.
5250 LoadExternalWeakUndeclaredIdentifiers();
5251 if (!WeakUndeclaredIdentifiers.empty()) {
5252 NamedDecl *ND = NULL;
5253 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5254 if (VD->isExternC())
5255 ND = VD;
5256 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5257 if (FD->isExternC())
5258 ND = FD;
5259 if (ND) {
5260 if (IdentifierInfo *Id = ND->getIdentifier()) {
5261 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5262 = WeakUndeclaredIdentifiers.find(Id);
5263 if (I != WeakUndeclaredIdentifiers.end()) {
5264 WeakInfo W = I->second;
5265 DeclApplyPragmaWeak(S, ND, W);
5266 WeakUndeclaredIdentifiers[Id] = W;
5267 }
5268 }
5269 }
5270 }
5271}
5272
Chris Lattner0744e5f2008-06-29 00:23:49 +00005273/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5274/// it, apply them to D. This is a bit tricky because PD can have attributes
5275/// specified in many different places, and we need to find and apply them all.
Richard Smith4a97b8e2013-08-29 00:47:48 +00005276void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner0744e5f2008-06-29 00:23:49 +00005277 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00005278 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005279 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00005280
Chris Lattner0744e5f2008-06-29 00:23:49 +00005281 // Walk the declarator structure, applying decl attributes that were in a type
5282 // position to the decl itself. This handles cases like:
5283 // int *__attr__(x)** D;
5284 // when X is a decl attribute.
5285 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5286 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005287 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00005288
Chris Lattner0744e5f2008-06-29 00:23:49 +00005289 // Finally, apply any attributes on the decl itself.
5290 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005291 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00005292}
John McCall54abf7d2009-11-04 02:18:39 +00005293
John McCallf85e1932011-06-15 23:02:42 +00005294/// Is the given declaration allowed to use a forbidden type?
5295static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5296 // Private ivars are always okay. Unfortunately, people don't
5297 // always properly make their ivars private, even in system headers.
5298 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00005299 // Function declarations in sys headers will be marked unavailable.
5300 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5301 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00005302 return false;
5303
5304 // Require it to be declared in a system header.
5305 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5306}
5307
5308/// Handle a delayed forbidden-type diagnostic.
5309static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5310 Decl *decl) {
5311 if (decl && isForbiddenTypeAllowed(S, decl)) {
5312 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5313 "this system declaration uses an unsupported type"));
5314 return;
5315 }
David Blaikie4e4d0842012-03-11 07:00:24 +00005316 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005317 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00005318 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005319 // kind of forbidden type messages on unavailable functions.
5320 if (FD->hasAttr<UnavailableAttr>() &&
5321 diag.getForbiddenTypeDiagnostic() ==
5322 diag::err_arc_array_param_no_ownership) {
5323 diag.Triggered = true;
5324 return;
5325 }
5326 }
John McCallf85e1932011-06-15 23:02:42 +00005327
5328 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5329 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5330 diag.Triggered = true;
5331}
5332
John McCall92576642012-05-07 06:16:41 +00005333void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5334 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00005335 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00005336 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00005337
John McCall92576642012-05-07 06:16:41 +00005338 // When delaying diagnostics to run in the context of a parsed
5339 // declaration, we only want to actually emit anything if parsing
5340 // succeeds.
5341 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00005342
John McCall92576642012-05-07 06:16:41 +00005343 // We emit all the active diagnostics in this pool or any of its
5344 // parents. In general, we'll get one pool for the decl spec
5345 // and a child pool for each declarator; in a decl group like:
5346 // deprecated_typedef foo, *bar, baz();
5347 // only the declarator pops will be passed decls. This is correct;
5348 // we really do need to consider delayed diagnostics from the decl spec
5349 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005350 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005351 do {
John McCall13489672012-05-07 06:16:58 +00005352 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005353 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5354 // This const_cast is a bit lame. Really, Triggered should be mutable.
5355 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005356 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005357 continue;
5358
John McCalleee1d542011-02-14 07:13:47 +00005359 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005360 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005361 // Don't bother giving deprecation diagnostics if the decl is invalid.
5362 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005363 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005364 break;
5365
5366 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005367 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005368 break;
John McCallf85e1932011-06-15 23:02:42 +00005369
5370 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005371 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005372 break;
John McCall2f514482010-01-27 03:50:35 +00005373 }
5374 }
John McCall92576642012-05-07 06:16:41 +00005375 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005376}
5377
John McCall13489672012-05-07 06:16:58 +00005378/// Given a set of delayed diagnostics, re-emit them as if they had
5379/// been delayed in the current context instead of in the given pool.
5380/// Essentially, this just moves them to the current pool.
5381void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5382 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5383 assert(curPool && "re-emitting in undelayed context not supported");
5384 curPool->steal(pool);
5385}
5386
John McCall54abf7d2009-11-04 02:18:39 +00005387static bool isDeclDeprecated(Decl *D) {
5388 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005389 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005390 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005391 // A category implicitly has the availability of the interface.
5392 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5393 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005394 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5395 return false;
5396}
5397
Eli Friedmanc3b23082012-08-08 21:52:41 +00005398static void
5399DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5400 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005401 const ObjCInterfaceDecl *UnknownObjCClass,
5402 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005403 DeclarationName Name = D->getDeclName();
5404 if (!Message.empty()) {
5405 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5406 S.Diag(D->getLocation(),
5407 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5408 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005409 if (ObjCPropery)
5410 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5411 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005412 } else if (!UnknownObjCClass) {
5413 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5414 S.Diag(D->getLocation(),
5415 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5416 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005417 if (ObjCPropery)
5418 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5419 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005420 } else {
5421 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5422 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5423 }
5424}
5425
John McCall9c3087b2010-08-26 02:13:20 +00005426void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005427 Decl *Ctx) {
5428 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005429 return;
5430
John McCall2f514482010-01-27 03:50:35 +00005431 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005432 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5433 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005434 DD.getUnknownObjCClass(),
5435 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005436}
5437
Chris Lattner5f9e2722011-07-23 10:55:15 +00005438void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005439 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005440 const ObjCInterfaceDecl *UnknownObjCClass,
5441 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005442 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005443 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005444 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5445 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005446 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005447 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005448 return;
5449 }
5450
5451 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005452 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005453 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005454 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005455}