blob: 78b963c16ffb4805354177c606613f32d76b45a2 [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) {
David Blaikiea33ab602013-09-06 01:28:43 +0000971 if (!checkAttributeNumArgs(S, Attr, 1))
972 return;
973
974 ConsumableAttr::ConsumedState DefaultState;
975
976 if (Attr.isArgIdent(0)) {
977 StringRef Param = Attr.getArgAsIdent(0)->Ident->getName();
978
979 if (Param == "unknown")
980 DefaultState = ConsumableAttr::Unknown;
981 else if (Param == "consumed")
982 DefaultState = ConsumableAttr::Consumed;
983 else if (Param == "unconsumed")
984 DefaultState = ConsumableAttr::Unconsumed;
985 else {
986 S.Diag(Attr.getLoc(), diag::warn_unknown_consumed_state) << Param;
987 return;
988 }
989
990 } else {
991 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
992 << Attr.getName() << AANT_ArgumentIdentifier;
993 return;
994 }
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +0000995
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +0000996 if (!isa<CXXRecordDecl>(D)) {
997 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
998 Attr.getName() << ExpectedClass;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +0000999 return;
1000 }
1001
1002 D->addAttr(::new (S.Context)
David Blaikiea33ab602013-09-06 01:28:43 +00001003 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001004 Attr.getAttributeSpellingListIndex()));
1005}
1006
1007static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
1008 const AttributeList &Attr) {
1009 ASTContext &CurrContext = S.getASTContext();
1010 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
1011
1012 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1013 if (!RD->hasAttr<ConsumableAttr>()) {
1014 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
1015 RD->getNameAsString();
1016
1017 return false;
1018 }
1019 }
1020
1021 return true;
1022}
1023
1024static void handleConsumesAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1025 if (!checkAttributeNumArgs(S, Attr, 0)) return;
1026
1027 if (!isa<CXXMethodDecl>(D)) {
1028 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1029 Attr.getName() << ExpectedMethod;
1030 return;
1031 }
1032
1033 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1034 return;
1035
1036 D->addAttr(::new (S.Context)
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001037 ConsumesAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001038 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001039}
1040
1041static void handleCallableWhenUnconsumedAttr(Sema &S, Decl *D,
1042 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001043 if (!checkAttributeNumArgs(S, Attr, 0)) return;
1044
1045 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001046 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1047 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001048 return;
1049 }
1050
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001051 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1052 return;
1053
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001054 D->addAttr(::new (S.Context)
1055 CallableWhenUnconsumedAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001056 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001057}
1058
1059static void handleTestsConsumedAttr(Sema &S, Decl *D,
1060 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001061 if (!checkAttributeNumArgs(S, Attr, 0)) return;
1062
1063 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001064 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1065 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001066 return;
1067 }
1068
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001069 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1070 return;
1071
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001072 D->addAttr(::new (S.Context)
1073 TestsConsumedAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001074 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001075}
1076
1077static void handleTestsUnconsumedAttr(Sema &S, Decl *D,
1078 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001079 if (!checkAttributeNumArgs(S, Attr, 0)) return;
1080
1081 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001082 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1083 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001084 return;
1085 }
1086
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001087 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1088 return;
1089
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001090 D->addAttr(::new (S.Context)
1091 TestsUnconsumedAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001092 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001093}
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00001094
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001095static void handleReturnTypestateAttr(Sema &S, Decl *D,
1096 const AttributeList &Attr) {
1097 if (!checkAttributeNumArgs(S, Attr, 1)) return;
1098
1099 ReturnTypestateAttr::ConsumedState ReturnState;
1100
1101 if (Attr.isArgIdent(0)) {
1102 StringRef Param = Attr.getArgAsIdent(0)->Ident->getName();
1103
1104 if (Param == "unknown") {
1105 ReturnState = ReturnTypestateAttr::Unknown;
1106 } else if (Param == "consumed") {
1107 ReturnState = ReturnTypestateAttr::Consumed;
1108 } else if (Param == "unconsumed") {
1109 ReturnState = ReturnTypestateAttr::Unconsumed;
1110 } else {
1111 S.Diag(Attr.getLoc(), diag::warn_unknown_consumed_state) << Param;
1112 return;
1113 }
1114
1115 } else {
1116 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1117 Attr.getName() << AANT_ArgumentIdentifier;
1118 return;
1119 }
1120
1121 if (!isa<FunctionDecl>(D)) {
1122 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1123 Attr.getName() << ExpectedFunction;
1124 return;
1125 }
1126
1127 // FIXME: This check is currently being done in the analysis. It can be
1128 // enabled here only after the parser propagates attributes at
1129 // template specialization definition, not declaration.
1130 //QualType ReturnType;
1131 //
1132 //if (const CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1133 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1134 //
1135 //} else {
1136 //
1137 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1138 //}
1139 //
1140 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1141 //
1142 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1143 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1144 // ReturnType.getAsString();
1145 // return;
1146 //}
1147
1148 D->addAttr(::new (S.Context)
1149 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1150 Attr.getAttributeSpellingListIndex()));
1151}
1152
Chandler Carruth1b03c872011-07-02 00:01:44 +00001153static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1154 const AttributeList &Attr) {
Richard Smitha4fa9002013-01-13 02:11:23 +00001155 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1156 if (TD == 0) {
1157 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1158 // and type-ids.
Chris Lattner803d0802008-06-29 00:43:07 +00001159 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +00001160 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001161 }
Mike Stumpbf916502009-07-24 19:02:52 +00001162
Richard Smitha4fa9002013-01-13 02:11:23 +00001163 // Remember this typedef decl, we will need it later for diagnostics.
1164 S.ExtVectorDecls.push_back(TD);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001165}
1166
Chandler Carruth1b03c872011-07-02 00:01:44 +00001167static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001168 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001169 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001170 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001171
Chandler Carruth87c44602011-07-01 23:49:12 +00001172 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001173 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +00001174 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001175 // If the alignment is less than or equal to 8 bits, the packed attribute
1176 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +00001177 if (!FD->getType()->isDependentType() &&
1178 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +00001179 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001180 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001181 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001182 else
Michael Han51d8c522013-01-24 16:46:58 +00001183 FD->addAttr(::new (S.Context)
1184 PackedAttr(Attr.getRange(), S.Context,
1185 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001186 } else
Chris Lattner3c73c412008-11-19 08:23:25 +00001187 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001188}
1189
Chandler Carruth1b03c872011-07-02 00:01:44 +00001190static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +00001191 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001192 RD->addAttr(::new (S.Context)
1193 MsStructAttr(Attr.getRange(), S.Context,
1194 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +00001195 else
1196 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1197}
1198
Chandler Carruth1b03c872011-07-02 00:01:44 +00001199static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +00001200 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001201 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +00001202 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001203
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001204 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +00001205 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001206 if (MD->isInstanceMethod()) {
Michael Han51d8c522013-01-24 16:46:58 +00001207 D->addAttr(::new (S.Context)
1208 IBActionAttr(Attr.getRange(), S.Context,
1209 Attr.getAttributeSpellingListIndex()));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001210 return;
1211 }
1212
Ted Kremenek4ee2bb12011-02-04 06:54:16 +00001213 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001214}
1215
Ted Kremenek2f041d02011-09-29 07:02:25 +00001216static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1217 // The IBOutlet/IBOutletCollection attributes only apply to instance
1218 // variables or properties of Objective-C classes. The outlet must also
1219 // have an object reference type.
1220 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1221 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001222 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001223 << Attr.getName() << VD->getType() << 0;
1224 return false;
1225 }
1226 }
1227 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1228 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001229 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001230 << Attr.getName() << PD->getType() << 1;
1231 return false;
1232 }
1233 }
1234 else {
1235 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1236 return false;
1237 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001238
Ted Kremenek2f041d02011-09-29 07:02:25 +00001239 return true;
1240}
1241
Chandler Carruth1b03c872011-07-02 00:01:44 +00001242static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001243 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001244 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001245 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001246
1247 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001248 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001249
Michael Han51d8c522013-01-24 16:46:58 +00001250 D->addAttr(::new (S.Context)
1251 IBOutletAttr(Attr.getRange(), S.Context,
1252 Attr.getAttributeSpellingListIndex()));
Ted Kremenek96329d42008-07-15 22:26:48 +00001253}
1254
Chandler Carruth1b03c872011-07-02 00:01:44 +00001255static void handleIBOutletCollection(Sema &S, Decl *D,
1256 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001257
1258 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00001259 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001260 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1261 << Attr.getName() << 1;
Ted Kremenek857e9182010-05-19 17:38:06 +00001262 return;
1263 }
1264
Ted Kremenek2f041d02011-09-29 07:02:25 +00001265 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001266 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001267
Aaron Ballman624421f2013-08-31 01:11:41 +00001268 IdentifierLoc *IL = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
1269 IdentifierInfo *II;
1270 SourceLocation ILS;
1271 if (IL) {
1272 II = IL->Ident;
1273 ILS = IL->Loc;
1274 } else {
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001275 II = &S.Context.Idents.get("NSObject");
Aaron Ballman624421f2013-08-31 01:11:41 +00001276 }
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001277
John McCallb3d87482010-08-24 05:47:05 +00001278 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001279 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001280 if (!TypeRep) {
1281 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1282 return;
1283 }
John McCallb3d87482010-08-24 05:47:05 +00001284 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001285 // Diagnose use of non-object type in iboutletcollection attribute.
1286 // FIXME. Gnu attribute extension ignores use of builtin types in
1287 // attributes. So, __attribute__((iboutletcollection(char))) will be
1288 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001289 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001290 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1291 return;
1292 }
Michael Han51d8c522013-01-24 16:46:58 +00001293 D->addAttr(::new (S.Context)
Aaron Ballman624421f2013-08-31 01:11:41 +00001294 IBOutletCollectionAttr(Attr.getRange(), S.Context, QT, ILS,
Michael Han51d8c522013-01-24 16:46:58 +00001295 Attr.getAttributeSpellingListIndex()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001296}
1297
Chandler Carruthd309c812011-07-01 23:49:16 +00001298static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001299 if (const RecordType *UT = T->getAsUnionType())
1300 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1301 RecordDecl *UD = UT->getDecl();
1302 for (RecordDecl::field_iterator it = UD->field_begin(),
1303 itend = UD->field_end(); it != itend; ++it) {
1304 QualType QT = it->getType();
1305 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1306 T = QT;
1307 return;
1308 }
1309 }
1310 }
1311}
1312
Nuno Lopes587de5b2012-05-24 00:22:00 +00001313static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001314 if (!isFunctionOrMethod(D)) {
1315 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001316 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopes174930d2012-06-18 16:39:04 +00001317 return;
1318 }
1319
Nuno Lopes587de5b2012-05-24 00:22:00 +00001320 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1321 return;
1322
Nuno Lopes587de5b2012-05-24 00:22:00 +00001323 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001324 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001325 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001326 uint64_t Idx;
1327 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1328 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes587de5b2012-05-24 00:22:00 +00001329 return;
Nuno Lopes587de5b2012-05-24 00:22:00 +00001330
1331 // check if the function argument is of an integer type
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001332 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001333 if (!T->isIntegerType()) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00001334 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1335 << Attr.getName() << AANT_ArgumentIntegerConstant
1336 << Ex->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001337 return;
1338 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001339 SizeArgs.push_back(Idx);
Nuno Lopes587de5b2012-05-24 00:22:00 +00001340 }
1341
1342 // check if the function returns a pointer
1343 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1344 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001345 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001346 }
1347
Michael Han51d8c522013-01-24 16:46:58 +00001348 D->addAttr(::new (S.Context)
1349 AllocSizeAttr(Attr.getRange(), S.Context,
1350 SizeArgs.data(), SizeArgs.size(),
1351 Attr.getAttributeSpellingListIndex()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001352}
1353
Chandler Carruth1b03c872011-07-02 00:01:44 +00001354static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001355 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1356 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001357 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001358 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001359 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001360 return;
1361 }
Mike Stumpbf916502009-07-24 19:02:52 +00001362
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001363 SmallVector<unsigned, 8> NonNullArgs;
1364 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001365 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001366 uint64_t Idx;
1367 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1368 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001369 return;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001370
1371 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001372 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001373 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001374
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001375 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001376 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001377 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001378 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001379 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001380 }
Mike Stumpbf916502009-07-24 19:02:52 +00001381
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001382 NonNullArgs.push_back(Idx);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001383 }
Mike Stumpbf916502009-07-24 19:02:52 +00001384
1385 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1386 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001387 if (NonNullArgs.empty()) {
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001388 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1389 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001390 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001391 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001392 NonNullArgs.push_back(i);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001393 }
Mike Stumpbf916502009-07-24 19:02:52 +00001394
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001395 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001396 if (NonNullArgs.empty()) {
1397 // Warn the trivial case only if attribute is not coming from a
1398 // macro instantiation.
1399 if (Attr.getLoc().isFileID())
1400 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001401 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001402 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001403 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001404
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001405 unsigned *start = &NonNullArgs[0];
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001406 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001407 llvm::array_pod_sort(start, start + size);
Michael Han51d8c522013-01-24 16:46:58 +00001408 D->addAttr(::new (S.Context)
1409 NonNullAttr(Attr.getRange(), S.Context, start, size,
1410 Attr.getAttributeSpellingListIndex()));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001411}
1412
Chandler Carruth1b03c872011-07-02 00:01:44 +00001413static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001414 // This attribute must be applied to a function declaration.
1415 // The first argument to the attribute must be a string,
1416 // the name of the resource, for example "malloc".
1417 // The following arguments must be argument indexes, the arguments must be
1418 // of integer type for Returns, otherwise of pointer type.
1419 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001420 // after being held. free() should be __attribute((ownership_takes)), whereas
1421 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001422
Aaron Ballman624421f2013-08-31 01:11:41 +00001423 if (!AL.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001424 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001425 << AL.getName()->getName() << 1 << AANT_ArgumentString;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001426 return;
1427 }
1428 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001429 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001430 switch (AL.getKind()) {
1431 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001432 K = OwnershipAttr::Takes;
Aaron Ballman624421f2013-08-31 01:11:41 +00001433 if (AL.getNumArgs() < 2) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001434 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1435 << AL.getName() << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001436 return;
1437 }
Jordy Rose2a479922010-08-12 08:54:03 +00001438 break;
1439 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001440 K = OwnershipAttr::Holds;
Aaron Ballman624421f2013-08-31 01:11:41 +00001441 if (AL.getNumArgs() < 2) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001442 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1443 << AL.getName() << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001444 return;
1445 }
Jordy Rose2a479922010-08-12 08:54:03 +00001446 break;
1447 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001448 K = OwnershipAttr::Returns;
Aaron Ballman624421f2013-08-31 01:11:41 +00001449 if (AL.getNumArgs() > 2) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001450 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
Aaron Ballman624421f2013-08-31 01:11:41 +00001451 << AL.getName() << AL.getNumArgs() + 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001452 return;
1453 }
Jordy Rose2a479922010-08-12 08:54:03 +00001454 break;
1455 default:
1456 // This should never happen given how we are called.
1457 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001458 }
1459
Chandler Carruth87c44602011-07-01 23:49:12 +00001460 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001461 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1462 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001463 return;
1464 }
1465
Aaron Ballman624421f2013-08-31 01:11:41 +00001466 StringRef Module = AL.getArgAsIdent(0)->Ident->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001467
1468 // Normalize the argument, __foo__ becomes foo.
1469 if (Module.startswith("__") && Module.endswith("__"))
1470 Module = Module.substr(2, Module.size() - 4);
1471
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001472
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001473 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman624421f2013-08-31 01:11:41 +00001474 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1475 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001476 uint64_t Idx;
1477 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
Aaron Ballman624421f2013-08-31 01:11:41 +00001478 AL.getLoc(), i, Ex, Idx))
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001479 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001480
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001481 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001482 case OwnershipAttr::Takes:
1483 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001484 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001485 QualType T = getFunctionOrMethodArgType(D, Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001486 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1487 // FIXME: Should also highlight argument in decl.
1488 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001489 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001490 << "pointer"
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001491 << Ex->getSourceRange();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001492 continue;
1493 }
1494 break;
1495 }
Sean Huntcf807c42010-08-18 23:23:40 +00001496 case OwnershipAttr::Returns: {
Aaron Ballman624421f2013-08-31 01:11:41 +00001497 if (AL.getNumArgs() > 2) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001498 // Is the function argument an integer type?
Aaron Ballman624421f2013-08-31 01:11:41 +00001499 Expr *IdxExpr = AL.getArgAsExpr(1);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001500 llvm::APSInt ArgNum(32);
1501 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1502 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1503 S.Diag(AL.getLoc(), diag::err_ownership_type)
1504 << "ownership_returns" << "integer"
1505 << IdxExpr->getSourceRange();
1506 return;
1507 }
1508 }
1509 break;
1510 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001511 } // switch
1512
1513 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001514 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001515 i = D->specific_attr_begin<OwnershipAttr>(),
1516 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001517 i != e; ++i) {
1518 if ((*i)->getOwnKind() != K) {
1519 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1520 I!=E; ++I) {
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001521 if (Idx == *I) {
Sean Huntcf807c42010-08-18 23:23:40 +00001522 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1523 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001524 }
1525 }
1526 }
1527 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001528 OwnershipArgs.push_back(Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001529 }
1530
1531 unsigned* start = OwnershipArgs.data();
1532 unsigned size = OwnershipArgs.size();
1533 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001534
1535 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001536 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1537 << AL.getName() << 2;
Sean Huntcf807c42010-08-18 23:23:40 +00001538 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001539 }
Sean Huntcf807c42010-08-18 23:23:40 +00001540
Michael Han51d8c522013-01-24 16:46:58 +00001541 D->addAttr(::new (S.Context)
1542 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1543 AL.getAttributeSpellingListIndex()));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001544}
1545
Chandler Carruth1b03c872011-07-02 00:01:44 +00001546static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001547 // Check the attribute arguments.
1548 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001549 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1550 << Attr.getName() << 1;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001551 return;
1552 }
1553
Chandler Carruth87c44602011-07-01 23:49:12 +00001554 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001555 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001556 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001557 return;
1558 }
1559
Chandler Carruth87c44602011-07-01 23:49:12 +00001560 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001561
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001562 // gcc rejects
1563 // class c {
1564 // static int a __attribute__((weakref ("v2")));
1565 // static int b() __attribute__((weakref ("f3")));
1566 // };
1567 // and ignores the attributes of
1568 // void f(void) {
1569 // static int a __attribute__((weakref ("v2")));
1570 // }
1571 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001572 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001573 if (!Ctx->isFileContext()) {
1574 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001575 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001576 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001577 }
1578
1579 // The GCC manual says
1580 //
1581 // At present, a declaration to which `weakref' is attached can only
1582 // be `static'.
1583 //
1584 // It also says
1585 //
1586 // Without a TARGET,
1587 // given as an argument to `weakref' or to `alias', `weakref' is
1588 // equivalent to `weak'.
1589 //
1590 // gcc 4.4.1 will accept
1591 // int a7 __attribute__((weakref));
1592 // as
1593 // int a7 __attribute__((weak));
1594 // This looks like a bug in gcc. We reject that for now. We should revisit
1595 // it if this behaviour is actually used.
1596
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001597 // GCC rejects
1598 // static ((alias ("y"), weakref)).
1599 // Should we? How to check that weakref is before or after alias?
1600
Aaron Ballman624421f2013-08-31 01:11:41 +00001601 if (Attr.isArgExpr(0)) {
1602 Expr *Arg = Attr.getArgAsExpr(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001603 Arg = Arg->IgnoreParenCasts();
1604 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1605
Douglas Gregor5cee1192011-07-27 05:40:30 +00001606 if (!Str || !Str->isAscii()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001607 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001608 << Attr.getName() << 1 << AANT_ArgumentString;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001609 return;
1610 }
1611 // GCC will accept anything as the argument of weakref. Should we
1612 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001613 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001614 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001615 }
1616
Michael Han51d8c522013-01-24 16:46:58 +00001617 D->addAttr(::new (S.Context)
1618 WeakRefAttr(Attr.getRange(), S.Context,
1619 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001620}
1621
Chandler Carruth1b03c872011-07-02 00:01:44 +00001622static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001623 // check the attribute arguments.
Aaron Ballmanffa9d572013-07-18 18:01:48 +00001624 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001625 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001626
Aaron Ballman624421f2013-08-31 01:11:41 +00001627 StringLiteral *Str = 0;
1628 if (Attr.isArgExpr(0))
1629 Str = dyn_cast<StringLiteral>(Attr.getArgAsExpr(0)->IgnoreParenCasts());
Mike Stumpbf916502009-07-24 19:02:52 +00001630
Douglas Gregor5cee1192011-07-27 05:40:30 +00001631 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001632 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1633 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001634 return;
1635 }
Mike Stumpbf916502009-07-24 19:02:52 +00001636
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001637 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001638 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1639 return;
1640 }
1641
Chris Lattner6b6b5372008-06-26 18:38:35 +00001642 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001643
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001644 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00001645 Str->getString(),
1646 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001647}
1648
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001649static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1650 // Check the attribute arguments.
1651 if (!checkAttributeNumArgs(S, Attr, 0))
1652 return;
1653
1654 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1655 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1656 << Attr.getName() << ExpectedFunctionOrMethod;
1657 return;
1658 }
1659
Michael Han51d8c522013-01-24 16:46:58 +00001660 D->addAttr(::new (S.Context)
1661 MinSizeAttr(Attr.getRange(), S.Context,
1662 Attr.getAttributeSpellingListIndex()));
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001663}
1664
Benjamin Krameree409a92012-05-12 21:10:52 +00001665static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1666 // Check the attribute arguments.
1667 if (!checkAttributeNumArgs(S, Attr, 0))
1668 return;
1669
1670 if (!isa<FunctionDecl>(D)) {
1671 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1672 << Attr.getName() << ExpectedFunction;
1673 return;
1674 }
1675
1676 if (D->hasAttr<HotAttr>()) {
1677 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1678 << Attr.getName() << "hot";
1679 return;
1680 }
1681
Michael Han51d8c522013-01-24 16:46:58 +00001682 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1683 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001684}
1685
1686static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1687 // Check the attribute arguments.
1688 if (!checkAttributeNumArgs(S, Attr, 0))
1689 return;
1690
1691 if (!isa<FunctionDecl>(D)) {
1692 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1693 << Attr.getName() << ExpectedFunction;
1694 return;
1695 }
1696
1697 if (D->hasAttr<ColdAttr>()) {
1698 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1699 << Attr.getName() << "cold";
1700 return;
1701 }
1702
Michael Han51d8c522013-01-24 16:46:58 +00001703 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1704 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001705}
1706
Chandler Carruth1b03c872011-07-02 00:01:44 +00001707static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001708 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001709 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001710 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001711
Chandler Carruth87c44602011-07-01 23:49:12 +00001712 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001713 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001714 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001715 return;
1716 }
1717
Michael Han51d8c522013-01-24 16:46:58 +00001718 D->addAttr(::new (S.Context)
1719 NakedAttr(Attr.getRange(), S.Context,
1720 Attr.getAttributeSpellingListIndex()));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001721}
1722
Chandler Carruth1b03c872011-07-02 00:01:44 +00001723static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1724 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001725 // Check the attribute arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00001726 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001727 return;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001728
Chandler Carruth87c44602011-07-01 23:49:12 +00001729 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001730 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001731 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001732 return;
1733 }
Mike Stumpbf916502009-07-24 19:02:52 +00001734
Michael Han51d8c522013-01-24 16:46:58 +00001735 D->addAttr(::new (S.Context)
1736 AlwaysInlineAttr(Attr.getRange(), S.Context,
1737 Attr.getAttributeSpellingListIndex()));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001738}
1739
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001740static void handleTLSModelAttr(Sema &S, Decl *D,
1741 const AttributeList &Attr) {
1742 // Check the attribute arguments.
Aaron Ballmanffa9d572013-07-18 18:01:48 +00001743 if (!checkAttributeNumArgs(S, Attr, 1))
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001744 return;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001745
Aaron Ballman624421f2013-08-31 01:11:41 +00001746 Expr *Arg = Attr.getArgAsExpr(0);
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001747 Arg = Arg->IgnoreParenCasts();
1748 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1749
1750 // Check that it is a string.
1751 if (!Str) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001752 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1753 << Attr.getName() << AANT_ArgumentString;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001754 return;
1755 }
1756
Richard Smith38afbc72013-04-13 02:43:54 +00001757 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001758 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1759 << Attr.getName() << ExpectedTLSVar;
1760 return;
1761 }
1762
1763 // Check that the value.
1764 StringRef Model = Str->getString();
1765 if (Model != "global-dynamic" && Model != "local-dynamic"
1766 && Model != "initial-exec" && Model != "local-exec") {
1767 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1768 return;
1769 }
1770
Michael Han51d8c522013-01-24 16:46:58 +00001771 D->addAttr(::new (S.Context)
1772 TLSModelAttr(Attr.getRange(), S.Context, Model,
1773 Attr.getAttributeSpellingListIndex()));
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001774}
1775
Chandler Carruth1b03c872011-07-02 00:01:44 +00001776static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001777 // Check the attribute arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00001778 if (!checkAttributeNumArgs(S, Attr, 0))
Ryan Flynn76168e22009-08-09 20:07:29 +00001779 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001780
Chandler Carruth87c44602011-07-01 23:49:12 +00001781 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001782 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001783 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han51d8c522013-01-24 16:46:58 +00001784 D->addAttr(::new (S.Context)
1785 MallocAttr(Attr.getRange(), S.Context,
1786 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001787 return;
1788 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001789 }
1790
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001791 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001792}
1793
Chandler Carruth1b03c872011-07-02 00:01:44 +00001794static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001795 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001796 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001797 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001798
Michael Han51d8c522013-01-24 16:46:58 +00001799 D->addAttr(::new (S.Context)
1800 MayAliasAttr(Attr.getRange(), S.Context,
1801 Attr.getAttributeSpellingListIndex()));
Dan Gohman34c26302010-11-17 00:03:07 +00001802}
1803
Chandler Carruth1b03c872011-07-02 00:01:44 +00001804static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001805 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001806 D->addAttr(::new (S.Context)
1807 NoCommonAttr(Attr.getRange(), S.Context,
1808 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001809 else
1810 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001811 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001812}
1813
Chandler Carruth1b03c872011-07-02 00:01:44 +00001814static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman3e1aca22013-06-20 22:55:04 +00001815 if (S.LangOpts.CPlusPlus) {
1816 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1817 return;
1818 }
1819
Chandler Carruth87c44602011-07-01 23:49:12 +00001820 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001821 D->addAttr(::new (S.Context)
1822 CommonAttr(Attr.getRange(), S.Context,
1823 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001824 else
1825 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001826 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001827}
1828
Chandler Carruth1b03c872011-07-02 00:01:44 +00001829static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001830 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001831
1832 if (S.CheckNoReturnAttr(attr)) return;
1833
Chandler Carruth87c44602011-07-01 23:49:12 +00001834 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001835 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001836 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001837 return;
1838 }
1839
Michael Han51d8c522013-01-24 16:46:58 +00001840 D->addAttr(::new (S.Context)
1841 NoReturnAttr(attr.getRange(), S.Context,
1842 attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00001843}
1844
1845bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001846 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall711c52b2011-01-05 12:14:39 +00001847 attr.setInvalid();
1848 return true;
1849 }
1850
1851 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001852}
1853
Chandler Carruth1b03c872011-07-02 00:01:44 +00001854static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1855 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001856
1857 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1858 // because 'analyzer_noreturn' does not impact the type.
1859
Chandler Carruth1731e202011-07-11 23:30:35 +00001860 if(!checkAttributeNumArgs(S, Attr, 0))
1861 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001862
Chandler Carruth87c44602011-07-01 23:49:12 +00001863 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1864 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001865 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1866 && !VD->getType()->isFunctionPointerType())) {
1867 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001868 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001869 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001870 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001871 return;
1872 }
1873 }
1874
Michael Han51d8c522013-01-24 16:46:58 +00001875 D->addAttr(::new (S.Context)
1876 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1877 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001878}
1879
Richard Smithcd8ab512013-01-17 01:30:42 +00001880static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1881 const AttributeList &Attr) {
1882 // C++11 [dcl.attr.noreturn]p1:
1883 // The attribute may be applied to the declarator-id in a function
1884 // declaration.
1885 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1886 if (!FD) {
1887 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1888 << Attr.getName() << ExpectedFunctionOrMethod;
1889 return;
1890 }
1891
Michael Han51d8c522013-01-24 16:46:58 +00001892 D->addAttr(::new (S.Context)
1893 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1894 Attr.getAttributeSpellingListIndex()));
Richard Smithcd8ab512013-01-17 01:30:42 +00001895}
1896
John Thompson35cc9622010-08-09 21:53:52 +00001897// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001898static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001899/*
1900 Returning a Vector Class in Registers
1901
Eric Christopherf48f3672010-12-01 22:13:54 +00001902 According to the PPU ABI specifications, a class with a single member of
1903 vector type is returned in memory when used as the return value of a function.
1904 This results in inefficient code when implementing vector classes. To return
1905 the value in a single vector register, add the vecreturn attribute to the
1906 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001907
1908 Example:
1909
1910 struct Vector
1911 {
1912 __vector float xyzw;
1913 } __attribute__((vecreturn));
1914
1915 Vector Add(Vector lhs, Vector rhs)
1916 {
1917 Vector result;
1918 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1919 return result; // This will be returned in a register
1920 }
1921*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001922 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001923 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001924 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001925 return;
1926 }
1927
Chandler Carruth87c44602011-07-01 23:49:12 +00001928 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001929 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1930 return;
1931 }
1932
Chandler Carruth87c44602011-07-01 23:49:12 +00001933 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001934 int count = 0;
1935
1936 if (!isa<CXXRecordDecl>(record)) {
1937 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1938 return;
1939 }
1940
1941 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1942 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1943 return;
1944 }
1945
Eric Christopherf48f3672010-12-01 22:13:54 +00001946 for (RecordDecl::field_iterator iter = record->field_begin();
1947 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001948 if ((count == 1) || !iter->getType()->isVectorType()) {
1949 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1950 return;
1951 }
1952 count++;
1953 }
1954
Michael Han51d8c522013-01-24 16:46:58 +00001955 D->addAttr(::new (S.Context)
1956 VecReturnAttr(Attr.getRange(), S.Context,
1957 Attr.getAttributeSpellingListIndex()));
John Thompson35cc9622010-08-09 21:53:52 +00001958}
1959
Richard Smith3a2b7a12013-01-28 22:42:45 +00001960static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1961 const AttributeList &Attr) {
1962 if (isa<ParmVarDecl>(D)) {
1963 // [[carries_dependency]] can only be applied to a parameter if it is a
1964 // parameter of a function declaration or lambda.
1965 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1966 S.Diag(Attr.getLoc(),
1967 diag::err_carries_dependency_param_not_function_decl);
1968 return;
1969 }
1970 } else if (!isa<FunctionDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001971 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001972 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001973 return;
1974 }
Richard Smith3a2b7a12013-01-28 22:42:45 +00001975
1976 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1977 Attr.getRange(), S.Context,
1978 Attr.getAttributeSpellingListIndex()));
Sean Huntbbd37c62009-11-21 08:43:09 +00001979}
1980
Chandler Carruth1b03c872011-07-02 00:01:44 +00001981static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001982 // check the attribute arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00001983 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek73798892008-07-25 04:39:19 +00001984 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001985
Chandler Carruth87c44602011-07-01 23:49:12 +00001986 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001987 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001988 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001989 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001990 return;
1991 }
Mike Stumpbf916502009-07-24 19:02:52 +00001992
Michael Han51d8c522013-01-24 16:46:58 +00001993 D->addAttr(::new (S.Context)
1994 UnusedAttr(Attr.getRange(), S.Context,
1995 Attr.getAttributeSpellingListIndex()));
Ted Kremenek73798892008-07-25 04:39:19 +00001996}
1997
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001998static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1999 const AttributeList &Attr) {
2000 // check the attribute arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00002001 if (!checkAttributeNumArgs(S, Attr, 0))
Rafael Espindolaf87cced2011-10-03 14:59:42 +00002002 return;
Rafael Espindolaf87cced2011-10-03 14:59:42 +00002003
2004 if (!isa<FunctionDecl>(D)) {
2005 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2006 << Attr.getName() << ExpectedFunction;
2007 return;
2008 }
2009
Michael Han51d8c522013-01-24 16:46:58 +00002010 D->addAttr(::new (S.Context)
2011 ReturnsTwiceAttr(Attr.getRange(), S.Context,
2012 Attr.getAttributeSpellingListIndex()));
Rafael Espindolaf87cced2011-10-03 14:59:42 +00002013}
2014
Chandler Carruth1b03c872011-07-02 00:01:44 +00002015static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00002016 // check the attribute arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00002017 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbarb805dad2009-02-13 19:23:53 +00002018 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002019
Chandler Carruth87c44602011-07-01 23:49:12 +00002020 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola29535ba2013-08-16 23:18:50 +00002021 if (VD->hasLocalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00002022 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
2023 return;
2024 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002025 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00002026 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002027 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00002028 return;
2029 }
Mike Stumpbf916502009-07-24 19:02:52 +00002030
Michael Han51d8c522013-01-24 16:46:58 +00002031 D->addAttr(::new (S.Context)
2032 UsedAttr(Attr.getRange(), S.Context,
2033 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00002034}
2035
Chandler Carruth1b03c872011-07-02 00:01:44 +00002036static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002037 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00002038 if (Attr.getNumArgs() > 1) {
2039 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002040 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002041 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002042
2043 int priority = 65535; // FIXME: Do not hardcode such constants.
2044 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002045 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002046 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002047 if (E->isTypeDependent() || E->isValueDependent() ||
2048 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002049 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002050 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002051 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002052 return;
2053 }
2054 priority = Idx.getZExtValue();
2055 }
Mike Stumpbf916502009-07-24 19:02:52 +00002056
Chandler Carruth87c44602011-07-01 23:49:12 +00002057 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002058 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002059 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002060 return;
2061 }
2062
Michael Han51d8c522013-01-24 16:46:58 +00002063 D->addAttr(::new (S.Context)
2064 ConstructorAttr(Attr.getRange(), S.Context, priority,
2065 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002066}
2067
Chandler Carruth1b03c872011-07-02 00:01:44 +00002068static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002069 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00002070 if (Attr.getNumArgs() > 1) {
2071 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002072 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002073 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002074
2075 int priority = 65535; // FIXME: Do not hardcode such constants.
2076 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002077 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002078 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002079 if (E->isTypeDependent() || E->isValueDependent() ||
2080 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002081 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002082 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002083 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002084 return;
2085 }
2086 priority = Idx.getZExtValue();
2087 }
Mike Stumpbf916502009-07-24 19:02:52 +00002088
Chandler Carruth87c44602011-07-01 23:49:12 +00002089 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002090 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002091 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002092 return;
2093 }
2094
Michael Han51d8c522013-01-24 16:46:58 +00002095 D->addAttr(::new (S.Context)
2096 DestructorAttr(Attr.getRange(), S.Context, priority,
2097 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00002098}
2099
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002100template <typename AttrTy>
Aaron Ballman2dbdef22013-07-18 13:13:52 +00002101static void handleAttrWithMessage(Sema &S, Decl *D,
2102 const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00002103 unsigned NumArgs = Attr.getNumArgs();
2104 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00002105 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002106 return;
2107 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00002108
2109 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002110 StringRef Str;
Aaron Ballman624421f2013-08-31 01:11:41 +00002111 if (Attr.isArgExpr(0)) {
2112 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArgAsExpr(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002113 if (!SE) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002114 S.Diag(Attr.getArgAsExpr(0)->getLocStart(),
2115 diag::err_attribute_argument_type) << Attr.getName()
2116 << AANT_ArgumentString;
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002117 return;
2118 }
Chris Lattner951bbb22011-02-24 05:42:24 +00002119 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002120 }
Mike Stumpbf916502009-07-24 19:02:52 +00002121
Michael Han51d8c522013-01-24 16:46:58 +00002122 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2123 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00002124}
2125
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002126static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2127 const AttributeList &Attr) {
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002128 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002129 return;
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002130
Michael Han51d8c522013-01-24 16:46:58 +00002131 D->addAttr(::new (S.Context)
2132 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2133 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002134}
2135
Patrick Beardb2f68202012-04-06 18:12:22 +00002136static void handleObjCRootClassAttr(Sema &S, Decl *D,
2137 const AttributeList &Attr) {
2138 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002139 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2140 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardb2f68202012-04-06 18:12:22 +00002141 return;
2142 }
2143
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002144 if (!checkAttributeNumArgs(S, Attr, 0))
Patrick Beardb2f68202012-04-06 18:12:22 +00002145 return;
Patrick Beardb2f68202012-04-06 18:12:22 +00002146
Michael Han51d8c522013-01-24 16:46:58 +00002147 D->addAttr(::new (S.Context)
2148 ObjCRootClassAttr(Attr.getRange(), S.Context,
2149 Attr.getAttributeSpellingListIndex()));
Patrick Beardb2f68202012-04-06 18:12:22 +00002150}
2151
Michael Han51d8c522013-01-24 16:46:58 +00002152static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2153 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00002154 if (!isa<ObjCInterfaceDecl>(D)) {
2155 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2156 return;
2157 }
2158
Aaron Ballmanfaf71a82013-07-23 15:16:00 +00002159 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002160 return;
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002161
Michael Han51d8c522013-01-24 16:46:58 +00002162 D->addAttr(::new (S.Context)
2163 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2164 Attr.getAttributeSpellingListIndex()));
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002165}
2166
Jordy Rosefad5de92012-05-08 03:27:22 +00002167static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2168 IdentifierInfo *Platform,
2169 VersionTuple Introduced,
2170 VersionTuple Deprecated,
2171 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002172 StringRef PlatformName
2173 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2174 if (PlatformName.empty())
2175 PlatformName = Platform->getName();
2176
2177 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2178 // of these steps are needed).
2179 if (!Introduced.empty() && !Deprecated.empty() &&
2180 !(Introduced <= Deprecated)) {
2181 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2182 << 1 << PlatformName << Deprecated.getAsString()
2183 << 0 << Introduced.getAsString();
2184 return true;
2185 }
2186
2187 if (!Introduced.empty() && !Obsoleted.empty() &&
2188 !(Introduced <= Obsoleted)) {
2189 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2190 << 2 << PlatformName << Obsoleted.getAsString()
2191 << 0 << Introduced.getAsString();
2192 return true;
2193 }
2194
2195 if (!Deprecated.empty() && !Obsoleted.empty() &&
2196 !(Deprecated <= Obsoleted)) {
2197 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2198 << 2 << PlatformName << Obsoleted.getAsString()
2199 << 1 << Deprecated.getAsString();
2200 return true;
2201 }
2202
2203 return false;
2204}
2205
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002206/// \brief Check whether the two versions match.
2207///
2208/// If either version tuple is empty, then they are assumed to match. If
2209/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2210static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2211 bool BeforeIsOkay) {
2212 if (X.empty() || Y.empty())
2213 return true;
2214
2215 if (X == Y)
2216 return true;
2217
2218 if (BeforeIsOkay && X < Y)
2219 return true;
2220
2221 return false;
2222}
2223
Rafael Espindola51be6e32013-01-08 22:04:34 +00002224AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002225 IdentifierInfo *Platform,
2226 VersionTuple Introduced,
2227 VersionTuple Deprecated,
2228 VersionTuple Obsoleted,
2229 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002230 StringRef Message,
Michael Han51d8c522013-01-24 16:46:58 +00002231 bool Override,
2232 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002233 VersionTuple MergedIntroduced = Introduced;
2234 VersionTuple MergedDeprecated = Deprecated;
2235 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002236 bool FoundAny = false;
2237
Rafael Espindola98ae8342012-05-10 02:50:16 +00002238 if (D->hasAttrs()) {
2239 AttrVec &Attrs = D->getAttrs();
2240 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2241 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2242 if (!OldAA) {
2243 ++i;
2244 continue;
2245 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002246
Rafael Espindola98ae8342012-05-10 02:50:16 +00002247 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2248 if (OldPlatform != Platform) {
2249 ++i;
2250 continue;
2251 }
2252
2253 FoundAny = true;
2254 VersionTuple OldIntroduced = OldAA->getIntroduced();
2255 VersionTuple OldDeprecated = OldAA->getDeprecated();
2256 VersionTuple OldObsoleted = OldAA->getObsoleted();
2257 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002258
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002259 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2260 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2261 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2262 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002263 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002264 if (Override) {
2265 int Which = -1;
2266 VersionTuple FirstVersion;
2267 VersionTuple SecondVersion;
2268 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2269 Which = 0;
2270 FirstVersion = OldIntroduced;
2271 SecondVersion = Introduced;
2272 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2273 Which = 1;
2274 FirstVersion = Deprecated;
2275 SecondVersion = OldDeprecated;
2276 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2277 Which = 2;
2278 FirstVersion = Obsoleted;
2279 SecondVersion = OldObsoleted;
2280 }
2281
2282 if (Which == -1) {
2283 Diag(OldAA->getLocation(),
2284 diag::warn_mismatched_availability_override_unavail)
2285 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2286 } else {
2287 Diag(OldAA->getLocation(),
2288 diag::warn_mismatched_availability_override)
2289 << Which
2290 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2291 << FirstVersion.getAsString() << SecondVersion.getAsString();
2292 }
2293 Diag(Range.getBegin(), diag::note_overridden_method);
2294 } else {
2295 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2296 Diag(Range.getBegin(), diag::note_previous_attribute);
2297 }
2298
Rafael Espindola98ae8342012-05-10 02:50:16 +00002299 Attrs.erase(Attrs.begin() + i);
2300 --e;
2301 continue;
2302 }
2303
2304 VersionTuple MergedIntroduced2 = MergedIntroduced;
2305 VersionTuple MergedDeprecated2 = MergedDeprecated;
2306 VersionTuple MergedObsoleted2 = MergedObsoleted;
2307
2308 if (MergedIntroduced2.empty())
2309 MergedIntroduced2 = OldIntroduced;
2310 if (MergedDeprecated2.empty())
2311 MergedDeprecated2 = OldDeprecated;
2312 if (MergedObsoleted2.empty())
2313 MergedObsoleted2 = OldObsoleted;
2314
2315 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2316 MergedIntroduced2, MergedDeprecated2,
2317 MergedObsoleted2)) {
2318 Attrs.erase(Attrs.begin() + i);
2319 --e;
2320 continue;
2321 }
2322
2323 MergedIntroduced = MergedIntroduced2;
2324 MergedDeprecated = MergedDeprecated2;
2325 MergedObsoleted = MergedObsoleted2;
2326 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002327 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002328 }
2329
2330 if (FoundAny &&
2331 MergedIntroduced == Introduced &&
2332 MergedDeprecated == Deprecated &&
2333 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002334 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002335
Ted Kremenekcb344392013-04-06 00:34:27 +00002336 // Only create a new attribute if !Override, but we want to do
2337 // the checking.
Rafael Espindola98ae8342012-05-10 02:50:16 +00002338 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekcb344392013-04-06 00:34:27 +00002339 MergedDeprecated, MergedObsoleted) &&
2340 !Override) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002341 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2342 Introduced, Deprecated,
Michael Han51d8c522013-01-24 16:46:58 +00002343 Obsoleted, IsUnavailable, Message,
2344 AttrSpellingListIndex);
Rafael Espindola3b294362012-05-06 19:56:25 +00002345 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002346 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002347}
2348
Chandler Carruth1b03c872011-07-02 00:01:44 +00002349static void handleAvailabilityAttr(Sema &S, Decl *D,
2350 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002351 if (!checkAttributeNumArgs(S, Attr, 1))
2352 return;
2353 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han51d8c522013-01-24 16:46:58 +00002354 unsigned Index = Attr.getAttributeSpellingListIndex();
2355
Aaron Ballman624421f2013-08-31 01:11:41 +00002356 IdentifierInfo *II = Platform->Ident;
2357 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2358 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2359 << Platform->Ident;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002360
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002361 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2362 if (!ND) {
2363 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2364 return;
2365 }
2366
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002367 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2368 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2369 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002370 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002371 StringRef Str;
2372 const StringLiteral *SE =
2373 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2374 if (SE)
2375 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002376
Aaron Ballman624421f2013-08-31 01:11:41 +00002377 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002378 Introduced.Version,
2379 Deprecated.Version,
2380 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002381 IsUnavailable, Str,
Michael Han51d8c522013-01-24 16:46:58 +00002382 /*Override=*/false,
2383 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002384 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002385 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002386}
2387
John McCalld4c3d662013-02-20 01:54:26 +00002388template <class T>
2389static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2390 typename T::VisibilityType value,
2391 unsigned attrSpellingListIndex) {
2392 T *existingAttr = D->getAttr<T>();
2393 if (existingAttr) {
2394 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2395 if (existingValue == value)
2396 return NULL;
2397 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2398 S.Diag(range.getBegin(), diag::note_previous_attribute);
2399 D->dropAttr<T>();
2400 }
2401 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2402}
2403
Rafael Espindola599f1b72012-05-13 03:25:18 +00002404VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002405 VisibilityAttr::VisibilityType Vis,
2406 unsigned AttrSpellingListIndex) {
John McCalld4c3d662013-02-20 01:54:26 +00002407 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2408 AttrSpellingListIndex);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002409}
2410
John McCalld4c3d662013-02-20 01:54:26 +00002411TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2412 TypeVisibilityAttr::VisibilityType Vis,
2413 unsigned AttrSpellingListIndex) {
2414 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2415 AttrSpellingListIndex);
2416}
2417
2418static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2419 bool isTypeVisibility) {
2420 // Visibility attributes don't mean anything on a typedef.
2421 if (isa<TypedefNameDecl>(D)) {
2422 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2423 << Attr.getName();
2424 return;
2425 }
2426
2427 // 'type_visibility' can only go on a type or namespace.
2428 if (isTypeVisibility &&
2429 !(isa<TagDecl>(D) ||
2430 isa<ObjCInterfaceDecl>(D) ||
2431 isa<NamespaceDecl>(D))) {
2432 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2433 << Attr.getName() << ExpectedTypeOrNamespace;
2434 return;
2435 }
2436
Chris Lattner6b6b5372008-06-26 18:38:35 +00002437 // check the attribute arguments.
John McCalld4c3d662013-02-20 01:54:26 +00002438 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002439 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002440
Aaron Ballman624421f2013-08-31 01:11:41 +00002441 Expr *Arg = Attr.getArgAsExpr(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002442 Arg = Arg->IgnoreParenCasts();
2443 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00002444
Douglas Gregor5cee1192011-07-27 05:40:30 +00002445 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002446 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2447 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002448 return;
2449 }
Mike Stumpbf916502009-07-24 19:02:52 +00002450
Chris Lattner5f9e2722011-07-23 10:55:15 +00002451 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002452 VisibilityAttr::VisibilityType type;
Michael Han51d8c522013-01-24 16:46:58 +00002453
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002454 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00002455 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002456 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00002457 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00002458 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00002459 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00002460 else if (TypeStr == "protected") {
2461 // Complain about attempts to use protected visibility on targets
2462 // (like Darwin) that don't support it.
2463 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
2464 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2465 type = VisibilityAttr::Default;
2466 } else {
2467 type = VisibilityAttr::Protected;
2468 }
2469 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00002470 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002471 return;
2472 }
Mike Stumpbf916502009-07-24 19:02:52 +00002473
Michael Han51d8c522013-01-24 16:46:58 +00002474 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld4c3d662013-02-20 01:54:26 +00002475 clang::Attr *newAttr;
2476 if (isTypeVisibility) {
2477 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2478 (TypeVisibilityAttr::VisibilityType) type,
2479 Index);
2480 } else {
2481 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2482 }
2483 if (newAttr)
2484 D->addAttr(newAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002485}
2486
Chandler Carruth1b03c872011-07-02 00:01:44 +00002487static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2488 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002489 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2490 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002491 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002492 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002493 return;
2494 }
2495
Aaron Ballman624421f2013-08-31 01:11:41 +00002496 if (!Attr.isArgIdent(0)) {
2497 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2498 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCalld5313b02011-03-02 11:33:24 +00002499 return;
2500 }
Aaron Ballman624421f2013-08-31 01:11:41 +00002501
2502 if (!checkAttributeNumArgs(S, Attr, 1))
2503 return;
John McCalld5313b02011-03-02 11:33:24 +00002504
Aaron Ballman624421f2013-08-31 01:11:41 +00002505 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2506
2507 StringRef param = IL->Ident->getName();
John McCalld5313b02011-03-02 11:33:24 +00002508 ObjCMethodFamilyAttr::FamilyKind family;
2509 if (param == "none")
2510 family = ObjCMethodFamilyAttr::OMF_None;
2511 else if (param == "alloc")
2512 family = ObjCMethodFamilyAttr::OMF_alloc;
2513 else if (param == "copy")
2514 family = ObjCMethodFamilyAttr::OMF_copy;
2515 else if (param == "init")
2516 family = ObjCMethodFamilyAttr::OMF_init;
2517 else if (param == "mutableCopy")
2518 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
2519 else if (param == "new")
2520 family = ObjCMethodFamilyAttr::OMF_new;
2521 else {
2522 // Just warn and ignore it. This is future-proof against new
2523 // families being used in system headers.
Aaron Ballman624421f2013-08-31 01:11:41 +00002524 S.Diag(IL->Loc, diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00002525 return;
2526 }
2527
John McCallf85e1932011-06-15 23:02:42 +00002528 if (family == ObjCMethodFamilyAttr::OMF_init &&
2529 !method->getResultType()->isObjCObjectPointerType()) {
2530 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2531 << method->getResultType();
2532 // Ignore the attribute.
2533 return;
2534 }
2535
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002536 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00002537 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00002538}
2539
Chandler Carruth1b03c872011-07-02 00:01:44 +00002540static void handleObjCExceptionAttr(Sema &S, Decl *D,
2541 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002542 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00002543 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002544
Chris Lattner0db29ec2009-02-14 08:09:34 +00002545 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2546 if (OCI == 0) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002547 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2548 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002549 return;
2550 }
Mike Stumpbf916502009-07-24 19:02:52 +00002551
Michael Han51d8c522013-01-24 16:46:58 +00002552 D->addAttr(::new (S.Context)
2553 ObjCExceptionAttr(Attr.getRange(), S.Context,
2554 Attr.getAttributeSpellingListIndex()));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002555}
2556
Chandler Carruth1b03c872011-07-02 00:01:44 +00002557static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman081c8832013-07-23 12:13:14 +00002558 if (!checkAttributeNumArgs(S, Attr, 0))
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002559 return;
Richard Smith162e1c12011-04-15 14:24:37 +00002560 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002561 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002562 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002563 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2564 return;
2565 }
2566 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002567 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2568 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002569 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002570 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2571 return;
2572 }
2573 }
2574 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002575 // It is okay to include this attribute on properties, e.g.:
2576 //
2577 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2578 //
2579 // In this case it follows tradition and suppresses an error in the above
2580 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002581 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002582 }
Michael Han51d8c522013-01-24 16:46:58 +00002583 D->addAttr(::new (S.Context)
2584 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2585 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002586}
2587
Mike Stumpbf916502009-07-24 19:02:52 +00002588static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002589handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman081c8832013-07-23 12:13:14 +00002590 if (!checkAttributeNumArgs(S, Attr, 0))
Douglas Gregorf9201e02009-02-11 23:02:49 +00002591 return;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002592
2593 if (!isa<FunctionDecl>(D)) {
2594 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2595 return;
2596 }
2597
Michael Han51d8c522013-01-24 16:46:58 +00002598 D->addAttr(::new (S.Context)
2599 OverloadableAttr(Attr.getRange(), S.Context,
2600 Attr.getAttributeSpellingListIndex()));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002601}
2602
Chandler Carruth1b03c872011-07-02 00:01:44 +00002603static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002604 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002605 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00002606 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff9eae5762008-09-18 16:44:58 +00002607 return;
2608 }
Aaron Ballman624421f2013-08-31 01:11:41 +00002609
2610 if (!checkAttributeNumArgs(S, Attr, 1))
Steve Naroff9eae5762008-09-18 16:44:58 +00002611 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002612
Aaron Ballman624421f2013-08-31 01:11:41 +00002613 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Sean Huntcf807c42010-08-18 23:23:40 +00002614 BlocksAttr::BlockType type;
Aaron Ballman624421f2013-08-31 01:11:41 +00002615 if (II->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00002616 type = BlocksAttr::ByRef;
2617 else {
Aaron Ballman624421f2013-08-31 01:11:41 +00002618 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) << "blocks"
2619 << II;
Steve Naroff9eae5762008-09-18 16:44:58 +00002620 return;
2621 }
Mike Stumpbf916502009-07-24 19:02:52 +00002622
Michael Han51d8c522013-01-24 16:46:58 +00002623 D->addAttr(::new (S.Context)
2624 BlocksAttr(Attr.getRange(), S.Context, type,
2625 Attr.getAttributeSpellingListIndex()));
Steve Naroff9eae5762008-09-18 16:44:58 +00002626}
2627
Chandler Carruth1b03c872011-07-02 00:01:44 +00002628static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002629 // check the attribute arguments.
2630 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002631 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002632 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002633 }
2634
John McCall3323fad2011-09-09 07:56:05 +00002635 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002636 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002637 Expr *E = Attr.getArgAsExpr(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002638 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002639 if (E->isTypeDependent() || E->isValueDependent() ||
2640 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002641 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002642 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002643 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002644 return;
2645 }
Mike Stumpbf916502009-07-24 19:02:52 +00002646
John McCall3323fad2011-09-09 07:56:05 +00002647 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002648 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2649 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002650 return;
2651 }
John McCall3323fad2011-09-09 07:56:05 +00002652
2653 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002654 }
2655
John McCall3323fad2011-09-09 07:56:05 +00002656 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002657 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002658 Expr *E = Attr.getArgAsExpr(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002659 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002660 if (E->isTypeDependent() || E->isValueDependent() ||
2661 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002662 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002663 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002664 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002665 return;
2666 }
2667 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002668
John McCall3323fad2011-09-09 07:56:05 +00002669 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002670 // FIXME: This error message could be improved, it would be nice
2671 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002672 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2673 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002674 return;
2675 }
2676 }
2677
Chandler Carruth87c44602011-07-01 23:49:12 +00002678 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002679 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002680 if (isa<FunctionNoProtoType>(FT)) {
2681 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2682 return;
2683 }
Mike Stumpbf916502009-07-24 19:02:52 +00002684
Chris Lattner897cd902009-03-17 23:03:47 +00002685 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002686 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002687 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002688 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002689 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002690 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002691 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002692 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002693 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002694 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2695 if (!BD->isVariadic()) {
2696 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2697 return;
2698 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002699 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002700 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002701 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002702 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002703 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002704 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002705 int m = Ty->isFunctionPointerType() ? 0 : 1;
2706 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002707 return;
2708 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002709 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002710 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002711 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002712 return;
2713 }
Anders Carlsson77091822008-10-05 18:05:59 +00002714 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002715 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002716 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002717 return;
2718 }
Michael Han51d8c522013-01-24 16:46:58 +00002719 D->addAttr(::new (S.Context)
2720 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2721 Attr.getAttributeSpellingListIndex()));
Anders Carlsson77091822008-10-05 18:05:59 +00002722}
2723
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002724static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2725 // Check the attribute arguments.
2726 if (!checkAttributeNumArgs(S, Attr, 0))
2727 return;
2728
2729 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2730 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2731 else
2732 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2733}
2734
Chandler Carruth1b03c872011-07-02 00:01:44 +00002735static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00002736 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002737 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00002738 return;
Chris Lattner026dc962009-02-14 07:37:35 +00002739
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002740 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002741 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002742 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002743 return;
2744 }
Mike Stumpbf916502009-07-24 19:02:52 +00002745
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002746 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2747 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2748 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002749 return;
2750 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002751 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2752 if (MD->getResultType()->isVoidType()) {
2753 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2754 << Attr.getName() << 1;
2755 return;
2756 }
2757
Michael Han51d8c522013-01-24 16:46:58 +00002758 D->addAttr(::new (S.Context)
2759 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2760 Attr.getAttributeSpellingListIndex()));
Chris Lattner026dc962009-02-14 07:37:35 +00002761}
2762
Chandler Carruth1b03c872011-07-02 00:01:44 +00002763static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002764 // check the attribute arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00002765 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002766 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002767
Chandler Carruth87c44602011-07-01 23:49:12 +00002768 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002769 if (isa<CXXRecordDecl>(D)) {
2770 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2771 return;
2772 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002773 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2774 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002775 return;
2776 }
2777
Chandler Carruth87c44602011-07-01 23:49:12 +00002778 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002779
Michael Han51d8c522013-01-24 16:46:58 +00002780 nd->addAttr(::new (S.Context)
2781 WeakAttr(Attr.getRange(), S.Context,
2782 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002783}
2784
Chandler Carruth1b03c872011-07-02 00:01:44 +00002785static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002786 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002787 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002788 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002789
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002790
2791 // weak_import only applies to variable & function declarations.
2792 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002793 if (!D->canBeWeakImported(isDef)) {
2794 if (isDef)
Reid Klecknerbeba3e82013-05-20 21:53:29 +00002795 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2796 << "weak_import";
Douglas Gregordef86312011-03-23 13:27:51 +00002797 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002798 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002799 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002800 // Nothing to warn about here.
2801 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002802 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002803 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002804
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002805 return;
2806 }
2807
Michael Han51d8c522013-01-24 16:46:58 +00002808 D->addAttr(::new (S.Context)
2809 WeakImportAttr(Attr.getRange(), S.Context,
2810 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002811}
2812
Tanya Lattner0df579e2012-07-09 22:06:01 +00002813// Handles reqd_work_group_size and work_group_size_hint.
2814static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002815 const AttributeList &Attr) {
Tanya Lattner0df579e2012-07-09 22:06:01 +00002816 assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2817 || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint);
2818
Nate Begeman6f3d8382009-06-26 06:32:41 +00002819 // Attribute has 3 arguments.
Tanya Lattner0df579e2012-07-09 22:06:01 +00002820 if (!checkAttributeNumArgs(S, Attr, 3)) return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002821
2822 unsigned WGSize[3];
2823 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002824 Expr *E = Attr.getArgAsExpr(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002825 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002826 if (E->isTypeDependent() || E->isValueDependent() ||
2827 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00002828 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2829 << Attr.getName() << AANT_ArgumentIntegerConstant
2830 << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002831 return;
2832 }
2833 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2834 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002835
2836 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2837 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2838 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2839 if (!(A->getXDim() == WGSize[0] &&
2840 A->getYDim() == WGSize[1] &&
2841 A->getZDim() == WGSize[2])) {
2842 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2843 Attr.getName();
2844 }
2845 }
2846
2847 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2848 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2849 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2850 if (!(A->getXDim() == WGSize[0] &&
2851 A->getYDim() == WGSize[1] &&
2852 A->getZDim() == WGSize[2])) {
2853 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2854 Attr.getName();
2855 }
2856 }
2857
2858 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2859 D->addAttr(::new (S.Context)
2860 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002861 WGSize[0], WGSize[1], WGSize[2],
2862 Attr.getAttributeSpellingListIndex()));
Tanya Lattner0df579e2012-07-09 22:06:01 +00002863 else
2864 D->addAttr(::new (S.Context)
2865 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002866 WGSize[0], WGSize[1], WGSize[2],
2867 Attr.getAttributeSpellingListIndex()));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002868}
2869
Joey Gouly37453b92013-03-08 09:42:32 +00002870static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2871 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2872
Aaron Ballman624421f2013-08-31 01:11:41 +00002873 if (!checkAttributeNumArgs(S, Attr, 0))
Joey Gouly37453b92013-03-08 09:42:32 +00002874 return;
2875
Aaron Ballman624421f2013-08-31 01:11:41 +00002876 if (!Attr.hasParsedType()) {
2877 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2878 << Attr.getName() << 1;
2879 return;
2880 }
2881
Joey Gouly37453b92013-03-08 09:42:32 +00002882 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg());
2883
2884 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2885 (ParmType->isBooleanType() ||
2886 !ParmType->isIntegralType(S.getASTContext()))) {
2887 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2888 << ParmType;
2889 return;
2890 }
2891
2892 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2893 D->hasAttr<VecTypeHintAttr>()) {
2894 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
2895 if (A->getTypeHint() != ParmType) {
2896 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2897 return;
2898 }
2899 }
2900
2901 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
2902 ParmType, Attr.getLoc()));
2903}
2904
Rafael Espindola599f1b72012-05-13 03:25:18 +00002905SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002906 StringRef Name,
2907 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002908 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2909 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002910 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002911 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2912 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002913 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002914 }
Michael Han51d8c522013-01-24 16:46:58 +00002915 return ::new (Context) SectionAttr(Range, Context, Name,
2916 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002917}
2918
Chandler Carruth1b03c872011-07-02 00:01:44 +00002919static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002920 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002921 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002922 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002923
2924 // Make sure that there is a string literal as the sections's single
2925 // argument.
Aaron Ballman624421f2013-08-31 01:11:41 +00002926 Expr *ArgExpr = Attr.getArgAsExpr(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002927 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002928 if (!SE) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002929 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
2930 << Attr.getName() << AANT_ArgumentString;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002931 return;
2932 }
Mike Stump1eb44332009-09-09 15:08:12 +00002933
Chris Lattner797c3c42009-08-10 19:03:04 +00002934 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002935 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002936 if (!Error.empty()) {
2937 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2938 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002939 return;
2940 }
Mike Stump1eb44332009-09-09 15:08:12 +00002941
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002942 // This attribute cannot be applied to local variables.
2943 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2944 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2945 return;
2946 }
Michael Han51d8c522013-01-24 16:46:58 +00002947
2948 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindola599f1b72012-05-13 03:25:18 +00002949 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han51d8c522013-01-24 16:46:58 +00002950 SE->getString(), Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002951 if (NewAttr)
2952 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002953}
2954
Chris Lattner6b6b5372008-06-26 18:38:35 +00002955
Chandler Carruth1b03c872011-07-02 00:01:44 +00002956static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002957 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002958 return;
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002959
Chandler Carruth87c44602011-07-01 23:49:12 +00002960 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002961 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002962 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002963 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002964 D->addAttr(::new (S.Context)
2965 NoThrowAttr(Attr.getRange(), S.Context,
2966 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002967 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002968}
2969
Chandler Carruth1b03c872011-07-02 00:01:44 +00002970static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002971 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002972 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002973
Chandler Carruth87c44602011-07-01 23:49:12 +00002974 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002975 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002976 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002977 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002978 D->addAttr(::new (S.Context)
2979 ConstAttr(Attr.getRange(), S.Context,
2980 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002981 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002982}
2983
Chandler Carruth1b03c872011-07-02 00:01:44 +00002984static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002985 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002986 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002987 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002988
Michael Han51d8c522013-01-24 16:46:58 +00002989 D->addAttr(::new (S.Context)
2990 PureAttr(Attr.getRange(), S.Context,
2991 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002992}
2993
Chandler Carruth1b03c872011-07-02 00:01:44 +00002994static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002995 if (!Attr.isArgIdent(0)) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00002996 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2997 << Attr.getName() << 1;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002998 return;
2999 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003000
3001 if (!checkAttributeNumArgs(S, Attr, 1))
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003002 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003003
Chandler Carruth87c44602011-07-01 23:49:12 +00003004 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00003005
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003006 if (!VD || !VD->hasLocalStorage()) {
3007 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
3008 return;
3009 }
Mike Stumpbf916502009-07-24 19:02:52 +00003010
Aaron Ballman624421f2013-08-31 01:11:41 +00003011 IdentifierLoc *IL = Attr.getArgAsIdent(0);
3012
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003013 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00003014 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00003015 NamedDecl *CleanupDecl
Aaron Ballman624421f2013-08-31 01:11:41 +00003016 = S.LookupSingleName(S.TUScope, IL->Ident, IL->Loc,
3017 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003018 if (!CleanupDecl) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003019 S.Diag(IL->Loc, diag::err_attribute_cleanup_arg_not_found) << IL->Ident;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003020 return;
3021 }
Mike Stumpbf916502009-07-24 19:02:52 +00003022
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003023 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
3024 if (!FD) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003025 S.Diag(IL->Loc, diag::err_attribute_cleanup_arg_not_function) << IL->Ident;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003026 return;
3027 }
3028
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003029 if (FD->getNumParams() != 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003030 S.Diag(IL->Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
3031 << IL->Ident;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003032 return;
3033 }
Mike Stumpbf916502009-07-24 19:02:52 +00003034
Anders Carlsson89941c12009-02-07 23:16:50 +00003035 // We're currently more strict than GCC about what function types we accept.
3036 // If this ever proves to be a problem it should be easy to fix.
3037 QualType Ty = S.Context.getPointerType(VD->getType());
3038 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00003039 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
3040 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003041 S.Diag(IL->Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) <<
3042 IL->Ident << ParamTy << Ty;
Anders Carlsson89941c12009-02-07 23:16:50 +00003043 return;
3044 }
Mike Stumpbf916502009-07-24 19:02:52 +00003045
Michael Han51d8c522013-01-24 16:46:58 +00003046 D->addAttr(::new (S.Context)
3047 CleanupAttr(Attr.getRange(), S.Context, FD,
3048 Attr.getAttributeSpellingListIndex()));
Aaron Ballman624421f2013-08-31 01:11:41 +00003049 S.MarkFunctionReferenced(IL->Loc, FD);
3050 S.DiagnoseUseOfDecl(FD, IL->Loc);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003051}
3052
Mike Stumpbf916502009-07-24 19:02:52 +00003053/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003054/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003055static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00003056 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003057 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003058
Chandler Carruth87c44602011-07-01 23:49:12 +00003059 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003060 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003061 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003062 return;
3063 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003064
Aaron Ballman624421f2013-08-31 01:11:41 +00003065 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00003066 uint64_t ArgIdx;
3067 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
3068 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003069 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003070
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003071 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003072 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00003073
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003074 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
3075 if (not_nsstring_type &&
3076 !isCFStringType(Ty, S.Context) &&
3077 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003078 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003079 // FIXME: Should highlight the actual expression that has the wrong type.
3080 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00003081 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003082 << IdxExpr->getSourceRange();
3083 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003084 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003085 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003086 if (!isNSStringType(Ty, S.Context) &&
3087 !isCFStringType(Ty, S.Context) &&
3088 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003089 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003090 // FIXME: Should highlight the actual expression that has the wrong type.
3091 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00003092 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003093 << IdxExpr->getSourceRange();
3094 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003095 }
3096
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00003097 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
3098 // because that has corrected for the implicit this parameter, and is zero-
3099 // based. The attribute expects what the user wrote explicitly.
3100 llvm::APSInt Val;
3101 IdxExpr->EvaluateAsInt(Val, S.Context);
3102
Michael Han51d8c522013-01-24 16:46:58 +00003103 D->addAttr(::new (S.Context)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00003104 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003105 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003106}
3107
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003108enum FormatAttrKind {
3109 CFStringFormat,
3110 NSStringFormat,
3111 StrftimeFormat,
3112 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00003113 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003114 InvalidFormat
3115};
3116
3117/// getFormatAttrKind - Map from format attribute names to supported format
3118/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003119static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003120 return llvm::StringSwitch<FormatAttrKind>(Format)
3121 // Check for formats that get handled specially.
3122 .Case("NSString", NSStringFormat)
3123 .Case("CFString", CFStringFormat)
3124 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003125
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003126 // Otherwise, check for supported formats.
3127 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3128 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3129 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003130
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003131 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3132 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003133}
3134
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003135/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003136/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003137static void handleInitPriorityAttr(Sema &S, Decl *D,
3138 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003139 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003140 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3141 return;
3142 }
3143
Chandler Carruth87c44602011-07-01 23:49:12 +00003144 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003145 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3146 Attr.setInvalid();
3147 return;
3148 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003149 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003150 if (S.Context.getAsArrayType(T))
3151 T = S.Context.getBaseElementType(T);
3152 if (!T->getAs<RecordType>()) {
3153 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3154 Attr.setInvalid();
3155 return;
3156 }
3157
Aaron Ballmanffa9d572013-07-18 18:01:48 +00003158 if (!checkAttributeNumArgs(S, Attr, 1)) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003159 Attr.setInvalid();
3160 return;
3161 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003162 Expr *priorityExpr = Attr.getArgAsExpr(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003163
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003164 llvm::APSInt priority(32);
3165 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3166 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003167 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3168 << Attr.getName() << AANT_ArgumentIntegerConstant
3169 << priorityExpr->getSourceRange();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003170 Attr.setInvalid();
3171 return;
3172 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00003173 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003174 if (prioritynum < 101 || prioritynum > 65535) {
3175 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3176 << priorityExpr->getSourceRange();
3177 Attr.setInvalid();
3178 return;
3179 }
Michael Han51d8c522013-01-24 16:46:58 +00003180 D->addAttr(::new (S.Context)
3181 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3182 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003183}
3184
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003185FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3186 IdentifierInfo *Format, int FormatIdx,
3187 int FirstArg,
Michael Han51d8c522013-01-24 16:46:58 +00003188 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003189 // Check whether we already have an equivalent format attribute.
3190 for (specific_attr_iterator<FormatAttr>
3191 i = D->specific_attr_begin<FormatAttr>(),
3192 e = D->specific_attr_end<FormatAttr>();
3193 i != e ; ++i) {
3194 FormatAttr *f = *i;
3195 if (f->getType() == Format &&
3196 f->getFormatIdx() == FormatIdx &&
3197 f->getFirstArg() == FirstArg) {
3198 // If we don't have a valid location for this attribute, adopt the
3199 // location.
3200 if (f->getLocation().isInvalid())
3201 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00003202 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003203 }
3204 }
3205
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003206 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3207 FirstArg, AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003208}
3209
Mike Stumpbf916502009-07-24 19:02:52 +00003210/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003211/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003212static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003213 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003214 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00003215 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003216 return;
3217 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003218
3219 if (!checkAttributeNumArgs(S, Attr, 3))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003220 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003221
Chandler Carruth87c44602011-07-01 23:49:12 +00003222 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003223 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003224 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003225 return;
3226 }
3227
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003228 // In C++ the implicit 'this' function parameter also counts, and they are
3229 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003230 bool HasImplicitThisParam = isInstanceMethod(D);
3231 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003232 unsigned FirstIdx = 1;
3233
Aaron Ballman624421f2013-08-31 01:11:41 +00003234 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
3235 StringRef Format = II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003236
3237 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003238 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003239 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003240 // If we've modified the string name, we need a new identifier for it.
3241 II = &S.Context.Idents.get(Format);
3242 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003243
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003244 // Check for supported formats.
3245 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003246
3247 if (Kind == IgnoredFormat)
3248 return;
3249
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003250 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003251 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman624421f2013-08-31 01:11:41 +00003252 << "format" << II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003253 return;
3254 }
3255
3256 // checks for the 2nd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003257 Expr *IdxExpr = Attr.getArgAsExpr(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003258 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003259 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3260 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003261 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003262 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003263 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003264 return;
3265 }
3266
3267 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003268 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003269 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003270 return;
3271 }
3272
3273 // FIXME: Do we need to bounds check?
3274 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003275
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003276 if (HasImplicitThisParam) {
3277 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003278 S.Diag(Attr.getLoc(),
3279 diag::err_format_attribute_implicit_this_format_string)
3280 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003281 return;
3282 }
3283 ArgIdx--;
3284 }
Mike Stump1eb44332009-09-09 15:08:12 +00003285
Chris Lattner6b6b5372008-06-26 18:38:35 +00003286 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003287 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003288
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003289 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003290 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003291 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3292 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003293 return;
3294 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003295 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003296 // FIXME: do we need to check if the type is NSString*? What are the
3297 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003298 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003299 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003300 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3301 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003302 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003303 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003304 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003305 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003306 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003307 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3308 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003309 return;
3310 }
3311
3312 // check the 3rd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003313 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Chris Lattner803d0802008-06-29 00:43:07 +00003314 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003315 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3316 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003317 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003318 << Attr.getName() << 3 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003319 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003320 return;
3321 }
3322
3323 // check if the function is variadic if the 3rd argument non-zero
3324 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003325 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003326 ++NumArgs; // +1 for ...
3327 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003328 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003329 return;
3330 }
3331 }
3332
Chris Lattner3c73c412008-11-19 08:23:25 +00003333 // strftime requires FirstArg to be 0 because it doesn't read from any
3334 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003335 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003336 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003337 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3338 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003339 return;
3340 }
3341 // if 0 it disables parameter checking (to use with e.g. va_list)
3342 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003343 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003344 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003345 return;
3346 }
3347
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003348 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Rafael Espindola599f1b72012-05-13 03:25:18 +00003349 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003350 FirstArg.getZExtValue(),
3351 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003352 if (NewAttr)
3353 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003354}
3355
Chandler Carruth1b03c872011-07-02 00:01:44 +00003356static void handleTransparentUnionAttr(Sema &S, Decl *D,
3357 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003358 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003359 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003360 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003361
Chris Lattner6b6b5372008-06-26 18:38:35 +00003362
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003363 // Try to find the underlying union declaration.
3364 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003365 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003366 if (TD && TD->getUnderlyingType()->isUnionType())
3367 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3368 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003369 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003370
3371 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003372 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003373 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003374 return;
3375 }
3376
John McCall5e1cdac2011-10-07 06:10:15 +00003377 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003378 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003379 diag::warn_transparent_union_attribute_not_definition);
3380 return;
3381 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003382
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003383 RecordDecl::field_iterator Field = RD->field_begin(),
3384 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003385 if (Field == FieldEnd) {
3386 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3387 return;
3388 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003389
David Blaikie581deb32012-06-06 20:45:41 +00003390 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003391 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003392 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003393 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003394 diag::warn_transparent_union_attribute_floating)
3395 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003396 return;
3397 }
3398
3399 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3400 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3401 for (; Field != FieldEnd; ++Field) {
3402 QualType FieldType = Field->getType();
3403 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3404 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3405 // Warn if we drop the attribute.
3406 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003407 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003408 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003409 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003410 diag::warn_transparent_union_attribute_field_size_align)
3411 << isSize << Field->getDeclName() << FieldBits;
3412 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003413 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003414 diag::note_transparent_union_first_field_size_align)
3415 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003416 return;
3417 }
3418 }
3419
Michael Han51d8c522013-01-24 16:46:58 +00003420 RD->addAttr(::new (S.Context)
3421 TransparentUnionAttr(Attr.getRange(), S.Context,
3422 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003423}
3424
Chandler Carruth1b03c872011-07-02 00:01:44 +00003425static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003426 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003427 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003428 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003429
Aaron Ballman624421f2013-08-31 01:11:41 +00003430 Expr *ArgExpr = Attr.getArgAsExpr(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003431 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003432
Chris Lattner6b6b5372008-06-26 18:38:35 +00003433 // Make sure that there is a string literal as the annotation's single
3434 // argument.
3435 if (!SE) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003436 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
3437 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003438 return;
3439 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003440
3441 // Don't duplicate annotations that are already set.
3442 for (specific_attr_iterator<AnnotateAttr>
3443 i = D->specific_attr_begin<AnnotateAttr>(),
3444 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3445 if ((*i)->getAnnotation() == SE->getString())
3446 return;
3447 }
Michael Han51d8c522013-01-24 16:46:58 +00003448
3449 D->addAttr(::new (S.Context)
3450 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3451 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003452}
3453
Chandler Carruth1b03c872011-07-02 00:01:44 +00003454static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003455 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003456 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003457 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3458 << Attr.getName() << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003459 return;
3460 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003461
Richard Smithbe507b62013-02-01 08:12:08 +00003462 if (Attr.getNumArgs() == 0) {
3463 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3464 true, 0, Attr.getAttributeSpellingListIndex()));
3465 return;
3466 }
3467
Aaron Ballman624421f2013-08-31 01:11:41 +00003468 Expr *E = Attr.getArgAsExpr(0);
Richard Smithf6565a92013-02-22 08:32:16 +00003469 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3470 S.Diag(Attr.getEllipsisLoc(),
3471 diag::err_pack_expansion_without_parameter_packs);
3472 return;
3473 }
3474
3475 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3476 return;
3477
3478 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3479 Attr.isPackExpansion());
Richard Smithbe507b62013-02-01 08:12:08 +00003480}
3481
3482void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smithf6565a92013-02-22 08:32:16 +00003483 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smithbe507b62013-02-01 08:12:08 +00003484 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3485 SourceLocation AttrLoc = AttrRange.getBegin();
3486
Richard Smith4cd81c52013-01-29 09:02:09 +00003487 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smithbe507b62013-02-01 08:12:08 +00003488 if (TmpAttr.isAlignas()) {
Richard Smith4cd81c52013-01-29 09:02:09 +00003489 // C++11 [dcl.align]p1:
3490 // An alignment-specifier may be applied to a variable or to a class
3491 // data member, but it shall not be applied to a bit-field, a function
3492 // parameter, the formal parameter of a catch clause, or a variable
3493 // declared with the register storage class specifier. An
3494 // alignment-specifier may also be applied to the declaration of a class
3495 // or enumeration type.
3496 // C11 6.7.5/2:
3497 // An alignment attribute shall not be specified in a declaration of
3498 // a typedef, or a bit-field, or a function, or a parameter, or an
3499 // object declared with the register storage-class specifier.
3500 int DiagKind = -1;
3501 if (isa<ParmVarDecl>(D)) {
3502 DiagKind = 0;
3503 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3504 if (VD->getStorageClass() == SC_Register)
3505 DiagKind = 1;
3506 if (VD->isExceptionVariable())
3507 DiagKind = 2;
3508 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3509 if (FD->isBitField())
3510 DiagKind = 3;
3511 } else if (!isa<TagDecl>(D)) {
Richard Smithbe507b62013-02-01 08:12:08 +00003512 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3513 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith5f838aa2013-02-01 08:25:07 +00003514 << (TmpAttr.isC11() ? ExpectedVariableOrField
3515 : ExpectedVariableFieldOrTag);
Richard Smith4cd81c52013-01-29 09:02:09 +00003516 return;
3517 }
3518 if (DiagKind != -1) {
Richard Smithbe507b62013-02-01 08:12:08 +00003519 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smith671b3212013-02-22 04:55:39 +00003520 << TmpAttr.isC11() << DiagKind;
Richard Smith4cd81c52013-01-29 09:02:09 +00003521 return;
3522 }
3523 }
3524
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003525 if (E->isTypeDependent() || E->isValueDependent()) {
3526 // Save dependent expressions in the AST to be instantiated.
Richard Smithf6565a92013-02-22 08:32:16 +00003527 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3528 AA->setPackExpansion(IsPackExpansion);
3529 D->addAttr(AA);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003530 return;
3531 }
Michael Hana31f65b2013-02-01 01:19:17 +00003532
Sean Huntcf807c42010-08-18 23:23:40 +00003533 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003534 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003535 ExprResult ICE
3536 = VerifyIntegerConstantExpression(E, &Alignment,
3537 diag::err_aligned_attribute_argument_not_int,
3538 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003539 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003540 return;
Richard Smithbe507b62013-02-01 08:12:08 +00003541
3542 // C++11 [dcl.align]p2:
3543 // -- if the constant expression evaluates to zero, the alignment
3544 // specifier shall have no effect
3545 // C11 6.7.5p6:
3546 // An alignment specification of zero has no effect.
3547 if (!(TmpAttr.isAlignas() && !Alignment) &&
3548 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003549 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3550 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003551 return;
3552 }
Michael Hana31f65b2013-02-01 01:19:17 +00003553
Richard Smithbe507b62013-02-01 08:12:08 +00003554 if (TmpAttr.isDeclspec()) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003555 // We've already verified it's a power of 2, now let's make sure it's
3556 // 8192 or less.
3557 if (Alignment.getZExtValue() > 8192) {
Michael Hana31f65b2013-02-01 01:19:17 +00003558 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003559 << E->getSourceRange();
3560 return;
3561 }
3562 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003563
Richard Smithf6565a92013-02-22 08:32:16 +00003564 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3565 ICE.take(), SpellingListIndex);
3566 AA->setPackExpansion(IsPackExpansion);
3567 D->addAttr(AA);
Sean Huntcf807c42010-08-18 23:23:40 +00003568}
3569
Michael Hana31f65b2013-02-01 01:19:17 +00003570void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smithf6565a92013-02-22 08:32:16 +00003571 unsigned SpellingListIndex, bool IsPackExpansion) {
Sean Huntcf807c42010-08-18 23:23:40 +00003572 // FIXME: Cache the number on the Attr object if non-dependent?
3573 // FIXME: Perform checking of type validity
Richard Smithf6565a92013-02-22 08:32:16 +00003574 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3575 SpellingListIndex);
3576 AA->setPackExpansion(IsPackExpansion);
3577 D->addAttr(AA);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003578}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003579
Richard Smithbe507b62013-02-01 08:12:08 +00003580void Sema::CheckAlignasUnderalignment(Decl *D) {
3581 assert(D->hasAttrs() && "no attributes on decl");
3582
3583 QualType Ty;
3584 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3585 Ty = VD->getType();
3586 else
3587 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith4da09032013-02-22 09:21:42 +00003588 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smithbe507b62013-02-01 08:12:08 +00003589 return;
3590
3591 // C++11 [dcl.align]p5, C11 6.7.5/4:
3592 // The combined effect of all alignment attributes in a declaration shall
3593 // not specify an alignment that is less strict than the alignment that
3594 // would otherwise be required for the entity being declared.
3595 AlignedAttr *AlignasAttr = 0;
3596 unsigned Align = 0;
3597 for (specific_attr_iterator<AlignedAttr>
3598 I = D->specific_attr_begin<AlignedAttr>(),
3599 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3600 if (I->isAlignmentDependent())
3601 return;
3602 if (I->isAlignas())
3603 AlignasAttr = *I;
3604 Align = std::max(Align, I->getAlignment(Context));
3605 }
3606
3607 if (AlignasAttr && Align) {
3608 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3609 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3610 if (NaturalAlign > RequestedAlign)
3611 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3612 << Ty << (unsigned)NaturalAlign.getQuantity();
3613 }
3614}
3615
Chandler Carruthd309c812011-07-01 23:49:16 +00003616/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003617/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003618///
Mike Stumpbf916502009-07-24 19:02:52 +00003619/// Despite what would be logical, the mode attribute is a decl attribute, not a
3620/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3621/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003622static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003623 // This attribute isn't documented, but glibc uses it. It changes
3624 // the width of an int or unsigned int to the specified size.
Aaron Ballman624421f2013-08-31 01:11:41 +00003625 if (!Attr.isArgIdent(0)) {
Aaron Ballman750f73a2013-07-30 14:29:12 +00003626 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3627 << AANT_ArgumentIdentifier;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003628 return;
3629 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003630
3631 if (!checkAttributeNumArgs(S, Attr, 1))
3632 return;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003633
Aaron Ballman624421f2013-08-31 01:11:41 +00003634 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3635 StringRef Str = Name->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003636
3637 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003638 if (Str.startswith("__") && Str.endswith("__"))
3639 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003640
3641 unsigned DestWidth = 0;
3642 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003643 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003644 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003645 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003646 switch (Str[0]) {
3647 case 'Q': DestWidth = 8; break;
3648 case 'H': DestWidth = 16; break;
3649 case 'S': DestWidth = 32; break;
3650 case 'D': DestWidth = 64; break;
3651 case 'X': DestWidth = 96; break;
3652 case 'T': DestWidth = 128; break;
3653 }
3654 if (Str[1] == 'F') {
3655 IntegerMode = false;
3656 } else if (Str[1] == 'C') {
3657 IntegerMode = false;
3658 ComplexMode = true;
3659 } else if (Str[1] != 'I') {
3660 DestWidth = 0;
3661 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003662 break;
3663 case 4:
3664 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3665 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003666 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003667 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003668 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003669 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003670 break;
3671 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003672 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003673 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003674 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003675 case 11:
3676 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003677 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003678 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003679 }
3680
3681 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003682 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003683 OldTy = TD->getUnderlyingType();
3684 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3685 OldTy = VD->getType();
3686 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003687 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003688 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003689 return;
3690 }
Eli Friedman73397492009-03-03 06:41:03 +00003691
John McCall183700f2009-09-21 23:43:11 +00003692 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003693 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3694 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003695 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003696 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3697 } else if (ComplexMode) {
3698 if (!OldTy->isComplexType())
3699 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3700 } else {
3701 if (!OldTy->isFloatingType())
3702 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3703 }
3704
Mike Stump390b4cc2009-05-16 07:39:55 +00003705 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3706 // and friends, at least with glibc.
3707 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3708 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003709 // FIXME: Make sure floating-point mappings are accurate
3710 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003711 QualType NewTy;
3712 switch (DestWidth) {
3713 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003714 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003715 return;
3716 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003717 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003718 return;
3719 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003720 if (!IntegerMode) {
3721 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3722 return;
3723 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003724 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003725 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003726 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003727 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003728 break;
3729 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003730 if (!IntegerMode) {
3731 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3732 return;
3733 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003734 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003735 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003736 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003737 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003738 break;
3739 case 32:
3740 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003741 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003742 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003743 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003744 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003745 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003746 break;
3747 case 64:
3748 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003749 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003750 else if (OldTy->isSignedIntegerType())
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.LongTy;
3753 else
3754 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003755 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003756 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003757 NewTy = S.Context.UnsignedLongTy;
3758 else
3759 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003760 break;
Eli Friedman73397492009-03-03 06:41:03 +00003761 case 96:
3762 NewTy = S.Context.LongDoubleTy;
3763 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003764 case 128:
Roman Divacky19645542013-07-03 21:08:41 +00003765 if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() !=
3766 &llvm::APFloat::PPCDoubleDouble) {
Eli Friedmanf98aba32009-02-13 02:31:07 +00003767 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3768 return;
3769 }
Roman Divackyf0d14cb2013-07-03 20:48:06 +00003770 if (IntegerMode) {
3771 if (OldTy->isSignedIntegerType())
3772 NewTy = S.Context.Int128Ty;
3773 else
3774 NewTy = S.Context.UnsignedInt128Ty;
3775 } else
3776 NewTy = S.Context.LongDoubleTy;
Eli Friedman73397492009-03-03 06:41:03 +00003777 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003778 }
3779
Eli Friedman73397492009-03-03 06:41:03 +00003780 if (ComplexMode) {
3781 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003782 }
3783
3784 // Install the new type.
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003785 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3786 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3787 else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003788 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003789
3790 D->addAttr(::new (S.Context)
3791 ModeAttr(Attr.getRange(), S.Context, Name,
3792 Attr.getAttributeSpellingListIndex()));
Chris Lattnerfbf13472008-06-27 22:18:37 +00003793}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003794
Chandler Carruth1b03c872011-07-02 00:01:44 +00003795static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003796 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003797 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003798 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003799
Nick Lewycky78d1a102012-07-24 01:40:49 +00003800 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3801 if (!VD->hasGlobalStorage())
3802 S.Diag(Attr.getLoc(),
3803 diag::warn_attribute_requires_functions_or_static_globals)
3804 << Attr.getName();
3805 } else if (!isFunctionOrMethod(D)) {
3806 S.Diag(Attr.getLoc(),
3807 diag::warn_attribute_requires_functions_or_static_globals)
3808 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003809 return;
3810 }
Mike Stumpbf916502009-07-24 19:02:52 +00003811
Michael Han51d8c522013-01-24 16:46:58 +00003812 D->addAttr(::new (S.Context)
3813 NoDebugAttr(Attr.getRange(), S.Context,
3814 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003815}
3816
Chandler Carruth1b03c872011-07-02 00:01:44 +00003817static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003818 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003819 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003820 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003821
Mike Stumpbf916502009-07-24 19:02:52 +00003822
Chandler Carruth87c44602011-07-01 23:49:12 +00003823 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003824 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003825 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003826 return;
3827 }
Mike Stumpbf916502009-07-24 19:02:52 +00003828
Michael Han51d8c522013-01-24 16:46:58 +00003829 D->addAttr(::new (S.Context)
3830 NoInlineAttr(Attr.getRange(), S.Context,
3831 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003832}
3833
Chandler Carruth1b03c872011-07-02 00:01:44 +00003834static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3835 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003836 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003837 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003838 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003839
Chris Lattner7255a2d2010-06-22 00:03:40 +00003840
Chandler Carruth87c44602011-07-01 23:49:12 +00003841 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003842 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003843 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003844 return;
3845 }
3846
Michael Han51d8c522013-01-24 16:46:58 +00003847 D->addAttr(::new (S.Context)
3848 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3849 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003850}
3851
Chandler Carruth1b03c872011-07-02 00:01:44 +00003852static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003853 if (S.LangOpts.CUDA) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003854 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003855 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003856
Chandler Carruth87c44602011-07-01 23:49:12 +00003857 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003858 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003859 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003860 return;
3861 }
3862
Michael Han51d8c522013-01-24 16:46:58 +00003863 D->addAttr(::new (S.Context)
3864 CUDAConstantAttr(Attr.getRange(), S.Context,
3865 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003866 } else {
3867 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3868 }
3869}
3870
Chandler Carruth1b03c872011-07-02 00:01:44 +00003871static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003872 if (S.LangOpts.CUDA) {
3873 // check the attribute arguments.
3874 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003875 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3876 << Attr.getName() << 0;
Peter Collingbourneced76712010-12-01 03:15:31 +00003877 return;
3878 }
3879
Chandler Carruth87c44602011-07-01 23:49:12 +00003880 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003881 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003882 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003883 return;
3884 }
3885
Michael Han51d8c522013-01-24 16:46:58 +00003886 D->addAttr(::new (S.Context)
3887 CUDADeviceAttr(Attr.getRange(), S.Context,
3888 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003889 } else {
3890 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3891 }
3892}
3893
Chandler Carruth1b03c872011-07-02 00:01:44 +00003894static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003895 if (S.LangOpts.CUDA) {
3896 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003897 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003898 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003899
Chandler Carruth87c44602011-07-01 23:49:12 +00003900 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003901 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003902 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003903 return;
3904 }
3905
Chandler Carruth87c44602011-07-01 23:49:12 +00003906 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003907 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003908 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00003909 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003910 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3911 << FD->getType()
David Blaikie39e6ab42013-02-18 22:06:02 +00003912 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003913 "void");
3914 } else {
3915 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3916 << FD->getType();
3917 }
3918 return;
3919 }
3920
Michael Han51d8c522013-01-24 16:46:58 +00003921 D->addAttr(::new (S.Context)
3922 CUDAGlobalAttr(Attr.getRange(), S.Context,
3923 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003924 } else {
3925 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3926 }
3927}
3928
Chandler Carruth1b03c872011-07-02 00:01:44 +00003929static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003930 if (S.LangOpts.CUDA) {
3931 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003932 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003933 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003934
Peter Collingbourneced76712010-12-01 03:15:31 +00003935
Chandler Carruth87c44602011-07-01 23:49:12 +00003936 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003937 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003938 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003939 return;
3940 }
3941
Michael Han51d8c522013-01-24 16:46:58 +00003942 D->addAttr(::new (S.Context)
3943 CUDAHostAttr(Attr.getRange(), S.Context,
3944 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003945 } else {
3946 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3947 }
3948}
3949
Chandler Carruth1b03c872011-07-02 00:01:44 +00003950static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003951 if (S.LangOpts.CUDA) {
3952 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003953 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003954 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003955
Chandler Carruth87c44602011-07-01 23:49:12 +00003956 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003957 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003958 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003959 return;
3960 }
3961
Michael Han51d8c522013-01-24 16:46:58 +00003962 D->addAttr(::new (S.Context)
3963 CUDASharedAttr(Attr.getRange(), S.Context,
3964 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003965 } else {
3966 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3967 }
3968}
3969
Chandler Carruth1b03c872011-07-02 00:01:44 +00003970static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003971 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003972 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003973 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003974
Chandler Carruth87c44602011-07-01 23:49:12 +00003975 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003976 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003977 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003978 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003979 return;
3980 }
Mike Stumpbf916502009-07-24 19:02:52 +00003981
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003982 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003983 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003984 return;
3985 }
Mike Stumpbf916502009-07-24 19:02:52 +00003986
Michael Han51d8c522013-01-24 16:46:58 +00003987 D->addAttr(::new (S.Context)
3988 GNUInlineAttr(Attr.getRange(), S.Context,
3989 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00003990}
3991
Chandler Carruth1b03c872011-07-02 00:01:44 +00003992static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003993 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003994
Aaron Ballmanfff32482012-12-09 17:45:41 +00003995 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003996 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003997 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3998 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003999 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00004000 return;
4001
Chandler Carruth87c44602011-07-01 23:49:12 +00004002 if (!isa<ObjCMethodDecl>(D)) {
4003 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4004 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00004005 return;
4006 }
4007
Chandler Carruth87c44602011-07-01 23:49:12 +00004008 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004009 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00004010 D->addAttr(::new (S.Context)
4011 FastCallAttr(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_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00004015 D->addAttr(::new (S.Context)
4016 StdCallAttr(Attr.getRange(), S.Context,
4017 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00004018 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004019 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00004020 D->addAttr(::new (S.Context)
4021 ThisCallAttr(Attr.getRange(), S.Context,
4022 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00004023 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004024 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00004025 D->addAttr(::new (S.Context)
4026 CDeclAttr(Attr.getRange(), S.Context,
4027 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00004028 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004029 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00004030 D->addAttr(::new (S.Context)
4031 PascalAttr(Attr.getRange(), S.Context,
4032 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00004033 return;
Charles Davise8519c32013-08-30 04:39:01 +00004034 case AttributeList::AT_MSABI:
4035 D->addAttr(::new (S.Context)
4036 MSABIAttr(Attr.getRange(), S.Context,
4037 Attr.getAttributeSpellingListIndex()));
4038 return;
4039 case AttributeList::AT_SysVABI:
4040 D->addAttr(::new (S.Context)
4041 SysVABIAttr(Attr.getRange(), S.Context,
4042 Attr.getAttributeSpellingListIndex()));
4043 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004044 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004045 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00004046 switch (CC) {
4047 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004048 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00004049 break;
4050 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004051 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00004052 break;
4053 default:
4054 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004055 }
4056
Michael Han51d8c522013-01-24 16:46:58 +00004057 D->addAttr(::new (S.Context)
4058 PcsAttr(Attr.getRange(), S.Context, PCS,
4059 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00004060 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004061 }
Derek Schuff263366f2012-10-16 22:30:41 +00004062 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00004063 D->addAttr(::new (S.Context)
4064 PnaclCallAttr(Attr.getRange(), S.Context,
4065 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00004066 return;
Guy Benyei38980082012-12-25 08:53:55 +00004067 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00004068 D->addAttr(::new (S.Context)
4069 IntelOclBiccAttr(Attr.getRange(), S.Context,
4070 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00004071 return;
Derek Schuff263366f2012-10-16 22:30:41 +00004072
Abramo Bagnarae215f722010-04-30 13:10:51 +00004073 default:
4074 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00004075 }
4076}
4077
Chandler Carruth1b03c872011-07-02 00:01:44 +00004078static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004079 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00004080}
4081
Guy Benyei1db70402013-03-24 13:58:12 +00004082static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
Aaron Ballman624421f2013-08-31 01:11:41 +00004083 Expr *E = Attr.getArgAsExpr(0);
Guy Benyei1db70402013-03-24 13:58:12 +00004084 llvm::APSInt ArgNum(32);
4085 if (E->isTypeDependent() || E->isValueDependent() ||
4086 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00004087 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4088 << Attr.getName() << AANT_ArgumentIntegerConstant
4089 << E->getSourceRange();
Guy Benyei1db70402013-03-24 13:58:12 +00004090 return;
4091 }
4092
4093 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
4094 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
4095}
4096
Aaron Ballmanfff32482012-12-09 17:45:41 +00004097bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
4098 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00004099 if (attr.isInvalid())
4100 return true;
4101
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004102 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman624421f2013-08-31 01:11:41 +00004103 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall711c52b2011-01-05 12:14:39 +00004104 attr.setInvalid();
4105 return true;
4106 }
4107
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004108 // TODO: diagnose uses of these conventions on the wrong target. Or, better
4109 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00004110 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004111 case AttributeList::AT_CDecl: CC = CC_C; break;
4112 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
4113 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
4114 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
4115 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davise8519c32013-08-30 04:39:01 +00004116 case AttributeList::AT_MSABI:
4117 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
4118 CC_X86_64Win64;
4119 break;
4120 case AttributeList::AT_SysVABI:
4121 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
4122 CC_C;
4123 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004124 case AttributeList::AT_Pcs: {
Aaron Ballman624421f2013-08-31 01:11:41 +00004125 StringLiteral *Str = 0;
4126 if (attr.isArgExpr(0))
4127 Str = dyn_cast<StringLiteral>(attr.getArgAsExpr(0));
4128
Douglas Gregor5cee1192011-07-27 05:40:30 +00004129 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004130 Diag(attr.getLoc(), diag::err_attribute_argument_type) << attr.getName()
4131 << AANT_ArgumentString;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004132 attr.setInvalid();
4133 return true;
4134 }
4135
Chris Lattner5f9e2722011-07-23 10:55:15 +00004136 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004137 if (StrRef == "aapcs") {
4138 CC = CC_AAPCS;
4139 break;
4140 } else if (StrRef == "aapcs-vfp") {
4141 CC = CC_AAPCS_VFP;
4142 break;
4143 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004144
4145 attr.setInvalid();
4146 Diag(attr.getLoc(), diag::err_invalid_pcs);
4147 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004148 }
Derek Schuff263366f2012-10-16 22:30:41 +00004149 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00004150 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00004151 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00004152 }
4153
Aaron Ballman82bfa192012-10-02 14:26:08 +00004154 const TargetInfo &TI = Context.getTargetInfo();
4155 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
4156 if (A == TargetInfo::CCCR_Warning) {
4157 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00004158
4159 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
4160 if (FD)
4161 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
4162 TargetInfo::CCMT_NonMember;
4163 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00004164 }
4165
John McCall711c52b2011-01-05 12:14:39 +00004166 return false;
4167}
4168
Chandler Carruth1b03c872011-07-02 00:01:44 +00004169static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004170 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00004171
4172 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00004173 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00004174 return;
4175
Chandler Carruth87c44602011-07-01 23:49:12 +00004176 if (!isa<ObjCMethodDecl>(D)) {
4177 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4178 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004179 return;
4180 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004181
Michael Han51d8c522013-01-24 16:46:58 +00004182 D->addAttr(::new (S.Context)
4183 RegparmAttr(Attr.getRange(), S.Context, numParams,
4184 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00004185}
4186
4187/// Checks a regparm attribute, returning true if it is ill-formed and
4188/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00004189bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
4190 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00004191 return true;
4192
Aaron Ballmanffa9d572013-07-18 18:01:48 +00004193 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004194 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004195 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004196 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004197
Aaron Ballman624421f2013-08-31 01:11:41 +00004198 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004199 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00004200 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00004201 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00004202 Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4203 << Attr.getName() << AANT_ArgumentIntegerConstant
4204 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004205 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004206 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004207 }
4208
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004209 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004210 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004211 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004212 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004213 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004214 }
4215
John McCall711c52b2011-01-05 12:14:39 +00004216 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004217 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004218 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004219 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004220 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004221 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004222 }
4223
John McCall711c52b2011-01-05 12:14:39 +00004224 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004225}
4226
Chandler Carruth1b03c872011-07-02 00:01:44 +00004227static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00004228 if (S.LangOpts.CUDA) {
4229 // check the attribute arguments.
4230 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00004231 // FIXME: 0 is not okay.
4232 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004233 return;
4234 }
4235
Chandler Carruth87c44602011-07-01 23:49:12 +00004236 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00004237 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00004238 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004239 return;
4240 }
4241
Aaron Ballman624421f2013-08-31 01:11:41 +00004242 Expr *MaxThreadsExpr = Attr.getArgAsExpr(0);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004243 llvm::APSInt MaxThreads(32);
4244 if (MaxThreadsExpr->isTypeDependent() ||
4245 MaxThreadsExpr->isValueDependent() ||
4246 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004247 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004248 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004249 << MaxThreadsExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004250 return;
4251 }
4252
4253 llvm::APSInt MinBlocks(32);
4254 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004255 Expr *MinBlocksExpr = Attr.getArgAsExpr(1);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004256 if (MinBlocksExpr->isTypeDependent() ||
4257 MinBlocksExpr->isValueDependent() ||
4258 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004259 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004260 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004261 << MinBlocksExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004262 return;
4263 }
4264 }
4265
Michael Han51d8c522013-01-24 16:46:58 +00004266 D->addAttr(::new (S.Context)
4267 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4268 MaxThreads.getZExtValue(),
4269 MinBlocks.getZExtValue(),
4270 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne7b381982010-12-12 23:03:07 +00004271 } else {
4272 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4273 }
4274}
4275
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004276static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4277 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004278 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004279 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004280 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004281 return;
4282 }
Aaron Ballman624421f2013-08-31 01:11:41 +00004283
4284 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004285 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004286
Aaron Ballman624421f2013-08-31 01:11:41 +00004287 StringRef AttrName = Attr.getName()->getName();
4288 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004289
4290 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4291 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4292 << Attr.getName() << ExpectedFunctionOrMethod;
4293 return;
4294 }
4295
4296 uint64_t ArgumentIdx;
4297 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4298 Attr.getLoc(), 2,
Aaron Ballman624421f2013-08-31 01:11:41 +00004299 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004300 return;
4301
4302 uint64_t TypeTagIdx;
4303 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4304 Attr.getLoc(), 3,
Aaron Ballman624421f2013-08-31 01:11:41 +00004305 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004306 return;
4307
4308 bool IsPointer = (AttrName == "pointer_with_type_tag");
4309 if (IsPointer) {
4310 // Ensure that buffer has a pointer type.
4311 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4312 if (!BufferTy->isPointerType()) {
4313 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004314 << Attr.getName();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004315 }
4316 }
4317
Michael Han51d8c522013-01-24 16:46:58 +00004318 D->addAttr(::new (S.Context)
4319 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4320 ArgumentIdx, TypeTagIdx, IsPointer,
4321 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004322}
4323
4324static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4325 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004326 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004327 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004328 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004329 return;
4330 }
Aaron Ballman624421f2013-08-31 01:11:41 +00004331
4332 if (!checkAttributeNumArgs(S, Attr, 1))
4333 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004334
Aaron Ballman624421f2013-08-31 01:11:41 +00004335 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004336 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4337
Michael Han51d8c522013-01-24 16:46:58 +00004338 D->addAttr(::new (S.Context)
4339 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4340 MatchingCType,
4341 Attr.getLayoutCompatible(),
4342 Attr.getMustBeNull(),
4343 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004344}
4345
Chris Lattner0744e5f2008-06-29 00:23:49 +00004346//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00004347// Checker-specific attribute handlers.
4348//===----------------------------------------------------------------------===//
4349
John McCallc7ad3812011-01-25 03:31:58 +00004350static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004351 return type->isDependentType() ||
4352 type->isObjCObjectPointerType() ||
4353 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00004354}
4355static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004356 return type->isDependentType() ||
4357 type->isPointerType() ||
4358 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00004359}
4360
Chandler Carruth1b03c872011-07-02 00:01:44 +00004361static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004362 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00004363 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004364 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004365 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00004366 return;
4367 }
4368
4369 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00004370 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00004371 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4372 cf = false;
4373 } else {
4374 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4375 cf = true;
4376 }
4377
4378 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004379 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004380 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00004381 return;
4382 }
4383
4384 if (cf)
Michael Han51d8c522013-01-24 16:46:58 +00004385 param->addAttr(::new (S.Context)
4386 CFConsumedAttr(Attr.getRange(), S.Context,
4387 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004388 else
Michael Han51d8c522013-01-24 16:46:58 +00004389 param->addAttr(::new (S.Context)
4390 NSConsumedAttr(Attr.getRange(), S.Context,
4391 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004392}
4393
Chandler Carruth1b03c872011-07-02 00:01:44 +00004394static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4395 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004396 if (!isa<ObjCMethodDecl>(D)) {
4397 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004398 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00004399 return;
4400 }
4401
Michael Han51d8c522013-01-24 16:46:58 +00004402 D->addAttr(::new (S.Context)
4403 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4404 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004405}
4406
Chandler Carruth1b03c872011-07-02 00:01:44 +00004407static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4408 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004409
John McCallc7ad3812011-01-25 03:31:58 +00004410 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00004411
Chandler Carruth87c44602011-07-01 23:49:12 +00004412 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004413 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004414 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00004415 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00004416 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00004417 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4418 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00004419 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004420 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004421 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00004422 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004423 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00004424 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004425 return;
4426 }
Mike Stumpbf916502009-07-24 19:02:52 +00004427
John McCallc7ad3812011-01-25 03:31:58 +00004428 bool typeOK;
4429 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00004430 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00004431 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004432 case AttributeList::AT_NSReturnsAutoreleased:
4433 case AttributeList::AT_NSReturnsRetained:
4434 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004435 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4436 cf = false;
4437 break;
4438
Sean Hunt8e083e72012-06-19 23:57:03 +00004439 case AttributeList::AT_CFReturnsRetained:
4440 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004441 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4442 cf = true;
4443 break;
4444 }
4445
4446 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004447 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004448 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00004449 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004450 }
Mike Stumpbf916502009-07-24 19:02:52 +00004451
Chandler Carruth87c44602011-07-01 23:49:12 +00004452 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004453 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00004454 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004455 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han51d8c522013-01-24 16:46:58 +00004456 D->addAttr(::new (S.Context)
4457 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4458 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004459 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004460 case AttributeList::AT_CFReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004461 D->addAttr(::new (S.Context)
4462 CFReturnsNotRetainedAttr(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_NSReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004466 D->addAttr(::new (S.Context)
4467 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4468 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004469 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004470 case AttributeList::AT_CFReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004471 D->addAttr(::new (S.Context)
4472 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4473 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004474 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004475 case AttributeList::AT_NSReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004476 D->addAttr(::new (S.Context)
4477 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4478 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004479 return;
4480 };
4481}
4482
John McCalldc7c5ad2011-07-22 08:53:00 +00004483static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4484 const AttributeList &attr) {
4485 SourceLocation loc = attr.getLoc();
4486
4487 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4488
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004489 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00004490 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004491 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00004492 return;
4493 }
4494
4495 // Check that the method returns a normal pointer.
4496 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004497
4498 if (!resultType->isReferenceType() &&
4499 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00004500 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4501 << SourceRange(loc)
4502 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4503
4504 // Drop the attribute.
4505 return;
4506 }
4507
Michael Han51d8c522013-01-24 16:46:58 +00004508 method->addAttr(::new (S.Context)
4509 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4510 attr.getAttributeSpellingListIndex()));
John McCalldc7c5ad2011-07-22 08:53:00 +00004511}
4512
Fariborz Jahanian84101132012-09-07 23:46:23 +00004513static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4514 const AttributeList &attr) {
4515 SourceLocation loc = attr.getLoc();
4516 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4517
4518 if (!method) {
4519 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4520 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4521 return;
4522 }
4523 DeclContext *DC = method->getDeclContext();
4524 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4525 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4526 << attr.getName() << 0;
4527 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4528 return;
4529 }
4530 if (method->getMethodFamily() == OMF_dealloc) {
4531 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4532 << attr.getName() << 1;
4533 return;
4534 }
4535
Michael Han51d8c522013-01-24 16:46:58 +00004536 method->addAttr(::new (S.Context)
4537 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4538 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian84101132012-09-07 23:46:23 +00004539}
4540
John McCall8dfac0b2011-09-30 05:12:12 +00004541/// Handle cf_audited_transfer and cf_unknown_transfer.
4542static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4543 if (!isa<FunctionDecl>(D)) {
4544 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004545 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004546 return;
4547 }
4548
Sean Hunt8e083e72012-06-19 23:57:03 +00004549 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004550
4551 // Check whether there's a conflicting attribute already present.
4552 Attr *Existing;
4553 if (IsAudited) {
4554 Existing = D->getAttr<CFUnknownTransferAttr>();
4555 } else {
4556 Existing = D->getAttr<CFAuditedTransferAttr>();
4557 }
4558 if (Existing) {
4559 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4560 << A.getName()
4561 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4562 << A.getRange() << Existing->getRange();
4563 return;
4564 }
4565
4566 // All clear; add the attribute.
4567 if (IsAudited) {
Michael Han51d8c522013-01-24 16:46:58 +00004568 D->addAttr(::new (S.Context)
4569 CFAuditedTransferAttr(A.getRange(), S.Context,
4570 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004571 } else {
Michael Han51d8c522013-01-24 16:46:58 +00004572 D->addAttr(::new (S.Context)
4573 CFUnknownTransferAttr(A.getRange(), S.Context,
4574 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004575 }
4576}
4577
John McCallfe98da02011-09-29 07:17:38 +00004578static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4579 const AttributeList &Attr) {
4580 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4581 if (!RD || RD->isUnion()) {
4582 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004583 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004584 }
4585
Aaron Ballman624421f2013-08-31 01:11:41 +00004586 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallfe98da02011-09-29 07:17:38 +00004587
4588 // In Objective-C, verify that the type names an Objective-C type.
4589 // We don't want to check this outside of ObjC because people sometimes
4590 // do crazy C declarations of Objective-C types.
Aaron Ballman624421f2013-08-31 01:11:41 +00004591 if (Parm && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004592 // Check for an existing type with this name.
Aaron Ballman624421f2013-08-31 01:11:41 +00004593 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallfe98da02011-09-29 07:17:38 +00004594 Sema::LookupOrdinaryName);
4595 if (S.LookupName(R, Sc)) {
4596 NamedDecl *Target = R.getFoundDecl();
4597 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4598 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4599 S.Diag(Target->getLocStart(), diag::note_declared_at);
4600 }
4601 }
4602 }
4603
Michael Han51d8c522013-01-24 16:46:58 +00004604 D->addAttr(::new (S.Context)
Aaron Ballman624421f2013-08-31 01:11:41 +00004605 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han51d8c522013-01-24 16:46:58 +00004606 Attr.getAttributeSpellingListIndex()));
John McCallfe98da02011-09-29 07:17:38 +00004607}
4608
Chandler Carruth1b03c872011-07-02 00:01:44 +00004609static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4610 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004611 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004612
Chandler Carruth87c44602011-07-01 23:49:12 +00004613 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004614 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004615}
4616
Chandler Carruth1b03c872011-07-02 00:01:44 +00004617static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4618 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004619 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004620 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004621 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004622 return;
4623 }
4624
Chandler Carruth87c44602011-07-01 23:49:12 +00004625 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004626 QualType type = vd->getType();
4627
4628 if (!type->isDependentType() &&
4629 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004630 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004631 << type;
4632 return;
4633 }
4634
4635 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4636
4637 // If we have no lifetime yet, check the lifetime we're presumably
4638 // going to infer.
4639 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4640 lifetime = type->getObjCARCImplicitLifetime();
4641
4642 switch (lifetime) {
4643 case Qualifiers::OCL_None:
4644 assert(type->isDependentType() &&
4645 "didn't infer lifetime for non-dependent type?");
4646 break;
4647
4648 case Qualifiers::OCL_Weak: // meaningful
4649 case Qualifiers::OCL_Strong: // meaningful
4650 break;
4651
4652 case Qualifiers::OCL_ExplicitNone:
4653 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004654 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004655 << (lifetime == Qualifiers::OCL_Autoreleasing);
4656 break;
4657 }
4658
Chandler Carruth87c44602011-07-01 23:49:12 +00004659 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004660 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4661 Attr.getAttributeSpellingListIndex()));
John McCallf85e1932011-06-15 23:02:42 +00004662}
4663
Francois Pichet11542142010-12-19 06:50:37 +00004664//===----------------------------------------------------------------------===//
4665// Microsoft specific attribute handlers.
4666//===----------------------------------------------------------------------===//
4667
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004668// Check if MS extensions or some other language extensions are enabled. If
4669// not, issue a diagnostic that the given attribute is unused.
4670static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4671 bool OtherExtension = false) {
4672 if (S.LangOpts.MicrosoftExt || OtherExtension)
4673 return true;
4674 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4675 return false;
4676}
4677
Chandler Carruth1b03c872011-07-02 00:01:44 +00004678static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004679 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4680 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004681
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004682 // check the attribute arguments.
4683 if (!checkAttributeNumArgs(S, Attr, 1))
4684 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004685
Aaron Ballman624421f2013-08-31 01:11:41 +00004686 Expr *Arg = Attr.getArgAsExpr(0);
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004687 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
4688 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004689 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4690 << Attr.getName() << AANT_ArgumentString;
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004691 return;
4692 }
Francois Pichetd3d3be92010-12-20 01:41:49 +00004693
David Majnemerbb0ed292013-08-09 08:56:20 +00004694 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4695 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004696 StringRef StrRef = Str->getString();
David Majnemerbb0ed292013-08-09 08:56:20 +00004697 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4698 StrRef = StrRef.drop_front().drop_back();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004699
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004700 // Validate GUID length.
David Majnemerbb0ed292013-08-09 08:56:20 +00004701 if (StrRef.size() != 36) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004702 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4703 return;
4704 }
Anders Carlssonf89e0422011-01-23 21:07:30 +00004705
David Majnemerbb0ed292013-08-09 08:56:20 +00004706 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004707 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemerbb0ed292013-08-09 08:56:20 +00004708 if (StrRef[i] != '-') {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004709 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4710 return;
4711 }
David Majnemerbb0ed292013-08-09 08:56:20 +00004712 } else if (!isHexDigit(StrRef[i])) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004713 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4714 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004715 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004716 }
Francois Pichet11542142010-12-19 06:50:37 +00004717
David Majnemerbb0ed292013-08-09 08:56:20 +00004718 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4719 Attr.getAttributeSpellingListIndex()));
Charles Davisf0122fe2010-02-16 18:27:26 +00004720}
4721
John McCallc052dbb2012-05-22 21:28:12 +00004722static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004723 if (!checkMicrosoftExt(S, Attr))
Nico Weber7b89ab72012-11-07 21:31:36 +00004724 return;
Nico Weber7b89ab72012-11-07 21:31:36 +00004725
4726 AttributeList::Kind Kind = Attr.getKind();
4727 if (Kind == AttributeList::AT_SingleInheritance)
4728 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004729 ::new (S.Context)
4730 SingleInheritanceAttr(Attr.getRange(), S.Context,
4731 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004732 else if (Kind == AttributeList::AT_MultipleInheritance)
4733 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004734 ::new (S.Context)
4735 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4736 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004737 else if (Kind == AttributeList::AT_VirtualInheritance)
4738 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004739 ::new (S.Context)
4740 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4741 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004742}
4743
4744static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004745 if (!checkMicrosoftExt(S, Attr))
4746 return;
4747
4748 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004749 if (Kind == AttributeList::AT_Win64)
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004750 D->addAttr(
4751 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4752 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004753}
4754
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004755static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004756 if (!checkMicrosoftExt(S, Attr))
4757 return;
4758 D->addAttr(::new (S.Context)
4759 ForceInlineAttr(Attr.getRange(), S.Context,
4760 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004761}
4762
Reid Klecknera7225342013-05-20 14:02:37 +00004763static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4764 if (!checkMicrosoftExt(S, Attr))
4765 return;
4766 // Check linkage after possibly merging declaratinos. See
4767 // checkAttributesAfterMerging().
4768 D->addAttr(::new (S.Context)
4769 SelectAnyAttr(Attr.getRange(), S.Context,
4770 Attr.getAttributeSpellingListIndex()));
4771}
4772
Ted Kremenekb71368d2009-05-09 02:44:38 +00004773//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004774// Top Level Sema Entry Points
4775//===----------------------------------------------------------------------===//
4776
Richard Smith4a97b8e2013-08-29 00:47:48 +00004777/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4778/// the attribute applies to decls. If the attribute is a type attribute, just
4779/// silently ignore it if a GNU attribute.
4780static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4781 const AttributeList &Attr,
4782 bool IncludeCXX11Attributes) {
4783 if (Attr.isInvalid())
4784 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00004785
Richard Smith4a97b8e2013-08-29 00:47:48 +00004786 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4787 // instead.
4788 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4789 return;
4790
Chris Lattner803d0802008-06-29 00:43:07 +00004791 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004792 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4793 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4794 case AttributeList::AT_IBOutletCollection:
4795 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004796 case AttributeList::AT_AddressSpace:
Sean Hunt8e083e72012-06-19 23:57:03 +00004797 case AttributeList::AT_ObjCGC:
4798 case AttributeList::AT_VectorSize:
4799 case AttributeList::AT_NeonVectorType:
4800 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004801 case AttributeList::AT_Ptr32:
4802 case AttributeList::AT_Ptr64:
4803 case AttributeList::AT_SPtr:
4804 case AttributeList::AT_UPtr:
Mike Stumpbf916502009-07-24 19:02:52 +00004805 // Ignore these, these are type attributes, handled by
4806 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004807 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004808 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4809 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4810 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4811 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004812 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004813 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004814 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004815 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004816 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4817 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4818 case AttributeList::AT_CarriesDependency:
Richard Smith3a2b7a12013-01-28 22:42:45 +00004819 handleDependencyAttr(S, scope, D, Attr);
4820 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004821 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4822 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4823 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004824 case AttributeList::AT_CXX11NoReturn:
4825 handleCXX11NoReturnAttr(S, D, Attr);
4826 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004827 case AttributeList::AT_Deprecated:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004828 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004829 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004830 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4831 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004832 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004833 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004834 case AttributeList::AT_MinSize:
4835 handleMinSizeAttr(S, D, Attr);
4836 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004837 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4838 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4839 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman19513232013-08-28 23:13:26 +00004840 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4841 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004842 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4843 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004844 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004845 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004846 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4847 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004848 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004849 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4850 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004851 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004852 case AttributeList::AT_ownership_returns:
4853 case AttributeList::AT_ownership_takes:
4854 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004855 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004856 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4857 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4858 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4859 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4860 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4861 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4862 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004863
Sean Hunt8e083e72012-06-19 23:57:03 +00004864 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004865 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004866 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004867 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004868
Sean Hunt8e083e72012-06-19 23:57:03 +00004869 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004870 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4871
Fariborz Jahanian84101132012-09-07 23:46:23 +00004872 case AttributeList::AT_ObjCRequiresSuper:
4873 handleObjCRequiresSuperAttr(S, D, Attr); break;
4874
Sean Hunt8e083e72012-06-19 23:57:03 +00004875 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004876 handleNSBridgedAttr(S, scope, D, Attr); break;
4877
Sean Hunt8e083e72012-06-19 23:57:03 +00004878 case AttributeList::AT_CFAuditedTransfer:
4879 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004880 handleCFTransferAttr(S, D, Attr); break;
4881
Ted Kremenekb71368d2009-05-09 02:44:38 +00004882 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004883 case AttributeList::AT_CFConsumed:
4884 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4885 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004886 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004887
Sean Hunt8e083e72012-06-19 23:57:03 +00004888 case AttributeList::AT_NSReturnsAutoreleased:
4889 case AttributeList::AT_NSReturnsNotRetained:
4890 case AttributeList::AT_CFReturnsNotRetained:
4891 case AttributeList::AT_NSReturnsRetained:
4892 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004893 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004894
Tanya Lattner0df579e2012-07-09 22:06:01 +00004895 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004896 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004897 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004898
Joey Gouly37453b92013-03-08 09:42:32 +00004899 case AttributeList::AT_VecTypeHint:
4900 handleVecTypeHint(S, D, Attr); break;
4901
Sean Hunt8e083e72012-06-19 23:57:03 +00004902 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004903 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004904
Sean Hunt8e083e72012-06-19 23:57:03 +00004905 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4906 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4907 case AttributeList::AT_Unavailable:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004908 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004909 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004910 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004911 handleArcWeakrefUnavailableAttr (S, D, Attr);
4912 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004913 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004914 handleObjCRootClassAttr(S, D, Attr);
4915 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004916 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004917 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004918 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004919 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4920 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004921 handleReturnsTwiceAttr(S, D, Attr);
4922 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004923 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld4c3d662013-02-20 01:54:26 +00004924 case AttributeList::AT_Visibility:
4925 handleVisibilityAttr(S, D, Attr, false);
4926 break;
4927 case AttributeList::AT_TypeVisibility:
4928 handleVisibilityAttr(S, D, Attr, true);
4929 break;
Lubos Lunak1d3ce652013-07-20 15:05:36 +00004930 case AttributeList::AT_WarnUnused:
4931 handleWarnUnusedAttr(S, D, Attr);
4932 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004933 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004934 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004935 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4936 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4937 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4938 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004939 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004940 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004941 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004942 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004943 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004944 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004945 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004946 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004947 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4948 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4949 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4950 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4951 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4952 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4953 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4954 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4955 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004956 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004957 // Just ignore
4958 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004959 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004960 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004961 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004962 case AttributeList::AT_StdCall:
4963 case AttributeList::AT_CDecl:
4964 case AttributeList::AT_FastCall:
4965 case AttributeList::AT_ThisCall:
4966 case AttributeList::AT_Pascal:
Charles Davise8519c32013-08-30 04:39:01 +00004967 case AttributeList::AT_MSABI:
4968 case AttributeList::AT_SysVABI:
Sean Hunt8e083e72012-06-19 23:57:03 +00004969 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004970 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004971 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004972 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004973 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004974 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004975 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004976 break;
Guy Benyei1db70402013-03-24 13:58:12 +00004977 case AttributeList::AT_OpenCLImageAccess:
4978 handleOpenCLImageAccessAttr(S, D, Attr);
4979 break;
John McCallc052dbb2012-05-22 21:28:12 +00004980
4981 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004982 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004983 handleMsStructAttr(S, D, Attr);
4984 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004985 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004986 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004987 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004988 case AttributeList::AT_SingleInheritance:
4989 case AttributeList::AT_MultipleInheritance:
4990 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004991 handleInheritanceAttr(S, D, Attr);
4992 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004993 case AttributeList::AT_Win64:
John McCallc052dbb2012-05-22 21:28:12 +00004994 handlePortabilityAttr(S, D, Attr);
4995 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004996 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004997 handleForceInlineAttr(S, D, Attr);
4998 break;
Reid Klecknera7225342013-05-20 14:02:37 +00004999 case AttributeList::AT_SelectAny:
5000 handleSelectAnyAttr(S, D, Attr);
5001 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005002
5003 // Thread safety attributes:
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +00005004 case AttributeList::AT_AssertExclusiveLock:
5005 handleAssertExclusiveLockAttr(S, D, Attr);
5006 break;
5007 case AttributeList::AT_AssertSharedLock:
5008 handleAssertSharedLockAttr(S, D, Attr);
5009 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005010 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005011 handleGuardedVarAttr(S, D, Attr);
5012 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005013 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00005014 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005015 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005016 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00005017 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005018 break;
Kostya Serebryany85aee962013-02-26 06:58:27 +00005019 case AttributeList::AT_NoSanitizeAddress:
5020 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany71efba02012-01-24 19:25:38 +00005021 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005022 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany85aee962013-02-26 06:58:27 +00005023 handleNoThreadSafetyAnalysis(S, D, Attr);
5024 break;
5025 case AttributeList::AT_NoSanitizeThread:
5026 handleNoSanitizeThread(S, D, Attr);
5027 break;
5028 case AttributeList::AT_NoSanitizeMemory:
5029 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005030 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005031 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005032 handleLockableAttr(S, D, Attr);
5033 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005034 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005035 handleGuardedByAttr(S, D, Attr);
5036 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005037 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00005038 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005039 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005040 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00005041 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005042 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005043 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00005044 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005045 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005046 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00005047 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005048 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005049 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005050 handleLockReturnedAttr(S, D, Attr);
5051 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005052 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005053 handleLocksExcludedAttr(S, D, Attr);
5054 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005055 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00005056 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005057 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005058 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00005059 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005060 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005061 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00005062 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005063 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005064 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005065 handleUnlockFunAttr(S, D, Attr);
5066 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005067 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00005068 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005069 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005070 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00005071 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005072 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005073
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00005074 // Uniqueness analysis attributes.
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00005075 case AttributeList::AT_Consumable:
5076 handleConsumableAttr(S, D, Attr);
5077 break;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00005078 case AttributeList::AT_Consumes:
5079 handleConsumesAttr(S, D, Attr);
5080 break;
5081 case AttributeList::AT_CallableWhenUnconsumed:
5082 handleCallableWhenUnconsumedAttr(S, D, Attr);
5083 break;
5084 case AttributeList::AT_TestsConsumed:
5085 handleTestsConsumedAttr(S, D, Attr);
5086 break;
5087 case AttributeList::AT_TestsUnconsumed:
5088 handleTestsUnconsumedAttr(S, D, Attr);
5089 break;
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00005090 case AttributeList::AT_ReturnTypestate:
5091 handleReturnTypestateAttr(S, D, Attr);
5092 break;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00005093
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00005094 // Type safety attributes.
5095 case AttributeList::AT_ArgumentWithTypeTag:
5096 handleArgumentWithTypeTagAttr(S, D, Attr);
5097 break;
5098 case AttributeList::AT_TypeTagForDatatype:
5099 handleTypeTagForDatatypeAttr(S, D, Attr);
5100 break;
5101
Chris Lattner803d0802008-06-29 00:43:07 +00005102 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005103 // Ask target about the attribute.
5104 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
5105 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00005106 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
5107 diag::warn_unhandled_ms_attribute_ignored :
5108 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00005109 break;
5110 }
5111}
5112
5113/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5114/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00005115void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00005116 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00005117 bool IncludeCXX11Attributes) {
5118 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005119 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005120
5121 // GCC accepts
5122 // static int a9 __attribute__((weakref));
5123 // but that looks really pointless. We reject it.
Richard Smith4a97b8e2013-08-29 00:47:48 +00005124 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005125 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00005126 cast<NamedDecl>(D)->getNameAsString();
5127 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005128 return;
Chris Lattner803d0802008-06-29 00:43:07 +00005129 }
5130}
5131
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005132// Annotation attributes are the only attributes allowed after an access
5133// specifier.
5134bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5135 const AttributeList *AttrList) {
5136 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00005137 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005138 handleAnnotateAttr(*this, ASDecl, *l);
5139 } else {
5140 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5141 return true;
5142 }
5143 }
5144
5145 return false;
5146}
5147
John McCalle82247a2011-10-01 05:17:03 +00005148/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5149/// contains any decl attributes that we should warn about.
5150static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5151 for ( ; A; A = A->getNext()) {
5152 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smithd03de6a2013-01-29 10:02:16 +00005153 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCalle82247a2011-10-01 05:17:03 +00005154 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5155
5156 if (A->getKind() == AttributeList::UnknownAttribute) {
5157 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5158 << A->getName() << A->getRange();
5159 } else {
5160 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5161 << A->getName() << A->getRange();
5162 }
5163 }
5164}
5165
5166/// checkUnusedDeclAttributes - Given a declarator which is not being
5167/// used to build a declaration, complain about any decl attributes
5168/// which might be lying around on it.
5169void Sema::checkUnusedDeclAttributes(Declarator &D) {
5170 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5171 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5172 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5173 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5174}
5175
Ryan Flynne25ff832009-07-30 03:15:39 +00005176/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00005177/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00005178NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5179 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005180 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00005181 NamedDecl *NewD = 0;
5182 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00005183 FunctionDecl *NewFD;
5184 // FIXME: Missing call to CheckFunctionDeclaration().
5185 // FIXME: Mangling?
5186 // FIXME: Is the qualifier info correct?
5187 // FIXME: Is the DeclContext correct?
5188 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5189 Loc, Loc, DeclarationName(II),
5190 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005191 SC_None, false/*isInlineSpecified*/,
Eli Friedman900693b2011-09-07 04:05:06 +00005192 FD->hasPrototype(),
5193 false/*isConstexprSpecified*/);
5194 NewD = NewFD;
5195
5196 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005197 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00005198
5199 // Fake up parameter variables; they are declared as if this were
5200 // a typedef.
5201 QualType FDTy = FD->getType();
5202 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5203 SmallVector<ParmVarDecl*, 16> Params;
5204 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5205 AE = FT->arg_type_end(); AI != AE; ++AI) {
5206 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5207 Param->setScopeInfo(0, Params.size());
5208 Params.push_back(Param);
5209 }
David Blaikie4278c652011-09-21 18:16:56 +00005210 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00005211 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005212 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5213 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005214 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00005215 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005216 VD->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00005217 if (VD->getQualifier()) {
5218 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005219 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00005220 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005221 }
5222 return NewD;
5223}
5224
James Dennett1dfbd922012-06-14 21:40:34 +00005225/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00005226/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005227void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005228 if (W.getUsed()) return; // only do this once
5229 W.setUsed(true);
5230 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5231 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00005232 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00005233 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5234 NDId->getName()));
5235 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005236 WeakTopLevelDecl.push_back(NewD);
5237 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5238 // to insert Decl at TU scope, sorry.
5239 DeclContext *SavedContext = CurContext;
5240 CurContext = Context.getTranslationUnitDecl();
5241 PushOnScopeChains(NewD, S);
5242 CurContext = SavedContext;
5243 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00005244 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00005245 }
5246}
5247
Rafael Espindola65611bf2013-03-02 21:41:48 +00005248void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5249 // It's valid to "forward-declare" #pragma weak, in which case we
5250 // have to do this.
5251 LoadExternalWeakUndeclaredIdentifiers();
5252 if (!WeakUndeclaredIdentifiers.empty()) {
5253 NamedDecl *ND = NULL;
5254 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5255 if (VD->isExternC())
5256 ND = VD;
5257 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5258 if (FD->isExternC())
5259 ND = FD;
5260 if (ND) {
5261 if (IdentifierInfo *Id = ND->getIdentifier()) {
5262 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5263 = WeakUndeclaredIdentifiers.find(Id);
5264 if (I != WeakUndeclaredIdentifiers.end()) {
5265 WeakInfo W = I->second;
5266 DeclApplyPragmaWeak(S, ND, W);
5267 WeakUndeclaredIdentifiers[Id] = W;
5268 }
5269 }
5270 }
5271 }
5272}
5273
Chris Lattner0744e5f2008-06-29 00:23:49 +00005274/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5275/// it, apply them to D. This is a bit tricky because PD can have attributes
5276/// specified in many different places, and we need to find and apply them all.
Richard Smith4a97b8e2013-08-29 00:47:48 +00005277void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner0744e5f2008-06-29 00:23:49 +00005278 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00005279 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005280 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00005281
Chris Lattner0744e5f2008-06-29 00:23:49 +00005282 // Walk the declarator structure, applying decl attributes that were in a type
5283 // position to the decl itself. This handles cases like:
5284 // int *__attr__(x)** D;
5285 // when X is a decl attribute.
5286 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5287 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005288 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00005289
Chris Lattner0744e5f2008-06-29 00:23:49 +00005290 // Finally, apply any attributes on the decl itself.
5291 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005292 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00005293}
John McCall54abf7d2009-11-04 02:18:39 +00005294
John McCallf85e1932011-06-15 23:02:42 +00005295/// Is the given declaration allowed to use a forbidden type?
5296static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5297 // Private ivars are always okay. Unfortunately, people don't
5298 // always properly make their ivars private, even in system headers.
5299 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00005300 // Function declarations in sys headers will be marked unavailable.
5301 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5302 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00005303 return false;
5304
5305 // Require it to be declared in a system header.
5306 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5307}
5308
5309/// Handle a delayed forbidden-type diagnostic.
5310static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5311 Decl *decl) {
5312 if (decl && isForbiddenTypeAllowed(S, decl)) {
5313 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5314 "this system declaration uses an unsupported type"));
5315 return;
5316 }
David Blaikie4e4d0842012-03-11 07:00:24 +00005317 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005318 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00005319 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005320 // kind of forbidden type messages on unavailable functions.
5321 if (FD->hasAttr<UnavailableAttr>() &&
5322 diag.getForbiddenTypeDiagnostic() ==
5323 diag::err_arc_array_param_no_ownership) {
5324 diag.Triggered = true;
5325 return;
5326 }
5327 }
John McCallf85e1932011-06-15 23:02:42 +00005328
5329 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5330 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5331 diag.Triggered = true;
5332}
5333
John McCall92576642012-05-07 06:16:41 +00005334void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5335 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00005336 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00005337 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00005338
John McCall92576642012-05-07 06:16:41 +00005339 // When delaying diagnostics to run in the context of a parsed
5340 // declaration, we only want to actually emit anything if parsing
5341 // succeeds.
5342 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00005343
John McCall92576642012-05-07 06:16:41 +00005344 // We emit all the active diagnostics in this pool or any of its
5345 // parents. In general, we'll get one pool for the decl spec
5346 // and a child pool for each declarator; in a decl group like:
5347 // deprecated_typedef foo, *bar, baz();
5348 // only the declarator pops will be passed decls. This is correct;
5349 // we really do need to consider delayed diagnostics from the decl spec
5350 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005351 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005352 do {
John McCall13489672012-05-07 06:16:58 +00005353 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005354 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5355 // This const_cast is a bit lame. Really, Triggered should be mutable.
5356 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005357 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005358 continue;
5359
John McCalleee1d542011-02-14 07:13:47 +00005360 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005361 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005362 // Don't bother giving deprecation diagnostics if the decl is invalid.
5363 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005364 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005365 break;
5366
5367 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005368 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005369 break;
John McCallf85e1932011-06-15 23:02:42 +00005370
5371 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005372 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005373 break;
John McCall2f514482010-01-27 03:50:35 +00005374 }
5375 }
John McCall92576642012-05-07 06:16:41 +00005376 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005377}
5378
John McCall13489672012-05-07 06:16:58 +00005379/// Given a set of delayed diagnostics, re-emit them as if they had
5380/// been delayed in the current context instead of in the given pool.
5381/// Essentially, this just moves them to the current pool.
5382void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5383 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5384 assert(curPool && "re-emitting in undelayed context not supported");
5385 curPool->steal(pool);
5386}
5387
John McCall54abf7d2009-11-04 02:18:39 +00005388static bool isDeclDeprecated(Decl *D) {
5389 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005390 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005391 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005392 // A category implicitly has the availability of the interface.
5393 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5394 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005395 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5396 return false;
5397}
5398
Eli Friedmanc3b23082012-08-08 21:52:41 +00005399static void
5400DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5401 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005402 const ObjCInterfaceDecl *UnknownObjCClass,
5403 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005404 DeclarationName Name = D->getDeclName();
5405 if (!Message.empty()) {
5406 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5407 S.Diag(D->getLocation(),
5408 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5409 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005410 if (ObjCPropery)
5411 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5412 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005413 } else if (!UnknownObjCClass) {
5414 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5415 S.Diag(D->getLocation(),
5416 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5417 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005418 if (ObjCPropery)
5419 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5420 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005421 } else {
5422 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5423 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5424 }
5425}
5426
John McCall9c3087b2010-08-26 02:13:20 +00005427void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005428 Decl *Ctx) {
5429 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005430 return;
5431
John McCall2f514482010-01-27 03:50:35 +00005432 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005433 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5434 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005435 DD.getUnknownObjCClass(),
5436 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005437}
5438
Chris Lattner5f9e2722011-07-23 10:55:15 +00005439void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005440 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005441 const ObjCInterfaceDecl *UnknownObjCClass,
5442 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005443 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005444 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005445 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5446 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005447 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005448 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005449 return;
5450 }
5451
5452 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005453 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005454 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005455 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005456}