blob: c226215c5dae3045e7af60ded1f2ecb331f2da6c [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 // D must be either a member field or global (potentially shared) variable.
501 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000502 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
503 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000504 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000505 }
506
Michael Handc691572012-07-23 18:48:41 +0000507 return true;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000508}
509
Michael Handc691572012-07-23 18:48:41 +0000510static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) {
511 if (!checkGuardedVarAttrCommon(S, D, Attr))
512 return;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000513
Michael Han51d8c522013-01-24 16:46:58 +0000514 D->addAttr(::new (S.Context)
515 GuardedVarAttr(Attr.getRange(), S.Context,
516 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000517}
518
Michael Hanf1aae3b2012-08-03 17:40:43 +0000519static void handlePtGuardedVarAttr(Sema &S, Decl *D,
Michael Han51d8c522013-01-24 16:46:58 +0000520 const AttributeList &Attr) {
Michael Handc691572012-07-23 18:48:41 +0000521 if (!checkGuardedVarAttrCommon(S, D, Attr))
522 return;
523
524 if (!threadSafetyCheckIsPointer(S, D, Attr))
525 return;
526
Michael Han51d8c522013-01-24 16:46:58 +0000527 D->addAttr(::new (S.Context)
528 PtGuardedVarAttr(Attr.getRange(), S.Context,
529 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000530}
531
Michael Hanf1aae3b2012-08-03 17:40:43 +0000532static bool checkGuardedByAttrCommon(Sema &S, Decl *D,
533 const AttributeList &Attr,
Michael Handc691572012-07-23 18:48:41 +0000534 Expr* &Arg) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000535 // D must be either a member field or global (potentially shared) variable.
536 if (!mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000537 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
538 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000539 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000540 }
541
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000542 SmallVector<Expr*, 1> Args;
543 // check that all arguments are lockable objects
544 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
545 unsigned Size = Args.size();
546 if (Size != 1)
Michael Handc691572012-07-23 18:48:41 +0000547 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000548
Michael Handc691572012-07-23 18:48:41 +0000549 Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000550
Michael Handc691572012-07-23 18:48:41 +0000551 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000552}
553
Michael Handc691572012-07-23 18:48:41 +0000554static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) {
555 Expr *Arg = 0;
556 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
557 return;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000558
Michael Handc691572012-07-23 18:48:41 +0000559 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
560}
561
Michael Hanf1aae3b2012-08-03 17:40:43 +0000562static void handlePtGuardedByAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000563 const AttributeList &Attr) {
564 Expr *Arg = 0;
565 if (!checkGuardedByAttrCommon(S, D, Attr, Arg))
566 return;
567
568 if (!threadSafetyCheckIsPointer(S, D, Attr))
569 return;
570
571 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
572 S.Context, Arg));
573}
574
Michael Hanf1aae3b2012-08-03 17:40:43 +0000575static bool checkLockableAttrCommon(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000576 const AttributeList &Attr) {
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000577 // FIXME: Lockable structs for C code.
David Blaikie88c4b5e2013-07-29 18:24:03 +0000578 if (!isa<RecordDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000579 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
580 << Attr.getName() << ThreadExpectedClassOrStruct;
Michael Handc691572012-07-23 18:48:41 +0000581 return false;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000582 }
583
Michael Handc691572012-07-23 18:48:41 +0000584 return true;
585}
586
587static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
588 if (!checkLockableAttrCommon(S, D, Attr))
589 return;
590
591 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
592}
593
Michael Hanf1aae3b2012-08-03 17:40:43 +0000594static void handleScopedLockableAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000595 const AttributeList &Attr) {
596 if (!checkLockableAttrCommon(S, D, Attr))
597 return;
598
Michael Han51d8c522013-01-24 16:46:58 +0000599 D->addAttr(::new (S.Context)
600 ScopedLockableAttr(Attr.getRange(), S.Context,
601 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000602}
603
Kostya Serebryany85aee962013-02-26 06:58:27 +0000604static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D,
605 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000606 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000607 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
608 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000609 return;
610 }
611
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000612 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000613 S.Context));
614}
615
Kostya Serebryany85aee962013-02-26 06:58:27 +0000616static void handleNoSanitizeAddressAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000617 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000618 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000619 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
Kostya Serebryany71efba02012-01-24 19:25:38 +0000620 << Attr.getName() << ExpectedFunctionOrMethod;
621 return;
622 }
623
Michael Han51d8c522013-01-24 16:46:58 +0000624 D->addAttr(::new (S.Context)
Kostya Serebryany85aee962013-02-26 06:58:27 +0000625 NoSanitizeAddressAttr(Attr.getRange(), S.Context,
626 Attr.getAttributeSpellingListIndex()));
627}
628
629static void handleNoSanitizeMemory(Sema &S, Decl *D,
630 const AttributeList &Attr) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000631 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
632 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
633 << Attr.getName() << ExpectedFunctionOrMethod;
634 return;
635 }
636
637 D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(),
638 S.Context));
639}
640
641static void handleNoSanitizeThread(Sema &S, Decl *D,
642 const AttributeList &Attr) {
Kostya Serebryany85aee962013-02-26 06:58:27 +0000643 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
644 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
645 << Attr.getName() << ExpectedFunctionOrMethod;
646 return;
647 }
648
649 D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(),
650 S.Context));
Kostya Serebryany71efba02012-01-24 19:25:38 +0000651}
652
Michael Hanf1aae3b2012-08-03 17:40:43 +0000653static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D,
654 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000655 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000656 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000657 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000658
659 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000660 ValueDecl *VD = dyn_cast<ValueDecl>(D);
661 if (!VD || !mayBeSharedVariable(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000662 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
663 << Attr.getName() << ThreadExpectedFieldOrGlobalVar;
Michael Handc691572012-07-23 18:48:41 +0000664 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000665 }
666
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000667 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000668 QualType QT = VD->getType();
669 if (!QT->isDependentType()) {
670 const RecordType *RT = getRecordType(QT);
671 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000672 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Michael Handc691572012-07-23 18:48:41 +0000673 << Attr.getName();
674 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000675 }
676 }
677
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000678 // Check that all arguments are lockable objects.
679 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000680 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000681 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000682
Michael Handc691572012-07-23 18:48:41 +0000683 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000684}
685
Michael Hanf1aae3b2012-08-03 17:40:43 +0000686static void handleAcquiredAfterAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000687 const AttributeList &Attr) {
688 SmallVector<Expr*, 1> Args;
689 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
690 return;
691
692 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000693 D->addAttr(::new (S.Context)
694 AcquiredAfterAttr(Attr.getRange(), S.Context,
695 StartArg, Args.size(),
696 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000697}
698
Michael Hanf1aae3b2012-08-03 17:40:43 +0000699static void handleAcquiredBeforeAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000700 const AttributeList &Attr) {
701 SmallVector<Expr*, 1> Args;
702 if (!checkAcquireOrderAttrCommon(S, D, Attr, Args))
703 return;
704
705 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000706 D->addAttr(::new (S.Context)
707 AcquiredBeforeAttr(Attr.getRange(), S.Context,
708 StartArg, Args.size(),
709 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000710}
711
Michael Hanf1aae3b2012-08-03 17:40:43 +0000712static bool checkLockFunAttrCommon(Sema &S, Decl *D,
713 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000714 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000715 // zero or more arguments ok
716
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000717 // check that the attribute is applied to a function
718 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000719 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
720 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000721 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000722 }
723
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000724 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000725 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000726
Michael Handc691572012-07-23 18:48:41 +0000727 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000728}
729
Michael Hanf1aae3b2012-08-03 17:40:43 +0000730static void handleSharedLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000731 const AttributeList &Attr) {
732 SmallVector<Expr*, 1> Args;
733 if (!checkLockFunAttrCommon(S, D, Attr, Args))
734 return;
735
736 unsigned Size = Args.size();
737 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000738 D->addAttr(::new (S.Context)
739 SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
740 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000741}
742
Michael Hanf1aae3b2012-08-03 17:40:43 +0000743static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000744 const AttributeList &Attr) {
745 SmallVector<Expr*, 1> Args;
746 if (!checkLockFunAttrCommon(S, D, Attr, Args))
747 return;
748
749 unsigned Size = Args.size();
750 Expr **StartArg = Size == 0 ? 0 : &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000751 D->addAttr(::new (S.Context)
752 ExclusiveLockFunctionAttr(Attr.getRange(), S.Context,
753 StartArg, Size,
754 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000755}
756
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +0000757static void handleAssertSharedLockAttr(Sema &S, Decl *D,
758 const AttributeList &Attr) {
759 SmallVector<Expr*, 1> Args;
760 if (!checkLockFunAttrCommon(S, D, Attr, Args))
761 return;
762
763 unsigned Size = Args.size();
764 Expr **StartArg = Size == 0 ? 0 : &Args[0];
765 D->addAttr(::new (S.Context)
766 AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size,
767 Attr.getAttributeSpellingListIndex()));
768}
769
770static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
771 const AttributeList &Attr) {
772 SmallVector<Expr*, 1> Args;
773 if (!checkLockFunAttrCommon(S, D, Attr, Args))
774 return;
775
776 unsigned Size = Args.size();
777 Expr **StartArg = Size == 0 ? 0 : &Args[0];
778 D->addAttr(::new (S.Context)
779 AssertExclusiveLockAttr(Attr.getRange(), S.Context,
780 StartArg, Size,
781 Attr.getAttributeSpellingListIndex()));
782}
783
784
Michael Hanf1aae3b2012-08-03 17:40:43 +0000785static bool checkTryLockFunAttrCommon(Sema &S, Decl *D,
786 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000787 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000788 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000789 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000790
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000791 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000792 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
793 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000794 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000795 }
796
Aaron Ballman624421f2013-08-31 01:11:41 +0000797 if (!isIntOrBool(Attr.getArgAsExpr(0))) {
Aaron Ballman437d43f2013-07-23 14:03:57 +0000798 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000799 << Attr.getName() << 1 << AANT_ArgumentIntOrBool;
Michael Handc691572012-07-23 18:48:41 +0000800 return false;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000801 }
802
803 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000804 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000805
Michael Handc691572012-07-23 18:48:41 +0000806 return true;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000807}
808
Michael Hanf1aae3b2012-08-03 17:40:43 +0000809static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000810 const AttributeList &Attr) {
811 SmallVector<Expr*, 2> Args;
812 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
813 return;
814
Michael Han51d8c522013-01-24 16:46:58 +0000815 D->addAttr(::new (S.Context)
816 SharedTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman624421f2013-08-31 01:11:41 +0000817 Attr.getArgAsExpr(0),
818 Args.data(), Args.size(),
Michael Han51d8c522013-01-24 16:46:58 +0000819 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000820}
821
Michael Hanf1aae3b2012-08-03 17:40:43 +0000822static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000823 const AttributeList &Attr) {
824 SmallVector<Expr*, 2> Args;
825 if (!checkTryLockFunAttrCommon(S, D, Attr, Args))
826 return;
827
Michael Han51d8c522013-01-24 16:46:58 +0000828 D->addAttr(::new (S.Context)
829 ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context,
Aaron Ballman624421f2013-08-31 01:11:41 +0000830 Attr.getArgAsExpr(0),
831 Args.data(), Args.size(),
Michael Han51d8c522013-01-24 16:46:58 +0000832 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000833}
834
Michael Hanf1aae3b2012-08-03 17:40:43 +0000835static bool checkLocksRequiredCommon(Sema &S, Decl *D,
836 const AttributeList &Attr,
Craig Topper6b9240e2013-07-05 19:34:19 +0000837 SmallVectorImpl<Expr *> &Args) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000838 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Michael Handc691572012-07-23 18:48:41 +0000839 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000840
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000841 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000842 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
843 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Michael Handc691572012-07-23 18:48:41 +0000844 return false;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000845 }
846
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000847 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000848 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Dmitri Gribenko1f78a502013-05-03 15:05:50 +0000849 if (Args.empty())
Michael Handc691572012-07-23 18:48:41 +0000850 return false;
Michael Hanf1aae3b2012-08-03 17:40:43 +0000851
Michael Handc691572012-07-23 18:48:41 +0000852 return true;
853}
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000854
Michael Hanf1aae3b2012-08-03 17:40:43 +0000855static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000856 const AttributeList &Attr) {
857 SmallVector<Expr*, 1> Args;
858 if (!checkLocksRequiredCommon(S, D, Attr, Args))
859 return;
860
861 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000862 D->addAttr(::new (S.Context)
863 ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context,
864 StartArg, Args.size(),
865 Attr.getAttributeSpellingListIndex()));
Michael Handc691572012-07-23 18:48:41 +0000866}
867
Michael Hanf1aae3b2012-08-03 17:40:43 +0000868static void handleSharedLocksRequiredAttr(Sema &S, Decl *D,
Michael Handc691572012-07-23 18:48:41 +0000869 const AttributeList &Attr) {
870 SmallVector<Expr*, 1> Args;
871 if (!checkLocksRequiredCommon(S, D, Attr, Args))
872 return;
873
874 Expr **StartArg = &Args[0];
Michael Han51d8c522013-01-24 16:46:58 +0000875 D->addAttr(::new (S.Context)
876 SharedLocksRequiredAttr(Attr.getRange(), S.Context,
877 StartArg, Args.size(),
878 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000879}
880
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000881static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000882 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000883 // zero or more arguments ok
884
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000885 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000886 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
887 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000888 return;
889 }
890
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000891 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000892 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000893 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000894 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000895 Expr **StartArg = Size == 0 ? 0 : &Args[0];
896
Michael Han51d8c522013-01-24 16:46:58 +0000897 D->addAttr(::new (S.Context)
898 UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size,
899 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000900}
901
902static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000903 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000904 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000905 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
906 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000907 return;
908 }
909
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000910 // check that the argument is lockable object
DeLesley Hutchinsf26efd72012-05-02 17:38:37 +0000911 SmallVector<Expr*, 1> Args;
912 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
913 unsigned Size = Args.size();
914 if (Size == 0)
915 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000916
Michael Han51d8c522013-01-24 16:46:58 +0000917 D->addAttr(::new (S.Context)
918 LockReturnedAttr(Attr.getRange(), S.Context, Args[0],
919 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000920}
921
922static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000923 const AttributeList &Attr) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000924 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000925 return;
926
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000927 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
DeLesley Hutchins0aa52aa2012-06-19 23:25:19 +0000928 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type)
929 << Attr.getName() << ThreadExpectedFunctionOrMethod;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000930 return;
931 }
932
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000933 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000934 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000935 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000936 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000937 if (Size == 0)
938 return;
939 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000940
Michael Han51d8c522013-01-24 16:46:58 +0000941 D->addAttr(::new (S.Context)
942 LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size,
943 Attr.getAttributeSpellingListIndex()));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000944}
945
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +0000946static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
David Blaikiea33ab602013-09-06 01:28:43 +0000947 ConsumableAttr::ConsumedState DefaultState;
948
949 if (Attr.isArgIdent(0)) {
Aaron Ballmand0686072013-09-11 19:47:58 +0000950 IdentifierLoc *IL = Attr.getArgAsIdent(0);
951 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
952 DefaultState)) {
953 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
954 << Attr.getName() << IL->Ident;
David Blaikiea33ab602013-09-06 01:28:43 +0000955 return;
956 }
David Blaikiea33ab602013-09-06 01:28:43 +0000957 } else {
958 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
959 << Attr.getName() << AANT_ArgumentIdentifier;
960 return;
961 }
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +0000962
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +0000963 if (!isa<CXXRecordDecl>(D)) {
964 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
965 Attr.getName() << ExpectedClass;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +0000966 return;
967 }
968
969 D->addAttr(::new (S.Context)
David Blaikiea33ab602013-09-06 01:28:43 +0000970 ConsumableAttr(Attr.getRange(), S.Context, DefaultState,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +0000971 Attr.getAttributeSpellingListIndex()));
972}
973
974static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
975 const AttributeList &Attr) {
976 ASTContext &CurrContext = S.getASTContext();
977 QualType ThisType = MD->getThisType(CurrContext)->getPointeeType();
978
979 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
980 if (!RD->hasAttr<ConsumableAttr>()) {
981 S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) <<
982 RD->getNameAsString();
983
984 return false;
985 }
986 }
987
988 return true;
989}
990
991static void handleConsumesAttr(Sema &S, Decl *D, const AttributeList &Attr) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +0000992 if (!isa<CXXMethodDecl>(D)) {
993 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
994 Attr.getName() << ExpectedMethod;
995 return;
996 }
997
998 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
999 return;
1000
1001 D->addAttr(::new (S.Context)
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001002 ConsumesAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001003 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001004}
1005
1006static void handleCallableWhenUnconsumedAttr(Sema &S, Decl *D,
1007 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001008 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001009 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1010 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001011 return;
1012 }
1013
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001014 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1015 return;
1016
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001017 D->addAttr(::new (S.Context)
1018 CallableWhenUnconsumedAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001019 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001020}
1021
1022static void handleTestsConsumedAttr(Sema &S, Decl *D,
1023 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001024 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001025 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1026 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001027 return;
1028 }
1029
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001030 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1031 return;
1032
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001033 D->addAttr(::new (S.Context)
1034 TestsConsumedAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001035 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001036}
1037
1038static void handleTestsUnconsumedAttr(Sema &S, Decl *D,
1039 const AttributeList &Attr) {
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001040 if (!isa<CXXMethodDecl>(D)) {
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001041 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1042 Attr.getName() << ExpectedMethod;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001043 return;
1044 }
1045
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001046 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
1047 return;
1048
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001049 D->addAttr(::new (S.Context)
1050 TestsUnconsumedAttr(Attr.getRange(), S.Context,
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00001051 Attr.getAttributeSpellingListIndex()));
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001052}
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00001053
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001054static void handleReturnTypestateAttr(Sema &S, Decl *D,
1055 const AttributeList &Attr) {
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001056 ReturnTypestateAttr::ConsumedState ReturnState;
1057
1058 if (Attr.isArgIdent(0)) {
Aaron Ballmand0686072013-09-11 19:47:58 +00001059 IdentifierLoc *IL = Attr.getArgAsIdent(0);
1060 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1061 ReturnState)) {
1062 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported)
1063 << Attr.getName() << IL->Ident;
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001064 return;
1065 }
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00001066 } else {
1067 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
1068 Attr.getName() << AANT_ArgumentIdentifier;
1069 return;
1070 }
1071
1072 if (!isa<FunctionDecl>(D)) {
1073 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
1074 Attr.getName() << ExpectedFunction;
1075 return;
1076 }
1077
1078 // FIXME: This check is currently being done in the analysis. It can be
1079 // enabled here only after the parser propagates attributes at
1080 // template specialization definition, not declaration.
1081 //QualType ReturnType;
1082 //
1083 //if (const CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1084 // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType();
1085 //
1086 //} else {
1087 //
1088 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1089 //}
1090 //
1091 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1092 //
1093 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1094 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1095 // ReturnType.getAsString();
1096 // return;
1097 //}
1098
1099 D->addAttr(::new (S.Context)
1100 ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState,
1101 Attr.getAttributeSpellingListIndex()));
1102}
1103
Chandler Carruth1b03c872011-07-02 00:01:44 +00001104static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
1105 const AttributeList &Attr) {
Richard Smitha4fa9002013-01-13 02:11:23 +00001106 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
1107 if (TD == 0) {
1108 // __attribute__((ext_vector_type(N))) can only be applied to typedefs
1109 // and type-ids.
Chris Lattner803d0802008-06-29 00:43:07 +00001110 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +00001111 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001112 }
Mike Stumpbf916502009-07-24 19:02:52 +00001113
Richard Smitha4fa9002013-01-13 02:11:23 +00001114 // Remember this typedef decl, we will need it later for diagnostics.
1115 S.ExtVectorDecls.push_back(TD);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001116}
1117
Chandler Carruth1b03c872011-07-02 00:01:44 +00001118static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001119 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001120 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +00001121 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001122 // If the alignment is less than or equal to 8 bits, the packed attribute
1123 // has no effect.
Eli Friedmanb68ec6b2012-11-07 00:35:20 +00001124 if (!FD->getType()->isDependentType() &&
1125 !FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +00001126 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001127 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001128 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001129 else
Michael Han51d8c522013-01-24 16:46:58 +00001130 FD->addAttr(::new (S.Context)
1131 PackedAttr(Attr.getRange(), S.Context,
1132 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001133 } else
Chris Lattner3c73c412008-11-19 08:23:25 +00001134 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001135}
1136
Chandler Carruth1b03c872011-07-02 00:01:44 +00001137static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman5f608ae2012-10-12 23:29:20 +00001138 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001139 RD->addAttr(::new (S.Context)
1140 MsStructAttr(Attr.getRange(), S.Context,
1141 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +00001142 else
1143 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1144}
1145
Chandler Carruth1b03c872011-07-02 00:01:44 +00001146static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001147 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +00001148 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001149 if (MD->isInstanceMethod()) {
Michael Han51d8c522013-01-24 16:46:58 +00001150 D->addAttr(::new (S.Context)
1151 IBActionAttr(Attr.getRange(), S.Context,
1152 Attr.getAttributeSpellingListIndex()));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001153 return;
1154 }
1155
Ted Kremenek4ee2bb12011-02-04 06:54:16 +00001156 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001157}
1158
Ted Kremenek2f041d02011-09-29 07:02:25 +00001159static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
1160 // The IBOutlet/IBOutletCollection attributes only apply to instance
1161 // variables or properties of Objective-C classes. The outlet must also
1162 // have an object reference type.
1163 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
1164 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +00001165 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001166 << Attr.getName() << VD->getType() << 0;
1167 return false;
1168 }
1169 }
1170 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1171 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001172 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +00001173 << Attr.getName() << PD->getType() << 1;
1174 return false;
1175 }
1176 }
1177 else {
1178 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
1179 return false;
1180 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +00001181
Ted Kremenek2f041d02011-09-29 07:02:25 +00001182 return true;
1183}
1184
Chandler Carruth1b03c872011-07-02 00:01:44 +00001185static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek2f041d02011-09-29 07:02:25 +00001186 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001187 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001188
Michael Han51d8c522013-01-24 16:46:58 +00001189 D->addAttr(::new (S.Context)
1190 IBOutletAttr(Attr.getRange(), S.Context,
1191 Attr.getAttributeSpellingListIndex()));
Ted Kremenek96329d42008-07-15 22:26:48 +00001192}
1193
Chandler Carruth1b03c872011-07-02 00:01:44 +00001194static void handleIBOutletCollection(Sema &S, Decl *D,
1195 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +00001196
1197 // The iboutletcollection attribute can have zero or one arguments.
Aaron Ballman624421f2013-08-31 01:11:41 +00001198 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001199 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1200 << Attr.getName() << 1;
Ted Kremenek857e9182010-05-19 17:38:06 +00001201 return;
1202 }
1203
Ted Kremenek2f041d02011-09-29 07:02:25 +00001204 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +00001205 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +00001206
Aaron Ballman624421f2013-08-31 01:11:41 +00001207 IdentifierLoc *IL = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
1208 IdentifierInfo *II;
1209 SourceLocation ILS;
1210 if (IL) {
1211 II = IL->Ident;
1212 ILS = IL->Loc;
1213 } else {
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001214 II = &S.Context.Idents.get("NSObject");
Aaron Ballman624421f2013-08-31 01:11:41 +00001215 }
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +00001216
John McCallb3d87482010-08-24 05:47:05 +00001217 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +00001218 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001219 if (!TypeRep) {
1220 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1221 return;
1222 }
John McCallb3d87482010-08-24 05:47:05 +00001223 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001224 // Diagnose use of non-object type in iboutletcollection attribute.
1225 // FIXME. Gnu attribute extension ignores use of builtin types in
1226 // attributes. So, __attribute__((iboutletcollection(char))) will be
1227 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +00001228 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +00001229 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
1230 return;
1231 }
Michael Han51d8c522013-01-24 16:46:58 +00001232 D->addAttr(::new (S.Context)
Aaron Ballman624421f2013-08-31 01:11:41 +00001233 IBOutletCollectionAttr(Attr.getRange(), S.Context, QT, ILS,
Michael Han51d8c522013-01-24 16:46:58 +00001234 Attr.getAttributeSpellingListIndex()));
Ted Kremenek857e9182010-05-19 17:38:06 +00001235}
1236
Chandler Carruthd309c812011-07-01 23:49:16 +00001237static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001238 if (const RecordType *UT = T->getAsUnionType())
1239 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1240 RecordDecl *UD = UT->getDecl();
1241 for (RecordDecl::field_iterator it = UD->field_begin(),
1242 itend = UD->field_end(); it != itend; ++it) {
1243 QualType QT = it->getType();
1244 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
1245 T = QT;
1246 return;
1247 }
1248 }
1249 }
1250}
1251
Nuno Lopes587de5b2012-05-24 00:22:00 +00001252static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nuno Lopes174930d2012-06-18 16:39:04 +00001253 if (!isFunctionOrMethod(D)) {
1254 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001255 << Attr.getName() << ExpectedFunctionOrMethod;
Nuno Lopes174930d2012-06-18 16:39:04 +00001256 return;
1257 }
1258
Nuno Lopes587de5b2012-05-24 00:22:00 +00001259 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
1260 return;
1261
Nuno Lopes587de5b2012-05-24 00:22:00 +00001262 SmallVector<unsigned, 8> SizeArgs;
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001263 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001264 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001265 uint64_t Idx;
1266 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1267 Attr.getLoc(), i + 1, Ex, Idx))
Nuno Lopes587de5b2012-05-24 00:22:00 +00001268 return;
Nuno Lopes587de5b2012-05-24 00:22:00 +00001269
1270 // check if the function argument is of an integer type
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001271 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001272 if (!T->isIntegerType()) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00001273 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1274 << Attr.getName() << AANT_ArgumentIntegerConstant
1275 << Ex->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001276 return;
1277 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001278 SizeArgs.push_back(Idx);
Nuno Lopes587de5b2012-05-24 00:22:00 +00001279 }
1280
1281 // check if the function returns a pointer
1282 if (!getFunctionType(D)->getResultType()->isAnyPointerType()) {
1283 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001284 << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange();
Nuno Lopes587de5b2012-05-24 00:22:00 +00001285 }
1286
Michael Han51d8c522013-01-24 16:46:58 +00001287 D->addAttr(::new (S.Context)
1288 AllocSizeAttr(Attr.getRange(), S.Context,
1289 SizeArgs.data(), SizeArgs.size(),
1290 Attr.getAttributeSpellingListIndex()));
Nuno Lopes587de5b2012-05-24 00:22:00 +00001291}
1292
Chandler Carruth1b03c872011-07-02 00:01:44 +00001293static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001294 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
1295 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +00001296 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001297 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001298 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001299 return;
1300 }
Mike Stumpbf916502009-07-24 19:02:52 +00001301
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001302 SmallVector<unsigned, 8> NonNullArgs;
1303 for (unsigned i = 0; i < Attr.getNumArgs(); ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001304 Expr *Ex = Attr.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001305 uint64_t Idx;
1306 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
1307 Attr.getLoc(), i + 1, Ex, Idx))
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001308 return;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001309
1310 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001311 QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001312 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +00001313
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001314 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001315 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +00001316 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001317 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001318 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001319 }
Mike Stumpbf916502009-07-24 19:02:52 +00001320
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001321 NonNullArgs.push_back(Idx);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001322 }
Mike Stumpbf916502009-07-24 19:02:52 +00001323
1324 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1325 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001326 if (NonNullArgs.empty()) {
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001327 for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) {
1328 QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +00001329 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00001330 if (T->isAnyPointerType() || T->isBlockPointerType())
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001331 NonNullArgs.push_back(i);
Ted Kremenek46bbaca2008-11-18 06:52:58 +00001332 }
Mike Stumpbf916502009-07-24 19:02:52 +00001333
Ted Kremenekee1c08c2010-10-21 18:49:36 +00001334 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001335 if (NonNullArgs.empty()) {
1336 // Warn the trivial case only if attribute is not coming from a
1337 // macro instantiation.
1338 if (Attr.getLoc().isFileID())
1339 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001340 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001341 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001342 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001343
Nick Lewycky5d9484d2013-01-24 01:12:16 +00001344 unsigned *start = &NonNullArgs[0];
Ted Kremenek7fb43c12008-09-01 19:57:52 +00001345 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001346 llvm::array_pod_sort(start, start + size);
Michael Han51d8c522013-01-24 16:46:58 +00001347 D->addAttr(::new (S.Context)
1348 NonNullAttr(Attr.getRange(), S.Context, start, size,
1349 Attr.getAttributeSpellingListIndex()));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +00001350}
1351
Chandler Carruth1b03c872011-07-02 00:01:44 +00001352static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001353 // This attribute must be applied to a function declaration.
1354 // The first argument to the attribute must be a string,
1355 // the name of the resource, for example "malloc".
1356 // The following arguments must be argument indexes, the arguments must be
1357 // of integer type for Returns, otherwise of pointer type.
1358 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +00001359 // after being held. free() should be __attribute((ownership_takes)), whereas
1360 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001361
Aaron Ballman624421f2013-08-31 01:11:41 +00001362 if (!AL.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001363 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001364 << AL.getName()->getName() << 1 << AANT_ArgumentString;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001365 return;
1366 }
1367 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +00001368 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +00001369 switch (AL.getKind()) {
1370 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +00001371 K = OwnershipAttr::Takes;
Aaron Ballman624421f2013-08-31 01:11:41 +00001372 if (AL.getNumArgs() < 2) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001373 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1374 << AL.getName() << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001375 return;
1376 }
Jordy Rose2a479922010-08-12 08:54:03 +00001377 break;
1378 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +00001379 K = OwnershipAttr::Holds;
Aaron Ballman624421f2013-08-31 01:11:41 +00001380 if (AL.getNumArgs() < 2) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001381 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1382 << AL.getName() << 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001383 return;
1384 }
Jordy Rose2a479922010-08-12 08:54:03 +00001385 break;
1386 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +00001387 K = OwnershipAttr::Returns;
Aaron Ballman624421f2013-08-31 01:11:41 +00001388 if (AL.getNumArgs() > 2) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001389 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
Aaron Ballman624421f2013-08-31 01:11:41 +00001390 << AL.getName() << AL.getNumArgs() + 2;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001391 return;
1392 }
Jordy Rose2a479922010-08-12 08:54:03 +00001393 break;
1394 default:
1395 // This should never happen given how we are called.
1396 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001397 }
1398
Chandler Carruth87c44602011-07-01 23:49:12 +00001399 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001400 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1401 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001402 return;
1403 }
1404
Aaron Ballman624421f2013-08-31 01:11:41 +00001405 StringRef Module = AL.getArgAsIdent(0)->Ident->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001406
1407 // Normalize the argument, __foo__ becomes foo.
1408 if (Module.startswith("__") && Module.endswith("__"))
1409 Module = Module.substr(2, Module.size() - 4);
1410
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001411
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001412 SmallVector<unsigned, 8> OwnershipArgs;
Aaron Ballman624421f2013-08-31 01:11:41 +00001413 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1414 Expr *Ex = AL.getArgAsExpr(i);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001415 uint64_t Idx;
1416 if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(),
Aaron Ballman624421f2013-08-31 01:11:41 +00001417 AL.getLoc(), i, Ex, Idx))
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001418 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001419
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001420 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001421 case OwnershipAttr::Takes:
1422 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001423 // Is the function argument a pointer type?
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001424 QualType T = getFunctionOrMethodArgType(D, Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001425 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1426 // FIXME: Should also highlight argument in decl.
1427 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001428 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001429 << "pointer"
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001430 << Ex->getSourceRange();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001431 continue;
1432 }
1433 break;
1434 }
Sean Huntcf807c42010-08-18 23:23:40 +00001435 case OwnershipAttr::Returns: {
Aaron Ballman624421f2013-08-31 01:11:41 +00001436 if (AL.getNumArgs() > 2) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001437 // Is the function argument an integer type?
Aaron Ballman624421f2013-08-31 01:11:41 +00001438 Expr *IdxExpr = AL.getArgAsExpr(1);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001439 llvm::APSInt ArgNum(32);
1440 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1441 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1442 S.Diag(AL.getLoc(), diag::err_ownership_type)
1443 << "ownership_returns" << "integer"
1444 << IdxExpr->getSourceRange();
1445 return;
1446 }
1447 }
1448 break;
1449 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001450 } // switch
1451
1452 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001453 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001454 i = D->specific_attr_begin<OwnershipAttr>(),
1455 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001456 i != e; ++i) {
1457 if ((*i)->getOwnKind() != K) {
1458 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1459 I!=E; ++I) {
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001460 if (Idx == *I) {
Sean Huntcf807c42010-08-18 23:23:40 +00001461 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1462 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001463 }
1464 }
1465 }
1466 }
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00001467 OwnershipArgs.push_back(Idx);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001468 }
1469
1470 unsigned* start = OwnershipArgs.data();
1471 unsigned size = OwnershipArgs.size();
1472 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001473
1474 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001475 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
1476 << AL.getName() << 2;
Sean Huntcf807c42010-08-18 23:23:40 +00001477 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001478 }
Sean Huntcf807c42010-08-18 23:23:40 +00001479
Michael Han51d8c522013-01-24 16:46:58 +00001480 D->addAttr(::new (S.Context)
1481 OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size,
1482 AL.getAttributeSpellingListIndex()));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001483}
1484
Chandler Carruth1b03c872011-07-02 00:01:44 +00001485static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001486 // Check the attribute arguments.
1487 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00001488 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1489 << Attr.getName() << 1;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001490 return;
1491 }
1492
Chandler Carruth87c44602011-07-01 23:49:12 +00001493 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001494 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001495 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001496 return;
1497 }
1498
Chandler Carruth87c44602011-07-01 23:49:12 +00001499 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001500
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001501 // gcc rejects
1502 // class c {
1503 // static int a __attribute__((weakref ("v2")));
1504 // static int b() __attribute__((weakref ("f3")));
1505 // };
1506 // and ignores the attributes of
1507 // void f(void) {
1508 // static int a __attribute__((weakref ("v2")));
1509 // }
1510 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001511 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001512 if (!Ctx->isFileContext()) {
1513 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001514 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001515 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001516 }
1517
1518 // The GCC manual says
1519 //
1520 // At present, a declaration to which `weakref' is attached can only
1521 // be `static'.
1522 //
1523 // It also says
1524 //
1525 // Without a TARGET,
1526 // given as an argument to `weakref' or to `alias', `weakref' is
1527 // equivalent to `weak'.
1528 //
1529 // gcc 4.4.1 will accept
1530 // int a7 __attribute__((weakref));
1531 // as
1532 // int a7 __attribute__((weak));
1533 // This looks like a bug in gcc. We reject that for now. We should revisit
1534 // it if this behaviour is actually used.
1535
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001536 // GCC rejects
1537 // static ((alias ("y"), weakref)).
1538 // Should we? How to check that weakref is before or after alias?
1539
Aaron Ballmanbe116e22013-09-09 23:40:31 +00001540 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1541 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1542 // StringRef parameter it was given anyway.
Aaron Ballman624421f2013-08-31 01:11:41 +00001543 if (Attr.isArgExpr(0)) {
1544 Expr *Arg = Attr.getArgAsExpr(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001545 Arg = Arg->IgnoreParenCasts();
1546 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1547
Douglas Gregor5cee1192011-07-27 05:40:30 +00001548 if (!Str || !Str->isAscii()) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001549 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001550 << Attr.getName() << 1 << AANT_ArgumentString;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001551 return;
1552 }
1553 // GCC will accept anything as the argument of weakref. Should we
1554 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001555 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001556 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001557 }
1558
Michael Han51d8c522013-01-24 16:46:58 +00001559 D->addAttr(::new (S.Context)
1560 WeakRefAttr(Attr.getRange(), S.Context,
1561 Attr.getAttributeSpellingListIndex()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001562}
1563
Chandler Carruth1b03c872011-07-02 00:01:44 +00001564static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001565 StringLiteral *Str = 0;
1566 if (Attr.isArgExpr(0))
1567 Str = dyn_cast<StringLiteral>(Attr.getArgAsExpr(0)->IgnoreParenCasts());
Mike Stumpbf916502009-07-24 19:02:52 +00001568
Douglas Gregor5cee1192011-07-27 05:40:30 +00001569 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001570 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1571 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001572 return;
1573 }
Mike Stumpbf916502009-07-24 19:02:52 +00001574
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001575 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001576 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1577 return;
1578 }
1579
Chris Lattner6b6b5372008-06-26 18:38:35 +00001580 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001581
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001582 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00001583 Str->getString(),
1584 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001585}
1586
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001587static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001588 if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) {
1589 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1590 << Attr.getName() << ExpectedFunctionOrMethod;
1591 return;
1592 }
1593
Michael Han51d8c522013-01-24 16:46:58 +00001594 D->addAttr(::new (S.Context)
1595 MinSizeAttr(Attr.getRange(), S.Context,
1596 Attr.getAttributeSpellingListIndex()));
Quentin Colombetaee56fa2012-11-01 23:55:47 +00001597}
1598
Benjamin Krameree409a92012-05-12 21:10:52 +00001599static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Krameree409a92012-05-12 21:10:52 +00001600 if (!isa<FunctionDecl>(D)) {
1601 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1602 << Attr.getName() << ExpectedFunction;
1603 return;
1604 }
1605
1606 if (D->hasAttr<HotAttr>()) {
1607 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1608 << Attr.getName() << "hot";
1609 return;
1610 }
1611
Michael Han51d8c522013-01-24 16:46:58 +00001612 D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context,
1613 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001614}
1615
1616static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Benjamin Krameree409a92012-05-12 21:10:52 +00001617 if (!isa<FunctionDecl>(D)) {
1618 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1619 << Attr.getName() << ExpectedFunction;
1620 return;
1621 }
1622
1623 if (D->hasAttr<ColdAttr>()) {
1624 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1625 << Attr.getName() << "cold";
1626 return;
1627 }
1628
Michael Han51d8c522013-01-24 16:46:58 +00001629 D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context,
1630 Attr.getAttributeSpellingListIndex()));
Benjamin Krameree409a92012-05-12 21:10:52 +00001631}
1632
Chandler Carruth1b03c872011-07-02 00:01:44 +00001633static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001634 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001635 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001636 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001637 return;
1638 }
1639
Michael Han51d8c522013-01-24 16:46:58 +00001640 D->addAttr(::new (S.Context)
1641 NakedAttr(Attr.getRange(), S.Context,
1642 Attr.getAttributeSpellingListIndex()));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001643}
1644
Chandler Carruth1b03c872011-07-02 00:01:44 +00001645static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1646 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001647 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001648 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001649 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001650 return;
1651 }
Mike Stumpbf916502009-07-24 19:02:52 +00001652
Michael Han51d8c522013-01-24 16:46:58 +00001653 D->addAttr(::new (S.Context)
1654 AlwaysInlineAttr(Attr.getRange(), S.Context,
1655 Attr.getAttributeSpellingListIndex()));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001656}
1657
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001658static void handleTLSModelAttr(Sema &S, Decl *D,
1659 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001660 Expr *Arg = Attr.getArgAsExpr(0);
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001661 Arg = Arg->IgnoreParenCasts();
1662 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1663
1664 // Check that it is a string.
1665 if (!Str) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001666 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
1667 << Attr.getName() << AANT_ArgumentString;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001668 return;
1669 }
1670
Richard Smith38afbc72013-04-13 02:43:54 +00001671 if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) {
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001672 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1673 << Attr.getName() << ExpectedTLSVar;
1674 return;
1675 }
1676
1677 // Check that the value.
1678 StringRef Model = Str->getString();
1679 if (Model != "global-dynamic" && Model != "local-dynamic"
1680 && Model != "initial-exec" && Model != "local-exec") {
1681 S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg);
1682 return;
1683 }
1684
Michael Han51d8c522013-01-24 16:46:58 +00001685 D->addAttr(::new (S.Context)
1686 TLSModelAttr(Attr.getRange(), S.Context, Model,
1687 Attr.getAttributeSpellingListIndex()));
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001688}
1689
Chandler Carruth1b03c872011-07-02 00:01:44 +00001690static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001691 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001692 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001693 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Michael Han51d8c522013-01-24 16:46:58 +00001694 D->addAttr(::new (S.Context)
1695 MallocAttr(Attr.getRange(), S.Context,
1696 Attr.getAttributeSpellingListIndex()));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001697 return;
1698 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001699 }
1700
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001701 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001702}
1703
Chandler Carruth1b03c872011-07-02 00:01:44 +00001704static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00001705 D->addAttr(::new (S.Context)
1706 MayAliasAttr(Attr.getRange(), S.Context,
1707 Attr.getAttributeSpellingListIndex()));
Dan Gohman34c26302010-11-17 00:03:07 +00001708}
1709
Chandler Carruth1b03c872011-07-02 00:01:44 +00001710static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001711 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001712 D->addAttr(::new (S.Context)
1713 NoCommonAttr(Attr.getRange(), S.Context,
1714 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001715 else
1716 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001717 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001718}
1719
Chandler Carruth1b03c872011-07-02 00:01:44 +00001720static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eli Friedman3e1aca22013-06-20 22:55:04 +00001721 if (S.LangOpts.CPlusPlus) {
1722 S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus);
1723 return;
1724 }
1725
Chandler Carruth87c44602011-07-01 23:49:12 +00001726 if (isa<VarDecl>(D))
Michael Han51d8c522013-01-24 16:46:58 +00001727 D->addAttr(::new (S.Context)
1728 CommonAttr(Attr.getRange(), S.Context,
1729 Attr.getAttributeSpellingListIndex()));
Eric Christopher722109c2010-12-03 06:58:14 +00001730 else
1731 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001732 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001733}
1734
Chandler Carruth1b03c872011-07-02 00:01:44 +00001735static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001736 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001737
1738 if (S.CheckNoReturnAttr(attr)) return;
1739
Chandler Carruth87c44602011-07-01 23:49:12 +00001740 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001741 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001742 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001743 return;
1744 }
1745
Michael Han51d8c522013-01-24 16:46:58 +00001746 D->addAttr(::new (S.Context)
1747 NoReturnAttr(attr.getRange(), S.Context,
1748 attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00001749}
1750
1751bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001752 if (!checkAttributeNumArgs(*this, attr, 0)) {
John McCall711c52b2011-01-05 12:14:39 +00001753 attr.setInvalid();
1754 return true;
1755 }
1756
1757 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001758}
1759
Chandler Carruth1b03c872011-07-02 00:01:44 +00001760static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1761 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001762
1763 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1764 // because 'analyzer_noreturn' does not impact the type.
Chandler Carruth87c44602011-07-01 23:49:12 +00001765 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1766 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001767 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1768 && !VD->getType()->isFunctionPointerType())) {
1769 S.Diag(Attr.getLoc(),
Richard Smith4e24f0f2013-01-02 12:01:23 +00001770 Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001771 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001772 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001773 return;
1774 }
1775 }
1776
Michael Han51d8c522013-01-24 16:46:58 +00001777 D->addAttr(::new (S.Context)
1778 AnalyzerNoReturnAttr(Attr.getRange(), S.Context,
1779 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001780}
1781
Richard Smithcd8ab512013-01-17 01:30:42 +00001782static void handleCXX11NoReturnAttr(Sema &S, Decl *D,
1783 const AttributeList &Attr) {
1784 // C++11 [dcl.attr.noreturn]p1:
1785 // The attribute may be applied to the declarator-id in a function
1786 // declaration.
1787 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1788 if (!FD) {
1789 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1790 << Attr.getName() << ExpectedFunctionOrMethod;
1791 return;
1792 }
1793
Michael Han51d8c522013-01-24 16:46:58 +00001794 D->addAttr(::new (S.Context)
1795 CXX11NoReturnAttr(Attr.getRange(), S.Context,
1796 Attr.getAttributeSpellingListIndex()));
Richard Smithcd8ab512013-01-17 01:30:42 +00001797}
1798
John Thompson35cc9622010-08-09 21:53:52 +00001799// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001800static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001801/*
1802 Returning a Vector Class in Registers
1803
Eric Christopherf48f3672010-12-01 22:13:54 +00001804 According to the PPU ABI specifications, a class with a single member of
1805 vector type is returned in memory when used as the return value of a function.
1806 This results in inefficient code when implementing vector classes. To return
1807 the value in a single vector register, add the vecreturn attribute to the
1808 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001809
1810 Example:
1811
1812 struct Vector
1813 {
1814 __vector float xyzw;
1815 } __attribute__((vecreturn));
1816
1817 Vector Add(Vector lhs, Vector rhs)
1818 {
1819 Vector result;
1820 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1821 return result; // This will be returned in a register
1822 }
1823*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001824 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001825 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001826 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001827 return;
1828 }
1829
Chandler Carruth87c44602011-07-01 23:49:12 +00001830 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001831 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1832 return;
1833 }
1834
Chandler Carruth87c44602011-07-01 23:49:12 +00001835 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001836 int count = 0;
1837
1838 if (!isa<CXXRecordDecl>(record)) {
1839 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1840 return;
1841 }
1842
1843 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1844 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1845 return;
1846 }
1847
Eric Christopherf48f3672010-12-01 22:13:54 +00001848 for (RecordDecl::field_iterator iter = record->field_begin();
1849 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001850 if ((count == 1) || !iter->getType()->isVectorType()) {
1851 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1852 return;
1853 }
1854 count++;
1855 }
1856
Michael Han51d8c522013-01-24 16:46:58 +00001857 D->addAttr(::new (S.Context)
1858 VecReturnAttr(Attr.getRange(), S.Context,
1859 Attr.getAttributeSpellingListIndex()));
John Thompson35cc9622010-08-09 21:53:52 +00001860}
1861
Richard Smith3a2b7a12013-01-28 22:42:45 +00001862static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
1863 const AttributeList &Attr) {
1864 if (isa<ParmVarDecl>(D)) {
1865 // [[carries_dependency]] can only be applied to a parameter if it is a
1866 // parameter of a function declaration or lambda.
1867 if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
1868 S.Diag(Attr.getLoc(),
1869 diag::err_carries_dependency_param_not_function_decl);
1870 return;
1871 }
1872 } else if (!isa<FunctionDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001873 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001874 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001875 return;
1876 }
Richard Smith3a2b7a12013-01-28 22:42:45 +00001877
1878 D->addAttr(::new (S.Context) CarriesDependencyAttr(
1879 Attr.getRange(), S.Context,
1880 Attr.getAttributeSpellingListIndex()));
Sean Huntbbd37c62009-11-21 08:43:09 +00001881}
1882
Chandler Carruth1b03c872011-07-02 00:01:44 +00001883static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001884 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
Daniel Jasper568eae42012-06-13 18:31:09 +00001885 !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001886 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001887 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001888 return;
1889 }
Mike Stumpbf916502009-07-24 19:02:52 +00001890
Michael Han51d8c522013-01-24 16:46:58 +00001891 D->addAttr(::new (S.Context)
1892 UnusedAttr(Attr.getRange(), S.Context,
1893 Attr.getAttributeSpellingListIndex()));
Ted Kremenek73798892008-07-25 04:39:19 +00001894}
1895
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001896static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1897 const AttributeList &Attr) {
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001898 if (!isa<FunctionDecl>(D)) {
1899 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1900 << Attr.getName() << ExpectedFunction;
1901 return;
1902 }
1903
Michael Han51d8c522013-01-24 16:46:58 +00001904 D->addAttr(::new (S.Context)
1905 ReturnsTwiceAttr(Attr.getRange(), S.Context,
1906 Attr.getAttributeSpellingListIndex()));
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001907}
1908
Chandler Carruth1b03c872011-07-02 00:01:44 +00001909static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001910 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Rafael Espindola29535ba2013-08-16 23:18:50 +00001911 if (VD->hasLocalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001912 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1913 return;
1914 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001915 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001916 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001917 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001918 return;
1919 }
Mike Stumpbf916502009-07-24 19:02:52 +00001920
Michael Han51d8c522013-01-24 16:46:58 +00001921 D->addAttr(::new (S.Context)
1922 UsedAttr(Attr.getRange(), S.Context,
1923 Attr.getAttributeSpellingListIndex()));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001924}
1925
Chandler Carruth1b03c872011-07-02 00:01:44 +00001926static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001927 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001928 if (Attr.getNumArgs() > 1) {
1929 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001930 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001931 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001932
1933 int priority = 65535; // FIXME: Do not hardcode such constants.
1934 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001935 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001936 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001937 if (E->isTypeDependent() || E->isValueDependent() ||
1938 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001939 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001940 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00001941 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001942 return;
1943 }
1944 priority = Idx.getZExtValue();
1945 }
Mike Stumpbf916502009-07-24 19:02:52 +00001946
Chandler Carruth87c44602011-07-01 23:49:12 +00001947 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001948 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001949 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001950 return;
1951 }
1952
Michael Han51d8c522013-01-24 16:46:58 +00001953 D->addAttr(::new (S.Context)
1954 ConstructorAttr(Attr.getRange(), S.Context, priority,
1955 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001956}
1957
Chandler Carruth1b03c872011-07-02 00:01:44 +00001958static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001959 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001960 if (Attr.getNumArgs() > 1) {
1961 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001962 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001963 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001964
1965 int priority = 65535; // FIXME: Do not hardcode such constants.
1966 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00001967 Expr *E = Attr.getArgAsExpr(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001968 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001969 if (E->isTypeDependent() || E->isValueDependent() ||
1970 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00001971 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00001972 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00001973 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001974 return;
1975 }
1976 priority = Idx.getZExtValue();
1977 }
Mike Stumpbf916502009-07-24 19:02:52 +00001978
Chandler Carruth87c44602011-07-01 23:49:12 +00001979 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001980 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001981 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001982 return;
1983 }
1984
Michael Han51d8c522013-01-24 16:46:58 +00001985 D->addAttr(::new (S.Context)
1986 DestructorAttr(Attr.getRange(), S.Context, priority,
1987 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001988}
1989
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001990template <typename AttrTy>
Aaron Ballman2dbdef22013-07-18 13:13:52 +00001991static void handleAttrWithMessage(Sema &S, Decl *D,
1992 const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001993 unsigned NumArgs = Attr.getNumArgs();
1994 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001995 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001996 return;
1997 }
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00001998
1999 // Handle the case where the attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002000 StringRef Str;
Aaron Ballman624421f2013-08-31 01:11:41 +00002001 if (Attr.isArgExpr(0)) {
2002 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArgAsExpr(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002003 if (!SE) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002004 S.Diag(Attr.getArgAsExpr(0)->getLocStart(),
2005 diag::err_attribute_argument_type) << Attr.getName()
2006 << AANT_ArgumentString;
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002007 return;
2008 }
Chris Lattner951bbb22011-02-24 05:42:24 +00002009 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002010 }
Mike Stumpbf916502009-07-24 19:02:52 +00002011
Michael Han51d8c522013-01-24 16:46:58 +00002012 D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str,
2013 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00002014}
2015
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002016static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
2017 const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00002018 D->addAttr(::new (S.Context)
2019 ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context,
2020 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00002021}
2022
Patrick Beardb2f68202012-04-06 18:12:22 +00002023static void handleObjCRootClassAttr(Sema &S, Decl *D,
2024 const AttributeList &Attr) {
2025 if (!isa<ObjCInterfaceDecl>(D)) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002026 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2027 << Attr.getName() << ExpectedObjectiveCInterface;
Patrick Beardb2f68202012-04-06 18:12:22 +00002028 return;
2029 }
2030
Michael Han51d8c522013-01-24 16:46:58 +00002031 D->addAttr(::new (S.Context)
2032 ObjCRootClassAttr(Attr.getRange(), S.Context,
2033 Attr.getAttributeSpellingListIndex()));
Patrick Beardb2f68202012-04-06 18:12:22 +00002034}
2035
Michael Han51d8c522013-01-24 16:46:58 +00002036static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
2037 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00002038 if (!isa<ObjCInterfaceDecl>(D)) {
2039 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
2040 return;
2041 }
2042
Michael Han51d8c522013-01-24 16:46:58 +00002043 D->addAttr(::new (S.Context)
2044 ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context,
2045 Attr.getAttributeSpellingListIndex()));
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00002046}
2047
Jordy Rosefad5de92012-05-08 03:27:22 +00002048static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2049 IdentifierInfo *Platform,
2050 VersionTuple Introduced,
2051 VersionTuple Deprecated,
2052 VersionTuple Obsoleted) {
Rafael Espindola3b294362012-05-06 19:56:25 +00002053 StringRef PlatformName
2054 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2055 if (PlatformName.empty())
2056 PlatformName = Platform->getName();
2057
2058 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2059 // of these steps are needed).
2060 if (!Introduced.empty() && !Deprecated.empty() &&
2061 !(Introduced <= Deprecated)) {
2062 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2063 << 1 << PlatformName << Deprecated.getAsString()
2064 << 0 << Introduced.getAsString();
2065 return true;
2066 }
2067
2068 if (!Introduced.empty() && !Obsoleted.empty() &&
2069 !(Introduced <= Obsoleted)) {
2070 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2071 << 2 << PlatformName << Obsoleted.getAsString()
2072 << 0 << Introduced.getAsString();
2073 return true;
2074 }
2075
2076 if (!Deprecated.empty() && !Obsoleted.empty() &&
2077 !(Deprecated <= Obsoleted)) {
2078 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2079 << 2 << PlatformName << Obsoleted.getAsString()
2080 << 1 << Deprecated.getAsString();
2081 return true;
2082 }
2083
2084 return false;
2085}
2086
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002087/// \brief Check whether the two versions match.
2088///
2089/// If either version tuple is empty, then they are assumed to match. If
2090/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2091static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2092 bool BeforeIsOkay) {
2093 if (X.empty() || Y.empty())
2094 return true;
2095
2096 if (X == Y)
2097 return true;
2098
2099 if (BeforeIsOkay && X < Y)
2100 return true;
2101
2102 return false;
2103}
2104
Rafael Espindola51be6e32013-01-08 22:04:34 +00002105AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002106 IdentifierInfo *Platform,
2107 VersionTuple Introduced,
2108 VersionTuple Deprecated,
2109 VersionTuple Obsoleted,
2110 bool IsUnavailable,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002111 StringRef Message,
Michael Han51d8c522013-01-24 16:46:58 +00002112 bool Override,
2113 unsigned AttrSpellingListIndex) {
Rafael Espindola98ae8342012-05-10 02:50:16 +00002114 VersionTuple MergedIntroduced = Introduced;
2115 VersionTuple MergedDeprecated = Deprecated;
2116 VersionTuple MergedObsoleted = Obsoleted;
Rafael Espindola3b294362012-05-06 19:56:25 +00002117 bool FoundAny = false;
2118
Rafael Espindola98ae8342012-05-10 02:50:16 +00002119 if (D->hasAttrs()) {
2120 AttrVec &Attrs = D->getAttrs();
2121 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2122 const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2123 if (!OldAA) {
2124 ++i;
2125 continue;
2126 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002127
Rafael Espindola98ae8342012-05-10 02:50:16 +00002128 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2129 if (OldPlatform != Platform) {
2130 ++i;
2131 continue;
2132 }
2133
2134 FoundAny = true;
2135 VersionTuple OldIntroduced = OldAA->getIntroduced();
2136 VersionTuple OldDeprecated = OldAA->getDeprecated();
2137 VersionTuple OldObsoleted = OldAA->getObsoleted();
2138 bool OldIsUnavailable = OldAA->getUnavailable();
Rafael Espindola98ae8342012-05-10 02:50:16 +00002139
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002140 if (!versionsMatch(OldIntroduced, Introduced, Override) ||
2141 !versionsMatch(Deprecated, OldDeprecated, Override) ||
2142 !versionsMatch(Obsoleted, OldObsoleted, Override) ||
2143 !(OldIsUnavailable == IsUnavailable ||
Douglas Gregor72daa3f2013-01-16 00:54:48 +00002144 (Override && !OldIsUnavailable && IsUnavailable))) {
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002145 if (Override) {
2146 int Which = -1;
2147 VersionTuple FirstVersion;
2148 VersionTuple SecondVersion;
2149 if (!versionsMatch(OldIntroduced, Introduced, Override)) {
2150 Which = 0;
2151 FirstVersion = OldIntroduced;
2152 SecondVersion = Introduced;
2153 } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) {
2154 Which = 1;
2155 FirstVersion = Deprecated;
2156 SecondVersion = OldDeprecated;
2157 } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) {
2158 Which = 2;
2159 FirstVersion = Obsoleted;
2160 SecondVersion = OldObsoleted;
2161 }
2162
2163 if (Which == -1) {
2164 Diag(OldAA->getLocation(),
2165 diag::warn_mismatched_availability_override_unavail)
2166 << AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2167 } else {
2168 Diag(OldAA->getLocation(),
2169 diag::warn_mismatched_availability_override)
2170 << Which
2171 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2172 << FirstVersion.getAsString() << SecondVersion.getAsString();
2173 }
2174 Diag(Range.getBegin(), diag::note_overridden_method);
2175 } else {
2176 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2177 Diag(Range.getBegin(), diag::note_previous_attribute);
2178 }
2179
Rafael Espindola98ae8342012-05-10 02:50:16 +00002180 Attrs.erase(Attrs.begin() + i);
2181 --e;
2182 continue;
2183 }
2184
2185 VersionTuple MergedIntroduced2 = MergedIntroduced;
2186 VersionTuple MergedDeprecated2 = MergedDeprecated;
2187 VersionTuple MergedObsoleted2 = MergedObsoleted;
2188
2189 if (MergedIntroduced2.empty())
2190 MergedIntroduced2 = OldIntroduced;
2191 if (MergedDeprecated2.empty())
2192 MergedDeprecated2 = OldDeprecated;
2193 if (MergedObsoleted2.empty())
2194 MergedObsoleted2 = OldObsoleted;
2195
2196 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2197 MergedIntroduced2, MergedDeprecated2,
2198 MergedObsoleted2)) {
2199 Attrs.erase(Attrs.begin() + i);
2200 --e;
2201 continue;
2202 }
2203
2204 MergedIntroduced = MergedIntroduced2;
2205 MergedDeprecated = MergedDeprecated2;
2206 MergedObsoleted = MergedObsoleted2;
2207 ++i;
Rafael Espindola3b294362012-05-06 19:56:25 +00002208 }
Rafael Espindola3b294362012-05-06 19:56:25 +00002209 }
2210
2211 if (FoundAny &&
2212 MergedIntroduced == Introduced &&
2213 MergedDeprecated == Deprecated &&
2214 MergedObsoleted == Obsoleted)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002215 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002216
Ted Kremenekcb344392013-04-06 00:34:27 +00002217 // Only create a new attribute if !Override, but we want to do
2218 // the checking.
Rafael Espindola98ae8342012-05-10 02:50:16 +00002219 if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced,
Ted Kremenekcb344392013-04-06 00:34:27 +00002220 MergedDeprecated, MergedObsoleted) &&
2221 !Override) {
Rafael Espindola599f1b72012-05-13 03:25:18 +00002222 return ::new (Context) AvailabilityAttr(Range, Context, Platform,
2223 Introduced, Deprecated,
Michael Han51d8c522013-01-24 16:46:58 +00002224 Obsoleted, IsUnavailable, Message,
2225 AttrSpellingListIndex);
Rafael Espindola3b294362012-05-06 19:56:25 +00002226 }
Rafael Espindola599f1b72012-05-13 03:25:18 +00002227 return NULL;
Rafael Espindola3b294362012-05-06 19:56:25 +00002228}
2229
Chandler Carruth1b03c872011-07-02 00:01:44 +00002230static void handleAvailabilityAttr(Sema &S, Decl *D,
2231 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002232 if (!checkAttributeNumArgs(S, Attr, 1))
2233 return;
2234 IdentifierLoc *Platform = Attr.getArgAsIdent(0);
Michael Han51d8c522013-01-24 16:46:58 +00002235 unsigned Index = Attr.getAttributeSpellingListIndex();
2236
Aaron Ballman624421f2013-08-31 01:11:41 +00002237 IdentifierInfo *II = Platform->Ident;
2238 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2239 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2240 << Platform->Ident;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002241
Rafael Espindola8c4222a2013-01-08 21:30:32 +00002242 NamedDecl *ND = dyn_cast<NamedDecl>(D);
2243 if (!ND) {
2244 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2245 return;
2246 }
2247
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002248 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
2249 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
2250 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00002251 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00002252 StringRef Str;
2253 const StringLiteral *SE =
2254 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
2255 if (SE)
2256 Str = SE->getString();
Rafael Espindola3b294362012-05-06 19:56:25 +00002257
Aaron Ballman624421f2013-08-31 01:11:41 +00002258 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II,
Rafael Espindola599f1b72012-05-13 03:25:18 +00002259 Introduced.Version,
2260 Deprecated.Version,
2261 Obsoleted.Version,
Douglas Gregorf4d918f2013-01-15 22:43:08 +00002262 IsUnavailable, Str,
Michael Han51d8c522013-01-24 16:46:58 +00002263 /*Override=*/false,
2264 Index);
Rafael Espindola838dc592013-01-12 06:42:30 +00002265 if (NewAttr)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002266 D->addAttr(NewAttr);
Rafael Espindola98ae8342012-05-10 02:50:16 +00002267}
2268
John McCalld4c3d662013-02-20 01:54:26 +00002269template <class T>
2270static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range,
2271 typename T::VisibilityType value,
2272 unsigned attrSpellingListIndex) {
2273 T *existingAttr = D->getAttr<T>();
2274 if (existingAttr) {
2275 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2276 if (existingValue == value)
2277 return NULL;
2278 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2279 S.Diag(range.getBegin(), diag::note_previous_attribute);
2280 D->dropAttr<T>();
2281 }
2282 return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex);
2283}
2284
Rafael Espindola599f1b72012-05-13 03:25:18 +00002285VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002286 VisibilityAttr::VisibilityType Vis,
2287 unsigned AttrSpellingListIndex) {
John McCalld4c3d662013-02-20 01:54:26 +00002288 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis,
2289 AttrSpellingListIndex);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002290}
2291
John McCalld4c3d662013-02-20 01:54:26 +00002292TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range,
2293 TypeVisibilityAttr::VisibilityType Vis,
2294 unsigned AttrSpellingListIndex) {
2295 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis,
2296 AttrSpellingListIndex);
2297}
2298
2299static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr,
2300 bool isTypeVisibility) {
2301 // Visibility attributes don't mean anything on a typedef.
2302 if (isa<TypedefNameDecl>(D)) {
2303 S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
2304 << Attr.getName();
2305 return;
2306 }
2307
2308 // 'type_visibility' can only go on a type or namespace.
2309 if (isTypeVisibility &&
2310 !(isa<TagDecl>(D) ||
2311 isa<ObjCInterfaceDecl>(D) ||
2312 isa<NamespaceDecl>(D))) {
2313 S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2314 << Attr.getName() << ExpectedTypeOrNamespace;
2315 return;
2316 }
2317
Benjamin Kramerae3a83f2013-09-09 15:08:57 +00002318 // Check that the argument is a string literal.
2319 StringLiteral *Str = 0;
2320 if (Attr.isArgExpr(0))
2321 Str = dyn_cast<StringLiteral>(Attr.getArgAsExpr(0)->IgnoreParenCasts());
Mike Stumpbf916502009-07-24 19:02:52 +00002322
Douglas Gregor5cee1192011-07-27 05:40:30 +00002323 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002324 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2325 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002326 return;
2327 }
Mike Stumpbf916502009-07-24 19:02:52 +00002328
Chris Lattner5f9e2722011-07-23 10:55:15 +00002329 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00002330 VisibilityAttr::VisibilityType type;
Aaron Ballmand0686072013-09-11 19:47:58 +00002331 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
2332 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2333 << Attr.getName() << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002334 return;
2335 }
Aaron Ballmand0686072013-09-11 19:47:58 +00002336
2337 // Complain about attempts to use protected visibility on targets
2338 // (like Darwin) that don't support it.
2339 if (type == VisibilityAttr::Protected &&
2340 !S.Context.getTargetInfo().hasProtectedVisibility()) {
2341 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
2342 type = VisibilityAttr::Default;
2343 }
Mike Stumpbf916502009-07-24 19:02:52 +00002344
Michael Han51d8c522013-01-24 16:46:58 +00002345 unsigned Index = Attr.getAttributeSpellingListIndex();
John McCalld4c3d662013-02-20 01:54:26 +00002346 clang::Attr *newAttr;
2347 if (isTypeVisibility) {
2348 newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(),
2349 (TypeVisibilityAttr::VisibilityType) type,
2350 Index);
2351 } else {
2352 newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index);
2353 }
2354 if (newAttr)
2355 D->addAttr(newAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002356}
2357
Chandler Carruth1b03c872011-07-02 00:01:44 +00002358static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
2359 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00002360 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
2361 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002362 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002363 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00002364 return;
2365 }
2366
Aaron Ballman624421f2013-08-31 01:11:41 +00002367 if (!Attr.isArgIdent(0)) {
2368 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
2369 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
John McCalld5313b02011-03-02 11:33:24 +00002370 return;
2371 }
Aaron Ballman624421f2013-08-31 01:11:41 +00002372
Aaron Ballmand0686072013-09-11 19:47:58 +00002373 IdentifierLoc *IL = Attr.getArgAsIdent(0);
2374 ObjCMethodFamilyAttr::FamilyKind F;
2375 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2376 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName()
2377 << IL->Ident;
John McCalld5313b02011-03-02 11:33:24 +00002378 return;
2379 }
2380
Aaron Ballmand0686072013-09-11 19:47:58 +00002381 if (F == ObjCMethodFamilyAttr::OMF_init &&
John McCallf85e1932011-06-15 23:02:42 +00002382 !method->getResultType()->isObjCObjectPointerType()) {
2383 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
2384 << method->getResultType();
2385 // Ignore the attribute.
2386 return;
2387 }
2388
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002389 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
Aaron Ballmand0686072013-09-11 19:47:58 +00002390 S.Context, F));
John McCalld5313b02011-03-02 11:33:24 +00002391}
2392
Chandler Carruth1b03c872011-07-02 00:01:44 +00002393static void handleObjCExceptionAttr(Sema &S, Decl *D,
2394 const AttributeList &Attr) {
Chris Lattner0db29ec2009-02-14 08:09:34 +00002395 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
2396 if (OCI == 0) {
Aaron Ballman37a89532013-07-18 14:56:42 +00002397 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
2398 << Attr.getName() << ExpectedObjectiveCInterface;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002399 return;
2400 }
Mike Stumpbf916502009-07-24 19:02:52 +00002401
Michael Han51d8c522013-01-24 16:46:58 +00002402 D->addAttr(::new (S.Context)
2403 ObjCExceptionAttr(Attr.getRange(), S.Context,
2404 Attr.getAttributeSpellingListIndex()));
Chris Lattner0db29ec2009-02-14 08:09:34 +00002405}
2406
Chandler Carruth1b03c872011-07-02 00:01:44 +00002407static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Richard Smith162e1c12011-04-15 14:24:37 +00002408 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002409 QualType T = TD->getUnderlyingType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002410 if (!T->isCARCBridgableType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002411 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2412 return;
2413 }
2414 }
Fariborz Jahanian34276822012-05-31 23:18:32 +00002415 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2416 QualType T = PD->getType();
Ted Kremenek9af91222012-08-29 22:54:47 +00002417 if (!T->isCARCBridgableType()) {
Fariborz Jahanian34276822012-05-31 23:18:32 +00002418 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2419 return;
2420 }
2421 }
2422 else {
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002423 // It is okay to include this attribute on properties, e.g.:
2424 //
2425 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2426 //
2427 // In this case it follows tradition and suppresses an error in the above
2428 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00002429 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00002430 }
Michael Han51d8c522013-01-24 16:46:58 +00002431 D->addAttr(::new (S.Context)
2432 ObjCNSObjectAttr(Attr.getRange(), S.Context,
2433 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002434}
2435
Mike Stumpbf916502009-07-24 19:02:52 +00002436static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00002437handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00002438 if (!isa<FunctionDecl>(D)) {
2439 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
2440 return;
2441 }
2442
Michael Han51d8c522013-01-24 16:46:58 +00002443 D->addAttr(::new (S.Context)
2444 OverloadableAttr(Attr.getRange(), S.Context,
2445 Attr.getAttributeSpellingListIndex()));
Douglas Gregorf9201e02009-02-11 23:02:49 +00002446}
2447
Chandler Carruth1b03c872011-07-02 00:01:44 +00002448static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002449 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002450 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00002451 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Steve Naroff9eae5762008-09-18 16:44:58 +00002452 return;
2453 }
Mike Stumpbf916502009-07-24 19:02:52 +00002454
Aaron Ballman624421f2013-08-31 01:11:41 +00002455 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
Sean Huntcf807c42010-08-18 23:23:40 +00002456 BlocksAttr::BlockType type;
Aaron Ballmand0686072013-09-11 19:47:58 +00002457 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2458 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2459 << Attr.getName() << II;
Steve Naroff9eae5762008-09-18 16:44:58 +00002460 return;
2461 }
Mike Stumpbf916502009-07-24 19:02:52 +00002462
Michael Han51d8c522013-01-24 16:46:58 +00002463 D->addAttr(::new (S.Context)
2464 BlocksAttr(Attr.getRange(), S.Context, type,
2465 Attr.getAttributeSpellingListIndex()));
Steve Naroff9eae5762008-09-18 16:44:58 +00002466}
2467
Chandler Carruth1b03c872011-07-02 00:01:44 +00002468static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00002469 // check the attribute arguments.
2470 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002471 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00002472 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002473 }
2474
John McCall3323fad2011-09-09 07:56:05 +00002475 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002476 if (Attr.getNumArgs() > 0) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002477 Expr *E = Attr.getArgAsExpr(0);
Anders Carlsson77091822008-10-05 18:05:59 +00002478 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002479 if (E->isTypeDependent() || E->isValueDependent() ||
2480 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002481 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002482 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002483 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002484 return;
2485 }
Mike Stumpbf916502009-07-24 19:02:52 +00002486
John McCall3323fad2011-09-09 07:56:05 +00002487 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002488 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2489 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002490 return;
2491 }
John McCall3323fad2011-09-09 07:56:05 +00002492
2493 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00002494 }
2495
John McCall3323fad2011-09-09 07:56:05 +00002496 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002497 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002498 Expr *E = Attr.getArgAsExpr(1);
Anders Carlsson77091822008-10-05 18:05:59 +00002499 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002500 if (E->isTypeDependent() || E->isValueDependent() ||
2501 !E->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00002502 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002503 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00002504 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002505 return;
2506 }
2507 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00002508
John McCall3323fad2011-09-09 07:56:05 +00002509 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00002510 // FIXME: This error message could be improved, it would be nice
2511 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002512 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2513 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00002514 return;
2515 }
2516 }
2517
Chandler Carruth87c44602011-07-01 23:49:12 +00002518 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00002519 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00002520 if (isa<FunctionNoProtoType>(FT)) {
2521 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2522 return;
2523 }
Mike Stumpbf916502009-07-24 19:02:52 +00002524
Chris Lattner897cd902009-03-17 23:03:47 +00002525 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002526 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002527 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002528 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002529 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00002530 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002531 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00002532 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002533 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00002534 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
2535 if (!BD->isVariadic()) {
2536 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2537 return;
2538 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002539 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002540 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00002541 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002542 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00002543 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002544 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00002545 int m = Ty->isFunctionPointerType() ? 0 : 1;
2546 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002547 return;
2548 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002549 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002550 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002551 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00002552 return;
2553 }
Anders Carlsson77091822008-10-05 18:05:59 +00002554 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002555 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002556 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00002557 return;
2558 }
Michael Han51d8c522013-01-24 16:46:58 +00002559 D->addAttr(::new (S.Context)
2560 SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos,
2561 Attr.getAttributeSpellingListIndex()));
Anders Carlsson77091822008-10-05 18:05:59 +00002562}
2563
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002564static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Lubos Lunak1d3ce652013-07-20 15:05:36 +00002565 if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
2566 RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context));
2567 else
2568 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2569}
2570
Chandler Carruth1b03c872011-07-02 00:01:44 +00002571static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Kaelyn Uhrain51ceb7b2012-11-12 23:48:05 +00002572 if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00002573 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Kaelyn Uhraind449c792012-11-13 00:18:47 +00002574 << Attr.getName() << ExpectedFunctionMethodOrClass;
Chris Lattner026dc962009-02-14 07:37:35 +00002575 return;
2576 }
Mike Stumpbf916502009-07-24 19:02:52 +00002577
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002578 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
2579 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2580 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00002581 return;
2582 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002583 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
2584 if (MD->getResultType()->isVoidType()) {
2585 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
2586 << Attr.getName() << 1;
2587 return;
2588 }
2589
Michael Han51d8c522013-01-24 16:46:58 +00002590 D->addAttr(::new (S.Context)
2591 WarnUnusedResultAttr(Attr.getRange(), S.Context,
2592 Attr.getAttributeSpellingListIndex()));
Chris Lattner026dc962009-02-14 07:37:35 +00002593}
2594
Chandler Carruth1b03c872011-07-02 00:01:44 +00002595static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002596 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00002597 if (isa<CXXRecordDecl>(D)) {
2598 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2599 return;
2600 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002601 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2602 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002603 return;
2604 }
2605
Chandler Carruth87c44602011-07-01 23:49:12 +00002606 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002607
Michael Han51d8c522013-01-24 16:46:58 +00002608 nd->addAttr(::new (S.Context)
2609 WeakAttr(Attr.getRange(), S.Context,
2610 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002611}
2612
Chandler Carruth1b03c872011-07-02 00:01:44 +00002613static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002614 // weak_import only applies to variable & function declarations.
2615 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002616 if (!D->canBeWeakImported(isDef)) {
2617 if (isDef)
Reid Klecknerbeba3e82013-05-20 21:53:29 +00002618 S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition)
2619 << "weak_import";
Douglas Gregordef86312011-03-23 13:27:51 +00002620 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002621 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002622 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002623 // Nothing to warn about here.
2624 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002625 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002626 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002627
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002628 return;
2629 }
2630
Michael Han51d8c522013-01-24 16:46:58 +00002631 D->addAttr(::new (S.Context)
2632 WeakImportAttr(Attr.getRange(), S.Context,
2633 Attr.getAttributeSpellingListIndex()));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002634}
2635
Tanya Lattner0df579e2012-07-09 22:06:01 +00002636// Handles reqd_work_group_size and work_group_size_hint.
2637static void handleWorkGroupSize(Sema &S, Decl *D,
Nick Lewycky4ae89bc2012-07-24 01:31:55 +00002638 const AttributeList &Attr) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002639 unsigned WGSize[3];
2640 for (unsigned i = 0; i < 3; ++i) {
Aaron Ballman624421f2013-08-31 01:11:41 +00002641 Expr *E = Attr.getArgAsExpr(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002642 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002643 if (E->isTypeDependent() || E->isValueDependent() ||
2644 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00002645 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2646 << Attr.getName() << AANT_ArgumentIntegerConstant
2647 << E->getSourceRange();
Nate Begeman6f3d8382009-06-26 06:32:41 +00002648 return;
2649 }
2650 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2651 }
Tanya Lattner0df579e2012-07-09 22:06:01 +00002652
2653 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize
2654 && D->hasAttr<ReqdWorkGroupSizeAttr>()) {
2655 ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>();
2656 if (!(A->getXDim() == WGSize[0] &&
2657 A->getYDim() == WGSize[1] &&
2658 A->getZDim() == WGSize[2])) {
2659 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2660 Attr.getName();
2661 }
2662 }
2663
2664 if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint
2665 && D->hasAttr<WorkGroupSizeHintAttr>()) {
2666 WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>();
2667 if (!(A->getXDim() == WGSize[0] &&
2668 A->getYDim() == WGSize[1] &&
2669 A->getZDim() == WGSize[2])) {
2670 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) <<
2671 Attr.getName();
2672 }
2673 }
2674
2675 if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize)
2676 D->addAttr(::new (S.Context)
2677 ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002678 WGSize[0], WGSize[1], WGSize[2],
2679 Attr.getAttributeSpellingListIndex()));
Tanya Lattner0df579e2012-07-09 22:06:01 +00002680 else
2681 D->addAttr(::new (S.Context)
2682 WorkGroupSizeHintAttr(Attr.getRange(), S.Context,
Michael Han51d8c522013-01-24 16:46:58 +00002683 WGSize[0], WGSize[1], WGSize[2],
2684 Attr.getAttributeSpellingListIndex()));
Nate Begeman6f3d8382009-06-26 06:32:41 +00002685}
2686
Joey Gouly37453b92013-03-08 09:42:32 +00002687static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) {
2688 assert(Attr.getKind() == AttributeList::AT_VecTypeHint);
2689
Aaron Ballman624421f2013-08-31 01:11:41 +00002690 if (!checkAttributeNumArgs(S, Attr, 0))
Joey Gouly37453b92013-03-08 09:42:32 +00002691 return;
2692
Aaron Ballman624421f2013-08-31 01:11:41 +00002693 if (!Attr.hasParsedType()) {
2694 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
2695 << Attr.getName() << 1;
2696 return;
2697 }
2698
Joey Gouly37453b92013-03-08 09:42:32 +00002699 QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg());
2700
2701 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
2702 (ParmType->isBooleanType() ||
2703 !ParmType->isIntegralType(S.getASTContext()))) {
2704 S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint)
2705 << ParmType;
2706 return;
2707 }
2708
2709 if (Attr.getKind() == AttributeList::AT_VecTypeHint &&
2710 D->hasAttr<VecTypeHintAttr>()) {
2711 VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>();
2712 if (A->getTypeHint() != ParmType) {
2713 S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName();
2714 return;
2715 }
2716 }
2717
2718 D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context,
2719 ParmType, Attr.getLoc()));
2720}
2721
Rafael Espindola599f1b72012-05-13 03:25:18 +00002722SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range,
Michael Han51d8c522013-01-24 16:46:58 +00002723 StringRef Name,
2724 unsigned AttrSpellingListIndex) {
Rafael Espindola420efd82012-05-13 02:42:42 +00002725 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2726 if (ExistingAttr->getName() == Name)
Rafael Espindola599f1b72012-05-13 03:25:18 +00002727 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002728 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2729 Diag(Range.getBegin(), diag::note_previous_attribute);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002730 return NULL;
Rafael Espindola420efd82012-05-13 02:42:42 +00002731 }
Michael Han51d8c522013-01-24 16:46:58 +00002732 return ::new (Context) SectionAttr(Range, Context, Name,
2733 AttrSpellingListIndex);
Rafael Espindola420efd82012-05-13 02:42:42 +00002734}
2735
Chandler Carruth1b03c872011-07-02 00:01:44 +00002736static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002737 // Make sure that there is a string literal as the sections's single
2738 // argument.
Aaron Ballman624421f2013-08-31 01:11:41 +00002739 Expr *ArgExpr = Attr.getArgAsExpr(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002740 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002741 if (!SE) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00002742 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
2743 << Attr.getName() << AANT_ArgumentString;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002744 return;
2745 }
Mike Stump1eb44332009-09-09 15:08:12 +00002746
Chris Lattner797c3c42009-08-10 19:03:04 +00002747 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002748 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002749 if (!Error.empty()) {
2750 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2751 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002752 return;
2753 }
Mike Stump1eb44332009-09-09 15:08:12 +00002754
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002755 // This attribute cannot be applied to local variables.
2756 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2757 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2758 return;
2759 }
Michael Han51d8c522013-01-24 16:46:58 +00002760
2761 unsigned Index = Attr.getAttributeSpellingListIndex();
Rafael Espindola599f1b72012-05-13 03:25:18 +00002762 SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(),
Michael Han51d8c522013-01-24 16:46:58 +00002763 SE->getString(), Index);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002764 if (NewAttr)
2765 D->addAttr(NewAttr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002766}
2767
Chris Lattner6b6b5372008-06-26 18:38:35 +00002768
Chandler Carruth1b03c872011-07-02 00:01:44 +00002769static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002770 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002771 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002772 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002773 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002774 D->addAttr(::new (S.Context)
2775 NoThrowAttr(Attr.getRange(), S.Context,
2776 Attr.getAttributeSpellingListIndex()));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002777 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002778}
2779
Chandler Carruth1b03c872011-07-02 00:01:44 +00002780static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002781 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002782 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002783 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002784 } else {
Michael Han51d8c522013-01-24 16:46:58 +00002785 D->addAttr(::new (S.Context)
2786 ConstAttr(Attr.getRange(), S.Context,
2787 Attr.getAttributeSpellingListIndex() ));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002788 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002789}
2790
Chandler Carruth1b03c872011-07-02 00:01:44 +00002791static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Michael Han51d8c522013-01-24 16:46:58 +00002792 D->addAttr(::new (S.Context)
2793 PureAttr(Attr.getRange(), S.Context,
2794 Attr.getAttributeSpellingListIndex()));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002795}
2796
Chandler Carruth1b03c872011-07-02 00:01:44 +00002797static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002798 VarDecl *VD = dyn_cast<VarDecl>(D);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002799 if (!VD || !VD->hasLocalStorage()) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002800 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002801 return;
2802 }
Mike Stumpbf916502009-07-24 19:02:52 +00002803
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002804 Expr *E = Attr.getArgAsExpr(0);
2805 SourceLocation Loc = E->getExprLoc();
2806 FunctionDecl *FD = 0;
2807 DeclarationNameInfo NI;
Aaron Ballman624421f2013-08-31 01:11:41 +00002808
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002809 // gcc only allows for simple identifiers. Since we support more than gcc, we
2810 // will warn the user.
2811 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2812 if (DRE->hasQualifier())
2813 S.Diag(Loc, diag::warn_cleanup_ext);
2814 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2815 NI = DRE->getNameInfo();
2816 if (!FD) {
2817 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
2818 << NI.getName();
2819 return;
2820 }
2821 } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2822 if (ULE->hasExplicitTemplateArgs())
2823 S.Diag(Loc, diag::warn_cleanup_ext);
Mike Stumpbf916502009-07-24 19:02:52 +00002824
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002825 // This will diagnose the case where the function cannot be found.
2826 FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
2827 NI = ULE->getNameInfo();
2828 } else {
2829 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002830 return;
2831 }
2832
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002833 if (FD->getNumParams() != 1) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002834 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
2835 << NI.getName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002836 return;
2837 }
Mike Stumpbf916502009-07-24 19:02:52 +00002838
Anders Carlsson89941c12009-02-07 23:16:50 +00002839 // We're currently more strict than GCC about what function types we accept.
2840 // If this ever proves to be a problem it should be easy to fix.
2841 QualType Ty = S.Context.getPointerType(VD->getType());
2842 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002843 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2844 ParamTy, Ty) != Sema::Compatible) {
Aaron Ballmanebaee6b2013-09-11 01:37:41 +00002845 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
2846 << NI.getName() << ParamTy << Ty;
Anders Carlsson89941c12009-02-07 23:16:50 +00002847 return;
2848 }
Mike Stumpbf916502009-07-24 19:02:52 +00002849
Michael Han51d8c522013-01-24 16:46:58 +00002850 D->addAttr(::new (S.Context)
2851 CleanupAttr(Attr.getRange(), S.Context, FD,
2852 Attr.getAttributeSpellingListIndex()));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002853}
2854
Mike Stumpbf916502009-07-24 19:02:52 +00002855/// Handle __attribute__((format_arg((idx)))) attribute based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002856/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002857static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002858 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002859 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002860 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002861 return;
2862 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002863
Aaron Ballman624421f2013-08-31 01:11:41 +00002864 Expr *IdxExpr = Attr.getArgAsExpr(0);
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002865 uint64_t ArgIdx;
2866 if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(),
2867 Attr.getLoc(), 1, IdxExpr, ArgIdx))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002868 return;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002869
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002870 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002871 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002872
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002873 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2874 if (not_nsstring_type &&
2875 !isCFStringType(Ty, S.Context) &&
2876 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002877 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002878 // FIXME: Should highlight the actual expression that has the wrong type.
2879 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002880 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002881 << IdxExpr->getSourceRange();
2882 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002883 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002884 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002885 if (!isNSStringType(Ty, S.Context) &&
2886 !isCFStringType(Ty, S.Context) &&
2887 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002888 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002889 // FIXME: Should highlight the actual expression that has the wrong type.
2890 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002891 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002892 << IdxExpr->getSourceRange();
2893 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002894 }
2895
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002896 // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex
2897 // because that has corrected for the implicit this parameter, and is zero-
2898 // based. The attribute expects what the user wrote explicitly.
2899 llvm::APSInt Val;
2900 IdxExpr->EvaluateAsInt(Val, S.Context);
2901
Michael Han51d8c522013-01-24 16:46:58 +00002902 D->addAttr(::new (S.Context)
Aaron Ballmanb3d7efe2013-07-30 00:48:57 +00002903 FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00002904 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002905}
2906
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002907enum FormatAttrKind {
2908 CFStringFormat,
2909 NSStringFormat,
2910 StrftimeFormat,
2911 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002912 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002913 InvalidFormat
2914};
2915
2916/// getFormatAttrKind - Map from format attribute names to supported format
2917/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002918static FormatAttrKind getFormatAttrKind(StringRef Format) {
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002919 return llvm::StringSwitch<FormatAttrKind>(Format)
2920 // Check for formats that get handled specially.
2921 .Case("NSString", NSStringFormat)
2922 .Case("CFString", CFStringFormat)
2923 .Case("strftime", StrftimeFormat)
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002924
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002925 // Otherwise, check for supported formats.
2926 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
2927 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
2928 .Case("kprintf", SupportedFormat) // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002929
Benjamin Kramerc51bb992012-05-16 12:44:25 +00002930 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
2931 .Default(InvalidFormat);
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002932}
2933
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002934/// Handle __attribute__((init_priority(priority))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00002935/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002936static void handleInitPriorityAttr(Sema &S, Decl *D,
2937 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002938 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002939 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2940 return;
2941 }
2942
Chandler Carruth87c44602011-07-01 23:49:12 +00002943 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002944 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2945 Attr.setInvalid();
2946 return;
2947 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002948 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002949 if (S.Context.getAsArrayType(T))
2950 T = S.Context.getBaseElementType(T);
2951 if (!T->getAs<RecordType>()) {
2952 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2953 Attr.setInvalid();
2954 return;
2955 }
2956
Aaron Ballman624421f2013-08-31 01:11:41 +00002957 Expr *priorityExpr = Attr.getArgAsExpr(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002958
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002959 llvm::APSInt priority(32);
2960 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2961 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00002962 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
2963 << Attr.getName() << AANT_ArgumentIntegerConstant
2964 << priorityExpr->getSourceRange();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002965 Attr.setInvalid();
2966 return;
2967 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002968 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002969 if (prioritynum < 101 || prioritynum > 65535) {
2970 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2971 << priorityExpr->getSourceRange();
2972 Attr.setInvalid();
2973 return;
2974 }
Michael Han51d8c522013-01-24 16:46:58 +00002975 D->addAttr(::new (S.Context)
2976 InitPriorityAttr(Attr.getRange(), S.Context, prioritynum,
2977 Attr.getAttributeSpellingListIndex()));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002978}
2979
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00002980FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range,
2981 IdentifierInfo *Format, int FormatIdx,
2982 int FirstArg,
Michael Han51d8c522013-01-24 16:46:58 +00002983 unsigned AttrSpellingListIndex) {
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002984 // Check whether we already have an equivalent format attribute.
2985 for (specific_attr_iterator<FormatAttr>
2986 i = D->specific_attr_begin<FormatAttr>(),
2987 e = D->specific_attr_end<FormatAttr>();
2988 i != e ; ++i) {
2989 FormatAttr *f = *i;
2990 if (f->getType() == Format &&
2991 f->getFormatIdx() == FormatIdx &&
2992 f->getFirstArg() == FirstArg) {
2993 // If we don't have a valid location for this attribute, adopt the
2994 // location.
2995 if (f->getLocation().isInvalid())
2996 f->setRange(Range);
Rafael Espindola599f1b72012-05-13 03:25:18 +00002997 return NULL;
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00002998 }
2999 }
3000
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003001 return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx,
3002 FirstArg, AttrSpellingListIndex);
Rafael Espindolabf9da1f2012-05-11 00:36:07 +00003003}
3004
Mike Stumpbf916502009-07-24 19:02:52 +00003005/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
Bill Wendlingad017fa2012-12-20 19:22:21 +00003006/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00003007static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003008 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003009 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman624421f2013-08-31 01:11:41 +00003010 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003011 return;
3012 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003013
Chandler Carruth87c44602011-07-01 23:49:12 +00003014 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003015 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003016 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003017 return;
3018 }
3019
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003020 // In C++ the implicit 'this' function parameter also counts, and they are
3021 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00003022 bool HasImplicitThisParam = isInstanceMethod(D);
3023 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003024 unsigned FirstIdx = 1;
3025
Aaron Ballman624421f2013-08-31 01:11:41 +00003026 IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident;
3027 StringRef Format = II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003028
3029 // Normalize the argument, __foo__ becomes foo.
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003030 if (Format.startswith("__") && Format.endswith("__")) {
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003031 Format = Format.substr(2, Format.size() - 4);
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003032 // If we've modified the string name, we need a new identifier for it.
3033 II = &S.Context.Idents.get(Format);
3034 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003035
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003036 // Check for supported formats.
3037 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00003038
3039 if (Kind == IgnoredFormat)
3040 return;
3041
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003042 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003043 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Aaron Ballman624421f2013-08-31 01:11:41 +00003044 << "format" << II->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003045 return;
3046 }
3047
3048 // checks for the 2nd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003049 Expr *IdxExpr = Attr.getArgAsExpr(1);
Chris Lattner803d0802008-06-29 00:43:07 +00003050 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003051 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
3052 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003053 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003054 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003055 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003056 return;
3057 }
3058
3059 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003060 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003061 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003062 return;
3063 }
3064
3065 // FIXME: Do we need to bounds check?
3066 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00003067
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003068 if (HasImplicitThisParam) {
3069 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00003070 S.Diag(Attr.getLoc(),
3071 diag::err_format_attribute_implicit_this_format_string)
3072 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00003073 return;
3074 }
3075 ArgIdx--;
3076 }
Mike Stump1eb44332009-09-09 15:08:12 +00003077
Chris Lattner6b6b5372008-06-26 18:38:35 +00003078 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00003079 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003080
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003081 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003082 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003083 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3084 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00003085 return;
3086 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003087 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003088 // FIXME: do we need to check if the type is NSString*? What are the
3089 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00003090 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003091 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003092 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3093 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003094 return;
Mike Stumpbf916502009-07-24 19:02:52 +00003095 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003096 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00003097 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00003098 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003099 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
3100 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003101 return;
3102 }
3103
3104 // check the 3rd argument
Aaron Ballman624421f2013-08-31 01:11:41 +00003105 Expr *FirstArgExpr = Attr.getArgAsExpr(2);
Chris Lattner803d0802008-06-29 00:43:07 +00003106 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003107 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
3108 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003109 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003110 << Attr.getName() << 3 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003111 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003112 return;
3113 }
3114
3115 // check if the function is variadic if the 3rd argument non-zero
3116 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003117 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003118 ++NumArgs; // +1 for ...
3119 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003120 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003121 return;
3122 }
3123 }
3124
Chris Lattner3c73c412008-11-19 08:23:25 +00003125 // strftime requires FirstArg to be 0 because it doesn't read from any
3126 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00003127 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003128 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003129 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
3130 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003131 return;
3132 }
3133 // if 0 it disables parameter checking (to use with e.g. va_list)
3134 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003135 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00003136 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00003137 return;
3138 }
3139
Aaron Ballmancaa5ab22013-09-03 21:02:22 +00003140 FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II,
Rafael Espindola599f1b72012-05-13 03:25:18 +00003141 Idx.getZExtValue(),
Michael Han51d8c522013-01-24 16:46:58 +00003142 FirstArg.getZExtValue(),
3143 Attr.getAttributeSpellingListIndex());
Rafael Espindola599f1b72012-05-13 03:25:18 +00003144 if (NewAttr)
3145 D->addAttr(NewAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003146}
3147
Chandler Carruth1b03c872011-07-02 00:01:44 +00003148static void handleTransparentUnionAttr(Sema &S, Decl *D,
3149 const AttributeList &Attr) {
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003150 // Try to find the underlying union declaration.
3151 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00003152 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003153 if (TD && TD->getUnderlyingType()->isUnionType())
3154 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3155 else
Chandler Carruth87c44602011-07-01 23:49:12 +00003156 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003157
3158 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003159 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003160 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003161 return;
3162 }
3163
John McCall5e1cdac2011-10-07 06:10:15 +00003164 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003165 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003166 diag::warn_transparent_union_attribute_not_definition);
3167 return;
3168 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00003169
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003170 RecordDecl::field_iterator Field = RD->field_begin(),
3171 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003172 if (Field == FieldEnd) {
3173 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3174 return;
3175 }
Eli Friedmanbc887452008-09-02 05:19:23 +00003176
David Blaikie581deb32012-06-06 20:45:41 +00003177 FieldDecl *FirstField = *Field;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003178 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00003179 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00003180 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00003181 diag::warn_transparent_union_attribute_floating)
3182 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003183 return;
3184 }
3185
3186 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3187 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3188 for (; Field != FieldEnd; ++Field) {
3189 QualType FieldType = Field->getType();
3190 if (S.Context.getTypeSize(FieldType) != FirstSize ||
3191 S.Context.getTypeAlign(FieldType) != FirstAlign) {
3192 // Warn if we drop the attribute.
3193 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00003194 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003195 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00003196 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003197 diag::warn_transparent_union_attribute_field_size_align)
3198 << isSize << Field->getDeclName() << FieldBits;
3199 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00003200 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00003201 diag::note_transparent_union_first_field_size_align)
3202 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00003203 return;
3204 }
3205 }
3206
Michael Han51d8c522013-01-24 16:46:58 +00003207 RD->addAttr(::new (S.Context)
3208 TransparentUnionAttr(Attr.getRange(), S.Context,
3209 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003210}
3211
Chandler Carruth1b03c872011-07-02 00:01:44 +00003212static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00003213 Expr *ArgExpr = Attr.getArgAsExpr(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00003214 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00003215
Chris Lattner6b6b5372008-06-26 18:38:35 +00003216 // Make sure that there is a string literal as the annotation's single
3217 // argument.
3218 if (!SE) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003219 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type)
3220 << Attr.getName() << AANT_ArgumentString;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003221 return;
3222 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00003223
3224 // Don't duplicate annotations that are already set.
3225 for (specific_attr_iterator<AnnotateAttr>
3226 i = D->specific_attr_begin<AnnotateAttr>(),
3227 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
3228 if ((*i)->getAnnotation() == SE->getString())
3229 return;
3230 }
Michael Han51d8c522013-01-24 16:46:58 +00003231
3232 D->addAttr(::new (S.Context)
3233 AnnotateAttr(Attr.getRange(), S.Context, SE->getString(),
3234 Attr.getAttributeSpellingListIndex()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00003235}
3236
Chandler Carruth1b03c872011-07-02 00:01:44 +00003237static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00003238 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00003239 if (Attr.getNumArgs() > 1) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003240 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3241 << Attr.getName() << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00003242 return;
3243 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003244
Richard Smithbe507b62013-02-01 08:12:08 +00003245 if (Attr.getNumArgs() == 0) {
3246 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context,
3247 true, 0, Attr.getAttributeSpellingListIndex()));
3248 return;
3249 }
3250
Aaron Ballman624421f2013-08-31 01:11:41 +00003251 Expr *E = Attr.getArgAsExpr(0);
Richard Smithf6565a92013-02-22 08:32:16 +00003252 if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) {
3253 S.Diag(Attr.getEllipsisLoc(),
3254 diag::err_pack_expansion_without_parameter_packs);
3255 return;
3256 }
3257
3258 if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
3259 return;
3260
3261 S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(),
3262 Attr.isPackExpansion());
Richard Smithbe507b62013-02-01 08:12:08 +00003263}
3264
3265void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E,
Richard Smithf6565a92013-02-22 08:32:16 +00003266 unsigned SpellingListIndex, bool IsPackExpansion) {
Richard Smithbe507b62013-02-01 08:12:08 +00003267 AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex);
3268 SourceLocation AttrLoc = AttrRange.getBegin();
3269
Richard Smith4cd81c52013-01-29 09:02:09 +00003270 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
Richard Smithbe507b62013-02-01 08:12:08 +00003271 if (TmpAttr.isAlignas()) {
Richard Smith4cd81c52013-01-29 09:02:09 +00003272 // C++11 [dcl.align]p1:
3273 // An alignment-specifier may be applied to a variable or to a class
3274 // data member, but it shall not be applied to a bit-field, a function
3275 // parameter, the formal parameter of a catch clause, or a variable
3276 // declared with the register storage class specifier. An
3277 // alignment-specifier may also be applied to the declaration of a class
3278 // or enumeration type.
3279 // C11 6.7.5/2:
3280 // An alignment attribute shall not be specified in a declaration of
3281 // a typedef, or a bit-field, or a function, or a parameter, or an
3282 // object declared with the register storage-class specifier.
3283 int DiagKind = -1;
3284 if (isa<ParmVarDecl>(D)) {
3285 DiagKind = 0;
3286 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3287 if (VD->getStorageClass() == SC_Register)
3288 DiagKind = 1;
3289 if (VD->isExceptionVariable())
3290 DiagKind = 2;
3291 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
3292 if (FD->isBitField())
3293 DiagKind = 3;
3294 } else if (!isa<TagDecl>(D)) {
Richard Smithbe507b62013-02-01 08:12:08 +00003295 Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
3296 << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'")
Richard Smith5f838aa2013-02-01 08:25:07 +00003297 << (TmpAttr.isC11() ? ExpectedVariableOrField
3298 : ExpectedVariableFieldOrTag);
Richard Smith4cd81c52013-01-29 09:02:09 +00003299 return;
3300 }
3301 if (DiagKind != -1) {
Richard Smithbe507b62013-02-01 08:12:08 +00003302 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
Richard Smith671b3212013-02-22 04:55:39 +00003303 << TmpAttr.isC11() << DiagKind;
Richard Smith4cd81c52013-01-29 09:02:09 +00003304 return;
3305 }
3306 }
3307
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003308 if (E->isTypeDependent() || E->isValueDependent()) {
3309 // Save dependent expressions in the AST to be instantiated.
Richard Smithf6565a92013-02-22 08:32:16 +00003310 AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr);
3311 AA->setPackExpansion(IsPackExpansion);
3312 D->addAttr(AA);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003313 return;
3314 }
Michael Hana31f65b2013-02-01 01:19:17 +00003315
Sean Huntcf807c42010-08-18 23:23:40 +00003316 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00003317 llvm::APSInt Alignment(32);
Douglas Gregorab41fe92012-05-04 22:38:52 +00003318 ExprResult ICE
3319 = VerifyIntegerConstantExpression(E, &Alignment,
3320 diag::err_aligned_attribute_argument_not_int,
3321 /*AllowFold*/ false);
Richard Smith282e7e62012-02-04 09:53:13 +00003322 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00003323 return;
Richard Smithbe507b62013-02-01 08:12:08 +00003324
3325 // C++11 [dcl.align]p2:
3326 // -- if the constant expression evaluates to zero, the alignment
3327 // specifier shall have no effect
3328 // C11 6.7.5p6:
3329 // An alignment specification of zero has no effect.
3330 if (!(TmpAttr.isAlignas() && !Alignment) &&
3331 !llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00003332 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
3333 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003334 return;
3335 }
Michael Hana31f65b2013-02-01 01:19:17 +00003336
Richard Smithbe507b62013-02-01 08:12:08 +00003337 if (TmpAttr.isDeclspec()) {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003338 // We've already verified it's a power of 2, now let's make sure it's
3339 // 8192 or less.
3340 if (Alignment.getZExtValue() > 8192) {
Michael Hana31f65b2013-02-01 01:19:17 +00003341 Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192)
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00003342 << E->getSourceRange();
3343 return;
3344 }
3345 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00003346
Richard Smithf6565a92013-02-22 08:32:16 +00003347 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true,
3348 ICE.take(), SpellingListIndex);
3349 AA->setPackExpansion(IsPackExpansion);
3350 D->addAttr(AA);
Sean Huntcf807c42010-08-18 23:23:40 +00003351}
3352
Michael Hana31f65b2013-02-01 01:19:17 +00003353void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS,
Richard Smithf6565a92013-02-22 08:32:16 +00003354 unsigned SpellingListIndex, bool IsPackExpansion) {
Sean Huntcf807c42010-08-18 23:23:40 +00003355 // FIXME: Cache the number on the Attr object if non-dependent?
3356 // FIXME: Perform checking of type validity
Richard Smithf6565a92013-02-22 08:32:16 +00003357 AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS,
3358 SpellingListIndex);
3359 AA->setPackExpansion(IsPackExpansion);
3360 D->addAttr(AA);
Chris Lattner6b6b5372008-06-26 18:38:35 +00003361}
Chris Lattnerfbf13472008-06-27 22:18:37 +00003362
Richard Smithbe507b62013-02-01 08:12:08 +00003363void Sema::CheckAlignasUnderalignment(Decl *D) {
3364 assert(D->hasAttrs() && "no attributes on decl");
3365
3366 QualType Ty;
3367 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3368 Ty = VD->getType();
3369 else
3370 Ty = Context.getTagDeclType(cast<TagDecl>(D));
Richard Smith4da09032013-02-22 09:21:42 +00003371 if (Ty->isDependentType() || Ty->isIncompleteType())
Richard Smithbe507b62013-02-01 08:12:08 +00003372 return;
3373
3374 // C++11 [dcl.align]p5, C11 6.7.5/4:
3375 // The combined effect of all alignment attributes in a declaration shall
3376 // not specify an alignment that is less strict than the alignment that
3377 // would otherwise be required for the entity being declared.
3378 AlignedAttr *AlignasAttr = 0;
3379 unsigned Align = 0;
3380 for (specific_attr_iterator<AlignedAttr>
3381 I = D->specific_attr_begin<AlignedAttr>(),
3382 E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) {
3383 if (I->isAlignmentDependent())
3384 return;
3385 if (I->isAlignas())
3386 AlignasAttr = *I;
3387 Align = std::max(Align, I->getAlignment(Context));
3388 }
3389
3390 if (AlignasAttr && Align) {
3391 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3392 CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty);
3393 if (NaturalAlign > RequestedAlign)
3394 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3395 << Ty << (unsigned)NaturalAlign.getQuantity();
3396 }
3397}
3398
Chandler Carruthd309c812011-07-01 23:49:16 +00003399/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00003400/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00003401///
Mike Stumpbf916502009-07-24 19:02:52 +00003402/// Despite what would be logical, the mode attribute is a decl attribute, not a
3403/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
3404/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003405static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003406 // This attribute isn't documented, but glibc uses it. It changes
3407 // the width of an int or unsigned int to the specified size.
Aaron Ballman624421f2013-08-31 01:11:41 +00003408 if (!Attr.isArgIdent(0)) {
Aaron Ballman750f73a2013-07-30 14:29:12 +00003409 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName()
3410 << AANT_ArgumentIdentifier;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003411 return;
3412 }
Aaron Ballman624421f2013-08-31 01:11:41 +00003413
Aaron Ballman624421f2013-08-31 01:11:41 +00003414 IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident;
3415 StringRef Str = Name->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003416
3417 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003418 if (Str.startswith("__") && Str.endswith("__"))
3419 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003420
3421 unsigned DestWidth = 0;
3422 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00003423 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00003424 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00003425 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00003426 switch (Str[0]) {
3427 case 'Q': DestWidth = 8; break;
3428 case 'H': DestWidth = 16; break;
3429 case 'S': DestWidth = 32; break;
3430 case 'D': DestWidth = 64; break;
3431 case 'X': DestWidth = 96; break;
3432 case 'T': DestWidth = 128; break;
3433 }
3434 if (Str[1] == 'F') {
3435 IntegerMode = false;
3436 } else if (Str[1] == 'C') {
3437 IntegerMode = false;
3438 ComplexMode = true;
3439 } else if (Str[1] != 'I') {
3440 DestWidth = 0;
3441 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00003442 break;
3443 case 4:
3444 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
3445 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00003446 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003447 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00003448 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003449 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003450 break;
3451 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00003452 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003453 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003454 break;
Rafael Espindola8e721b72013-01-07 19:58:54 +00003455 case 11:
3456 if (Str == "unwind_word")
Rafael Espindola0b1de542013-01-07 20:01:57 +00003457 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
Rafael Espindola8e721b72013-01-07 19:58:54 +00003458 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003459 }
3460
3461 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00003462 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00003463 OldTy = TD->getUnderlyingType();
3464 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
3465 OldTy = VD->getType();
3466 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00003467 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003468 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00003469 return;
3470 }
Eli Friedman73397492009-03-03 06:41:03 +00003471
John McCall183700f2009-09-21 23:43:11 +00003472 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00003473 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
3474 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003475 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00003476 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3477 } else if (ComplexMode) {
3478 if (!OldTy->isComplexType())
3479 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3480 } else {
3481 if (!OldTy->isFloatingType())
3482 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
3483 }
3484
Mike Stump390b4cc2009-05-16 07:39:55 +00003485 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
3486 // and friends, at least with glibc.
Stepan Dyatkovskiy8e366f02013-09-10 08:37:22 +00003487 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
3488 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00003489 // FIXME: Make sure floating-point mappings are accurate
3490 // FIXME: Support XF and TF types
Stepan Dyatkovskiy8e366f02013-09-10 08:37:22 +00003491 QualType NewTy;
3492 switch (DestWidth) {
3493 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00003494 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003495 return;
Stepan Dyatkovskiy8e366f02013-09-10 08:37:22 +00003496 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00003497 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003498 return;
Stepan Dyatkovskiy8e366f02013-09-10 08:37:22 +00003499 case 8:
3500 if (!IntegerMode) {
3501 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3502 return;
3503 }
3504 if (OldTy->isSignedIntegerType())
3505 NewTy = S.Context.SignedCharTy;
3506 else
3507 NewTy = S.Context.UnsignedCharTy;
3508 break;
3509 case 16:
3510 if (!IntegerMode) {
3511 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3512 return;
3513 }
3514 if (OldTy->isSignedIntegerType())
3515 NewTy = S.Context.ShortTy;
3516 else
3517 NewTy = S.Context.UnsignedShortTy;
3518 break;
3519 case 32:
3520 if (!IntegerMode)
3521 NewTy = S.Context.FloatTy;
3522 else if (OldTy->isSignedIntegerType())
3523 NewTy = S.Context.IntTy;
3524 else
3525 NewTy = S.Context.UnsignedIntTy;
3526 break;
3527 case 64:
3528 if (!IntegerMode)
3529 NewTy = S.Context.DoubleTy;
3530 else if (OldTy->isSignedIntegerType())
3531 if (S.Context.getTargetInfo().getLongWidth() == 64)
3532 NewTy = S.Context.LongTy;
3533 else
3534 NewTy = S.Context.LongLongTy;
3535 else
3536 if (S.Context.getTargetInfo().getLongWidth() == 64)
3537 NewTy = S.Context.UnsignedLongTy;
3538 else
3539 NewTy = S.Context.UnsignedLongLongTy;
3540 break;
3541 case 96:
3542 NewTy = S.Context.LongDoubleTy;
3543 break;
3544 case 128:
3545 if (!IntegerMode && &S.Context.getTargetInfo().getLongDoubleFormat() !=
3546 &llvm::APFloat::PPCDoubleDouble) {
3547 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
3548 return;
3549 }
3550 if (IntegerMode) {
3551 if (OldTy->isSignedIntegerType())
3552 NewTy = S.Context.Int128Ty;
3553 else
3554 NewTy = S.Context.UnsignedInt128Ty;
3555 } else
3556 NewTy = S.Context.LongDoubleTy;
3557 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00003558 }
3559
Eli Friedman73397492009-03-03 06:41:03 +00003560 if (ComplexMode) {
3561 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00003562 }
3563
3564 // Install the new type.
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003565 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
3566 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
3567 else
Chris Lattnerfbf13472008-06-27 22:18:37 +00003568 cast<ValueDecl>(D)->setType(NewTy);
Enea Zaffanellac2fa6b62013-06-20 12:46:19 +00003569
3570 D->addAttr(::new (S.Context)
3571 ModeAttr(Attr.getRange(), S.Context, Name,
3572 Attr.getAttributeSpellingListIndex()));
Chris Lattnerfbf13472008-06-27 22:18:37 +00003573}
Chris Lattner0744e5f2008-06-29 00:23:49 +00003574
Chandler Carruth1b03c872011-07-02 00:01:44 +00003575static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Nick Lewycky78d1a102012-07-24 01:40:49 +00003576 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
3577 if (!VD->hasGlobalStorage())
3578 S.Diag(Attr.getLoc(),
3579 diag::warn_attribute_requires_functions_or_static_globals)
3580 << Attr.getName();
3581 } else if (!isFunctionOrMethod(D)) {
3582 S.Diag(Attr.getLoc(),
3583 diag::warn_attribute_requires_functions_or_static_globals)
3584 << Attr.getName();
Anders Carlssond87df372009-02-13 06:46:13 +00003585 return;
3586 }
Mike Stumpbf916502009-07-24 19:02:52 +00003587
Michael Han51d8c522013-01-24 16:46:58 +00003588 D->addAttr(::new (S.Context)
3589 NoDebugAttr(Attr.getRange(), S.Context,
3590 Attr.getAttributeSpellingListIndex()));
Anders Carlssond87df372009-02-13 06:46:13 +00003591}
3592
Chandler Carruth1b03c872011-07-02 00:01:44 +00003593static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003594 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00003595 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003596 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00003597 return;
3598 }
Mike Stumpbf916502009-07-24 19:02:52 +00003599
Michael Han51d8c522013-01-24 16:46:58 +00003600 D->addAttr(::new (S.Context)
3601 NoInlineAttr(Attr.getRange(), S.Context,
3602 Attr.getAttributeSpellingListIndex()));
Anders Carlsson5bab7882009-02-19 19:16:48 +00003603}
3604
Chandler Carruth1b03c872011-07-02 00:01:44 +00003605static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
3606 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003607 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00003608 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003609 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003610 return;
3611 }
3612
Michael Han51d8c522013-01-24 16:46:58 +00003613 D->addAttr(::new (S.Context)
3614 NoInstrumentFunctionAttr(Attr.getRange(), S.Context,
3615 Attr.getAttributeSpellingListIndex()));
Chris Lattner7255a2d2010-06-22 00:03:40 +00003616}
3617
Chandler Carruth1b03c872011-07-02 00:01:44 +00003618static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003619 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003620 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003621 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003622 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003623 return;
3624 }
3625
Michael Han51d8c522013-01-24 16:46:58 +00003626 D->addAttr(::new (S.Context)
3627 CUDAConstantAttr(Attr.getRange(), S.Context,
3628 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003629 } else {
3630 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
3631 }
3632}
3633
Chandler Carruth1b03c872011-07-02 00:01:44 +00003634static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003635 if (S.LangOpts.CUDA) {
3636 // check the attribute arguments.
3637 if (Attr.getNumArgs() != 0) {
Aaron Ballmanbaec7782013-07-23 19:30:11 +00003638 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
3639 << Attr.getName() << 0;
Peter Collingbourneced76712010-12-01 03:15:31 +00003640 return;
3641 }
3642
Chandler Carruth87c44602011-07-01 23:49:12 +00003643 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003644 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003645 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003646 return;
3647 }
3648
Michael Han51d8c522013-01-24 16:46:58 +00003649 D->addAttr(::new (S.Context)
3650 CUDADeviceAttr(Attr.getRange(), S.Context,
3651 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003652 } else {
3653 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
3654 }
3655}
3656
Chandler Carruth1b03c872011-07-02 00:01:44 +00003657static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003658 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003659 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003660 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003661 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003662 return;
3663 }
3664
Chandler Carruth87c44602011-07-01 23:49:12 +00003665 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003666 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00003667 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +00003668 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003669 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3670 << FD->getType()
David Blaikie39e6ab42013-02-18 22:06:02 +00003671 << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(),
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00003672 "void");
3673 } else {
3674 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
3675 << FD->getType();
3676 }
3677 return;
3678 }
3679
Michael Han51d8c522013-01-24 16:46:58 +00003680 D->addAttr(::new (S.Context)
3681 CUDAGlobalAttr(Attr.getRange(), S.Context,
3682 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003683 } else {
3684 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
3685 }
3686}
3687
Chandler Carruth1b03c872011-07-02 00:01:44 +00003688static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003689 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003690 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003691 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003692 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00003693 return;
3694 }
3695
Michael Han51d8c522013-01-24 16:46:58 +00003696 D->addAttr(::new (S.Context)
3697 CUDAHostAttr(Attr.getRange(), S.Context,
3698 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003699 } else {
3700 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
3701 }
3702}
3703
Chandler Carruth1b03c872011-07-02 00:01:44 +00003704static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003705 if (S.LangOpts.CUDA) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003706 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00003707 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003708 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00003709 return;
3710 }
3711
Michael Han51d8c522013-01-24 16:46:58 +00003712 D->addAttr(::new (S.Context)
3713 CUDASharedAttr(Attr.getRange(), S.Context,
3714 Attr.getAttributeSpellingListIndex()));
Peter Collingbourneced76712010-12-01 03:15:31 +00003715 } else {
3716 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
3717 }
3718}
3719
Chandler Carruth1b03c872011-07-02 00:01:44 +00003720static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003721 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00003722 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00003723 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003724 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00003725 return;
3726 }
Mike Stumpbf916502009-07-24 19:02:52 +00003727
Douglas Gregor0130f3c2009-10-27 21:01:01 +00003728 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00003729 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00003730 return;
3731 }
Mike Stumpbf916502009-07-24 19:02:52 +00003732
Michael Han51d8c522013-01-24 16:46:58 +00003733 D->addAttr(::new (S.Context)
3734 GNUInlineAttr(Attr.getRange(), S.Context,
3735 Attr.getAttributeSpellingListIndex()));
Chris Lattner26e25542009-04-14 16:30:50 +00003736}
3737
Chandler Carruth1b03c872011-07-02 00:01:44 +00003738static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003739 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003740
Aaron Ballmanfff32482012-12-09 17:45:41 +00003741 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Chandler Carruth87c44602011-07-01 23:49:12 +00003742 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003743 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3744 CallingConv CC;
Aaron Ballmanfff32482012-12-09 17:45:41 +00003745 if (S.CheckCallingConvAttr(Attr, CC, FD))
John McCall711c52b2011-01-05 12:14:39 +00003746 return;
3747
Chandler Carruth87c44602011-07-01 23:49:12 +00003748 if (!isa<ObjCMethodDecl>(D)) {
3749 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3750 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003751 return;
3752 }
3753
Chandler Carruth87c44602011-07-01 23:49:12 +00003754 switch (Attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003755 case AttributeList::AT_FastCall:
Michael Han51d8c522013-01-24 16:46:58 +00003756 D->addAttr(::new (S.Context)
3757 FastCallAttr(Attr.getRange(), S.Context,
3758 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003759 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003760 case AttributeList::AT_StdCall:
Michael Han51d8c522013-01-24 16:46:58 +00003761 D->addAttr(::new (S.Context)
3762 StdCallAttr(Attr.getRange(), S.Context,
3763 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003764 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003765 case AttributeList::AT_ThisCall:
Michael Han51d8c522013-01-24 16:46:58 +00003766 D->addAttr(::new (S.Context)
3767 ThisCallAttr(Attr.getRange(), S.Context,
3768 Attr.getAttributeSpellingListIndex()));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003769 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003770 case AttributeList::AT_CDecl:
Michael Han51d8c522013-01-24 16:46:58 +00003771 D->addAttr(::new (S.Context)
3772 CDeclAttr(Attr.getRange(), S.Context,
3773 Attr.getAttributeSpellingListIndex()));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003774 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003775 case AttributeList::AT_Pascal:
Michael Han51d8c522013-01-24 16:46:58 +00003776 D->addAttr(::new (S.Context)
3777 PascalAttr(Attr.getRange(), S.Context,
3778 Attr.getAttributeSpellingListIndex()));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003779 return;
Charles Davise8519c32013-08-30 04:39:01 +00003780 case AttributeList::AT_MSABI:
3781 D->addAttr(::new (S.Context)
3782 MSABIAttr(Attr.getRange(), S.Context,
3783 Attr.getAttributeSpellingListIndex()));
3784 return;
3785 case AttributeList::AT_SysVABI:
3786 D->addAttr(::new (S.Context)
3787 SysVABIAttr(Attr.getRange(), S.Context,
3788 Attr.getAttributeSpellingListIndex()));
3789 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00003790 case AttributeList::AT_Pcs: {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003791 PcsAttr::PCSType PCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003792 switch (CC) {
3793 case CC_AAPCS:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003794 PCS = PcsAttr::AAPCS;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003795 break;
3796 case CC_AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003797 PCS = PcsAttr::AAPCS_VFP;
Benjamin Kramer9071def2012-08-14 13:24:39 +00003798 break;
3799 default:
3800 llvm_unreachable("unexpected calling convention in pcs attribute");
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003801 }
3802
Michael Han51d8c522013-01-24 16:46:58 +00003803 D->addAttr(::new (S.Context)
3804 PcsAttr(Attr.getRange(), S.Context, PCS,
3805 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003806 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003807 }
Derek Schuff263366f2012-10-16 22:30:41 +00003808 case AttributeList::AT_PnaclCall:
Michael Han51d8c522013-01-24 16:46:58 +00003809 D->addAttr(::new (S.Context)
3810 PnaclCallAttr(Attr.getRange(), S.Context,
3811 Attr.getAttributeSpellingListIndex()));
Derek Schuff263366f2012-10-16 22:30:41 +00003812 return;
Guy Benyei38980082012-12-25 08:53:55 +00003813 case AttributeList::AT_IntelOclBicc:
Michael Han51d8c522013-01-24 16:46:58 +00003814 D->addAttr(::new (S.Context)
3815 IntelOclBiccAttr(Attr.getRange(), S.Context,
3816 Attr.getAttributeSpellingListIndex()));
Guy Benyei38980082012-12-25 08:53:55 +00003817 return;
Derek Schuff263366f2012-10-16 22:30:41 +00003818
Abramo Bagnarae215f722010-04-30 13:10:51 +00003819 default:
3820 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003821 }
3822}
3823
Chandler Carruth1b03c872011-07-02 00:01:44 +00003824static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003825 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003826}
3827
Guy Benyei1db70402013-03-24 13:58:12 +00003828static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){
Aaron Ballman624421f2013-08-31 01:11:41 +00003829 Expr *E = Attr.getArgAsExpr(0);
Guy Benyei1db70402013-03-24 13:58:12 +00003830 llvm::APSInt ArgNum(32);
3831 if (E->isTypeDependent() || E->isValueDependent() ||
3832 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003833 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3834 << Attr.getName() << AANT_ArgumentIntegerConstant
3835 << E->getSourceRange();
Guy Benyei1db70402013-03-24 13:58:12 +00003836 return;
3837 }
3838
3839 D->addAttr(::new (S.Context) OpenCLImageAccessAttr(
3840 Attr.getRange(), S.Context, ArgNum.getZExtValue()));
3841}
3842
Aaron Ballmanfff32482012-12-09 17:45:41 +00003843bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC,
3844 const FunctionDecl *FD) {
John McCall711c52b2011-01-05 12:14:39 +00003845 if (attr.isInvalid())
3846 return true;
3847
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003848 unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0;
Aaron Ballman624421f2013-08-31 01:11:41 +00003849 if (!checkAttributeNumArgs(*this, attr, ReqArgs)) {
John McCall711c52b2011-01-05 12:14:39 +00003850 attr.setInvalid();
3851 return true;
3852 }
3853
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003854 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3855 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003856 switch (attr.getKind()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00003857 case AttributeList::AT_CDecl: CC = CC_C; break;
3858 case AttributeList::AT_FastCall: CC = CC_X86FastCall; break;
3859 case AttributeList::AT_StdCall: CC = CC_X86StdCall; break;
3860 case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break;
3861 case AttributeList::AT_Pascal: CC = CC_X86Pascal; break;
Charles Davise8519c32013-08-30 04:39:01 +00003862 case AttributeList::AT_MSABI:
3863 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
3864 CC_X86_64Win64;
3865 break;
3866 case AttributeList::AT_SysVABI:
3867 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
3868 CC_C;
3869 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00003870 case AttributeList::AT_Pcs: {
Aaron Ballman624421f2013-08-31 01:11:41 +00003871 StringLiteral *Str = 0;
3872 if (attr.isArgExpr(0))
3873 Str = dyn_cast<StringLiteral>(attr.getArgAsExpr(0));
3874
Douglas Gregor5cee1192011-07-27 05:40:30 +00003875 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003876 Diag(attr.getLoc(), diag::err_attribute_argument_type) << attr.getName()
3877 << AANT_ArgumentString;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003878 attr.setInvalid();
3879 return true;
3880 }
3881
Chris Lattner5f9e2722011-07-23 10:55:15 +00003882 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003883 if (StrRef == "aapcs") {
3884 CC = CC_AAPCS;
3885 break;
3886 } else if (StrRef == "aapcs-vfp") {
3887 CC = CC_AAPCS_VFP;
3888 break;
3889 }
Benjamin Kramerfac8e432012-08-14 13:13:47 +00003890
3891 attr.setInvalid();
3892 Diag(attr.getLoc(), diag::err_invalid_pcs);
3893 return true;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003894 }
Derek Schuff263366f2012-10-16 22:30:41 +00003895 case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break;
Guy Benyei38980082012-12-25 08:53:55 +00003896 case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break;
David Blaikie7530c032012-01-17 06:56:22 +00003897 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003898 }
3899
Aaron Ballman82bfa192012-10-02 14:26:08 +00003900 const TargetInfo &TI = Context.getTargetInfo();
3901 TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC);
3902 if (A == TargetInfo::CCCR_Warning) {
3903 Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName();
Aaron Ballmanfff32482012-12-09 17:45:41 +00003904
3905 TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown;
3906 if (FD)
3907 MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member :
3908 TargetInfo::CCMT_NonMember;
3909 CC = TI.getDefaultCallingConv(MT);
Aaron Ballman82bfa192012-10-02 14:26:08 +00003910 }
3911
John McCall711c52b2011-01-05 12:14:39 +00003912 return false;
3913}
3914
Chandler Carruth1b03c872011-07-02 00:01:44 +00003915static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003916 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003917
3918 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003919 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003920 return;
3921
Chandler Carruth87c44602011-07-01 23:49:12 +00003922 if (!isa<ObjCMethodDecl>(D)) {
3923 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3924 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003925 return;
3926 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003927
Michael Han51d8c522013-01-24 16:46:58 +00003928 D->addAttr(::new (S.Context)
3929 RegparmAttr(Attr.getRange(), S.Context, numParams,
3930 Attr.getAttributeSpellingListIndex()));
John McCall711c52b2011-01-05 12:14:39 +00003931}
3932
3933/// Checks a regparm attribute, returning true if it is ill-formed and
3934/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003935bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3936 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003937 return true;
3938
Aaron Ballmanffa9d572013-07-18 18:01:48 +00003939 if (!checkAttributeNumArgs(*this, Attr, 1)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003940 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003941 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003942 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003943
Aaron Ballman624421f2013-08-31 01:11:41 +00003944 Expr *NumParamsExpr = Attr.getArgAsExpr(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003945 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003946 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003947 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Aaron Ballman9f939f72013-07-30 14:10:17 +00003948 Diag(Attr.getLoc(), diag::err_attribute_argument_type)
3949 << Attr.getName() << AANT_ArgumentIntegerConstant
3950 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003951 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003952 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003953 }
3954
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003955 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003956 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003957 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003958 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003959 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003960 }
3961
John McCall711c52b2011-01-05 12:14:39 +00003962 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003963 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003964 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003965 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003966 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003967 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003968 }
3969
John McCall711c52b2011-01-05 12:14:39 +00003970 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003971}
3972
Chandler Carruth1b03c872011-07-02 00:01:44 +00003973static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003974 if (S.LangOpts.CUDA) {
3975 // check the attribute arguments.
3976 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003977 // FIXME: 0 is not okay.
3978 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003979 return;
3980 }
3981
Chandler Carruth87c44602011-07-01 23:49:12 +00003982 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003983 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003984 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003985 return;
3986 }
3987
Aaron Ballman624421f2013-08-31 01:11:41 +00003988 Expr *MaxThreadsExpr = Attr.getArgAsExpr(0);
Peter Collingbourne7b381982010-12-12 23:03:07 +00003989 llvm::APSInt MaxThreads(32);
3990 if (MaxThreadsExpr->isTypeDependent() ||
3991 MaxThreadsExpr->isValueDependent() ||
3992 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00003993 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00003994 << Attr.getName() << 1 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00003995 << MaxThreadsExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00003996 return;
3997 }
3998
3999 llvm::APSInt MinBlocks(32);
4000 if (Attr.getNumArgs() > 1) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004001 Expr *MinBlocksExpr = Attr.getArgAsExpr(1);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004002 if (MinBlocksExpr->isTypeDependent() ||
4003 MinBlocksExpr->isValueDependent() ||
4004 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004005 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004006 << Attr.getName() << 2 << AANT_ArgumentIntegerConstant
Aaron Ballman437d43f2013-07-23 14:03:57 +00004007 << MinBlocksExpr->getSourceRange();
Peter Collingbourne7b381982010-12-12 23:03:07 +00004008 return;
4009 }
4010 }
4011
Michael Han51d8c522013-01-24 16:46:58 +00004012 D->addAttr(::new (S.Context)
4013 CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
4014 MaxThreads.getZExtValue(),
4015 MinBlocks.getZExtValue(),
4016 Attr.getAttributeSpellingListIndex()));
Peter Collingbourne7b381982010-12-12 23:03:07 +00004017 } else {
4018 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
4019 }
4020}
4021
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004022static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
4023 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004024 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004025 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004026 << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004027 return;
4028 }
Aaron Ballman624421f2013-08-31 01:11:41 +00004029
4030 if (!checkAttributeNumArgs(S, Attr, 3))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004031 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004032
Aaron Ballman624421f2013-08-31 01:11:41 +00004033 StringRef AttrName = Attr.getName()->getName();
4034 IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004035
4036 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
4037 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
4038 << Attr.getName() << ExpectedFunctionOrMethod;
4039 return;
4040 }
4041
4042 uint64_t ArgumentIdx;
4043 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4044 Attr.getLoc(), 2,
Aaron Ballman624421f2013-08-31 01:11:41 +00004045 Attr.getArgAsExpr(1), ArgumentIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004046 return;
4047
4048 uint64_t TypeTagIdx;
4049 if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName,
4050 Attr.getLoc(), 3,
Aaron Ballman624421f2013-08-31 01:11:41 +00004051 Attr.getArgAsExpr(2), TypeTagIdx))
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004052 return;
4053
4054 bool IsPointer = (AttrName == "pointer_with_type_tag");
4055 if (IsPointer) {
4056 // Ensure that buffer has a pointer type.
4057 QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx);
4058 if (!BufferTy->isPointerType()) {
4059 S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only)
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004060 << Attr.getName();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004061 }
4062 }
4063
Michael Han51d8c522013-01-24 16:46:58 +00004064 D->addAttr(::new (S.Context)
4065 ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind,
4066 ArgumentIdx, TypeTagIdx, IsPointer,
4067 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004068}
4069
4070static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D,
4071 const AttributeList &Attr) {
Aaron Ballman624421f2013-08-31 01:11:41 +00004072 if (!Attr.isArgIdent(0)) {
Aaron Ballman437d43f2013-07-23 14:03:57 +00004073 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type)
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004074 << Attr.getName() << 1 << AANT_ArgumentIdentifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004075 return;
4076 }
Aaron Ballman624421f2013-08-31 01:11:41 +00004077
4078 if (!checkAttributeNumArgs(S, Attr, 1))
4079 return;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004080
Aaron Ballman624421f2013-08-31 01:11:41 +00004081 IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004082 QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL);
4083
Michael Han51d8c522013-01-24 16:46:58 +00004084 D->addAttr(::new (S.Context)
4085 TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind,
4086 MatchingCType,
4087 Attr.getLayoutCompatible(),
4088 Attr.getMustBeNull(),
4089 Attr.getAttributeSpellingListIndex()));
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004090}
4091
Chris Lattner0744e5f2008-06-29 00:23:49 +00004092//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00004093// Checker-specific attribute handlers.
4094//===----------------------------------------------------------------------===//
4095
John McCallc7ad3812011-01-25 03:31:58 +00004096static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004097 return type->isDependentType() ||
4098 type->isObjCObjectPointerType() ||
4099 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00004100}
4101static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00004102 return type->isDependentType() ||
4103 type->isPointerType() ||
4104 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00004105}
4106
Chandler Carruth1b03c872011-07-02 00:01:44 +00004107static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004108 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00004109 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004110 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004111 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00004112 return;
4113 }
4114
4115 bool typeOK, cf;
Sean Hunt8e083e72012-06-19 23:57:03 +00004116 if (Attr.getKind() == AttributeList::AT_NSConsumed) {
John McCallc7ad3812011-01-25 03:31:58 +00004117 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
4118 cf = false;
4119 } else {
4120 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
4121 cf = true;
4122 }
4123
4124 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004125 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004126 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00004127 return;
4128 }
4129
4130 if (cf)
Michael Han51d8c522013-01-24 16:46:58 +00004131 param->addAttr(::new (S.Context)
4132 CFConsumedAttr(Attr.getRange(), S.Context,
4133 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004134 else
Michael Han51d8c522013-01-24 16:46:58 +00004135 param->addAttr(::new (S.Context)
4136 NSConsumedAttr(Attr.getRange(), S.Context,
4137 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004138}
4139
Chandler Carruth1b03c872011-07-02 00:01:44 +00004140static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
4141 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004142 if (!isa<ObjCMethodDecl>(D)) {
4143 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004144 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00004145 return;
4146 }
4147
Michael Han51d8c522013-01-24 16:46:58 +00004148 D->addAttr(::new (S.Context)
4149 NSConsumesSelfAttr(Attr.getRange(), S.Context,
4150 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004151}
4152
Chandler Carruth1b03c872011-07-02 00:01:44 +00004153static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
4154 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004155
John McCallc7ad3812011-01-25 03:31:58 +00004156 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00004157
Chandler Carruth87c44602011-07-01 23:49:12 +00004158 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004159 returnType = MD->getResultType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004160 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Sean Hunt8e083e72012-06-19 23:57:03 +00004161 (Attr.getKind() == AttributeList::AT_NSReturnsRetained))
John McCallf85e1932011-06-15 23:02:42 +00004162 return; // ignore: was handled as a type attribute
Fariborz Jahaniana23bd4c2012-08-28 22:26:21 +00004163 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
4164 returnType = PD->getType();
Chandler Carruth87c44602011-07-01 23:49:12 +00004165 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00004166 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004167 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00004168 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004169 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00004170 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004171 return;
4172 }
Mike Stumpbf916502009-07-24 19:02:52 +00004173
John McCallc7ad3812011-01-25 03:31:58 +00004174 bool typeOK;
4175 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00004176 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00004177 default: llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004178 case AttributeList::AT_NSReturnsAutoreleased:
4179 case AttributeList::AT_NSReturnsRetained:
4180 case AttributeList::AT_NSReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004181 typeOK = isValidSubjectOfNSAttribute(S, returnType);
4182 cf = false;
4183 break;
4184
Sean Hunt8e083e72012-06-19 23:57:03 +00004185 case AttributeList::AT_CFReturnsRetained:
4186 case AttributeList::AT_CFReturnsNotRetained:
John McCallc7ad3812011-01-25 03:31:58 +00004187 typeOK = isValidSubjectOfCFAttribute(S, returnType);
4188 cf = true;
4189 break;
4190 }
4191
4192 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004193 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004194 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00004195 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00004196 }
Mike Stumpbf916502009-07-24 19:02:52 +00004197
Chandler Carruth87c44602011-07-01 23:49:12 +00004198 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00004199 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00004200 llvm_unreachable("invalid ownership attribute");
Sean Hunt8e083e72012-06-19 23:57:03 +00004201 case AttributeList::AT_NSReturnsAutoreleased:
Michael Han51d8c522013-01-24 16:46:58 +00004202 D->addAttr(::new (S.Context)
4203 NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context,
4204 Attr.getAttributeSpellingListIndex()));
John McCallc7ad3812011-01-25 03:31:58 +00004205 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004206 case AttributeList::AT_CFReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004207 D->addAttr(::new (S.Context)
4208 CFReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4209 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004210 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004211 case AttributeList::AT_NSReturnsNotRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004212 D->addAttr(::new (S.Context)
4213 NSReturnsNotRetainedAttr(Attr.getRange(), S.Context,
4214 Attr.getAttributeSpellingListIndex()));
Ted Kremenek31c780d2010-02-18 00:05:45 +00004215 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004216 case AttributeList::AT_CFReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004217 D->addAttr(::new (S.Context)
4218 CFReturnsRetainedAttr(Attr.getRange(), S.Context,
4219 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004220 return;
Sean Hunt8e083e72012-06-19 23:57:03 +00004221 case AttributeList::AT_NSReturnsRetained:
Michael Han51d8c522013-01-24 16:46:58 +00004222 D->addAttr(::new (S.Context)
4223 NSReturnsRetainedAttr(Attr.getRange(), S.Context,
4224 Attr.getAttributeSpellingListIndex()));
Ted Kremenekb71368d2009-05-09 02:44:38 +00004225 return;
4226 };
4227}
4228
John McCalldc7c5ad2011-07-22 08:53:00 +00004229static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
4230 const AttributeList &attr) {
4231 SourceLocation loc = attr.getLoc();
4232
4233 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4234
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00004235 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00004236 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004237 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00004238 return;
4239 }
4240
4241 // Check that the method returns a normal pointer.
4242 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00004243
4244 if (!resultType->isReferenceType() &&
4245 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00004246 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
4247 << SourceRange(loc)
4248 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
4249
4250 // Drop the attribute.
4251 return;
4252 }
4253
Michael Han51d8c522013-01-24 16:46:58 +00004254 method->addAttr(::new (S.Context)
4255 ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context,
4256 attr.getAttributeSpellingListIndex()));
John McCalldc7c5ad2011-07-22 08:53:00 +00004257}
4258
Fariborz Jahanian84101132012-09-07 23:46:23 +00004259static void handleObjCRequiresSuperAttr(Sema &S, Decl *D,
4260 const AttributeList &attr) {
4261 SourceLocation loc = attr.getLoc();
4262 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
4263
4264 if (!method) {
4265 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
4266 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
4267 return;
4268 }
4269 DeclContext *DC = method->getDeclContext();
4270 if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) {
4271 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4272 << attr.getName() << 0;
4273 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
4274 return;
4275 }
4276 if (method->getMethodFamily() == OMF_dealloc) {
4277 S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol)
4278 << attr.getName() << 1;
4279 return;
4280 }
4281
Michael Han51d8c522013-01-24 16:46:58 +00004282 method->addAttr(::new (S.Context)
4283 ObjCRequiresSuperAttr(attr.getRange(), S.Context,
4284 attr.getAttributeSpellingListIndex()));
Fariborz Jahanian84101132012-09-07 23:46:23 +00004285}
4286
John McCall8dfac0b2011-09-30 05:12:12 +00004287/// Handle cf_audited_transfer and cf_unknown_transfer.
4288static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
4289 if (!isa<FunctionDecl>(D)) {
4290 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004291 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00004292 return;
4293 }
4294
Sean Hunt8e083e72012-06-19 23:57:03 +00004295 bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer);
John McCall8dfac0b2011-09-30 05:12:12 +00004296
4297 // Check whether there's a conflicting attribute already present.
4298 Attr *Existing;
4299 if (IsAudited) {
4300 Existing = D->getAttr<CFUnknownTransferAttr>();
4301 } else {
4302 Existing = D->getAttr<CFAuditedTransferAttr>();
4303 }
4304 if (Existing) {
4305 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
4306 << A.getName()
4307 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
4308 << A.getRange() << Existing->getRange();
4309 return;
4310 }
4311
4312 // All clear; add the attribute.
4313 if (IsAudited) {
Michael Han51d8c522013-01-24 16:46:58 +00004314 D->addAttr(::new (S.Context)
4315 CFAuditedTransferAttr(A.getRange(), S.Context,
4316 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004317 } else {
Michael Han51d8c522013-01-24 16:46:58 +00004318 D->addAttr(::new (S.Context)
4319 CFUnknownTransferAttr(A.getRange(), S.Context,
4320 A.getAttributeSpellingListIndex()));
John McCall8dfac0b2011-09-30 05:12:12 +00004321 }
4322}
4323
John McCallfe98da02011-09-29 07:17:38 +00004324static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
4325 const AttributeList &Attr) {
4326 RecordDecl *RD = dyn_cast<RecordDecl>(D);
4327 if (!RD || RD->isUnion()) {
4328 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004329 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00004330 }
4331
Aaron Ballman624421f2013-08-31 01:11:41 +00004332 IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
John McCallfe98da02011-09-29 07:17:38 +00004333
4334 // In Objective-C, verify that the type names an Objective-C type.
4335 // We don't want to check this outside of ObjC because people sometimes
4336 // do crazy C declarations of Objective-C types.
Aaron Ballman624421f2013-08-31 01:11:41 +00004337 if (Parm && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00004338 // Check for an existing type with this name.
Aaron Ballman624421f2013-08-31 01:11:41 +00004339 LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc,
John McCallfe98da02011-09-29 07:17:38 +00004340 Sema::LookupOrdinaryName);
4341 if (S.LookupName(R, Sc)) {
4342 NamedDecl *Target = R.getFoundDecl();
4343 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
4344 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
4345 S.Diag(Target->getLocStart(), diag::note_declared_at);
4346 }
4347 }
4348 }
4349
Michael Han51d8c522013-01-24 16:46:58 +00004350 D->addAttr(::new (S.Context)
Aaron Ballman624421f2013-08-31 01:11:41 +00004351 NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
Michael Han51d8c522013-01-24 16:46:58 +00004352 Attr.getAttributeSpellingListIndex()));
John McCallfe98da02011-09-29 07:17:38 +00004353}
4354
Chandler Carruth1b03c872011-07-02 00:01:44 +00004355static void handleObjCOwnershipAttr(Sema &S, Decl *D,
4356 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004357 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00004358
Chandler Carruth87c44602011-07-01 23:49:12 +00004359 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004360 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004361}
4362
Chandler Carruth1b03c872011-07-02 00:01:44 +00004363static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
4364 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004365 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004366 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00004367 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00004368 return;
4369 }
4370
Chandler Carruth87c44602011-07-01 23:49:12 +00004371 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00004372 QualType type = vd->getType();
4373
4374 if (!type->isDependentType() &&
4375 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00004376 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00004377 << type;
4378 return;
4379 }
4380
4381 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
4382
4383 // If we have no lifetime yet, check the lifetime we're presumably
4384 // going to infer.
4385 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
4386 lifetime = type->getObjCARCImplicitLifetime();
4387
4388 switch (lifetime) {
4389 case Qualifiers::OCL_None:
4390 assert(type->isDependentType() &&
4391 "didn't infer lifetime for non-dependent type?");
4392 break;
4393
4394 case Qualifiers::OCL_Weak: // meaningful
4395 case Qualifiers::OCL_Strong: // meaningful
4396 break;
4397
4398 case Qualifiers::OCL_ExplicitNone:
4399 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00004400 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00004401 << (lifetime == Qualifiers::OCL_Autoreleasing);
4402 break;
4403 }
4404
Chandler Carruth87c44602011-07-01 23:49:12 +00004405 D->addAttr(::new (S.Context)
Michael Han51d8c522013-01-24 16:46:58 +00004406 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context,
4407 Attr.getAttributeSpellingListIndex()));
John McCallf85e1932011-06-15 23:02:42 +00004408}
4409
Francois Pichet11542142010-12-19 06:50:37 +00004410//===----------------------------------------------------------------------===//
4411// Microsoft specific attribute handlers.
4412//===----------------------------------------------------------------------===//
4413
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004414// Check if MS extensions or some other language extensions are enabled. If
4415// not, issue a diagnostic that the given attribute is unused.
4416static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr,
4417 bool OtherExtension = false) {
4418 if (S.LangOpts.MicrosoftExt || OtherExtension)
4419 return true;
4420 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
4421 return false;
4422}
4423
Chandler Carruth1b03c872011-07-02 00:01:44 +00004424static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004425 if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland))
4426 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00004427
Aaron Ballman624421f2013-08-31 01:11:41 +00004428 Expr *Arg = Attr.getArgAsExpr(0);
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004429 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
4430 if (!Str || !Str->isAscii()) {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00004431 S.Diag(Attr.getLoc(), diag::err_attribute_argument_type)
4432 << Attr.getName() << AANT_ArgumentString;
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004433 return;
4434 }
Francois Pichetd3d3be92010-12-20 01:41:49 +00004435
David Majnemerbb0ed292013-08-09 08:56:20 +00004436 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
4437 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004438 StringRef StrRef = Str->getString();
David Majnemerbb0ed292013-08-09 08:56:20 +00004439 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
4440 StrRef = StrRef.drop_front().drop_back();
Francois Pichetd3d3be92010-12-20 01:41:49 +00004441
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004442 // Validate GUID length.
David Majnemerbb0ed292013-08-09 08:56:20 +00004443 if (StrRef.size() != 36) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004444 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4445 return;
4446 }
Anders Carlssonf89e0422011-01-23 21:07:30 +00004447
David Majnemerbb0ed292013-08-09 08:56:20 +00004448 for (unsigned i = 0; i < 36; ++i) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004449 if (i == 8 || i == 13 || i == 18 || i == 23) {
David Majnemerbb0ed292013-08-09 08:56:20 +00004450 if (StrRef[i] != '-') {
Francois Pichetd3d3be92010-12-20 01:41:49 +00004451 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4452 return;
4453 }
David Majnemerbb0ed292013-08-09 08:56:20 +00004454 } else if (!isHexDigit(StrRef[i])) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004455 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
4456 return;
Francois Pichetd3d3be92010-12-20 01:41:49 +00004457 }
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004458 }
Francois Pichet11542142010-12-19 06:50:37 +00004459
David Majnemerbb0ed292013-08-09 08:56:20 +00004460 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef,
4461 Attr.getAttributeSpellingListIndex()));
Charles Davisf0122fe2010-02-16 18:27:26 +00004462}
4463
John McCallc052dbb2012-05-22 21:28:12 +00004464static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004465 if (!checkMicrosoftExt(S, Attr))
Nico Weber7b89ab72012-11-07 21:31:36 +00004466 return;
Nico Weber7b89ab72012-11-07 21:31:36 +00004467
4468 AttributeList::Kind Kind = Attr.getKind();
4469 if (Kind == AttributeList::AT_SingleInheritance)
4470 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004471 ::new (S.Context)
4472 SingleInheritanceAttr(Attr.getRange(), S.Context,
4473 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004474 else if (Kind == AttributeList::AT_MultipleInheritance)
4475 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004476 ::new (S.Context)
4477 MultipleInheritanceAttr(Attr.getRange(), S.Context,
4478 Attr.getAttributeSpellingListIndex()));
Nico Weber7b89ab72012-11-07 21:31:36 +00004479 else if (Kind == AttributeList::AT_VirtualInheritance)
4480 D->addAttr(
Michael Han51d8c522013-01-24 16:46:58 +00004481 ::new (S.Context)
4482 VirtualInheritanceAttr(Attr.getRange(), S.Context,
4483 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004484}
4485
4486static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004487 if (!checkMicrosoftExt(S, Attr))
4488 return;
4489
4490 AttributeList::Kind Kind = Attr.getKind();
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004491 if (Kind == AttributeList::AT_Win64)
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004492 D->addAttr(
4493 ::new (S.Context) Win64Attr(Attr.getRange(), S.Context,
4494 Attr.getAttributeSpellingListIndex()));
John McCallc052dbb2012-05-22 21:28:12 +00004495}
4496
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004497static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Reid Kleckner8fbda8e2013-05-17 14:04:52 +00004498 if (!checkMicrosoftExt(S, Attr))
4499 return;
4500 D->addAttr(::new (S.Context)
4501 ForceInlineAttr(Attr.getRange(), S.Context,
4502 Attr.getAttributeSpellingListIndex()));
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004503}
4504
Reid Klecknera7225342013-05-20 14:02:37 +00004505static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
4506 if (!checkMicrosoftExt(S, Attr))
4507 return;
4508 // Check linkage after possibly merging declaratinos. See
4509 // checkAttributesAfterMerging().
4510 D->addAttr(::new (S.Context)
4511 SelectAnyAttr(Attr.getRange(), S.Context,
4512 Attr.getAttributeSpellingListIndex()));
4513}
4514
Aaron Ballmanbbb3b322013-09-09 23:33:17 +00004515/// Handles semantic checking for features that are common to all attributes,
4516/// such as checking whether a parameter was properly specified, or the correct
4517/// number of arguments were passed, etc.
4518static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D,
4519 const AttributeList &Attr) {
4520 // Several attributes carry different semantics than the parsing requires, so
4521 // those are opted out of the common handling.
4522 //
4523 // We also bail on unknown and ignored attributes because those are handled
4524 // as part of the target-specific handling logic.
4525 if (Attr.hasCustomParsing() ||
4526 Attr.getKind() == AttributeList::UnknownAttribute ||
4527 Attr.getKind() == AttributeList::IgnoredAttribute)
4528 return false;
4529
4530 // If there are no optional arguments, then checking for the argument count
4531 // is trivial.
4532 if (Attr.getMinArgs() == Attr.getMaxArgs() &&
4533 !checkAttributeNumArgs(S, Attr, Attr.getMinArgs()))
4534 return true;
4535 return false;
4536}
4537
Ted Kremenekb71368d2009-05-09 02:44:38 +00004538//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00004539// Top Level Sema Entry Points
4540//===----------------------------------------------------------------------===//
4541
Richard Smith4a97b8e2013-08-29 00:47:48 +00004542/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
4543/// the attribute applies to decls. If the attribute is a type attribute, just
4544/// silently ignore it if a GNU attribute.
4545static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
4546 const AttributeList &Attr,
4547 bool IncludeCXX11Attributes) {
4548 if (Attr.isInvalid())
4549 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00004550
Richard Smith4a97b8e2013-08-29 00:47:48 +00004551 // Ignore C++11 attributes on declarator chunks: they appertain to the type
4552 // instead.
4553 if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes)
4554 return;
4555
Aaron Ballmanbbb3b322013-09-09 23:33:17 +00004556 if (handleCommonAttributeFeatures(S, scope, D, Attr))
4557 return;
4558
Chris Lattner803d0802008-06-29 00:43:07 +00004559 switch (Attr.getKind()) {
Richard Smithcd8ab512013-01-17 01:30:42 +00004560 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
4561 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
4562 case AttributeList::AT_IBOutletCollection:
4563 handleIBOutletCollection(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004564 case AttributeList::AT_AddressSpace:
Sean Hunt8e083e72012-06-19 23:57:03 +00004565 case AttributeList::AT_ObjCGC:
4566 case AttributeList::AT_VectorSize:
4567 case AttributeList::AT_NeonVectorType:
4568 case AttributeList::AT_NeonPolyVectorType:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004569 case AttributeList::AT_Ptr32:
4570 case AttributeList::AT_Ptr64:
4571 case AttributeList::AT_SPtr:
4572 case AttributeList::AT_UPtr:
Mike Stumpbf916502009-07-24 19:02:52 +00004573 // Ignore these, these are type attributes, handled by
4574 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00004575 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004576 case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break;
4577 case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break;
4578 case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break;
4579 case AttributeList::AT_AlwaysInline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004580 handleAlwaysInlineAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004581 case AttributeList::AT_AnalyzerNoReturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004582 handleAnalyzerNoReturnAttr (S, D, Attr); break;
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00004583 case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004584 case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break;
4585 case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break;
4586 case AttributeList::AT_CarriesDependency:
Richard Smith3a2b7a12013-01-28 22:42:45 +00004587 handleDependencyAttr(S, scope, D, Attr);
4588 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004589 case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break;
4590 case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break;
4591 case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break;
Richard Smithcd8ab512013-01-17 01:30:42 +00004592 case AttributeList::AT_CXX11NoReturn:
4593 handleCXX11NoReturnAttr(S, D, Attr);
4594 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004595 case AttributeList::AT_Deprecated:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004596 handleAttrWithMessage<DeprecatedAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004597 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004598 case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break;
4599 case AttributeList::AT_ExtVectorType:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004600 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004601 break;
Quentin Colombetaee56fa2012-11-01 23:55:47 +00004602 case AttributeList::AT_MinSize:
4603 handleMinSizeAttr(S, D, Attr);
4604 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004605 case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break;
4606 case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break;
4607 case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break;
Aaron Ballman19513232013-08-28 23:13:26 +00004608 case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break;
4609 case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004610 case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break;
4611 case AttributeList::AT_CUDALaunchBounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004612 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00004613 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004614 case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break;
4615 case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004616 case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004617 case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break;
4618 case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break;
Richard Smith4a97b8e2013-08-29 00:47:48 +00004619 case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00004620 case AttributeList::AT_ownership_returns:
4621 case AttributeList::AT_ownership_takes:
4622 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004623 handleOwnershipAttr (S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004624 case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break;
4625 case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break;
4626 case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break;
4627 case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break;
4628 case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break;
4629 case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break;
4630 case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004631
Sean Hunt8e083e72012-06-19 23:57:03 +00004632 case AttributeList::AT_ObjCOwnership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004633 handleObjCOwnershipAttr(S, D, Attr); break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004634 case AttributeList::AT_ObjCPreciseLifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004635 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00004636
Sean Hunt8e083e72012-06-19 23:57:03 +00004637 case AttributeList::AT_ObjCReturnsInnerPointer:
John McCalldc7c5ad2011-07-22 08:53:00 +00004638 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
4639
Fariborz Jahanian84101132012-09-07 23:46:23 +00004640 case AttributeList::AT_ObjCRequiresSuper:
4641 handleObjCRequiresSuperAttr(S, D, Attr); break;
4642
Sean Hunt8e083e72012-06-19 23:57:03 +00004643 case AttributeList::AT_NSBridged:
John McCallfe98da02011-09-29 07:17:38 +00004644 handleNSBridgedAttr(S, scope, D, Attr); break;
4645
Sean Hunt8e083e72012-06-19 23:57:03 +00004646 case AttributeList::AT_CFAuditedTransfer:
4647 case AttributeList::AT_CFUnknownTransfer:
John McCall8dfac0b2011-09-30 05:12:12 +00004648 handleCFTransferAttr(S, D, Attr); break;
4649
Ted Kremenekb71368d2009-05-09 02:44:38 +00004650 // Checker-specific.
Sean Hunt8e083e72012-06-19 23:57:03 +00004651 case AttributeList::AT_CFConsumed:
4652 case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break;
4653 case AttributeList::AT_NSConsumesSelf:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004654 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00004655
Sean Hunt8e083e72012-06-19 23:57:03 +00004656 case AttributeList::AT_NSReturnsAutoreleased:
4657 case AttributeList::AT_NSReturnsNotRetained:
4658 case AttributeList::AT_CFReturnsNotRetained:
4659 case AttributeList::AT_NSReturnsRetained:
4660 case AttributeList::AT_CFReturnsRetained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004661 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00004662
Tanya Lattner0df579e2012-07-09 22:06:01 +00004663 case AttributeList::AT_WorkGroupSizeHint:
Sean Hunt8e083e72012-06-19 23:57:03 +00004664 case AttributeList::AT_ReqdWorkGroupSize:
Tanya Lattner0df579e2012-07-09 22:06:01 +00004665 handleWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00004666
Joey Gouly37453b92013-03-08 09:42:32 +00004667 case AttributeList::AT_VecTypeHint:
4668 handleVecTypeHint(S, D, Attr); break;
4669
Sean Hunt8e083e72012-06-19 23:57:03 +00004670 case AttributeList::AT_InitPriority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004671 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00004672
Sean Hunt8e083e72012-06-19 23:57:03 +00004673 case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break;
4674 case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break;
4675 case AttributeList::AT_Unavailable:
Aaron Ballman2dbdef22013-07-18 13:13:52 +00004676 handleAttrWithMessage<UnavailableAttr>(S, D, Attr);
Benjamin Kramerbc3260d2012-05-16 12:19:08 +00004677 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004678 case AttributeList::AT_ArcWeakrefUnavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00004679 handleArcWeakrefUnavailableAttr (S, D, Attr);
4680 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004681 case AttributeList::AT_ObjCRootClass:
Patrick Beardb2f68202012-04-06 18:12:22 +00004682 handleObjCRootClassAttr(S, D, Attr);
4683 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004684 case AttributeList::AT_ObjCRequiresPropertyDefs:
Ted Kremenek71207fc2012-01-05 22:47:47 +00004685 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00004686 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004687 case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break;
4688 case AttributeList::AT_ReturnsTwice:
Rafael Espindolaf87cced2011-10-03 14:59:42 +00004689 handleReturnsTwiceAttr(S, D, Attr);
4690 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004691 case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break;
John McCalld4c3d662013-02-20 01:54:26 +00004692 case AttributeList::AT_Visibility:
4693 handleVisibilityAttr(S, D, Attr, false);
4694 break;
4695 case AttributeList::AT_TypeVisibility:
4696 handleVisibilityAttr(S, D, Attr, true);
4697 break;
Lubos Lunak1d3ce652013-07-20 15:05:36 +00004698 case AttributeList::AT_WarnUnused:
4699 handleWarnUnusedAttr(S, D, Attr);
4700 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004701 case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00004702 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004703 case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break;
4704 case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break;
4705 case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break;
4706 case AttributeList::AT_TransparentUnion:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004707 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00004708 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004709 case AttributeList::AT_ObjCException:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004710 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00004711 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004712 case AttributeList::AT_ObjCMethodFamily:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004713 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00004714 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004715 case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break;
4716 case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break;
4717 case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break;
4718 case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break;
4719 case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break;
4720 case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break;
4721 case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break;
4722 case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break;
4723 case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00004724 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00004725 // Just ignore
4726 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004727 case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00004728 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00004729 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004730 case AttributeList::AT_StdCall:
4731 case AttributeList::AT_CDecl:
4732 case AttributeList::AT_FastCall:
4733 case AttributeList::AT_ThisCall:
4734 case AttributeList::AT_Pascal:
Charles Davise8519c32013-08-30 04:39:01 +00004735 case AttributeList::AT_MSABI:
4736 case AttributeList::AT_SysVABI:
Sean Hunt8e083e72012-06-19 23:57:03 +00004737 case AttributeList::AT_Pcs:
Derek Schuff263366f2012-10-16 22:30:41 +00004738 case AttributeList::AT_PnaclCall:
Guy Benyei38980082012-12-25 08:53:55 +00004739 case AttributeList::AT_IntelOclBicc:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004740 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00004741 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004742 case AttributeList::AT_OpenCLKernel:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004743 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00004744 break;
Guy Benyei1db70402013-03-24 13:58:12 +00004745 case AttributeList::AT_OpenCLImageAccess:
4746 handleOpenCLImageAccessAttr(S, D, Attr);
4747 break;
John McCallc052dbb2012-05-22 21:28:12 +00004748
4749 // Microsoft attributes:
Sean Hunt8e083e72012-06-19 23:57:03 +00004750 case AttributeList::AT_MsStruct:
John McCallc052dbb2012-05-22 21:28:12 +00004751 handleMsStructAttr(S, D, Attr);
4752 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004753 case AttributeList::AT_Uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00004754 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00004755 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004756 case AttributeList::AT_SingleInheritance:
4757 case AttributeList::AT_MultipleInheritance:
4758 case AttributeList::AT_VirtualInheritance:
John McCallc052dbb2012-05-22 21:28:12 +00004759 handleInheritanceAttr(S, D, Attr);
4760 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004761 case AttributeList::AT_Win64:
John McCallc052dbb2012-05-22 21:28:12 +00004762 handlePortabilityAttr(S, D, Attr);
4763 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004764 case AttributeList::AT_ForceInline:
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00004765 handleForceInlineAttr(S, D, Attr);
4766 break;
Reid Klecknera7225342013-05-20 14:02:37 +00004767 case AttributeList::AT_SelectAny:
4768 handleSelectAnyAttr(S, D, Attr);
4769 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004770
4771 // Thread safety attributes:
DeLesley Hutchins5c6134f2013-05-17 23:02:59 +00004772 case AttributeList::AT_AssertExclusiveLock:
4773 handleAssertExclusiveLockAttr(S, D, Attr);
4774 break;
4775 case AttributeList::AT_AssertSharedLock:
4776 handleAssertSharedLockAttr(S, D, Attr);
4777 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004778 case AttributeList::AT_GuardedVar:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004779 handleGuardedVarAttr(S, D, Attr);
4780 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004781 case AttributeList::AT_PtGuardedVar:
Michael Handc691572012-07-23 18:48:41 +00004782 handlePtGuardedVarAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004783 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004784 case AttributeList::AT_ScopedLockable:
Michael Handc691572012-07-23 18:48:41 +00004785 handleScopedLockableAttr(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004786 break;
Kostya Serebryany85aee962013-02-26 06:58:27 +00004787 case AttributeList::AT_NoSanitizeAddress:
4788 handleNoSanitizeAddressAttr(S, D, Attr);
Kostya Serebryany71efba02012-01-24 19:25:38 +00004789 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004790 case AttributeList::AT_NoThreadSafetyAnalysis:
Kostya Serebryany85aee962013-02-26 06:58:27 +00004791 handleNoThreadSafetyAnalysis(S, D, Attr);
4792 break;
4793 case AttributeList::AT_NoSanitizeThread:
4794 handleNoSanitizeThread(S, D, Attr);
4795 break;
4796 case AttributeList::AT_NoSanitizeMemory:
4797 handleNoSanitizeMemory(S, D, Attr);
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004798 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004799 case AttributeList::AT_Lockable:
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004800 handleLockableAttr(S, D, Attr);
4801 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004802 case AttributeList::AT_GuardedBy:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004803 handleGuardedByAttr(S, D, Attr);
4804 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004805 case AttributeList::AT_PtGuardedBy:
Michael Handc691572012-07-23 18:48:41 +00004806 handlePtGuardedByAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004807 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004808 case AttributeList::AT_ExclusiveLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004809 handleExclusiveLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004810 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004811 case AttributeList::AT_ExclusiveLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004812 handleExclusiveLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004813 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004814 case AttributeList::AT_ExclusiveTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004815 handleExclusiveTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004816 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004817 case AttributeList::AT_LockReturned:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004818 handleLockReturnedAttr(S, D, Attr);
4819 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004820 case AttributeList::AT_LocksExcluded:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004821 handleLocksExcludedAttr(S, D, Attr);
4822 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004823 case AttributeList::AT_SharedLockFunction:
Michael Handc691572012-07-23 18:48:41 +00004824 handleSharedLockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004825 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004826 case AttributeList::AT_SharedLocksRequired:
Michael Handc691572012-07-23 18:48:41 +00004827 handleSharedLocksRequiredAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004828 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004829 case AttributeList::AT_SharedTrylockFunction:
Michael Handc691572012-07-23 18:48:41 +00004830 handleSharedTrylockFunctionAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004831 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004832 case AttributeList::AT_UnlockFunction:
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004833 handleUnlockFunAttr(S, D, Attr);
4834 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004835 case AttributeList::AT_AcquiredBefore:
Michael Handc691572012-07-23 18:48:41 +00004836 handleAcquiredBeforeAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004837 break;
Sean Hunt8e083e72012-06-19 23:57:03 +00004838 case AttributeList::AT_AcquiredAfter:
Michael Handc691572012-07-23 18:48:41 +00004839 handleAcquiredAfterAttr(S, D, Attr);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00004840 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00004841
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00004842 // Uniqueness analysis attributes.
DeLesley Hutchinsc55bee62013-08-30 22:56:34 +00004843 case AttributeList::AT_Consumable:
4844 handleConsumableAttr(S, D, Attr);
4845 break;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00004846 case AttributeList::AT_Consumes:
4847 handleConsumesAttr(S, D, Attr);
4848 break;
4849 case AttributeList::AT_CallableWhenUnconsumed:
4850 handleCallableWhenUnconsumedAttr(S, D, Attr);
4851 break;
4852 case AttributeList::AT_TestsConsumed:
4853 handleTestsConsumedAttr(S, D, Attr);
4854 break;
4855 case AttributeList::AT_TestsUnconsumed:
4856 handleTestsUnconsumedAttr(S, D, Attr);
4857 break;
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00004858 case AttributeList::AT_ReturnTypestate:
4859 handleReturnTypestateAttr(S, D, Attr);
4860 break;
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00004861
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00004862 // Type safety attributes.
4863 case AttributeList::AT_ArgumentWithTypeTag:
4864 handleArgumentWithTypeTagAttr(S, D, Attr);
4865 break;
4866 case AttributeList::AT_TypeTagForDatatype:
4867 handleTypeTagForDatatypeAttr(S, D, Attr);
4868 break;
4869
Chris Lattner803d0802008-06-29 00:43:07 +00004870 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004871 // Ask target about the attribute.
4872 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
4873 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Aaron Ballmanfc685ac2012-06-19 22:09:27 +00004874 S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ?
4875 diag::warn_unhandled_ms_attribute_ignored :
4876 diag::warn_unknown_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00004877 break;
4878 }
4879}
4880
4881/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
4882/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00004883void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00004884 const AttributeList *AttrList,
Richard Smithcd8ab512013-01-17 01:30:42 +00004885 bool IncludeCXX11Attributes) {
4886 for (const AttributeList* l = AttrList; l; l = l->getNext())
Richard Smith4a97b8e2013-08-29 00:47:48 +00004887 ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004888
4889 // GCC accepts
4890 // static int a9 __attribute__((weakref));
4891 // but that looks really pointless. We reject it.
Richard Smith4a97b8e2013-08-29 00:47:48 +00004892 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004893 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Rafael Espindola4d8a33b2013-01-16 23:49:06 +00004894 cast<NamedDecl>(D)->getNameAsString();
4895 D->dropAttr<WeakRefAttr>();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00004896 return;
Chris Lattner803d0802008-06-29 00:43:07 +00004897 }
4898}
4899
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004900// Annotation attributes are the only attributes allowed after an access
4901// specifier.
4902bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
4903 const AttributeList *AttrList) {
4904 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Sean Hunt8e083e72012-06-19 23:57:03 +00004905 if (l->getKind() == AttributeList::AT_Annotate) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00004906 handleAnnotateAttr(*this, ASDecl, *l);
4907 } else {
4908 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
4909 return true;
4910 }
4911 }
4912
4913 return false;
4914}
4915
John McCalle82247a2011-10-01 05:17:03 +00004916/// checkUnusedDeclAttributes - Check a list of attributes to see if it
4917/// contains any decl attributes that we should warn about.
4918static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
4919 for ( ; A; A = A->getNext()) {
4920 // Only warn if the attribute is an unignored, non-type attribute.
Richard Smithd03de6a2013-01-29 10:02:16 +00004921 if (A->isUsedAsTypeAttr() || A->isInvalid()) continue;
John McCalle82247a2011-10-01 05:17:03 +00004922 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
4923
4924 if (A->getKind() == AttributeList::UnknownAttribute) {
4925 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
4926 << A->getName() << A->getRange();
4927 } else {
4928 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
4929 << A->getName() << A->getRange();
4930 }
4931 }
4932}
4933
4934/// checkUnusedDeclAttributes - Given a declarator which is not being
4935/// used to build a declaration, complain about any decl attributes
4936/// which might be lying around on it.
4937void Sema::checkUnusedDeclAttributes(Declarator &D) {
4938 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
4939 ::checkUnusedDeclAttributes(*this, D.getAttributes());
4940 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
4941 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
4942}
4943
Ryan Flynne25ff832009-07-30 03:15:39 +00004944/// DeclClonePragmaWeak - clone existing decl (maybe definition),
James Dennett1dfbd922012-06-14 21:40:34 +00004945/// \#pragma weak needs a non-definition decl and source may not have one.
Eli Friedman900693b2011-09-07 04:05:06 +00004946NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
4947 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004948 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00004949 NamedDecl *NewD = 0;
4950 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00004951 FunctionDecl *NewFD;
4952 // FIXME: Missing call to CheckFunctionDeclaration().
4953 // FIXME: Mangling?
4954 // FIXME: Is the qualifier info correct?
4955 // FIXME: Is the DeclContext correct?
4956 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
4957 Loc, Loc, DeclarationName(II),
4958 FD->getType(), FD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00004959 SC_None, false/*isInlineSpecified*/,
Eli Friedman900693b2011-09-07 04:05:06 +00004960 FD->hasPrototype(),
4961 false/*isConstexprSpecified*/);
4962 NewD = NewFD;
4963
4964 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004965 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00004966
4967 // Fake up parameter variables; they are declared as if this were
4968 // a typedef.
4969 QualType FDTy = FD->getType();
4970 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
4971 SmallVector<ParmVarDecl*, 16> Params;
4972 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
4973 AE = FT->arg_type_end(); AI != AE; ++AI) {
4974 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
4975 Param->setScopeInfo(0, Params.size());
4976 Params.push_back(Param);
4977 }
David Blaikie4278c652011-09-21 18:16:56 +00004978 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00004979 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004980 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
4981 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004982 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00004983 VD->getType(), VD->getTypeSourceInfo(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00004984 VD->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00004985 if (VD->getQualifier()) {
4986 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004987 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00004988 }
Ryan Flynne25ff832009-07-30 03:15:39 +00004989 }
4990 return NewD;
4991}
4992
James Dennett1dfbd922012-06-14 21:40:34 +00004993/// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
Ryan Flynne25ff832009-07-30 03:15:39 +00004994/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00004995void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00004996 if (W.getUsed()) return; // only do this once
4997 W.setUsed(true);
4998 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
4999 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00005000 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00005001 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
5002 NDId->getName()));
5003 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00005004 WeakTopLevelDecl.push_back(NewD);
5005 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
5006 // to insert Decl at TU scope, sorry.
5007 DeclContext *SavedContext = CurContext;
5008 CurContext = Context.getTranslationUnitDecl();
5009 PushOnScopeChains(NewD, S);
5010 CurContext = SavedContext;
5011 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00005012 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00005013 }
5014}
5015
Rafael Espindola65611bf2013-03-02 21:41:48 +00005016void Sema::ProcessPragmaWeak(Scope *S, Decl *D) {
5017 // It's valid to "forward-declare" #pragma weak, in which case we
5018 // have to do this.
5019 LoadExternalWeakUndeclaredIdentifiers();
5020 if (!WeakUndeclaredIdentifiers.empty()) {
5021 NamedDecl *ND = NULL;
5022 if (VarDecl *VD = dyn_cast<VarDecl>(D))
5023 if (VD->isExternC())
5024 ND = VD;
5025 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5026 if (FD->isExternC())
5027 ND = FD;
5028 if (ND) {
5029 if (IdentifierInfo *Id = ND->getIdentifier()) {
5030 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
5031 = WeakUndeclaredIdentifiers.find(Id);
5032 if (I != WeakUndeclaredIdentifiers.end()) {
5033 WeakInfo W = I->second;
5034 DeclApplyPragmaWeak(S, ND, W);
5035 WeakUndeclaredIdentifiers[Id] = W;
5036 }
5037 }
5038 }
5039 }
5040}
5041
Chris Lattner0744e5f2008-06-29 00:23:49 +00005042/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
5043/// it, apply them to D. This is a bit tricky because PD can have attributes
5044/// specified in many different places, and we need to find and apply them all.
Richard Smith4a97b8e2013-08-29 00:47:48 +00005045void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Chris Lattner0744e5f2008-06-29 00:23:49 +00005046 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00005047 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005048 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00005049
Chris Lattner0744e5f2008-06-29 00:23:49 +00005050 // Walk the declarator structure, applying decl attributes that were in a type
5051 // position to the decl itself. This handles cases like:
5052 // int *__attr__(x)** D;
5053 // when X is a decl attribute.
5054 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
5055 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005056 ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false);
Mike Stumpbf916502009-07-24 19:02:52 +00005057
Chris Lattner0744e5f2008-06-29 00:23:49 +00005058 // Finally, apply any attributes on the decl itself.
5059 if (const AttributeList *Attrs = PD.getAttributes())
Richard Smith4a97b8e2013-08-29 00:47:48 +00005060 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00005061}
John McCall54abf7d2009-11-04 02:18:39 +00005062
John McCallf85e1932011-06-15 23:02:42 +00005063/// Is the given declaration allowed to use a forbidden type?
5064static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
5065 // Private ivars are always okay. Unfortunately, people don't
5066 // always properly make their ivars private, even in system headers.
5067 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00005068 // Function declarations in sys headers will be marked unavailable.
5069 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
5070 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00005071 return false;
5072
5073 // Require it to be declared in a system header.
5074 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
5075}
5076
5077/// Handle a delayed forbidden-type diagnostic.
5078static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
5079 Decl *decl) {
5080 if (decl && isForbiddenTypeAllowed(S, decl)) {
5081 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
5082 "this system declaration uses an unsupported type"));
5083 return;
5084 }
David Blaikie4e4d0842012-03-11 07:00:24 +00005085 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005086 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
Benjamin Kramer48d798c2012-06-02 10:20:41 +00005087 // FIXME: we may want to suppress diagnostics for all
Fariborz Jahanian175fb102011-10-03 22:11:57 +00005088 // kind of forbidden type messages on unavailable functions.
5089 if (FD->hasAttr<UnavailableAttr>() &&
5090 diag.getForbiddenTypeDiagnostic() ==
5091 diag::err_arc_array_param_no_ownership) {
5092 diag.Triggered = true;
5093 return;
5094 }
5095 }
John McCallf85e1932011-06-15 23:02:42 +00005096
5097 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
5098 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
5099 diag.Triggered = true;
5100}
5101
John McCall92576642012-05-07 06:16:41 +00005102void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) {
5103 assert(DelayedDiagnostics.getCurrentPool());
John McCall13489672012-05-07 06:16:58 +00005104 DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool();
John McCall92576642012-05-07 06:16:41 +00005105 DelayedDiagnostics.popWithoutEmitting(state);
John McCalleee1d542011-02-14 07:13:47 +00005106
John McCall92576642012-05-07 06:16:41 +00005107 // When delaying diagnostics to run in the context of a parsed
5108 // declaration, we only want to actually emit anything if parsing
5109 // succeeds.
5110 if (!decl) return;
John McCalleee1d542011-02-14 07:13:47 +00005111
John McCall92576642012-05-07 06:16:41 +00005112 // We emit all the active diagnostics in this pool or any of its
5113 // parents. In general, we'll get one pool for the decl spec
5114 // and a child pool for each declarator; in a decl group like:
5115 // deprecated_typedef foo, *bar, baz();
5116 // only the declarator pops will be passed decls. This is correct;
5117 // we really do need to consider delayed diagnostics from the decl spec
5118 // for each of the different declarations.
John McCall13489672012-05-07 06:16:58 +00005119 const DelayedDiagnosticPool *pool = &poppedPool;
John McCall92576642012-05-07 06:16:41 +00005120 do {
John McCall13489672012-05-07 06:16:58 +00005121 for (DelayedDiagnosticPool::pool_iterator
John McCall92576642012-05-07 06:16:41 +00005122 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
5123 // This const_cast is a bit lame. Really, Triggered should be mutable.
5124 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
John McCalleee1d542011-02-14 07:13:47 +00005125 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00005126 continue;
5127
John McCalleee1d542011-02-14 07:13:47 +00005128 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00005129 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00005130 // Don't bother giving deprecation diagnostics if the decl is invalid.
5131 if (!decl->isInvalidDecl())
John McCall92576642012-05-07 06:16:41 +00005132 HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005133 break;
5134
5135 case DelayedDiagnostic::Access:
John McCall92576642012-05-07 06:16:41 +00005136 HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00005137 break;
John McCallf85e1932011-06-15 23:02:42 +00005138
5139 case DelayedDiagnostic::ForbiddenType:
John McCall92576642012-05-07 06:16:41 +00005140 handleDelayedForbiddenType(*this, diag, decl);
John McCallf85e1932011-06-15 23:02:42 +00005141 break;
John McCall2f514482010-01-27 03:50:35 +00005142 }
5143 }
John McCall92576642012-05-07 06:16:41 +00005144 } while ((pool = pool->getParent()));
John McCall54abf7d2009-11-04 02:18:39 +00005145}
5146
John McCall13489672012-05-07 06:16:58 +00005147/// Given a set of delayed diagnostics, re-emit them as if they had
5148/// been delayed in the current context instead of in the given pool.
5149/// Essentially, this just moves them to the current pool.
5150void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) {
5151 DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool();
5152 assert(curPool && "re-emitting in undelayed context not supported");
5153 curPool->steal(pool);
5154}
5155
John McCall54abf7d2009-11-04 02:18:39 +00005156static bool isDeclDeprecated(Decl *D) {
5157 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005158 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00005159 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00005160 // A category implicitly has the availability of the interface.
5161 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
5162 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00005163 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
5164 return false;
5165}
5166
Eli Friedmanc3b23082012-08-08 21:52:41 +00005167static void
5168DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message,
5169 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005170 const ObjCInterfaceDecl *UnknownObjCClass,
5171 const ObjCPropertyDecl *ObjCPropery) {
Eli Friedmanc3b23082012-08-08 21:52:41 +00005172 DeclarationName Name = D->getDeclName();
5173 if (!Message.empty()) {
5174 S.Diag(Loc, diag::warn_deprecated_message) << Name << Message;
5175 S.Diag(D->getLocation(),
5176 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5177 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005178 if (ObjCPropery)
5179 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5180 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005181 } else if (!UnknownObjCClass) {
5182 S.Diag(Loc, diag::warn_deprecated) << D->getDeclName();
5183 S.Diag(D->getLocation(),
5184 isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at
5185 : diag::note_previous_decl) << Name;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005186 if (ObjCPropery)
5187 S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute)
5188 << ObjCPropery->getDeclName() << 0;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005189 } else {
5190 S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name;
5191 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
5192 }
5193}
5194
John McCall9c3087b2010-08-26 02:13:20 +00005195void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00005196 Decl *Ctx) {
5197 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00005198 return;
5199
John McCall2f514482010-01-27 03:50:35 +00005200 DD.Triggered = true;
Eli Friedmanc3b23082012-08-08 21:52:41 +00005201 DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(),
5202 DD.getDeprecationMessage(), DD.Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005203 DD.getUnknownObjCClass(),
5204 DD.getObjCProperty());
John McCall54abf7d2009-11-04 02:18:39 +00005205}
5206
Chris Lattner5f9e2722011-07-23 10:55:15 +00005207void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00005208 SourceLocation Loc,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005209 const ObjCInterfaceDecl *UnknownObjCClass,
5210 const ObjCPropertyDecl *ObjCProperty) {
John McCall54abf7d2009-11-04 02:18:39 +00005211 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00005212 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005213 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
5214 UnknownObjCClass,
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005215 ObjCProperty,
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00005216 Message));
John McCall54abf7d2009-11-04 02:18:39 +00005217 return;
5218 }
5219
5220 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00005221 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00005222 return;
Fariborz Jahanianfd090882012-09-21 20:46:37 +00005223 DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty);
John McCall54abf7d2009-11-04 02:18:39 +00005224}