blob: 39347fa39495b24949e00c7fe9cd5b22b4856872 [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
Joey Gouly96cead52013-03-14 09:54:43 +00002905static void handleEndianAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002906 if (!Attr.isArgIdent(0)) {
2907 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2908 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
2909 return;
2910 }
2911
2912 if (!checkAttributeNumArgs(S, Attr, 1))
2913 return;
2914
Joey Gouly96cead52013-03-14 09:54:43 +00002915 if (!dyn_cast<VarDecl>(D))
Aaron Ballman624421f2013-08-31 01:11:41 +00002916 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2917 << Attr.getName() << ExpectedVariable;
2918 StringRef EndianType = Attr.getArgAsIdent(0)->Ident->getName();
Joey Gouly96cead52013-03-14 09:54:43 +00002919 if (EndianType != "host" && EndianType != "device")
2920 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_endian) << EndianType;
2921}
2922
Rafael Espindola599f1b72012-05-13 03:25:18 +00002923SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002924 StringRef Name,
2925 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002926 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2927 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002928 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002929 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2930 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002931 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002932 }
Michael Han51d8c522013-01-24 16:46:58 +00002933 return ::new (Context) SectionAttr(Range, Context, Name,
2934 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002935}
2936
Chandler Carruth1b03c872011-07-02 00:01:44 +00002937static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002938 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002939 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002940 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002941
2942 // Make sure that there is a string literal as the sections's single
2943 // argument.
Aaron Ballman624421f2013-08-31 01:11:41 +00002944 Expr *ArgExpr = Attr.getArgAsExpr(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002945 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002946 if (!SE) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002947 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
2948 << Attr.getName() << AANT_ArgumentString;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002949 return;
2950 }
Mike Stump1eb44332009-09-09 15:08:12 +00002951
Chris Lattner797c3c42009-08-10 19:03:04 +00002952 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002953 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002954 if (!Error.empty()) {
2955 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2956 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002957 return;
2958 }
Mike Stump1eb44332009-09-09 15:08:12 +00002959
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002960 // This attribute cannot be applied to local variables.
2961 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2962 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2963 return;
2964 }
Michael Han51d8c522013-01-24 16:46:58 +00002965
2966 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindola599f1b72012-05-13 03:25:18 +00002967 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han51d8c522013-01-24 16:46:58 +00002968 SE->getString(), Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002969 if (NewAttr)
2970 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002971}
2972
Chris Lattner6b6b5372008-06-26 18:38:35 +00002973
Chandler Carruth1b03c872011-07-02 00:01:44 +00002974static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002975 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002976 return;
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002977
Chandler Carruth87c44602011-07-01 23:49:12 +00002978 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002979 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002980 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002981 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002982 D->addAttr(::new (S.Context)
2983 NoThrowAttr(Attr.getRange(), S.Context,
2984 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002985 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002986}
2987
Chandler Carruth1b03c872011-07-02 00:01:44 +00002988static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002989 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002990 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002991
Chandler Carruth87c44602011-07-01 23:49:12 +00002992 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002993 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002994 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002995 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002996 D->addAttr(::new (S.Context)
2997 ConstAttr(Attr.getRange(), S.Context,
2998 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002999 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00003000}
3001
Chandler Carruth1b03c872011-07-02 00:01:44 +00003002static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00003003 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003004 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00003005 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003006
Michael Han51d8c522013-01-24 16:46:58 +00003007 D->addAttr(::new (S.Context)
3008 PureAttr(Attr.getRange(), S.Context,
3009 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00003010}
3011
Chandler Carruth1b03c872011-07-02 00:01:44 +00003012static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003013 if (!Attr.isArgIdent(0)) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003014 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3015 << Attr.getName() << 1;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003016 return;
3017 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003018
3019 if (!checkAttributeNumArgs(S, Attr, 1))
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003020 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003021
Chandler Carruth87c44602011-07-01 23:49:12 +00003022 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00003023
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003024 if (!VD || !VD->hasLocalStorage()) {
3025 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
3026 return;
3027 }
Mike Stumpbf916502009-07-24 19:02:52 +00003028
Aaron Ballman624421f2013-08-31 01:11:41 +00003029 IdentifierLoc *IL = Attr.getArgAsIdent(0);
3030
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003031 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00003032 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00003033 NamedDecl *CleanupDecl
Aaron Ballman624421f2013-08-31 01:11:41 +00003034 = S.LookupSingleName(S.TUScope, IL->Ident, IL->Loc,
3035 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003036 if (!CleanupDecl) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003037 S.Diag(IL->Loc, diag::err_attribute_cleanup_arg_not_found) << IL->Ident;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003038 return;
3039 }
Mike Stumpbf916502009-07-24 19:02:52 +00003040
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003041 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
3042 if (!FD) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003043 S.Diag(IL->Loc, diag::err_attribute_cleanup_arg_not_function) << IL->Ident;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003044 return;
3045 }
3046
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003047 if (FD->getNumParams() != 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003048 S.Diag(IL->Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
3049 << IL->Ident;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003050 return;
3051 }
Mike Stumpbf916502009-07-24 19:02:52 +00003052
Anders Carlsson89941c12009-02-07 23:16:50 +00003053 // We're currently more strict than GCC about what function types we accept.
3054 // If this ever proves to be a problem it should be easy to fix.
3055 QualType Ty = S.Context.getPointerType(VD->getType());
3056 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00003057 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
3058 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003059 S.Diag(IL->Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) <<
3060 IL->Ident << ParamTy << Ty;
Anders Carlsson89941c12009-02-07 23:16:50 +00003061 return;
3062 }
Mike Stumpbf916502009-07-24 19:02:52 +00003063
Michael Han51d8c522013-01-24 16:46:58 +00003064 D->addAttr(::new (S.Context)
3065 CleanupAttr(Attr.getRange(), S.Context, FD,
3066 Attr.getAttributeSpellingListIndex()));
Aaron Ballman624421f2013-08-31 01:11:41 +00003067 S.MarkFunctionReferenced(IL->Loc, FD);
3068 S.DiagnoseUseOfDecl(FD, IL->Loc);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00003069}
3070
Mike Stumpbf916502009-07-24 19:02:52 +00003071/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003072/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003073static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00003074 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003075 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003076
Chandler Carruth87c44602011-07-01 23:49:12 +00003077 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003078 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003079 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003080 return;
3081 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003082
Aaron Ballman624421f2013-08-31 01:11:41 +00003083 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00003084 uint64_t ArgIdx;
3085 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
3086 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003087 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003088
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003089 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003090 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00003091
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003092 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
3093 if (not_nsstring_type &&
3094 !isCFStringType(Ty, S.Context) &&
3095 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003096 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003097 // FIXME: Should highlight the actual expression that has the wrong type.
3098 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00003099 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003100 << IdxExpr->getSourceRange();
3101 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003102 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003103 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003104 if (!isNSStringType(Ty, S.Context) &&
3105 !isCFStringType(Ty, S.Context) &&
3106 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003107 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003108 // FIXME: Should highlight the actual expression that has the wrong type.
3109 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00003110 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003111 << IdxExpr->getSourceRange();
3112 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003113 }
3114
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00003115 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
3116 // because that has corrected for the implicit this parameter, and is zero-
3117 // based. The attribute expects what the user wrote explicitly.
3118 llvm::APSInt Val;
3119 IdxExpr->EvaluateAsInt(Val, S.Context);
3120
Michael Han51d8c522013-01-24 16:46:58 +00003121 D->addAttr(::new (S.Context)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00003122 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003123 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00003124}
3125
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003126enum FormatAttrKind {
3127 CFStringFormat,
3128 NSStringFormat,
3129 StrftimeFormat,
3130 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00003131 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003132 InvalidFormat
3133};
3134
3135/// getFormatAttrKind - Map from format attribute names to supported format
3136/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003137static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003138 return llvm::StringSwitch<FormatAttrKind>(Format)
3139 // Check for formats that get handled specially.
3140 .Case("NSString", NSStringFormat)
3141 .Case("CFString", CFStringFormat)
3142 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003143
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003144 // Otherwise, check for supported formats.
3145 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3146 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3147 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003148
Benjamin Kramerc51bb992012-05-16 12:44:25 +00003149 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3150 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003151}
3152
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003153/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003154/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003155static void handleInitPriorityAttr(Sema &S, Decl *D,
3156 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003157 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003158 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
3159 return;
3160 }
3161
Chandler Carruth87c44602011-07-01 23:49:12 +00003162 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003163 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3164 Attr.setInvalid();
3165 return;
3166 }
Chandler Carruth87c44602011-07-01 23:49:12 +00003167 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003168 if (S.Context.getAsArrayType(T))
3169 T = S.Context.getBaseElementType(T);
3170 if (!T->getAs<RecordType>()) {
3171 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
3172 Attr.setInvalid();
3173 return;
3174 }
3175
Aaron Ballmanffa9d572013-07-18 18:01:48 +00003176 if (!checkAttributeNumArgs(S, Attr, 1)) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003177 Attr.setInvalid();
3178 return;
3179 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003180 Expr *priorityExpr = Attr.getArgAsExpr(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00003181
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003182 llvm::APSInt priority(32);
3183 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
3184 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003185 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3186 << Attr.getName() << AANT_ArgumentIntegerConstant
3187 << priorityExpr->getSourceRange();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003188 Attr.setInvalid();
3189 return;
3190 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00003191 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003192 if (prioritynum < 101 || prioritynum > 65535) {
3193 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
3194 << priorityExpr->getSourceRange();
3195 Attr.setInvalid();
3196 return;
3197 }
Michael Han51d8c522013-01-24 16:46:58 +00003198 D->addAttr(::new (S.Context)
3199 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
3200 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003201}
3202
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003203FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
3204 IdentifierInfo *Format, int FormatIdx,
3205 int FirstArg,
Michael Han51d8c522013-01-24 16:46:58 +00003206 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003207 // Check whether we already have an equivalent format attribute.
3208 for (specific_attr_iterator<FormatAttr>
3209 i = D->specific_attr_begin<FormatAttr>(),
3210 e = D->specific_attr_end<FormatAttr>();
3211 i != e ; ++i) {
3212 FormatAttr *f = *i;
3213 if (f->getType() == Format &&
3214 f->getFormatIdx() == FormatIdx &&
3215 f->getFirstArg() == FirstArg) {
3216 // If we don't have a valid location for this attribute, adopt the
3217 // location.
3218 if (f->getLocation().isInvalid())
3219 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00003220 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003221 }
3222 }
3223
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003224 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3225 FirstArg, AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003226}
3227
Mike Stumpbf916502009-07-24 19:02:52 +00003228/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003229/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003230static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003231 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003232 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00003233 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003234 return;
3235 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003236
3237 if (!checkAttributeNumArgs(S, Attr, 3))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003238 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003239
Chandler Carruth87c44602011-07-01 23:49:12 +00003240 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003241 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003242 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003243 return;
3244 }
3245
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003246 // In C++ the implicit 'this' function parameter also counts, and they are
3247 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003248 bool HasImplicitThisParam = isInstanceMethod(D);
3249 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003250 unsigned FirstIdx = 1;
3251
Aaron Ballman624421f2013-08-31 01:11:41 +00003252 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
3253 StringRef Format = II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003254
3255 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003256 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003257 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003258 // If we've modified the string name, we need a new identifier for it.
3259 II = &S.Context.Idents.get(Format);
3260 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003261
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003262 // Check for supported formats.
3263 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003264
3265 if (Kind == IgnoredFormat)
3266 return;
3267
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003268 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003269 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman624421f2013-08-31 01:11:41 +00003270 << "format" << II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003271 return;
3272 }
3273
3274 // checks for the 2nd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003275 Expr *IdxExpr = Attr.getArgAsExpr(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003276 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003277 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3278 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003279 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003280 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003281 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003282 return;
3283 }
3284
3285 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003286 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003287 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003288 return;
3289 }
3290
3291 // FIXME: Do we need to bounds check?
3292 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003293
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003294 if (HasImplicitThisParam) {
3295 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003296 S.Diag(Attr.getLoc(),
3297 diag::err_format_attribute_implicit_this_format_string)
3298 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003299 return;
3300 }
3301 ArgIdx--;
3302 }
Mike Stump1eb44332009-09-09 15:08:12 +00003303
Chris Lattner6b6b5372008-06-26 18:38:35 +00003304 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003305 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003306
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003307 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003308 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003309 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3310 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003311 return;
3312 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003313 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003314 // FIXME: do we need to check if the type is NSString*? What are the
3315 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003316 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003317 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003318 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3319 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003320 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003321 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003322 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003323 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003324 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003325 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3326 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003327 return;
3328 }
3329
3330 // check the 3rd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003331 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Chris Lattner803d0802008-06-29 00:43:07 +00003332 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003333 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3334 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003335 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003336 << Attr.getName() << 3 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003337 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003338 return;
3339 }
3340
3341 // check if the function is variadic if the 3rd argument non-zero
3342 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003343 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003344 ++NumArgs; // +1 for ...
3345 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003346 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003347 return;
3348 }
3349 }
3350
Chris Lattner3c73c412008-11-19 08:23:25 +00003351 // strftime requires FirstArg to be 0 because it doesn't read from any
3352 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003353 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003354 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003355 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3356 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003357 return;
3358 }
3359 // if 0 it disables parameter checking (to use with e.g. va_list)
3360 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003361 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003362 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003363 return;
3364 }
3365
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003366 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Rafael Espindola599f1b72012-05-13 03:25:18 +00003367 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003368 FirstArg.getZExtValue(),
3369 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003370 if (NewAttr)
3371 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003372}
3373
Chandler Carruth1b03c872011-07-02 00:01:44 +00003374static void handleTransparentUnionAttr(Sema &S, Decl *D,
3375 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003376 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003377 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003378 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003379
Chris Lattner6b6b5372008-06-26 18:38:35 +00003380
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003381 // Try to find the underlying union declaration.
3382 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003383 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003384 if (TD && TD->getUnderlyingType()->isUnionType())
3385 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3386 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003387 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003388
3389 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003390 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003391 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003392 return;
3393 }
3394
John McCall5e1cdac2011-10-07 06:10:15 +00003395 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003396 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003397 diag::warn_transparent_union_attribute_not_definition);
3398 return;
3399 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003400
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003401 RecordDecl::field_iterator Field = RD->field_begin(),
3402 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003403 if (Field == FieldEnd) {
3404 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3405 return;
3406 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003407
David Blaikie581deb32012-06-06 20:45:41 +00003408 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003409 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003410 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003411 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003412 diag::warn_transparent_union_attribute_floating)
3413 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003414 return;
3415 }
3416
3417 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3418 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3419 for (; Field != FieldEnd; ++Field) {
3420 QualType FieldType = Field->getType();
3421 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3422 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3423 // Warn if we drop the attribute.
3424 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003425 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003426 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003427 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003428 diag::warn_transparent_union_attribute_field_size_align)
3429 << isSize << Field->getDeclName() << FieldBits;
3430 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003431 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003432 diag::note_transparent_union_first_field_size_align)
3433 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003434 return;
3435 }
3436 }
3437
Michael Han51d8c522013-01-24 16:46:58 +00003438 RD->addAttr(::new (S.Context)
3439 TransparentUnionAttr(Attr.getRange(), S.Context,
3440 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003441}
3442
Chandler Carruth1b03c872011-07-02 00:01:44 +00003443static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003444 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003445 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00003446 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003447
Aaron Ballman624421f2013-08-31 01:11:41 +00003448 Expr *ArgExpr = Attr.getArgAsExpr(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003449 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003450
Chris Lattner6b6b5372008-06-26 18:38:35 +00003451 // Make sure that there is a string literal as the annotation's single
3452 // argument.
3453 if (!SE) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003454 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
3455 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003456 return;
3457 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003458
3459 // Don't duplicate annotations that are already set.
3460 for (specific_attr_iterator<AnnotateAttr>
3461 i = D->specific_attr_begin<AnnotateAttr>(),
3462 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3463 if ((*i)->getAnnotation() == SE->getString())
3464 return;
3465 }
Michael Han51d8c522013-01-24 16:46:58 +00003466
3467 D->addAttr(::new (S.Context)
3468 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3469 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003470}
3471
Chandler Carruth1b03c872011-07-02 00:01:44 +00003472static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003473 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003474 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003475 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3476 << Attr.getName() << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003477 return;
3478 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003479
Richard Smithbe507b62013-02-01 08:12:08 +00003480 if (Attr.getNumArgs() == 0) {
3481 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3482 true, 0, Attr.getAttributeSpellingListIndex()));
3483 return;
3484 }
3485
Aaron Ballman624421f2013-08-31 01:11:41 +00003486 Expr *E = Attr.getArgAsExpr(0);
Richard Smithf6565a92013-02-22 08:32:16 +00003487 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3488 S.Diag(Attr.getEllipsisLoc(),
3489 diag::err_pack_expansion_without_parameter_packs);
3490 return;
3491 }
3492
3493 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3494 return;
3495
3496 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3497 Attr.isPackExpansion());
Richard Smithbe507b62013-02-01 08:12:08 +00003498}
3499
3500void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smithf6565a92013-02-22 08:32:16 +00003501 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smithbe507b62013-02-01 08:12:08 +00003502 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3503 SourceLocation AttrLoc = AttrRange.getBegin();
3504
Richard Smith4cd81c52013-01-29 09:02:09 +00003505 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smithbe507b62013-02-01 08:12:08 +00003506 if (TmpAttr.isAlignas()) {
Richard Smith4cd81c52013-01-29 09:02:09 +00003507 // C++11 [dcl.align]p1:
3508 // An alignment-specifier may be applied to a variable or to a class
3509 // data member, but it shall not be applied to a bit-field, a function
3510 // parameter, the formal parameter of a catch clause, or a variable
3511 // declared with the register storage class specifier. An
3512 // alignment-specifier may also be applied to the declaration of a class
3513 // or enumeration type.
3514 // C11 6.7.5/2:
3515 // An alignment attribute shall not be specified in a declaration of
3516 // a typedef, or a bit-field, or a function, or a parameter, or an
3517 // object declared with the register storage-class specifier.
3518 int DiagKind = -1;
3519 if (isa<ParmVarDecl>(D)) {
3520 DiagKind = 0;
3521 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3522 if (VD->getStorageClass() == SC_Register)
3523 DiagKind = 1;
3524 if (VD->isExceptionVariable())
3525 DiagKind = 2;
3526 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3527 if (FD->isBitField())
3528 DiagKind = 3;
3529 } else if (!isa<TagDecl>(D)) {
Richard Smithbe507b62013-02-01 08:12:08 +00003530 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3531 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith5f838aa2013-02-01 08:25:07 +00003532 << (TmpAttr.isC11() ? ExpectedVariableOrField
3533 : ExpectedVariableFieldOrTag);
Richard Smith4cd81c52013-01-29 09:02:09 +00003534 return;
3535 }
3536 if (DiagKind != -1) {
Richard Smithbe507b62013-02-01 08:12:08 +00003537 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smith671b3212013-02-22 04:55:39 +00003538 << TmpAttr.isC11() << DiagKind;
Richard Smith4cd81c52013-01-29 09:02:09 +00003539 return;
3540 }
3541 }
3542
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003543 if (E->isTypeDependent() || E->isValueDependent()) {
3544 // Save dependent expressions in the AST to be instantiated.
Richard Smithf6565a92013-02-22 08:32:16 +00003545 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3546 AA->setPackExpansion(IsPackExpansion);
3547 D->addAttr(AA);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003548 return;
3549 }
Michael Hana31f65b2013-02-01 01:19:17 +00003550
Sean Huntcf807c42010-08-18 23:23:40 +00003551 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003552 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003553 ExprResult ICE
3554 = VerifyIntegerConstantExpression(E, &Alignment,
3555 diag::err_aligned_attribute_argument_not_int,
3556 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003557 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003558 return;
Richard Smithbe507b62013-02-01 08:12:08 +00003559
3560 // C++11 [dcl.align]p2:
3561 // -- if the constant expression evaluates to zero, the alignment
3562 // specifier shall have no effect
3563 // C11 6.7.5p6:
3564 // An alignment specification of zero has no effect.
3565 if (!(TmpAttr.isAlignas() && !Alignment) &&
3566 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003567 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3568 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003569 return;
3570 }
Michael Hana31f65b2013-02-01 01:19:17 +00003571
Richard Smithbe507b62013-02-01 08:12:08 +00003572 if (TmpAttr.isDeclspec()) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003573 // We've already verified it's a power of 2, now let's make sure it's
3574 // 8192 or less.
3575 if (Alignment.getZExtValue() > 8192) {
Michael Hana31f65b2013-02-01 01:19:17 +00003576 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003577 << E->getSourceRange();
3578 return;
3579 }
3580 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003581
Richard Smithf6565a92013-02-22 08:32:16 +00003582 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3583 ICE.take(), SpellingListIndex);
3584 AA->setPackExpansion(IsPackExpansion);
3585 D->addAttr(AA);
Sean Huntcf807c42010-08-18 23:23:40 +00003586}
3587
Michael Hana31f65b2013-02-01 01:19:17 +00003588void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smithf6565a92013-02-22 08:32:16 +00003589 unsigned SpellingListIndex, bool IsPackExpansion) {
Sean Huntcf807c42010-08-18 23:23:40 +00003590 // FIXME: Cache the number on the Attr object if non-dependent?
3591 // FIXME: Perform checking of type validity
Richard Smithf6565a92013-02-22 08:32:16 +00003592 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3593 SpellingListIndex);
3594 AA->setPackExpansion(IsPackExpansion);
3595 D->addAttr(AA);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003596}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003597
Richard Smithbe507b62013-02-01 08:12:08 +00003598void Sema::CheckAlignasUnderalignment(Decl *D) {
3599 assert(D->hasAttrs() && "no attributes on decl");
3600
3601 QualType Ty;
3602 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3603 Ty = VD->getType();
3604 else
3605 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith4da09032013-02-22 09:21:42 +00003606 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smithbe507b62013-02-01 08:12:08 +00003607 return;
3608
3609 // C++11 [dcl.align]p5, C11 6.7.5/4:
3610 // The combined effect of all alignment attributes in a declaration shall
3611 // not specify an alignment that is less strict than the alignment that
3612 // would otherwise be required for the entity being declared.
3613 AlignedAttr *AlignasAttr = 0;
3614 unsigned Align = 0;
3615 for (specific_attr_iterator<AlignedAttr>
3616 I = D->specific_attr_begin<AlignedAttr>(),
3617 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3618 if (I->isAlignmentDependent())
3619 return;
3620 if (I->isAlignas())
3621 AlignasAttr = *I;
3622 Align = std::max(Align, I->getAlignment(Context));
3623 }
3624
3625 if (AlignasAttr && Align) {
3626 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3627 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3628 if (NaturalAlign > RequestedAlign)
3629 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3630 << Ty << (unsigned)NaturalAlign.getQuantity();
3631 }
3632}
3633
Chandler Carruthd309c812011-07-01 23:49:16 +00003634/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003635/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003636///
Mike Stumpbf916502009-07-24 19:02:52 +00003637/// Despite what would be logical, the mode attribute is a decl attribute, not a
3638/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3639/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003640static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003641 // This attribute isn't documented, but glibc uses it. It changes
3642 // the width of an int or unsigned int to the specified size.
Aaron Ballman624421f2013-08-31 01:11:41 +00003643 if (!Attr.isArgIdent(0)) {
Aaron Ballman750f73a2013-07-30 14:29:12 +00003644 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3645 << AANT_ArgumentIdentifier;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003646 return;
3647 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003648
3649 if (!checkAttributeNumArgs(S, Attr, 1))
3650 return;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003651
Aaron Ballman624421f2013-08-31 01:11:41 +00003652 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3653 StringRef Str = Name->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003654
3655 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003656 if (Str.startswith("__") && Str.endswith("__"))
3657 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003658
3659 unsigned DestWidth = 0;
3660 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003661 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003662 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003663 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003664 switch (Str[0]) {
3665 case 'Q': DestWidth = 8; break;
3666 case 'H': DestWidth = 16; break;
3667 case 'S': DestWidth = 32; break;
3668 case 'D': DestWidth = 64; break;
3669 case 'X': DestWidth = 96; break;
3670 case 'T': DestWidth = 128; break;
3671 }
3672 if (Str[1] == 'F') {
3673 IntegerMode = false;
3674 } else if (Str[1] == 'C') {
3675 IntegerMode = false;
3676 ComplexMode = true;
3677 } else if (Str[1] != 'I') {
3678 DestWidth = 0;
3679 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003680 break;
3681 case 4:
3682 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3683 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003684 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003685 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003686 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003687 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003688 break;
3689 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003690 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003691 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003692 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003693 case 11:
3694 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003695 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003696 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003697 }
3698
3699 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003700 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003701 OldTy = TD->getUnderlyingType();
3702 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3703 OldTy = VD->getType();
3704 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003705 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003706 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003707 return;
3708 }
Eli Friedman73397492009-03-03 06:41:03 +00003709
John McCall183700f2009-09-21 23:43:11 +00003710 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003711 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3712 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003713 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003714 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3715 } else if (ComplexMode) {
3716 if (!OldTy->isComplexType())
3717 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3718 } else {
3719 if (!OldTy->isFloatingType())
3720 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3721 }
3722
Mike Stump390b4cc2009-05-16 07:39:55 +00003723 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3724 // and friends, at least with glibc.
3725 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3726 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003727 // FIXME: Make sure floating-point mappings are accurate
3728 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00003729 QualType NewTy;
3730 switch (DestWidth) {
3731 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003732 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003733 return;
3734 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003735 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003736 return;
3737 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00003738 if (!IntegerMode) {
3739 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3740 return;
3741 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003742 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003743 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003744 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003745 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003746 break;
3747 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00003748 if (!IntegerMode) {
3749 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3750 return;
3751 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003752 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003753 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003754 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003755 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003756 break;
3757 case 32:
3758 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003759 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003760 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003761 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003762 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003763 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003764 break;
3765 case 64:
3766 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00003767 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003768 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003769 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003770 NewTy = S.Context.LongTy;
3771 else
3772 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003773 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003774 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00003775 NewTy = S.Context.UnsignedLongTy;
3776 else
3777 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003778 break;
Eli Friedman73397492009-03-03 06:41:03 +00003779 case 96:
3780 NewTy = S.Context.LongDoubleTy;
3781 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003782 case 128:
Roman Divacky19645542013-07-03 21:08:41 +00003783 if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() !=
3784 &llvm::APFloat::PPCDoubleDouble) {
Eli Friedmanf98aba32009-02-13 02:31:07 +00003785 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3786 return;
3787 }
Roman Divackyf0d14cb2013-07-03 20:48:06 +00003788 if (IntegerMode) {
3789 if (OldTy->isSignedIntegerType())
3790 NewTy = S.Context.Int128Ty;
3791 else
3792 NewTy = S.Context.UnsignedInt128Ty;
3793 } else
3794 NewTy = S.Context.LongDoubleTy;
Eli Friedman73397492009-03-03 06:41:03 +00003795 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003796 }
3797
Eli Friedman73397492009-03-03 06:41:03 +00003798 if (ComplexMode) {
3799 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003800 }
3801
3802 // Install the new type.
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003803 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3804 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3805 else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003806 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003807
3808 D->addAttr(::new (S.Context)
3809 ModeAttr(Attr.getRange(), S.Context, Name,
3810 Attr.getAttributeSpellingListIndex()));
Chris Lattnerfbf13472008-06-27 22:18:37 +00003811}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003812
Chandler Carruth1b03c872011-07-02 00:01:44 +00003813static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00003814 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003815 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00003816 return;
Anders Carlssone896d982009-02-13 08:11:52 +00003817
Nick Lewycky78d1a102012-07-24 01:40:49 +00003818 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3819 if (!VD->hasGlobalStorage())
3820 S.Diag(Attr.getLoc(),
3821 diag::warn_attribute_requires_functions_or_static_globals)
3822 << Attr.getName();
3823 } else if (!isFunctionOrMethod(D)) {
3824 S.Diag(Attr.getLoc(),
3825 diag::warn_attribute_requires_functions_or_static_globals)
3826 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003827 return;
3828 }
Mike Stumpbf916502009-07-24 19:02:52 +00003829
Michael Han51d8c522013-01-24 16:46:58 +00003830 D->addAttr(::new (S.Context)
3831 NoDebugAttr(Attr.getRange(), S.Context,
3832 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003833}
3834
Chandler Carruth1b03c872011-07-02 00:01:44 +00003835static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003836 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003837 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00003838 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003839
Mike Stumpbf916502009-07-24 19:02:52 +00003840
Chandler Carruth87c44602011-07-01 23:49:12 +00003841 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003842 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003843 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003844 return;
3845 }
Mike Stumpbf916502009-07-24 19:02:52 +00003846
Michael Han51d8c522013-01-24 16:46:58 +00003847 D->addAttr(::new (S.Context)
3848 NoInlineAttr(Attr.getRange(), S.Context,
3849 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003850}
3851
Chandler Carruth1b03c872011-07-02 00:01:44 +00003852static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3853 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003854 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003855 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00003856 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003857
Chris Lattner7255a2d2010-06-22 00:03:40 +00003858
Chandler Carruth87c44602011-07-01 23:49:12 +00003859 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003860 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003861 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003862 return;
3863 }
3864
Michael Han51d8c522013-01-24 16:46:58 +00003865 D->addAttr(::new (S.Context)
3866 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3867 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003868}
3869
Chandler Carruth1b03c872011-07-02 00:01:44 +00003870static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003871 if (S.LangOpts.CUDA) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003872 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003873 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003874
Chandler Carruth87c44602011-07-01 23:49:12 +00003875 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003876 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003877 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003878 return;
3879 }
3880
Michael Han51d8c522013-01-24 16:46:58 +00003881 D->addAttr(::new (S.Context)
3882 CUDAConstantAttr(Attr.getRange(), S.Context,
3883 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003884 } else {
3885 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3886 }
3887}
3888
Chandler Carruth1b03c872011-07-02 00:01:44 +00003889static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003890 if (S.LangOpts.CUDA) {
3891 // check the attribute arguments.
3892 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003893 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3894 << Attr.getName() << 0;
Peter Collingbourneced76712010-12-01 03:15:31 +00003895 return;
3896 }
3897
Chandler Carruth87c44602011-07-01 23:49:12 +00003898 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003899 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003900 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003901 return;
3902 }
3903
Michael Han51d8c522013-01-24 16:46:58 +00003904 D->addAttr(::new (S.Context)
3905 CUDADeviceAttr(Attr.getRange(), S.Context,
3906 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003907 } else {
3908 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3909 }
3910}
3911
Chandler Carruth1b03c872011-07-02 00:01:44 +00003912static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003913 if (S.LangOpts.CUDA) {
3914 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003915 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003916 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00003917
Chandler Carruth87c44602011-07-01 23:49:12 +00003918 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003919 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003920 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003921 return;
3922 }
3923
Chandler Carruth87c44602011-07-01 23:49:12 +00003924 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003925 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003926 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00003927 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003928 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3929 << FD->getType()
David Blaikie39e6ab42013-02-18 22:06:02 +00003930 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003931 "void");
3932 } else {
3933 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3934 << FD->getType();
3935 }
3936 return;
3937 }
3938
Michael Han51d8c522013-01-24 16:46:58 +00003939 D->addAttr(::new (S.Context)
3940 CUDAGlobalAttr(Attr.getRange(), S.Context,
3941 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003942 } else {
3943 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3944 }
3945}
3946
Chandler Carruth1b03c872011-07-02 00:01:44 +00003947static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003948 if (S.LangOpts.CUDA) {
3949 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003950 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003951 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003952
Peter Collingbourneced76712010-12-01 03:15:31 +00003953
Chandler Carruth87c44602011-07-01 23:49:12 +00003954 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003955 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003956 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003957 return;
3958 }
3959
Michael Han51d8c522013-01-24 16:46:58 +00003960 D->addAttr(::new (S.Context)
3961 CUDAHostAttr(Attr.getRange(), S.Context,
3962 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003963 } else {
3964 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3965 }
3966}
3967
Chandler Carruth1b03c872011-07-02 00:01:44 +00003968static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003969 if (S.LangOpts.CUDA) {
3970 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003971 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00003972 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003973
Chandler Carruth87c44602011-07-01 23:49:12 +00003974 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003975 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003976 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003977 return;
3978 }
3979
Michael Han51d8c522013-01-24 16:46:58 +00003980 D->addAttr(::new (S.Context)
3981 CUDASharedAttr(Attr.getRange(), S.Context,
3982 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003983 } else {
3984 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3985 }
3986}
3987
Chandler Carruth1b03c872011-07-02 00:01:44 +00003988static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00003989 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003990 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00003991 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003992
Chandler Carruth87c44602011-07-01 23:49:12 +00003993 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003994 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003995 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003996 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003997 return;
3998 }
Mike Stumpbf916502009-07-24 19:02:52 +00003999
Douglas Gregor0130f3c2009-10-27 21:01:01 +00004000 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00004001 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00004002 return;
4003 }
Mike Stumpbf916502009-07-24 19:02:52 +00004004
Michael Han51d8c522013-01-24 16:46:58 +00004005 D->addAttr(::new (S.Context)
4006 GNUInlineAttr(Attr.getRange(), S.Context,
4007 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00004008}
4009
Chandler Carruth1b03c872011-07-02 00:01:44 +00004010static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004011 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00004012
Aaron Ballmanfff32482012-12-09 17:45:41 +00004013 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00004014 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00004015 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
4016 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00004017 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00004018 return;
4019
Chandler Carruth87c44602011-07-01 23:49:12 +00004020 if (!isa<ObjCMethodDecl>(D)) {
4021 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4022 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00004023 return;
4024 }
4025
Chandler Carruth87c44602011-07-01 23:49:12 +00004026 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004027 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00004028 D->addAttr(::new (S.Context)
4029 FastCallAttr(Attr.getRange(), S.Context,
4030 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00004031 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004032 case AttributeList::AT_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00004033 D->addAttr(::new (S.Context)
4034 StdCallAttr(Attr.getRange(), S.Context,
4035 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00004036 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004037 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00004038 D->addAttr(::new (S.Context)
4039 ThisCallAttr(Attr.getRange(), S.Context,
4040 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00004041 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004042 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00004043 D->addAttr(::new (S.Context)
4044 CDeclAttr(Attr.getRange(), S.Context,
4045 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00004046 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004047 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00004048 D->addAttr(::new (S.Context)
4049 PascalAttr(Attr.getRange(), S.Context,
4050 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00004051 return;
Charles Davise8519c32013-08-30 04:39:01 +00004052 case AttributeList::AT_MSABI:
4053 D->addAttr(::new (S.Context)
4054 MSABIAttr(Attr.getRange(), S.Context,
4055 Attr.getAttributeSpellingListIndex()));
4056 return;
4057 case AttributeList::AT_SysVABI:
4058 D->addAttr(::new (S.Context)
4059 SysVABIAttr(Attr.getRange(), S.Context,
4060 Attr.getAttributeSpellingListIndex()));
4061 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004062 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004063 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00004064 switch (CC) {
4065 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004066 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00004067 break;
4068 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004069 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00004070 break;
4071 default:
4072 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004073 }
4074
Michael Han51d8c522013-01-24 16:46:58 +00004075 D->addAttr(::new (S.Context)
4076 PcsAttr(Attr.getRange(), S.Context, PCS,
4077 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00004078 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004079 }
Derek Schuff263366f2012-10-16 22:30:41 +00004080 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00004081 D->addAttr(::new (S.Context)
4082 PnaclCallAttr(Attr.getRange(), S.Context,
4083 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00004084 return;
Guy Benyei38980082012-12-25 08:53:55 +00004085 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00004086 D->addAttr(::new (S.Context)
4087 IntelOclBiccAttr(Attr.getRange(), S.Context,
4088 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00004089 return;
Derek Schuff263366f2012-10-16 22:30:41 +00004090
Abramo Bagnarae215f722010-04-30 13:10:51 +00004091 default:
4092 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00004093 }
4094}
4095
Chandler Carruth1b03c872011-07-02 00:01:44 +00004096static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004097 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00004098}
4099
Guy Benyei1db70402013-03-24 13:58:12 +00004100static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
Aaron Ballman624421f2013-08-31 01:11:41 +00004101 Expr *E = Attr.getArgAsExpr(0);
Guy Benyei1db70402013-03-24 13:58:12 +00004102 llvm::APSInt ArgNum(32);
4103 if (E->isTypeDependent() || E->isValueDependent() ||
4104 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00004105 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4106 << Attr.getName() << AANT_ArgumentIntegerConstant
4107 << E->getSourceRange();
Guy Benyei1db70402013-03-24 13:58:12 +00004108 return;
4109 }
4110
4111 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
4112 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
4113}
4114
Aaron Ballmanfff32482012-12-09 17:45:41 +00004115bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
4116 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00004117 if (attr.isInvalid())
4118 return true;
4119
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004120 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman624421f2013-08-31 01:11:41 +00004121 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall711c52b2011-01-05 12:14:39 +00004122 attr.setInvalid();
4123 return true;
4124 }
4125
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004126 // TODO: diagnose uses of these conventions on the wrong target. Or, better
4127 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00004128 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004129 case AttributeList::AT_CDecl: CC = CC_C; break;
4130 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
4131 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
4132 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
4133 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davise8519c32013-08-30 04:39:01 +00004134 case AttributeList::AT_MSABI:
4135 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
4136 CC_X86_64Win64;
4137 break;
4138 case AttributeList::AT_SysVABI:
4139 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
4140 CC_C;
4141 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004142 case AttributeList::AT_Pcs: {
Aaron Ballman624421f2013-08-31 01:11:41 +00004143 StringLiteral *Str = 0;
4144 if (attr.isArgExpr(0))
4145 Str = dyn_cast<StringLiteral>(attr.getArgAsExpr(0));
4146
Douglas Gregor5cee1192011-07-27 05:40:30 +00004147 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004148 Diag(attr.getLoc(), diag::err_attribute_argument_type) << attr.getName()
4149 << AANT_ArgumentString;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004150 attr.setInvalid();
4151 return true;
4152 }
4153
Chris Lattner5f9e2722011-07-23 10:55:15 +00004154 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004155 if (StrRef == "aapcs") {
4156 CC = CC_AAPCS;
4157 break;
4158 } else if (StrRef == "aapcs-vfp") {
4159 CC = CC_AAPCS_VFP;
4160 break;
4161 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00004162
4163 attr.setInvalid();
4164 Diag(attr.getLoc(), diag::err_invalid_pcs);
4165 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00004166 }
Derek Schuff263366f2012-10-16 22:30:41 +00004167 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00004168 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00004169 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00004170 }
4171
Aaron Ballman82bfa192012-10-02 14:26:08 +00004172 const TargetInfo &TI = Context.getTargetInfo();
4173 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
4174 if (A == TargetInfo::CCCR_Warning) {
4175 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00004176
4177 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
4178 if (FD)
4179 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
4180 TargetInfo::CCMT_NonMember;
4181 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00004182 }
4183
John McCall711c52b2011-01-05 12:14:39 +00004184 return false;
4185}
4186
Chandler Carruth1b03c872011-07-02 00:01:44 +00004187static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004188 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00004189
4190 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00004191 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00004192 return;
4193
Chandler Carruth87c44602011-07-01 23:49:12 +00004194 if (!isa<ObjCMethodDecl>(D)) {
4195 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
4196 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004197 return;
4198 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004199
Michael Han51d8c522013-01-24 16:46:58 +00004200 D->addAttr(::new (S.Context)
4201 RegparmAttr(Attr.getRange(), S.Context, numParams,
4202 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00004203}
4204
4205/// Checks a regparm attribute, returning true if it is ill-formed and
4206/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00004207bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
4208 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00004209 return true;
4210
Aaron Ballmanffa9d572013-07-18 18:01:48 +00004211 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004212 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004213 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004214 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004215
Aaron Ballman624421f2013-08-31 01:11:41 +00004216 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004217 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00004218 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00004219 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00004220 Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4221 << Attr.getName() << AANT_ArgumentIntegerConstant
4222 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004223 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004224 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004225 }
4226
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004227 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004228 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004229 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004230 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004231 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004232 }
4233
John McCall711c52b2011-01-05 12:14:39 +00004234 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004235 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004236 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004237 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00004238 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00004239 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00004240 }
4241
John McCall711c52b2011-01-05 12:14:39 +00004242 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00004243}
4244
Chandler Carruth1b03c872011-07-02 00:01:44 +00004245static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00004246 if (S.LangOpts.CUDA) {
4247 // check the attribute arguments.
4248 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00004249 // FIXME: 0 is not okay.
4250 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004251 return;
4252 }
4253
Chandler Carruth87c44602011-07-01 23:49:12 +00004254 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00004255 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00004256 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00004257 return;
4258 }
4259
Aaron Ballman624421f2013-08-31 01:11:41 +00004260 Expr *MaxThreadsExpr = Attr.getArgAsExpr(0);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004261 llvm::APSInt MaxThreads(32);
4262 if (MaxThreadsExpr->isTypeDependent() ||
4263 MaxThreadsExpr->isValueDependent() ||
4264 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004265 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004266 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004267 << MaxThreadsExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004268 return;
4269 }
4270
4271 llvm::APSInt MinBlocks(32);
4272 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004273 Expr *MinBlocksExpr = Attr.getArgAsExpr(1);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004274 if (MinBlocksExpr->isTypeDependent() ||
4275 MinBlocksExpr->isValueDependent() ||
4276 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004277 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004278 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004279 << MinBlocksExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004280 return;
4281 }
4282 }
4283
Michael Han51d8c522013-01-24 16:46:58 +00004284 D->addAttr(::new (S.Context)
4285 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4286 MaxThreads.getZExtValue(),
4287 MinBlocks.getZExtValue(),
4288 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne7b381982010-12-12 23:03:07 +00004289 } else {
4290 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4291 }
4292}
4293
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004294static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4295 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004296 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004297 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004298 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004299 return;
4300 }
Aaron Ballman624421f2013-08-31 01:11:41 +00004301
4302 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004303 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004304
Aaron Ballman624421f2013-08-31 01:11:41 +00004305 StringRef AttrName = Attr.getName()->getName();
4306 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004307
4308 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4309 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4310 << Attr.getName() << ExpectedFunctionOrMethod;
4311 return;
4312 }
4313
4314 uint64_t ArgumentIdx;
4315 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4316 Attr.getLoc(), 2,
Aaron Ballman624421f2013-08-31 01:11:41 +00004317 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004318 return;
4319
4320 uint64_t TypeTagIdx;
4321 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4322 Attr.getLoc(), 3,
Aaron Ballman624421f2013-08-31 01:11:41 +00004323 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004324 return;
4325
4326 bool IsPointer = (AttrName == "pointer_with_type_tag");
4327 if (IsPointer) {
4328 // Ensure that buffer has a pointer type.
4329 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4330 if (!BufferTy->isPointerType()) {
4331 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004332 << Attr.getName();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004333 }
4334 }
4335
Michael Han51d8c522013-01-24 16:46:58 +00004336 D->addAttr(::new (S.Context)
4337 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4338 ArgumentIdx, TypeTagIdx, IsPointer,
4339 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004340}
4341
4342static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4343 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004344 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004345 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004346 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004347 return;
4348 }
Aaron Ballman624421f2013-08-31 01:11:41 +00004349
4350 if (!checkAttributeNumArgs(S, Attr, 1))
4351 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004352
Aaron Ballman624421f2013-08-31 01:11:41 +00004353 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004354 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4355
Michael Han51d8c522013-01-24 16:46:58 +00004356 D->addAttr(::new (S.Context)
4357 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4358 MatchingCType,
4359 Attr.getLayoutCompatible(),
4360 Attr.getMustBeNull(),
4361 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004362}
4363
Chris Lattner0744e5f2008-06-29 00:23:49 +00004364//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00004365// Checker-specific attribute handlers.
4366//===----------------------------------------------------------------------===//
4367
John McCallc7ad3812011-01-25 03:31:58 +00004368static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004369 return type->isDependentType() ||
4370 type->isObjCObjectPointerType() ||
4371 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00004372}
4373static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004374 return type->isDependentType() ||
4375 type->isPointerType() ||
4376 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00004377}
4378
Chandler Carruth1b03c872011-07-02 00:01:44 +00004379static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004380 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00004381 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004382 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004383 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00004384 return;
4385 }
4386
4387 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00004388 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00004389 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4390 cf = false;
4391 } else {
4392 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4393 cf = true;
4394 }
4395
4396 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004397 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004398 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00004399 return;
4400 }
4401
4402 if (cf)
Michael Han51d8c522013-01-24 16:46:58 +00004403 param->addAttr(::new (S.Context)
4404 CFConsumedAttr(Attr.getRange(), S.Context,
4405 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004406 else
Michael Han51d8c522013-01-24 16:46:58 +00004407 param->addAttr(::new (S.Context)
4408 NSConsumedAttr(Attr.getRange(), S.Context,
4409 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004410}
4411
Chandler Carruth1b03c872011-07-02 00:01:44 +00004412static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4413 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004414 if (!isa<ObjCMethodDecl>(D)) {
4415 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004416 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00004417 return;
4418 }
4419
Michael Han51d8c522013-01-24 16:46:58 +00004420 D->addAttr(::new (S.Context)
4421 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4422 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004423}
4424
Chandler Carruth1b03c872011-07-02 00:01:44 +00004425static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4426 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004427
John McCallc7ad3812011-01-25 03:31:58 +00004428 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00004429
Chandler Carruth87c44602011-07-01 23:49:12 +00004430 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004431 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004432 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00004433 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00004434 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00004435 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4436 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00004437 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004438 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004439 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00004440 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004441 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00004442 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004443 return;
4444 }
Mike Stumpbf916502009-07-24 19:02:52 +00004445
John McCallc7ad3812011-01-25 03:31:58 +00004446 bool typeOK;
4447 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00004448 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00004449 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004450 case AttributeList::AT_NSReturnsAutoreleased:
4451 case AttributeList::AT_NSReturnsRetained:
4452 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004453 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4454 cf = false;
4455 break;
4456
Sean Hunt8e083e72012-06-19 23:57:03 +00004457 case AttributeList::AT_CFReturnsRetained:
4458 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004459 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4460 cf = true;
4461 break;
4462 }
4463
4464 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004465 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004466 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00004467 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004468 }
Mike Stumpbf916502009-07-24 19:02:52 +00004469
Chandler Carruth87c44602011-07-01 23:49:12 +00004470 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004471 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00004472 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004473 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han51d8c522013-01-24 16:46:58 +00004474 D->addAttr(::new (S.Context)
4475 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4476 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004477 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004478 case AttributeList::AT_CFReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004479 D->addAttr(::new (S.Context)
4480 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4481 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004482 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004483 case AttributeList::AT_NSReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004484 D->addAttr(::new (S.Context)
4485 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4486 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004487 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004488 case AttributeList::AT_CFReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004489 D->addAttr(::new (S.Context)
4490 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4491 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004492 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004493 case AttributeList::AT_NSReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004494 D->addAttr(::new (S.Context)
4495 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4496 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004497 return;
4498 };
4499}
4500
John McCalldc7c5ad2011-07-22 08:53:00 +00004501static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4502 const AttributeList &attr) {
4503 SourceLocation loc = attr.getLoc();
4504
4505 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4506
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004507 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00004508 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004509 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00004510 return;
4511 }
4512
4513 // Check that the method returns a normal pointer.
4514 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004515
4516 if (!resultType->isReferenceType() &&
4517 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00004518 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4519 << SourceRange(loc)
4520 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4521
4522 // Drop the attribute.
4523 return;
4524 }
4525
Michael Han51d8c522013-01-24 16:46:58 +00004526 method->addAttr(::new (S.Context)
4527 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4528 attr.getAttributeSpellingListIndex()));
John McCalldc7c5ad2011-07-22 08:53:00 +00004529}
4530
Fariborz Jahanian84101132012-09-07 23:46:23 +00004531static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4532 const AttributeList &attr) {
4533 SourceLocation loc = attr.getLoc();
4534 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4535
4536 if (!method) {
4537 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4538 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4539 return;
4540 }
4541 DeclContext *DC = method->getDeclContext();
4542 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4543 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4544 << attr.getName() << 0;
4545 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4546 return;
4547 }
4548 if (method->getMethodFamily() == OMF_dealloc) {
4549 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4550 << attr.getName() << 1;
4551 return;
4552 }
4553
Michael Han51d8c522013-01-24 16:46:58 +00004554 method->addAttr(::new (S.Context)
4555 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4556 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian84101132012-09-07 23:46:23 +00004557}
4558
John McCall8dfac0b2011-09-30 05:12:12 +00004559/// Handle cf_audited_transfer and cf_unknown_transfer.
4560static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4561 if (!isa<FunctionDecl>(D)) {
4562 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004563 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004564 return;
4565 }
4566
Sean Hunt8e083e72012-06-19 23:57:03 +00004567 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004568
4569 // Check whether there's a conflicting attribute already present.
4570 Attr *Existing;
4571 if (IsAudited) {
4572 Existing = D->getAttr<CFUnknownTransferAttr>();
4573 } else {
4574 Existing = D->getAttr<CFAuditedTransferAttr>();
4575 }
4576 if (Existing) {
4577 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4578 << A.getName()
4579 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4580 << A.getRange() << Existing->getRange();
4581 return;
4582 }
4583
4584 // All clear; add the attribute.
4585 if (IsAudited) {
Michael Han51d8c522013-01-24 16:46:58 +00004586 D->addAttr(::new (S.Context)
4587 CFAuditedTransferAttr(A.getRange(), S.Context,
4588 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004589 } else {
Michael Han51d8c522013-01-24 16:46:58 +00004590 D->addAttr(::new (S.Context)
4591 CFUnknownTransferAttr(A.getRange(), S.Context,
4592 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004593 }
4594}
4595
John McCallfe98da02011-09-29 07:17:38 +00004596static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4597 const AttributeList &Attr) {
4598 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4599 if (!RD || RD->isUnion()) {
4600 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004601 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004602 }
4603
Aaron Ballman624421f2013-08-31 01:11:41 +00004604 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallfe98da02011-09-29 07:17:38 +00004605
4606 // In Objective-C, verify that the type names an Objective-C type.
4607 // We don't want to check this outside of ObjC because people sometimes
4608 // do crazy C declarations of Objective-C types.
Aaron Ballman624421f2013-08-31 01:11:41 +00004609 if (Parm && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004610 // Check for an existing type with this name.
Aaron Ballman624421f2013-08-31 01:11:41 +00004611 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallfe98da02011-09-29 07:17:38 +00004612 Sema::LookupOrdinaryName);
4613 if (S.LookupName(R, Sc)) {
4614 NamedDecl *Target = R.getFoundDecl();
4615 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4616 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4617 S.Diag(Target->getLocStart(), diag::note_declared_at);
4618 }
4619 }
4620 }
4621
Michael Han51d8c522013-01-24 16:46:58 +00004622 D->addAttr(::new (S.Context)
Aaron Ballman624421f2013-08-31 01:11:41 +00004623 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han51d8c522013-01-24 16:46:58 +00004624 Attr.getAttributeSpellingListIndex()));
John McCallfe98da02011-09-29 07:17:38 +00004625}
4626
Chandler Carruth1b03c872011-07-02 00:01:44 +00004627static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4628 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004629 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004630
Chandler Carruth87c44602011-07-01 23:49:12 +00004631 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004632 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004633}
4634
Chandler Carruth1b03c872011-07-02 00:01:44 +00004635static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4636 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004637 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004638 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004639 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004640 return;
4641 }
4642
Chandler Carruth87c44602011-07-01 23:49:12 +00004643 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004644 QualType type = vd->getType();
4645
4646 if (!type->isDependentType() &&
4647 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004648 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004649 << type;
4650 return;
4651 }
4652
4653 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4654
4655 // If we have no lifetime yet, check the lifetime we're presumably
4656 // going to infer.
4657 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4658 lifetime = type->getObjCARCImplicitLifetime();
4659
4660 switch (lifetime) {
4661 case Qualifiers::OCL_None:
4662 assert(type->isDependentType() &&
4663 "didn't infer lifetime for non-dependent type?");
4664 break;
4665
4666 case Qualifiers::OCL_Weak: // meaningful
4667 case Qualifiers::OCL_Strong: // meaningful
4668 break;
4669
4670 case Qualifiers::OCL_ExplicitNone:
4671 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004672 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004673 << (lifetime == Qualifiers::OCL_Autoreleasing);
4674 break;
4675 }
4676
Chandler Carruth87c44602011-07-01 23:49:12 +00004677 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004678 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4679 Attr.getAttributeSpellingListIndex()));
John McCallf85e1932011-06-15 23:02:42 +00004680}
4681
Francois Pichet11542142010-12-19 06:50:37 +00004682//===----------------------------------------------------------------------===//
4683// Microsoft specific attribute handlers.
4684//===----------------------------------------------------------------------===//
4685
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004686// Check if MS extensions or some other language extensions are enabled. If
4687// not, issue a diagnostic that the given attribute is unused.
4688static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4689 bool OtherExtension = false) {
4690 if (S.LangOpts.MicrosoftExt || OtherExtension)
4691 return true;
4692 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4693 return false;
4694}
4695
Chandler Carruth1b03c872011-07-02 00:01:44 +00004696static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004697 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4698 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004699
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004700 // check the attribute arguments.
4701 if (!checkAttributeNumArgs(S, Attr, 1))
4702 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004703
Aaron Ballman624421f2013-08-31 01:11:41 +00004704 Expr *Arg = Attr.getArgAsExpr(0);
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004705 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
4706 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004707 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4708 << Attr.getName() << AANT_ArgumentString;
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004709 return;
4710 }
Francois Pichetd3d3be92010-12-20 01:41:49 +00004711
David Majnemerbb0ed292013-08-09 08:56:20 +00004712 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4713 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004714 StringRef StrRef = Str->getString();
David Majnemerbb0ed292013-08-09 08:56:20 +00004715 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4716 StrRef = StrRef.drop_front().drop_back();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004717
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004718 // Validate GUID length.
David Majnemerbb0ed292013-08-09 08:56:20 +00004719 if (StrRef.size() != 36) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004720 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4721 return;
4722 }
Anders Carlssonf89e0422011-01-23 21:07:30 +00004723
David Majnemerbb0ed292013-08-09 08:56:20 +00004724 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004725 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemerbb0ed292013-08-09 08:56:20 +00004726 if (StrRef[i] != '-') {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004727 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4728 return;
4729 }
David Majnemerbb0ed292013-08-09 08:56:20 +00004730 } else if (!isHexDigit(StrRef[i])) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004731 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4732 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004733 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004734 }
Francois Pichet11542142010-12-19 06:50:37 +00004735
David Majnemerbb0ed292013-08-09 08:56:20 +00004736 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4737 Attr.getAttributeSpellingListIndex()));
Charles Davisf0122fe2010-02-16 18:27:26 +00004738}
4739
John McCallc052dbb2012-05-22 21:28:12 +00004740static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004741 if (!checkMicrosoftExt(S, Attr))
Nico Weber7b89ab72012-11-07 21:31:36 +00004742 return;
Nico Weber7b89ab72012-11-07 21:31:36 +00004743
4744 AttributeList::Kind Kind = Attr.getKind();
4745 if (Kind == AttributeList::AT_SingleInheritance)
4746 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004747 ::new (S.Context)
4748 SingleInheritanceAttr(Attr.getRange(), S.Context,
4749 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004750 else if (Kind == AttributeList::AT_MultipleInheritance)
4751 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004752 ::new (S.Context)
4753 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4754 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004755 else if (Kind == AttributeList::AT_VirtualInheritance)
4756 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004757 ::new (S.Context)
4758 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4759 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004760}
4761
4762static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004763 if (!checkMicrosoftExt(S, Attr))
4764 return;
4765
4766 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004767 if (Kind == AttributeList::AT_Win64)
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004768 D->addAttr(
4769 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4770 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004771}
4772
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004773static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004774 if (!checkMicrosoftExt(S, Attr))
4775 return;
4776 D->addAttr(::new (S.Context)
4777 ForceInlineAttr(Attr.getRange(), S.Context,
4778 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004779}
4780
Reid Klecknera7225342013-05-20 14:02:37 +00004781static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4782 if (!checkMicrosoftExt(S, Attr))
4783 return;
4784 // Check linkage after possibly merging declaratinos. See
4785 // checkAttributesAfterMerging().
4786 D->addAttr(::new (S.Context)
4787 SelectAnyAttr(Attr.getRange(), S.Context,
4788 Attr.getAttributeSpellingListIndex()));
4789}
4790
Ted Kremenekb71368d2009-05-09 02:44:38 +00004791//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004792// Top Level Sema Entry Points
4793//===----------------------------------------------------------------------===//
4794
Richard Smith4a97b8e2013-08-29 00:47:48 +00004795/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4796/// the attribute applies to decls. If the attribute is a type attribute, just
4797/// silently ignore it if a GNU attribute.
4798static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4799 const AttributeList &Attr,
4800 bool IncludeCXX11Attributes) {
4801 if (Attr.isInvalid())
4802 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00004803
Richard Smith4a97b8e2013-08-29 00:47:48 +00004804 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4805 // instead.
4806 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4807 return;
4808
Chris Lattner803d0802008-06-29 00:43:07 +00004809 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004810 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4811 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4812 case AttributeList::AT_IBOutletCollection:
4813 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004814 case AttributeList::AT_AddressSpace:
Sean Hunt8e083e72012-06-19 23:57:03 +00004815 case AttributeList::AT_ObjCGC:
4816 case AttributeList::AT_VectorSize:
4817 case AttributeList::AT_NeonVectorType:
4818 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004819 case AttributeList::AT_Ptr32:
4820 case AttributeList::AT_Ptr64:
4821 case AttributeList::AT_SPtr:
4822 case AttributeList::AT_UPtr:
Mike Stumpbf916502009-07-24 19:02:52 +00004823 // Ignore these, these are type attributes, handled by
4824 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004825 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004826 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4827 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4828 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4829 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004830 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004831 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004832 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004833 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004834 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4835 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4836 case AttributeList::AT_CarriesDependency:
Richard Smith3a2b7a12013-01-28 22:42:45 +00004837 handleDependencyAttr(S, scope, D, Attr);
4838 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004839 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4840 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4841 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004842 case AttributeList::AT_CXX11NoReturn:
4843 handleCXX11NoReturnAttr(S, D, Attr);
4844 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004845 case AttributeList::AT_Deprecated:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004846 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004847 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004848 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4849 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004850 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004851 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004852 case AttributeList::AT_MinSize:
4853 handleMinSizeAttr(S, D, Attr);
4854 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004855 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4856 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4857 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman19513232013-08-28 23:13:26 +00004858 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4859 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004860 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4861 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004862 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004863 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004864 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4865 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004866 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004867 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4868 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004869 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004870 case AttributeList::AT_ownership_returns:
4871 case AttributeList::AT_ownership_takes:
4872 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004873 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004874 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4875 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4876 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4877 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4878 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4879 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4880 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004881
Sean Hunt8e083e72012-06-19 23:57:03 +00004882 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004883 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004884 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004885 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004886
Sean Hunt8e083e72012-06-19 23:57:03 +00004887 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004888 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4889
Fariborz Jahanian84101132012-09-07 23:46:23 +00004890 case AttributeList::AT_ObjCRequiresSuper:
4891 handleObjCRequiresSuperAttr(S, D, Attr); break;
4892
Sean Hunt8e083e72012-06-19 23:57:03 +00004893 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004894 handleNSBridgedAttr(S, scope, D, Attr); break;
4895
Sean Hunt8e083e72012-06-19 23:57:03 +00004896 case AttributeList::AT_CFAuditedTransfer:
4897 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004898 handleCFTransferAttr(S, D, Attr); break;
4899
Ted Kremenekb71368d2009-05-09 02:44:38 +00004900 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004901 case AttributeList::AT_CFConsumed:
4902 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4903 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004904 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004905
Sean Hunt8e083e72012-06-19 23:57:03 +00004906 case AttributeList::AT_NSReturnsAutoreleased:
4907 case AttributeList::AT_NSReturnsNotRetained:
4908 case AttributeList::AT_CFReturnsNotRetained:
4909 case AttributeList::AT_NSReturnsRetained:
4910 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004911 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004912
Tanya Lattner0df579e2012-07-09 22:06:01 +00004913 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004914 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004915 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004916
Joey Gouly37453b92013-03-08 09:42:32 +00004917 case AttributeList::AT_VecTypeHint:
4918 handleVecTypeHint(S, D, Attr); break;
4919
Joey Gouly96cead52013-03-14 09:54:43 +00004920 case AttributeList::AT_Endian:
4921 handleEndianAttr(S, D, Attr);
4922 break;
4923
Sean Hunt8e083e72012-06-19 23:57:03 +00004924 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004925 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004926
Sean Hunt8e083e72012-06-19 23:57:03 +00004927 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4928 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4929 case AttributeList::AT_Unavailable:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004930 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004931 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004932 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004933 handleArcWeakrefUnavailableAttr (S, D, Attr);
4934 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004935 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004936 handleObjCRootClassAttr(S, D, Attr);
4937 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004938 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004939 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004940 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004941 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4942 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004943 handleReturnsTwiceAttr(S, D, Attr);
4944 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004945 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld4c3d662013-02-20 01:54:26 +00004946 case AttributeList::AT_Visibility:
4947 handleVisibilityAttr(S, D, Attr, false);
4948 break;
4949 case AttributeList::AT_TypeVisibility:
4950 handleVisibilityAttr(S, D, Attr, true);
4951 break;
Lubos Lunak1d3ce652013-07-20 15:05:36 +00004952 case AttributeList::AT_WarnUnused:
4953 handleWarnUnusedAttr(S, D, Attr);
4954 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004955 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004956 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004957 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4958 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4959 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4960 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004961 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004962 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004963 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004964 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004965 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004966 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004967 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004968 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004969 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4970 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4971 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4972 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4973 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4974 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4975 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4976 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4977 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004978 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004979 // Just ignore
4980 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004981 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004982 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004983 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004984 case AttributeList::AT_StdCall:
4985 case AttributeList::AT_CDecl:
4986 case AttributeList::AT_FastCall:
4987 case AttributeList::AT_ThisCall:
4988 case AttributeList::AT_Pascal:
Charles Davise8519c32013-08-30 04:39:01 +00004989 case AttributeList::AT_MSABI:
4990 case AttributeList::AT_SysVABI:
Sean Hunt8e083e72012-06-19 23:57:03 +00004991 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004992 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004993 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004994 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004995 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004996 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004997 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004998 break;
Guy Benyei1db70402013-03-24 13:58:12 +00004999 case AttributeList::AT_OpenCLImageAccess:
5000 handleOpenCLImageAccessAttr(S, D, Attr);
5001 break;
John McCallc052dbb2012-05-22 21:28:12 +00005002
5003 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00005004 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00005005 handleMsStructAttr(S, D, Attr);
5006 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005007 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00005008 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00005009 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005010 case AttributeList::AT_SingleInheritance:
5011 case AttributeList::AT_MultipleInheritance:
5012 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00005013 handleInheritanceAttr(S, D, Attr);
5014 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005015 case AttributeList::AT_Win64:
John McCallc052dbb2012-05-22 21:28:12 +00005016 handlePortabilityAttr(S, D, Attr);
5017 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005018 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00005019 handleForceInlineAttr(S, D, Attr);
5020 break;
Reid Klecknera7225342013-05-20 14:02:37 +00005021 case AttributeList::AT_SelectAny:
5022 handleSelectAnyAttr(S, D, Attr);
5023 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005024
5025 // Thread safety attributes:
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +00005026 case AttributeList::AT_AssertExclusiveLock:
5027 handleAssertExclusiveLockAttr(S, D, Attr);
5028 break;
5029 case AttributeList::AT_AssertSharedLock:
5030 handleAssertSharedLockAttr(S, D, Attr);
5031 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005032 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005033 handleGuardedVarAttr(S, D, Attr);
5034 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005035 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00005036 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005037 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005038 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00005039 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005040 break;
Kostya Serebryany85aee962013-02-26 06:58:27 +00005041 case AttributeList::AT_NoSanitizeAddress:
5042 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany71efba02012-01-24 19:25:38 +00005043 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005044 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany85aee962013-02-26 06:58:27 +00005045 handleNoThreadSafetyAnalysis(S, D, Attr);
5046 break;
5047 case AttributeList::AT_NoSanitizeThread:
5048 handleNoSanitizeThread(S, D, Attr);
5049 break;
5050 case AttributeList::AT_NoSanitizeMemory:
5051 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005052 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005053 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005054 handleLockableAttr(S, D, Attr);
5055 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005056 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005057 handleGuardedByAttr(S, D, Attr);
5058 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005059 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00005060 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005061 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005062 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00005063 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005064 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005065 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00005066 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005067 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005068 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00005069 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005070 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005071 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005072 handleLockReturnedAttr(S, D, Attr);
5073 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005074 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005075 handleLocksExcludedAttr(S, D, Attr);
5076 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005077 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00005078 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005079 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005080 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00005081 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005082 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005083 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00005084 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005085 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005086 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005087 handleUnlockFunAttr(S, D, Attr);
5088 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005089 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00005090 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005091 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00005092 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00005093 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00005094 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00005095
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00005096 // Uniqueness analysis attributes.
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00005097 case AttributeList::AT_Consumable:
5098 handleConsumableAttr(S, D, Attr);
5099 break;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00005100 case AttributeList::AT_Consumes:
5101 handleConsumesAttr(S, D, Attr);
5102 break;
5103 case AttributeList::AT_CallableWhenUnconsumed:
5104 handleCallableWhenUnconsumedAttr(S, D, Attr);
5105 break;
5106 case AttributeList::AT_TestsConsumed:
5107 handleTestsConsumedAttr(S, D, Attr);
5108 break;
5109 case AttributeList::AT_TestsUnconsumed:
5110 handleTestsUnconsumedAttr(S, D, Attr);
5111 break;
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00005112 case AttributeList::AT_ReturnTypestate:
5113 handleReturnTypestateAttr(S, D, Attr);
5114 break;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00005115
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00005116 // Type safety attributes.
5117 case AttributeList::AT_ArgumentWithTypeTag:
5118 handleArgumentWithTypeTagAttr(S, D, Attr);
5119 break;
5120 case AttributeList::AT_TypeTagForDatatype:
5121 handleTypeTagForDatatypeAttr(S, D, Attr);
5122 break;
5123
Chris Lattner803d0802008-06-29 00:43:07 +00005124 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005125 // Ask target about the attribute.
5126 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
5127 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00005128 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
5129 diag::warn_unhandled_ms_attribute_ignored :
5130 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00005131 break;
5132 }
5133}
5134
5135/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
5136/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00005137void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00005138 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00005139 bool IncludeCXX11Attributes) {
5140 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005141 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005142
5143 // GCC accepts
5144 // static int a9 __attribute__((weakref));
5145 // but that looks really pointless. We reject it.
Richard Smith4a97b8e2013-08-29 00:47:48 +00005146 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005147 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00005148 cast<NamedDecl>(D)->getNameAsString();
5149 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00005150 return;
Chris Lattner803d0802008-06-29 00:43:07 +00005151 }
5152}
5153
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005154// Annotation attributes are the only attributes allowed after an access
5155// specifier.
5156bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
5157 const AttributeList *AttrList) {
5158 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00005159 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00005160 handleAnnotateAttr(*this, ASDecl, *l);
5161 } else {
5162 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
5163 return true;
5164 }
5165 }
5166
5167 return false;
5168}
5169
John McCalle82247a2011-10-01 05:17:03 +00005170/// checkUnusedDeclAttributes - Check a list of attributes to see if it
5171/// contains any decl attributes that we should warn about.
5172static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
5173 for ( ; A; A = A->getNext()) {
5174 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smithd03de6a2013-01-29 10:02:16 +00005175 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCalle82247a2011-10-01 05:17:03 +00005176 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
5177
5178 if (A->getKind() == AttributeList::UnknownAttribute) {
5179 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
5180 << A->getName() << A->getRange();
5181 } else {
5182 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
5183 << A->getName() << A->getRange();
5184 }
5185 }
5186}
5187
5188/// checkUnusedDeclAttributes - Given a declarator which is not being
5189/// used to build a declaration, complain about any decl attributes
5190/// which might be lying around on it.
5191void Sema::checkUnusedDeclAttributes(Declarator &D) {
5192 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
5193 ::checkUnusedDeclAttributes(*this, D.getAttributes());
5194 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
5195 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
5196}
5197
Ryan Flynne25ff832009-07-30 03:15:39 +00005198/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00005199/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00005200NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
5201 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005202 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00005203 NamedDecl *NewD = 0;
5204 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00005205 FunctionDecl *NewFD;
5206 // FIXME: Missing call to CheckFunctionDeclaration().
5207 // FIXME: Mangling?
5208 // FIXME: Is the qualifier info correct?
5209 // FIXME: Is the DeclContext correct?
5210 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
5211 Loc, Loc, DeclarationName(II),
5212 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005213 SC_None, false/*isInlineSpecified*/,
Eli Friedman900693b2011-09-07 04:05:06 +00005214 FD->hasPrototype(),
5215 false/*isConstexprSpecified*/);
5216 NewD = NewFD;
5217
5218 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005219 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00005220
5221 // Fake up parameter variables; they are declared as if this were
5222 // a typedef.
5223 QualType FDTy = FD->getType();
5224 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
5225 SmallVector<ParmVarDecl*, 16> Params;
5226 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
5227 AE = FT->arg_type_end(); AI != AE; ++AI) {
5228 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
5229 Param->setScopeInfo(0, Params.size());
5230 Params.push_back(Param);
5231 }
David Blaikie4278c652011-09-21 18:16:56 +00005232 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00005233 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005234 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
5235 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005236 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00005237 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005238 VD->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00005239 if (VD->getQualifier()) {
5240 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005241 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00005242 }
Ryan Flynne25ff832009-07-30 03:15:39 +00005243 }
5244 return NewD;
5245}
5246
James Dennett1dfbd922012-06-14 21:40:34 +00005247/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00005248/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00005249void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005250 if (W.getUsed()) return; // only do this once
5251 W.setUsed(true);
5252 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
5253 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00005254 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00005255 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5256 NDId->getName()));
5257 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005258 WeakTopLevelDecl.push_back(NewD);
5259 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5260 // to insert Decl at TU scope, sorry.
5261 DeclContext *SavedContext = CurContext;
5262 CurContext = Context.getTranslationUnitDecl();
5263 PushOnScopeChains(NewD, S);
5264 CurContext = SavedContext;
5265 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00005266 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00005267 }
5268}
5269
Rafael Espindola65611bf2013-03-02 21:41:48 +00005270void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5271 // It's valid to "forward-declare" #pragma weak, in which case we
5272 // have to do this.
5273 LoadExternalWeakUndeclaredIdentifiers();
5274 if (!WeakUndeclaredIdentifiers.empty()) {
5275 NamedDecl *ND = NULL;
5276 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5277 if (VD->isExternC())
5278 ND = VD;
5279 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5280 if (FD->isExternC())
5281 ND = FD;
5282 if (ND) {
5283 if (IdentifierInfo *Id = ND->getIdentifier()) {
5284 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5285 = WeakUndeclaredIdentifiers.find(Id);
5286 if (I != WeakUndeclaredIdentifiers.end()) {
5287 WeakInfo W = I->second;
5288 DeclApplyPragmaWeak(S, ND, W);
5289 WeakUndeclaredIdentifiers[Id] = W;
5290 }
5291 }
5292 }
5293 }
5294}
5295
Chris Lattner0744e5f2008-06-29 00:23:49 +00005296/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5297/// it, apply them to D. This is a bit tricky because PD can have attributes
5298/// specified in many different places, and we need to find and apply them all.
Richard Smith4a97b8e2013-08-29 00:47:48 +00005299void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner0744e5f2008-06-29 00:23:49 +00005300 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00005301 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005302 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00005303
Chris Lattner0744e5f2008-06-29 00:23:49 +00005304 // Walk the declarator structure, applying decl attributes that were in a type
5305 // position to the decl itself. This handles cases like:
5306 // int *__attr__(x)** D;
5307 // when X is a decl attribute.
5308 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5309 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005310 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00005311
Chris Lattner0744e5f2008-06-29 00:23:49 +00005312 // Finally, apply any attributes on the decl itself.
5313 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005314 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00005315}
John McCall54abf7d2009-11-04 02:18:39 +00005316
John McCallf85e1932011-06-15 23:02:42 +00005317/// Is the given declaration allowed to use a forbidden type?
5318static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5319 // Private ivars are always okay. Unfortunately, people don't
5320 // always properly make their ivars private, even in system headers.
5321 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00005322 // Function declarations in sys headers will be marked unavailable.
5323 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5324 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00005325 return false;
5326
5327 // Require it to be declared in a system header.
5328 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5329}
5330
5331/// Handle a delayed forbidden-type diagnostic.
5332static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5333 Decl *decl) {
5334 if (decl && isForbiddenTypeAllowed(S, decl)) {
5335 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5336 "this system declaration uses an unsupported type"));
5337 return;
5338 }
David Blaikie4e4d0842012-03-11 07:00:24 +00005339 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005340 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00005341 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005342 // kind of forbidden type messages on unavailable functions.
5343 if (FD->hasAttr<UnavailableAttr>() &&
5344 diag.getForbiddenTypeDiagnostic() ==
5345 diag::err_arc_array_param_no_ownership) {
5346 diag.Triggered = true;
5347 return;
5348 }
5349 }
John McCallf85e1932011-06-15 23:02:42 +00005350
5351 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5352 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5353 diag.Triggered = true;
5354}
5355
John McCall92576642012-05-07 06:16:41 +00005356void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5357 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00005358 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00005359 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00005360
John McCall92576642012-05-07 06:16:41 +00005361 // When delaying diagnostics to run in the context of a parsed
5362 // declaration, we only want to actually emit anything if parsing
5363 // succeeds.
5364 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00005365
John McCall92576642012-05-07 06:16:41 +00005366 // We emit all the active diagnostics in this pool or any of its
5367 // parents. In general, we'll get one pool for the decl spec
5368 // and a child pool for each declarator; in a decl group like:
5369 // deprecated_typedef foo, *bar, baz();
5370 // only the declarator pops will be passed decls. This is correct;
5371 // we really do need to consider delayed diagnostics from the decl spec
5372 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005373 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005374 do {
John McCall13489672012-05-07 06:16:58 +00005375 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005376 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5377 // This const_cast is a bit lame. Really, Triggered should be mutable.
5378 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005379 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005380 continue;
5381
John McCalleee1d542011-02-14 07:13:47 +00005382 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005383 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005384 // Don't bother giving deprecation diagnostics if the decl is invalid.
5385 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005386 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005387 break;
5388
5389 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005390 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005391 break;
John McCallf85e1932011-06-15 23:02:42 +00005392
5393 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005394 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005395 break;
John McCall2f514482010-01-27 03:50:35 +00005396 }
5397 }
John McCall92576642012-05-07 06:16:41 +00005398 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005399}
5400
John McCall13489672012-05-07 06:16:58 +00005401/// Given a set of delayed diagnostics, re-emit them as if they had
5402/// been delayed in the current context instead of in the given pool.
5403/// Essentially, this just moves them to the current pool.
5404void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5405 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5406 assert(curPool && "re-emitting in undelayed context not supported");
5407 curPool->steal(pool);
5408}
5409
John McCall54abf7d2009-11-04 02:18:39 +00005410static bool isDeclDeprecated(Decl *D) {
5411 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005412 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005413 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005414 // A category implicitly has the availability of the interface.
5415 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5416 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005417 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5418 return false;
5419}
5420
Eli Friedmanc3b23082012-08-08 21:52:41 +00005421static void
5422DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5423 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005424 const ObjCInterfaceDecl *UnknownObjCClass,
5425 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005426 DeclarationName Name = D->getDeclName();
5427 if (!Message.empty()) {
5428 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5429 S.Diag(D->getLocation(),
5430 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5431 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005432 if (ObjCPropery)
5433 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5434 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005435 } else if (!UnknownObjCClass) {
5436 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5437 S.Diag(D->getLocation(),
5438 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5439 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005440 if (ObjCPropery)
5441 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5442 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005443 } else {
5444 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5445 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5446 }
5447}
5448
John McCall9c3087b2010-08-26 02:13:20 +00005449void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005450 Decl *Ctx) {
5451 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005452 return;
5453
John McCall2f514482010-01-27 03:50:35 +00005454 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005455 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5456 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005457 DD.getUnknownObjCClass(),
5458 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005459}
5460
Chris Lattner5f9e2722011-07-23 10:55:15 +00005461void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005462 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005463 const ObjCInterfaceDecl *UnknownObjCClass,
5464 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005465 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005466 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005467 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5468 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005469 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005470 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005471 return;
5472 }
5473
5474 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005475 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005476 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005477 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005478}